GNU Octave  8.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-2023 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  octave_value a = m_lhs->evaluate (tw);
46 
47  if (a.is_defined () && m_rhs)
48  {
49  octave_value b = m_rhs->evaluate (tw);
50 
51  if (b.is_defined ())
52  {
53  interpreter& interp = tw.get_interpreter ();
54 
55  type_info& ti = interp.get_type_info ();
56 
57  val = binary_op (ti, m_etype, a, b);
58  }
59  }
60  }
61 
62  return val;
63 }
64 
66 
67 // If a tree expression is a transpose or hermitian transpose, return
68 // the argument and corresponding operator.
69 
72 {
73  if (exp->is_unary_expression ())
74  {
76  = dynamic_cast<tree_unary_expression *> (exp);
77 
78  octave_value::unary_op op = uexp->op_type ();
79 
82  exp = uexp->operand ();
83  else
85 
86  return op;
87  }
88  else
90 }
91 
92 #if 0
93 // Restore this code if short-circuit behavior can be preserved when needed.
94 // See bug #54465.
95 
97 strip_not (tree_expression_ptr_t& exp)
98 {
99  if (exp->is_unary_expression ())
100  {
102  = dynamic_cast<tree_unary_expression *> (exp);
103 
104  octave_value::unary_op op = uexp->op_type ();
105 
106  if (op == octave_value::op_not)
107  exp = uexp->operand ();
108  else
110 
111  return op;
112  }
113  else
115 }
116 #endif
117 
118 // Possibly convert multiplication to trans_mul, mul_trans, herm_mul,
119 // or mul_herm.
120 
123 {
126 
128 
129  if (opa == octave_value::op_hermitian)
131  else if (opa == octave_value::op_transpose)
133  else
134  {
136 
137  if (opb == octave_value::op_hermitian)
139  else if (opb == octave_value::op_transpose)
141  }
142 
143  return retop;
144 }
145 
146 // Possibly convert left division to trans_ldiv or herm_ldiv.
147 
150 {
153 
155 
156  if (opa == octave_value::op_hermitian)
158  else if (opa == octave_value::op_transpose)
160 
161  return retop;
162 }
163 
164 // Possibly contract and/or with negation.
165 
166 #if 0
167 // Restore this code if short-circuit behavior can be preserved when needed.
168 // See bug #54465.
170 simplify_and_or_op (tree_expression_ptr_t& a, tree_expression_ptr_t& b,
172 {
175 
176  octave_value::unary_op opa = strip_not (a);
177 
178  if (opa == octave_value::op_not)
179  {
180  if (op == octave_value::op_el_and)
182  else if (op == octave_value::op_el_or)
184  }
185  else
186  {
187  octave_value::unary_op opb = strip_not (b);
188 
189  if (opb == octave_value::op_not)
190  {
191  if (op == octave_value::op_el_and)
193  else if (op == octave_value::op_el_or)
195  }
196  }
197 
198  return retop;
199 }
200 #endif
201 
204  int l, int c, octave_value::binary_op t)
205 {
206  tree_expression *ca = a;
207  tree_expression *cb = b;
209 
210  switch (t)
211  {
213  ct = simplify_mul_op (ca, cb);
214  break;
215 
217  ct = simplify_ldiv_op (ca, cb);
218  break;
219 
220 #if 0
221  // Restore this case if short-circuit behavior can be preserved
222  // when needed. See bug #54465.
225  ct = simplify_and_or_op (ca, cb, t);
226  break;
227 #endif
228 
229  default:
231  break;
232  }
233 
236  ? new tree_binary_expression (a, b, l, c, t)
237  : new tree_compound_binary_expression (a, b, l, c, t, ca, cb, ct));
238 
239  return ret;
240 }
241 
OCTAVE_END_NAMESPACE(octave)
type_info & get_type_info(void)
Definition: interpreter.h:293
unary_op
Definition: ov.h:77
@ op_hermitian
Definition: ov.h:82
@ op_not
Definition: ov.h:78
@ op_transpose
Definition: ov.h:81
@ unknown_unary_op
Definition: ov.h:86
compound_binary_op
Definition: ov.h:115
@ op_herm_ldiv
Definition: ov.h:122
@ op_el_not_or
Definition: ov.h:124
@ op_el_not_and
Definition: ov.h:123
@ op_el_and_not
Definition: ov.h:125
@ op_mul_trans
Definition: ov.h:118
@ op_mul_herm
Definition: ov.h:120
@ op_trans_ldiv
Definition: ov.h:121
@ unknown_compound_binary_op
Definition: ov.h:128
@ op_el_or_not
Definition: ov.h:126
@ op_herm_mul
Definition: ov.h:119
@ op_trans_mul
Definition: ov.h:117
bool is_defined(void) const
Definition: ov.h:637
binary_op
Definition: ov.h:90
@ op_ldiv
Definition: ov.h:96
@ op_el_or
Definition: ov.h:108
@ op_mul
Definition: ov.h:93
@ op_el_and
Definition: ov.h:107
tree_expression * m_rhs
Definition: pt-cbinop.h:79
octave_value::compound_binary_op m_etype
Definition: pt-cbinop.h:81
octave_value evaluate(tree_evaluator &, int nargout=1)
Definition: pt-cbinop.cc:39
tree_expression * m_lhs
Definition: pt-cbinop.h:78
interpreter & get_interpreter(void)
Definition: pt-eval.h:427
virtual octave_value evaluate(tree_evaluator &tw, int nargout=1)=0
virtual bool is_unary_expression(void) const
Definition: pt-exp.h:78
tree_expression * operand(void)
Definition: pt-unop.h:72
octave_value::unary_op op_type(void) const
Definition: pt-unop.h:76
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
OCTINTERP_API octave_value binary_op(type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)
static octave_value::compound_binary_op simplify_ldiv_op(tree_expression_ptr_t &a, tree_expression_ptr_t &)
Definition: pt-cbinop.cc:149
static octave_value::compound_binary_op simplify_mul_op(tree_expression_ptr_t &a, tree_expression_ptr_t &b)
Definition: pt-cbinop.cc:122
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:203
static octave_value::unary_op strip_trans_herm(tree_expression_ptr_t &exp)
Definition: pt-cbinop.cc:71
tree_expression * tree_expression_ptr_t
Definition: pt-cbinop.cc:65