GNU Octave  9.1.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-2024 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 
42 
43 // matrix unary ops.
44 
45 DEFNDUNOP_OP (not, matrix, array, !)
46 DEFNDUNOP_OP (uplus, matrix, array, /* no-op */)
47 DEFNDUNOP_OP (uminus, matrix, array, -)
48 
49 DEFUNOP (transpose, matrix)
50 {
52 
53  if (v.ndims () > 2)
54  error ("transpose not defined for N-D objects");
55 
56  return octave_value (v.matrix_value ().transpose ());
57 }
58 
59 DEFNCUNOP_METHOD (incr, matrix, increment)
60 DEFNCUNOP_METHOD (decr, matrix, decrement)
61 DEFNCUNOP_METHOD (changesign, matrix, changesign)
62 
63 // matrix by matrix ops.
64 
65 DEFNDBINOP_OP (add, matrix, matrix, array, array, +)
66 DEFNDBINOP_OP (sub, matrix, matrix, array, array, -)
67 
68 DEFBINOP_OP (mul, matrix, matrix, *)
69 
70 DEFBINOP (div, matrix, matrix)
71 {
72  OCTAVE_CAST_BASE_VALUE (const octave_matrix&, v1, a1);
74  MatrixType typ = v2.matrix_type ();
75 
76  Matrix ret = xdiv (v1.matrix_value (), v2.matrix_value (), typ);
77 
78  v2.matrix_type (typ);
79  return ret;
80 }
81 
82 DEFBINOPX (pow, matrix, matrix)
83 {
84  error ("can't do A ^ B for A and B both matrices");
85 }
86 
87 DEFBINOP (ldiv, matrix, matrix)
88 {
89  OCTAVE_CAST_BASE_VALUE (const octave_matrix&, v1, a1);
91  MatrixType typ = v1.matrix_type ();
92 
93  Matrix ret = xleftdiv (v1.matrix_value (), v2.matrix_value (), typ);
94 
95  v1.matrix_type (typ);
96  return ret;
97 }
98 
99 DEFBINOP (trans_mul, matrix, matrix)
100 {
101  OCTAVE_CAST_BASE_VALUE (const octave_matrix&, v1, a1);
103  return octave_value(xgemm (v1.matrix_value (), v2.matrix_value (),
105 }
106 
107 DEFBINOP (mul_trans, matrix, matrix)
108 {
109  OCTAVE_CAST_BASE_VALUE (const octave_matrix&, v1, a1);
111  return octave_value(xgemm (v1.matrix_value (), v2.matrix_value (),
113 }
114 
115 DEFBINOP (trans_ldiv, matrix, matrix)
116 {
117  OCTAVE_CAST_BASE_VALUE (const octave_matrix&, v1, a1);
119  MatrixType typ = v1.matrix_type ();
120 
121  Matrix ret = xleftdiv (v1.matrix_value (), v2.matrix_value (),
122  typ, blas_trans);
123 
124  v1.matrix_type (typ);
125  return ret;
126 }
127 
128 DEFNDBINOP_FN (lt, matrix, matrix, array, array, mx_el_lt)
129 DEFNDBINOP_FN (le, matrix, matrix, array, array, mx_el_le)
130 DEFNDBINOP_FN (eq, matrix, matrix, array, array, mx_el_eq)
131 DEFNDBINOP_FN (ge, matrix, matrix, array, array, mx_el_ge)
132 DEFNDBINOP_FN (gt, matrix, matrix, array, array, mx_el_gt)
133 DEFNDBINOP_FN (ne, matrix, matrix, array, array, mx_el_ne)
134 
135 DEFNDBINOP_FN (el_mul, matrix, matrix, array, array, product)
136 DEFNDBINOP_FN (el_div, matrix, matrix, array, array, quotient)
137 DEFNDBINOP_FN (el_pow, matrix, matrix, array, array, elem_xpow)
138 
139 DEFBINOP (el_ldiv, matrix, matrix)
140 {
141  OCTAVE_CAST_BASE_VALUE (const octave_matrix&, v1, a1);
143 
144  return octave_value (quotient (v2.array_value (), v1.array_value ()));
145 }
146 
147 DEFNDBINOP_FN (el_and, matrix, matrix, array, array, mx_el_and)
148 DEFNDBINOP_FN (el_or, matrix, matrix, array, array, mx_el_or)
149 DEFNDBINOP_FN (el_not_and, matrix, matrix, array, array, mx_el_not_and)
150 DEFNDBINOP_FN (el_not_or, matrix, matrix, array, array, mx_el_not_or)
151 DEFNDBINOP_FN (el_and_not, matrix, matrix, array, array, mx_el_and_not)
152 DEFNDBINOP_FN (el_or_not, matrix, matrix, array, array, mx_el_or_not)
153 
154 DEFNDCATOP_FN (m_m, matrix, matrix, array, array, concat)
155 
156 DEFNDASSIGNOP_FN (assign, matrix, matrix, array, assign)
157 DEFNDASSIGNOP_FN (sgl_assign, float_matrix, matrix, float_array, assign)
158 
159 DEFNULLASSIGNOP_FN (null_assign, matrix, delete_elements)
160 
161 DEFNDASSIGNOP_OP (assign_add, matrix, matrix, array, +=)
162 DEFNDASSIGNOP_OP (assign_sub, matrix, matrix, array, -=)
163 DEFNDASSIGNOP_FNOP (assign_el_mul, matrix, matrix, array, product_eq)
164 DEFNDASSIGNOP_FNOP (assign_el_div, matrix, matrix, array, quotient_eq)
165 
166 void
168 {
170  INSTALL_UNOP_TI (ti, op_uplus, octave_matrix, uplus);
171  INSTALL_UNOP_TI (ti, op_uminus, octave_matrix, uminus);
172  INSTALL_UNOP_TI (ti, op_transpose, octave_matrix, transpose);
173  INSTALL_UNOP_TI (ti, op_hermitian, octave_matrix, transpose);
174 
175  INSTALL_NCUNOP_TI (ti, op_incr, octave_matrix, incr);
176  INSTALL_NCUNOP_TI (ti, op_decr, octave_matrix, decr);
177  INSTALL_NCUNOP_TI (ti, op_uminus, octave_matrix, changesign);
178 
197  INSTALL_BINOP_TI (ti, op_el_and_not, octave_matrix, octave_matrix, el_and_not);
198  INSTALL_BINOP_TI (ti, op_el_or_not, octave_matrix, octave_matrix, el_or_not);
199  INSTALL_BINOP_TI (ti, op_el_not_and, octave_matrix, octave_matrix, el_not_and);
200  INSTALL_BINOP_TI (ti, op_el_not_or, octave_matrix, octave_matrix, el_not_or);
205  INSTALL_BINOP_TI (ti, op_trans_ldiv, octave_matrix, octave_matrix, trans_ldiv);
206  INSTALL_BINOP_TI (ti, op_herm_ldiv, octave_matrix, octave_matrix, trans_ldiv);
207 
209 
210  INSTALL_ASSIGNOP_TI (ti, op_asn_eq, octave_matrix, octave_matrix, assign);
212  sgl_assign);
213 
215  null_assign);
217  null_assign);
219  null_assign);
220 
221  INSTALL_ASSIGNOP_TI (ti, op_add_eq, octave_matrix, octave_matrix, assign_add);
222  INSTALL_ASSIGNOP_TI (ti, op_sub_eq, octave_matrix, octave_matrix, assign_sub);
223  INSTALL_ASSIGNOP_TI (ti, op_el_mul_eq, octave_matrix, octave_matrix,
224  assign_el_mul);
225  INSTALL_ASSIGNOP_TI (ti, op_el_div_eq, octave_matrix, octave_matrix,
226  assign_el_div);
227 }
228 
229 OCTAVE_END_NAMESPACE(octave)
ComplexColumnVector quotient(const ComplexColumnVector &x, const ComplexColumnVector &y)
Definition: CColVector.h:158
ComplexColumnVector product_eq(ComplexColumnVector &x, const ComplexColumnVector &y)
Definition: CColVector.h:158
ComplexColumnVector product(const ComplexColumnVector &x, const ComplexColumnVector &y)
Definition: CColVector.h:158
ComplexColumnVector quotient_eq(ComplexColumnVector &x, const ComplexColumnVector &y)
Definition: CColVector.h:158
ComplexMatrix xgemm(const ComplexMatrix &a, const ComplexMatrix &b, blas_trans_type transa, blas_trans_type transb)
Definition: CMatrix.cc:3346
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:7516
ComplexMatrix trans_mul(const SparseComplexMatrix &m, const ComplexMatrix &a)
Definition: CSparse.cc:7546
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
MatrixType matrix_type() const
Definition: ov-base-mat.h:139
Matrix matrix_value(bool=false) const
Definition: ov-ch-mat.h:114
NDArray array_value(bool=false) const
Definition: ov-ch-mat.h:120
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988
@ blas_no_trans
Definition: mx-defs.h:81
@ blas_trans
Definition: mx-defs.h:82
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:167
#define DEFNDASSIGNOP_FN(name, t1, t2, e, f)
Definition: ops.h:127
#define DEFNDBINOP_OP(name, t1, t2, e1, e2, op)
Definition: ops.h:291
#define INSTALL_NCUNOP_TI(ti, op, t, f)
Definition: ops.h:66
#define DEFNULLASSIGNOP_FN(name, t, f)
Definition: ops.h:115
#define DEFNDBINOP_FN(name, t1, t2, e1, e2, f)
Definition: ops.h:316
#define DEFBINOPX(name, t1, t2)
Definition: ops.h:240
#define DEFNDASSIGNOP_FNOP(name, t1, t2, f, fnop)
Definition: ops.h:156
#define DEFBINOP(name, t1, t2)
Definition: ops.h:245
#define DEFNDUNOP_OP(name, t, e, op)
Definition: ops.h:206
#define DEFBINOP_OP(name, t1, t2, op)
Definition: ops.h:250
#define OCTAVE_CAST_BASE_VALUE(T, T_VAL, BASE_VAL)
Definition: ops.h:52
#define DEFNCUNOP_METHOD(name, t, method)
Definition: ops.h:232
#define INSTALL_BINOP_TI(ti, op, t1, t2, f)
Definition: ops.h:70
#define DEFNDCATOP_FN(name, t1, t2, e1, e2, f)
Definition: ops.h:364
#define DEFUNOP(name, t)
Definition: ops.h:194
#define INSTALL_CATOP_TI(ti, t1, t2, f)
Definition: ops.h:75
#define INSTALL_UNOP_TI(ti, op, t, f)
Definition: ops.h:62
#define DEFNDASSIGNOP_OP(name, t1, t2, f, op)
Definition: ops.h:141
#define INSTALL_ASSIGNOP_TI(ti, op, t1, t2, f)
Definition: ops.h:79
octave_value op_ne(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1676
octave_value op_el_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1681
octave_value op_uplus(const octave_value &a)
Definition: ov.h:1634
octave_value op_add(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1663
octave_value op_gt(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1675
octave_value op_uminus(const octave_value &a)
Definition: ov.h:1635
octave_value op_el_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1679
octave_value op_mul_herm(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1697
octave_value op_transpose(const octave_value &a)
Definition: ov.h:1637
octave_value op_el_or(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1683
octave_value op_sub(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1664
octave_value op_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1665
octave_value op_hermitian(const octave_value &a)
Definition: ov.h:1638
octave_value op_le(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1672
octave_value op_el_and(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1682
octave_value op_herm_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1696
octave_value op_eq(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1673
octave_value op_el_pow(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1680
octave_value op_not(const octave_value &a)
Definition: ov.h:1633
octave_value op_ldiv(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1669
octave_value op_pow(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1668
octave_value op_ge(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1674
octave_value op_el_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1678
octave_value op_div(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1666
octave_value op_trans_mul(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1694
octave_value op_mul_trans(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1695
octave_value op_lt(const octave_value &a1, const octave_value &a2)
Definition: ov.h:1671
Matrix xdiv(const Matrix &a, const SparseMatrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:137
Matrix xleftdiv(const SparseMatrix &a, const Matrix &b, MatrixType &typ)
Definition: sparse-xdiv.cc:469
octave_value elem_xpow(double a, const SparseMatrix &b)
Definition: sparse-xpow.cc:347