1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1998-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29
30 #include "ovl.h"
31 #include "ov.h"
32 #include "ov-typeinfo.h"
33 #include "ov-null-mat.h"
34 #include "ops.h"
35
36 #include "sparse-xdiv.h"
37 #include "sparse-xpow.h"
38 #include "ov-cx-mat.h"
39 #include "ov-cx-sparse.h"
40 #include "ov-re-sparse.h"
41
42 #include "xdiv.h"
43 #include "xpow.h"
44
45 // unary sparse complex matrix ops.
46
47 DEFUNOP_OP (not, sparse_complex_matrix, !)
48 DEFUNOP_OP (uplus, sparse_complex_matrix, /* no-op */)
49 DEFUNOP_OP (uminus, sparse_complex_matrix, -)
50
DEFUNOP(transpose,sparse_complex_matrix)51 DEFUNOP (transpose, sparse_complex_matrix)
52 {
53 const octave_sparse_complex_matrix& v
54 = dynamic_cast<const octave_sparse_complex_matrix&> (a);
55 return octave_value
56 (v.sparse_complex_matrix_value ().transpose (),
57 v.matrix_type ().transpose ());
58 }
59
DEFUNOP(hermitian,sparse_complex_matrix)60 DEFUNOP (hermitian, sparse_complex_matrix)
61 {
62 const octave_sparse_complex_matrix& v
63 = dynamic_cast<const octave_sparse_complex_matrix&> (a);
64 return octave_value
65 (v.sparse_complex_matrix_value ().hermitian (),
66 v.matrix_type ().transpose ());
67 }
68
69 // complex matrix by complex matrix ops.
70
71 DEFBINOP_OP (add, sparse_complex_matrix, sparse_complex_matrix, +)
72 DEFBINOP_OP (sub, sparse_complex_matrix, sparse_complex_matrix, -)
73
74 DEFBINOP_OP (mul, sparse_complex_matrix, sparse_complex_matrix, *)
75
DEFBINOP(div,sparse_complex_matrix,sparse_complex_matrix)76 DEFBINOP (div, sparse_complex_matrix, sparse_complex_matrix)
77 {
78 const octave_sparse_complex_matrix& v1
79 = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
80 const octave_sparse_complex_matrix& v2
81 = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
82
83 if (v2.rows () == 1 && v2.columns () == 1)
84 return octave_value (v1.sparse_complex_matrix_value () / v2.complex_value ());
85 else
86 {
87 MatrixType typ = v2.matrix_type ();
88 SparseComplexMatrix ret = xdiv (v1.sparse_complex_matrix_value (),
89 v2.sparse_complex_matrix_value (), typ);
90
91 v2.matrix_type (typ);
92 return ret;
93 }
94 }
95
DEFBINOPX(pow,sparse_complex_matrix,sparse_complex_matrix)96 DEFBINOPX (pow, sparse_complex_matrix, sparse_complex_matrix)
97 {
98 error ("can't do A ^ B for A and B both matrices");
99 }
100
DEFBINOP(ldiv,sparse_complex_matrix,sparse_complex_matrix)101 DEFBINOP (ldiv, sparse_complex_matrix, sparse_complex_matrix)
102 {
103 const octave_sparse_complex_matrix& v1
104 = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
105 const octave_sparse_complex_matrix& v2
106 = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
107
108 if (v1.rows () == 1 && v1.columns () == 1)
109 return octave_value (v2.sparse_complex_matrix_value () / v1.complex_value ());
110 else
111 {
112 MatrixType typ = v1.matrix_type ();
113
114 SparseComplexMatrix ret
115 = xleftdiv (v1.sparse_complex_matrix_value (),
116 v2.sparse_complex_matrix_value (), typ);
117
118 v1.matrix_type (typ);
119 return ret;
120 }
121 }
122
DEFBINOP_FN(lt,sparse_complex_matrix,sparse_complex_matrix,mx_el_lt)123 DEFBINOP_FN (lt, sparse_complex_matrix, sparse_complex_matrix, mx_el_lt)
124 DEFBINOP_FN (le, sparse_complex_matrix, sparse_complex_matrix, mx_el_le)
125 DEFBINOP_FN (eq, sparse_complex_matrix, sparse_complex_matrix, mx_el_eq)
126 DEFBINOP_FN (ge, sparse_complex_matrix, sparse_complex_matrix, mx_el_ge)
127 DEFBINOP_FN (gt, sparse_complex_matrix, sparse_complex_matrix, mx_el_gt)
128 DEFBINOP_FN (ne, sparse_complex_matrix, sparse_complex_matrix, mx_el_ne)
129
130 DEFBINOP_FN (el_mul, sparse_complex_matrix, sparse_complex_matrix, product)
131 DEFBINOP_FN (el_div, sparse_complex_matrix, sparse_complex_matrix, quotient)
132 DEFBINOP_FN (el_pow, sparse_complex_matrix, sparse_complex_matrix, elem_xpow)
133
134 DEFBINOP (el_ldiv, sparse_complex_matrix, sparse_complex_matrix)
135 {
136 const octave_sparse_complex_matrix& v1
137 = dynamic_cast<const octave_sparse_complex_matrix&> (a1);
138 const octave_sparse_complex_matrix& v2
139 = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
140
141 return octave_value (quotient (v2.sparse_complex_matrix_value (),
142 v1.sparse_complex_matrix_value ()));
143 }
144
DEFBINOP_FN(el_and,sparse_complex_matrix,sparse_complex_matrix,mx_el_and)145 DEFBINOP_FN (el_and, sparse_complex_matrix, sparse_complex_matrix, mx_el_and)
146 DEFBINOP_FN (el_or, sparse_complex_matrix, sparse_complex_matrix, mx_el_or)
147
148 DEFCATOP_FN (scm_scm, sparse_complex_matrix, sparse_complex_matrix, concat)
149
150 DEFASSIGNOP_FN (assign, sparse_complex_matrix, sparse_complex_matrix, assign)
151
152 DEFNULLASSIGNOP_FN (null_assign, sparse_complex_matrix, delete_elements)
153
154 void
155 install_scm_scm_ops (octave::type_info& ti)
156 {
157 INSTALL_UNOP_TI (ti, op_not, octave_sparse_complex_matrix, not);
158 INSTALL_UNOP_TI (ti, op_uplus, octave_sparse_complex_matrix, uplus);
159 INSTALL_UNOP_TI (ti, op_uminus, octave_sparse_complex_matrix, uminus);
160 INSTALL_UNOP_TI (ti, op_transpose, octave_sparse_complex_matrix, transpose);
161 INSTALL_UNOP_TI (ti, op_hermitian, octave_sparse_complex_matrix, hermitian);
162
163 INSTALL_BINOP_TI (ti, op_add, octave_sparse_complex_matrix,
164 octave_sparse_complex_matrix, add);
165 INSTALL_BINOP_TI (ti, op_sub, octave_sparse_complex_matrix,
166 octave_sparse_complex_matrix, sub);
167 INSTALL_BINOP_TI (ti, op_mul, octave_sparse_complex_matrix,
168 octave_sparse_complex_matrix, mul);
169 INSTALL_BINOP_TI (ti, op_div, octave_sparse_complex_matrix,
170 octave_sparse_complex_matrix, div);
171 INSTALL_BINOP_TI (ti, op_pow, octave_sparse_complex_matrix,
172 octave_sparse_complex_matrix, pow);
173 INSTALL_BINOP_TI (ti, op_ldiv, octave_sparse_complex_matrix,
174 octave_sparse_complex_matrix, ldiv);
175 INSTALL_BINOP_TI (ti, op_lt, octave_sparse_complex_matrix,
176 octave_sparse_complex_matrix, lt);
177 INSTALL_BINOP_TI (ti, op_le, octave_sparse_complex_matrix,
178 octave_sparse_complex_matrix, le);
179 INSTALL_BINOP_TI (ti, op_eq, octave_sparse_complex_matrix,
180 octave_sparse_complex_matrix, eq);
181 INSTALL_BINOP_TI (ti, op_ge, octave_sparse_complex_matrix,
182 octave_sparse_complex_matrix, ge);
183 INSTALL_BINOP_TI (ti, op_gt, octave_sparse_complex_matrix,
184 octave_sparse_complex_matrix, gt);
185 INSTALL_BINOP_TI (ti, op_ne, octave_sparse_complex_matrix,
186 octave_sparse_complex_matrix, ne);
187 INSTALL_BINOP_TI (ti, op_el_mul, octave_sparse_complex_matrix,
188 octave_sparse_complex_matrix, el_mul);
189 INSTALL_BINOP_TI (ti, op_el_div, octave_sparse_complex_matrix,
190 octave_sparse_complex_matrix, el_div);
191 INSTALL_BINOP_TI (ti, op_el_pow, octave_sparse_complex_matrix,
192 octave_sparse_complex_matrix, el_pow);
193 INSTALL_BINOP_TI (ti, op_el_ldiv, octave_sparse_complex_matrix,
194 octave_sparse_complex_matrix, el_ldiv);
195 INSTALL_BINOP_TI (ti, op_el_and, octave_sparse_complex_matrix,
196 octave_sparse_complex_matrix, el_and);
197 INSTALL_BINOP_TI (ti, op_el_or, octave_sparse_complex_matrix,
198 octave_sparse_complex_matrix, el_or);
199
200 INSTALL_CATOP_TI (ti, octave_sparse_complex_matrix,
201 octave_sparse_complex_matrix, scm_scm);
202
203 INSTALL_ASSIGNOP_TI (ti, op_asn_eq, octave_sparse_complex_matrix,
204 octave_sparse_complex_matrix, assign);
205
206 INSTALL_ASSIGNOP_TI (ti, op_asn_eq, octave_sparse_complex_matrix,
207 octave_null_matrix, null_assign);
208 INSTALL_ASSIGNOP_TI (ti, op_asn_eq, octave_sparse_complex_matrix,
209 octave_null_str, null_assign);
210 INSTALL_ASSIGNOP_TI (ti, op_asn_eq, octave_sparse_complex_matrix,
211 octave_null_sq_str, null_assign);
212 }
213