GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pt-cbinop.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2008-2015 Jaroslav Hajek
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "error.h"
28 #include "oct-obj.h"
29 #include "ov.h"
30 #include "pt-cbinop.h"
31 #include "pt-bp.h"
32 #include "pt-unop.h"
33 #include "pt-walk.h"
34 
37 {
38  octave_value_list retval;
39 
40  if (nargout > 1)
41  error ("binary operator '%s': invalid number of output arguments",
42  oper () . c_str ());
43  else
44  retval = rvalue1 (nargout);
45 
46  return retval;
47 }
48 
51 {
52  octave_value retval;
53 
54  if (error_state)
55  return retval;
56 
57  if (op_lhs)
58  {
59  octave_value a = op_lhs->rvalue1 ();
60 
61  if (! error_state && a.is_defined () && op_rhs)
62  {
63  octave_value b = op_rhs->rvalue1 ();
64 
65  if (! error_state && b.is_defined ())
66  {
67  retval = ::do_binary_op (etype, a, b);
68 
69  if (error_state)
70  retval = octave_value ();
71  }
72  }
73  }
74 
75  return retval;
76 }
77 
78 // If a tree expression is a transpose or hermitian transpose, return
79 // the argument and corresponding operator.
80 
83 {
84  if (exp->is_unary_expression ())
85  {
86  tree_unary_expression *uexp =
87  dynamic_cast<tree_unary_expression *> (exp);
88 
89  octave_value::unary_op op = uexp->op_type ();
90 
93  exp = uexp->operand ();
94  else
96 
97  return op;
98  }
99  else
101 }
102 
105 {
106  if (exp->is_unary_expression ())
107  {
108  tree_unary_expression *uexp =
109  dynamic_cast<tree_unary_expression *> (exp);
110 
111  octave_value::unary_op op = uexp->op_type ();
112 
113  if (op == octave_value::op_not)
114  exp = uexp->operand ();
115  else
117 
118  return op;
119  }
120  else
122 }
123 
124 // Possibly convert multiplication to trans_mul, mul_trans, herm_mul,
125 // or mul_herm.
126 
129 {
132 
134 
135  if (opa == octave_value::op_hermitian)
137  else if (opa == octave_value::op_transpose)
139  else
140  {
142 
143  if (opb == octave_value::op_hermitian)
145  else if (opb == octave_value::op_transpose)
147  }
148 
149  return retop;
150 }
151 
152 // Possibly convert left division to trans_ldiv or herm_ldiv.
153 
156 {
159 
161 
162  if (opa == octave_value::op_hermitian)
164  else if (opa == octave_value::op_transpose)
166 
167  return retop;
168 }
169 
170 // Possibly contract and/or with negation.
171 
175 {
178 
180 
181  if (opa == octave_value::op_not)
182  {
183  if (op == octave_value::op_el_and)
185  else if (op == octave_value::op_el_or)
187  }
188  else
189  {
191 
192  if (opb == octave_value::op_not)
193  {
194  if (op == octave_value::op_el_and)
196  else if (op == octave_value::op_el_or)
198  }
199  }
200 
201  return retop;
202 }
203 
206  int l, int c, octave_value::binary_op t)
207 {
208  tree_expression *ca = a;
209  tree_expression *cb = b;
211 
212  switch (t)
213  {
215  ct = simplify_mul_op (ca, cb);
216  break;
217 
219  ct = simplify_ldiv_op (ca, cb);
220  break;
221 
224  ct = simplify_and_or_op (ca, cb, t);
225  break;
226 
227  default:
229  break;
230  }
231 
233  ? new tree_binary_expression (a, b, l, c, t)
234  : new tree_compound_binary_expression (a, b, l,
235  c, t, ca,
236  cb, ct);
237 
238  return ret;
239 }
tree_expression * operand(void)
Definition: pt-unop.h:60
octave_value::unary_op op_type(void) const
Definition: pt-unop.h:64
bool is_defined(void) const
Definition: ov.h:520
binary_op
Definition: ov.h:87
tree_expression * op_lhs
Definition: pt-cbinop.h:63
void error(const char *fmt,...)
Definition: error.cc:476
octave_value_list rvalue(int nargout)
Definition: pt-cbinop.cc:36
octave_value::compound_binary_op etype
Definition: pt-cbinop.h:65
std::string oper(void) const
Definition: pt-binop.cc:160
static octave_value::compound_binary_op simplify_mul_op(tree_expression *&a, tree_expression *&b)
Definition: pt-cbinop.cc:128
static octave_value::unary_op strip_trans_herm(tree_expression *&exp)
Definition: pt-cbinop.cc:82
static octave_value::compound_binary_op simplify_and_or_op(tree_expression *&a, tree_expression *&b, octave_value::binary_op op)
Definition: pt-cbinop.cc:173
int error_state
Definition: error.cc:101
octave_value rvalue1(int nargout=1)
Definition: pt-cbinop.cc:50
static octave_value::unary_op strip_not(tree_expression *&exp)
Definition: pt-cbinop.cc:104
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:205
virtual octave_value rvalue1(int nargout=1)
Definition: pt-exp.cc:58
static octave_value::compound_binary_op simplify_ldiv_op(tree_expression *&a, tree_expression *&)
Definition: pt-cbinop.cc:155
virtual bool is_unary_expression(void) const
Definition: pt-exp.h:67
tree_expression * op_rhs
Definition: pt-cbinop.h:64
compound_binary_op
Definition: ov.h:114
unary_op
Definition: ov.h:74
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
octave_value do_binary_op(octave_value::binary_op op, const octave_value &v1, const octave_value &v2)
Definition: ov.cc:1978