GNU Octave 10.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-2025 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
35template <typename T> OCTARRAY_API MArray<T>& operator += (MArray<T>&, const T&);
36template <typename T> OCTARRAY_API MArray<T>& operator -= (MArray<T>&, const T&);
37template <typename T> OCTARRAY_API MArray<T>& operator *= (MArray<T>&, const T&);
38template <typename T> OCTARRAY_API MArray<T>& operator /= (MArray<T>&, const T&);
39template <typename T> OCTARRAY_API MArray<T>& operator += (MArray<T>&, const MArray<T>&);
40template <typename T> OCTARRAY_API MArray<T>& operator -= (MArray<T>&, const MArray<T>&);
41template <typename T> OCTARRAY_API MArray<T>& product_eq (MArray<T>&, const MArray<T>&);
42template <typename T> OCTARRAY_API MArray<T>& quotient_eq (MArray<T>&, const MArray<T>&);
43template <typename T> OCTARRAY_API MArray<T> operator + (const MArray<T>&);
44template <typename T> OCTARRAY_API MArray<T> operator - (const MArray<T>&);
45template <typename T> OCTARRAY_API MArray<T> operator + (const MArray<T>&, const T&);
46template <typename T> OCTARRAY_API MArray<T> operator - (const MArray<T>&, const T&);
47template <typename T> OCTARRAY_API MArray<T> operator * (const MArray<T>&, const T&);
48template <typename T> OCTARRAY_API MArray<T> operator / (const MArray<T>&, const T&);
49template <typename T> OCTARRAY_API MArray<T> operator + (const T&, const MArray<T>&);
50template <typename T> OCTARRAY_API MArray<T> operator - (const T&, const MArray<T>&);
51template <typename T> OCTARRAY_API MArray<T> operator * (const T&, const MArray<T>&);
52template <typename T> OCTARRAY_API MArray<T> operator / (const T&, const MArray<T>&);
53template <typename T> OCTARRAY_API MArray<T> operator + (const MArray<T>&, const MArray<T>&);
54template <typename T> OCTARRAY_API MArray<T> operator - (const MArray<T>&, const MArray<T>&);
55template <typename T> OCTARRAY_API MArray<T> quotient (const MArray<T>&, const MArray<T>&);
56template <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.
59template <typename T>
60class OCTARRAY_API MArray : public Array<T>
61{
62public:
63
64 MArray () : Array<T> () { }
65
66 explicit MArray (const dim_vector& dv)
67 : Array<T> (dv) { }
68
69 explicit MArray (const dim_vector& dv, const T& val)
70 : Array<T> (dv, val) { }
71
72 MArray (const MArray<T>& a) : Array<T> (a) { }
73
74 template <typename U>
75 MArray (const Array<U>& a) : Array<T> (a) { }
76
77 ~MArray () = default;
78
80 {
82 return *this;
83 }
84
85 MArray<T> reshape (const dim_vector& new_dims) const
86 { return Array<T>::reshape (new_dims); }
87
89 bool inv = false) const
90 { return Array<T>::permute (vec, inv); }
91
93 { return Array<T>::ipermute (vec); }
94
95 MArray squeeze () const { return Array<T>::squeeze (); }
96
98 { return Array<T>::transpose (); }
99
100 MArray<T> hermitian (T (*fcn) (const T&) = nullptr) const
101 { return Array<T>::hermitian (fcn); }
102
103 //! Performs indexed accumulative addition.
104 //@{
105 OCTARRAY_API void idx_add (const octave::idx_vector& idx, T val);
106 OCTARRAY_API void
107 idx_add (const octave::idx_vector& idx, const MArray<T>& vals);
108 //@}
109
110 OCTARRAY_API void
111 idx_min (const octave::idx_vector& idx, const MArray<T>& vals);
112
113 OCTARRAY_API void
114 idx_max (const octave::idx_vector& idx, const MArray<T>& vals);
115
116 OCTARRAY_API void
117 idx_add_nd (const octave::idx_vector& idx, const MArray<T>& vals,
118 int dim = -1);
119
120 OCTARRAY_API void changesign ();
121
122private:
123 OCTARRAY_API static void instantiation_guard ();
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 > 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.h:130
Array< T, Alloc > ipermute(const Array< octave_idx_type > &vec) const
Size of the specified dimension.
Definition Array.h:646
Array< T, Alloc > transpose() const
Size of the specified dimension.
Array< T, Alloc > & operator=(const Array< T, Alloc > &a)
Definition Array.h:365
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.h:636
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:88
MArray< T > transpose() const
Definition MArray.h:97
MArray< T > hermitian(T(*fcn)(const T &)=nullptr) const
Definition MArray.h:100
MArray(const Array< U > &a)
Definition MArray.h:75
MArray< T > reshape(const dim_vector &new_dims) const
Definition MArray.h:85
MArray squeeze() const
Definition MArray.h:95
~MArray()=default
MArray(const dim_vector &dv, const T &val)
Definition MArray.h:69
MArray< T > ipermute(const Array< octave_idx_type > &vec) const
Definition MArray.h:92
MArray()
Definition MArray.h:64
MArray(const MArray< T > &a)
Definition MArray.h:72
MArray(const dim_vector &dv)
Definition MArray.h:66
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90