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 "ops.h"
34 
35 #include "sparse-xdiv.h"
36 #include "sparse-xpow.h"
37 #include "smx-sm-scm.h"
38 #include "smx-scm-sm.h"
39 #include "ov-re-sparse.h"
40 #include "ov-cx-sparse.h"
41 
42 // sparse matrix by sparse complex matrix ops.
43 
44 DEFBINOP_OP (add, sparse_matrix, sparse_complex_matrix, +)
45 DEFBINOP_OP (sub, sparse_matrix, sparse_complex_matrix, -)
46 
47 DEFBINOP_OP (mul, sparse_matrix, sparse_complex_matrix, *)
48 
DEFBINOP(div,sparse_matrix,sparse_complex_matrix)49 DEFBINOP (div, sparse_matrix, sparse_complex_matrix)
50 {
51   const octave_sparse_matrix& v1
52     = dynamic_cast<const octave_sparse_matrix&> (a1);
53   const octave_sparse_complex_matrix& v2
54     = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
55 
56   if (v2.rows () == 1 && v2.columns () == 1)
57     return octave_value (v1.sparse_matrix_value () / v2.complex_value ());
58   else
59     {
60       MatrixType typ = v2.matrix_type ();
61       SparseComplexMatrix ret = xdiv (v1.sparse_matrix_value (),
62                                       v2.sparse_complex_matrix_value (), typ);
63 
64       v2.matrix_type (typ);
65       return ret;
66     }
67 }
68 
DEFBINOPX(pow,sparse_matrix,sparse_complex_matrix)69 DEFBINOPX (pow, sparse_matrix, sparse_complex_matrix)
70 {
71   error ("can't do A ^ B for A and B both matrices");
72 }
73 
DEFBINOP(ldiv,sparse_matrix,sparse_complex_matrix)74 DEFBINOP (ldiv, sparse_matrix, sparse_complex_matrix)
75 {
76   const octave_sparse_matrix& v1
77     = dynamic_cast<const octave_sparse_matrix&> (a1);
78   const octave_sparse_complex_matrix& v2
79     = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
80 
81   if (v1.rows () == 1 && v1.columns () == 1)
82     return octave_value (v2.sparse_complex_matrix_value () / v1.scalar_value ());
83   else
84     {
85       MatrixType typ = v1.matrix_type ();
86 
87       SparseComplexMatrix ret
88         = xleftdiv (v1.sparse_matrix_value (),
89                     v2.sparse_complex_matrix_value (), typ);
90 
91       v1.matrix_type (typ);
92       return ret;
93     }
94 }
95 
DEFBINOP_FN(lt,sparse_matrix,sparse_complex_matrix,mx_el_lt)96 DEFBINOP_FN (lt, sparse_matrix, sparse_complex_matrix, mx_el_lt)
97 DEFBINOP_FN (le, sparse_matrix, sparse_complex_matrix, mx_el_le)
98 DEFBINOP_FN (eq, sparse_matrix, sparse_complex_matrix, mx_el_eq)
99 DEFBINOP_FN (ge, sparse_matrix, sparse_complex_matrix, mx_el_ge)
100 DEFBINOP_FN (gt, sparse_matrix, sparse_complex_matrix, mx_el_gt)
101 DEFBINOP_FN (ne, sparse_matrix, sparse_complex_matrix, mx_el_ne)
102 
103 DEFBINOP_FN (el_mul, sparse_matrix, sparse_complex_matrix, product)
104 DEFBINOP_FN (el_div, sparse_matrix, sparse_complex_matrix, quotient)
105 DEFBINOP_FN (el_pow, sparse_matrix, sparse_complex_matrix, elem_xpow)
106 
107 DEFBINOP (el_ldiv, sparse_matrix, sparse_complex_matrix)
108 {
109   const octave_sparse_matrix& v1
110     = dynamic_cast<const octave_sparse_matrix&> (a1);
111   const octave_sparse_complex_matrix& v2
112     = dynamic_cast<const octave_sparse_complex_matrix&> (a2);
113 
114   return octave_value (quotient (v2.sparse_complex_matrix_value (),
115                                  v1.sparse_matrix_value ()));
116 }
117 
DEFBINOP_FN(el_and,sparse_matrix,sparse_complex_matrix,mx_el_and)118 DEFBINOP_FN (el_and, sparse_matrix, sparse_complex_matrix, mx_el_and)
119 DEFBINOP_FN (el_or,  sparse_matrix, sparse_complex_matrix, mx_el_or)
120 
121 DEFCATOP_FN (sm_scm, sparse_matrix, sparse_complex_matrix, concat)
122 
123 DEFCONV (sparse_complex_matrix_conv, sparse_matrix, sparse_complex_matrix)
124 {
125   const octave_sparse_matrix& v
126     = dynamic_cast<const octave_sparse_matrix&> (a);
127   return new octave_sparse_complex_matrix (v.sparse_complex_matrix_value ());
128 }
129 
130 void
install_sm_scm_ops(octave::type_info & ti)131 install_sm_scm_ops (octave::type_info& ti)
132 {
133   INSTALL_BINOP_TI (ti, op_add, octave_sparse_matrix,
134                     octave_sparse_complex_matrix,
135                     add);
136   INSTALL_BINOP_TI (ti, op_sub, octave_sparse_matrix,
137                     octave_sparse_complex_matrix,
138                     sub);
139   INSTALL_BINOP_TI (ti, op_mul, octave_sparse_matrix,
140                     octave_sparse_complex_matrix,
141                     mul);
142   INSTALL_BINOP_TI (ti, op_div, octave_sparse_matrix,
143                     octave_sparse_complex_matrix,
144                     div);
145   INSTALL_BINOP_TI (ti, op_pow, octave_sparse_matrix,
146                     octave_sparse_complex_matrix,
147                     pow);
148   INSTALL_BINOP_TI (ti, op_ldiv, octave_sparse_matrix,
149                     octave_sparse_complex_matrix,
150                     ldiv);
151   INSTALL_BINOP_TI (ti, op_lt, octave_sparse_matrix, octave_sparse_complex_matrix,
152                     lt);
153   INSTALL_BINOP_TI (ti, op_le, octave_sparse_matrix, octave_sparse_complex_matrix,
154                     le);
155   INSTALL_BINOP_TI (ti, op_eq, octave_sparse_matrix, octave_sparse_complex_matrix,
156                     eq);
157   INSTALL_BINOP_TI (ti, op_ge, octave_sparse_matrix, octave_sparse_complex_matrix,
158                     ge);
159   INSTALL_BINOP_TI (ti, op_gt, octave_sparse_matrix, octave_sparse_complex_matrix,
160                     gt);
161   INSTALL_BINOP_TI (ti, op_ne, octave_sparse_matrix, octave_sparse_complex_matrix,
162                     ne);
163   INSTALL_BINOP_TI (ti, op_el_mul, octave_sparse_matrix,
164                     octave_sparse_complex_matrix, el_mul);
165   INSTALL_BINOP_TI (ti, op_el_div, octave_sparse_matrix,
166                     octave_sparse_complex_matrix, el_div);
167   INSTALL_BINOP_TI (ti, op_el_pow, octave_sparse_matrix,
168                     octave_sparse_complex_matrix, el_pow);
169   INSTALL_BINOP_TI (ti, op_el_ldiv, octave_sparse_matrix,
170                     octave_sparse_complex_matrix, el_ldiv);
171   INSTALL_BINOP_TI (ti, op_el_and, octave_sparse_matrix,
172                     octave_sparse_complex_matrix, el_and);
173   INSTALL_BINOP_TI (ti, op_el_or, octave_sparse_matrix,
174                     octave_sparse_complex_matrix, el_or);
175 
176   INSTALL_CATOP_TI (ti, octave_sparse_matrix, octave_sparse_complex_matrix,
177                     sm_scm);
178 
179   INSTALL_ASSIGNCONV_TI (ti, octave_sparse_matrix, octave_sparse_complex_matrix,
180                          octave_sparse_complex_matrix);
181 
182   INSTALL_WIDENOP_TI (ti, octave_sparse_matrix, octave_sparse_complex_matrix,
183                       sparse_complex_matrix_conv);
184 }
185