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