1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1994-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_fMatrix_h)
27 #define octave_fMatrix_h 1
28 
29 #include "octave-config.h"
30 
31 #include "DET.h"
32 #include "MArray.h"
33 #include "MDiagArray2.h"
34 #include "MatrixType.h"
35 #include "fNDArray.h"
36 #include "mx-defs.h"
37 #include "mx-op-decl.h"
38 
39 class
40 OCTAVE_API
41 FloatMatrix : public FloatNDArray
42 {
43 public:
44 
45   typedef FloatColumnVector column_vector_type;
46   typedef FloatRowVector row_vector_type;
47 
48   typedef FloatColumnVector real_column_vector_type;
49   typedef FloatRowVector real_row_vector_type;
50 
51   typedef FloatMatrix real_matrix_type;
52   typedef FloatComplexMatrix complex_matrix_type;
53 
54   typedef FloatDiagMatrix real_diag_matrix_type;
55   typedef FloatComplexDiagMatrix complex_diag_matrix_type;
56 
57   typedef float real_elt_type;
58   typedef FloatComplex complex_elt_type;
59 
60   typedef void (*solve_singularity_handler) (float rcon);
61 
62   FloatMatrix (void) = default;
63 
64   FloatMatrix (const FloatMatrix& a) = default;
65 
66   FloatMatrix& operator = (const FloatMatrix& a) = default;
67 
68   ~FloatMatrix (void) = default;
69 
FloatMatrix(octave_idx_type r,octave_idx_type c)70   FloatMatrix (octave_idx_type r, octave_idx_type c)
71     : FloatNDArray (dim_vector (r, c)) { }
72 
FloatMatrix(octave_idx_type r,octave_idx_type c,float val)73   FloatMatrix (octave_idx_type r, octave_idx_type c, float val)
74     : FloatNDArray (dim_vector (r, c), val) { }
75 
FloatMatrix(const dim_vector & dv)76   FloatMatrix (const dim_vector& dv) : FloatNDArray (dv.redim (2)) { }
77 
FloatMatrix(const dim_vector & dv,float val)78   FloatMatrix (const dim_vector& dv, float val)
79     : FloatNDArray (dv.redim (2), val) { }
80 
81   template <typename U>
FloatMatrix(const MArray<U> & a)82   FloatMatrix (const MArray<U>& a) : FloatNDArray (a.as_matrix ()) { }
83 
84   template <typename U>
FloatMatrix(const Array<U> & a)85   FloatMatrix (const Array<U>& a) : FloatNDArray (a.as_matrix ()) { }
86 
87   explicit FloatMatrix (const FloatRowVector& rv);
88 
89   explicit FloatMatrix (const FloatColumnVector& cv);
90 
91   explicit FloatMatrix (const FloatDiagMatrix& a);
92 
93   explicit FloatMatrix (const MDiagArray2<float>& a);
94 
95   explicit FloatMatrix (const DiagArray2<float>& a);
96 
97   explicit FloatMatrix (const PermMatrix& a);
98 
99   explicit FloatMatrix (const boolMatrix& a);
100 
101   explicit FloatMatrix (const charMatrix& a);
102 
103   bool operator == (const FloatMatrix& a) const;
104   bool operator != (const FloatMatrix& a) const;
105 
106   bool issymmetric (void) const;
107 
108   // destructive insert/delete/reorder operations
109 
110   FloatMatrix& insert (const FloatMatrix& a,
111                        octave_idx_type r, octave_idx_type c);
112   FloatMatrix& insert (const FloatRowVector& a,
113                        octave_idx_type r, octave_idx_type c);
114   FloatMatrix& insert (const FloatColumnVector& a,
115                        octave_idx_type r, octave_idx_type c);
116   FloatMatrix& insert (const FloatDiagMatrix& a,
117                        octave_idx_type r, octave_idx_type c);
118 
119   FloatMatrix& fill (float val);
120   FloatMatrix& fill (float val, octave_idx_type r1, octave_idx_type c1,
121                      octave_idx_type r2, octave_idx_type c2);
122 
123   FloatMatrix append (const FloatMatrix& a) const;
124   FloatMatrix append (const FloatRowVector& a) const;
125   FloatMatrix append (const FloatColumnVector& a) const;
126   FloatMatrix append (const FloatDiagMatrix& a) const;
127 
128   FloatMatrix stack (const FloatMatrix& a) const;
129   FloatMatrix stack (const FloatRowVector& a) const;
130   FloatMatrix stack (const FloatColumnVector& a) const;
131   FloatMatrix stack (const FloatDiagMatrix& a) const;
132 
133   friend OCTAVE_API FloatMatrix real (const FloatComplexMatrix& a);
134   friend OCTAVE_API FloatMatrix imag (const FloatComplexMatrix& a);
135 
136   friend class FloatComplexMatrix;
137 
hermitian(void)138   FloatMatrix hermitian (void) const { return MArray<float>::transpose (); }
transpose(void)139   FloatMatrix transpose (void) const { return MArray<float>::transpose (); }
140 
141   // resize is the destructive equivalent for this one
142 
143   FloatMatrix extract (octave_idx_type r1, octave_idx_type c1,
144                        octave_idx_type r2, octave_idx_type c2) const;
145 
146   FloatMatrix extract_n (octave_idx_type r1, octave_idx_type c1,
147                          octave_idx_type nr, octave_idx_type nc) const;
148 
149   // extract row or column i.
150 
151   FloatRowVector row (octave_idx_type i) const;
152 
153   FloatColumnVector column (octave_idx_type i) const;
154 
155   void resize (octave_idx_type nr, octave_idx_type nc, float rfv = 0)
156   {
157     MArray<float>::resize (dim_vector (nr, nc), rfv);
158   }
159 
160 private:
161   FloatMatrix tinverse (MatrixType& mattype, octave_idx_type& info,
162                         float& rcon, bool force, bool calc_cond) const;
163 
164   FloatMatrix finverse (MatrixType& mattype, octave_idx_type& info,
165                         float& rcon, bool force, bool calc_cond) const;
166 
167 public:
168   FloatMatrix inverse (void) const;
169   FloatMatrix inverse (octave_idx_type& info) const;
170   FloatMatrix inverse (octave_idx_type& info, float& rcon, bool force = false,
171                        bool calc_cond = true) const;
172 
173   FloatMatrix inverse (MatrixType& mattype) const;
174   FloatMatrix inverse (MatrixType& mattype, octave_idx_type& info) const;
175   FloatMatrix inverse (MatrixType& mattype, octave_idx_type& info, float& rcon,
176                        bool force = false, bool calc_cond = true) const;
177 
178   FloatMatrix pseudo_inverse (float tol = 0.0) const;
179 
180   FloatComplexMatrix fourier (void) const;
181   FloatComplexMatrix ifourier (void) const;
182 
183   FloatComplexMatrix fourier2d (void) const;
184   FloatComplexMatrix ifourier2d (void) const;
185 
186   FloatDET determinant (void) const;
187   FloatDET determinant (octave_idx_type& info) const;
188   FloatDET determinant (octave_idx_type& info, float& rcon,
189                         bool calc_cond = true) const;
190   FloatDET determinant (MatrixType& mattype, octave_idx_type& info,
191                         float& rcon, bool calc_cond = true) const;
192 
193   float rcond (void) const;
194   float rcond (MatrixType& mattype) const;
195 
196 private:
197   // Upper triangular matrix solvers
198   FloatMatrix utsolve (MatrixType& mattype, const FloatMatrix& b,
199                        octave_idx_type& info,
200                        float& rcon, solve_singularity_handler sing_handler,
201                        bool calc_cond = false,
202                        blas_trans_type transt = blas_no_trans) const;
203 
204   // Lower triangular matrix solvers
205   FloatMatrix ltsolve (MatrixType& mattype, const FloatMatrix& b,
206                        octave_idx_type& info,
207                        float& rcon, solve_singularity_handler sing_handler,
208                        bool calc_cond = false,
209                        blas_trans_type transt = blas_no_trans) const;
210 
211   // Full matrix solvers (lu/cholesky)
212   FloatMatrix fsolve (MatrixType& mattype, const FloatMatrix& b,
213                       octave_idx_type& info,
214                       float& rcon, solve_singularity_handler sing_handler,
215                       bool calc_cond = false) const;
216 
217 public:
218   // Generic interface to solver with no probing of type
219   FloatMatrix solve (MatrixType& mattype, const FloatMatrix& b) const;
220   FloatMatrix solve (MatrixType& mattype, const FloatMatrix& b,
221                      octave_idx_type& info) const;
222   FloatMatrix solve (MatrixType& mattype, const FloatMatrix& b,
223                      octave_idx_type& info, float& rcon) const;
224   FloatMatrix solve (MatrixType& mattype, const FloatMatrix& b,
225                      octave_idx_type& info, float& rcon,
226                      solve_singularity_handler sing_handler,
227                      bool singular_fallback = true,
228                      blas_trans_type transt = blas_no_trans) const;
229 
230   FloatComplexMatrix solve (MatrixType& mattype,
231                             const FloatComplexMatrix& b) const;
232   FloatComplexMatrix solve (MatrixType& mattype, const FloatComplexMatrix& b,
233                             octave_idx_type& info) const;
234   FloatComplexMatrix solve (MatrixType& mattype, const FloatComplexMatrix& b,
235                             octave_idx_type& info, float& rcon) const;
236   FloatComplexMatrix solve (MatrixType& mattype, const FloatComplexMatrix& b,
237                             octave_idx_type& info, float& rcon,
238                             solve_singularity_handler sing_handler,
239                             bool singular_fallback = true,
240                             blas_trans_type transt = blas_no_trans) const;
241 
242   FloatColumnVector solve (MatrixType& mattype, const FloatColumnVector& b) const;
243   FloatColumnVector solve (MatrixType& mattype, const FloatColumnVector& b,
244                            octave_idx_type& info) const;
245   FloatColumnVector solve (MatrixType& mattype, const FloatColumnVector& b,
246                            octave_idx_type& info, float& rcon) const;
247   FloatColumnVector solve (MatrixType& mattype, const FloatColumnVector& b,
248                            octave_idx_type& info, float& rcon,
249                            solve_singularity_handler sing_handler,
250                            blas_trans_type transt = blas_no_trans) const;
251 
252   FloatComplexColumnVector solve (MatrixType& mattype,
253                                   const FloatComplexColumnVector& b) const;
254   FloatComplexColumnVector solve (MatrixType& mattype,
255                                   const FloatComplexColumnVector& b,
256                                   octave_idx_type& info) const;
257   FloatComplexColumnVector solve (MatrixType& mattype,
258                                   const FloatComplexColumnVector& b,
259                                   octave_idx_type& info, float& rcon) const;
260   FloatComplexColumnVector solve (MatrixType& mattype,
261                                   const FloatComplexColumnVector& b,
262                                   octave_idx_type& info, float& rcon,
263                                   solve_singularity_handler sing_handler,
264                                   blas_trans_type transt = blas_no_trans) const;
265 
266   // Generic interface to solver with probing of type
267   FloatMatrix solve (const FloatMatrix& b) const;
268   FloatMatrix solve (const FloatMatrix& b, octave_idx_type& info) const;
269   FloatMatrix solve (const FloatMatrix& b, octave_idx_type& info,
270                      float& rcon) const;
271   FloatMatrix solve (const FloatMatrix& b, octave_idx_type& info, float& rcon,
272                      solve_singularity_handler sing_handler,
273                      blas_trans_type transt = blas_no_trans) const;
274 
275   FloatComplexMatrix solve (const FloatComplexMatrix& b) const;
276   FloatComplexMatrix solve (const FloatComplexMatrix& b,
277                             octave_idx_type& info) const;
278   FloatComplexMatrix solve (const FloatComplexMatrix& b, octave_idx_type& info,
279                             float& rcon) const;
280   FloatComplexMatrix solve (const FloatComplexMatrix& b, octave_idx_type& info,
281                             float& rcon,
282                             solve_singularity_handler sing_handler,
283                             blas_trans_type transt = blas_no_trans) const;
284 
285   FloatColumnVector solve (const FloatColumnVector& b) const;
286   FloatColumnVector solve (const FloatColumnVector& b,
287                            octave_idx_type& info) const;
288   FloatColumnVector solve (const FloatColumnVector& b, octave_idx_type& info,
289                            float& rcon) const;
290   FloatColumnVector solve (const FloatColumnVector& b, octave_idx_type& info,
291                            float& rcon,
292                            solve_singularity_handler sing_handler,
293                            blas_trans_type transt = blas_no_trans) const;
294 
295   FloatComplexColumnVector solve (const FloatComplexColumnVector& b) const;
296   FloatComplexColumnVector solve (const FloatComplexColumnVector& b,
297                                   octave_idx_type& info) const;
298   FloatComplexColumnVector solve (const FloatComplexColumnVector& b,
299                                   octave_idx_type& info,
300                                   float& rcon) const;
301   FloatComplexColumnVector solve (const FloatComplexColumnVector& b,
302                                   octave_idx_type& info,
303                                   float& rcon,
304                                   solve_singularity_handler sing_handler,
305                                   blas_trans_type transt = blas_no_trans) const;
306 
307   // Singular solvers
308   FloatMatrix lssolve (const FloatMatrix& b) const;
309   FloatMatrix lssolve (const FloatMatrix& b, octave_idx_type& info) const;
310   FloatMatrix lssolve (const FloatMatrix& b, octave_idx_type& info,
311                        octave_idx_type& rank) const;
312   FloatMatrix lssolve (const FloatMatrix& b, octave_idx_type& info,
313                        octave_idx_type& rank, float& rcon) const;
314 
315   FloatComplexMatrix lssolve (const FloatComplexMatrix& b) const;
316   FloatComplexMatrix lssolve (const FloatComplexMatrix& b,
317                               octave_idx_type& info) const;
318   FloatComplexMatrix lssolve (const FloatComplexMatrix& b,
319                               octave_idx_type& info,
320                               octave_idx_type& rank) const;
321   FloatComplexMatrix lssolve (const FloatComplexMatrix& b,
322                               octave_idx_type& info, octave_idx_type& rank,
323                               float& rcon) const;
324 
325   FloatColumnVector lssolve (const FloatColumnVector& b) const;
326   FloatColumnVector lssolve (const FloatColumnVector& b,
327                              octave_idx_type& info) const;
328   FloatColumnVector lssolve (const FloatColumnVector& b, octave_idx_type& info,
329                              octave_idx_type& rank) const;
330   FloatColumnVector lssolve (const FloatColumnVector& b, octave_idx_type& info,
331                              octave_idx_type& rank, float& rcon) const;
332 
333   FloatComplexColumnVector lssolve (const FloatComplexColumnVector& b) const;
334   FloatComplexColumnVector lssolve (const FloatComplexColumnVector& b,
335                                     octave_idx_type& info) const;
336   FloatComplexColumnVector lssolve (const FloatComplexColumnVector& b,
337                                     octave_idx_type& info,
338                                     octave_idx_type& rank) const;
339   FloatComplexColumnVector lssolve (const FloatComplexColumnVector& b,
340                                     octave_idx_type& info,
341                                     octave_idx_type& rank, float& rcon) const;
342 
343   FloatMatrix& operator += (const FloatDiagMatrix& a);
344   FloatMatrix& operator -= (const FloatDiagMatrix& a);
345 
346   FloatMatrix cumprod (int dim = -1) const;
347   FloatMatrix cumsum (int dim = -1) const;
348   FloatMatrix prod (int dim = -1) const;
349   FloatMatrix sum (int dim = -1) const;
350   FloatMatrix sumsq (int dim = -1) const;
351   FloatMatrix abs (void) const;
352 
353   FloatMatrix diag (octave_idx_type k = 0) const;
354 
355   FloatDiagMatrix diag (octave_idx_type m, octave_idx_type n) const;
356 
357   FloatColumnVector row_min (void) const;
358   FloatColumnVector row_max (void) const;
359 
360   FloatColumnVector row_min (Array<octave_idx_type>& index) const;
361   FloatColumnVector row_max (Array<octave_idx_type>& index) const;
362 
363   FloatRowVector column_min (void) const;
364   FloatRowVector column_max (void) const;
365 
366   FloatRowVector column_min (Array<octave_idx_type>& index) const;
367   FloatRowVector column_max (Array<octave_idx_type>& index) const;
368 
369   // i/o
370 
371   friend OCTAVE_API std::ostream& operator << (std::ostream& os,
372                                                const FloatMatrix& a);
373   friend OCTAVE_API std::istream& operator >> (std::istream& is,
374                                                FloatMatrix& a);
375 };
376 
377 // Publish externally used friend functions.
378 
379 extern OCTAVE_API FloatMatrix real (const FloatComplexMatrix& a);
380 extern OCTAVE_API FloatMatrix imag (const FloatComplexMatrix& a);
381 
382 // column vector by row vector -> matrix operations
383 
384 extern OCTAVE_API FloatMatrix operator * (const FloatColumnVector& a,
385                                           const FloatRowVector& b);
386 
387 // Other functions.
388 
389 extern OCTAVE_API FloatMatrix Givens (float, float);
390 
391 extern OCTAVE_API FloatMatrix Sylvester (const FloatMatrix&, const FloatMatrix&,
392                                          const FloatMatrix&);
393 
394 extern OCTAVE_API FloatMatrix xgemm (const FloatMatrix& a, const FloatMatrix& b,
395                                      blas_trans_type transa = blas_no_trans,
396                                      blas_trans_type transb = blas_no_trans);
397 
398 extern OCTAVE_API FloatMatrix operator * (const FloatMatrix& a,
399                                           const FloatMatrix& b);
400 
401 extern OCTAVE_API FloatMatrix min (float d, const FloatMatrix& m);
402 extern OCTAVE_API FloatMatrix min (const FloatMatrix& m, float d);
403 extern OCTAVE_API FloatMatrix min (const FloatMatrix& a, const FloatMatrix& b);
404 
405 extern OCTAVE_API FloatMatrix max (float d, const FloatMatrix& m);
406 extern OCTAVE_API FloatMatrix max (const FloatMatrix& m, float d);
407 extern OCTAVE_API FloatMatrix max (const FloatMatrix& a, const FloatMatrix& b);
408 
409 extern OCTAVE_API FloatMatrix linspace (const FloatColumnVector& x1,
410                                         const FloatColumnVector& x2,
411                                         octave_idx_type n);
412 
413 MS_CMP_OP_DECLS (FloatMatrix, float, OCTAVE_API)
414 MS_BOOL_OP_DECLS (FloatMatrix, float, OCTAVE_API)
415 
416 SM_CMP_OP_DECLS (float, FloatMatrix, OCTAVE_API)
417 SM_BOOL_OP_DECLS (float, FloatMatrix, OCTAVE_API)
418 
419 MM_CMP_OP_DECLS (FloatMatrix, FloatMatrix, OCTAVE_API)
420 MM_BOOL_OP_DECLS (FloatMatrix, FloatMatrix, OCTAVE_API)
421 
422 MARRAY_FORWARD_DEFS (MArray, FloatMatrix, float)
423 
424 template <typename T>
425 void read_int (std::istream& is, bool swap_bytes, T& val);
426 
427 #endif
428