GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
lo-mappers.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 John W. Eaton
4 Copyright (C) 2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include "lo-mappers.h"
29 #include "lo-specfun.h"
30 #include "math-wrappers.h"
31 
32 // FIXME: We used to have this situation:
33 //
34 // Functions that forward to gnulib belong here so we can keep
35 // gnulib:: out of lo-mappers.h.
36 //
37 // but now we just use std:: and explicit wrappers in C++ code so maybe
38 // some of the forwarding functions can be defined inline here.
39 
40 namespace octave
41 {
42  namespace math
43  {
44  bool
45  isna (double x)
46  {
47  return lo_ieee_is_NA (x);
48  }
49 
50  bool
51  isna (const Complex& x)
52  {
53  return (isna (std::real (x)) || isna (std::imag (x)));
54  }
55 
56  bool
57  isna (float x)
58  {
59  return lo_ieee_is_NA (x);
60  }
61 
62  bool
63  isna (const FloatComplex& x)
64  {
65  return (isna (std::real (x)) || isna (std::imag (x)));
66  }
67 
68  bool
70  {
71  return (isnan (std::real (x)) || isnan (std::imag (x)));
72  }
73 
74  bool
76  {
77  return (isnan (std::real (x)) || isnan (std::imag (x)));
78  }
79 
80  // Matlab returns a different phase for acos, asin then std library
81  // which requires a small function to remap the phase.
82  Complex
83  acos (const Complex& x)
84  {
85  Complex y = std::acos (x);
86 
87  if (std::imag (x) == 0.0 && std::real (x) > 1.0)
88  return std::conj (y);
89  else
90  return y;
91  }
92 
94  acos (const FloatComplex& x)
95  {
97 
98  if (std::imag (x) == 0.0f && std::real (x) > 1.0f)
99  return std::conj (y);
100  else
101  return y;
102  }
103 
104  Complex
105  asin (const Complex& x)
106  {
107  Complex y = std::asin (x);
108 
109  if (std::imag (x) == 0.0 && std::real (x) > 1.0)
110  return std::conj (y);
111  else
112  return y;
113  }
114 
116  asin (const FloatComplex& x)
117  {
119 
120  if (std::imag (x) == 0.0f && std::real (x) > 1.0f)
121  return std::conj (y);
122  else
123  return y;
124  }
125 
126  double frexp (double x, int *expptr)
127  {
128  return octave_frexp_wrapper (x, expptr);
129  }
130 
131  float frexp (float x, int *expptr)
132  {
133  return octave_frexpf_wrapper (x, expptr);
134  }
135 
136  Complex
137  log2 (const Complex& x)
138  {
139  return std::log (x) / M_LN2;
140  }
141 
143  log2 (const FloatComplex& x)
144  {
145  return std::log (x) / static_cast<float> (M_LN2);
146  }
147 
148  double
149  log2 (double x, int& exp)
150  {
151  return frexp (x, &exp);
152  }
153 
154  float
155  log2 (float x, int& exp)
156  {
157  return frexp (x, &exp);
158  }
159 
160  Complex
161  log2 (const Complex& x, int& exp)
162  {
163  double ax = std::abs (x);
164  double lax = log2 (ax, exp);
165  return (ax != lax) ? (x / ax) * lax : x;
166  }
167 
169  log2 (const FloatComplex& x, int& exp)
170  {
171  float ax = std::abs (x);
172  float lax = log2 (ax, exp);
173  return (ax != lax) ? (x / ax) * lax : x;
174  }
175 
176  bool negative_sign (double x) { return __lo_ieee_signbit (x); }
177  bool negative_sign (float x) { return __lo_ieee_float_signbit (x); }
178 
179  // Sometimes you need a large integer, but not always.
180 
182  nint_big (double x)
183  {
188  else
189  return static_cast<octave_idx_type> ((x > 0.0) ? (x + 0.5)
190  : (x - 0.5));
191  }
192 
194  nint_big (float x)
195  {
200  else
201  return static_cast<octave_idx_type> ((x > 0.0f) ? (x + 0.5f)
202  : (x - 0.5f));
203  }
204 
205  int
206  nint (double x)
207  {
210  else if (x < std::numeric_limits<int>::min ())
212  else
213  return static_cast<int> ((x > 0.0) ? (x + 0.5) : (x - 0.5));
214  }
215 
216  int
217  nint (float x)
218  {
221  else if (x < std::numeric_limits<int>::min ())
223  else
224  return static_cast<int> ((x > 0.0f) ? (x + 0.5f) : (x - 0.5f));
225  }
226 
227  Complex
228  rc_acos (double x)
229  {
230  return fabs (x) > 1.0 ? acos (Complex (x)) : Complex (std::acos (x));
231  }
232 
234  rc_acos (float x)
235  {
236  return fabsf (x) > 1.0f ? acos (FloatComplex (x))
237  : FloatComplex (std::acos (x));
238  }
239 
240  Complex
241  rc_acosh (double x)
242  {
243  return x < 1.0 ? acosh (Complex (x)) : Complex (acosh (x));
244  }
245 
247  rc_acosh (float x)
248  {
249  return x < 1.0f ? acosh (FloatComplex (x)) : FloatComplex (acosh (x));
250  }
251 
252  Complex
253  rc_asin (double x)
254  {
255  return fabs (x) > 1.0 ? asin (Complex (x)) : Complex (std::asin (x));
256  }
257 
259  rc_asin (float x)
260  {
261  return fabsf (x) > 1.0f ? asin (FloatComplex (x))
262  : FloatComplex (::asinf (x));
263  }
264 
265  Complex
266  rc_atanh (double x)
267  {
268  return fabs (x) > 1.0 ? atanh (Complex (x)) : Complex (atanh (x));
269  }
270 
272  rc_atanh (float x)
273  {
274  return fabsf (x) > 1.0f ? atanh (FloatComplex (x))
275  : FloatComplex (atanh (x));
276  }
277 
278  Complex
279  rc_log (double x)
280  {
281  return x < 0.0 ? Complex (std::log (-x), M_PI) : Complex (std::log (x));
282  }
283 
285  rc_log (float x)
286  {
287  return x < 0.0f ? FloatComplex (std::log (-x), static_cast<float> (M_PI))
288  : FloatComplex (std::log (x));
289  }
290 
291  Complex
292  rc_log2 (double x)
293  {
294  constexpr double PI_LN2 = 4.53236014182719380962; // = pi / log(2)
295  return x < 0.0 ? Complex (log2 (-x), PI_LN2) : Complex (log2 (x));
296  }
297 
299  rc_log2 (float x)
300  {
301  constexpr float PI_LN2 = 4.53236014182719380962f; // = pi / log(2)
302  return x < 0.0f ? FloatComplex (log2 (-x), PI_LN2)
303  : FloatComplex (log2 (x));
304  }
305 
306  Complex
307  rc_log10 (double x)
308  {
309  constexpr double PI_LN10 = 1.36437635384184134748; // = pi / log(10)
310  return x < 0.0 ? Complex (log10 (-x), PI_LN10) : Complex (log10 (x));
311  }
312 
314  rc_log10 (float x)
315  {
316  constexpr float PI_LN10 = 1.36437635384184134748f; // = pi / log(10)
317  return x < 0.0f ? FloatComplex (log10 (-x), PI_LN10)
318  : FloatComplex (log10f (x));
319  }
320 
321  Complex
322  rc_sqrt (double x)
323  {
324  return x < 0.0 ? Complex (0.0, std::sqrt (-x)) : Complex (std::sqrt (x));
325  }
326 
328  rc_sqrt (float x)
329  {
330  return x < 0.0f ? FloatComplex (0.0f, std::sqrt (-x))
331  : FloatComplex (std::sqrt (x));
332  }
333  }
334 }
#define lo_ieee_is_NA(x)
Definition: lo-ieee.h:117
Complex rc_log10(double x)
Definition: lo-mappers.cc:307
FloatComplex asin(const FloatComplex &x)
Definition: lo-mappers.cc:116
Complex rc_acosh(double x)
Definition: lo-mappers.cc:241
Complex rc_sqrt(double x)
Definition: lo-mappers.cc:322
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
bool isna(double x)
Definition: lo-mappers.cc:45
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the base of natural logarithms The constant ex $e satisfies the equation log(e)
bool isnan(bool)
Definition: lo-mappers.h:187
static T abs(T x)
Definition: pr-output.cc:1696
Complex acos(const Complex &x)
Definition: lo-mappers.cc:83
int __lo_ieee_signbit(double x)
Definition: lo-ieee.h:87
Complex asin(const Complex &x)
Definition: lo-mappers.cc:105
double frexp(double x, int *expptr)
Definition: lo-mappers.cc:126
Complex log2(const Complex &x)
Definition: lo-mappers.cc:137
Complex rc_atanh(double x)
Definition: lo-mappers.cc:266
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:215
float octave_frexpf_wrapper(float x, int *expptr)
Definition: math-wrappers.c:43
double acosh(double x)
Definition: lo-specfun.h:49
double octave_frexp_wrapper(double x, int *expptr)
Definition: math-wrappers.c:37
Complex rc_log2(double x)
Definition: lo-mappers.cc:292
Complex rc_log(double x)
Definition: lo-mappers.cc:279
double atanh(double x)
Definition: lo-specfun.h:72
FloatComplex acos(const FloatComplex &x)
Definition: lo-mappers.cc:94
Complex rc_acos(double x)
Definition: lo-mappers.cc:228
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:227
bool is_NaN_or_NA(const Complex &x)
Definition: lo-mappers.cc:69
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:182
bool negative_sign(double x)
Definition: lo-mappers.cc:176
Complex rc_asin(double x)
Definition: lo-mappers.cc:253
the element is set to zero In other the statement xample y
Definition: data.cc:5264
int __lo_ieee_float_signbit(float x)
Definition: lo-ieee.h:99
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:141
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
std::complex< double > Complex
Definition: oct-cmplx.h:31
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:135
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:204
int nint(double x)
Definition: lo-mappers.cc:206