GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
MArray.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2018 John W. Eaton
4 Copyright (C) 2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if ! defined (octave_MArray_h)
25 #define octave_MArray_h 1
26 
27 #include "octave-config.h"
28 
29 #include "Array.h"
30 #include "mx-inlines.cc"
31 
32 template <typename T> class MArray;
33 
34 template <typename T> MArray<T>& operator += (MArray<T>&, const T&);
35 template <typename T> MArray<T>& operator -= (MArray<T>&, const T&);
36 template <typename T> MArray<T>& operator *= (MArray<T>&, const T&);
37 template <typename T> MArray<T>& operator /= (MArray<T>&, const T&);
38 template <typename T> MArray<T>& operator += (MArray<T>&, const MArray<T>&);
39 template <typename T> MArray<T>& operator -= (MArray<T>&, const MArray<T>&);
40 template <typename T> MArray<T>& product_eq (MArray<T>&, const MArray<T>&);
41 template <typename T> MArray<T>& quotient_eq (MArray<T>&, const MArray<T>&);
42 template <typename T> MArray<T> operator + (const MArray<T>&);
43 template <typename T> MArray<T> operator - (const MArray<T>&);
44 template <typename T> MArray<T> operator + (const MArray<T>&, const T&);
45 template <typename T> MArray<T> operator - (const MArray<T>&, const T&);
46 template <typename T> MArray<T> operator * (const MArray<T>&, const T&);
47 template <typename T> MArray<T> operator / (const MArray<T>&, const T&);
48 template <typename T> MArray<T> operator + (const T&, const MArray<T>&);
49 template <typename T> MArray<T> operator - (const T&, const MArray<T>&);
50 template <typename T> MArray<T> operator * (const T&, const MArray<T>&);
51 template <typename T> MArray<T> operator / (const T&, const MArray<T>&);
52 template <typename T> MArray<T> operator + (const MArray<T>&, const MArray<T>&);
53 template <typename T> MArray<T> operator - (const MArray<T>&, const MArray<T>&);
54 template <typename T> MArray<T> quotient (const MArray<T>&, const MArray<T>&);
55 template <typename T> MArray<T> product (const MArray<T>&, const MArray<T>&);
56 
57 //! Template for N-dimensional array classes with like-type math operators.
58 template <typename T>
59 class
60 MArray : public Array<T>
61 {
62 protected:
63 
64  // For jit support
65  MArray (T *sdata, octave_idx_type slen, octave_idx_type *adims, void *arep)
66  : Array<T> (sdata, slen, adims, arep) { }
67 
68 public:
69 
70  MArray (void) : Array<T> () { }
71 
72  explicit MArray (const dim_vector& dv)
73  : Array<T> (dv) { }
74 
75  explicit MArray (const dim_vector& dv, const T& val)
76  : Array<T> (dv, val) { }
77 
78  MArray (const MArray<T>& a) : Array<T> (a) { }
79 
80  template <typename U>
81  MArray (const Array<U>& a) : Array<T> (a) { }
82 
83  ~MArray (void) = default;
84 
86  {
88  return *this;
89  }
90 
91  MArray<T> reshape (const dim_vector& new_dims) const
92  { return Array<T>::reshape (new_dims); }
93 
95  bool inv = false) const
96  { return Array<T>::permute (vec, inv); }
97 
99  { return Array<T>::ipermute (vec); }
100 
101  MArray squeeze (void) const { return Array<T>::squeeze (); }
102 
103  MArray<T> transpose (void) const
104  { return Array<T>::transpose (); }
105 
106  MArray<T> hermitian (T (*fcn) (const T&) = nullptr) const
107  { return Array<T>::hermitian (fcn); }
108 
109  //! Performs indexed accumulative addition.
110  //@{
111  void idx_add (const idx_vector& idx, T val);
112  void idx_add (const idx_vector& idx, const MArray<T>& vals);
113  //@}
114 
115  void idx_min (const idx_vector& idx, const MArray<T>& vals);
116 
117  void idx_max (const idx_vector& idx, const MArray<T>& vals);
118 
119  void idx_add_nd (const idx_vector& idx, const MArray<T>& vals, int dim = -1);
120 
121  void changesign (void);
122 };
123 
124 // Define all the MArray forwarding functions for return type R and
125 // MArray element type T
126 #define MARRAY_FORWARD_DEFS(B, R, T) \
127  inline R operator += (R& x, const T& y) \
128  { \
129  return R (operator += (dynamic_cast<B<T>&> (x), (y))); \
130  } \
131  inline R operator -= (R& x, const T& y) \
132  { \
133  return R (operator -= (dynamic_cast<B<T>&> (x), (y))); \
134  } \
135  inline R operator *= (R& x, const T& y) \
136  { \
137  return R (operator *= (dynamic_cast<B<T>&> (x), (y))); \
138  } \
139  inline R operator /= (R& x, const T& y) \
140  { \
141  return R (operator /= (dynamic_cast<B<T>&> (x), (y))); \
142  } \
143  inline R operator += (R& x, const R& y) \
144  { \
145  return R (operator += (dynamic_cast<B<T>&> (x), \
146  dynamic_cast<const B<T>&> (y))); \
147  } \
148  inline R operator -= (R& x, const R& y) \
149  { \
150  return R (operator -= (dynamic_cast<B<T>&> (x), \
151  dynamic_cast<const B<T>&> (y))); \
152  } \
153  inline R product_eq (R& x, const R& y) \
154  { \
155  return R (product_eq (dynamic_cast<B<T>&> (x), \
156  dynamic_cast<const B<T>&> (y))); \
157  } \
158  inline R quotient_eq (R& x, const R& y) \
159  { \
160  return R (quotient_eq (dynamic_cast<B<T>&> (x), \
161  dynamic_cast<const B<T>&> (y))); \
162  } \
163  inline R operator + (const R& x) \
164  { \
165  return R (operator + (dynamic_cast<const B<T>&> (x))); \
166  } \
167  inline R operator - (const R& x) \
168  { \
169  return R (operator - (dynamic_cast<const B<T>&> (x))); \
170  } \
171  inline R operator + (const R& x, const T& y) \
172  { \
173  return R (operator + (dynamic_cast<const B<T>&> (x), (y))); \
174  } \
175  inline R operator - (const R& x, const T& y) \
176  { \
177  return R (operator - (dynamic_cast<const B<T>&> (x), (y))); \
178  } \
179  inline R operator * (const R& x, const T& y) \
180  { \
181  return R (operator * (dynamic_cast<const B<T>&> (x), (y))); \
182  } \
183  inline R operator / (const R& x, const T& y) \
184  { \
185  return R (operator / (dynamic_cast<const B<T>&> (x), (y))); \
186  } \
187  inline R operator + (const T& x, const R& y) \
188  { \
189  return R (operator + ( (x), dynamic_cast<const B<T>&> (y))); \
190  } \
191  inline R operator - (const T& x, const R& y) \
192  { \
193  return R (operator - ( (x), dynamic_cast<const B<T>&> (y))); \
194  } \
195  inline R operator * (const T& x, const R& y) \
196  { \
197  return R (operator * ( (x), dynamic_cast<const B<T>&> (y))); \
198  } \
199  inline R operator / (const T& x, const R& y) \
200  { \
201  return R (operator / ( (x), dynamic_cast<const B<T>&> (y))); \
202  } \
203  inline R operator + (const R& x, const R& y) \
204  { \
205  return R (operator + (dynamic_cast<const B<T>&> (x), \
206  dynamic_cast<const B<T>&> (y))); \
207  } \
208  inline R operator - (const R& x, const R& y) \
209  { \
210  return R (operator - (dynamic_cast<const B<T>&> (x), \
211  dynamic_cast<const B<T>&> (y))); \
212  } \
213  inline R product (const R& x, const R& y) \
214  { \
215  return R (product (dynamic_cast<const B<T>&> (x), \
216  dynamic_cast<const B<T>&> (y))); \
217  } \
218  inline R quotient (const R& x, const R& y) \
219  { \
220  return R (quotient (dynamic_cast<const B<T>&> (x), \
221  dynamic_cast<const B<T>&> (y))); \
222  }
223 
224 // Instantiate all the MArray friends for MArray element type T.
225 #define INSTANTIATE_MARRAY_FRIENDS(T, API) \
226  template API MArray<T>& operator += (MArray<T>&, const T&); \
227  template API MArray<T>& operator -= (MArray<T>&, const T&); \
228  template API MArray<T>& operator *= (MArray<T>&, const T&); \
229  template API MArray<T>& operator /= (MArray<T>&, const T&); \
230  template API MArray<T>& operator += (MArray<T>&, const MArray<T>&); \
231  template API MArray<T>& operator -= (MArray<T>&, const MArray<T>&); \
232  template API MArray<T>& product_eq (MArray<T>&, const MArray<T>&); \
233  template API MArray<T>& quotient_eq (MArray<T>&, const MArray<T>&); \
234  template API MArray<T> operator + (const MArray<T>&); \
235  template API MArray<T> operator - (const MArray<T>&); \
236  template API MArray<T> operator + (const MArray<T>&, const T&); \
237  template API MArray<T> operator - (const MArray<T>&, const T&); \
238  template API MArray<T> operator * (const MArray<T>&, const T&); \
239  template API MArray<T> operator / (const MArray<T>&, const T&); \
240  template API MArray<T> operator + (const T&, const MArray<T>&); \
241  template API MArray<T> operator - (const T&, const MArray<T>&); \
242  template API MArray<T> operator * (const T&, const MArray<T>&); \
243  template API MArray<T> operator / (const T&, const MArray<T>&); \
244  template API MArray<T> operator + (const MArray<T>&, const MArray<T>&); \
245  template API MArray<T> operator - (const MArray<T>&, const MArray<T>&); \
246  template API MArray<T> quotient (const MArray<T>&, const MArray<T>&); \
247  template API MArray<T> product (const MArray<T>&, const MArray<T>&);
248 
249 // Instantiate all the MDiagArray2 friends for MDiagArray2 element type T.
250 #define INSTANTIATE_MDIAGARRAY2_FRIENDS(T, API) \
251  template API MDiagArray2<T> operator + (const MDiagArray2<T>&); \
252  template API MDiagArray2<T> operator - (const MDiagArray2<T>&); \
253  template API MDiagArray2<T> operator * (const MDiagArray2<T>&, const T&); \
254  template API MDiagArray2<T> operator / (const MDiagArray2<T>&, const T&); \
255  template API MDiagArray2<T> operator * (const T&, const MDiagArray2<T>&); \
256  template API MDiagArray2<T> operator + (const MDiagArray2<T>&, \
257  const MDiagArray2<T>&); \
258  template API MDiagArray2<T> operator - (const MDiagArray2<T>&, \
259  const MDiagArray2<T>&); \
260  template API MDiagArray2<T> product (const MDiagArray2<T>&, \
261  const MDiagArray2<T>&);
262 
263 #endif
void idx_min(const idx_vector &idx, const MArray< T > &vals)
Definition: MArray.cc:101
Template for N-dimensional array classes with like-type math operators.
Definition: MArray.h:32
MArray< T > & operator+=(MArray< T > &, const T &)
Definition: MArray.cc:216
void idx_add_nd(const idx_vector &idx, const MArray< T > &vals, int dim=-1)
Definition: MArray.cc:140
MArray< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition: MArray.h:106
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
Array< T > squeeze(void) const
Chop off leading singleton dimensions.
Definition: Array.cc:116
MArray< T > ipermute(const Array< octave_idx_type > &vec) const
Definition: MArray.h:98
void changesign(void)
Definition: MArray.cc:204
MArray< T > & operator*=(MArray< T > &, const T &)
Definition: MArray.cc:238
Array< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: Array.cc:430
MArray< T > & quotient_eq(MArray< T > &, const MArray< T > &)
Definition: MArray.cc:295
~MArray(void)=default
Array< T > ipermute(const Array< octave_idx_type > &vec) const
Definition: Array.h:556
MArray< T > transpose(void) const
Definition: MArray.h:103
MArray< T > quotient(const MArray< T > &, const MArray< T > &)
Definition: MArray.cc:347
octave_function * fcn
Definition: ov-class.cc:1754
Array< T > reshape(octave_idx_type nr, octave_idx_type nc) const
Definition: Array.h:549
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:400
MArray< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: MArray.h:94
MArray< T > reshape(const dim_vector &new_dims) const
Definition: MArray.h:91
MArray(const dim_vector &dv)
Definition: MArray.h:72
MArray(const dim_vector &dv, const T &val)
Definition: MArray.h:75
MArray(const Array< U > &a)
Definition: MArray.h:81
static const octave_idx_type idx_max
Definition: dlmread.cc:45
MArray< T > & operator/=(MArray< T > &, const T &)
Definition: MArray.cc:249
MArray< T > operator*(const MArray< T > &, const T &)
Definition: MArray.cc:316
MArray< T > operator-(const MArray< T > &)
Definition: MArray.cc:358
MArray< T > operator/(const MArray< T > &, const T &)
Definition: MArray.cc:317
Array< T > transpose(void) const
Definition: Array.cc:1598
MArray< T > operator+(const MArray< T > &)
Definition: MArray.cc:351
MArray< T > product(const MArray< T > &, const MArray< T > &)
Definition: MArray.cc:346
MArray< T > & operator=(const MArray< T > &a)
Definition: MArray.h:85
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:125
MArray(const MArray< T > &a)
Definition: MArray.h:78
MArray squeeze(void) const
Definition: MArray.h:101
MArray(void)
Definition: MArray.h:70
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
Array< T > & operator=(const Array< T > &a)
Definition: Array.h:311
dim_vector dv
Definition: sub2ind.cc:263
void idx_add(const idx_vector &idx, T val)
Performs indexed accumulative addition.
Definition: MArray.cc:54
MArray< T > & operator-=(MArray< T > &, const T &)
Definition: MArray.cc:227
Array< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition: Array.cc:1641
MArray(T *sdata, octave_idx_type slen, octave_idx_type *adims, void *arep)
Definition: MArray.h:65
MArray< T > & product_eq(MArray< T > &, const MArray< T > &)
Definition: MArray.cc:284