GNU Octave  9.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-2024 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 {
59 
60  if (v2.rows () == 1 && v2.columns () == 1)
61  // If v2 is a scalar in disguise, return a diagonal matrix rather than
62  // a sparse matrix.
63  {
64  std::complex<double> d = v2.complex_value ();
65 
66  return octave_value (v1.diag_matrix_value () * d);
67  }
68  else
69  {
70  MatrixType typ = v2.matrix_type ();
71  SparseComplexMatrix ret = v1.diag_matrix_value () *
73  octave_value out = octave_value (ret);
74  typ.mark_as_unsymmetric ();
75  out.matrix_type (typ);
76  return out;
77  }
78 }
79 
80 DEFBINOP (mul_cdm_sm, complex_diag_matrix, sparse_matrix)
81 {
84 
85  if (v2.rows () == 1 && v2.columns () == 1)
86  // If v2 is a scalar in disguise, return a diagonal matrix rather than
87  // a sparse matrix.
88  {
89  std::complex<double> d = v2.scalar_value ();
90 
91  return octave_value (v1.complex_diag_matrix_value () * d);
92  }
93  else
94  {
95  MatrixType typ = v2.matrix_type ();
96  SparseComplexMatrix ret = v1.complex_diag_matrix_value () *
98  octave_value out = octave_value (ret);
99  typ.mark_as_unsymmetric ();
100  out.matrix_type (typ);
101  return out;
102  }
103 }
104 
105 DEFBINOP (mul_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
106 {
109 
110  if (v2.rows () == 1 && v2.columns () == 1)
111  // If v2 is a scalar in disguise, return a diagonal matrix rather than
112  // a sparse matrix.
113  {
114  std::complex<double> d = v2.complex_value ();
115 
116  return octave_value (v1.complex_diag_matrix_value () * d);
117  }
118  else
119  {
120  MatrixType typ = v2.matrix_type ();
121  SparseComplexMatrix ret = v1.complex_diag_matrix_value () *
123  octave_value out = octave_value (ret);
124  typ.mark_as_unsymmetric ();
125  out.matrix_type (typ);
126  return out;
127  }
128 }
129 
130 DEFBINOP (ldiv_dm_scm, diag_matrix, sparse_complex_matrix)
131 {
134 
135  MatrixType typ = v2.matrix_type ();
136  return xleftdiv (v1.diag_matrix_value (), v2.sparse_complex_matrix_value (),
137  typ);
138 }
139 
140 DEFBINOP (ldiv_cdm_sm, complex_diag_matrix, sparse_matrix)
141 {
144 
145  MatrixType typ = v2.matrix_type ();
146  return xleftdiv (v1.complex_diag_matrix_value (), v2.sparse_matrix_value (),
147  typ);
148 }
149 
150 DEFBINOP (ldiv_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
151 {
154 
155  MatrixType typ = v2.matrix_type ();
156  return xleftdiv (v1.complex_diag_matrix_value (),
158  typ);
159 }
160 
161 DEFBINOP (add_dm_scm, diag_matrix, sparse_complex_matrix)
162 {
165 
166  if (v2.rows () == 1 && v2.columns () == 1)
167  // If v2 is a scalar in disguise, return a diagonal matrix rather than
168  // a sparse matrix.
169  {
170  std::complex<double> d = v2.complex_value ();
171 
172  return octave_value (v1.matrix_value () + d);
173  }
174  else
175  return v1.diag_matrix_value () + v2.sparse_complex_matrix_value ();
176 }
177 
178 DEFBINOP (add_cdm_sm, complex_diag_matrix, sparse_matrix)
179 {
182 
183  if (v2.rows () == 1 && v2.columns () == 1)
184  // If v2 is a scalar in disguise, return a diagonal matrix rather than
185  // a sparse matrix.
186  {
187  double d = v2.scalar_value ();
188 
189  return octave_value (v1.complex_matrix_value () + d);
190  }
191  else
192  return v1.complex_diag_matrix_value () + v2.sparse_matrix_value ();
193 }
194 
195 DEFBINOP (add_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
196 {
199 
200  if (v2.rows () == 1 && v2.columns () == 1)
201  // If v2 is a scalar in disguise, return a diagonal matrix rather than
202  // a sparse matrix.
203  {
204  std::complex<double> d = v2.complex_value ();
205 
206  return octave_value (v1.complex_matrix_value () + d);
207  }
208  else
209  return v1.complex_diag_matrix_value () + v2.sparse_complex_matrix_value ();
210 }
211 
212 DEFBINOP (sub_dm_scm, diag_matrix, sparse_complex_matrix)
213 {
216 
217  if (v2.rows () == 1 && v2.columns () == 1)
218  // If v2 is a scalar in disguise, return a diagonal matrix rather than
219  // a sparse matrix.
220  {
221  std::complex<double> d = v2.complex_value ();
222 
223  return octave_value (v1.matrix_value () + (-d));
224  }
225  else
226  return v1.diag_matrix_value () - v2.sparse_complex_matrix_value ();
227 }
228 
229 DEFBINOP (sub_cdm_sm, complex_diag_matrix, sparse_matrix)
230 {
233 
234  if (v2.rows () == 1 && v2.columns () == 1)
235  // If v2 is a scalar in disguise, return a diagonal matrix rather than
236  // a sparse matrix.
237  {
238  double d = v2.scalar_value ();
239 
240  return octave_value (v1.complex_matrix_value () + (-d));
241  }
242  else
243  return v1.complex_diag_matrix_value () - v2.sparse_matrix_value ();
244 }
245 
246 DEFBINOP (sub_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
247 {
250 
251  if (v2.rows () == 1 && v2.columns () == 1)
252  // If v2 is a scalar in disguise, return a diagonal matrix rather than
253  // a sparse matrix.
254  {
255  std::complex<double> d = v2.complex_value ();
256 
257  return octave_value (v1.complex_matrix_value () + (-d));
258  }
259  else
260  return v1.complex_diag_matrix_value () - v2.sparse_complex_matrix_value ();
261 }
262 
263 // sparse matrix by diagonal matrix ops
264 
265 DEFBINOP (mul_scm_dm, sparse_complex_matrix, diag_matrix)
266 {
269 
270  if (v1.rows () == 1 && v1.columns () == 1)
271  // If v1 is a scalar in disguise, return a diagonal matrix rather than
272  // a sparse matrix.
273  {
274  std::complex<double> d = v1.complex_value ();
275 
276  return octave_value (d * v2.diag_matrix_value ());
277  }
278  else
279  {
280  MatrixType typ = v1.matrix_type ();
281  SparseComplexMatrix ret = v1.sparse_complex_matrix_value () *
283  octave_value out = octave_value (ret);
284  typ.mark_as_unsymmetric ();
285  out.matrix_type (typ);
286  return out;
287  }
288 }
289 
290 DEFBINOP (mul_sm_cdm, sparse_matrix, complex_diag_matrix)
291 {
294 
295  if (v1.rows () == 1 && v1.columns () == 1)
296  // If v1 is a scalar in disguise, return a diagonal matrix rather than
297  // a sparse matrix.
298  {
299  std::complex<double> d = v1.complex_value ();
300 
302  }
303  else
304  {
305  MatrixType typ = v1.matrix_type ();
306  SparseComplexMatrix ret = v1.sparse_matrix_value () *
308  octave_value out = octave_value (ret);
309  typ.mark_as_unsymmetric ();
310  out.matrix_type (typ);
311  return out;
312  }
313 }
314 
315 DEFBINOP (mul_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
316 {
319 
320  if (v1.rows () == 1 && v1.columns () == 1)
321  // If v1 is a scalar in disguise, return a diagonal matrix rather than
322  // a sparse matrix.
323  {
324  std::complex<double> d = v1.complex_value ();
325 
327  }
328  else if (v2.rows () == 1 && v2.columns () == 1)
329  // If v2 is a scalar in disguise, don't bother with further dispatching.
330  {
331  std::complex<double> d = v2.complex_value ();
332 
333  return octave_value (v1.sparse_complex_matrix_value () * d);
334  }
335  else
336  {
337  MatrixType typ = v1.matrix_type ();
338  SparseComplexMatrix ret = v1.sparse_complex_matrix_value () *
340  octave_value out = octave_value (ret);
341  typ.mark_as_unsymmetric ();
342  out.matrix_type (typ);
343  return out;
344  }
345 }
346 
347 DEFBINOP (div_scm_dm, sparse_complex_matrix, diag_matrix)
348 {
351 
352  if (v2.rows () == 1 && v2.columns () == 1)
353  return octave_value (v1.sparse_complex_matrix_value () / v2.scalar_value ());
354  else
355  {
356  MatrixType typ = v2.matrix_type ();
357  return xdiv (v1.sparse_complex_matrix_value (),
358  v2.diag_matrix_value (), typ);
359  }
360 }
361 
362 DEFBINOP (div_sm_cdm, sparse_matrix, complex_diag_matrix)
363 {
366 
367  if (v2.rows () == 1 && v2.columns () == 1)
368  return octave_value (v1.sparse_matrix_value () / v2.complex_value ());
369  else
370  {
371  MatrixType typ = v2.matrix_type ();
372  return xdiv (v1.sparse_matrix_value (),
373  v2.complex_diag_matrix_value (), typ);
374  }
375 }
376 
377 DEFBINOP (div_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
378 {
381 
382  if (v2.rows () == 1 && v2.columns () == 1)
383  return octave_value (v1.sparse_complex_matrix_value () / v2.complex_value ());
384  else
385  {
386  MatrixType typ = v2.matrix_type ();
387  return xdiv (v1.sparse_complex_matrix_value (),
388  v2.complex_diag_matrix_value (), typ);
389  }
390 }
391 
392 DEFBINOP (add_sm_cdm, sparse_matrix, complex_diag_matrix)
393 {
396 
397  if (v2.rows () == 1 && v2.columns () == 1)
398  // If v2 is a scalar in disguise, return a diagonal matrix rather than
399  // a sparse matrix.
400  {
401  std::complex<double> d = v2.complex_value ();
402 
403  return octave_value (v1.sparse_matrix_value () + d);
404  }
405  else
406  return v1.sparse_matrix_value () + v2.complex_diag_matrix_value ();
407 }
408 
409 DEFBINOP (add_scm_dm, sparse_complex_matrix, diag_matrix)
410 {
413 
414  if (v2.rows () == 1 && v2.columns () == 1)
415  // If v2 is a scalar in disguise, return a diagonal matrix rather than
416  // a sparse matrix.
417  {
418  double d = v2.scalar_value ();
419 
420  return octave_value (v1.sparse_complex_matrix_value () + d);
421  }
422  else
423  return v1.sparse_complex_matrix_value () + v2.diag_matrix_value ();
424 }
425 
426 DEFBINOP (add_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
427 {
430 
431  if (v2.rows () == 1 && v2.columns () == 1)
432  // If v2 is a scalar in disguise, return a diagonal matrix rather than
433  // a sparse matrix.
434  {
435  std::complex<double> d = v2.complex_value ();
436 
437  return octave_value (v1.sparse_complex_matrix_value () + d);
438  }
439  else
440  return v1.sparse_complex_matrix_value () + v2.complex_diag_matrix_value ();
441 }
442 
443 DEFBINOP (sub_sm_cdm, sparse_matrix, complex_diag_matrix)
444 {
447 
448  if (v2.rows () == 1 && v2.columns () == 1)
449  // If v2 is a scalar in disguise, return a diagonal matrix rather than
450  // a sparse matrix.
451  {
452  std::complex<double> d = v2.complex_value ();
453 
454  return octave_value (v1.sparse_matrix_value () + (-d));
455  }
456  else
457  return v1.sparse_matrix_value () - v2.complex_diag_matrix_value ();
458 }
459 
460 DEFBINOP (sub_scm_dm, sparse_complex_matrix, diag_matrix)
461 {
464 
465  if (v2.rows () == 1 && v2.columns () == 1)
466  // If v2 is a scalar in disguise, return a diagonal matrix rather than
467  // a sparse matrix.
468  {
469  double d = v2.scalar_value ();
470 
471  return octave_value (v1.sparse_complex_matrix_value () + (-d));
472  }
473  else
474  return v1.sparse_complex_matrix_value () - v2.diag_matrix_value ();
475 }
476 
477 DEFBINOP (sub_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
478 {
481 
482  if (v2.rows () == 1 && v2.columns () == 1)
483  // If v2 is a scalar in disguise, return a diagonal matrix rather than
484  // a sparse matrix.
485  {
486  std::complex<double> d = v2.complex_value ();
487 
488  return octave_value (v1.sparse_complex_matrix_value () + (-d));
489  }
490  else
491  return v1.sparse_complex_matrix_value () - v2.complex_diag_matrix_value ();
492 }
493 
494 void
495 install_dm_scm_ops (octave::type_info& ti)
496 {
498  mul_dm_scm);
500  mul_cdm_sm);
502  octave_sparse_complex_matrix, mul_cdm_scm);
504  ldiv_dm_scm);
506  ldiv_cdm_sm);
508  octave_sparse_complex_matrix, ldiv_cdm_scm);
509 
511  add_dm_scm);
513  add_cdm_sm);
515  octave_sparse_complex_matrix, add_cdm_scm);
517  sub_dm_scm);
519  sub_cdm_sm);
521  octave_sparse_complex_matrix, sub_cdm_scm);
522 
524  mul_scm_dm);
526  mul_sm_cdm);
528  octave_complex_diag_matrix, mul_scm_cdm);
529 
531  div_scm_dm);
533  div_sm_cdm);
535  octave_complex_diag_matrix, div_scm_cdm);
536 
538  add_scm_dm);
540  add_sm_cdm);
542  octave_complex_diag_matrix, add_scm_cdm);
544  sub_scm_dm);
546  sub_sm_cdm);
548  octave_complex_diag_matrix, sub_scm_cdm);
549 }
550 
551 OCTAVE_END_NAMESPACE(octave)
void mark_as_unsymmetric()
Definition: MatrixType.cc:920
MatrixType matrix_type() const
Definition: ov-base-mat.h:139
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-base.cc:695
octave_idx_type rows() const
Definition: ov-base.h:374
octave_idx_type columns() const
Definition: ov-base.h:381
virtual DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-base.cc:715
virtual SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-base.cc:701
virtual ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-base.cc:728
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
MatrixType matrix_type() const
Definition: ov.h:583
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:495
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:245
#define OCTAVE_CAST_BASE_VALUE(T, T_VAL, BASE_VAL)
Definition: ops.h:52
#define INSTALL_BINOP_TI(ti, op, t1, t2, f)
Definition: ops.h:70
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1663
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1664
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1665
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1669
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1666
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:469