GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
op-dm-scm.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009-2015 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef 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 "gripes.h"
37 #include "oct-obj.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 {
55 
56  if (v2.rows () == 1 && v2.columns () == 1)
57  // If v2 is a scalar in disguise, return a diagonal matrix rather than
58  // a sparse matrix.
59  {
60  std::complex<double> d = v2.complex_value ();
61 
62  return octave_value (v1.diag_matrix_value () * d);
63  }
64  else
65  {
66  MatrixType typ = v2.matrix_type ();
69  octave_value out = octave_value (ret);
70  typ.mark_as_unsymmetric ();
71  out.matrix_type (typ);
72  return out;
73  }
74 }
75 
76 DEFBINOP (mul_cdm_sm, complex_diag_matrix, sparse_matrix)
77 {
79  const octave_sparse_matrix&);
80 
81  if (v2.rows () == 1 && v2.columns () == 1)
82  // If v2 is a scalar in disguise, return a diagonal matrix rather than
83  // a sparse matrix.
84  {
85  std::complex<double> d = v2.scalar_value ();
86 
88  }
89  else
90  {
91  MatrixType typ = v2.matrix_type ();
94  octave_value out = octave_value (ret);
95  typ.mark_as_unsymmetric ();
96  out.matrix_type (typ);
97  return out;
98  }
99 }
100 
101 DEFBINOP (mul_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
102 {
105 
106  if (v2.rows () == 1 && v2.columns () == 1)
107  // If v2 is a scalar in disguise, return a diagonal matrix rather than
108  // a sparse matrix.
109  {
110  std::complex<double> d = v2.complex_value ();
111 
113  }
114  else
115  {
116  MatrixType typ = v2.matrix_type ();
119  octave_value out = octave_value (ret);
120  typ.mark_as_unsymmetric ();
121  out.matrix_type (typ);
122  return out;
123  }
124 }
125 
126 DEFBINOP (ldiv_dm_scm, diag_matrix, sparse_complex_matrix)
127 {
130 
131  MatrixType typ = v2.matrix_type ();
133  typ);
134 }
135 
136 DEFBINOP (ldiv_cdm_sm, complex_diag_matrix, sparse_matrix)
137 {
139  const octave_sparse_matrix&);
140 
141  MatrixType typ = v2.matrix_type ();
143  typ);
144 }
145 
146 DEFBINOP (ldiv_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
147 {
150 
151  MatrixType typ = v2.matrix_type ();
154  typ);
155 }
156 
157 DEFBINOP (add_dm_scm, diag_matrix, sparse_complex_matrix)
158 {
161 
162  if (v2.rows () == 1 && v2.columns () == 1)
163  // If v2 is a scalar in disguise, return a diagonal matrix rather than
164  // a sparse matrix.
165  {
166  std::complex<double> d = v2.complex_value ();
167 
168  return octave_value (v1.matrix_value () + d);
169  }
170  else
172 }
173 
174 DEFBINOP (add_cdm_sm, complex_diag_matrix, sparse_matrix)
175 {
177  const octave_sparse_matrix&);
178 
179  if (v2.rows () == 1 && v2.columns () == 1)
180  // If v2 is a scalar in disguise, return a diagonal matrix rather than
181  // a sparse matrix.
182  {
183  double d = v2.scalar_value ();
184 
185  return octave_value (v1.complex_matrix_value () + d);
186  }
187  else
189 }
190 
191 DEFBINOP (add_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
192 {
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  std::complex<double> d = v2.complex_value ();
201 
202  return octave_value (v1.complex_matrix_value () + d);
203  }
204  else
206 }
207 
208 DEFBINOP (sub_dm_scm, diag_matrix, sparse_complex_matrix)
209 {
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.matrix_value () + (-d));
220  }
221  else
223 }
224 
225 DEFBINOP (sub_cdm_sm, complex_diag_matrix, sparse_matrix)
226 {
228  const octave_sparse_matrix&);
229 
230  if (v2.rows () == 1 && v2.columns () == 1)
231  // If v2 is a scalar in disguise, return a diagonal matrix rather than
232  // a sparse matrix.
233  {
234  double d = v2.scalar_value ();
235 
236  return octave_value (v1.complex_matrix_value () + (-d));
237  }
238  else
240 }
241 
242 DEFBINOP (sub_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
243 {
246 
247  if (v2.rows () == 1 && v2.columns () == 1)
248  // If v2 is a scalar in disguise, return a diagonal matrix rather than
249  // a sparse matrix.
250  {
251  std::complex<double> d = v2.complex_value ();
252 
253  return octave_value (v1.complex_matrix_value () + (-d));
254  }
255  else
257 }
258 
259 // sparse matrix by diagonal matrix ops
260 
261 DEFBINOP (mul_scm_dm, sparse_complex_matrix, diag_matrix)
262 {
264  const octave_diag_matrix&);
265 
266  if (v1.rows () == 1 && v1.columns () == 1)
267  // If v1 is a scalar in disguise, return a diagonal matrix rather than
268  // a sparse matrix.
269  {
270  std::complex<double> d = v1.complex_value ();
271 
272  return octave_value (d * v2.diag_matrix_value ());
273  }
274  else
275  {
276  MatrixType typ = v1.matrix_type ();
279  octave_value out = octave_value (ret);
280  typ.mark_as_unsymmetric ();
281  out.matrix_type (typ);
282  return out;
283  }
284 }
285 
286 DEFBINOP (mul_sm_cdm, sparse_matrix, complex_diag_matrix)
287 {
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.complex_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_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
312 {
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 
322  return octave_value (d * v2.complex_diag_matrix_value ());
323  }
324  else if (v2.rows () == 1 && v2.columns () == 1)
325  // If v2 is a scalar in disguise, don't bother with further dispatching.
326  {
327  std::complex<double> d = v2.complex_value ();
328 
330  }
331  else
332  {
333  MatrixType typ = v1.matrix_type ();
336  octave_value out = octave_value (ret);
337  typ.mark_as_unsymmetric ();
338  out.matrix_type (typ);
339  return out;
340  }
341 }
342 
343 DEFBINOP (div_scm_dm, sparse_complex_matrix, diag_matrix)
344 {
346  const octave_diag_matrix&);
347 
348  if (v2.rows () == 1 && v2.columns () == 1)
349  {
350  double d = v2.scalar_value ();
351 
352  if (d == 0.0)
354 
356  }
357  else
358  {
359  MatrixType typ = v2.matrix_type ();
361  v2.diag_matrix_value (), typ);
362  }
363 }
364 
365 DEFBINOP (div_sm_cdm, sparse_matrix, complex_diag_matrix)
366 {
369 
370  if (v2.rows () == 1 && v2.columns () == 1)
371  {
372  std::complex<double> d = v2.complex_value ();
373 
374  if (d == 0.0)
376 
377  return octave_value (v1.sparse_matrix_value () / d);
378  }
379  else
380  {
381  MatrixType typ = v2.matrix_type ();
382  return xdiv (v1.sparse_matrix_value (),
383  v2.complex_diag_matrix_value (), typ);
384  }
385 }
386 
387 DEFBINOP (div_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
388 {
391 
392  if (v2.rows () == 1 && v2.columns () == 1)
393  {
394  std::complex<double> d = v2.complex_value ();
395 
396  if (d == 0.0)
398 
400  }
401  else
402  {
403  MatrixType typ = v2.matrix_type ();
405  v2.complex_diag_matrix_value (), typ);
406  }
407 }
408 
409 DEFBINOP (add_sm_cdm, sparse_matrix, complex_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  std::complex<double> d = v2.complex_value ();
419 
420  return octave_value (v1.sparse_matrix_value () + d);
421  }
422  else
424 }
425 
426 DEFBINOP (add_scm_dm, sparse_complex_matrix, diag_matrix)
427 {
429  const octave_diag_matrix&);
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  double d = v2.scalar_value ();
436 
438  }
439  else
441 }
442 
443 DEFBINOP (add_scm_cdm, sparse_complex_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 
455  }
456  else
458 }
459 
460 DEFBINOP (sub_sm_cdm, sparse_matrix, complex_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  std::complex<double> d = v2.complex_value ();
470 
471  return octave_value (v1.sparse_matrix_value () + (-d));
472  }
473  else
475 }
476 
477 DEFBINOP (sub_scm_dm, sparse_complex_matrix, diag_matrix)
478 {
480  const octave_diag_matrix&);
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  double d = v2.scalar_value ();
487 
488  return octave_value (v1.sparse_complex_matrix_value () + (-d));
489  }
490  else
492 }
493 
494 DEFBINOP (sub_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
495 {
498 
499  if (v2.rows () == 1 && v2.columns () == 1)
500  // If v2 is a scalar in disguise, return a diagonal matrix rather than
501  // a sparse matrix.
502  {
503  std::complex<double> d = v2.complex_value ();
504 
505  return octave_value (v1.sparse_complex_matrix_value () + (-d));
506  }
507  else
509 }
510 
511 void
513 {
515  mul_dm_scm);
517  mul_cdm_sm);
519  octave_sparse_complex_matrix, mul_cdm_scm);
521  ldiv_dm_scm);
523  ldiv_cdm_sm);
525  octave_sparse_complex_matrix, ldiv_cdm_scm);
526 
528  add_dm_scm);
530  add_cdm_sm);
532  octave_sparse_complex_matrix, add_cdm_scm);
534  sub_dm_scm);
536  sub_cdm_sm);
538  octave_sparse_complex_matrix, sub_cdm_scm);
539 
541  mul_scm_dm);
543  mul_sm_cdm);
545  octave_complex_diag_matrix, mul_scm_cdm);
546 
548  div_scm_dm);
550  div_sm_cdm);
552  octave_complex_diag_matrix, div_scm_cdm);
553 
555  add_scm_dm);
557  add_sm_cdm);
559  octave_complex_diag_matrix, add_scm_cdm);
561  sub_scm_dm);
563  sub_sm_cdm);
565  octave_complex_diag_matrix, sub_scm_cdm);
566 }
double scalar_value(bool frc_str_conv=false) const
Definition: ov-ch-mat.h:102
#define DEFBINOP(name, t1, t2)
Definition: ops.h:279
virtual MatrixType matrix_type(void) const
Definition: ov-base.cc:347
virtual SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-base.cc:686
octave_idx_type columns(void) const
Definition: ov-base.h:301
void install_dm_scm_ops(void)
Definition: op-dm-scm.cc:512
const octave_base_value const Array< octave_idx_type > &ra_idx octave_int16_scalar & v1
virtual ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
Definition: ov-base.cc:721
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:120
F77_RET_T const double const double double * d
#define CAST_BINOP_ARGS(t1, t2)
Definition: ops.h:79
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1326
virtual Complex complex_value(bool=false) const
Definition: ov-base.cc:574
#define INSTALL_BINOP(op, t1, t2, f)
Definition: ops.h:46
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-base.cc:677
virtual DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-base.cc:704
void mark_as_unsymmetric(void)
Definition: MatrixType.cc:1224
Matrix xleftdiv(const SparseMatrix &a, const Matrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:466
virtual ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-base.cc:593
virtual Matrix matrix_value(bool=false) const
Definition: ov-base.cc:541
MatrixType matrix_type(void) const
Definition: ov.h:510
const octave_char_matrix & v2
Matrix xdiv(const Matrix &a, const SparseMatrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:135
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1323
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1329
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1324
Complex complex_value(bool=false) const
Definition: ov-ch-mat.cc:91
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1325
void gripe_divide_by_zero(void)
Definition: gripes.cc:195
octave_idx_type rows(void) const
Definition: ov-base.h:294
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))