GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
op-dm-scm.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2009-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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "mx-cm-s.h"
31 #include "mx-s-cm.h"
32 
33 #include "mx-dm-cs.h"
34 #include "mx-cs-dm.h"
35 
36 #include "mx-m-cs.h"
37 #include "mx-cs-m.h"
38 
39 #include "ovl.h"
40 #include "ov.h"
41 #include "ov-typeinfo.h"
42 #include "ops.h"
43 
44 #include "ov-re-diag.h"
45 #include "ov-cx-diag.h"
46 #include "ov-re-sparse.h"
47 #include "ov-cx-sparse.h"
48 
49 #include "sparse-xdiv.h"
50 
51 // diagonal matrix by sparse matrix ops
52 
53 DEFBINOP (mul_dm_scm, diag_matrix, sparse_complex_matrix)
54 {
55  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
57  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
58 
59  if (v2.rows () == 1 && v2.columns () == 1)
60  // If v2 is a scalar in disguise, return a diagonal matrix rather than
61  // a sparse matrix.
62  {
63  std::complex<double> d = v2.complex_value ();
64 
65  return octave_value (v1.diag_matrix_value () * d);
66  }
67  else
68  {
69  MatrixType typ = v2.matrix_type ();
72  octave_value out = octave_value (ret);
73  typ.mark_as_unsymmetric ();
74  out.matrix_type (typ);
75  return out;
76  }
77 }
78 
79 DEFBINOP (mul_cdm_sm, complex_diag_matrix, sparse_matrix)
80 {
82  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
84  = dynamic_cast<const octave_sparse_matrix&> (a2);
85 
86  if (v2.rows () == 1 && v2.columns () == 1)
87  // If v2 is a scalar in disguise, return a diagonal matrix rather than
88  // a sparse matrix.
89  {
90  std::complex<double> d = v2.scalar_value ();
91 
92  return octave_value (v1.complex_diag_matrix_value () * d);
93  }
94  else
95  {
96  MatrixType typ = v2.matrix_type ();
99  octave_value out = octave_value (ret);
100  typ.mark_as_unsymmetric ();
101  out.matrix_type (typ);
102  return out;
103  }
104 }
105 
106 DEFBINOP (mul_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
107 {
109  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
111  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
112 
113  if (v2.rows () == 1 && v2.columns () == 1)
114  // If v2 is a scalar in disguise, return a diagonal matrix rather than
115  // a sparse matrix.
116  {
117  std::complex<double> d = v2.complex_value ();
118 
119  return octave_value (v1.complex_diag_matrix_value () * d);
120  }
121  else
122  {
123  MatrixType typ = v2.matrix_type ();
126  octave_value out = octave_value (ret);
127  typ.mark_as_unsymmetric ();
128  out.matrix_type (typ);
129  return out;
130  }
131 }
132 
133 DEFBINOP (ldiv_dm_scm, diag_matrix, sparse_complex_matrix)
134 {
135  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
137  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
138 
139  MatrixType typ = v2.matrix_type ();
141  typ);
142 }
143 
144 DEFBINOP (ldiv_cdm_sm, complex_diag_matrix, sparse_matrix)
145 {
147  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
148  const octave_sparse_matrix& v2
149  = dynamic_cast<const octave_sparse_matrix&> (a2);
150 
151  MatrixType typ = v2.matrix_type ();
153  typ);
154 }
155 
156 DEFBINOP (ldiv_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
157 {
159  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
161  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
162 
163  MatrixType typ = v2.matrix_type ();
164  return xleftdiv (v1.complex_diag_matrix_value (),
166  typ);
167 }
168 
169 DEFBINOP (add_dm_scm, diag_matrix, sparse_complex_matrix)
170 {
171  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
173  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
174 
175  if (v2.rows () == 1 && v2.columns () == 1)
176  // If v2 is a scalar in disguise, return a diagonal matrix rather than
177  // a sparse matrix.
178  {
179  std::complex<double> d = v2.complex_value ();
180 
181  return octave_value (v1.matrix_value () + d);
182  }
183  else
185 }
186 
187 DEFBINOP (add_cdm_sm, complex_diag_matrix, sparse_matrix)
188 {
190  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
191  const octave_sparse_matrix& v2
192  = dynamic_cast<const octave_sparse_matrix&> (a2);
193 
194  if (v2.rows () == 1 && v2.columns () == 1)
195  // If v2 is a scalar in disguise, return a diagonal matrix rather than
196  // a sparse matrix.
197  {
198  double d = v2.scalar_value ();
199 
200  return octave_value (v1.complex_matrix_value () + d);
201  }
202  else
204 }
205 
206 DEFBINOP (add_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
207 {
209  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
211  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
212 
213  if (v2.rows () == 1 && v2.columns () == 1)
214  // If v2 is a scalar in disguise, return a diagonal matrix rather than
215  // a sparse matrix.
216  {
217  std::complex<double> d = v2.complex_value ();
218 
219  return octave_value (v1.complex_matrix_value () + d);
220  }
221  else
223 }
224 
225 DEFBINOP (sub_dm_scm, diag_matrix, sparse_complex_matrix)
226 {
227  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
229  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
230 
231  if (v2.rows () == 1 && v2.columns () == 1)
232  // If v2 is a scalar in disguise, return a diagonal matrix rather than
233  // a sparse matrix.
234  {
235  std::complex<double> d = v2.complex_value ();
236 
237  return octave_value (v1.matrix_value () + (-d));
238  }
239  else
241 }
242 
243 DEFBINOP (sub_cdm_sm, complex_diag_matrix, sparse_matrix)
244 {
246  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
247  const octave_sparse_matrix& v2
248  = dynamic_cast<const octave_sparse_matrix&> (a2);
249 
250  if (v2.rows () == 1 && v2.columns () == 1)
251  // If v2 is a scalar in disguise, return a diagonal matrix rather than
252  // a sparse matrix.
253  {
254  double d = v2.scalar_value ();
255 
256  return octave_value (v1.complex_matrix_value () + (-d));
257  }
258  else
260 }
261 
262 DEFBINOP (sub_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
263 {
265  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
267  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
268 
269  if (v2.rows () == 1 && v2.columns () == 1)
270  // If v2 is a scalar in disguise, return a diagonal matrix rather than
271  // a sparse matrix.
272  {
273  std::complex<double> d = v2.complex_value ();
274 
275  return octave_value (v1.complex_matrix_value () + (-d));
276  }
277  else
279 }
280 
281 // sparse matrix by diagonal matrix ops
282 
283 DEFBINOP (mul_scm_dm, sparse_complex_matrix, diag_matrix)
284 {
286  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
287  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
288 
289  if (v1.rows () == 1 && v1.columns () == 1)
290  // If v1 is a scalar in disguise, return a diagonal matrix rather than
291  // a sparse matrix.
292  {
293  std::complex<double> d = v1.complex_value ();
294 
295  return octave_value (d * v2.diag_matrix_value ());
296  }
297  else
298  {
299  MatrixType typ = v1.matrix_type ();
302  octave_value out = octave_value (ret);
303  typ.mark_as_unsymmetric ();
304  out.matrix_type (typ);
305  return out;
306  }
307 }
308 
309 DEFBINOP (mul_sm_cdm, sparse_matrix, complex_diag_matrix)
310 {
311  const octave_sparse_matrix& v1
312  = dynamic_cast<const octave_sparse_matrix&> (a1);
314  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
315 
316  if (v1.rows () == 1 && v1.columns () == 1)
317  // If v1 is a scalar in disguise, return a diagonal matrix rather than
318  // a sparse matrix.
319  {
320  std::complex<double> d = v1.complex_value ();
321 
323  }
324  else
325  {
326  MatrixType typ = v1.matrix_type ();
329  octave_value out = octave_value (ret);
330  typ.mark_as_unsymmetric ();
331  out.matrix_type (typ);
332  return out;
333  }
334 }
335 
336 DEFBINOP (mul_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
337 {
339  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
341  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
342 
343  if (v1.rows () == 1 && v1.columns () == 1)
344  // If v1 is a scalar in disguise, return a diagonal matrix rather than
345  // a sparse matrix.
346  {
347  std::complex<double> d = v1.complex_value ();
348 
350  }
351  else if (v2.rows () == 1 && v2.columns () == 1)
352  // If v2 is a scalar in disguise, don't bother with further dispatching.
353  {
354  std::complex<double> d = v2.complex_value ();
355 
356  return octave_value (v1.sparse_complex_matrix_value () * d);
357  }
358  else
359  {
360  MatrixType typ = v1.matrix_type ();
363  octave_value out = octave_value (ret);
364  typ.mark_as_unsymmetric ();
365  out.matrix_type (typ);
366  return out;
367  }
368 }
369 
370 DEFBINOP (div_scm_dm, sparse_complex_matrix, diag_matrix)
371 {
373  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
374  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
375 
376  if (v2.rows () == 1 && v2.columns () == 1)
378  else
379  {
380  MatrixType typ = v2.matrix_type ();
381  return xdiv (v1.sparse_complex_matrix_value (),
382  v2.diag_matrix_value (), typ);
383  }
384 }
385 
386 DEFBINOP (div_sm_cdm, sparse_matrix, complex_diag_matrix)
387 {
388  const octave_sparse_matrix& v1
389  = dynamic_cast<const octave_sparse_matrix&> (a1);
391  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
392 
393  if (v2.rows () == 1 && v2.columns () == 1)
394  return octave_value (v1.sparse_matrix_value () / v2.complex_value ());
395  else
396  {
397  MatrixType typ = v2.matrix_type ();
398  return xdiv (v1.sparse_matrix_value (),
399  v2.complex_diag_matrix_value (), typ);
400  }
401 }
402 
403 DEFBINOP (div_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
404 {
406  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
408  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
409 
410  if (v2.rows () == 1 && v2.columns () == 1)
412  else
413  {
414  MatrixType typ = v2.matrix_type ();
415  return xdiv (v1.sparse_complex_matrix_value (),
416  v2.complex_diag_matrix_value (), typ);
417  }
418 }
419 
420 DEFBINOP (add_sm_cdm, sparse_matrix, complex_diag_matrix)
421 {
422  const octave_sparse_matrix& v1
423  = dynamic_cast<const octave_sparse_matrix&> (a1);
425  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
426 
427  if (v2.rows () == 1 && v2.columns () == 1)
428  // If v2 is a scalar in disguise, return a diagonal matrix rather than
429  // a sparse matrix.
430  {
431  std::complex<double> d = v2.complex_value ();
432 
433  return octave_value (v1.sparse_matrix_value () + d);
434  }
435  else
437 }
438 
439 DEFBINOP (add_scm_dm, sparse_complex_matrix, diag_matrix)
440 {
442  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
443  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
444 
445  if (v2.rows () == 1 && v2.columns () == 1)
446  // If v2 is a scalar in disguise, return a diagonal matrix rather than
447  // a sparse matrix.
448  {
449  double d = v2.scalar_value ();
450 
451  return octave_value (v1.sparse_complex_matrix_value () + d);
452  }
453  else
455 }
456 
457 DEFBINOP (add_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
458 {
460  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
462  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
463 
464  if (v2.rows () == 1 && v2.columns () == 1)
465  // If v2 is a scalar in disguise, return a diagonal matrix rather than
466  // a sparse matrix.
467  {
468  std::complex<double> d = v2.complex_value ();
469 
470  return octave_value (v1.sparse_complex_matrix_value () + d);
471  }
472  else
474 }
475 
476 DEFBINOP (sub_sm_cdm, sparse_matrix, complex_diag_matrix)
477 {
478  const octave_sparse_matrix& v1
479  = dynamic_cast<const octave_sparse_matrix&> (a1);
481  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
482 
483  if (v2.rows () == 1 && v2.columns () == 1)
484  // If v2 is a scalar in disguise, return a diagonal matrix rather than
485  // a sparse matrix.
486  {
487  std::complex<double> d = v2.complex_value ();
488 
489  return octave_value (v1.sparse_matrix_value () + (-d));
490  }
491  else
493 }
494 
495 DEFBINOP (sub_scm_dm, sparse_complex_matrix, diag_matrix)
496 {
498  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
499  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
500 
501  if (v2.rows () == 1 && v2.columns () == 1)
502  // If v2 is a scalar in disguise, return a diagonal matrix rather than
503  // a sparse matrix.
504  {
505  double d = v2.scalar_value ();
506 
507  return octave_value (v1.sparse_complex_matrix_value () + (-d));
508  }
509  else
511 }
512 
513 DEFBINOP (sub_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
514 {
516  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
518  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
519 
520  if (v2.rows () == 1 && v2.columns () == 1)
521  // If v2 is a scalar in disguise, return a diagonal matrix rather than
522  // a sparse matrix.
523  {
524  std::complex<double> d = v2.complex_value ();
525 
526  return octave_value (v1.sparse_complex_matrix_value () + (-d));
527  }
528  else
530 }
531 
532 void
534 {
536  mul_dm_scm);
538  mul_cdm_sm);
540  octave_sparse_complex_matrix, mul_cdm_scm);
542  ldiv_dm_scm);
544  ldiv_cdm_sm);
546  octave_sparse_complex_matrix, ldiv_cdm_scm);
547 
549  add_dm_scm);
551  add_cdm_sm);
553  octave_sparse_complex_matrix, add_cdm_scm);
555  sub_dm_scm);
557  sub_cdm_sm);
559  octave_sparse_complex_matrix, sub_cdm_scm);
560 
562  mul_scm_dm);
564  mul_sm_cdm);
566  octave_complex_diag_matrix, mul_scm_cdm);
567 
569  div_scm_dm);
571  div_sm_cdm);
573  octave_complex_diag_matrix, div_scm_cdm);
574 
576  add_scm_dm);
578  add_sm_cdm);
580  octave_complex_diag_matrix, add_scm_cdm);
582  sub_scm_dm);
584  sub_sm_cdm);
586  octave_complex_diag_matrix, sub_scm_cdm);
587 }
void mark_as_unsymmetric(void)
Definition: MatrixType.cc:916
Matrix matrix_value(bool=false) const
ComplexMatrix complex_matrix_value(bool=false) const
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:131
MatrixType matrix_type(void) const
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-base.cc:636
octave_idx_type columns(void) const
Definition: ov-base.h:325
virtual DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-base.cc:656
virtual SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-base.cc:642
octave_idx_type rows(void) const
Definition: ov-base.h:318
virtual ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-base.cc:669
Complex complex_value(bool=false) const
Definition: ov-ch-mat.cc:127
double scalar_value(bool frc_str_conv=false) const
Definition: ov-ch-mat.h:105
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:133
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:140
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-cx-sparse.h:125
Complex complex_value(bool=false) const
Complex complex_value(bool=false) const
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:127
MatrixType matrix_type(void) const
Definition: ov.h:542
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void install_dm_scm_ops(octave::type_info &ti)
Definition: op-dm-scm.cc:533
const octave_base_value & a2
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
const octave_char_matrix & v2
#define DEFBINOP(name, t1, t2)
Definition: ops.h:230
#define INSTALL_BINOP_TI(ti, op, t1, t2, f)
Definition: ops.h:55
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1566
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1567
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1568
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1572
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1569
Matrix xdiv(const Matrix &a, const SparseMatrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:135
Matrix xleftdiv(const SparseMatrix &a, const Matrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:466