GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
op-dm-sm.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2009-2021 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 "ovl.h"
31 #include "ov.h"
32 #include "ov-typeinfo.h"
33 #include "ops.h"
34 
35 #include "ov-re-diag.h"
36 #include "ov-re-sparse.h"
37 
38 #include "sparse-xdiv.h"
39 
40 // diagonal matrix by sparse matrix ops
41 
42 DEFBINOP (mul_dm_sm, diag_matrix, sparse_matrix)
43 {
44  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
45  const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2);
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  double d = v2.scalar_value ();
52 
53  return octave_value (v1.diag_matrix_value () * d);
54  }
55  else
56  {
57  MatrixType typ = v2.matrix_type ();
59  octave_value out = octave_value (ret);
60  typ.mark_as_unsymmetric ();
61  out.matrix_type (typ);
62  return out;
63  }
64 }
65 
66 DEFBINOP (ldiv_dm_sm, diag_matrix, sparse_matrix)
67 {
68  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
69  const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2);
70 
71  MatrixType typ = v2.matrix_type ();
72  return xleftdiv (v1.diag_matrix_value (), v2.sparse_matrix_value (), typ);
73 }
74 
75 DEFBINOP (add_dm_sm, diag_matrix, sparse_matrix)
76 {
77  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
78  const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2);
79 
80  if (v2.rows () == 1 && v2.columns () == 1)
81  // If v2 is a scalar in disguise, return a diagonal matrix rather than
82  // a sparse matrix.
83  {
84  double d = v2.scalar_value ();
85 
86  return octave_value (v1.matrix_value () + d);
87  }
88  else
89  return v1.diag_matrix_value () + v2.sparse_matrix_value ();
90 }
91 
92 DEFBINOP (sub_dm_sm, diag_matrix, sparse_matrix)
93 {
94  const octave_diag_matrix& v1 = dynamic_cast<const octave_diag_matrix&> (a1);
95  const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2);
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  double d = v2.scalar_value ();
102 
103  return octave_value (v1.matrix_value () - d);
104  }
105  else
106  return v1.diag_matrix_value () - v2.sparse_matrix_value ();
107 }
108 
109 // sparse matrix by diagonal matrix ops
110 
111 DEFBINOP (mul_sm_dm, sparse_matrix, diag_matrix)
112 {
113  const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1);
114  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
115 
116  if (v1.rows () == 1 && v1.columns () == 1)
117  // If v1 is a scalar in disguise, return a diagonal matrix rather than
118  // a sparse matrix.
119  {
120  double d = v1.scalar_value ();
121 
122  return octave_value (d * v2.diag_matrix_value ());
123  }
124  else
125  {
126  MatrixType typ = v1.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 
135 DEFBINOP (div_sm_dm, sparse_matrix, diag_matrix)
136 {
137  const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1);
138  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
139 
140  if (v2.rows () == 1 && v2.columns () == 1)
141  return octave_value (v1.sparse_matrix_value () / v2.scalar_value ());
142  else
143  {
144  MatrixType typ = v2.matrix_type ();
145  return xdiv (v1.sparse_matrix_value (), v2.diag_matrix_value (), typ);
146  }
147 }
148 
149 DEFBINOP (add_sm_dm, sparse_matrix, diag_matrix)
150 {
151  const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1);
152  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
153 
154  if (v1.rows () == 1 && v1.columns () == 1)
155  // If v1 is a scalar in disguise, return a diagonal matrix rather than
156  // a sparse matrix.
157  {
158  double d = v1.scalar_value ();
159 
160  return octave_value (d + v2.matrix_value ());
161  }
162  else
163  return v1.sparse_matrix_value () + v2.diag_matrix_value ();
164 }
165 
166 DEFBINOP (sub_sm_dm, sparse_matrix, diag_matrix)
167 {
168  const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1);
169  const octave_diag_matrix& v2 = dynamic_cast<const octave_diag_matrix&> (a2);
170 
171  if (v1.rows () == 1 && v1.columns () == 1)
172  // If v1 is a scalar in disguise, return a diagonal matrix rather than
173  // a sparse matrix.
174  {
175  double d = v1.scalar_value ();
176 
177  return octave_value (d - v2.matrix_value ());
178  }
179  else
180  return v1.sparse_matrix_value () - v2.diag_matrix_value ();
181 }
182 
183 void
185 {
187  mul_dm_sm);
188 
190  add_dm_sm);
192  sub_dm_sm);
194  ldiv_dm_sm);
195 
197  mul_sm_dm);
198 
200  add_sm_dm);
202  sub_sm_dm);
204  div_sm_dm);
205 }
void mark_as_unsymmetric(void)
Definition: MatrixType.cc:916
Matrix matrix_value(bool=false) const
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:131
MatrixType matrix_type(void) const
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-base.cc:636
octave_idx_type columns(void) const
Definition: ov-base.h:325
virtual DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-base.cc:656
octave_idx_type rows(void) const
Definition: ov-base.h:318
Matrix matrix_value(bool=false) const
Definition: ov-ch-mat.h:114
double scalar_value(bool frc_str_conv=false) const
Definition: ov-ch-mat.h:105
DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-re-diag.cc:140
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-re-sparse.h:127
double scalar_value(bool frc_str_conv=false) const
Definition: ov-re-sparse.h:110
MatrixType matrix_type(void) const
Definition: ov.h:542
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void install_dm_sm_ops(octave::type_info &ti)
Definition: op-dm-sm.cc:184
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:230
#define INSTALL_BINOP_TI(ti, op, t1, t2, f)
Definition: ops.h:55
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1566
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1567
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1568
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1572
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1569
Matrix xdiv(const Matrix &a, const SparseMatrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:135
Matrix xleftdiv(const SparseMatrix &a, const Matrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:466