1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* complex/math.c (from the GSL 1.1.1)
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000 Jorma Olavi Thtinen, Brian Gough
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Basic complex arithmetic functions
* Original version by Jorma Olavi Thtinen <jotahtin@cc.hut.fi>
*
* Modified for GSL by Brian Gough, 3/2000
*/
/* The following references describe the methods used in these
* functions,
*
* T. E. Hull and Thomas F. Fairgrieve and Ping Tak Peter Tang,
* "Implementing Complex Elementary Functions Using Exception
* Handling", ACM Transactions on Mathematical Software, Volume 20
* (1994), pp 215-244, Corrigenda, p553
*
* Hull et al, "Implementing the complex arcsin and arccosine
* functions using exception handling", ACM Transactions on
* Mathematical Software, Volume 23 (1997) pp 299-335
*
* Abramowitz and Stegun, Handbook of Mathematical Functions, "Inverse
* Circular Functions in Terms of Real and Imaginary Parts", Formulas
* 4.4.37, 4.4.38, 4.4.39
*/
/*
* Gnumeric specific modifications written by Jukka-Pekka Iivonen
* (jiivonen@hutcs.cs.hut.fi)
*
* long double modifications by Morten Welinder.
*/
#include <gnumeric-config.h>
#include <glib/gi18n-lib.h>
#include <gnumeric.h>
#include <func.h>
#include <complex.h>
#include "gsl-complex.h"
#include <parse-util.h>
#include <cell.h>
#include <expr.h>
#include <value.h>
#include <mathfunc.h>
#define GSL_REAL(x) (x)->re
#define GSL_IMAG(x) (x)->im
/***********************************************************************
* Complex arithmetic operators
***********************************************************************/
static inline void
gsl_complex_mul_imag (complex_t const *a, gnm_float y, complex_t *res)
{ /* z=a*iy */
complex_init (res, -y * GSL_IMAG (a), y * GSL_REAL (a));
}
void
gsl_complex_inverse (complex_t const *a, complex_t *res)
{ /* z=1/a */
gnm_float s = 1.0 / complex_mod (a);
complex_init (res, (GSL_REAL (a) * s) * s, -(GSL_IMAG (a) * s) * s);
}
void
gsl_complex_negative (complex_t const *a, complex_t *res)
{
complex_init (res, -GSL_REAL (a), -GSL_IMAG (a));
}
/**********************************************************************
* Inverse Complex Trigonometric Functions
**********************************************************************/
static void
gsl_complex_arcsin_real (gnm_float a, complex_t *res)
{ /* z = arcsin(a) */
if (gnm_abs (a) <= 1.0) {
complex_init (res, gnm_asin (a), 0.0);
} else {
if (a < 0.0) {
complex_init (res, -M_PI_2gnum, gnm_acosh (-a));
} else {
complex_init (res, M_PI_2gnum, -gnm_acosh (a));
}
}
}
void
gsl_complex_arcsin (complex_t const *a, complex_t *res)
{ /* z = arcsin(a) */
gnm_float R = GSL_REAL (a), I = GSL_IMAG (a);
if (I == 0) {
gsl_complex_arcsin_real (R, res);
} else {
gnm_float x = gnm_abs (R), y = gnm_abs (I);
gnm_float r = gnm_hypot (x + 1, y);
gnm_float s = gnm_hypot (x - 1, y);
gnm_float A = 0.5 * (r + s);
gnm_float B = x / A;
gnm_float y2 = y * y;
gnm_float real, imag;
const gnm_float A_crossover = 1.5, B_crossover = 0.6417;
if (B <= B_crossover) {
real = gnm_asin (B);
} else {
if (x <= 1) {
gnm_float D = 0.5 * (A + x) *
(y2 / (r + x + 1) + (s + (1 - x)));
real = gnm_atan (x / gnm_sqrt (D));
} else {
gnm_float Apx = A + x;
gnm_float D = 0.5 * (Apx / (r + x + 1)
+ Apx / (s + (x - 1)));
real = gnm_atan (x / (y * gnm_sqrt (D)));
}
}
if (A <= A_crossover) {
gnm_float Am1;
if (x < 1) {
Am1 = 0.5 * (y2 / (r + (x + 1)) + y2 /
(s + (1 - x)));
} else {
Am1 = 0.5 * (y2 / (r + (x + 1)) +
(s + (x - 1)));
}
imag = gnm_log1p (Am1 + gnm_sqrt (Am1 * (A + 1)));
} else {
imag = gnm_log (A + gnm_sqrt (A * A - 1));
}
complex_init (res, (R >= 0) ? real : -real, (I >= 0) ?
imag : -imag);
}
}
static void
gsl_complex_arccos_real (gnm_float a, complex_t *res)
{ /* z = arccos(a) */
if (gnm_abs (a) <= 1.0) {
complex_init (res, gnm_acos (a), 0);
} else {
if (a < 0.0) {
complex_init (res, M_PIgnum, -gnm_acosh (-a));
} else {
complex_init (res, 0, gnm_acosh (a));
}
}
}
void
gsl_complex_arccos (complex_t const *a, complex_t *res)
{ /* z = arccos(a) */
gnm_float R = GSL_REAL (a), I = GSL_IMAG (a);
if (I == 0) {
gsl_complex_arccos_real (R, res);
} else {
gnm_float x = gnm_abs (R);
gnm_float y = gnm_abs (I);
gnm_float r = gnm_hypot (x + 1, y);
gnm_float s = gnm_hypot (x - 1, y);
gnm_float A = 0.5 * (r + s);
gnm_float B = x / A;
gnm_float y2 = y * y;
gnm_float real, imag;
const gnm_float A_crossover = 1.5;
const gnm_float B_crossover = 0.6417;
if (B <= B_crossover) {
real = gnm_acos (B);
} else {
if (x <= 1) {
gnm_float D = 0.5 * (A + x) *
(y2 / (r + x + 1) + (s + (1 - x)));
real = gnm_atan (gnm_sqrt (D) / x);
} else {
gnm_float Apx = A + x;
gnm_float D = 0.5 * (Apx / (r + x + 1) + Apx /
(s + (x - 1)));
real = gnm_atan ((y * gnm_sqrt (D)) / x);
}
}
if (A <= A_crossover) {
gnm_float Am1;
if (x < 1) {
Am1 = 0.5 * (y2 / (r + (x + 1)) + y2 /
(s + (1 - x)));
} else {
Am1 = 0.5 * (y2 / (r + (x + 1)) +
(s + (x - 1)));
}
imag = gnm_log1p (Am1 + gnm_sqrt (Am1 * (A + 1)));
} else {
imag = gnm_log (A + gnm_sqrt (A * A - 1));
}
complex_init (res, (R >= 0) ? real : M_PIgnum - real, (I >= 0) ?
-imag : imag);
}
}
void
gsl_complex_arctan (complex_t const *a, complex_t *res)
{ /* z = arctan(a) */
gnm_float R = GSL_REAL (a), I = GSL_IMAG (a);
if (I == 0) {
complex_init (res, gnm_atan (R), 0);
} else {
/* FIXME: This is a naive implementation which does not fully
* take into account cancellation errors, overflow, underflow
* etc. It would benefit from the Hull et al treatment. */
gnm_float r = gnm_hypot (R, I);
gnm_float imag;
gnm_float u = 2 * I / (1 + r * r);
/* FIXME: the following cross-over should be optimized but 0.1
* seems to work ok */
if (gnm_abs (u) < 0.1) {
imag = 0.25 * (gnm_log1p (u) - gnm_log1p (-u));
} else {
gnm_float A = gnm_hypot (R, I + 1);
gnm_float B = gnm_hypot (R, I - 1);
imag = 0.5 * gnm_log (A / B);
}
if (R == 0) {
if (I > 1) {
complex_init (res, M_PI_2gnum, imag);
} else if (I < -1) {
complex_init (res, -M_PI_2gnum, imag);
} else {
complex_init (res, 0, imag);
}
} else {
complex_init (res, 0.5 * gnm_atan2 (2 * R,
((1 + r) * (1 - r))),
imag);
}
}
}
void
gsl_complex_arcsec (complex_t const *a, complex_t *res)
{ /* z = arcsec(a) */
gsl_complex_inverse (a, res);
gsl_complex_arccos (res, res);
}
void
gsl_complex_arccsc (complex_t const *a, complex_t *res)
{ /* z = arccsc(a) */
gsl_complex_inverse (a, res);
gsl_complex_arcsin (res, res);
}
void
gsl_complex_arccot (complex_t const *a, complex_t *res)
{ /* z = arccot(a) */
if (GSL_REAL (a) == 0.0 && GSL_IMAG (a) == 0.0) {
complex_init (res, M_PI_2gnum, 0);
} else {
gsl_complex_inverse (a, res);
gsl_complex_arctan (res, res);
}
}
/**********************************************************************
* Complex Hyperbolic Functions
**********************************************************************/
void
gsl_complex_sinh (complex_t const *a, complex_t *res)
{ /* z = sinh(a) */
gnm_float R = GSL_REAL (a), I = GSL_IMAG (a);
complex_init (res, gnm_sinh (R) * gnm_cos (I), cosh (R) * gnm_sin (I));
}
void
gsl_complex_cosh (complex_t const *a, complex_t *res)
{ /* z = cosh(a) */
gnm_float R = GSL_REAL (a), I = GSL_IMAG (a);
complex_init (res, cosh (R) * gnm_cos (I), gnm_sinh (R) * gnm_sin (I));
}
void
gsl_complex_tanh (complex_t const *a, complex_t *res)
{ /* z = tanh(a) */
gnm_float R = GSL_REAL (a), I = GSL_IMAG (a);
if (gnm_abs (R) < 1.0) {
gnm_float D =
gnm_pow (gnm_cos (I), 2.0) +
gnm_pow (gnm_sinh (R), 2.0);
complex_init (res, gnm_sinh (R) * cosh (R) / D,
0.5 * gnm_sin (2 * I) / D);
} else {
gnm_float D =
gnm_pow (gnm_cos (I), 2.0) +
gnm_pow (gnm_sinh (R), 2.0);
gnm_float F = 1 + gnm_pow (gnm_cos (I) / gnm_sinh (R), 2.0);
complex_init (res, 1.0 / (gnm_tanh (R) * F),
0.5 * gnm_sin (2 * I) / D);
}
}
void
gsl_complex_sech (complex_t const *a, complex_t *res)
{ /* z = sech(a) */
gsl_complex_cosh (a, res);
gsl_complex_inverse (res, res);
}
void
gsl_complex_csch (complex_t const *a, complex_t *res)
{ /* z = csch(a) */
gsl_complex_sinh (a, res);
gsl_complex_inverse (res, res);
}
void
gsl_complex_coth (complex_t const *a, complex_t *res)
{ /* z = coth(a) */
gsl_complex_tanh (a, res);
gsl_complex_inverse (res, res);
}
/**********************************************************************
* Inverse Complex Hyperbolic Functions
**********************************************************************/
void
gsl_complex_arcsinh (complex_t const *a, complex_t *res)
{ /* z = arcsinh(a) */
gsl_complex_mul_imag (a, 1.0, res);
gsl_complex_arcsin (res, res);
gsl_complex_mul_imag (res, -1.0, res);
}
void
gsl_complex_arccosh (complex_t const *a, complex_t *res)
{ /* z = arccosh(a) */
gsl_complex_arccos (a, res);
gsl_complex_mul_imag (res, GSL_IMAG (res) > 0 ? -1.0 : 1.0, res);
}
static void
gsl_complex_arctanh_real (gnm_float a, complex_t *res)
{ /* z = arctanh(a) */
if (a > -1.0 && a < 1.0) {
complex_init (res, gnm_atanh (a), 0);
} else {
complex_init (res, gnm_atanh (1 / a),
(a < 0) ? M_PI_2gnum : -M_PI_2gnum);
}
}
void
gsl_complex_arctanh (complex_t const *a, complex_t *res)
{ /* z = arctanh(a) */
if (GSL_IMAG (a) == 0.0) {
gsl_complex_arctanh_real (GSL_REAL (a), res);
} else {
gsl_complex_mul_imag (a, 1.0, res);
gsl_complex_arctan (res, res);
gsl_complex_mul_imag (res, -1.0, res);
}
}
void
gsl_complex_arcsech (complex_t const *a, complex_t *res)
{ /* z = arcsech(a); */
gsl_complex_inverse (a, res);
gsl_complex_arccosh (res, res);
}
void
gsl_complex_arccsch (complex_t const *a, complex_t *res)
{ /* z = arccsch(a); */
gsl_complex_inverse (a, res);
gsl_complex_arcsinh (res, res);
}
void
gsl_complex_arccoth (complex_t const *a, complex_t *res)
{ /* z = arccoth(a); */
gsl_complex_inverse (a, res);
gsl_complex_arctanh (res, res);
}
|