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-re-mat.h" |
|
32 #include "ops.h" |
|
33 |
|
34 #include "sparse-xpow.h" |
|
35 #include "sparse-xdiv.h" |
|
36 #include "ov-re-sparse.h" |
|
37 |
|
38 // sparse matrix unary ops. |
|
39 |
|
40 DEFUNOP_OP (not, sparse_matrix, !) |
|
41 DEFUNOP_OP (uplus, sparse_matrix, /* no-op */) |
|
42 DEFUNOP_OP (uminus, sparse_matrix, -) |
|
43 |
|
44 DEFUNOP (transpose, sparse_matrix) |
|
45 { |
|
46 CAST_UNOP_ARG (const octave_sparse_matrix&); |
5322
|
47 return octave_value (v.sparse_matrix_value().transpose (), |
5785
|
48 v.matrix_type ().transpose ()); |
5164
|
49 } |
|
50 |
|
51 // sparse matrix by sparse matrix ops. |
|
52 |
|
53 DEFBINOP_OP (add, sparse_matrix, sparse_matrix, +) |
|
54 DEFBINOP_OP (sub, sparse_matrix, sparse_matrix, -) |
|
55 DEFBINOP_OP (mul, sparse_matrix, sparse_matrix, *) |
|
56 |
5322
|
57 DEFBINOP (div, sparse_matrix, sparse_matrix) |
|
58 { |
5760
|
59 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_sparse_matrix&); |
6221
|
60 |
|
61 if (v2.rows() == 1 && v2.columns() == 1) |
|
62 { |
|
63 double d = v2.scalar_value (); |
|
64 |
|
65 if (d == 0.0) |
|
66 gripe_divide_by_zero (); |
|
67 |
|
68 return octave_value (v1.sparse_matrix_value () / d); |
|
69 } |
|
70 else |
|
71 { |
|
72 MatrixType typ = v2.matrix_type (); |
|
73 SparseMatrix ret = xdiv (v1.sparse_matrix_value (), |
|
74 v2.sparse_matrix_value (), typ); |
5322
|
75 |
6221
|
76 v2.matrix_type (typ); |
|
77 return ret; |
|
78 } |
5322
|
79 } |
5164
|
80 |
|
81 DEFBINOPX (pow, sparse_matrix, sparse_matrix) |
|
82 { |
|
83 error ("can't do A ^ B for A and B both matrices"); |
|
84 return octave_value (); |
|
85 } |
|
86 |
5322
|
87 DEFBINOP (ldiv, sparse_matrix, sparse_matrix) |
|
88 { |
5760
|
89 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_sparse_matrix&); |
6221
|
90 |
|
91 if (v1.rows() == 1 && v1.columns() == 1) |
|
92 { |
|
93 double d = v1.double_value (); |
|
94 |
|
95 if (d == 0.0) |
|
96 gripe_divide_by_zero (); |
5322
|
97 |
6221
|
98 return octave_value (v2.sparse_matrix_value () / d); |
|
99 } |
|
100 else |
|
101 { |
|
102 MatrixType typ = v1.matrix_type (); |
5322
|
103 |
6221
|
104 SparseMatrix ret = xleftdiv (v1.sparse_matrix_value (), |
|
105 v2.sparse_matrix_value (), typ); |
|
106 |
|
107 v1.matrix_type (typ); |
|
108 return ret; |
|
109 } |
5322
|
110 } |
5164
|
111 |
|
112 DEFBINOP_FN (lt, sparse_matrix, sparse_matrix, mx_el_lt) |
|
113 DEFBINOP_FN (le, sparse_matrix, sparse_matrix, mx_el_le) |
|
114 DEFBINOP_FN (eq, sparse_matrix, sparse_matrix, mx_el_eq) |
|
115 DEFBINOP_FN (ge, sparse_matrix, sparse_matrix, mx_el_ge) |
|
116 DEFBINOP_FN (gt, sparse_matrix, sparse_matrix, mx_el_gt) |
|
117 DEFBINOP_FN (ne, sparse_matrix, sparse_matrix, mx_el_ne) |
|
118 |
|
119 DEFBINOP_FN (el_mul, sparse_matrix, sparse_matrix, product) |
|
120 DEFBINOP_FN (el_div, sparse_matrix, sparse_matrix, quotient) |
|
121 |
|
122 DEFBINOP_FN (el_pow, sparse_matrix, sparse_matrix, elem_xpow) |
|
123 |
|
124 DEFBINOP (el_ldiv, sparse_matrix, sparse_matrix) |
|
125 { |
|
126 CAST_BINOP_ARGS (const octave_sparse_matrix&, const octave_sparse_matrix&); |
|
127 return octave_value |
|
128 (quotient (v2.sparse_matrix_value (), v1.sparse_matrix_value ())); |
|
129 } |
|
130 |
|
131 DEFBINOP_FN (el_and, sparse_matrix, sparse_matrix, mx_el_and) |
|
132 DEFBINOP_FN (el_or, sparse_matrix, sparse_matrix, mx_el_or) |
|
133 |
|
134 DEFCATOP_FN (sm_sm, sparse_matrix, sparse_matrix, concat) |
|
135 |
|
136 DEFASSIGNOP_FN (assign, sparse_matrix, sparse_matrix, assign) |
|
137 |
|
138 void |
|
139 install_sm_sm_ops (void) |
|
140 { |
|
141 INSTALL_UNOP (op_not, octave_sparse_matrix, not); |
|
142 INSTALL_UNOP (op_uplus, octave_sparse_matrix, uplus); |
|
143 INSTALL_UNOP (op_uminus, octave_sparse_matrix, uminus); |
|
144 INSTALL_UNOP (op_transpose, octave_sparse_matrix, transpose); |
|
145 INSTALL_UNOP (op_hermitian, octave_sparse_matrix, transpose); |
|
146 |
|
147 INSTALL_BINOP (op_add, octave_sparse_matrix, octave_sparse_matrix, add); |
|
148 INSTALL_BINOP (op_sub, octave_sparse_matrix, octave_sparse_matrix, sub); |
|
149 INSTALL_BINOP (op_mul, octave_sparse_matrix, octave_sparse_matrix, mul); |
|
150 INSTALL_BINOP (op_div, octave_sparse_matrix, octave_sparse_matrix, div); |
|
151 INSTALL_BINOP (op_pow, octave_sparse_matrix, octave_sparse_matrix, pow); |
|
152 INSTALL_BINOP (op_ldiv, octave_sparse_matrix, octave_sparse_matrix, ldiv); |
|
153 INSTALL_BINOP (op_lt, octave_sparse_matrix, octave_sparse_matrix, lt); |
|
154 INSTALL_BINOP (op_le, octave_sparse_matrix, octave_sparse_matrix, le); |
|
155 INSTALL_BINOP (op_eq, octave_sparse_matrix, octave_sparse_matrix, eq); |
|
156 INSTALL_BINOP (op_ge, octave_sparse_matrix, octave_sparse_matrix, ge); |
|
157 INSTALL_BINOP (op_gt, octave_sparse_matrix, octave_sparse_matrix, gt); |
|
158 INSTALL_BINOP (op_ne, octave_sparse_matrix, octave_sparse_matrix, ne); |
|
159 INSTALL_BINOP (op_el_mul, octave_sparse_matrix, octave_sparse_matrix, |
|
160 el_mul); |
|
161 INSTALL_BINOP (op_el_div, octave_sparse_matrix, octave_sparse_matrix, |
|
162 el_div); |
|
163 INSTALL_BINOP (op_el_pow, octave_sparse_matrix, octave_sparse_matrix, |
|
164 el_pow); |
|
165 INSTALL_BINOP (op_el_ldiv, octave_sparse_matrix, octave_sparse_matrix, |
|
166 el_ldiv); |
|
167 INSTALL_BINOP (op_el_and, octave_sparse_matrix, octave_sparse_matrix, |
|
168 el_and); |
|
169 INSTALL_BINOP (op_el_or, octave_sparse_matrix, octave_sparse_matrix, |
|
170 el_or); |
|
171 |
|
172 INSTALL_CATOP (octave_sparse_matrix, octave_sparse_matrix, sm_sm); |
|
173 |
|
174 INSTALL_ASSIGNOP (op_asn_eq, octave_sparse_matrix, octave_sparse_matrix, |
|
175 assign); |
|
176 } |
|
177 |
|
178 /* |
|
179 ;;; Local Variables: *** |
|
180 ;;; mode: C++ *** |
|
181 ;;; End: *** |
|
182 */ |