GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
mappers.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2026 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 "mappers.h"
31#include "math-wrappers.h"
32#include "oct-specfun.h"
33
34// FIXME: We used to have this situation:
35//
36// Functions that forward to gnulib belong here so we can keep
37// gnulib:: out of mappers.h.
38//
39// but now we just use std:: and explicit wrappers in C++ code so maybe
40// some of the forwarding functions can be defined inline here.
41
44
45bool
46isna (double x)
47{
48 return lo_ieee_is_NA (x);
49}
50
51bool
52isna (const Complex& x)
53{
54 return (isna (std::real (x)) || isna (std::imag (x)));
55}
56
57bool
58isna (float x)
59{
60 return lo_ieee_is_NA (x);
61}
62
63bool
65{
66 return (isna (std::real (x)) || isna (std::imag (x)));
67}
68
69bool
71{
72 return (isnan (std::real (x)) || isnan (std::imag (x)));
73}
74
75bool
77{
78 return (isnan (std::real (x)) || isnan (std::imag (x)));
79}
80
81// Matlab returns a different phase for acos, asin then std library
82// which requires a small function to remap the phase.
84acos (const Complex& x)
85{
86 Complex y = std::acos (x);
87
88 if (std::imag (x) == 0.0 && std::real (x) > 1.0)
89 return std::conj (y);
90 else
91 return y;
92}
93
96{
97 FloatComplex y = std::acos (x);
98
99 if (std::imag (x) == 0.0f && std::real (x) > 1.0f)
100 return std::conj (y);
101 else
102 return y;
103}
104
106asin (const Complex& x)
107{
108 Complex y = std::asin (x);
109
110 if (std::imag (x) == 0.0 && std::real (x) > 1.0)
111 return std::conj (y);
112 else
113 return y;
114}
115
118{
119 FloatComplex y = std::asin (x);
120
121 if (std::imag (x) == 0.0f && std::real (x) > 1.0f)
122 return std::conj (y);
123 else
124 return y;
125}
126
127double
128frexp (double x, int *expptr)
129{
130 return octave_frexp_wrapper (x, expptr);
131}
132
133float
134frexp (float x, int *expptr)
135{
136 return octave_frexpf_wrapper (x, expptr);
137}
138
140log2 (const Complex& x)
141{
142 return std::log (x) / M_LN2;
143}
144
147{
148 return std::log (x) / static_cast<float> (M_LN2);
149}
150
151double
152log2 (double x, int& exp)
153{
154 return frexp (x, &exp);
155}
156
157float
158log2 (float x, int& exp)
159{
160 return frexp (x, &exp);
161}
162
164log2 (const Complex& x, int& exp)
165{
166 double ax = std::abs (x);
167 double lax = log2 (ax, exp);
168 return (ax != lax) ? (x / ax) * lax : x;
169}
170
172log2 (const FloatComplex& x, int& exp)
173{
174 float ax = std::abs (x);
175 float lax = log2 (ax, exp);
176 return (ax != lax) ? (x / ax) * lax : x;
177}
178
179bool
180negative_sign (double x) { return signbit (x); }
181bool
182negative_sign (float x) { return signbit (x); }
183
184// Sometimes you need a large integer, but not always.
185
187nint_big (double x)
188{
189 static constexpr double out_of_range_top
190 = static_cast<double> (std::numeric_limits<octave_idx_type>::max ()) + 1.0;
191
192 if (x >= out_of_range_top)
193 return std::numeric_limits<octave_idx_type>::max ();
194 else if (x < std::numeric_limits<octave_idx_type>::min ())
195 return std::numeric_limits<octave_idx_type>::min ();
196 else
197 return static_cast<octave_idx_type> (round (x));
198}
199
201nint_big (float x)
202{
203 static constexpr float out_of_range_top
204 = static_cast<float> (std::numeric_limits<octave_idx_type>::max ()) + 1.0;
205
206 if (x >= out_of_range_top)
207 return std::numeric_limits<octave_idx_type>::max ();
208 else if (x < std::numeric_limits<octave_idx_type>::min ())
209 return std::numeric_limits<octave_idx_type>::min ();
210 else
211 return static_cast<octave_idx_type> (round (x));
212}
213
214int
215nint (double x)
216{
217 if (x > std::numeric_limits<int>::max ())
218 return std::numeric_limits<int>::max ();
219 else if (x < std::numeric_limits<int>::min ())
220 return std::numeric_limits<int>::min ();
221 else
222 return static_cast<int> (round (x));
223}
224
225int
226nint (float x)
227{
228 static constexpr float out_of_range_top
229 = static_cast<float> (std::numeric_limits<int>::max ()) + 1.0;
230
231 if (x >= out_of_range_top)
232 return std::numeric_limits<int>::max ();
233 else if (x < std::numeric_limits<int>::min ())
234 return std::numeric_limits<int>::min ();
235 else
236 return static_cast<int> (round (x));
237}
238
240rc_acos (double x)
241{
242 return fabs (x) > 1.0 ? acos (Complex (x)) : Complex (std::acos (x));
243}
244
246rc_acos (float x)
247{
248 return fabsf (x) > 1.0f ? acos (FloatComplex (x))
249 : FloatComplex (std::acos (x));
250}
251
253rc_acosh (double x)
254{
255 return x < 1.0 ? acosh (Complex (x)) : Complex (acosh (x));
256}
257
259rc_acosh (float x)
260{
261 return x < 1.0f ? acosh (FloatComplex (x)) : FloatComplex (acosh (x));
262}
263
265rc_asin (double x)
266{
267 return fabs (x) > 1.0 ? asin (Complex (x)) : Complex (std::asin (x));
268}
269
271rc_asin (float x)
272{
273 return fabsf (x) > 1.0f ? asin (FloatComplex (x))
274 : FloatComplex (::asinf (x));
275}
276
278rc_atanh (double x)
279{
280 return fabs (x) > 1.0 ? atanh (Complex (x)) : Complex (atanh (x));
281}
282
284rc_atanh (float x)
285{
286 return fabsf (x) > 1.0f ? atanh (FloatComplex (x))
287 : FloatComplex (atanh (x));
288}
289
291rc_log (double x)
292{
293 return x < 0.0 ? Complex (std::log (-x), M_PI) : Complex (std::log (x));
294}
295
297rc_log (float x)
298{
299 return x < 0.0f ? FloatComplex (std::log (-x), static_cast<float> (M_PI))
300 : FloatComplex (std::log (x));
301}
302
304rc_log2 (double x)
305{
306 constexpr double PI_LN2 = 4.53236014182719380962; // = pi / log(2)
307 return x < 0.0 ? Complex (log2 (-x), PI_LN2) : Complex (log2 (x));
308}
309
311rc_log2 (float x)
312{
313 constexpr float PI_LN2 = 4.53236014182719380962f; // = pi / log(2)
314 return x < 0.0f ? FloatComplex (log2 (-x), PI_LN2)
315 : FloatComplex (log2 (x));
316}
317
319rc_log10 (double x)
320{
321 constexpr double PI_LN10 = 1.36437635384184134748; // = pi / log(10)
322 return x < 0.0 ? Complex (log10 (-x), PI_LN10) : Complex (log10 (x));
323}
324
326rc_log10 (float x)
327{
328 constexpr float PI_LN10 = 1.36437635384184134748f; // = pi / log(10)
329 return x < 0.0f ? FloatComplex (log10 (-x), PI_LN10)
330 : FloatComplex (log10f (x));
331}
332
334rc_sqrt (double x)
335{
336 return x < 0.0 ? Complex (0.0, std::sqrt (-x)) : Complex (std::sqrt (x));
337}
338
340rc_sqrt (float x)
341{
342 return x < 0.0f ? FloatComplex (0.0f, std::sqrt (-x))
343 : FloatComplex (std::sqrt (x));
344}
345
346OCTAVE_END_NAMESPACE(math)
347OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
bool is_NaN_or_NA(const Complex &x)
Definition mappers.cc:70
Complex log2(const Complex &x)
Definition mappers.cc:140
Complex rc_acosh(double x)
Definition mappers.cc:253
Complex rc_atanh(double x)
Definition mappers.cc:278
Complex rc_log(double x)
Definition mappers.cc:291
Complex asin(const Complex &x)
Definition mappers.cc:106
octave_idx_type nint_big(double x)
Definition mappers.cc:187
int nint(double x)
Definition mappers.cc:215
Complex rc_asin(double x)
Definition mappers.cc:265
bool negative_sign(double x)
Definition mappers.cc:180
bool isna(double x)
Definition mappers.cc:46
Complex rc_acos(double x)
Definition mappers.cc:240
Complex rc_sqrt(double x)
Definition mappers.cc:334
Complex rc_log10(double x)
Definition mappers.cc:319
Complex acos(const Complex &x)
Definition mappers.cc:84
Complex rc_log2(double x)
Definition mappers.cc:304
double frexp(double x, int *expptr)
Definition mappers.cc:128
#define lo_ieee_is_NA(x)
Definition lo-ieee.h:144
double signbit(double x)
Definition mappers.h:66
double round(double x)
Definition mappers.h:148
bool isnan(bool)
Definition mappers.h:192
double octave_frexp_wrapper(double x, int *expptr)
float octave_frexpf_wrapper(float x, int *expptr)
std::complex< double > Complex
Definition oct-cmplx.h:33
std::complex< float > FloatComplex
Definition oct-cmplx.h:34
double atanh(double x)
Definition oct-specfun.h:62
double acosh(double x)
Definition oct-specfun.h:39
F77_RET_T const F77_DBLE * x