Module: CCMath
- Defined in:
- lib/ccmath.rb,
ext/ccmath/ccmath.c
Defined Under Namespace
Classes: DomainError
Constant Summary collapse
- PI =
DBL2NUM(atan(1.0)*4.0)
- E =
DBL2NUM(exp(1.0))
Class Method Summary collapse
- .acos(z) ⇒ Object
- .acosh(z) ⇒ Object
- .asin(z) ⇒ Object
- .asinh(z) ⇒ Object
- .atan(z) ⇒ Object
- .atanh(z) ⇒ Object
- .cos(z) ⇒ Object
- .cosh(z) ⇒ Object
- .exp(z) ⇒ Object
-
.log(*args) ⇒ Object
log(d * 2 ** numbits).
- .log10(z) ⇒ Object
- .log2(z) ⇒ Object
- .set=(str) ⇒ Object
- .sin(z) ⇒ Object
- .sinh(z) ⇒ Object
- .sqrt(z) ⇒ Object
- .tan(z) ⇒ Object
- .tanh(z) ⇒ Object
Class Method Details
.acos(z) ⇒ Object
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'ext/ccmath/ccmath.c', line 393
static VALUE
ccmath_acos(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
double d = NUM2DBL_F(z);
if (d < -1.0 || 1.0 < d) domain_error("acos");
return DBL2NUM(acos(d));
}
else {
EXTRACT_DBLS(z);;
if (z_imag == 0.0) return DBLS2COMP(acos(z_real), 0.0);
return rb_funcall(DBL2NUM(M_PI / 2.0), '-', 1, ccmath_asin(obj, z));
}
}
|
.acosh(z) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'ext/ccmath/ccmath.c', line 368
static VALUE
ccmath_acosh(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
double d = NUM2DBL_F(z);
if (d < 1.0) domain_error("acosh");
return DBL2NUM(acosh(d));
}
else {
EXTRACT_DBLS(z);;
if (z_imag == 0.0) return DBLS2COMP(acosh(z_real), 0.0);
VALUE s1 = DBLS2COMP(z_real - 1.0, z_imag);
VALUE s2 = DBLS2COMP(z_real + 1.0, z_imag);
s1 = ccmath_sqrt(obj, s1);
s2 = ccmath_sqrt(obj, s2);
EXTRACT_DBLS(s1);
EXTRACT_DBLS(s2);
return DBLS2COMP(asinh(s1_real * s2_real + s1_imag * s2_imag), 2.0 * atan2(s1_imag, s2_real));
}
}
|
.asin(z) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'ext/ccmath/ccmath.c', line 349
static VALUE
ccmath_asin(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
double d = NUM2DBL_F(z);
if (d < -1.0 || 1.0 < d) domain_error("asin");
return DBL2NUM(asin(d));
}
else {
EXTRACT_DBLS(z);;
if (z_imag == 0.0) return DBLS2COMP(asin(z_real), 0.0);
VALUE s = ccmath_asinh(obj, DBLS2COMP(-z_imag, z_real));
EXTRACT_DBLS(s);
return DBLS2COMP(s_imag, -s_real);
}
}
|
.asinh(z) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'ext/ccmath/ccmath.c', line 326
static VALUE
ccmath_asinh(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
return DBL2NUM(asinh(NUM2DBL_F(z)));
}
else {
EXTRACT_DBLS(z);;
if (z_imag == 0.0) return DBLS2COMP(asinh(z_real), 0.0);
VALUE s1 = DBLS2COMP(1.0 + z_imag, -z_real);
VALUE s2 = DBLS2COMP(1.0 - z_imag, z_real);
s1 = ccmath_sqrt(obj, s1);
s2 = ccmath_sqrt(obj, s2);
EXTRACT_DBLS(s1);
EXTRACT_DBLS(s2);
return DBLS2COMP(asinh(s1_real * s2_imag - s2_real * s1_imag), atan2(z_imag, s1_real * s2_real - s1_imag * s2_imag));
}
}
|
.atan(z) ⇒ Object
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'ext/ccmath/ccmath.c', line 433
static VALUE
ccmath_atan(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
return DBL2NUM(atan(NUM2DBL_F(z)));
}
else {
EXTRACT_DBLS(z);;
if (z_imag == 0.0) return DBLS2COMP(atan(z_real), 0.0);
VALUE s = ccmath_atanh(obj, DBLS2COMP(-z_imag, z_real));
EXTRACT_DBLS(s);
return DBLS2COMP(s_imag, -s_real);
}
}
|
.atanh(z) ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'ext/ccmath/ccmath.c', line 410
static VALUE
ccmath_atanh(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
double d = NUM2DBL_F(z);
if (d < -1.0 || +1.0 < d) domain_error("atanh");
if (d == -1.0) return DBL2NUM(-INFINITY);
if (d == +1.0) return DBL2NUM(+INFINITY);
return DBL2NUM(atanh(d));
}
else {
EXTRACT_DBLS(z);;
if (z_imag == 0.0) return DBLS2COMP(atanh(z_real), 0.0);
double sq_imag = z_imag * z_imag;
return DBLS2COMP(m_log1p(4.0 * z_real / ((1.0 - z_real) * (1.0 - z_real) + sq_imag)) / 4.0,
-m_atan2(-2.0 * z_imag, (1.0 - z_real) * (1.0 + z_real) - sq_imag) / 2.0);
}
}
|
.cos(z) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'ext/ccmath/ccmath.c', line 247
static VALUE
ccmath_cos(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
return DBL2NUM(cos(NUM2DBL_F(z)));
}
else {
EXTRACT_DBLS(z);;
if (z_real == 0.0) return DBLS2COMP(cosh(z_imag), 0.0);
if (z_imag == 0.0) return DBLS2COMP(cos(z_real), 0.0);
return DBLS2COMP(cos(z_real) * cosh(z_imag), -sin(z_real) * sinh(z_imag));
}
}
|
.cosh(z) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'ext/ccmath/ccmath.c', line 287
static VALUE
ccmath_cosh(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
return DBL2NUM(cosh(NUM2DBL_F(z)));
}
else {
EXTRACT_DBLS(z);;
if (z_real == 0.0) return DBLS2COMP(cos(z_imag), 0.0);
if (z_imag == 0.0) return DBLS2COMP(cosh(z_real), 0.0);
return DBLS2COMP(cosh(z_real) * cos(z_imag), sinh(z_real) * sin(z_imag));
}
}
|
.exp(z) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'ext/ccmath/ccmath.c', line 128
static VALUE
ccmath_exp(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
return DBL2NUM(exp(NUM2DBL_F(z)));
}
else {
EXTRACT_DBLS(z);
if (z_imag == 0.0) return DBLS2COMP(exp(z_real), 0.0);
double ere = exp(z_real);
return DBLS2COMP(ere * cos(z_imag), ere * sin(z_imag));
}
}
|
.log(*args) ⇒ Object
log(d * 2 ** numbits)
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 |
# File 'ext/ccmath/ccmath.c', line 170
static VALUE
ccmath_log(int argc, const VALUE* argv, VALUE obj)
{
VALUE z, base;
rb_scan_args(argc, argv, "11", &z, &base);
if (codomain_check(z)) {
double d = internal_log(z);
if (argc == 2) {
d /= internal_log(base);
}
return DBL2NUM(d);
}
else {
EXTRACT_DBLS(z);;
float r = hypot(z_real, z_imag);
if (argc == 2) {
if (!rb_respond_to(base, id_real_p)) rb_raise(rb_eTypeError, "Numeric Number required");
EXTRACT_DBLS(base);
if (base_imag != 0.0) domain_error("log");
if (base_real > 0.0) {
double ln_base = log(base_real);
return DBLS2COMP(log(r) / ln_base, m_atan2(z_imag, z_real) / ln_base);
}
else {
VALUE ln_base = DBLS2COMP(log(fabs(base_real)), M_PI);
return rb_funcall(DBLS2COMP(log(r), m_atan2(z_imag, z_real)), '/', 1, ln_base);
}
}
else {
return DBLS2COMP(log(r), m_atan2(z_imag, z_real));
}
}
}
|
.log10(z) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'ext/ccmath/ccmath.c', line 226
static VALUE
ccmath_log10(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
double d;
size_t numbits;
assing_numbits(z, &d, &numbits);
if (d < 0.0) domain_error("log10");
if (d == 0.0) return DBL2NUM(-INFINITY);
return DBL2NUM(log10(d) + numbits * log10(2));
}
else {
EXTRACT_DBLS(z);;
float r = hypot(z_real, z_imag);
return DBLS2COMP(log(r) / M_LN10, m_atan2(z_imag, z_real) / M_LN10);
}
}
|
.log2(z) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'ext/ccmath/ccmath.c', line 205
static VALUE
ccmath_log2(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
double d;
size_t numbits;
assing_numbits(z, &d, &numbits);
if (d < 0.0) domain_error("log2");
if (d == 0.0) return DBL2NUM(-INFINITY);
return DBL2NUM(log2(d) + numbits);
}
else {
EXTRACT_DBLS(z);;
float r = hypot(z_real, z_imag);
return DBLS2COMP(log(r) / M_LN2, m_atan2(z_imag, z_real) / M_LN2);
}
}
|
.set=(str) ⇒ Object
450 451 452 453 454 455 |
# File 'ext/ccmath/ccmath.c', line 450
static VALUE
ccmath_define_set(VALUE obj, VALUE str)
{
rb_ivar_set(obj, id_codomain, str);
return obj;
}
|
.sin(z) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'ext/ccmath/ccmath.c', line 261
static VALUE
ccmath_sin(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
return DBL2NUM(sin(NUM2DBL_F(z)));
}
else {
EXTRACT_DBLS(z);;
if (z_real == 0.0) return DBLS2COMP(0.0, sinh(z_imag));
if (z_imag == 0.0) return DBLS2COMP(sin(z_real), 0.0);
return DBLS2COMP(sin(z_real) * cosh(z_imag), cos(z_real) * sinh(z_imag));
}
}
|
.sinh(z) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'ext/ccmath/ccmath.c', line 301
static VALUE
ccmath_sinh(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
return DBL2NUM(sinh(NUM2DBL_F(z)));
}
else {
EXTRACT_DBLS(z);;
if (z_real == 0.0) return DBLS2COMP(0.0, sin(z_imag));
if (z_imag == 0.0) return DBLS2COMP(sinh(z_real), 0.0);
return DBLS2COMP(sinh(z_real) * cos(z_imag), cosh(z_real) * sin(z_imag));
}
}
|
.sqrt(z) ⇒ Object
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 |
# File 'ext/ccmath/ccmath.c', line 102
static VALUE
ccmath_sqrt(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
double d = NUM2DBL_F(z);
if (d < 0.0) domain_error("sqrt");
if (d == 0.0) return DBL2NUM(0.0);
return DBL2NUM(sqrt(d));
}
else {
EXTRACT_DBLS(z);
if (z_imag == 0.0) {
if (z_real < 0.0) {
return DBLS2COMP(0.0, sqrt(fabs(z_real)));
}
else {
return DBLS2COMP(sqrt(z_real), 0.0);
}
}
else {
double s = sqrt((hypot(z_real, z_imag) + z_real) / 2.0);
return DBLS2COMP(s, z_imag / (2 * s));
}
}
}
|
.tan(z) ⇒ Object
276 277 278 279 280 281 282 283 284 285 |
# File 'ext/ccmath/ccmath.c', line 276
static VALUE
ccmath_tan(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
return DBL2NUM(tan(NUM2DBL_F(z)));
}
else {
return rb_funcall(ccmath_sin(obj, z), '/', 1, ccmath_cos(obj, z));
}
}
|
.tanh(z) ⇒ Object
315 316 317 318 319 320 321 322 323 324 |
# File 'ext/ccmath/ccmath.c', line 315
static VALUE
ccmath_tanh(VALUE obj, VALUE z)
{
if (codomain_check(z)) {
return DBL2NUM(tanh(NUM2DBL_F(z)));
}
else {
return rb_funcall(ccmath_sinh(obj, z), '/', 1, ccmath_cosh(obj, z));
}
}
|