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

Class Method Details

.acos(z) ⇒ Object



300
301
302
303
304
305
306
307
308
# File 'ext/ccmath/ccmath.c', line 300

static VALUE
ccmath_acos(VALUE obj, VALUE z)
{
    EXTRACT_DBLS(z);

    if (z_imag == 0.0) return DBLS2COMP(acos(z_real), 0.0);

    return f_sub(DBL2NUM(M_PI / 2.0), ccmath_asin(obj, z));
}

.acosh(z) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'ext/ccmath/ccmath.c', line 282

static VALUE
ccmath_acosh(VALUE obj, VALUE z)
{
    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



270
271
272
273
274
275
276
277
278
279
280
# File 'ext/ccmath/ccmath.c', line 270

static VALUE
ccmath_asin(VALUE obj, VALUE z)
{
    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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'ext/ccmath/ccmath.c', line 252

static VALUE
ccmath_asinh(VALUE obj, VALUE z)
{
    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



323
324
325
326
327
328
329
330
331
332
333
# File 'ext/ccmath/ccmath.c', line 323

static VALUE
ccmath_atan(VALUE obj, VALUE z)
{
    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);
}

.atan2(y, x) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/ccmath.rb', line 9

def atan2(y,x)
  if y.real? and x.real?
    atan2!(y,x)
  else
    (-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y))
  end
end

.atanh(z) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/ccmath/ccmath.c', line 310

static VALUE
ccmath_atanh(VALUE obj, VALUE z)
{
    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);
}

.cbrt(z) ⇒ Object



5
6
7
# File 'lib/ccmath.rb', line 5

def cbrt(z)
  z ** (1.0/3)
end

.cos(z) ⇒ Object



204
205
206
207
208
209
210
211
# File 'ext/ccmath/ccmath.c', line 204

static VALUE
ccmath_cos(VALUE obj, VALUE z)
{
    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



228
229
230
231
232
233
234
235
# File 'ext/ccmath/ccmath.c', line 228

static VALUE
ccmath_cosh(VALUE obj, VALUE z)
{
    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));
}

.erfObject

.erfcObject

.exp(z) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'ext/ccmath/ccmath.c', line 125

static VALUE
ccmath_exp(VALUE obj, VALUE z)
{
    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));
}

.frexpObject

.gamma(z) ⇒ Object



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
# File 'ext/ccmath/ccmath.c', line 335

static VALUE
ccmath_gamma(VALUE obj, VALUE z)
{
    EXTRACT_DBLS(z);
    if (z_real < 0.5) {
        VALUE s1, s2;
        s1 = ccmath_gamma(obj, f_sub(DBL2NUM(1), z));
        s2 = ccmath_sin(obj, f_mul(DBL2NUM(M_PI), z));
        return f_div(DBL2NUM(M_PI), f_mul(s1, s2));
    }
    else {
        static const double lanczos_coef[] = {
            0.99999999999980993,
            676.5203681218851,
            -1259.1392167224028,
            771.32342877765313,
            -176.61502916214059,
            12.507343278686905,
            -0.13857109526572012,
            9.9843695780195716e-6,
            1.5056327351493116e-7
        };
        int i;
        double g, s, x_real, x_imag;

        g = 7.0;
        z_real -= 1;
        x_real = lanczos_coef[0];
        x_imag = 0.0;
        for(i=1; i<g+2; i++) {
            s = lanczos_coef[i] / ((z_real+i) * (z_real+i) + (z_imag * z_imag));
            x_real += s * (z_real+i);
            x_imag -= s * z_imag;
        }
        VALUE x = DBLS2COMP(x_real, x_imag);
        VALUE sqrt_2_pi = DBL2NUM(sqrt(2 * M_PI));
        VALUE t = DBLS2COMP(z_real + g + 0.5, z_imag);
        return f_mul(sqrt_2_pi,
               f_mul(f_pow(t, DBLS2COMP(z_real+0.5, z_imag)),
               f_mul(f_div(DBL2NUM(1), ccmath_exp(obj,t)), x)));

    }
}

.hypotObject

.ldexpObject

.lgammaObject

.log(*args) ⇒ Object

log(d * 2 ** numbits)



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
# File 'ext/ccmath/ccmath.c', line 162

static VALUE
ccmath_log(int argc, const VALUE* argv, VALUE obj)
{
    VALUE z, base;
    rb_scan_args(argc, argv, "11", &z, &base);

    EXTRACT_DBLS(z);
    float r = hypot(z_real, z_imag);
    if (argc == 2) {
        real_p(base);
        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 f_div(DBLS2COMP(log(r), m_atan2(z_imag, z_real)), ln_base);
        }
    }
    else {
        return DBLS2COMP(log(r), m_atan2(z_imag, z_real));
    }
}

.log10(z) ⇒ Object



196
197
198
199
200
201
202
# File 'ext/ccmath/ccmath.c', line 196

static VALUE
ccmath_log10(VALUE obj, VALUE z)
{
    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



188
189
190
191
192
193
194
# File 'ext/ccmath/ccmath.c', line 188

static VALUE
ccmath_log2(VALUE obj, VALUE z)
{
    EXTRACT_DBLS(z);
    float r = hypot(z_real, z_imag);
    return DBLS2COMP(log(r) / M_LN2, m_atan2(z_imag, z_real) / M_LN2);
}

.sin(z) ⇒ Object



213
214
215
216
217
218
219
220
# File 'ext/ccmath/ccmath.c', line 213

static VALUE
ccmath_sin(VALUE obj, VALUE z)
{
    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



237
238
239
240
241
242
243
244
# File 'ext/ccmath/ccmath.c', line 237

static VALUE
ccmath_sinh(VALUE obj, VALUE z)
{
    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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/ccmath/ccmath.c', line 107

static VALUE
ccmath_sqrt(VALUE obj, VALUE z)
{
    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



222
223
224
225
226
# File 'ext/ccmath/ccmath.c', line 222

static VALUE
ccmath_tan(VALUE obj, VALUE z)
{
    return f_div(ccmath_sin(obj, z), ccmath_cos(obj, z));
}

.tanh(z) ⇒ Object



246
247
248
249
250
# File 'ext/ccmath/ccmath.c', line 246

static VALUE
ccmath_tanh(VALUE obj, VALUE z)
{
    return f_div(ccmath_sinh(obj, z), ccmath_cosh(obj, z));
}