GNU Octave  3.8.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-2013 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 "gripes.h"
28 #include "oct-obj.h"
29 #include "ov.h"
30 #include "ov-typeinfo.h"
31 #include "ops.h"
32 
33 #include "ov-re-diag.h"
34 #include "ov-cx-diag.h"
35 #include "ov-re-sparse.h"
36 #include "ov-cx-sparse.h"
37 
38 #include "sparse-xdiv.h"
39 
40 // diagonal matrix by sparse matrix ops
41 
42 DEFBINOP (mul_dm_scm, diag_matrix, sparse_complex_matrix)
43 {
46 
47  if (v2.rows () == 1 && v2.columns () == 1)
48  // If v2 is a scalar in disguise, return a diagonal matrix rather than
49  // a sparse matrix.
50  {
51  std::complex<double> d = v2.complex_value ();
52 
53  return octave_value (v1.diag_matrix_value () * d);
54  }
55  else
56  {
57  MatrixType typ = v2.matrix_type ();
60  octave_value out = octave_value (ret);
61  typ.mark_as_unsymmetric ();
62  out.matrix_type (typ);
63  return out;
64  }
65 }
66 
67 DEFBINOP (mul_cdm_sm, complex_diag_matrix, sparse_matrix)
68 {
70  const octave_sparse_matrix&);
71 
72  if (v2.rows () == 1 && v2.columns () == 1)
73  // If v2 is a scalar in disguise, return a diagonal matrix rather than
74  // a sparse matrix.
75  {
76  std::complex<double> d = v2.scalar_value ();
77 
79  }
80  else
81  {
82  MatrixType typ = v2.matrix_type ();
85  octave_value out = octave_value (ret);
86  typ.mark_as_unsymmetric ();
87  out.matrix_type (typ);
88  return out;
89  }
90 }
91 
92 DEFBINOP (mul_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
93 {
96 
97  if (v2.rows () == 1 && v2.columns () == 1)
98  // If v2 is a scalar in disguise, return a diagonal matrix rather than
99  // a sparse matrix.
100  {
101  std::complex<double> d = v2.complex_value ();
102 
104  }
105  else
106  {
107  MatrixType typ = v2.matrix_type ();
110  octave_value out = octave_value (ret);
111  typ.mark_as_unsymmetric ();
112  out.matrix_type (typ);
113  return out;
114  }
115 }
116 
117 DEFBINOP (ldiv_dm_scm, diag_matrix, sparse_complex_matrix)
118 {
121 
122  MatrixType typ = v2.matrix_type ();
124  typ);
125 }
126 
127 DEFBINOP (ldiv_cdm_sm, complex_diag_matrix, sparse_matrix)
128 {
130  const octave_sparse_matrix&);
131 
132  MatrixType typ = v2.matrix_type ();
134  typ);
135 }
136 
137 DEFBINOP (ldiv_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
138 {
141 
142  MatrixType typ = v2.matrix_type ();
145  typ);
146 }
147 
148 DEFBINOP (add_dm_scm, diag_matrix, sparse_complex_matrix)
149 {
152 
153  if (v2.rows () == 1 && v2.columns () == 1)
154  // If v2 is a scalar in disguise, return a diagonal matrix rather than
155  // a sparse matrix.
156  {
157  std::complex<double> d = v2.complex_value ();
158 
159  return octave_value (v1.matrix_value () + d);
160  }
161  else
163 }
164 
165 DEFBINOP (add_cdm_sm, complex_diag_matrix, sparse_matrix)
166 {
168  const octave_sparse_matrix&);
169 
170  if (v2.rows () == 1 && v2.columns () == 1)
171  // If v2 is a scalar in disguise, return a diagonal matrix rather than
172  // a sparse matrix.
173  {
174  double d = v2.scalar_value ();
175 
176  return octave_value (v1.complex_matrix_value () + d);
177  }
178  else
180 }
181 
182 DEFBINOP (add_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
183 {
186 
187  if (v2.rows () == 1 && v2.columns () == 1)
188  // If v2 is a scalar in disguise, return a diagonal matrix rather than
189  // a sparse matrix.
190  {
191  std::complex<double> d = v2.complex_value ();
192 
193  return octave_value (v1.complex_matrix_value () + d);
194  }
195  else
197 }
198 
199 DEFBINOP (sub_dm_scm, diag_matrix, sparse_complex_matrix)
200 {
203 
204  if (v2.rows () == 1 && v2.columns () == 1)
205  // If v2 is a scalar in disguise, return a diagonal matrix rather than
206  // a sparse matrix.
207  {
208  std::complex<double> d = v2.complex_value ();
209 
210  return octave_value (v1.matrix_value () + (-d));
211  }
212  else
214 }
215 
216 DEFBINOP (sub_cdm_sm, complex_diag_matrix, sparse_matrix)
217 {
219  const octave_sparse_matrix&);
220 
221  if (v2.rows () == 1 && v2.columns () == 1)
222  // If v2 is a scalar in disguise, return a diagonal matrix rather than
223  // a sparse matrix.
224  {
225  double d = v2.scalar_value ();
226 
227  return octave_value (v1.complex_matrix_value () + (-d));
228  }
229  else
231 }
232 
233 DEFBINOP (sub_cdm_scm, complex_diag_matrix, sparse_complex_matrix)
234 {
237 
238  if (v2.rows () == 1 && v2.columns () == 1)
239  // If v2 is a scalar in disguise, return a diagonal matrix rather than
240  // a sparse matrix.
241  {
242  std::complex<double> d = v2.complex_value ();
243 
244  return octave_value (v1.complex_matrix_value () + (-d));
245  }
246  else
248 }
249 
250 // sparse matrix by diagonal matrix ops
251 
252 DEFBINOP (mul_scm_dm, sparse_complex_matrix, diag_matrix)
253 {
255  const octave_diag_matrix&);
256 
257  if (v1.rows () == 1 && v1.columns () == 1)
258  // If v1 is a scalar in disguise, return a diagonal matrix rather than
259  // a sparse matrix.
260  {
261  std::complex<double> d = v1.complex_value ();
262 
263  return octave_value (d * v2.diag_matrix_value ());
264  }
265  else
266  {
267  MatrixType typ = v1.matrix_type ();
270  octave_value out = octave_value (ret);
271  typ.mark_as_unsymmetric ();
272  out.matrix_type (typ);
273  return out;
274  }
275 }
276 
277 DEFBINOP (mul_sm_cdm, sparse_matrix, complex_diag_matrix)
278 {
281 
282  if (v1.rows () == 1 && v1.columns () == 1)
283  // If v1 is a scalar in disguise, return a diagonal matrix rather than
284  // a sparse matrix.
285  {
286  std::complex<double> d = v1.complex_value ();
287 
288  return octave_value (d * v2.complex_diag_matrix_value ());
289  }
290  else
291  {
292  MatrixType typ = v1.matrix_type ();
295  octave_value out = octave_value (ret);
296  typ.mark_as_unsymmetric ();
297  out.matrix_type (typ);
298  return out;
299  }
300 }
301 
302 DEFBINOP (mul_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
303 {
306 
307  if (v1.rows () == 1 && v1.columns () == 1)
308  // If v1 is a scalar in disguise, return a diagonal matrix rather than
309  // a sparse matrix.
310  {
311  std::complex<double> d = v1.complex_value ();
312 
313  return octave_value (d * v2.complex_diag_matrix_value ());
314  }
315  else if (v2.rows () == 1 && v2.columns () == 1)
316  // If v2 is a scalar in disguise, don't bother with further dispatching.
317  {
318  std::complex<double> d = v2.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 (div_scm_dm, sparse_complex_matrix, diag_matrix)
335 {
337  const octave_diag_matrix&);
338 
339  if (v2.rows () == 1 && v2.columns () == 1)
340  {
341  double d = v2.scalar_value ();
342 
343  if (d == 0.0)
345 
347  }
348  else
349  {
350  MatrixType typ = v2.matrix_type ();
352  v2.diag_matrix_value (), typ);
353  }
354 }
355 
356 DEFBINOP (div_sm_cdm, sparse_matrix, complex_diag_matrix)
357 {
360 
361  if (v2.rows () == 1 && v2.columns () == 1)
362  {
363  std::complex<double> d = v2.complex_value ();
364 
365  if (d == 0.0)
367 
368  return octave_value (v1.sparse_matrix_value () / d);
369  }
370  else
371  {
372  MatrixType typ = v2.matrix_type ();
373  return xdiv (v1.sparse_matrix_value (),
374  v2.complex_diag_matrix_value (), typ);
375  }
376 }
377 
378 DEFBINOP (div_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
379 {
382 
383  if (v2.rows () == 1 && v2.columns () == 1)
384  {
385  std::complex<double> d = v2.complex_value ();
386 
387  if (d == 0.0)
389 
391  }
392  else
393  {
394  MatrixType typ = v2.matrix_type ();
396  v2.complex_diag_matrix_value (), typ);
397  }
398 }
399 
400 DEFBINOP (add_sm_cdm, sparse_matrix, complex_diag_matrix)
401 {
404 
405  if (v2.rows () == 1 && v2.columns () == 1)
406  // If v2 is a scalar in disguise, return a diagonal matrix rather than
407  // a sparse matrix.
408  {
409  std::complex<double> d = v2.complex_value ();
410 
411  return octave_value (v1.sparse_matrix_value () + d);
412  }
413  else
415 }
416 
417 DEFBINOP (add_scm_dm, sparse_complex_matrix, diag_matrix)
418 {
420  const octave_diag_matrix&);
421 
422  if (v2.rows () == 1 && v2.columns () == 1)
423  // If v2 is a scalar in disguise, return a diagonal matrix rather than
424  // a sparse matrix.
425  {
426  double d = v2.scalar_value ();
427 
429  }
430  else
432 }
433 
434 DEFBINOP (add_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
435 {
438 
439  if (v2.rows () == 1 && v2.columns () == 1)
440  // If v2 is a scalar in disguise, return a diagonal matrix rather than
441  // a sparse matrix.
442  {
443  std::complex<double> d = v2.complex_value ();
444 
446  }
447  else
449 }
450 
451 DEFBINOP (sub_sm_cdm, sparse_matrix, complex_diag_matrix)
452 {
455 
456  if (v2.rows () == 1 && v2.columns () == 1)
457  // If v2 is a scalar in disguise, return a diagonal matrix rather than
458  // a sparse matrix.
459  {
460  std::complex<double> d = v2.complex_value ();
461 
462  return octave_value (v1.sparse_matrix_value () + (-d));
463  }
464  else
466 }
467 
468 DEFBINOP (sub_scm_dm, sparse_complex_matrix, diag_matrix)
469 {
471  const octave_diag_matrix&);
472 
473  if (v2.rows () == 1 && v2.columns () == 1)
474  // If v2 is a scalar in disguise, return a diagonal matrix rather than
475  // a sparse matrix.
476  {
477  double d = v2.scalar_value ();
478 
479  return octave_value (v1.sparse_complex_matrix_value () + (-d));
480  }
481  else
483 }
484 
485 DEFBINOP (sub_scm_cdm, sparse_complex_matrix, complex_diag_matrix)
486 {
489 
490  if (v2.rows () == 1 && v2.columns () == 1)
491  // If v2 is a scalar in disguise, return a diagonal matrix rather than
492  // a sparse matrix.
493  {
494  std::complex<double> d = v2.complex_value ();
495 
496  return octave_value (v1.sparse_complex_matrix_value () + (-d));
497  }
498  else
500 }
501 
502 void
504 {
506  mul_dm_scm);
508  mul_cdm_sm);
510  octave_sparse_complex_matrix, mul_cdm_scm);
512  ldiv_dm_scm);
514  ldiv_cdm_sm);
516  octave_sparse_complex_matrix, ldiv_cdm_scm);
517 
519  add_dm_scm);
521  add_cdm_sm);
523  octave_sparse_complex_matrix, add_cdm_scm);
525  sub_dm_scm);
527  sub_cdm_sm);
529  octave_sparse_complex_matrix, sub_cdm_scm);
530 
532  mul_scm_dm);
534  mul_sm_cdm);
536  octave_complex_diag_matrix, mul_scm_cdm);
537 
539  div_scm_dm);
541  div_sm_cdm);
543  octave_complex_diag_matrix, div_scm_cdm);
544 
546  add_scm_dm);
548  add_sm_cdm);
550  octave_complex_diag_matrix, add_scm_cdm);
552  sub_scm_dm);
554  sub_sm_cdm);
556  octave_complex_diag_matrix, sub_scm_cdm);
557 }