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 "Array-util.h"
31 
32 #include "ovl.h"
33 #include "ov.h"
34 #include "ov-scalar.h"
35 #include "ov-float.h"
36 #include "ov-re-mat.h"
37 #include "ov-flt-re-mat.h"
38 #include "ov-typeinfo.h"
39 #include "ov-null-mat.h"
40 #include "ops.h"
41 #include "xdiv.h"
42 #include "xpow.h"
43 
44 // scalar unary ops.
45 
DEFUNOP(not,scalar)46 DEFUNOP (not, scalar)
47 {
48   const octave_scalar& v = dynamic_cast<const octave_scalar&> (a);
49   double x = v.scalar_value ();
50   if (octave::math::isnan (x))
51     octave::err_nan_to_logical_conversion ();
52 
53   return octave_value (x == 0.0);
54 }
55 
56 DEFUNOP_OP (uplus, scalar, /* no-op */)
57 DEFUNOP_OP (uminus, scalar, -)
58 DEFUNOP_OP (transpose, scalar, /* no-op */)
59 DEFUNOP_OP (hermitian, scalar, /* no-op */)
60 
DEFNCUNOP_METHOD(incr,scalar,increment)61 DEFNCUNOP_METHOD (incr, scalar, increment)
62 DEFNCUNOP_METHOD (decr, scalar, decrement)
63 
64 // scalar by scalar ops.
65 
66 DEFBINOP_OP (add, scalar, scalar, +)
67 DEFBINOP_OP (sub, scalar, scalar, -)
68 DEFBINOP_OP (mul, scalar, scalar, *)
69 
70 DEFBINOP (div, scalar, scalar)
71 {
72   const octave_scalar& v1 = dynamic_cast<const octave_scalar&> (a1);
73   const octave_scalar& v2 = dynamic_cast<const octave_scalar&> (a2);
74 
75   return octave_value (v1.double_value () / v2.double_value ());
76 }
77 
DEFBINOP_FN(pow,scalar,scalar,xpow)78 DEFBINOP_FN (pow, scalar, scalar, xpow)
79 
80 DEFBINOP (ldiv, scalar, scalar)
81 {
82   const octave_scalar& v1 = dynamic_cast<const octave_scalar&> (a1);
83   const octave_scalar& v2 = dynamic_cast<const octave_scalar&> (a2);
84 
85   return octave_value (v2.double_value () / v1.double_value ());
86 }
87 
88 DEFBINOP_OP (lt, scalar, scalar, <)
89 DEFBINOP_OP (le, scalar, scalar, <=)
90 DEFBINOP_OP (eq, scalar, scalar, ==)
91 DEFBINOP_OP (ge, scalar, scalar, >=)
92 DEFBINOP_OP (gt, scalar, scalar, >)
93 DEFBINOP_OP (ne, scalar, scalar, !=)
94 
95 DEFBINOP_OP (el_mul, scalar, scalar, *)
96 
DEFBINOP(el_div,scalar,scalar)97 DEFBINOP (el_div, scalar, scalar)
98 {
99   const octave_scalar& v1 = dynamic_cast<const octave_scalar&> (a1);
100   const octave_scalar& v2 = dynamic_cast<const octave_scalar&> (a2);
101 
102   return octave_value (v1.double_value () / v2.double_value ());
103 }
104 
DEFBINOP_FN(el_pow,scalar,scalar,xpow)105 DEFBINOP_FN (el_pow, scalar, scalar, xpow)
106 
107 DEFBINOP (el_ldiv, scalar, scalar)
108 {
109   const octave_scalar& v1 = dynamic_cast<const octave_scalar&> (a1);
110   const octave_scalar& v2 = dynamic_cast<const octave_scalar&> (a2);
111 
112   return octave_value (v2.double_value () / v1.double_value ());
113 }
114 
115 DEFSCALARBOOLOP_OP (el_and, scalar, scalar, &&)
116 DEFSCALARBOOLOP_OP (el_or, scalar, scalar, ||)
117 
DEFNDCATOP_FN(s_s,scalar,scalar,array,array,concat)118 DEFNDCATOP_FN (s_s, scalar, scalar, array, array, concat)
119 
120 void
121 install_s_s_ops (octave::type_info& ti)
122 {
123   INSTALL_UNOP_TI (ti, op_not, octave_scalar, not);
124   INSTALL_UNOP_TI (ti, op_uplus, octave_scalar, uplus);
125   INSTALL_UNOP_TI (ti, op_uminus, octave_scalar, uminus);
126   INSTALL_UNOP_TI (ti, op_transpose, octave_scalar, transpose);
127   INSTALL_UNOP_TI (ti, op_hermitian, octave_scalar, hermitian);
128 
129   INSTALL_NCUNOP_TI (ti, op_incr, octave_scalar, incr);
130   INSTALL_NCUNOP_TI (ti, op_decr, octave_scalar, decr);
131 
132   INSTALL_BINOP_TI (ti, op_add, octave_scalar, octave_scalar, add);
133   INSTALL_BINOP_TI (ti, op_sub, octave_scalar, octave_scalar, sub);
134   INSTALL_BINOP_TI (ti, op_mul, octave_scalar, octave_scalar, mul);
135   INSTALL_BINOP_TI (ti, op_div, octave_scalar, octave_scalar, div);
136   INSTALL_BINOP_TI (ti, op_pow, octave_scalar, octave_scalar, pow);
137   INSTALL_BINOP_TI (ti, op_ldiv, octave_scalar, octave_scalar, ldiv);
138   INSTALL_BINOP_TI (ti, op_lt, octave_scalar, octave_scalar, lt);
139   INSTALL_BINOP_TI (ti, op_le, octave_scalar, octave_scalar, le);
140   INSTALL_BINOP_TI (ti, op_eq, octave_scalar, octave_scalar, eq);
141   INSTALL_BINOP_TI (ti, op_ge, octave_scalar, octave_scalar, ge);
142   INSTALL_BINOP_TI (ti, op_gt, octave_scalar, octave_scalar, gt);
143   INSTALL_BINOP_TI (ti, op_ne, octave_scalar, octave_scalar, ne);
144   INSTALL_BINOP_TI (ti, op_el_mul, octave_scalar, octave_scalar, el_mul);
145   INSTALL_BINOP_TI (ti, op_el_div, octave_scalar, octave_scalar, el_div);
146   INSTALL_BINOP_TI (ti, op_el_pow, octave_scalar, octave_scalar, el_pow);
147   INSTALL_BINOP_TI (ti, op_el_ldiv, octave_scalar, octave_scalar, el_ldiv);
148   INSTALL_BINOP_TI (ti, op_el_and, octave_scalar, octave_scalar, el_and);
149   INSTALL_BINOP_TI (ti, op_el_or, octave_scalar, octave_scalar, el_or);
150 
151   INSTALL_CATOP_TI (ti, octave_scalar, octave_scalar, s_s);
152 
153   INSTALL_ASSIGNCONV_TI (ti, octave_scalar, octave_scalar, octave_matrix);
154   INSTALL_ASSIGNCONV_TI (ti, octave_float_scalar, octave_scalar,
155                          octave_float_matrix);
156 
157   INSTALL_ASSIGNCONV_TI (ti, octave_scalar, octave_null_matrix, octave_matrix);
158   INSTALL_ASSIGNCONV_TI (ti, octave_scalar, octave_null_str, octave_matrix);
159   INSTALL_ASSIGNCONV_TI (ti, octave_scalar, octave_null_sq_str, octave_matrix);
160 }
161