ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
casts.h
Go to the documentation of this file.
1// -*- c++ -*-
2/*
3 * Copyright (c) 2010-2012, Jim Bosch
4 * All rights reserved.
5 *
6 * ndarray is distributed under a simple BSD-like license;
7 * see the LICENSE file that should be present in the root
8 * of the source distribution, or alternately available at:
9 * https://github.com/ndarray/ndarray
10 */
11#ifndef NDARRAY_casts_h_INCLUDED
12#define NDARRAY_casts_h_INCLUDED
13
20#include "ndarray/Array.h"
21#include "ndarray/ArrayRef.h"
22#include <boost/type_traits/add_const.hpp>
23#include <boost/type_traits/remove_const.hpp>
24#include <boost/mpl/comparison.hpp>
25#include <boost/utility/enable_if.hpp>
26#include <boost/static_assert.hpp>
27
28namespace ndarray {
29namespace detail {
30
31template <typename Array_>
33 typedef typename ExpressionTraits<Array_>::Element ComplexElement;
34 typedef typename boost::remove_const<ComplexElement>::type ComplexValue;
35 typedef typename ExpressionTraits<Array_>::ND ND;
36 BOOST_STATIC_ASSERT( boost::is_complex<ComplexValue>::value );
37 typedef typename ComplexValue::value_type RealValue;
38 typedef typename boost::mpl::if_<
39 boost::is_const<ComplexElement>, RealValue const, RealValue
40 >::type RealElement;
44
45 static inline Result apply(Array_ const & array, Offset offset) {
46 return Access::construct(
47 reinterpret_cast<RealElement*>(array.getData()) + offset,
48 Access::Core::create(array.getShape(), array.getStrides() * 2, array.getManager())
49 );
50 }
51};
52
53} // namespace detail
54
57
62template <typename T_, typename T, int N, int C>
65 return detail::ArrayAccess< Array<T_,N,C> >::construct(
66 const_cast<T_*>(array.getData()),
67 detail::ArrayAccess< Array<T,N,C> >::getCore(array)
68 );
69}
70
75template <int C_, typename T, int N, int C>
76Array<T,N,C_>
78 return detail::ArrayAccess< Array<T,N,C_> >::construct(
79 array.getData(),
80 detail::ArrayAccess< Array<T,N,C> >::getCore(array)
81 );
82}
83
91template <int C_, typename T, int N, int C>
92Array<T,N,C_>
94 Vector<Size,N> shape = array.getShape();
95 Vector<Offset,N> strides = array.getStrides();
96 if (C_ >= 0) {
97 Offset n = 1;
98 for (int i=1; i <= C_; ++i) {
99 if (strides[N-i] != n) return Array<T,N,C_>();
100 n *= shape[N-i];
101 }
102 } else {
103 Offset n = 1;
104 for (int i=0; i < -C_; ++i) {
105 if (strides[i] != n) return Array<T,N,C_>();
106 n *= strides[i];
107 }
108 }
109 return static_dimension_cast<C_>(array);
110}
111
115template <typename Array_>
116typename detail::ComplexExtractor<Array_>::Result
117getReal(Array_ const & array) {
119}
120
124template <typename Array_>
125typename detail::ComplexExtractor<Array_>::Result
126getImag(Array_ const & array) {
128}
129
136template <int Nf, typename T, int N, int C>
137inline typename boost::enable_if_c< ((C+Nf-N)>=1), ArrayRef<T,Nf,(C+Nf-N)> >::type
138flatten(Array<T,N,C> const & input) {
139 typedef detail::ArrayAccess< ArrayRef<T,Nf,(C+Nf-N)> > Access;
140 typedef typename Access::Core Core;
141 BOOST_STATIC_ASSERT(C+Nf-N >= 1);
142 Vector<Size,N> oldShape = input.getShape();
143 Vector<Size,Nf> newShape = oldShape.template first<Nf>();
144 for (int n=Nf; n<N; ++n)
145 newShape[Nf-1] *= oldShape[n];
146 Vector<Offset,Nf> newStrides = input.getStrides().template first<Nf>();
147 newStrides[Nf-1] = 1;
148 return Access::construct(input.getData(), Core::create(newShape, newStrides, input.getManager()));
149}
150
157template <int Nf, typename T, int N, int C>
158inline typename boost::enable_if_c< ((C+Nf-N)>=1), ArrayRef<T,Nf,(C+Nf-N)> >::type
159flatten(ArrayRef<T,N,C> const & input) {
160 return flatten<Nf>(input.shallow());
161}
162
164
165} // namespace ndarray
166
167#endif // !NDARRAY_casts_h_INCLUDED
Definitions for ArrayRef.
Definitions for Array.
Manager::Ptr getManager() const
Return the opaque object responsible for memory management.
Definition ArrayBase.h:136
Shallow const shallow() const
Return a Array view to this.
Definition ArrayBase.h:188
Element * getData() const
Return a raw pointer to the first element of the array.
Definition ArrayBase.h:130
Strides getStrides() const
Return a Vector of the strides of all dimensions.
Definition ArrayBase.h:152
Index getShape() const
Return a Vector of the sizes of all dimensions.
Definition ArrayBase.h:149
A proxy class for Array with deep assignment operators.
Definition ArrayRef.h:34
A multidimensional strided array.
Definition Array.h:35
Definition ArrayAccess.h:26
Array< T, N, C_ > dynamic_dimension_cast(Array< T, N, C > const &array)
Definition casts.h:93
Array< T, N, C_ > static_dimension_cast(Array< T, N, C > const &array)
Definition casts.h:77
detail::ComplexExtractor< Array_ >::Result getImag(Array_ const &array)
Return an ArrayRef view into the imaginary part of a complex array.
Definition casts.h:126
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:138
detail::ComplexExtractor< Array_ >::Result getReal(Array_ const &array)
Return an ArrayRef view into the real part of a complex array.
Definition casts.h:117
Array< T_, N, C > const_array_cast(Array< T, N, C > const &array)
Definition casts.h:64
Traits for expressions.
Definition ExpressionTraits.h:30
A fixed-size 1D array class.
Definition Vector.h:82