2
complex.c: Coded by Tadayoshi Funaba 2008,2009
2
complex.c: Coded by Tadayoshi Funaba 2008-2011
4
4
This implementation is based on Keiju Ishitsuka's Complex library
5
5
which is written in ruby.
28
29
inline static VALUE \
29
30
f_##n(VALUE x, VALUE y)\
31
return rb_funcall(x, op, 1, y);\
32
return rb_funcall(x, (op), 1, y);\
169
if (TYPE(x) == T_STRING)
170
return rb_str_to_inum(x, 10, 0);
171
return rb_funcall(x, id_to_i, 0);
176
if (TYPE(x) == T_STRING)
177
return DBL2NUM(rb_str_to_dbl(x, 0));
178
return rb_funcall(x, id_to_f, 0);
496
#define m_hypot(x,y) m_hypot_bang(x,y)
498
extern VALUE rb_math_log(int argc, VALUE *argv);
508
#define m_hypot(x,y) m_hypot_bang((x),(y))
501
511
m_log_bang(VALUE x)
576
586
* Complex.polar(abs[, arg]) -> complex
578
588
* Returns a complex object which denotes the given polar form.
590
* Complex.polar(3, 0) #=> (3.0+0.0i)
591
* Complex.polar(3, Math::PI/2) #=> (1.836909530733566e-16+3.0i)
592
* Complex.polar(3, Math::PI) #=> (-3.0+3.673819061467132e-16i)
593
* Complex.polar(3, -Math::PI/2) #=> (1.836909530733566e-16-3.0i)
581
596
nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
825
840
nucomp_expt(VALUE self, VALUE other)
827
if (k_exact_zero_p(other))
842
if (k_numeric_p(other) && k_exact_zero_p(other))
828
843
return f_complex_new_bang1(CLASS_OF(self), ONE);
830
845
if (k_rational_p(other) && f_one_p(f_denominator(other)))
1074
extern VALUE rb_lcm(VALUE x, VALUE y);
1078
1094
* cmp.denominator -> integer
1080
* Returns the denominator (lcm of both denominator, real and imag).
1096
* Returns the denominator (lcm of both denominator - real and imag).
1082
1098
* See numerator.
1339
1360
* cmp.rationalize([eps]) -> rational
1341
* Returns the value as a rational if possible. An optional argument
1342
* eps is always ignored.
1362
* If the imaginary part is exactly 0, returns the real part as a Rational,
1363
* otherwise a RangeError is raised.
1345
1366
nucomp_rationalize(int argc, VALUE *argv, VALUE self)
1347
1370
rb_scan_args(argc, argv, "01", NULL);
1348
return nucomp_to_r(self);
1372
if (k_inexact_p(dat->imag) || f_nonzero_p(dat->imag)) {
1373
VALUE s = f_to_s(self);
1374
rb_raise(rb_eRangeError, "can't convert %s into Rational",
1377
return rb_funcall(dat->real, rb_intern("rationalize"), argc, argv);
1424
1453
#define id_match rb_intern("match")
1425
#define f_match(x,y) rb_funcall(x, id_match, 1, y)
1427
#define id_aref rb_intern("[]")
1428
#define f_aref(x,y) rb_funcall(x, id_aref, 1, y)
1430
#define id_post_match rb_intern("post_match")
1431
#define f_post_match(x) rb_funcall(x, id_post_match, 0)
1433
#define id_split rb_intern("split")
1434
#define f_split(x,y) rb_funcall(x, id_split, 1, y)
1436
#define id_include_p rb_intern("include?")
1437
#define f_include_p(x,y) rb_funcall(x, id_include_p, 1, y)
1439
#define id_count rb_intern("count")
1440
#define f_count(x,y) rb_funcall(x, id_count, 1, y)
1454
#define f_match(x,y) rb_funcall((x), id_match, 1, (y))
1442
1456
#define id_gsub_bang rb_intern("gsub!")
1443
#define f_gsub_bang(x,y,z) rb_funcall(x, id_gsub_bang, 2, y, z)
1457
#define f_gsub_bang(x,y,z) rb_funcall((x), id_gsub_bang, 2, (y), (z))
1446
1460
string_to_c_internal(VALUE self)
1459
1473
m = f_match(comp_pat0, s);
1460
1474
if (!NIL_P(m)) {
1461
sr = f_aref(m, INT2FIX(1));
1462
si = f_aref(m, INT2FIX(2));
1463
re = f_post_match(m);
1475
sr = rb_reg_nth_match(1, m);
1476
si = rb_reg_nth_match(2, m);
1477
re = rb_reg_match_post(m);
1466
1480
if (NIL_P(m)) {
1467
1481
m = f_match(comp_pat1, s);
1468
1482
if (!NIL_P(m)) {
1470
si = f_aref(m, INT2FIX(1));
1484
si = rb_reg_nth_match(1, m);
1472
1486
si = rb_usascii_str_new2("");
1476
t = f_aref(m, INT2FIX(2));
1490
t = rb_reg_nth_match(2, m);
1478
1492
t = rb_usascii_str_new2("1");
1479
1493
rb_str_concat(si, t);
1481
re = f_post_match(m);
1495
re = rb_reg_match_post(m);
1486
1500
m = f_match(comp_pat2, s);
1488
1502
return rb_assoc_new(Qnil, self);
1489
sr = f_aref(m, INT2FIX(1));
1490
if (NIL_P(f_aref(m, INT2FIX(2))))
1503
sr = rb_reg_nth_match(1, m);
1504
if (NIL_P(rb_reg_nth_match(2, m)))
1495
si = f_aref(m, INT2FIX(3));
1496
t = f_aref(m, INT2FIX(4));
1509
si = rb_reg_nth_match(3, m);
1510
t = rb_reg_nth_match(4, m);
1498
1512
t = rb_usascii_str_new2("1");
1499
1513
rb_str_concat(si, t);
1501
re = f_post_match(m);
1515
re = rb_reg_match_post(m);
1504
1518
r = INT2FIX(0);
1505
1519
i = INT2FIX(0);
1506
1520
if (!NIL_P(sr)) {
1507
if (f_include_p(sr, a_slash))
1521
if (strchr(RSTRING_PTR(sr), '/'))
1508
1522
r = f_to_r(sr);
1509
else if (f_gt_p(f_count(sr, a_dot_and_an_e), INT2FIX(0)))
1523
else if (strpbrk(RSTRING_PTR(sr), ".eE"))
1510
1524
r = f_to_f(sr);
1512
1526
r = f_to_i(sr);
1514
1528
if (!NIL_P(si)) {
1515
if (f_include_p(si, a_slash))
1529
if (strchr(RSTRING_PTR(si), '/'))
1516
1530
i = f_to_r(si);
1517
else if (f_gt_p(f_count(si, a_dot_and_an_e), INT2FIX(0)))
1531
else if (strpbrk(RSTRING_PTR(si), ".eE"))
1518
1532
i = f_to_f(si);
1520
1534
i = f_to_i(si);
1541
1555
#define id_gsub rb_intern("gsub")
1542
#define f_gsub(x,y,z) rb_funcall(x, id_gsub, 2, y, z)
1556
#define f_gsub(x,y,z) rb_funcall((x), id_gsub, 2, (y), (z))
1646
1660
if (argc == 1) {
1647
1661
if (k_numeric_p(a1) && !f_real_p(a1))
1649
/* expect raise exception for consistency */
1663
/* should raise exception for consistency */
1650
1664
if (!k_numeric_p(a1))
1651
1665
return rb_convert_type(a1, T_COMPLEX, "Complex", "to_c");