5164
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
7016
|
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
|
5 |
|
6 This file is part of Octave. |
5164
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
5164
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
5164
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "gripes.h" |
|
29 #include "oct-obj.h" |
|
30 #include "ov.h" |
|
31 #include "ov-typeinfo.h" |
|
32 #include "ov-re-mat.h" |
|
33 #include "ops.h" |
|
34 |
|
35 #include "sparse-xpow.h" |
|
36 #include "sparse-xdiv.h" |
|
37 #include "ov-re-sparse.h" |
|
38 |
|
39 // sparse matrix unary ops. |
|
40 |
|
41 DEFUNOP_OP (not, sparse_matrix, !) |
|
42 DEFUNOP_OP (uplus, sparse_matrix, /* no-op */) |
|
43 DEFUNOP_OP (uminus, sparse_matrix, -) |
|
44 |
|
45 DEFUNOP (transpose, sparse_matrix) |
|
46 { |
|
47 CAST_UNOP_ARG (const octave_sparse_matrix&); |
5322
|
48 return octave_value (v.sparse_matrix_value().transpose (), |
5785
|
49 v.matrix_type ().transpose ()); |
5164
|
50 } |
|
51 |
|
52 // sparse matrix by sparse matrix ops. |
|
53 |
|
54 DEFBINOP_OP (add, sparse_matrix, sparse_matrix, +) |
7342
|
55 |
|
56 // DEFBINOP_OP (sub, sparse_matrix, sparse_matrix, -) |
|
57 |
|
58 static octave_value |
|
59 oct_binop_sub (const octave_base_value& a1, const octave_base_value& a2) |
|
60 { |
|
61 const octave_sparse_matrix& v1 = dynamic_cast<const octave_sparse_matrix&> (a1); |
|
62 const octave_sparse_matrix& v2 = dynamic_cast<const octave_sparse_matrix&> (a2); |
|
63 SparseMatrix m = v1.sparse_matrix_value () - v2.sparse_matrix_value (); |
|
64 |
|
65 return octave_value (m); |
|
66 } |
|
67 |
5164
|
68 DEFBINOP_OP (mul, sparse_matrix, sparse_matrix, *) |
|
69 |
5322
|
70 DEFBINOP (div, sparse_matrix, sparse_matrix) |
|
71 { |
5760
|
72 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_sparse_matrix&); |
6221
|
73 |
|
74 if (v2.rows() == 1 && v2.columns() == 1) |
|
75 { |
|
76 double d = v2.scalar_value (); |
|
77 |
|
78 if (d == 0.0) |
|
79 gripe_divide_by_zero (); |
|
80 |
|
81 return octave_value (v1.sparse_matrix_value () / d); |
|
82 } |
|
83 else |
|
84 { |
|
85 MatrixType typ = v2.matrix_type (); |
|
86 SparseMatrix ret = xdiv (v1.sparse_matrix_value (), |
|
87 v2.sparse_matrix_value (), typ); |
5322
|
88 |
6221
|
89 v2.matrix_type (typ); |
|
90 return ret; |
|
91 } |
5322
|
92 } |
5164
|
93 |
|
94 DEFBINOPX (pow, sparse_matrix, sparse_matrix) |
|
95 { |
|
96 error ("can't do A ^ B for A and B both matrices"); |
|
97 return octave_value (); |
|
98 } |
|
99 |
5322
|
100 DEFBINOP (ldiv, sparse_matrix, sparse_matrix) |
|
101 { |
5760
|
102 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_sparse_matrix&); |
6221
|
103 |
|
104 if (v1.rows() == 1 && v1.columns() == 1) |
|
105 { |
|
106 double d = v1.double_value (); |
|
107 |
|
108 if (d == 0.0) |
|
109 gripe_divide_by_zero (); |
5322
|
110 |
6221
|
111 return octave_value (v2.sparse_matrix_value () / d); |
|
112 } |
|
113 else |
|
114 { |
|
115 MatrixType typ = v1.matrix_type (); |
5322
|
116 |
6221
|
117 SparseMatrix ret = xleftdiv (v1.sparse_matrix_value (), |
|
118 v2.sparse_matrix_value (), typ); |
|
119 |
|
120 v1.matrix_type (typ); |
|
121 return ret; |
|
122 } |
5322
|
123 } |
5164
|
124 |
|
125 DEFBINOP_FN (lt, sparse_matrix, sparse_matrix, mx_el_lt) |
|
126 DEFBINOP_FN (le, sparse_matrix, sparse_matrix, mx_el_le) |
|
127 DEFBINOP_FN (eq, sparse_matrix, sparse_matrix, mx_el_eq) |
|
128 DEFBINOP_FN (ge, sparse_matrix, sparse_matrix, mx_el_ge) |
|
129 DEFBINOP_FN (gt, sparse_matrix, sparse_matrix, mx_el_gt) |
|
130 DEFBINOP_FN (ne, sparse_matrix, sparse_matrix, mx_el_ne) |
|
131 |
|
132 DEFBINOP_FN (el_mul, sparse_matrix, sparse_matrix, product) |
|
133 DEFBINOP_FN (el_div, sparse_matrix, sparse_matrix, quotient) |
|
134 |
|
135 DEFBINOP_FN (el_pow, sparse_matrix, sparse_matrix, elem_xpow) |
|
136 |
|
137 DEFBINOP (el_ldiv, sparse_matrix, sparse_matrix) |
|
138 { |
|
139 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_sparse_matrix&); |
|
140 return octave_value |
|
141 (quotient (v2.sparse_matrix_value (), v1.sparse_matrix_value ())); |
|
142 } |
|
143 |
|
144 DEFBINOP_FN (el_and, sparse_matrix, sparse_matrix, mx_el_and) |
|
145 DEFBINOP_FN (el_or, sparse_matrix, sparse_matrix, mx_el_or) |
|
146 |
|
147 DEFCATOP_FN (sm_sm, sparse_matrix, sparse_matrix, concat) |
|
148 |
|
149 DEFASSIGNOP_FN (assign, sparse_matrix, sparse_matrix, assign) |
|
150 |
|
151 void |
|
152 install_sm_sm_ops (void) |
|
153 { |
|
154 INSTALL_UNOP (op_not, octave_sparse_matrix, not); |
|
155 INSTALL_UNOP (op_uplus, octave_sparse_matrix, uplus); |
|
156 INSTALL_UNOP (op_uminus, octave_sparse_matrix, uminus); |
|
157 INSTALL_UNOP (op_transpose, octave_sparse_matrix, transpose); |
|
158 INSTALL_UNOP (op_hermitian, octave_sparse_matrix, transpose); |
|
159 |
|
160 INSTALL_BINOP (op_add, octave_sparse_matrix, octave_sparse_matrix, add); |
|
161 INSTALL_BINOP (op_sub, octave_sparse_matrix, octave_sparse_matrix, sub); |
|
162 INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_sparse_matrix, mul); |
|
163 INSTALL_BINOP (op_div, octave_sparse_matrix, octave_sparse_matrix, div); |
|
164 INSTALL_BINOP (op_pow, octave_sparse_matrix, octave_sparse_matrix, pow); |
|
165 INSTALL_BINOP (op_ldiv, octave_sparse_matrix, octave_sparse_matrix, ldiv); |
|
166 INSTALL_BINOP (op_lt, octave_sparse_matrix, octave_sparse_matrix, lt); |
|
167 INSTALL_BINOP (op_le, octave_sparse_matrix, octave_sparse_matrix, le); |
|
168 INSTALL_BINOP (op_eq, octave_sparse_matrix, octave_sparse_matrix, eq); |
|
169 INSTALL_BINOP (op_ge, octave_sparse_matrix, octave_sparse_matrix, ge); |
|
170 INSTALL_BINOP (op_gt, octave_sparse_matrix, octave_sparse_matrix, gt); |
|
171 INSTALL_BINOP (op_ne, octave_sparse_matrix, octave_sparse_matrix, ne); |
|
172 INSTALL_BINOP (op_el_mul, octave_sparse_matrix, octave_sparse_matrix, |
|
173 el_mul); |
|
174 INSTALL_BINOP (op_el_div, octave_sparse_matrix, octave_sparse_matrix, |
|
175 el_div); |
|
176 INSTALL_BINOP (op_el_pow, octave_sparse_matrix, octave_sparse_matrix, |
|
177 el_pow); |
|
178 INSTALL_BINOP (op_el_ldiv, octave_sparse_matrix, octave_sparse_matrix, |
|
179 el_ldiv); |
|
180 INSTALL_BINOP (op_el_and, octave_sparse_matrix, octave_sparse_matrix, |
|
181 el_and); |
|
182 INSTALL_BINOP (op_el_or, octave_sparse_matrix, octave_sparse_matrix, |
|
183 el_or); |
|
184 |
|
185 INSTALL_CATOP (octave_sparse_matrix, octave_sparse_matrix, sm_sm); |
|
186 |
|
187 INSTALL_ASSIGNOP (op_asn_eq, octave_sparse_matrix, octave_sparse_matrix, |
|
188 assign); |
|
189 } |
|
190 |
|
191 /* |
|
192 ;;; Local Variables: *** |
|
193 ;;; mode: C++ *** |
|
194 ;;; End: *** |
|
195 */ |