GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
op-m-m.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-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 "errwarn.h"
31 #include "ovl.h"
32 #include "ov.h"
33 #include "ov-re-mat.h"
34 #include "ov-flt-re-mat.h"
35 #include "ov-typeinfo.h"
36 #include "ov-null-mat.h"
37 #include "ops.h"
38 #include "xdiv.h"
39 #include "xpow.h"
40 
41 // matrix unary ops.
42 
43 DEFNDUNOP_OP (not, matrix, array, !)
44 DEFNDUNOP_OP (uplus, matrix, array, /* no-op */)
45 DEFNDUNOP_OP (uminus, matrix, array, -)
46 
47 DEFUNOP (transpose, matrix)
48 {
49  const octave_matrix& v = dynamic_cast<const octave_matrix&> (a);
50 
51  if (v.ndims () > 2)
52  error ("transpose not defined for N-D objects");
53 
54  return octave_value (v.matrix_value ().transpose ());
55 }
56 
57 DEFNCUNOP_METHOD (incr, matrix, increment)
58 DEFNCUNOP_METHOD (decr, matrix, decrement)
59 DEFNCUNOP_METHOD (changesign, matrix, changesign)
60 
61 // matrix by matrix ops.
62 
63 DEFNDBINOP_OP (add, matrix, matrix, array, array, +)
64 DEFNDBINOP_OP (sub, matrix, matrix, array, array, -)
65 
66 DEFBINOP_OP (mul, matrix, matrix, *)
67 
68 DEFBINOP (div, matrix, matrix)
69 {
70  const octave_matrix& v1 = dynamic_cast<const octave_matrix&> (a1);
71  const octave_matrix& v2 = dynamic_cast<const octave_matrix&> (a2);
72  MatrixType typ = v2.matrix_type ();
73 
74  Matrix ret = xdiv (v1.matrix_value (), v2.matrix_value (), typ);
75 
76  v2.matrix_type (typ);
77  return ret;
78 }
79 
80 DEFBINOPX (pow, matrix, matrix)
81 {
82  error ("can't do A ^ B for A and B both matrices");
83 }
84 
85 DEFBINOP (ldiv, matrix, matrix)
86 {
87  const octave_matrix& v1 = dynamic_cast<const octave_matrix&> (a1);
88  const octave_matrix& v2 = dynamic_cast<const octave_matrix&> (a2);
89  MatrixType typ = v1.matrix_type ();
90 
91  Matrix ret = xleftdiv (v1.matrix_value (), v2.matrix_value (), typ);
92 
93  v1.matrix_type (typ);
94  return ret;
95 }
96 
97 DEFBINOP (trans_mul, matrix, matrix)
98 {
99  const octave_matrix& v1 = dynamic_cast<const octave_matrix&> (a1);
100  const octave_matrix& v2 = dynamic_cast<const octave_matrix&> (a2);
101  return octave_value(xgemm (v1.matrix_value (), v2.matrix_value (),
103 }
104 
105 DEFBINOP (mul_trans, matrix, matrix)
106 {
107  const octave_matrix& v1 = dynamic_cast<const octave_matrix&> (a1);
108  const octave_matrix& v2 = dynamic_cast<const octave_matrix&> (a2);
109  return octave_value(xgemm (v1.matrix_value (), v2.matrix_value (),
111 }
112 
113 DEFBINOP (trans_ldiv, matrix, matrix)
114 {
115  const octave_matrix& v1 = dynamic_cast<const octave_matrix&> (a1);
116  const octave_matrix& v2 = dynamic_cast<const octave_matrix&> (a2);
117  MatrixType typ = v1.matrix_type ();
118 
119  Matrix ret = xleftdiv (v1.matrix_value (), v2.matrix_value (),
120  typ, blas_trans);
121 
122  v1.matrix_type (typ);
123  return ret;
124 }
125 
126 DEFNDBINOP_FN (lt, matrix, matrix, array, array, mx_el_lt)
127 DEFNDBINOP_FN (le, matrix, matrix, array, array, mx_el_le)
128 DEFNDBINOP_FN (eq, matrix, matrix, array, array, mx_el_eq)
129 DEFNDBINOP_FN (ge, matrix, matrix, array, array, mx_el_ge)
130 DEFNDBINOP_FN (gt, matrix, matrix, array, array, mx_el_gt)
131 DEFNDBINOP_FN (ne, matrix, matrix, array, array, mx_el_ne)
132 
133 DEFNDBINOP_FN (el_mul, matrix, matrix, array, array, product)
134 DEFNDBINOP_FN (el_div, matrix, matrix, array, array, quotient)
135 DEFNDBINOP_FN (el_pow, matrix, matrix, array, array, elem_xpow)
136 
137 DEFBINOP (el_ldiv, matrix, matrix)
138 {
139  const octave_matrix& v1 = dynamic_cast<const octave_matrix&> (a1);
140  const octave_matrix& v2 = dynamic_cast<const octave_matrix&> (a2);
141 
142  return octave_value (quotient (v2.array_value (), v1.array_value ()));
143 }
144 
145 DEFNDBINOP_FN (el_and, matrix, matrix, array, array, mx_el_and)
146 DEFNDBINOP_FN (el_or, matrix, matrix, array, array, mx_el_or)
147 DEFNDBINOP_FN (el_not_and, matrix, matrix, array, array, mx_el_not_and)
148 DEFNDBINOP_FN (el_not_or, matrix, matrix, array, array, mx_el_not_or)
149 DEFNDBINOP_FN (el_and_not, matrix, matrix, array, array, mx_el_and_not)
150 DEFNDBINOP_FN (el_or_not, matrix, matrix, array, array, mx_el_or_not)
151 
152 DEFNDCATOP_FN (m_m, matrix, matrix, array, array, concat)
153 
154 DEFNDASSIGNOP_FN (assign, matrix, matrix, array, assign)
155 DEFNDASSIGNOP_FN (sgl_assign, float_matrix, matrix, float_array, assign)
156 
157 DEFNULLASSIGNOP_FN (null_assign, matrix, delete_elements)
158 
159 DEFNDASSIGNOP_OP (assign_add, matrix, matrix, array, +=)
160 DEFNDASSIGNOP_OP (assign_sub, matrix, matrix, array, -=)
161 DEFNDASSIGNOP_FNOP (assign_el_mul, matrix, matrix, array, product_eq)
162 DEFNDASSIGNOP_FNOP (assign_el_div, matrix, matrix, array, quotient_eq)
163 
164 void
165 install_m_m_ops (octave::type_info& ti)
166 {
168  INSTALL_UNOP_TI (ti, op_uplus, octave_matrix, uplus);
169  INSTALL_UNOP_TI (ti, op_uminus, octave_matrix, uminus);
172 
173  INSTALL_NCUNOP_TI (ti, op_incr, octave_matrix, incr);
174  INSTALL_NCUNOP_TI (ti, op_decr, octave_matrix, decr);
175  INSTALL_NCUNOP_TI (ti, op_uminus, octave_matrix, changesign);
176 
195  INSTALL_BINOP_TI (ti, op_el_and_not, octave_matrix, octave_matrix, el_and_not);
196  INSTALL_BINOP_TI (ti, op_el_or_not, octave_matrix, octave_matrix, el_or_not);
197  INSTALL_BINOP_TI (ti, op_el_not_and, octave_matrix, octave_matrix, el_not_and);
198  INSTALL_BINOP_TI (ti, op_el_not_or, octave_matrix, octave_matrix, el_not_or);
203  INSTALL_BINOP_TI (ti, op_trans_ldiv, octave_matrix, octave_matrix, trans_ldiv);
204  INSTALL_BINOP_TI (ti, op_herm_ldiv, octave_matrix, octave_matrix, trans_ldiv);
205 
207 
208  INSTALL_ASSIGNOP_TI (ti, op_asn_eq, octave_matrix, octave_matrix, assign);
210  sgl_assign);
211 
213  null_assign);
215  null_assign);
217  null_assign);
218 
219  INSTALL_ASSIGNOP_TI (ti, op_add_eq, octave_matrix, octave_matrix, assign_add);
220  INSTALL_ASSIGNOP_TI (ti, op_sub_eq, octave_matrix, octave_matrix, assign_sub);
221  INSTALL_ASSIGNOP_TI (ti, op_el_mul_eq, octave_matrix, octave_matrix,
222  assign_el_mul);
223  INSTALL_ASSIGNOP_TI (ti, op_el_div_eq, octave_matrix, octave_matrix,
224  assign_el_div);
225 }
ComplexColumnVector quotient(const ComplexColumnVector &x, const ComplexColumnVector &y)
Definition: CColVector.h:152
ComplexColumnVector product_eq(ComplexColumnVector &x, const ComplexColumnVector &y)
Definition: CColVector.h:152
ComplexColumnVector product(const ComplexColumnVector &x, const ComplexColumnVector &y)
Definition: CColVector.h:152
ComplexColumnVector quotient_eq(ComplexColumnVector &x, const ComplexColumnVector &y)
Definition: CColVector.h:152
ComplexMatrix xgemm(const ComplexMatrix &a, const ComplexMatrix &b, blas_trans_type transa, blas_trans_type transb)
Definition: CMatrix.cc:3322
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: CNDArray.cc:418
ComplexMatrix mul_trans(const ComplexMatrix &m, const SparseComplexMatrix &a)
Definition: CSparse.cc:7543
ComplexMatrix trans_mul(const SparseComplexMatrix &m, const ComplexMatrix &a)
Definition: CSparse.cc:7573
boolMatrix mx_el_le(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:91
boolMatrix mx_el_ne(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:91
boolMatrix mx_el_or(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:88
boolMatrix mx_el_and(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:88
boolMatrix mx_el_gt(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:91
boolMatrix mx_el_eq(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:91
boolMatrix mx_el_lt(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:91
boolMatrix mx_el_ge(const boolMatrix &m1, const boolMatrix &m2)
Definition: boolMatrix.cc:91
boolNDArray mx_el_or_not(const boolNDArray &m1, const boolNDArray &m2)
Definition: boolNDArray.cc:122
boolNDArray mx_el_and_not(const boolNDArray &m1, const boolNDArray &m2)
Definition: boolNDArray.cc:122
boolNDArray mx_el_not_and(const boolNDArray &m1, const boolNDArray &m2)
Definition: boolNDArray.cc:122
boolNDArray mx_el_not_or(const boolNDArray &m1, const boolNDArray &m2)
Definition: boolNDArray.cc:122
Definition: dMatrix.h:42
Matrix transpose(void) const
Definition: dMatrix.h:135
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:131
int ndims(void) const
Definition: ov-base-mat.h:116
Matrix matrix_value(bool=false) const
Definition: ov-ch-mat.h:114
NDArray array_value(bool=false) const
Definition: ov-ch-mat.h:120
Matrix matrix_value(bool=false) const
Definition: ov-re-mat.cc:145
NDArray array_value(bool=false) const
Definition: ov-re-mat.h:172
void error(const char *fmt,...)
Definition: error.cc:968
@ blas_no_trans
Definition: mx-defs.h:110
@ blas_trans
Definition: mx-defs.h:111
octave_int< T > pow(const octave_int< T > &a, const octave_int< T > &b)
const octave_base_value & a2
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
const octave_char_matrix & v2
void install_m_m_ops(octave::type_info &ti)
Definition: op-m-m.cc:165
#define DEFNDASSIGNOP_FN(name, t1, t2, e, f)
Definition: ops.h:112
#define DEFNDBINOP_OP(name, t1, t2, e1, e2, op)
Definition: ops.h:276
#define INSTALL_NCUNOP_TI(ti, op, t, f)
Definition: ops.h:51
#define DEFNULLASSIGNOP_FN(name, t, f)
Definition: ops.h:100
#define DEFNDBINOP_FN(name, t1, t2, e1, e2, f)
Definition: ops.h:301
#define DEFBINOPX(name, t1, t2)
Definition: ops.h:225
#define DEFNDASSIGNOP_FNOP(name, t1, t2, f, fnop)
Definition: ops.h:141
#define DEFBINOP(name, t1, t2)
Definition: ops.h:230
#define DEFNDUNOP_OP(name, t, e, op)
Definition: ops.h:191
#define DEFBINOP_OP(name, t1, t2, op)
Definition: ops.h:235
#define DEFNCUNOP_METHOD(name, t, method)
Definition: ops.h:217
#define INSTALL_BINOP_TI(ti, op, t1, t2, f)
Definition: ops.h:55
#define DEFNDCATOP_FN(name, t1, t2, e1, e2, f)
Definition: ops.h:348
#define DEFUNOP(name, t)
Definition: ops.h:179
#define INSTALL_CATOP_TI(ti, t1, t2, f)
Definition: ops.h:60
#define INSTALL_UNOP_TI(ti, op, t, f)
Definition: ops.h:47
#define DEFNDASSIGNOP_OP(name, t1, t2, f, op)
Definition: ops.h:126
#define INSTALL_ASSIGNOP_TI(ti, op, t1, t2, f)
Definition: ops.h:64
octave_value op_ne(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1579
octave_value op_el_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1584
octave_value op_uplus(const octave_value &a)
Definition: ov.h:1537
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1566
octave_value op_gt(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1578
octave_value op_uminus(const octave_value &a)
Definition: ov.h:1538
octave_value op_el_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1582
octave_value op_mul_herm(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1600
octave_value op_transpose(const octave_value &a)
Definition: ov.h:1540
octave_value op_el_or(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1586
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1567
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1568
octave_value op_hermitian(const octave_value &a)
Definition: ov.h:1541
octave_value op_le(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1575
octave_value op_el_and(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1585
octave_value op_herm_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1599
octave_value op_eq(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1576
octave_value op_el_pow(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1583
octave_value op_not(const octave_value &a)
Definition: ov.h:1536
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1572
octave_value op_pow(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1571
octave_value op_ge(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1577
octave_value op_el_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1581
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1569
octave_value op_trans_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1597
octave_value op_mul_trans(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1598
octave_value op_lt(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1574
Matrix xdiv(const Matrix &a, const SparseMatrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:135
Matrix xleftdiv(const SparseMatrix &a, const Matrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:466
octave_value elem_xpow(double a, const SparseMatrix &b)
Definition: sparse-xpow.cc:249
static void transpose(octave_idx_type N, const octave_idx_type *ridx, const octave_idx_type *cidx, octave_idx_type *ridx2, octave_idx_type *cidx2)
Definition: symrcm.cc:389