GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-cbinop.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-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 "interpreter.h"
31 #include "ov.h"
32 #include "pt-cbinop.h"
33 #include "pt-eval.h"
34 #include "pt-unop.h"
35 
37 
40 {
41  octave_value val;
42 
43  if (m_lhs)
44  {
45  // Evaluate with unknown number of output arguments
46  octave_value a = m_lhs->evaluate (tw, -1);
47 
48  if (a.is_defined () && m_rhs)
49  {
50  // Evaluate with unknown number of output arguments
51  octave_value b = m_rhs->evaluate (tw, -1);
52 
53  if (b.is_defined ())
54  {
55  interpreter& interp = tw.get_interpreter ();
56 
57  type_info& ti = interp.get_type_info ();
58 
59  val = binary_op (ti, m_etype, a, b);
60  }
61  }
62  }
63 
64  return val;
65 }
66 
68 
69 // If a tree expression is a transpose or hermitian transpose, return
70 // the argument and corresponding operator.
71 
73 strip_trans_herm (tree_expression_ptr_t& exp)
74 {
75  if (exp->is_unary_expression ())
76  {
78  = dynamic_cast<tree_unary_expression *> (exp);
79 
80  octave_value::unary_op op = uexp->op_type ();
81 
84  exp = uexp->operand ();
85  else
87 
88  return op;
89  }
90  else
92 }
93 
94 // Possibly convert multiplication to trans_mul, mul_trans, herm_mul,
95 // or mul_herm.
96 
98 simplify_mul_op (tree_expression_ptr_t& a, tree_expression_ptr_t& b)
99 {
102 
103  octave_value::unary_op opa = strip_trans_herm (a);
104 
105  if (opa == octave_value::op_hermitian)
107  else if (opa == octave_value::op_transpose)
109  else
110  {
111  octave_value::unary_op opb = strip_trans_herm (b);
112 
113  if (opb == octave_value::op_hermitian)
115  else if (opb == octave_value::op_transpose)
117  }
118 
119  return retop;
120 }
121 
122 // Possibly convert left division to trans_ldiv or herm_ldiv.
123 
125 simplify_ldiv_op (tree_expression_ptr_t& a, tree_expression_ptr_t&)
126 {
129 
130  octave_value::unary_op opa = strip_trans_herm (a);
131 
132  if (opa == octave_value::op_hermitian)
134  else if (opa == octave_value::op_transpose)
136 
137  return retop;
138 }
139 
140 // Possibly contract and/or with negation.
141 
144  int l, int c, octave_value::binary_op t)
145 {
146  tree_expression *ca = a;
147  tree_expression *cb = b;
149 
150  switch (t)
151  {
153  ct = simplify_mul_op (ca, cb);
154  break;
155 
157  ct = simplify_ldiv_op (ca, cb);
158  break;
159 
160  default:
162  break;
163  }
164 
167  ? new tree_binary_expression (a, b, l, c, t)
168  : new tree_compound_binary_expression (a, b, l, c, t, ca, cb, ct));
169 
170  return ret;
171 }
172 
173 OCTAVE_END_NAMESPACE(octave)
type_info & get_type_info()
Definition: interpreter.h:293
unary_op
Definition: ov.h:79
@ op_hermitian
Definition: ov.h:84
@ op_transpose
Definition: ov.h:83
@ unknown_unary_op
Definition: ov.h:88
compound_binary_op
Definition: ov.h:117
@ op_herm_ldiv
Definition: ov.h:124
@ op_mul_trans
Definition: ov.h:120
@ op_mul_herm
Definition: ov.h:122
@ op_trans_ldiv
Definition: ov.h:123
@ unknown_compound_binary_op
Definition: ov.h:130
@ op_herm_mul
Definition: ov.h:121
@ op_trans_mul
Definition: ov.h:119
bool is_defined() const
Definition: ov.h:592
binary_op
Definition: ov.h:92
@ op_ldiv
Definition: ov.h:98
@ op_mul
Definition: ov.h:95
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-cbinop.cc:39
interpreter & get_interpreter()
Definition: pt-eval.h:420
virtual octave_value evaluate(tree_evaluator &tw, int nargout=1)=0
virtual bool is_unary_expression() const
Definition: pt-exp.h:74
tree_expression * operand()
Definition: pt-unop.h:68
octave_value::unary_op op_type() const
Definition: pt-unop.h:72
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
octave_value binary_op(type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)
tree_binary_expression * maybe_compound_binary_expression(tree_expression *a, tree_expression *b, int l, int c, octave_value::binary_op t)
Definition: pt-cbinop.cc:143
tree_expression * tree_expression_ptr_t
Definition: pt-cbinop.cc:67