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 |
5322
|
79 SparseType typ = v1.sparse_type (); |
5164
|
80 SparseMatrix m1 = v1.sparse_matrix_value (); |
|
81 Matrix m2 = Matrix (1, 1, v2.scalar_value ()); |
5322
|
82 Matrix ret = xleftdiv (m1, m2, typ); |
|
83 v1.sparse_type (typ); |
5164
|
84 |
5322
|
85 return ret; |
5164
|
86 } |
|
87 |
|
88 DEFBINOP_FN (lt, sparse_matrix, scalar, mx_el_lt) |
|
89 DEFBINOP_FN (le, sparse_matrix, scalar, mx_el_le) |
|
90 DEFBINOP_FN (eq, sparse_matrix, scalar, mx_el_eq) |
|
91 DEFBINOP_FN (ge, sparse_matrix, scalar, mx_el_ge) |
|
92 DEFBINOP_FN (gt, sparse_matrix, scalar, mx_el_gt) |
|
93 DEFBINOP_FN (ne, sparse_matrix, scalar, mx_el_ne) |
|
94 |
|
95 DEFBINOP_OP (el_mul, sparse_matrix, scalar, *) |
|
96 |
|
97 DEFBINOP (el_div, sparse_matrix, scalar) |
|
98 { |
|
99 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_scalar&); |
|
100 |
|
101 double d = v2.double_value (); |
|
102 octave_value retval; |
|
103 |
|
104 if (d == 0.0) |
|
105 { |
|
106 gripe_divide_by_zero (); |
|
107 |
|
108 retval = octave_value (v1.matrix_value () / d); |
|
109 } |
|
110 else |
|
111 retval = octave_value (v1.sparse_matrix_value () / d); |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
|
116 DEFBINOP_FN (el_pow, sparse_matrix, scalar, elem_xpow) |
|
117 |
|
118 DEFBINOP (el_ldiv, sparse_matrix, scalar) |
|
119 { |
|
120 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_scalar&); |
|
121 |
|
122 return octave_value |
|
123 (x_el_div (v2.complex_value (), v1.sparse_matrix_value ())); |
|
124 } |
|
125 |
|
126 DEFBINOP_FN (el_and, sparse_matrix, scalar, mx_el_and) |
|
127 DEFBINOP_FN (el_or, sparse_matrix, scalar, mx_el_or) |
|
128 |
|
129 DEFCATOP (sm_s, sparse_matrix, scalar) |
|
130 { |
|
131 CAST_BINOP_ARGS (octave_sparse_matrix&, const octave_scalar&); |
|
132 SparseMatrix tmp (1, 1, v2.scalar_value ()); |
|
133 return octave_value (v1.sparse_matrix_value (). concat (tmp, ra_idx)); |
|
134 } |
|
135 |
|
136 DEFASSIGNOP (assign, sparse_matrix, scalar) |
|
137 { |
|
138 CAST_BINOP_ARGS (octave_sparse_matrix&, const octave_scalar&); |
|
139 |
|
140 SparseMatrix tmp (1, 1, v2.scalar_value ()); |
|
141 v1.assign (idx, tmp); |
|
142 return octave_value (); |
|
143 } |
|
144 |
|
145 void |
|
146 install_sm_s_ops (void) |
|
147 { |
|
148 INSTALL_BINOP (op_add, octave_sparse_matrix, octave_scalar, add); |
|
149 INSTALL_BINOP (op_sub, octave_sparse_matrix, octave_scalar, sub); |
|
150 INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_scalar, mul); |
|
151 INSTALL_BINOP (op_div, octave_sparse_matrix, octave_scalar, div); |
|
152 INSTALL_BINOP (op_pow, octave_sparse_matrix, octave_scalar, pow); |
|
153 INSTALL_BINOP (op_ldiv, octave_sparse_matrix, octave_scalar, ldiv); |
|
154 |
|
155 INSTALL_BINOP (op_lt, octave_sparse_matrix, octave_scalar, lt); |
|
156 INSTALL_BINOP (op_le, octave_sparse_matrix, octave_scalar, le); |
|
157 INSTALL_BINOP (op_eq, octave_sparse_matrix, octave_scalar, eq); |
|
158 INSTALL_BINOP (op_ge, octave_sparse_matrix, octave_scalar, ge); |
|
159 INSTALL_BINOP (op_gt, octave_sparse_matrix, octave_scalar, gt); |
|
160 INSTALL_BINOP (op_ne, octave_sparse_matrix, octave_scalar, ne); |
|
161 INSTALL_BINOP (op_el_mul, octave_sparse_matrix, octave_scalar, el_mul); |
|
162 INSTALL_BINOP (op_el_div, octave_sparse_matrix, octave_scalar, el_div); |
|
163 INSTALL_BINOP (op_el_pow, octave_sparse_matrix, octave_scalar, el_pow); |
|
164 INSTALL_BINOP (op_el_ldiv, octave_sparse_matrix, octave_scalar, el_ldiv); |
|
165 INSTALL_BINOP (op_el_and, octave_sparse_matrix, octave_scalar, el_and); |
|
166 INSTALL_BINOP (op_el_or, octave_sparse_matrix, octave_scalar, el_or); |
|
167 |
|
168 INSTALL_CATOP (octave_sparse_matrix, octave_scalar, sm_s); |
|
169 |
|
170 INSTALL_ASSIGNOP (op_asn_eq, octave_sparse_matrix, octave_scalar, assign); |
|
171 } |
|
172 |
|
173 /* |
|
174 ;;; Local Variables: *** |
|
175 ;;; mode: C++ *** |
|
176 ;;; End: *** |
|
177 */ |