GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
MArray.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2026 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-oct.h"
32#include "MArray-fwd.h"
33#include "mx-inlines.cc"
34
35template <typename T> OCTAVE_API MArray<T>& operator += (MArray<T>&, const T&);
36template <typename T> OCTAVE_API MArray<T>& operator -= (MArray<T>&, const T&);
37template <typename T> OCTAVE_API MArray<T>& operator *= (MArray<T>&, const T&);
38template <typename T> OCTAVE_API MArray<T>& operator /= (MArray<T>&, const T&);
39template <typename T> OCTAVE_API MArray<T>& operator += (MArray<T>&, const MArray<T>&);
40template <typename T> OCTAVE_API MArray<T>& operator -= (MArray<T>&, const MArray<T>&);
41template <typename T> OCTAVE_API MArray<T>& product_eq (MArray<T>&, const MArray<T>&);
42template <typename T> OCTAVE_API MArray<T>& quotient_eq (MArray<T>&, const MArray<T>&);
43template <typename T> OCTAVE_API MArray<T> operator + (const MArray<T>&);
44template <typename T> OCTAVE_API MArray<T> operator - (const MArray<T>&);
45template <typename T> OCTAVE_API MArray<T> operator + (const MArray<T>&, const T&);
46template <typename T> OCTAVE_API MArray<T> operator - (const MArray<T>&, const T&);
47template <typename T> OCTAVE_API MArray<T> operator * (const MArray<T>&, const T&);
48template <typename T> OCTAVE_API MArray<T> operator / (const MArray<T>&, const T&);
49template <typename T> OCTAVE_API MArray<T> operator + (const T&, const MArray<T>&);
50template <typename T> OCTAVE_API MArray<T> operator - (const T&, const MArray<T>&);
51template <typename T> OCTAVE_API MArray<T> operator * (const T&, const MArray<T>&);
52template <typename T> OCTAVE_API MArray<T> operator / (const T&, const MArray<T>&);
53template <typename T> OCTAVE_API MArray<T> operator + (const MArray<T>&, const MArray<T>&);
54template <typename T> OCTAVE_API MArray<T> operator - (const MArray<T>&, const MArray<T>&);
55template <typename T> OCTAVE_API MArray<T> quotient (const MArray<T>&, const MArray<T>&);
56template <typename T> OCTAVE_API MArray<T> product (const MArray<T>&, const MArray<T>&);
57
58//! Template for N-dimensional array classes with like-type math operators.
59template <typename T>
60class OCTAVE_API MArray : public Array<T>
61{
62public:
63
64 OCTAVE_OVERRIDABLE_FUNC_API
65 MArray () : Array<T> () { }
66
67 explicit OCTAVE_OVERRIDABLE_FUNC_API
68 MArray (const dim_vector& dv)
69 : Array<T> (dv) { }
70
71 explicit OCTAVE_OVERRIDABLE_FUNC_API
72 MArray (const dim_vector& dv, const T& val)
73 : Array<T> (dv, val) { }
74
75 OCTAVE_OVERRIDABLE_FUNC_API
76 MArray (const MArray<T>& a) : Array<T> (a) { }
77
78 template <typename U>
79 OCTAVE_OVERRIDABLE_FUNC_API
80 MArray (const Array<U>& a) : Array<T> (a) { }
81
82 OCTAVE_OVERRIDABLE_FUNC_API ~MArray () = default;
83
84 OCTAVE_OVERRIDABLE_FUNC_API MArray<T>& operator = (const MArray<T>& a)
85 {
87 return *this;
88 }
89
90 OCTAVE_OVERRIDABLE_FUNC_API MArray<T>
91 reshape (const dim_vector& new_dims) const
92 { return Array<T>::reshape (new_dims); }
93
94 OCTAVE_OVERRIDABLE_FUNC_API MArray<T>
95 permute (const Array<octave_idx_type>& vec, bool inv = false) const
96 { return Array<T>::permute (vec, inv); }
97
98 OCTAVE_OVERRIDABLE_FUNC_API MArray<T>
100 { return Array<T>::ipermute (vec); }
101
102 OCTAVE_OVERRIDABLE_FUNC_API MArray squeeze () const
103 { return Array<T>::squeeze (); }
104
105 OCTAVE_OVERRIDABLE_FUNC_API MArray<T> transpose () const
106 { return Array<T>::transpose (); }
107
108 OCTAVE_OVERRIDABLE_FUNC_API MArray<T>
109 hermitian (T (*fcn) (const T&) = nullptr) const
110 { return Array<T>::hermitian (fcn); }
111
112 //! Performs indexed accumulative addition.
113 //@{
114 OCTAVE_API void idx_add (const octave::idx_vector& idx, T val);
115 OCTAVE_API void
116 idx_add (const octave::idx_vector& idx, const MArray<T>& vals);
117 //@}
118
119 OCTAVE_API void
120 idx_min (const octave::idx_vector& idx, const MArray<T>& vals);
121
122 OCTAVE_API void
123 idx_max (const octave::idx_vector& idx, const MArray<T>& vals);
124
125 OCTAVE_API void
126 idx_add_nd (const octave::idx_vector& idx, const MArray<T>& vals,
127 int dim = -1);
128
129 OCTAVE_API void changesign ();
130
131private:
132 OCTAVE_API static void instantiation_guard ();
133};
134
135// Define all the MArray forwarding functions for return type R and
136// MArray element type T
137#define MARRAY_FORWARD_DEFS(B, R, T) \
138 inline R operator += (R& x, const T& y) \
139 { \
140 return R (operator += (dynamic_cast<B<T>&> (x), (y))); \
141 } \
142 inline R operator -= (R& x, const T& y) \
143 { \
144 return R (operator -= (dynamic_cast<B<T>&> (x), (y))); \
145 } \
146 inline R operator *= (R& x, const T& y) \
147 { \
148 return R (operator *= (dynamic_cast<B<T>&> (x), (y))); \
149 } \
150 inline R operator /= (R& x, const T& y) \
151 { \
152 return R (operator /= (dynamic_cast<B<T>&> (x), (y))); \
153 } \
154 inline R operator += (R& x, const R& y) \
155 { \
156 return R (operator += (dynamic_cast<B<T>&> (x), \
157 dynamic_cast<const B<T>&> (y))); \
158 } \
159 inline R operator -= (R& x, const R& y) \
160 { \
161 return R (operator -= (dynamic_cast<B<T>&> (x), \
162 dynamic_cast<const B<T>&> (y))); \
163 } \
164 inline R product_eq (R& x, const R& y) \
165 { \
166 return R (product_eq (dynamic_cast<B<T>&> (x), \
167 dynamic_cast<const B<T>&> (y))); \
168 } \
169 inline R quotient_eq (R& x, const R& y) \
170 { \
171 return R (quotient_eq (dynamic_cast<B<T>&> (x), \
172 dynamic_cast<const B<T>&> (y))); \
173 } \
174 inline R operator + (const R& x) \
175 { \
176 return R (operator + (dynamic_cast<const B<T>&> (x))); \
177 } \
178 inline R operator - (const R& x) \
179 { \
180 return R (operator - (dynamic_cast<const B<T>&> (x))); \
181 } \
182 inline R operator + (const R& x, const T& y) \
183 { \
184 return R (operator + (dynamic_cast<const B<T>&> (x), (y))); \
185 } \
186 inline R operator - (const R& x, const T& y) \
187 { \
188 return R (operator - (dynamic_cast<const B<T>&> (x), (y))); \
189 } \
190 inline R operator * (const R& x, const T& y) \
191 { \
192 return R (operator * (dynamic_cast<const B<T>&> (x), (y))); \
193 } \
194 inline R operator / (const R& x, const T& y) \
195 { \
196 return R (operator / (dynamic_cast<const B<T>&> (x), (y))); \
197 } \
198 inline R operator + (const T& x, const R& y) \
199 { \
200 return R (operator + ( (x), dynamic_cast<const B<T>&> (y))); \
201 } \
202 inline R operator - (const T& x, const R& y) \
203 { \
204 return R (operator - ( (x), dynamic_cast<const B<T>&> (y))); \
205 } \
206 inline R operator * (const T& x, const R& y) \
207 { \
208 return R (operator * ( (x), dynamic_cast<const B<T>&> (y))); \
209 } \
210 inline R operator / (const T& x, const R& y) \
211 { \
212 return R (operator / ( (x), dynamic_cast<const B<T>&> (y))); \
213 } \
214 inline R operator + (const R& x, const R& y) \
215 { \
216 return R (operator + (dynamic_cast<const B<T>&> (x), \
217 dynamic_cast<const B<T>&> (y))); \
218 } \
219 inline R operator - (const R& x, const R& y) \
220 { \
221 return R (operator - (dynamic_cast<const B<T>&> (x), \
222 dynamic_cast<const B<T>&> (y))); \
223 } \
224 inline R product (const R& x, const R& y) \
225 { \
226 return R (product (dynamic_cast<const B<T>&> (x), \
227 dynamic_cast<const B<T>&> (y))); \
228 } \
229 inline R quotient (const R& x, const R& y) \
230 { \
231 return R (quotient (dynamic_cast<const B<T>&> (x), \
232 dynamic_cast<const B<T>&> (y))); \
233 }
234
235// Instantiate all the MArray friends for MArray element type T.
236#define INSTANTIATE_MARRAY_FRIENDS(T, API) \
237 template API MArray<T>& operator += (MArray<T>&, const T&); \
238 template API MArray<T>& operator -= (MArray<T>&, const T&); \
239 template API MArray<T>& operator *= (MArray<T>&, const T&); \
240 template API MArray<T>& operator /= (MArray<T>&, const T&); \
241 template API MArray<T>& operator += (MArray<T>&, const MArray<T>&); \
242 template API MArray<T>& operator -= (MArray<T>&, const MArray<T>&); \
243 template API MArray<T>& product_eq (MArray<T>&, const MArray<T>&); \
244 template API MArray<T>& quotient_eq (MArray<T>&, const MArray<T>&); \
245 template API MArray<T> operator + (const MArray<T>&); \
246 template API MArray<T> operator - (const MArray<T>&); \
247 template API MArray<T> operator + (const MArray<T>&, const T&); \
248 template API MArray<T> operator - (const MArray<T>&, const T&); \
249 template API MArray<T> operator * (const MArray<T>&, const T&); \
250 template API MArray<T> operator / (const MArray<T>&, const T&); \
251 template API MArray<T> operator + (const T&, const MArray<T>&); \
252 template API MArray<T> operator - (const T&, const MArray<T>&); \
253 template API MArray<T> operator * (const T&, const MArray<T>&); \
254 template API MArray<T> operator / (const T&, const MArray<T>&); \
255 template API MArray<T> operator + (const MArray<T>&, const MArray<T>&); \
256 template API MArray<T> operator - (const MArray<T>&, const MArray<T>&); \
257 template API MArray<T> quotient (const MArray<T>&, const MArray<T>&); \
258 template API MArray<T> product (const MArray<T>&, const MArray<T>&);
259
260// Instantiate all the MDiagArray2 friends for MDiagArray2 element type T.
261#define INSTANTIATE_MDIAGARRAY2_FRIENDS(T, API) \
262 template API MDiagArray2<T> operator + (const MDiagArray2<T>&); \
263 template API MDiagArray2<T> operator - (const MDiagArray2<T>&); \
264 template API MDiagArray2<T> operator * (const MDiagArray2<T>&, const T&); \
265 template API MDiagArray2<T> operator / (const MDiagArray2<T>&, const T&); \
266 template API MDiagArray2<T> operator * (const T&, const MDiagArray2<T>&); \
267 template API MDiagArray2<T> operator + (const MDiagArray2<T>&, \
268 const MDiagArray2<T>&); \
269 template API MDiagArray2<T> operator - (const MDiagArray2<T>&, \
270 const MDiagArray2<T>&); \
271 template API MDiagArray2<T> product (const MDiagArray2<T>&, \
272 const MDiagArray2<T>&);
273
274#if ! defined (OCTAVE_EXTERN_TEMPLATE_MARRAY)
275// guard against double extern template declarations
276#define OCTAVE_EXTERN_TEMPLATE_MARRAY
277
278#include "oct-cmplx.h"
279#include "oct-inttypes-fwd.h"
280
281// "Protect" MArray<T> instantiations that are exported by liboctave from
282// being implicitly instantiated in compilation units including this file.
283
284// instantiated in MArray-C.cc
285extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<Complex>;
286// instantiated in MArray-d.cc
287extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<double>;
288// instantiated in MArray-f.cc
289extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<float>;
290// instantiated in MArray-fC.cc
291extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<FloatComplex>;
292// instantiated in MArray-i.cc
293extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<int>;
294#if defined (OCTAVE_ENABLE_64)
295extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<int64_t>;
296#endif
297extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<octave_int8>;
298extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<octave_int16>;
299extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<octave_int32>;
300extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<octave_int64>;
301extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<octave_uint8>;
302extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<octave_uint16>;
303extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<octave_uint32>;
304extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<octave_uint64>;
305// instantiated in MArray-s.cc
306extern template class OCTAVE_EXTERN_TEMPLATE_API MArray<short>;
307
308#endif // OCTAVE_EXTERN_TEMPLATE_MARRAY
309
310#endif
MArray< T > operator*(const MArray< T > &, const T &)
Definition MArray.cc:329
MArray< T > & operator/=(MArray< T > &, const T &)
Definition MArray.cc:262
MArray< T > & operator-=(MArray< T > &, const T &)
Definition MArray.cc:240
MArray< T > & product_eq(MArray< T > &, const MArray< T > &)
Definition MArray.cc:297
MArray< T > operator+(const MArray< T > &)
Definition MArray.cc:364
MArray< T > & operator*=(MArray< T > &, const T &)
Definition MArray.cc:251
MArray< T > quotient(const MArray< T > &, const MArray< T > &)
Definition MArray.cc:360
MArray< T > product(const MArray< T > &, const MArray< T > &)
Definition MArray.cc:359
MArray< T > operator/(const MArray< T > &, const T &)
Definition MArray.cc:330
MArray< T > & operator+=(MArray< T > &, const T &)
Definition MArray.cc:229
MArray< T > & quotient_eq(MArray< T > &, const MArray< T > &)
Definition MArray.cc:308
MArray< T > operator-(const MArray< T > &)
Definition MArray.cc:371
N Dimensional Array with copy-on-write semantics.
Definition Array-base.h:130
Array< T, Alloc > ipermute(const Array< octave_idx_type > &vec) const
Size of the specified dimension.
Definition Array-base.h:668
Array< T, Alloc > transpose() const
Size of the specified dimension.
Array< T, Alloc > & operator=(const Array< T, Alloc > &a)
Definition Array-base.h:385
Array< T, Alloc > hermitian(T(*fcn)(const T &)=nullptr) const
Size of the specified dimension.
Array< T, Alloc > squeeze() const
Chop off leading singleton dimensions.
Array< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Size of the specified dimension.
Array< T, Alloc > reshape(octave_idx_type nr, octave_idx_type nc) const
Size of the specified dimension.
Definition Array-base.h:658
Template for N-dimensional array classes with like-type math operators.
Definition MArray.h:61
MArray< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition MArray.h:95
MArray< T > transpose() const
Definition MArray.h:105
MArray< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition MArray.h:109
MArray(const Array< U > &a)
Definition MArray.h:80
MArray< T > reshape(const dim_vector &new_dims) const
Definition MArray.h:91
MArray squeeze() const
Definition MArray.h:102
~MArray()=default
MArray(const dim_vector &dv, const T &val)
Definition MArray.h:72
MArray< T > ipermute(const Array< octave_idx_type > &vec) const
Definition MArray.h:99
MArray()
Definition MArray.h:65
MArray(const MArray< T > &a)
Definition MArray.h:76
MArray(const dim_vector &dv)
Definition MArray.h:68
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:92
#define OCTAVE_API
Definition main.in.cc:55