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