GNU Octave  9.1.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-2024 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 
41 
42 // diagonal matrix by sparse matrix ops
43 
44 DEFBINOP (mul_dm_sm, diag_matrix, sparse_matrix)
45 {
48 
49  if (v2.rows () == 1 && v2.columns () == 1)
50  // If v2 is a scalar in disguise, return a diagonal matrix rather than
51  // a sparse matrix.
52  {
53  double d = v2.scalar_value ();
54 
55  return octave_value (v1.diag_matrix_value () * d);
56  }
57  else
58  {
59  MatrixType typ = v2.matrix_type ();
60  SparseMatrix ret = v1.diag_matrix_value () * v2.sparse_matrix_value ();
61  octave_value out = octave_value (ret);
62  typ.mark_as_unsymmetric ();
63  out.matrix_type (typ);
64  return out;
65  }
66 }
67 
68 DEFBINOP (ldiv_dm_sm, diag_matrix, sparse_matrix)
69 {
72 
73  MatrixType typ = v2.matrix_type ();
74  return xleftdiv (v1.diag_matrix_value (), v2.sparse_matrix_value (), typ);
75 }
76 
77 DEFBINOP (add_dm_sm, diag_matrix, sparse_matrix)
78 {
81 
82  if (v2.rows () == 1 && v2.columns () == 1)
83  // If v2 is a scalar in disguise, return a diagonal matrix rather than
84  // a sparse matrix.
85  {
86  double d = v2.scalar_value ();
87 
88  return octave_value (v1.matrix_value () + d);
89  }
90  else
91  return v1.diag_matrix_value () + v2.sparse_matrix_value ();
92 }
93 
94 DEFBINOP (sub_dm_sm, diag_matrix, sparse_matrix)
95 {
98 
99  if (v2.rows () == 1 && v2.columns () == 1)
100  // If v2 is a scalar in disguise, return a diagonal matrix rather than
101  // a sparse matrix.
102  {
103  double d = v2.scalar_value ();
104 
105  return octave_value (v1.matrix_value () - d);
106  }
107  else
108  return v1.diag_matrix_value () - v2.sparse_matrix_value ();
109 }
110 
111 // sparse matrix by diagonal matrix ops
112 
113 DEFBINOP (mul_sm_dm, sparse_matrix, diag_matrix)
114 {
117 
118  if (v1.rows () == 1 && v1.columns () == 1)
119  // If v1 is a scalar in disguise, return a diagonal matrix rather than
120  // a sparse matrix.
121  {
122  double d = v1.scalar_value ();
123 
124  return octave_value (d * v2.diag_matrix_value ());
125  }
126  else
127  {
128  MatrixType typ = v1.matrix_type ();
129  SparseMatrix ret = v1.sparse_matrix_value () * v2.diag_matrix_value ();
130  octave_value out = octave_value (ret);
131  typ.mark_as_unsymmetric ();
132  out.matrix_type (typ);
133  return out;
134  }
135 }
136 
137 DEFBINOP (div_sm_dm, sparse_matrix, diag_matrix)
138 {
141 
142  if (v2.rows () == 1 && v2.columns () == 1)
143  return octave_value (v1.sparse_matrix_value () / v2.scalar_value ());
144  else
145  {
146  MatrixType typ = v2.matrix_type ();
147  return xdiv (v1.sparse_matrix_value (), v2.diag_matrix_value (), typ);
148  }
149 }
150 
151 DEFBINOP (add_sm_dm, sparse_matrix, diag_matrix)
152 {
155 
156  if (v1.rows () == 1 && v1.columns () == 1)
157  // If v1 is a scalar in disguise, return a diagonal matrix rather than
158  // a sparse matrix.
159  {
160  double d = v1.scalar_value ();
161 
162  return octave_value (d + v2.matrix_value ());
163  }
164  else
165  return v1.sparse_matrix_value () + v2.diag_matrix_value ();
166 }
167 
168 DEFBINOP (sub_sm_dm, sparse_matrix, diag_matrix)
169 {
172 
173  if (v1.rows () == 1 && v1.columns () == 1)
174  // If v1 is a scalar in disguise, return a diagonal matrix rather than
175  // a sparse matrix.
176  {
177  double d = v1.scalar_value ();
178 
179  return octave_value (d - v2.matrix_value ());
180  }
181  else
182  return v1.sparse_matrix_value () - v2.diag_matrix_value ();
183 }
184 
185 void
186 install_dm_sm_ops (octave::type_info& ti)
187 {
189  mul_dm_sm);
190 
192  add_dm_sm);
194  sub_dm_sm);
196  ldiv_dm_sm);
197 
199  mul_sm_dm);
200 
202  add_sm_dm);
204  sub_sm_dm);
206  div_sm_dm);
207 }
208 
209 OCTAVE_END_NAMESPACE(octave)
void mark_as_unsymmetric()
Definition: MatrixType.cc:920
MatrixType matrix_type() const
Definition: ov-base-mat.h:139
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-base.cc:695
octave_idx_type rows() const
Definition: ov-base.h:374
octave_idx_type columns() const
Definition: ov-base.h:381
virtual DiagMatrix diag_matrix_value(bool=false) const
Definition: ov-base.cc:715
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
MatrixType matrix_type() const
Definition: ov.h:583
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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:186
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:245
#define OCTAVE_CAST_BASE_VALUE(T, T_VAL, BASE_VAL)
Definition: ops.h:52
#define INSTALL_BINOP_TI(ti, op, t1, t2, f)
Definition: ops.h:70
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1663
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1664
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1665
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1669
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1666
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:469