GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
op-dm-sm.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2009-2025 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
44DEFBINOP (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);
63 out.matrix_type (typ);
64 return out;
65 }
66}
67
68DEFBINOP (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
77DEFBINOP (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
94DEFBINOP (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
113DEFBINOP (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
137DEFBINOP (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
151DEFBINOP (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
168DEFBINOP (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
185void
208
209OCTAVE_END_NAMESPACE(octave)
void mark_as_unsymmetric()
MatrixType matrix_type() const
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition ov-base.cc:692
octave_idx_type rows() const
Definition ov-base.h:384
octave_idx_type columns() const
Definition ov-base.h:391
virtual DiagMatrix diag_matrix_value(bool=false) const
Definition ov-base.cc:712
Matrix matrix_value(bool=false) const
Definition ov-ch-mat.h:115
double scalar_value(bool frc_str_conv=false) const
Definition ov-ch-mat.h:106
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
const octave_char_matrix & v2
#define DEFBINOP(name, t1, t2)
Definition ops.h:248
#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:1678
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition ov.h:1679
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition ov.h:1680
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition ov.h:1684
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition ov.h:1681
Matrix xdiv(const Matrix &a, const SparseMatrix &b, MatrixType &typ)
Matrix xleftdiv(const SparseMatrix &a, const Matrix &b, MatrixType &typ)