GNU Octave  9.1.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-2024 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 "MArray-fwd.h"
33 #include "mx-inlines.cc"
34 
35 template <typename T> OCTARRAY_API MArray<T>& operator += (MArray<T>&, const T&);
36 template <typename T> OCTARRAY_API MArray<T>& operator -= (MArray<T>&, const T&);
37 template <typename T> OCTARRAY_API MArray<T>& operator *= (MArray<T>&, const T&);
38 template <typename T> OCTARRAY_API MArray<T>& operator /= (MArray<T>&, const T&);
39 template <typename T> OCTARRAY_API MArray<T>& operator += (MArray<T>&, const MArray<T>&);
40 template <typename T> OCTARRAY_API MArray<T>& operator -= (MArray<T>&, const MArray<T>&);
41 template <typename T> OCTARRAY_API MArray<T>& product_eq (MArray<T>&, const MArray<T>&);
42 template <typename T> OCTARRAY_API MArray<T>& quotient_eq (MArray<T>&, const MArray<T>&);
43 template <typename T> OCTARRAY_API MArray<T> operator + (const MArray<T>&);
44 template <typename T> OCTARRAY_API MArray<T> operator - (const MArray<T>&);
45 template <typename T> OCTARRAY_API MArray<T> operator + (const MArray<T>&, const T&);
46 template <typename T> OCTARRAY_API MArray<T> operator - (const MArray<T>&, const T&);
47 template <typename T> OCTARRAY_API MArray<T> operator * (const MArray<T>&, const T&);
48 template <typename T> OCTARRAY_API MArray<T> operator / (const MArray<T>&, const T&);
49 template <typename T> OCTARRAY_API MArray<T> operator + (const T&, const MArray<T>&);
50 template <typename T> OCTARRAY_API MArray<T> operator - (const T&, const MArray<T>&);
51 template <typename T> OCTARRAY_API MArray<T> operator * (const T&, const MArray<T>&);
52 template <typename T> OCTARRAY_API MArray<T> operator / (const T&, const MArray<T>&);
53 template <typename T> OCTARRAY_API MArray<T> operator + (const MArray<T>&, const MArray<T>&);
54 template <typename T> OCTARRAY_API MArray<T> operator - (const MArray<T>&, const MArray<T>&);
55 template <typename T> OCTARRAY_API MArray<T> quotient (const MArray<T>&, const MArray<T>&);
56 template <typename T> OCTARRAY_API MArray<T> product (const MArray<T>&, const MArray<T>&);
57 
58 //! Template for N-dimensional array classes with like-type math operators.
59 template <typename T>
60 class
61 OCTARRAY_API
62 MArray : public Array<T>
63 {
64 public:
65 
66  MArray () : Array<T> () { }
67 
68  explicit MArray (const dim_vector& dv)
69  : Array<T> (dv) { }
70 
71  explicit MArray (const dim_vector& dv, const T& val)
72  : Array<T> (dv, val) { }
73 
74  MArray (const MArray<T>& a) : Array<T> (a) { }
75 
76  template <typename U>
77  MArray (const Array<U>& a) : Array<T> (a) { }
78 
79  ~MArray () = default;
80 
81  MArray<T>& operator = (const MArray<T>& a)
82  {
84  return *this;
85  }
86 
87  MArray<T> reshape (const dim_vector& new_dims) const
88  { return Array<T>::reshape (new_dims); }
89 
91  bool inv = false) const
92  { return Array<T>::permute (vec, inv); }
93 
95  { return Array<T>::ipermute (vec); }
96 
97  MArray squeeze () const { return Array<T>::squeeze (); }
98 
100  { return Array<T>::transpose (); }
101 
102  MArray<T> hermitian (T (*fcn) (const T&) = nullptr) const
103  { return Array<T>::hermitian (fcn); }
104 
105  //! Performs indexed accumulative addition.
106  //@{
107  OCTARRAY_API void idx_add (const octave::idx_vector& idx, T val);
108  OCTARRAY_API void
109  idx_add (const octave::idx_vector& idx, const MArray<T>& vals);
110  //@}
111 
112  OCTARRAY_API void
113  idx_min (const octave::idx_vector& idx, const MArray<T>& vals);
114 
115  OCTARRAY_API void
116  idx_max (const octave::idx_vector& idx, const MArray<T>& vals);
117 
118  OCTARRAY_API void
119  idx_add_nd (const octave::idx_vector& idx, const MArray<T>& vals,
120  int dim = -1);
121 
122  OCTARRAY_API void changesign ();
123 
124 private:
125  OCTARRAY_API static void instantiation_guard ();
126 };
127 
128 // Define all the MArray forwarding functions for return type R and
129 // MArray element type T
130 #define MARRAY_FORWARD_DEFS(B, R, T) \
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 T& y) \
144  { \
145  return R (operator /= (dynamic_cast<B<T>&> (x), (y))); \
146  } \
147  inline R operator += (R& x, const R& y) \
148  { \
149  return R (operator += (dynamic_cast<B<T>&> (x), \
150  dynamic_cast<const B<T>&> (y))); \
151  } \
152  inline R operator -= (R& x, const R& y) \
153  { \
154  return R (operator -= (dynamic_cast<B<T>&> (x), \
155  dynamic_cast<const B<T>&> (y))); \
156  } \
157  inline R product_eq (R& x, const R& y) \
158  { \
159  return R (product_eq (dynamic_cast<B<T>&> (x), \
160  dynamic_cast<const B<T>&> (y))); \
161  } \
162  inline R quotient_eq (R& x, const R& y) \
163  { \
164  return R (quotient_eq (dynamic_cast<B<T>&> (x), \
165  dynamic_cast<const B<T>&> (y))); \
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) \
172  { \
173  return R (operator - (dynamic_cast<const B<T>&> (x))); \
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 R& x, const T& y) \
188  { \
189  return R (operator / (dynamic_cast<const B<T>&> (x), (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 T& x, const R& y) \
204  { \
205  return R (operator / ( (x), dynamic_cast<const B<T>&> (y))); \
206  } \
207  inline R operator + (const R& x, const R& y) \
208  { \
209  return R (operator + (dynamic_cast<const B<T>&> (x), \
210  dynamic_cast<const B<T>&> (y))); \
211  } \
212  inline R operator - (const R& x, const R& y) \
213  { \
214  return R (operator - (dynamic_cast<const B<T>&> (x), \
215  dynamic_cast<const B<T>&> (y))); \
216  } \
217  inline R product (const R& x, const R& y) \
218  { \
219  return R (product (dynamic_cast<const B<T>&> (x), \
220  dynamic_cast<const B<T>&> (y))); \
221  } \
222  inline R quotient (const R& x, const R& y) \
223  { \
224  return R (quotient (dynamic_cast<const B<T>&> (x), \
225  dynamic_cast<const B<T>&> (y))); \
226  }
227 
228 // Instantiate all the MArray friends for MArray element type T.
229 #define INSTANTIATE_MARRAY_FRIENDS(T, API) \
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 T&); \
233  template API MArray<T>& operator /= (MArray<T>&, const T&); \
234  template API MArray<T>& operator += (MArray<T>&, const MArray<T>&); \
235  template API MArray<T>& operator -= (MArray<T>&, const MArray<T>&); \
236  template API MArray<T>& product_eq (MArray<T>&, const MArray<T>&); \
237  template API MArray<T>& quotient_eq (MArray<T>&, const MArray<T>&); \
238  template API MArray<T> operator + (const MArray<T>&); \
239  template API MArray<T> operator - (const MArray<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 MArray<T>&, const T&); \
243  template API MArray<T> operator / (const MArray<T>&, const 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 T&, const MArray<T>&); \
247  template API MArray<T> operator / (const T&, const MArray<T>&); \
248  template API MArray<T> operator + (const MArray<T>&, const MArray<T>&); \
249  template API MArray<T> operator - (const MArray<T>&, const MArray<T>&); \
250  template API MArray<T> quotient (const MArray<T>&, const MArray<T>&); \
251  template API MArray<T> product (const MArray<T>&, const MArray<T>&);
252 
253 // Instantiate all the MDiagArray2 friends for MDiagArray2 element type T.
254 #define INSTANTIATE_MDIAGARRAY2_FRIENDS(T, API) \
255  template API MDiagArray2<T> operator + (const MDiagArray2<T>&); \
256  template API MDiagArray2<T> operator - (const MDiagArray2<T>&); \
257  template API MDiagArray2<T> operator * (const MDiagArray2<T>&, const T&); \
258  template API MDiagArray2<T> operator / (const MDiagArray2<T>&, const T&); \
259  template API MDiagArray2<T> operator * (const T&, const MDiagArray2<T>&); \
260  template API MDiagArray2<T> operator + (const MDiagArray2<T>&, \
261  const MDiagArray2<T>&); \
262  template API MDiagArray2<T> operator - (const MDiagArray2<T>&, \
263  const MDiagArray2<T>&); \
264  template API MDiagArray2<T> product (const MDiagArray2<T>&, \
265  const MDiagArray2<T>&);
266 
267 #endif
MArray< T > & product_eq(MArray< T > &, const MArray< T > &)
Definition: MArray.cc:297
MArray< T > quotient(const MArray< T > &, const MArray< T > &)
Definition: MArray.cc:360
MArray< T > & quotient_eq(MArray< T > &, const MArray< T > &)
Definition: MArray.cc:308
MArray< T > operator/(const MArray< T > &, const T &)
Definition: MArray.cc:330
MArray< T > operator-(const MArray< T > &)
Definition: MArray.cc:371
MArray< T > & operator+=(MArray< T > &, const T &)
Definition: MArray.cc:229
MArray< T > & operator/=(MArray< T > &, const T &)
Definition: MArray.cc:262
MArray< T > & operator*=(MArray< T > &, const T &)
Definition: MArray.cc:251
MArray< T > & operator-=(MArray< T > &, const T &)
Definition: MArray.cc:240
MArray< T > operator*(const MArray< T > &, const T &)
Definition: MArray.cc:329
MArray< T > operator+(const MArray< T > &)
Definition: MArray.cc:364
MArray< T > product(const MArray< T > &, const MArray< T > &)
Definition: MArray.cc:359
Array< T, Alloc > transpose() const
Size of the specified dimension.
Definition: Array-base.cc:1623
Array< T, Alloc > hermitian(T(*fcn)(const T &)=nullptr) const
Size of the specified dimension.
Definition: Array-base.cc:1666
Array< T, Alloc > ipermute(const Array< octave_idx_type > &vec) const
Size of the specified dimension.
Definition: Array.h:645
Array< T, Alloc > squeeze() const
Chop off leading singleton dimensions.
Definition: Array-base.cc:139
Array< T, Alloc > & operator=(const Array< T, Alloc > &a)
Definition: Array.h:361
Array< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Size of the specified dimension.
Definition: Array-base.cc:450
Array< T, Alloc > reshape(octave_idx_type nr, octave_idx_type nc) const
Size of the specified dimension.
Definition: Array.h:635
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:102
MArray< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: MArray.h:90
MArray< T > transpose() const
Definition: MArray.h:99
MArray< T > reshape(const dim_vector &new_dims) const
Definition: MArray.h:87
MArray(const Array< U > &a)
Definition: MArray.h:77
MArray squeeze() const
Definition: MArray.h:97
~MArray()=default
MArray(const dim_vector &dv, const T &val)
Definition: MArray.h:71
MArray()
Definition: MArray.h:66
MArray< T > ipermute(const Array< octave_idx_type > &vec) const
Definition: MArray.h:94
MArray(const MArray< T > &a)
Definition: MArray.h:74
MArray(const dim_vector &dv)
Definition: MArray.h:68
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave::idx_vector idx_vector
Definition: idx-vector.h:1022