GNU Octave 7.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-2022 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
51OCTAVE_NAMESPACE_BEGIN
52
53// diagonal matrix by sparse matrix ops
54
55DEFBINOP (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);
76 out.matrix_type (typ);
77 return out;
78 }
79}
80
81DEFBINOP (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
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
108DEFBINOP (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
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
135DEFBINOP (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
146DEFBINOP (ldiv_cdm_sm, complex_diag_matrix, sparse_matrix)
147{
149 = dynamic_cast<const octave_complex_diag_matrix&> (a1);
151 = dynamic_cast<const octave_sparse_matrix&> (a2);
152
153 MatrixType typ = v2.matrix_type ();
155 typ);
156}
157
158DEFBINOP (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 ();
168 typ);
169}
170
171DEFBINOP (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
189DEFBINOP (add_cdm_sm, complex_diag_matrix, sparse_matrix)
190{
192 = dynamic_cast<const octave_complex_diag_matrix&> (a1);
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
208DEFBINOP (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
227DEFBINOP (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
245DEFBINOP (sub_cdm_sm, complex_diag_matrix, sparse_matrix)
246{
248 = dynamic_cast<const octave_complex_diag_matrix&> (a1);
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
264DEFBINOP (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
285DEFBINOP (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
311DEFBINOP (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
338DEFBINOP (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
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
372DEFBINOP (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
388DEFBINOP (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)
397 else
398 {
399 MatrixType typ = v2.matrix_type ();
400 return xdiv (v1.sparse_matrix_value (),
402 }
403}
404
405DEFBINOP (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 (),
419 }
420}
421
422DEFBINOP (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
441DEFBINOP (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
454 }
455 else
457}
458
459DEFBINOP (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
473 }
474 else
476}
477
478DEFBINOP (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
497DEFBINOP (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
515DEFBINOP (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
534void
535install_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
591OCTAVE_NAMESPACE_END
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:135
MatrixType matrix_type(void) const
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-base.cc:638
octave_idx_type columns(void) const
Definition: ov-base.h:355
virtual DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-base.cc:658
virtual SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-base.cc:644
octave_idx_type rows(void) const
Definition: ov-base.h:348
virtual ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-base.cc:671
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
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:127
MatrixType matrix_type(void) const
Definition: ov.h:628
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:228
#define INSTALL_BINOP_TI(ti, op, t1, t2, f)
Definition: ops.h:53
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1865
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1866
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1867
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1871
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1868
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