GNU Octave  4.2.1
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-sm.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009-2017 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 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "errwarn.h"
28 #include "ovl.h"
29 #include "ov.h"
30 #include "ov-typeinfo.h"
31 #include "ops.h"
32 
33 #include "ov-re-diag.h"
34 #include "ov-re-sparse.h"
35 
36 #include "sparse-xdiv.h"
37 
38 // diagonal matrix by sparse matrix ops
39 
40 DEFBINOP (mul_dm_sm, diag_matrix, sparse_matrix)
41 {
42  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
43  const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2);
44 
45  if (v2.rows () == 1 && v2.columns () == 1)
46  // If v2 is a scalar in disguise, return a diagonal matrix rather than
47  // a sparse matrix.
48  {
49  double d = v2.scalar_value ();
50 
51  return octave_value (v1.diag_matrix_value () * d);
52  }
53  else
54  {
55  MatrixType typ = v2.matrix_type ();
57  octave_value out = octave_value (ret);
58  typ.mark_as_unsymmetric ();
59  out.matrix_type (typ);
60  return out;
61  }
62 }
63 
64 DEFBINOP (ldiv_dm_sm, diag_matrix, sparse_matrix)
65 {
66  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
67  const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2);
68 
69  MatrixType typ = v2.matrix_type ();
70  return xleftdiv (v1.diag_matrix_value (), v2.sparse_matrix_value (), typ);
71 }
72 
73 DEFBINOP (add_dm_sm, diag_matrix, sparse_matrix)
74 {
75  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
76  const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2);
77 
78  if (v2.rows () == 1 && v2.columns () == 1)
79  // If v2 is a scalar in disguise, return a diagonal matrix rather than
80  // a sparse matrix.
81  {
82  double d = v2.scalar_value ();
83 
84  return octave_value (v1.matrix_value () + d);
85  }
86  else
87  return v1.diag_matrix_value () + v2.sparse_matrix_value ();
88 }
89 
90 DEFBINOP (sub_dm_sm, diag_matrix, sparse_matrix)
91 {
92  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
93  const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2);
94 
95  if (v2.rows () == 1 && v2.columns () == 1)
96  // If v2 is a scalar in disguise, return a diagonal matrix rather than
97  // a sparse matrix.
98  {
99  double d = v2.scalar_value ();
100 
101  return octave_value (v1.matrix_value () - d);
102  }
103  else
104  return v1.diag_matrix_value () - v2.sparse_matrix_value ();
105 }
106 
107 // sparse matrix by diagonal matrix ops
108 
109 DEFBINOP (mul_sm_dm, sparse_matrix, diag_matrix)
110 {
111  const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1);
112  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
113 
114  if (v1.rows () == 1 && v1.columns () == 1)
115  // If v1 is a scalar in disguise, return a diagonal matrix rather than
116  // a sparse matrix.
117  {
118  double d = v1.scalar_value ();
119 
120  return octave_value (d * v2.diag_matrix_value ());
121  }
122  else
123  {
124  MatrixType typ = v1.matrix_type ();
126  octave_value out = octave_value (ret);
127  typ.mark_as_unsymmetric ();
128  out.matrix_type (typ);
129  return out;
130  }
131 }
132 
133 DEFBINOP (div_sm_dm, sparse_matrix, diag_matrix)
134 {
135  const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1);
136  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
137 
138  if (v2.rows () == 1 && v2.columns () == 1)
139  {
140  double d = v2.scalar_value ();
141 
142  if (d == 0.0)
144 
145  return octave_value (v1.sparse_matrix_value () / d);
146  }
147  else
148  {
149  MatrixType typ = v2.matrix_type ();
150  return xdiv (v1.sparse_matrix_value (), v2.diag_matrix_value (), typ);
151  }
152 }
153 
154 DEFBINOP (add_sm_dm, sparse_matrix, diag_matrix)
155 {
156  const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1);
157  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
158 
159  if (v1.rows () == 1 && v1.columns () == 1)
160  // If v1 is a scalar in disguise, return a diagonal matrix rather than
161  // a sparse matrix.
162  {
163  double d = v1.scalar_value ();
164 
165  return octave_value (d + v2.matrix_value ());
166  }
167  else
168  return v1.sparse_matrix_value () + v2.diag_matrix_value ();
169 }
170 
171 DEFBINOP (sub_sm_dm, sparse_matrix, diag_matrix)
172 {
173  const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1);
174  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
175 
176  if (v1.rows () == 1 && v1.columns () == 1)
177  // If v1 is a scalar in disguise, return a diagonal matrix rather than
178  // a sparse matrix.
179  {
180  double d = v1.scalar_value ();
181 
182  return octave_value (d - v2.matrix_value ());
183  }
184  else
185  return v1.sparse_matrix_value () - v2.diag_matrix_value ();
186 }
187 
188 void
190 {
192  mul_dm_sm);
193 
197 
199  mul_sm_dm);
200 
204 }
#define DEFBINOP(name, t1, t2)
Definition: ops.h:223
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:127
octave_idx_type columns(void) const
Definition: ov-base.h:319
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &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:1517
#define INSTALL_BINOP(op, t1, t2, f)
Definition: ops.h:48
void mark_as_unsymmetric(void)
Definition: MatrixType.cc:911
Matrix xleftdiv(const SparseMatrix &a, const Matrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:464
void warn_divide_by_zero(void)
Definition: errwarn.cc:320
MatrixType matrix_type(void) const
Definition: ov.h:527
const octave_char_matrix & v2
double scalar_value(bool frc_str_conv=false) const
Definition: ov-base-diag.h:142
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:137
Matrix xdiv(const Matrix &a, const SparseMatrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:133
double scalar_value(bool frc_str_conv=false) const
Definition: ov-re-sparse.h:110
Matrix matrix_value(bool=false) const
MatrixType matrix_type(void) const
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1514
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1520
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1515
MatrixType matrix_type(void) const
Definition: ov-base-diag.h:105
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1516
void install_dm_sm_ops(void)
Definition: op-dm-sm.cc:189
octave_idx_type rows(void) const
Definition: ov-base.h:312
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))