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