GNU Octave  8.1.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-2023 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 
52 
53 // diagonal matrix by sparse matrix ops
54 
55 DEFBINOP (mul_dm_scm, diag_matrix, sparse_complex_matrix)
56 {
57  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
59  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
60 
61  if (v2.rows () == 1 && v2.columns () == 1)
62  // If v2 is a scalar in disguise, return a diagonal matrix rather than
63  // a sparse matrix.
64  {
65  std::complex<double> d = v2.complex_value ();
66 
67  return octave_value (v1.diag_matrix_value () * d);
68  }
69  else
70  {
71  MatrixType typ = v2.matrix_type ();
74  octave_value out = octave_value (ret);
75  typ.mark_as_unsymmetric ();
76  out.matrix_type (typ);
77  return out;
78  }
79 }
80 
81 DEFBINOP (mul_cdm_sm, complex_diag_matrix, sparse_matrix)
82 {
84  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
86  = dynamic_cast<const octave_sparse_matrix&> (a2);
87 
88  if (v2.rows () == 1 && v2.columns () == 1)
89  // If v2 is a scalar in disguise, return a diagonal matrix rather than
90  // a sparse matrix.
91  {
92  std::complex<double> d = v2.scalar_value ();
93 
94  return octave_value (v1.complex_diag_matrix_value () * d);
95  }
96  else
97  {
98  MatrixType typ = v2.matrix_type ();
101  octave_value out = octave_value (ret);
102  typ.mark_as_unsymmetric ();
103  out.matrix_type (typ);
104  return out;
105  }
106 }
107 
108 DEFBINOP (mul_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
109 {
111  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
113  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
114 
115  if (v2.rows () == 1 && v2.columns () == 1)
116  // If v2 is a scalar in disguise, return a diagonal matrix rather than
117  // a sparse matrix.
118  {
119  std::complex<double> d = v2.complex_value ();
120 
121  return octave_value (v1.complex_diag_matrix_value () * d);
122  }
123  else
124  {
125  MatrixType typ = v2.matrix_type ();
128  octave_value out = octave_value (ret);
129  typ.mark_as_unsymmetric ();
130  out.matrix_type (typ);
131  return out;
132  }
133 }
134 
135 DEFBINOP (ldiv_dm_scm, diag_matrix, sparse_complex_matrix)
136 {
137  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
139  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
140 
141  MatrixType typ = v2.matrix_type ();
143  typ);
144 }
145 
146 DEFBINOP (ldiv_cdm_sm, complex_diag_matrix, sparse_matrix)
147 {
149  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
150  const octave_sparse_matrix& v2
151  = dynamic_cast<const octave_sparse_matrix&> (a2);
152 
153  MatrixType typ = v2.matrix_type ();
155  typ);
156 }
157 
158 DEFBINOP (ldiv_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
159 {
161  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
163  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
164 
165  MatrixType typ = v2.matrix_type ();
166  return xleftdiv (v1.complex_diag_matrix_value (),
168  typ);
169 }
170 
171 DEFBINOP (add_dm_scm, diag_matrix, sparse_complex_matrix)
172 {
173  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
175  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
176 
177  if (v2.rows () == 1 && v2.columns () == 1)
178  // If v2 is a scalar in disguise, return a diagonal matrix rather than
179  // a sparse matrix.
180  {
181  std::complex<double> d = v2.complex_value ();
182 
183  return octave_value (v1.matrix_value () + d);
184  }
185  else
187 }
188 
189 DEFBINOP (add_cdm_sm, complex_diag_matrix, sparse_matrix)
190 {
192  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
193  const octave_sparse_matrix& v2
194  = dynamic_cast<const octave_sparse_matrix&> (a2);
195 
196  if (v2.rows () == 1 && v2.columns () == 1)
197  // If v2 is a scalar in disguise, return a diagonal matrix rather than
198  // a sparse matrix.
199  {
200  double d = v2.scalar_value ();
201 
202  return octave_value (v1.complex_matrix_value () + d);
203  }
204  else
206 }
207 
208 DEFBINOP (add_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
209 {
211  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
213  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
214 
215  if (v2.rows () == 1 && v2.columns () == 1)
216  // If v2 is a scalar in disguise, return a diagonal matrix rather than
217  // a sparse matrix.
218  {
219  std::complex<double> d = v2.complex_value ();
220 
221  return octave_value (v1.complex_matrix_value () + d);
222  }
223  else
225 }
226 
227 DEFBINOP (sub_dm_scm, diag_matrix, sparse_complex_matrix)
228 {
229  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
231  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
232 
233  if (v2.rows () == 1 && v2.columns () == 1)
234  // If v2 is a scalar in disguise, return a diagonal matrix rather than
235  // a sparse matrix.
236  {
237  std::complex<double> d = v2.complex_value ();
238 
239  return octave_value (v1.matrix_value () + (-d));
240  }
241  else
243 }
244 
245 DEFBINOP (sub_cdm_sm, complex_diag_matrix, sparse_matrix)
246 {
248  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
249  const octave_sparse_matrix& v2
250  = dynamic_cast<const octave_sparse_matrix&> (a2);
251 
252  if (v2.rows () == 1 && v2.columns () == 1)
253  // If v2 is a scalar in disguise, return a diagonal matrix rather than
254  // a sparse matrix.
255  {
256  double d = v2.scalar_value ();
257 
258  return octave_value (v1.complex_matrix_value () + (-d));
259  }
260  else
262 }
263 
264 DEFBINOP (sub_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
265 {
267  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
269  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
270 
271  if (v2.rows () == 1 && v2.columns () == 1)
272  // If v2 is a scalar in disguise, return a diagonal matrix rather than
273  // a sparse matrix.
274  {
275  std::complex<double> d = v2.complex_value ();
276 
277  return octave_value (v1.complex_matrix_value () + (-d));
278  }
279  else
281 }
282 
283 // sparse matrix by diagonal matrix ops
284 
285 DEFBINOP (mul_scm_dm, sparse_complex_matrix, diag_matrix)
286 {
288  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
289  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
290 
291  if (v1.rows () == 1 && v1.columns () == 1)
292  // If v1 is a scalar in disguise, return a diagonal matrix rather than
293  // a sparse matrix.
294  {
295  std::complex<double> d = v1.complex_value ();
296 
297  return octave_value (d * v2.diag_matrix_value ());
298  }
299  else
300  {
301  MatrixType typ = v1.matrix_type ();
304  octave_value out = octave_value (ret);
305  typ.mark_as_unsymmetric ();
306  out.matrix_type (typ);
307  return out;
308  }
309 }
310 
311 DEFBINOP (mul_sm_cdm, sparse_matrix, complex_diag_matrix)
312 {
313  const octave_sparse_matrix& v1
314  = dynamic_cast<const octave_sparse_matrix&> (a1);
316  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
317 
318  if (v1.rows () == 1 && v1.columns () == 1)
319  // If v1 is a scalar in disguise, return a diagonal matrix rather than
320  // a sparse matrix.
321  {
322  std::complex<double> d = v1.complex_value ();
323 
325  }
326  else
327  {
328  MatrixType typ = v1.matrix_type ();
331  octave_value out = octave_value (ret);
332  typ.mark_as_unsymmetric ();
333  out.matrix_type (typ);
334  return out;
335  }
336 }
337 
338 DEFBINOP (mul_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
339 {
341  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
343  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
344 
345  if (v1.rows () == 1 && v1.columns () == 1)
346  // If v1 is a scalar in disguise, return a diagonal matrix rather than
347  // a sparse matrix.
348  {
349  std::complex<double> d = v1.complex_value ();
350 
352  }
353  else if (v2.rows () == 1 && v2.columns () == 1)
354  // If v2 is a scalar in disguise, don't bother with further dispatching.
355  {
356  std::complex<double> d = v2.complex_value ();
357 
358  return octave_value (v1.sparse_complex_matrix_value () * d);
359  }
360  else
361  {
362  MatrixType typ = v1.matrix_type ();
365  octave_value out = octave_value (ret);
366  typ.mark_as_unsymmetric ();
367  out.matrix_type (typ);
368  return out;
369  }
370 }
371 
372 DEFBINOP (div_scm_dm, sparse_complex_matrix, diag_matrix)
373 {
375  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
376  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
377 
378  if (v2.rows () == 1 && v2.columns () == 1)
380  else
381  {
382  MatrixType typ = v2.matrix_type ();
383  return xdiv (v1.sparse_complex_matrix_value (),
384  v2.diag_matrix_value (), typ);
385  }
386 }
387 
388 DEFBINOP (div_sm_cdm, sparse_matrix, complex_diag_matrix)
389 {
390  const octave_sparse_matrix& v1
391  = dynamic_cast<const octave_sparse_matrix&> (a1);
393  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
394 
395  if (v2.rows () == 1 && v2.columns () == 1)
396  return octave_value (v1.sparse_matrix_value () / v2.complex_value ());
397  else
398  {
399  MatrixType typ = v2.matrix_type ();
400  return xdiv (v1.sparse_matrix_value (),
401  v2.complex_diag_matrix_value (), typ);
402  }
403 }
404 
405 DEFBINOP (div_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
406 {
408  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
410  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
411 
412  if (v2.rows () == 1 && v2.columns () == 1)
414  else
415  {
416  MatrixType typ = v2.matrix_type ();
417  return xdiv (v1.sparse_complex_matrix_value (),
418  v2.complex_diag_matrix_value (), typ);
419  }
420 }
421 
422 DEFBINOP (add_sm_cdm, sparse_matrix, complex_diag_matrix)
423 {
424  const octave_sparse_matrix& v1
425  = dynamic_cast<const octave_sparse_matrix&> (a1);
427  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
428 
429  if (v2.rows () == 1 && v2.columns () == 1)
430  // If v2 is a scalar in disguise, return a diagonal matrix rather than
431  // a sparse matrix.
432  {
433  std::complex<double> d = v2.complex_value ();
434 
435  return octave_value (v1.sparse_matrix_value () + d);
436  }
437  else
439 }
440 
441 DEFBINOP (add_scm_dm, sparse_complex_matrix, diag_matrix)
442 {
444  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
445  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
446 
447  if (v2.rows () == 1 && v2.columns () == 1)
448  // If v2 is a scalar in disguise, return a diagonal matrix rather than
449  // a sparse matrix.
450  {
451  double d = v2.scalar_value ();
452 
453  return octave_value (v1.sparse_complex_matrix_value () + d);
454  }
455  else
457 }
458 
459 DEFBINOP (add_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
460 {
462  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
464  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
465 
466  if (v2.rows () == 1 && v2.columns () == 1)
467  // If v2 is a scalar in disguise, return a diagonal matrix rather than
468  // a sparse matrix.
469  {
470  std::complex<double> d = v2.complex_value ();
471 
472  return octave_value (v1.sparse_complex_matrix_value () + d);
473  }
474  else
476 }
477 
478 DEFBINOP (sub_sm_cdm, sparse_matrix, complex_diag_matrix)
479 {
480  const octave_sparse_matrix& v1
481  = dynamic_cast<const octave_sparse_matrix&> (a1);
483  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
484 
485  if (v2.rows () == 1 && v2.columns () == 1)
486  // If v2 is a scalar in disguise, return a diagonal matrix rather than
487  // a sparse matrix.
488  {
489  std::complex<double> d = v2.complex_value ();
490 
491  return octave_value (v1.sparse_matrix_value () + (-d));
492  }
493  else
495 }
496 
497 DEFBINOP (sub_scm_dm, sparse_complex_matrix, diag_matrix)
498 {
500  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
501  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
502 
503  if (v2.rows () == 1 && v2.columns () == 1)
504  // If v2 is a scalar in disguise, return a diagonal matrix rather than
505  // a sparse matrix.
506  {
507  double d = v2.scalar_value ();
508 
509  return octave_value (v1.sparse_complex_matrix_value () + (-d));
510  }
511  else
513 }
514 
515 DEFBINOP (sub_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
516 {
518  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
520  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
521 
522  if (v2.rows () == 1 && v2.columns () == 1)
523  // If v2 is a scalar in disguise, return a diagonal matrix rather than
524  // a sparse matrix.
525  {
526  std::complex<double> d = v2.complex_value ();
527 
528  return octave_value (v1.sparse_complex_matrix_value () + (-d));
529  }
530  else
532 }
533 
534 void
535 install_dm_scm_ops (octave::type_info& ti)
536 {
538  mul_dm_scm);
540  mul_cdm_sm);
542  octave_sparse_complex_matrix, mul_cdm_scm);
544  ldiv_dm_scm);
546  ldiv_cdm_sm);
548  octave_sparse_complex_matrix, ldiv_cdm_scm);
549 
551  add_dm_scm);
553  add_cdm_sm);
555  octave_sparse_complex_matrix, add_cdm_scm);
557  sub_dm_scm);
559  sub_cdm_sm);
561  octave_sparse_complex_matrix, sub_cdm_scm);
562 
564  mul_scm_dm);
566  mul_sm_cdm);
568  octave_complex_diag_matrix, mul_scm_cdm);
569 
571  div_scm_dm);
573  div_sm_cdm);
575  octave_complex_diag_matrix, div_scm_cdm);
576 
578  add_scm_dm);
580  add_sm_cdm);
582  octave_complex_diag_matrix, add_scm_cdm);
584  sub_scm_dm);
586  sub_sm_cdm);
588  octave_complex_diag_matrix, sub_scm_cdm);
589 }
590 
OCTAVE_END_NAMESPACE(octave)
OCTAVE_API void mark_as_unsymmetric(void)
Definition: MatrixType.cc:920
OCTINTERP_API Matrix matrix_value(bool=false) const
OCTINTERP_API ComplexMatrix complex_matrix_value(bool=false) const
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:136
MatrixType matrix_type(void) const
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-base.cc:637
octave_idx_type columns(void) const
Definition: ov-base.h:363
virtual DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-base.cc:657
virtual SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-base.cc:643
octave_idx_type rows(void) const
Definition: ov-base.h:356
virtual ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-base.cc:670
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
OCTINTERP_API 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
Definition: ov-re-sparse.cc:86
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:125
MatrixType matrix_type(void) const
Definition: ov.h:628
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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:535
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:1868
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1869
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1870
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1874
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1871
Matrix xdiv(const Matrix &a, const SparseMatrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:137
Matrix xleftdiv(const SparseMatrix &a, const Matrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:468