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



288
289
290
291
292
293
294
295
296
# File 'ext/ccmath/ccmath.c', line 288

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



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'ext/ccmath/ccmath.c', line 270

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



258
259
260
261
262
263
264
265
266
267
268
# File 'ext/ccmath/ccmath.c', line 258

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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'ext/ccmath/ccmath.c', line 240

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



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

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



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

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



192
193
194
195
196
197
198
199
# File 'ext/ccmath/ccmath.c', line 192

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



216
217
218
219
220
221
222
223
# File 'ext/ccmath/ccmath.c', line 216

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



113
114
115
116
117
118
119
120
121
# File 'ext/ccmath/ccmath.c', line 113

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



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

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)



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

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) {
        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 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



184
185
186
187
188
189
190
# File 'ext/ccmath/ccmath.c', line 184

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



176
177
178
179
180
181
182
# File 'ext/ccmath/ccmath.c', line 176

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



201
202
203
204
205
206
207
208
# File 'ext/ccmath/ccmath.c', line 201

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



225
226
227
228
229
230
231
232
# File 'ext/ccmath/ccmath.c', line 225

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/ccmath/ccmath.c', line 95

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



210
211
212
213
214
# File 'ext/ccmath/ccmath.c', line 210

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

.tanh(z) ⇒ Object



234
235
236
237
238
# File 'ext/ccmath/ccmath.c', line 234

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