GNU Octave  4.4.1
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-2018 Jason Riedy, Jaroslav Hajek
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "mx-cm-s.h"
28 #include "mx-s-cm.h"
29 
30 #include "mx-dm-cs.h"
31 #include "mx-cs-dm.h"
32 
33 #include "mx-m-cs.h"
34 #include "mx-cs-m.h"
35 
36 #include "errwarn.h"
37 #include "ovl.h"
38 #include "ov.h"
39 #include "ov-typeinfo.h"
40 #include "ops.h"
41 
42 #include "ov-re-diag.h"
43 #include "ov-cx-diag.h"
44 #include "ov-re-sparse.h"
45 #include "ov-cx-sparse.h"
46 
47 #include "sparse-xdiv.h"
48 
49 // diagonal matrix by sparse matrix ops
50 
51 DEFBINOP (mul_dm_scm, diag_matrix, sparse_complex_matrix)
52 {
53  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
55  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
56 
57  if (v2.rows () == 1 && v2.columns () == 1)
58  // If v2 is a scalar in disguise, return a diagonal matrix rather than
59  // a sparse matrix.
60  {
61  std::complex<double> d = v2.complex_value ();
62 
63  return octave_value (v1.diag_matrix_value () * d);
64  }
65  else
66  {
67  MatrixType typ = v2.matrix_type ();
70  octave_value out = octave_value (ret);
71  typ.mark_as_unsymmetric ();
72  out.matrix_type (typ);
73  return out;
74  }
75 }
76 
77 DEFBINOP (mul_cdm_sm, complex_diag_matrix, sparse_matrix)
78 {
80  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
82  = dynamic_cast<const octave_sparse_matrix&> (a2);
83 
84  if (v2.rows () == 1 && v2.columns () == 1)
85  // If v2 is a scalar in disguise, return a diagonal matrix rather than
86  // a sparse matrix.
87  {
88  std::complex<double> d = v2.scalar_value ();
89 
90  return octave_value (v1.complex_diag_matrix_value () * d);
91  }
92  else
93  {
94  MatrixType typ = v2.matrix_type ();
97  octave_value out = octave_value (ret);
98  typ.mark_as_unsymmetric ();
99  out.matrix_type (typ);
100  return out;
101  }
102 }
103 
104 DEFBINOP (mul_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
105 {
107  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
109  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
110 
111  if (v2.rows () == 1 && v2.columns () == 1)
112  // If v2 is a scalar in disguise, return a diagonal matrix rather than
113  // a sparse matrix.
114  {
115  std::complex<double> d = v2.complex_value ();
116 
117  return octave_value (v1.complex_diag_matrix_value () * d);
118  }
119  else
120  {
121  MatrixType typ = v2.matrix_type ();
124  octave_value out = octave_value (ret);
125  typ.mark_as_unsymmetric ();
126  out.matrix_type (typ);
127  return out;
128  }
129 }
130 
131 DEFBINOP (ldiv_dm_scm, diag_matrix, sparse_complex_matrix)
132 {
133  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
135  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
136 
137  MatrixType typ = v2.matrix_type ();
139  typ);
140 }
141 
142 DEFBINOP (ldiv_cdm_sm, complex_diag_matrix, sparse_matrix)
143 {
145  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
146  const octave_sparse_matrix& v2
147  = dynamic_cast<const octave_sparse_matrix&> (a2);
148 
149  MatrixType typ = v2.matrix_type ();
151  typ);
152 }
153 
154 DEFBINOP (ldiv_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
155 {
157  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
159  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
160 
161  MatrixType typ = v2.matrix_type ();
162  return xleftdiv (v1.complex_diag_matrix_value (),
164  typ);
165 }
166 
167 DEFBINOP (add_dm_scm, diag_matrix, sparse_complex_matrix)
168 {
169  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
171  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
172 
173  if (v2.rows () == 1 && v2.columns () == 1)
174  // If v2 is a scalar in disguise, return a diagonal matrix rather than
175  // a sparse matrix.
176  {
177  std::complex<double> d = v2.complex_value ();
178 
179  return octave_value (v1.matrix_value () + d);
180  }
181  else
183 }
184 
185 DEFBINOP (add_cdm_sm, complex_diag_matrix, sparse_matrix)
186 {
188  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
189  const octave_sparse_matrix& v2
190  = dynamic_cast<const octave_sparse_matrix&> (a2);
191 
192  if (v2.rows () == 1 && v2.columns () == 1)
193  // If v2 is a scalar in disguise, return a diagonal matrix rather than
194  // a sparse matrix.
195  {
196  double d = v2.scalar_value ();
197 
198  return octave_value (v1.complex_matrix_value () + d);
199  }
200  else
202 }
203 
204 DEFBINOP (add_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
205 {
207  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
209  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
210 
211  if (v2.rows () == 1 && v2.columns () == 1)
212  // If v2 is a scalar in disguise, return a diagonal matrix rather than
213  // a sparse matrix.
214  {
215  std::complex<double> d = v2.complex_value ();
216 
217  return octave_value (v1.complex_matrix_value () + d);
218  }
219  else
221 }
222 
223 DEFBINOP (sub_dm_scm, diag_matrix, sparse_complex_matrix)
224 {
225  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
227  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
228 
229  if (v2.rows () == 1 && v2.columns () == 1)
230  // If v2 is a scalar in disguise, return a diagonal matrix rather than
231  // a sparse matrix.
232  {
233  std::complex<double> d = v2.complex_value ();
234 
235  return octave_value (v1.matrix_value () + (-d));
236  }
237  else
239 }
240 
241 DEFBINOP (sub_cdm_sm, complex_diag_matrix, sparse_matrix)
242 {
244  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
245  const octave_sparse_matrix& v2
246  = dynamic_cast<const octave_sparse_matrix&> (a2);
247 
248  if (v2.rows () == 1 && v2.columns () == 1)
249  // If v2 is a scalar in disguise, return a diagonal matrix rather than
250  // a sparse matrix.
251  {
252  double d = v2.scalar_value ();
253 
254  return octave_value (v1.complex_matrix_value () + (-d));
255  }
256  else
258 }
259 
260 DEFBINOP (sub_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
261 {
263  = dynamic_cast<const octave_complex_diag_matrix&> (a1);
265  = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
266 
267  if (v2.rows () == 1 && v2.columns () == 1)
268  // If v2 is a scalar in disguise, return a diagonal matrix rather than
269  // a sparse matrix.
270  {
271  std::complex<double> d = v2.complex_value ();
272 
273  return octave_value (v1.complex_matrix_value () + (-d));
274  }
275  else
277 }
278 
279 // sparse matrix by diagonal matrix ops
280 
281 DEFBINOP (mul_scm_dm, sparse_complex_matrix, diag_matrix)
282 {
284  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
285  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
286 
287  if (v1.rows () == 1 && v1.columns () == 1)
288  // If v1 is a scalar in disguise, return a diagonal matrix rather than
289  // a sparse matrix.
290  {
291  std::complex<double> d = v1.complex_value ();
292 
293  return octave_value (d * v2.diag_matrix_value ());
294  }
295  else
296  {
297  MatrixType typ = v1.matrix_type ();
300  octave_value out = octave_value (ret);
301  typ.mark_as_unsymmetric ();
302  out.matrix_type (typ);
303  return out;
304  }
305 }
306 
307 DEFBINOP (mul_sm_cdm, sparse_matrix, complex_diag_matrix)
308 {
309  const octave_sparse_matrix& v1
310  = dynamic_cast<const octave_sparse_matrix&> (a1);
312  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
313 
314  if (v1.rows () == 1 && v1.columns () == 1)
315  // If v1 is a scalar in disguise, return a diagonal matrix rather than
316  // a sparse matrix.
317  {
318  std::complex<double> d = v1.complex_value ();
319 
321  }
322  else
323  {
324  MatrixType typ = v1.matrix_type ();
327  octave_value out = octave_value (ret);
328  typ.mark_as_unsymmetric ();
329  out.matrix_type (typ);
330  return out;
331  }
332 }
333 
334 DEFBINOP (mul_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
335 {
337  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
339  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
340 
341  if (v1.rows () == 1 && v1.columns () == 1)
342  // If v1 is a scalar in disguise, return a diagonal matrix rather than
343  // a sparse matrix.
344  {
345  std::complex<double> d = v1.complex_value ();
346 
348  }
349  else if (v2.rows () == 1 && v2.columns () == 1)
350  // If v2 is a scalar in disguise, don't bother with further dispatching.
351  {
352  std::complex<double> d = v2.complex_value ();
353 
354  return octave_value (v1.sparse_complex_matrix_value () * d);
355  }
356  else
357  {
358  MatrixType typ = v1.matrix_type ();
361  octave_value out = octave_value (ret);
362  typ.mark_as_unsymmetric ();
363  out.matrix_type (typ);
364  return out;
365  }
366 }
367 
368 DEFBINOP (div_scm_dm, sparse_complex_matrix, diag_matrix)
369 {
371  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
372  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
373 
374  if (v2.rows () == 1 && v2.columns () == 1)
375  {
376  double d = v2.scalar_value ();
377 
378  if (d == 0.0)
380 
381  return octave_value (v1.sparse_complex_matrix_value () / d);
382  }
383  else
384  {
385  MatrixType typ = v2.matrix_type ();
386  return xdiv (v1.sparse_complex_matrix_value (),
387  v2.diag_matrix_value (), typ);
388  }
389 }
390 
391 DEFBINOP (div_sm_cdm, sparse_matrix, complex_diag_matrix)
392 {
393  const octave_sparse_matrix& v1
394  = dynamic_cast<const octave_sparse_matrix&> (a1);
396  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
397 
398  if (v2.rows () == 1 && v2.columns () == 1)
399  {
400  std::complex<double> d = v2.complex_value ();
401 
402  if (d == 0.0)
404 
405  return octave_value (v1.sparse_matrix_value () / d);
406  }
407  else
408  {
409  MatrixType typ = v2.matrix_type ();
410  return xdiv (v1.sparse_matrix_value (),
411  v2.complex_diag_matrix_value (), typ);
412  }
413 }
414 
415 DEFBINOP (div_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
416 {
418  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
420  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
421 
422  if (v2.rows () == 1 && v2.columns () == 1)
423  {
424  std::complex<double> d = v2.complex_value ();
425 
426  if (d == 0.0)
428 
429  return octave_value (v1.sparse_complex_matrix_value () / d);
430  }
431  else
432  {
433  MatrixType typ = v2.matrix_type ();
434  return xdiv (v1.sparse_complex_matrix_value (),
435  v2.complex_diag_matrix_value (), typ);
436  }
437 }
438 
439 DEFBINOP (add_sm_cdm, sparse_matrix, complex_diag_matrix)
440 {
441  const octave_sparse_matrix& v1
442  = dynamic_cast<const octave_sparse_matrix&> (a1);
444  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
445 
446  if (v2.rows () == 1 && v2.columns () == 1)
447  // If v2 is a scalar in disguise, return a diagonal matrix rather than
448  // a sparse matrix.
449  {
450  std::complex<double> d = v2.complex_value ();
451 
452  return octave_value (v1.sparse_matrix_value () + d);
453  }
454  else
456 }
457 
458 DEFBINOP (add_scm_dm, sparse_complex_matrix, diag_matrix)
459 {
461  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
462  const octave_diag_matrix& v2 = dynamic_cast<const octave_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  double d = v2.scalar_value ();
469 
470  return octave_value (v1.sparse_complex_matrix_value () + d);
471  }
472  else
474 }
475 
476 DEFBINOP (add_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
477 {
479  = dynamic_cast<const octave_sparse_complex_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_complex_matrix_value () + d);
490  }
491  else
493 }
494 
495 DEFBINOP (sub_sm_cdm, sparse_matrix, complex_diag_matrix)
496 {
497  const octave_sparse_matrix& v1
498  = dynamic_cast<const octave_sparse_matrix&> (a1);
500  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
501 
502  if (v2.rows () == 1 && v2.columns () == 1)
503  // If v2 is a scalar in disguise, return a diagonal matrix rather than
504  // a sparse matrix.
505  {
506  std::complex<double> d = v2.complex_value ();
507 
508  return octave_value (v1.sparse_matrix_value () + (-d));
509  }
510  else
512 }
513 
514 DEFBINOP (sub_scm_dm, sparse_complex_matrix, diag_matrix)
515 {
517  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
518  const octave_diag_matrix& v2 = dynamic_cast<const octave_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  double d = v2.scalar_value ();
525 
526  return octave_value (v1.sparse_complex_matrix_value () + (-d));
527  }
528  else
530 }
531 
532 DEFBINOP (sub_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
533 {
535  = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
537  = dynamic_cast<const octave_complex_diag_matrix&> (a2);
538 
539  if (v2.rows () == 1 && v2.columns () == 1)
540  // If v2 is a scalar in disguise, return a diagonal matrix rather than
541  // a sparse matrix.
542  {
543  std::complex<double> d = v2.complex_value ();
544 
545  return octave_value (v1.sparse_complex_matrix_value () + (-d));
546  }
547  else
549 }
550 
551 void
553 {
555  mul_dm_scm);
557  mul_cdm_sm);
559  octave_sparse_complex_matrix, mul_cdm_scm);
561  ldiv_dm_scm);
563  ldiv_cdm_sm);
565  octave_sparse_complex_matrix, ldiv_cdm_scm);
566 
568  add_dm_scm);
570  add_cdm_sm);
572  octave_sparse_complex_matrix, add_cdm_scm);
574  sub_dm_scm);
576  sub_cdm_sm);
578  octave_sparse_complex_matrix, sub_cdm_scm);
579 
581  mul_scm_dm);
583  mul_sm_cdm);
585  octave_complex_diag_matrix, mul_scm_cdm);
586 
588  div_scm_dm);
590  div_sm_cdm);
592  octave_complex_diag_matrix, div_scm_cdm);
593 
595  add_scm_dm);
597  add_sm_cdm);
599  octave_complex_diag_matrix, add_scm_cdm);
601  sub_scm_dm);
603  sub_sm_cdm);
605  octave_complex_diag_matrix, sub_scm_cdm);
606 }
Matrix matrix_value(bool=false) const
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:137
void install_dm_scm_ops(octave::type_info &ti)
Definition: op-dm-scm.cc:552
virtual ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-base.cc:667
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-cx-diag.cc:130
double scalar_value(bool frc_str_conv=false) const
Definition: ov-ch-mat.h:103
#define DEFBINOP(name, t1, t2)
Definition: ops.h:264
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-base.cc:634
#define INSTALL_BINOP_TI(ti, op, t1, t2, f)
Definition: ops.h:53
virtual SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-base.cc:640
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
const octave_base_value & a2
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1606
Complex complex_value(bool=false) const
octave_idx_type rows(void) const
Definition: ov-base.h:316
virtual DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-base.cc:654
Complex complex_value(bool=false) const
Definition: ov-ch-mat.cc:122
void mark_as_unsymmetric(void)
Definition: MatrixType.cc:911
MatrixType matrix_type(void) const
Matrix xleftdiv(const SparseMatrix &a, const Matrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:464
void warn_divide_by_zero(void)
Definition: errwarn.cc:326
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-cx-sparse.h:123
Complex complex_value(bool=false) const
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
MatrixType matrix_type(void) const
Definition: ov.h:514
const octave_char_matrix & v2
Matrix xdiv(const Matrix &a, const SparseMatrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:133
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:124
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1603
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1609
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1604
octave_idx_type columns(void) const
Definition: ov-base.h:323
ComplexMatrix complex_matrix_value(bool=false) const
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:125
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1605