5164
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 David Bateman |
|
4 Copyright (C) 1998-2004 Andy Adler |
|
5 |
|
6 Octave is free software; you can redistribute it and/or modify it |
|
7 under the terms of the GNU General Public License as published by the |
|
8 Free Software Foundation; either version 2, or (at your option) any |
|
9 later version. |
|
10 |
|
11 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
5307
|
17 along with this program; see the file COPYING. If not, write to the |
|
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 Boston, MA 02110-1301, USA. |
5164
|
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 "ov-scalar.h" |
|
32 #include "ops.h" |
|
33 #include "xpow.h" |
|
34 |
|
35 #include "sparse-xpow.h" |
|
36 #include "sparse-xdiv.h" |
|
37 #include "ov-re-sparse.h" |
|
38 |
|
39 // sparse matrix by scalar ops. |
|
40 |
|
41 DEFBINOP_OP (add, sparse_matrix, scalar, +) |
|
42 DEFBINOP_OP (sub, sparse_matrix, scalar, -) |
|
43 DEFBINOP_OP (mul, sparse_matrix, scalar, *) |
|
44 |
|
45 DEFBINOP (div, sparse_matrix, scalar) |
|
46 { |
|
47 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_scalar&); |
|
48 |
|
49 double d = v2.double_value (); |
|
50 octave_value retval; |
|
51 |
|
52 if (d == 0.0) |
|
53 { |
|
54 gripe_divide_by_zero (); |
|
55 |
|
56 retval = octave_value (v1.matrix_value () / d); |
|
57 } |
|
58 else |
|
59 retval = octave_value (v1.sparse_matrix_value () / d); |
|
60 |
|
61 return retval; |
|
62 } |
|
63 |
|
64 DEFBINOP (pow, sparse_matrix, scalar) |
|
65 { |
|
66 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_scalar&); |
|
67 |
|
68 double tmp = v2.scalar_value (); |
|
69 if (static_cast<int> (tmp) == tmp) |
|
70 return xpow (v1.sparse_matrix_value (), tmp); |
|
71 else |
|
72 return xpow (v1.matrix_value (), tmp); |
|
73 } |
|
74 |
|
75 DEFBINOP (ldiv, sparse_matrix, scalar) |
|
76 { |
5760
|
77 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_scalar&); |
5164
|
78 |
6221
|
79 if (v1.rows() == 1 && v1.columns() == 1) |
|
80 { |
|
81 double d = v1.scalar_value (); |
|
82 |
|
83 if (d == 0.0) |
|
84 gripe_divide_by_zero (); |
5164
|
85 |
6221
|
86 return octave_value (v2.scalar_value () / d); |
|
87 } |
|
88 else |
|
89 { |
|
90 MatrixType typ = v1.matrix_type (); |
|
91 SparseMatrix m1 = v1.sparse_matrix_value (); |
|
92 Matrix m2 = Matrix (1, 1, v2.scalar_value ()); |
|
93 Matrix ret = xleftdiv (m1, m2, typ); |
|
94 v1.matrix_type (typ); |
|
95 return ret; |
|
96 } |
5164
|
97 } |
|
98 |
|
99 DEFBINOP_FN (lt, sparse_matrix, scalar, mx_el_lt) |
|
100 DEFBINOP_FN (le, sparse_matrix, scalar, mx_el_le) |
|
101 DEFBINOP_FN (eq, sparse_matrix, scalar, mx_el_eq) |
|
102 DEFBINOP_FN (ge, sparse_matrix, scalar, mx_el_ge) |
|
103 DEFBINOP_FN (gt, sparse_matrix, scalar, mx_el_gt) |
|
104 DEFBINOP_FN (ne, sparse_matrix, scalar, mx_el_ne) |
|
105 |
|
106 DEFBINOP_OP (el_mul, sparse_matrix, scalar, *) |
|
107 |
|
108 DEFBINOP (el_div, sparse_matrix, scalar) |
|
109 { |
|
110 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_scalar&); |
|
111 |
|
112 double d = v2.double_value (); |
|
113 octave_value retval; |
|
114 |
|
115 if (d == 0.0) |
|
116 { |
|
117 gripe_divide_by_zero (); |
|
118 |
|
119 retval = octave_value (v1.matrix_value () / d); |
|
120 } |
|
121 else |
|
122 retval = octave_value (v1.sparse_matrix_value () / d); |
|
123 |
|
124 return retval; |
|
125 } |
|
126 |
|
127 DEFBINOP_FN (el_pow, sparse_matrix, scalar, elem_xpow) |
|
128 |
|
129 DEFBINOP (el_ldiv, sparse_matrix, scalar) |
|
130 { |
|
131 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_scalar&); |
|
132 |
|
133 return octave_value |
|
134 (x_el_div (v2.complex_value (), v1.sparse_matrix_value ())); |
|
135 } |
|
136 |
|
137 DEFBINOP_FN (el_and, sparse_matrix, scalar, mx_el_and) |
|
138 DEFBINOP_FN (el_or, sparse_matrix, scalar, mx_el_or) |
|
139 |
|
140 DEFCATOP (sm_s, sparse_matrix, scalar) |
|
141 { |
|
142 CAST_BINOP_ARGS (octave_sparse_matrix&, const octave_scalar&); |
|
143 SparseMatrix tmp (1, 1, v2.scalar_value ()); |
|
144 return octave_value (v1.sparse_matrix_value (). concat (tmp, ra_idx)); |
|
145 } |
|
146 |
|
147 DEFASSIGNOP (assign, sparse_matrix, scalar) |
|
148 { |
|
149 CAST_BINOP_ARGS (octave_sparse_matrix&, const octave_scalar&); |
|
150 |
|
151 SparseMatrix tmp (1, 1, v2.scalar_value ()); |
|
152 v1.assign (idx, tmp); |
|
153 return octave_value (); |
|
154 } |
|
155 |
|
156 void |
|
157 install_sm_s_ops (void) |
|
158 { |
|
159 INSTALL_BINOP (op_add, octave_sparse_matrix, octave_scalar, add); |
|
160 INSTALL_BINOP (op_sub, octave_sparse_matrix, octave_scalar, sub); |
|
161 INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_scalar, mul); |
|
162 INSTALL_BINOP (op_div, octave_sparse_matrix, octave_scalar, div); |
|
163 INSTALL_BINOP (op_pow, octave_sparse_matrix, octave_scalar, pow); |
|
164 INSTALL_BINOP (op_ldiv, octave_sparse_matrix, octave_scalar, ldiv); |
|
165 |
|
166 INSTALL_BINOP (op_lt, octave_sparse_matrix, octave_scalar, lt); |
|
167 INSTALL_BINOP (op_le, octave_sparse_matrix, octave_scalar, le); |
|
168 INSTALL_BINOP (op_eq, octave_sparse_matrix, octave_scalar, eq); |
|
169 INSTALL_BINOP (op_ge, octave_sparse_matrix, octave_scalar, ge); |
|
170 INSTALL_BINOP (op_gt, octave_sparse_matrix, octave_scalar, gt); |
|
171 INSTALL_BINOP (op_ne, octave_sparse_matrix, octave_scalar, ne); |
|
172 INSTALL_BINOP (op_el_mul, octave_sparse_matrix, octave_scalar, el_mul); |
|
173 INSTALL_BINOP (op_el_div, octave_sparse_matrix, octave_scalar, el_div); |
|
174 INSTALL_BINOP (op_el_pow, octave_sparse_matrix, octave_scalar, el_pow); |
|
175 INSTALL_BINOP (op_el_ldiv, octave_sparse_matrix, octave_scalar, el_ldiv); |
|
176 INSTALL_BINOP (op_el_and, octave_sparse_matrix, octave_scalar, el_and); |
|
177 INSTALL_BINOP (op_el_or, octave_sparse_matrix, octave_scalar, el_or); |
|
178 |
|
179 INSTALL_CATOP (octave_sparse_matrix, octave_scalar, sm_s); |
|
180 |
|
181 INSTALL_ASSIGNOP (op_asn_eq, octave_sparse_matrix, octave_scalar, assign); |
|
182 } |
|
183 |
|
184 /* |
|
185 ;;; Local Variables: *** |
|
186 ;;; mode: C++ *** |
|
187 ;;; End: *** |
|
188 */ |