GNU Octave
3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Pages
libinterp
operators
op-dm-sm.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-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
CAST_BINOP_ARGS
(
const
octave_diag_matrix
&,
const
octave_sparse_matrix
&);
43
44
if
(
v2
.
rows
() == 1 &&
v2
.
columns
() == 1)
45
// If v2 is a scalar in disguise, return a diagonal matrix rather than
46
// a sparse matrix.
47
{
48
double
d
=
v2
.
scalar_value
();
49
50
return
octave_value
(
v1
.
diag_matrix_value
() *
d
);
51
}
52
else
53
{
54
MatrixType
typ =
v2
.
matrix_type
();
55
SparseMatrix
ret =
v1
.
diag_matrix_value
() *
v2
.
sparse_matrix_value
();
56
octave_value
out =
octave_value
(ret);
57
typ.
mark_as_unsymmetric
();
58
out.
matrix_type
(typ);
59
return
out;
60
}
61
}
62
63
DEFBINOP
(ldiv_dm_sm, diag_matrix, sparse_matrix)
64
{
65
CAST_BINOP_ARGS
(
const
octave_diag_matrix
&,
const
octave_sparse_matrix
&);
66
67
MatrixType
typ =
v2
.
matrix_type
();
68
return
xleftdiv
(
v1
.
diag_matrix_value
(),
v2
.
sparse_matrix_value
(), typ);
69
}
70
71
DEFBINOP
(add_dm_sm, diag_matrix, sparse_matrix)
72
{
73
CAST_BINOP_ARGS
(
const
octave_diag_matrix
&,
const
octave_sparse_matrix
&);
74
75
if
(
v2
.
rows
() == 1 &&
v2
.
columns
() == 1)
76
// If v2 is a scalar in disguise, return a diagonal matrix rather than
77
// a sparse matrix.
78
{
79
double
d
=
v2
.
scalar_value
();
80
81
return
octave_value
(
v1
.
matrix_value
() +
d
);
82
}
83
else
84
return
v1
.
diag_matrix_value
() +
v2
.
sparse_matrix_value
();
85
}
86
87
DEFBINOP
(sub_dm_sm, diag_matrix, sparse_matrix)
88
{
89
CAST_BINOP_ARGS
(
const
octave_diag_matrix
&,
const
octave_sparse_matrix
&);
90
91
if
(
v2
.
rows
() == 1 &&
v2
.
columns
() == 1)
92
// If v2 is a scalar in disguise, return a diagonal matrix rather than
93
// a sparse matrix.
94
{
95
double
d
=
v2
.
scalar_value
();
96
97
return
octave_value
(
v1
.
matrix_value
() -
d
);
98
}
99
else
100
return
v1
.
diag_matrix_value
() -
v2
.
sparse_matrix_value
();
101
}
102
103
// sparse matrix by diagonal matrix ops
104
105
DEFBINOP
(mul_sm_dm, sparse_matrix, diag_matrix)
106
{
107
CAST_BINOP_ARGS
(
const
octave_sparse_matrix
&,
const
octave_diag_matrix
&);
108
109
if
(
v1
.
rows
() == 1 &&
v1
.
columns
() == 1)
110
// If v1 is a scalar in disguise, return a diagonal matrix rather than
111
// a sparse matrix.
112
{
113
double
d
=
v1
.
scalar_value
();
114
115
return
octave_value
(d *
v2
.
diag_matrix_value
());
116
}
117
else
118
{
119
MatrixType
typ =
v1
.
matrix_type
();
120
SparseMatrix
ret =
v1
.
sparse_matrix_value
() *
v2
.
diag_matrix_value
();
121
octave_value
out =
octave_value
(ret);
122
typ.
mark_as_unsymmetric
();
123
out.
matrix_type
(typ);
124
return
out;
125
}
126
}
127
128
DEFBINOP
(div_sm_dm, sparse_matrix, diag_matrix)
129
{
130
CAST_BINOP_ARGS
(
const
octave_sparse_matrix
&,
const
octave_diag_matrix
&);
131
132
if
(
v2
.
rows
() == 1 &&
v2
.
columns
() == 1)
133
{
134
double
d
=
v2
.
scalar_value
();
135
136
if
(d == 0.0)
137
gripe_divide_by_zero
();
138
139
return
octave_value
(
v1
.
sparse_matrix_value
() /
d
);
140
}
141
else
142
{
143
MatrixType
typ =
v2
.
matrix_type
();
144
return
xdiv
(
v1
.
sparse_matrix_value
(),
v2
.
diag_matrix_value
(), typ);
145
}
146
}
147
148
DEFBINOP
(add_sm_dm, sparse_matrix, diag_matrix)
149
{
150
CAST_BINOP_ARGS
(
const
octave_sparse_matrix
&,
const
octave_diag_matrix
&);
151
152
if
(
v1
.
rows
() == 1 &&
v1
.
columns
() == 1)
153
// If v1 is a scalar in disguise, return a diagonal matrix rather than
154
// a sparse matrix.
155
{
156
double
d
=
v1
.
scalar_value
();
157
158
return
octave_value
(d +
v2
.
matrix_value
());
159
}
160
else
161
return
v1
.
sparse_matrix_value
() +
v2
.
diag_matrix_value
();
162
}
163
164
DEFBINOP
(sub_sm_dm, sparse_matrix, diag_matrix)
165
{
166
CAST_BINOP_ARGS
(
const
octave_sparse_matrix
&,
const
octave_diag_matrix
&);
167
168
if
(
v1
.
rows
() == 1 &&
v1
.
columns
() == 1)
169
// If v1 is a scalar in disguise, return a diagonal matrix rather than
170
// a sparse matrix.
171
{
172
double
d
=
v1
.
scalar_value
();
173
174
return
octave_value
(d -
v2
.
matrix_value
());
175
}
176
else
177
return
v1
.
sparse_matrix_value
() -
v2
.
diag_matrix_value
();
178
}
179
180
void
181
install_dm_sm_ops
(
void
)
182
{
183
INSTALL_BINOP
(
op_mul
,
octave_diag_matrix
,
octave_sparse_matrix
,
184
mul_dm_sm);
185
186
INSTALL_BINOP
(
op_add
,
octave_diag_matrix
,
octave_sparse_matrix
, add_dm_sm);
187
INSTALL_BINOP
(
op_sub
,
octave_diag_matrix
,
octave_sparse_matrix
, sub_dm_sm);
188
INSTALL_BINOP
(
op_ldiv
,
octave_diag_matrix
,
octave_sparse_matrix
, ldiv_dm_sm);
189
190
INSTALL_BINOP
(
op_mul
,
octave_sparse_matrix
,
octave_diag_matrix
,
191
mul_sm_dm);
192
193
INSTALL_BINOP
(
op_add
,
octave_sparse_matrix
,
octave_diag_matrix
, add_sm_dm);
194
INSTALL_BINOP
(
op_sub
,
octave_sparse_matrix
,
octave_diag_matrix
, sub_sm_dm);
195
INSTALL_BINOP
(
op_div
,
octave_sparse_matrix
,
octave_diag_matrix
, div_sm_dm);
196
}
Generated on Mon Dec 30 2013 03:04:35 for GNU Octave by
1.8.1.2