Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
real.cpp
Go to the documentation of this file.
1 /*
2  * real.cpp - some real valued function implementations
3  *
4  * Copyright (C) 2008 Stefan Jahn <stefan@lkcc.org>
5  * Copyright (C) 2014 Guilheme Brondani Torri <guitorri@gmail.com>
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this package; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  * $Id$
23  *
24  */
25 
26 #if HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29 
30 //#include <cstdlib>
31 #include <cmath>
32 #include <cassert>
33 
34 #include "consts.h"
35 #include "real.h"
36 
37 namespace qucs {
38 
39 //
40 // trigonometric
41 //
42 
47 nr_double_t cos (const nr_double_t arg) {
48  return std::cos (arg);
49 }
50 
55 nr_double_t sin (const nr_double_t arg) {
56  return std::sin (arg);
57 }
58 
63 nr_double_t tan (const nr_double_t arg) {
64  return std::tan (arg);
65 }
66 
71 nr_double_t acos (const nr_double_t arg) {
72  return std::acos (arg);
73 }
74 
79 nr_double_t asin (const nr_double_t arg) {
80  return std::asin (arg);
81 }
82 
87 nr_double_t atan (const nr_double_t arg) {
88  return std::atan (arg);
89 }
90 
96 nr_double_t atan2 (const nr_double_t x, const nr_double_t y) {
97  return std::atan2 (x,y);
98 }
99 
100 //
101 // hyperbolic
102 //
103 
108 nr_double_t cosh (const nr_double_t arg) {
109  return std::cosh (arg);
110 }
111 
116 nr_double_t sinh (const nr_double_t arg) {
117  return std::sinh (arg);
118 }
119 
124 nr_double_t tanh (const nr_double_t arg) {
125  return std::tanh (arg);
126 }
127 
132 nr_double_t acosh (const nr_double_t arg) {
133 #ifdef HAVE_STD_ACOSH
134  // c++11
135  return std::acosh (arg);
136 #elif HAVE_ACOSH
137  return ::acosh (arg);
138 #else
139  return log (arg + sqrt (arg * arg - 1.0));
140 #endif
141 }
142 
147 nr_double_t asinh (const nr_double_t arg)
148 {
149 #ifdef HAVE_STD_ASINH
150  // c++11
151  return std::asinh (arg);
152 #elif HAVE_ASINH
153  return ::asinh (arg);
154 #else
155  return log (arg + sqrt (arg * arg + 1.0));
156 #endif
157 }
158 
163 nr_double_t atanh (const nr_double_t arg)
164 {
165 #ifdef HAVE_STD_ATANH
166  // c++11
167  return std::atanh (arg);
168 #elif HAVE_ATANH
169  return ::atanh (arg);
170 #else
171  return 0.5 * log ( 2.0 / (1.0 - arg) - 1.0);
172 #endif
173 }
174 
175 
176 //
177 // exponential and logarithmic functions
178 //
179 nr_double_t exp (const nr_double_t arg) {
180  return std::exp (arg);
181 }
182 nr_double_t log (const nr_double_t arg) {
183  return std::log (arg);
184 }
185 nr_double_t log10 (const nr_double_t arg) {
186  return std::log10 (arg);
187 }
188 
189 //
190 // power functions
191 //
192 
193 nr_double_t pow (const nr_double_t a, const nr_double_t b)
194 {
195  return std::pow (a,b);
196 }
197 
198 nr_double_t sqrt (const nr_double_t d) {
199  return std::sqrt (d);
200 }
201 
213 nr_double_t xhypot (const nr_double_t a, const nr_double_t b) {
214 #ifdef HAVE_STD_HYPOT
215  return std::hypot(a,b) // c++11
216 #else
217  nr_double_t c = fabs (a);
218  nr_double_t d = fabs (b);
219  if (c > d) {
220  nr_double_t e = d / c;
221  return c * sqrt (1 + e * e);
222  }
223  else if (d == 0)
224  return 0;
225  else {
226  nr_double_t e = c / d;
227  return d * sqrt (1 + e * e);
228  }
229 #endif
230 }
231 
232 //
233 // error functions
234 //
235 
236 nr_double_t erf( nr_double_t arg) {
237 #ifdef HAVE_STD_ERF
238  return std::erf (arg); // c++11
239 #elif HAVE_ERF
240  return ::erf (arg);
241 #endif
242 }
243 
244 
245 //
246 // rounding and remainder functions
247 //
248 nr_double_t ceil( nr_double_t arg) {
249  return std::ceil(arg);
250 }
251 
252 nr_double_t floor( nr_double_t arg) {
253  return std::floor(arg);
254 }
255 
256 nr_double_t fmod( nr_double_t arg) {
257 #ifdef HAVE_STD_TRUNC
258  return std::fmod(arg);
259 #else
260  return fmod(arg);
261 #endif
262 }
263 
264 nr_double_t trunc( nr_double_t arg) {
265 #ifdef HAVE_STD_TRUNC
266  return qucs::trunc(arg);
267 #elif HAVE_TRUNC
268  return ::trunc (arg);
269 #else
270  return arg > 0 ? floor (arg) : floor (arg + 1);
271 #endif
272 }
273 nr_double_t round( nr_double_t arg) {
274 #ifdef HAVE_STD_ROUND
275  return qucs::round(arg);
276 #elif HAVE_ROUND
277  return ::round (arg);
278 #else
279  return (arg > 0) ? floor (arg + 0.5) : ceil (arg - 0.5);
280 #endif
281 }
282 
283 
284 //
285 // Qucs extra trigonometric helper
286 //
287 nr_double_t coth (const nr_double_t d) {
288  return 1.0 / std::tanh (d);
289 }
290 
291 nr_double_t sech (const nr_double_t d) {
292  return (1.0 / std::cosh (d));
293 }
294 
295 nr_double_t cosech (const nr_double_t d) {
296  return (1.0 / std::sinh (d));
297 }
298 
299 
300 //
301 // Qucs extra math functions
302 //
303 
309 nr_double_t sqr (const nr_double_t r) {
310  return r * r;
311 }
312 
313 unsigned int sqr (unsigned int r) {
314  return r * r;
315 }
316 
317 
318 
324 nr_double_t quadr (const nr_double_t r) {
325  return r * r * r * r;
326 }
327 
328 
329 //
330 // extra math functions
331 //
332 
350 nr_double_t limexp (const nr_double_t r) {
351  return r < M_LIMEXP ? exp (r) : exp (M_LIMEXP) * (1.0 + (r - M_LIMEXP));
352 }
353 
368 nr_double_t signum (const nr_double_t d) {
369  if (d == 0) return 0;
370  return d < 0 ? -1 : 1;
371 }
372 
386 nr_double_t sign (const nr_double_t d) {
387  return d < 0 ? -1 : 1;
388 }
389 
397 nr_double_t sinc (const nr_double_t d) {
398  if (d == 0) return 1;
399  return sin (d) / d;
400 }
401 
416 nr_double_t fix (const nr_double_t d) {
417  return (d > 0) ? floor (d) : ceil (d);
418 }
419 
429 nr_double_t step (const nr_double_t d) {
430  nr_double_t x = d;
431  if (x < 0.0)
432  x = 0.0;
433  else if (x > 0.0)
434  x = 1.0;
435  else
436  x = 0.5;
437  return x;
438 }
439 
443 unsigned int
444 factorial (unsigned int n) {
445  unsigned int result = 1;
446 
447  /* 13! > 2^32 */
448  assert (n < 13);
449 
450  if (n == 0)
451  return 1;
452 
453  for (; n > 1; n--)
454  result = result * n;
455 
456  return result;
457 }
458 
459 
460 //
461 // overload complex manipulations on reals
462 //
463 
464 
470 nr_double_t real (const nr_double_t r) {
471  return r;
472 }
473 
479 nr_double_t imag (const nr_double_t r) {
480  return 0.0;
481 }
482 
489 nr_double_t norm (const nr_double_t r) {
490  return r * r;
491 }
492 
498 nr_double_t abs (const nr_double_t r) {
499  return std::abs (r);
500 }
501 
507 nr_double_t conj (const nr_double_t r) {
508  return r;
509 }
510 
511 } // namespace qucs
matrix real(matrix a)
Real part matrix.
Definition: matrix.cpp:568
matrix abs(matrix a)
Computes magnitude of each matrix element.
Definition: matrix.cpp:531
nr_complex_t erf(const nr_complex_t z)
Error function.
Definition: complex.cpp:766
nr_complex_t ceil(const nr_complex_t z)
Complex ceil Ceil is the smallest integral value not less than argument Apply ceil to real and imagin...
Definition: complex.cpp:634
nr_complex_t coth(const nr_complex_t z)
Compute complex hyperbolic cotangent.
Definition: complex.cpp:320
nr_double_t round(nr_double_t arg)
Definition: real.cpp:273
nr_complex_t pow(const nr_complex_t z, const nr_double_t d)
Compute power function with real exponent.
Definition: complex.cpp:238
nr_complex_t cos(const nr_complex_t z)
Compute complex cosine.
Definition: complex.cpp:57
nr_complex_t step(const nr_complex_t z)
Heaviside step function for complex number.
Definition: complex.cpp:691
nr_double_t abs(const nr_double_t r)
Compute complex modulus of real number.
Definition: real.cpp:498
nr_double_t atanh(const nr_double_t arg)
Compute arc hyperbolic tangent.
Definition: real.cpp:163
nr_complex_t signum(const nr_complex_t z)
complex signum function
Definition: complex.cpp:416
nr_double_t ceil(nr_double_t arg)
Definition: real.cpp:248
nr_complex_t atan(const nr_complex_t z)
Compute complex arc tangent.
Definition: complex.cpp:117
nr_double_t cosh(const nr_double_t arg)
Compute hyperbolic cosine.
Definition: real.cpp:108
nr_double_t log(const nr_double_t arg)
Definition: real.cpp:182
nr_double_t floor(nr_double_t arg)
Definition: real.cpp:252
n
Definition: parse_citi.y:147
nr_double_t cos(const nr_double_t arg)
Compute cosine of an angle.
Definition: real.cpp:47
r
Definition: parse_mdl.y:515
nr_complex_t acosh(const nr_complex_t z)
Compute complex arc hyperbolic cosine.
Definition: complex.cpp:162
nr_double_t acosh(const nr_double_t arg)
Compute arc hyperbolic cosine.
Definition: real.cpp:132
nr_complex_t sign(const nr_complex_t z)
complex sign function
Definition: complex.cpp:435
nr_complex_t asinh(const nr_complex_t z)
Compute complex arc hyperbolic sine.
Definition: complex.cpp:175
nr_complex_t cosh(const nr_complex_t z)
Compute complex hyperbolic cosine.
Definition: complex.cpp:135
nr_complex_t sqr(const nr_complex_t z)
Square of complex number.
Definition: complex.cpp:673
matrix imag(matrix a)
Imaginary part matrix.
Definition: matrix.cpp:581
nr_double_t trunc(nr_double_t arg)
Definition: real.cpp:264
nr_complex_t fix(const nr_complex_t z)
Complex fix.
Definition: complex.cpp:645
nr_complex_t sqrt(const nr_complex_t z)
Compute principal value of square root.
Definition: complex.cpp:271
#define M_LIMEXP
LIMEXP.
Definition: consts.h:101
nr_double_t tanh(const nr_double_t arg)
Compute hyperbolic tangent.
Definition: real.cpp:124
nr_double_t xhypot(const nr_complex_t a, const nr_complex_t b)
Euclidean distance function for complex argument.
Definition: complex.cpp:465
nr_complex_t trunc(const nr_complex_t z)
Complex trunc Apply round to integer, towards zero to real and imaginary part.
Definition: complex.cpp:512
nr_complex_t acos(const nr_complex_t z)
Compute complex arc cosine.
Definition: complex.cpp:84
nr_double_t asinh(const nr_double_t arg)
Compute arc hyperbolic sine.
Definition: real.cpp:147
nr_complex_t fmod(const nr_complex_t x, const nr_complex_t y)
Complex fmod Apply fmod to the complex z.
Definition: complex.cpp:662
nr_complex_t cosech(const nr_complex_t z)
Compute complex argument hyperbolic cosec.
Definition: complex.cpp:364
nr_complex_t tanh(const nr_complex_t z)
Compute complex hyperbolic tangent.
Definition: complex.cpp:153
nr_complex_t sin(const nr_complex_t z)
Compute complex sine.
Definition: complex.cpp:66
nr_complex_t asin(const nr_complex_t z)
Compute complex arc sine.
Definition: complex.cpp:102
nr_double_t quadr(const nr_double_t r)
Quartic function.
Definition: real.cpp:324
nr_double_t asin(const nr_double_t arg)
Compute arc sine.
Definition: real.cpp:79
Global math constants header file.
nr_double_t pow(const nr_double_t a, const nr_double_t b)
Definition: real.cpp:193
x
Definition: parse_mdl.y:498
nr_double_t log10(const nr_double_t arg)
Definition: real.cpp:185
nr_double_t sqrt(const nr_double_t d)
Definition: real.cpp:198
nr_complex_t log10(const nr_complex_t z)
Compute principal value of decimal logarithm of z.
Definition: complex.cpp:225
nr_complex_t floor(const nr_complex_t z)
Complex floor.
Definition: complex.cpp:623
nr_complex_t sech(const nr_complex_t z)
Compute complex hyperbolic secant.
Definition: complex.cpp:343
nr_double_t fmod(nr_double_t arg)
Definition: real.cpp:256
nr_double_t exp(const nr_double_t arg)
Definition: real.cpp:179
nr_double_t acos(const nr_double_t arg)
Compute arc cosine.
Definition: real.cpp:71
nr_complex_t atan2(const nr_complex_t y, const nr_complex_t x)
Compute complex arc tangent fortran like function.
Definition: complex.cpp:377
nr_complex_t sinc(const nr_complex_t z)
Cardinal sine.
Definition: complex.cpp:448
nr_double_t sinh(const nr_double_t arg)
Compute hyperbolic sine.
Definition: real.cpp:116
nr_complex_t limexp(const nr_complex_t z)
Compute limited complex exponential.
Definition: complex.cpp:539
nr_double_t norm(const nr_complex_t z)
Compute euclidian norm of complex number.
Definition: complex.cpp:283
nr_complex_t sinh(const nr_complex_t z)
Compute complex hyperbolic sine.
Definition: complex.cpp:144
y
Definition: parse_mdl.y:499
result
nr_double_t erf(nr_double_t arg)
Definition: real.cpp:236
nr_double_t tan(const nr_double_t arg)
Compute tangent of an angle.
Definition: real.cpp:63
nr_complex_t tan(const nr_complex_t z)
Compute complex tangent.
Definition: complex.cpp:75
nr_complex_t exp(const nr_complex_t z)
Compute complex exponential.
Definition: complex.cpp:205
matrix conj(matrix a)
Conjugate complex matrix.
Definition: matrix.cpp:505
unsigned int factorial(unsigned int n)
Compute factorial n ie $n!$.
Definition: real.cpp:444
nr_complex_t log(const nr_complex_t z)
Compute principal value of natural logarithm of z.
Definition: complex.cpp:215
nr_double_t atan(const nr_double_t arg)
Compute arc tangent.
Definition: real.cpp:87
nr_complex_t round(const nr_complex_t z)
Complex round Round is the nearest integral value Apply round to real and imaginary part...
Definition: complex.cpp:496
matrix arg(matrix a)
Computes the argument of each matrix element.
Definition: matrix.cpp:555
nr_double_t atan2(const nr_double_t x, const nr_double_t y)
Compute arc tangent with two parameters (fortran like function)
Definition: real.cpp:96
nr_double_t sin(const nr_double_t arg)
Compute sine of an angle.
Definition: real.cpp:55
nr_complex_t atanh(const nr_complex_t z)
Compute complex arc hyperbolic tangent.
Definition: complex.cpp:188