LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
casts.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010, 2011 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 #ifndef NDARRAY_casts_h_INCLUDED
24 #define NDARRAY_casts_h_INCLUDED
25 
32 #include "ndarray/Array.h"
33 #include "ndarray/ArrayRef.h"
34 #include <boost/type_traits/add_const.hpp>
35 #include <boost/type_traits/remove_const.hpp>
36 #include <boost/mpl/comparison.hpp>
37 #include <boost/utility/enable_if.hpp>
38 #include <boost/static_assert.hpp>
39 
40 namespace ndarray {
41 namespace detail {
42 
43 template <typename Array_>
46  typedef typename boost::remove_const<ComplexElement>::type ComplexValue;
47  typedef typename ExpressionTraits<Array_>::ND ND;
48  BOOST_STATIC_ASSERT( boost::is_complex<ComplexValue>::value );
49  typedef typename ComplexValue::value_type RealValue;
50  typedef typename boost::mpl::if_<
51  boost::is_const<ComplexElement>, RealValue const, RealValue
52  >::type RealElement;
56 
57  static inline Result apply(Array_ const & array, int offset) {
58  return Access::construct(
59  reinterpret_cast<RealElement*>(array.getData()) + offset,
60  Access::Core::create(array.getShape(), array.getStrides() * 2, array.getManager())
61  );
62  }
63 };
64 
65 } // namespace detail
66 
69 
74 template <typename T_, typename T, int N, int C>
75 Array<T_,N,C>
77  return detail::ArrayAccess< Array<T_,N,C> >::construct(
78  const_cast<T_*>(array.getData()),
79  detail::ArrayAccess< Array<T,N,C> >::getCore(array)
80  );
81 }
82 
87 template <int C_, typename T, int N, int C>
88 Array<T,N,C_>
90  return detail::ArrayAccess< Array<T,N,C_> >::construct(
91  array.getData(),
92  detail::ArrayAccess< Array<T,N,C> >::getCore(array)
93  );
94 }
95 
103 template <int C_, typename T, int N, int C>
104 Array<T,N,C_>
106  Vector<int,N> shape = array.getShape();
107  Vector<int,N> strides = array.getStrides();
108  if (C_ >= 0) {
109  int n = 1;
110  for (int i=1; i <= C_; ++i) {
111  if (strides[N-i] != n) return Array<T,N,C_>();
112  n *= shape[N-i];
113  }
114  } else {
115  int n = 1;
116  for (int i=0; i < -C_; ++i) {
117  if (strides[i] != n) return Array<T,N,C_>();
118  n *= strides[i];
119  }
120  }
121  return static_dimension_cast<C_>(array);
122 }
123 
127 template <typename Array_>
128 typename detail::ComplexExtractor<Array_>::Result
129 getReal(Array_ const & array) {
131 }
132 
136 template <typename Array_>
137 typename detail::ComplexExtractor<Array_>::Result
138 getImag(Array_ const & array) {
140 }
141 
148 template <int Nf, typename T, int N, int C>
149 inline typename boost::enable_if_c< ((C+Nf-N)>=1), ArrayRef<T,Nf,(C+Nf-N)> >::type
150 flatten(Array<T,N,C> const & input) {
151  typedef detail::ArrayAccess< ArrayRef<T,Nf,(C+Nf-N)> > Access;
152  typedef typename Access::Core Core;
153  BOOST_STATIC_ASSERT(C+Nf-N >= 1);
154  Vector<int,N> oldShape = input.getShape();
155  Vector<int,Nf> newShape = oldShape.template first<Nf>();
156  for (int n=Nf; n<N; ++n)
157  newShape[Nf-1] *= oldShape[n];
158  Vector<int,Nf> newStrides = input.getStrides().template first<Nf>();
159  newStrides[Nf-1] = 1;
160  return Access::construct(input.getData(), Core::create(newShape, newStrides, input.getManager()));
161 }
162 
169 template <int Nf, typename T, int N, int C>
170 inline typename boost::enable_if_c< ((C+Nf-N)>=1), ArrayRef<T,Nf,(C+Nf-N)> >::type
171 flatten(ArrayRef<T,N,C> const & input) {
172  return flatten<Nf>(input.shallow());
173 }
174 
176 
177 } // namespace ndarray
178 
179 #endif // !NDARRAY_casts_h_INCLUDED
Vector< int, ND::value > Index
Definition: casts.h:55
ComplexValue::value_type RealValue
Definition: casts.h:49
A proxy class for Array with deep assignment operators.
Definition: ArrayRef.h:46
Definitions for ArrayRef.
detail::ArrayAccess< Result > Access
Definition: casts.h:54
Index getStrides() const
Return a Vector of the strides of all dimensions.
Definition: ArrayBase.h:139
static Result apply(Array_ const &array, int offset)
Definition: casts.h:57
Array< T_, N, C > const_array_cast(Array< T, N, C > const &array)
Definition: casts.h:76
Array< T, N, C_ > dynamic_dimension_cast(Array< T, N, C > const &array)
Definition: casts.h:105
static Array_ construct(Element *data, CorePtr const &core)
Definition: ArrayAccess.h:48
Definitions for Array.
Array< T, N, C_ > static_dimension_cast(Array< T, N, C > const &array)
Definition: casts.h:89
A fixed-size 1D array class.
Definition: Vector.h:93
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition: ArrayBase.h:136
boost::remove_const< ComplexElement >::type ComplexValue
Definition: casts.h:46
ExpressionTraits< Array_ >::ND ND
Definition: casts.h:47
detail::ComplexExtractor< Array_ >::Result getReal(Array_ const &array)
Return an ArrayRef view into the real part of a complex array.
Definition: casts.h:129
Traits for expressions.
detail::ComplexExtractor< Array_ >::Result getImag(Array_ const &array)
Return an ArrayRef view into the imaginary part of a complex array.
Definition: casts.h:138
boost::enable_if_c< ((C+Nf-N)>=1), ArrayRef< T, Nf,(C+Nf-N)> >::type flatten(Array< T, N, C > const &input)
Create a view into an array with trailing contiguous dimensions merged.
Definition: casts.h:150
Element * getData() const
Return a raw pointer to the first element of the array.
Definition: ArrayBase.h:117
boost::mpl::if_< boost::is_const< ComplexElement >, RealValue const, RealValue >::type RealElement
Definition: casts.h:52
A multidimensional strided array.
Definition: Array.h:47
ExpressionTraits< Array_ >::Element ComplexElement
Definition: casts.h:45
Shallow const shallow() const
Return a Array view to this.
Definition: ArrayBase.h:175
BOOST_STATIC_ASSERT(boost::is_complex< ComplexValue >::value)
ArrayRef< RealElement, ND::value, 0 > Result
Definition: casts.h:53
Manager::Ptr getManager() const
Return the opaque object responsible for memory management.
Definition: ArrayBase.h:123