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