3 class ComplexSub < Complex; end
5 class Complex_Test < Test::Unit::TestCase
8 c = ComplexSub.__send__(:new, 1)
9 cc = ComplexSub.__send__(:convert, 1)
10 if defined?(ComplexSub::Unify)
11 assert_instance_of(Fixnum, c)
12 assert_instance_of(Fixnum, cc)
14 assert_instance_of(ComplexSub, c)
15 assert_instance_of(ComplexSub, cc)
18 assert_instance_of(ComplexSub, c2)
20 assert_instance_of(ComplexSub, c2)
23 assert_instance_of(ComplexSub, c3)
28 assert_instance_of(ComplexSub, c5)
38 assert_equal(true, c.eql?(c2))
39 assert_equal(false, c.eql?(c3))
41 if defined?(Complex::Unify)
42 assert_equal(true, c.eql?(0))
44 assert_equal(false, c.eql?(0))
49 assert_instance_of(Fixnum, Complex(1,2).hash)
57 assert_equal(4, h.size)
58 assert_equal(2, h[Complex(1,0)])
61 assert_equal(4, h.size)
67 unless defined?(Complex::Unify)
68 assert_equal(true, c.frozen?)
70 assert_instance_of(String, c.to_s)
73 def test_new_bang # no unify
74 assert_instance_of(Complex, Complex.__send__(:new!, 2,0))
75 assert_equal([2,0], Complex.__send__(:new!, 2,0).
76 instance_eval{[real, image]})
77 assert_equal([2,4], Complex.__send__(:new!, 2,4).
78 instance_eval{[real, image]})
79 assert_equal([-2,4], Complex.__send__(:new!, -2,4).
80 instance_eval{[real, image]})
81 assert_equal([2,-4], Complex.__send__(:new!, 2,-4).
82 instance_eval{[real, image]})
83 assert_equal([-2,-4], Complex.__send__(:new!, -2,-4).
84 instance_eval{[real, image]})
86 assert_equal([2,0], Complex.__send__(:new!, Complex(2)).
87 instance_eval{[real, image]})
88 assert_equal([2,3], Complex.__send__(:new!, Complex(2), Complex(3)).
89 instance_eval{[real, image]})
90 assert_equal([2,3], Complex.__send__(:new!, 2, Complex(3)).
91 instance_eval{[real, image]})
93 assert_equal([1.1,0], Complex.__send__(:new!, 1.1).
94 instance_eval{[real, image]})
95 assert_equal([-1.1,0], Complex.__send__(:new!, -1.1).
96 instance_eval{[real, image]})
97 assert_equal([1,0], Complex.__send__(:new!, '1').
98 instance_eval{[real, image]})
99 assert_equal([0,0], Complex.__send__(:new!, nil).
100 instance_eval{[real, image]})
104 if defined?(Complex::Unify)
105 assert_instance_of(Fixnum, Complex.__send__(:new, 2,0))
107 assert_instance_of(Complex, Complex.__send__(:new, 2,0))
108 assert_equal([2,0], Complex.__send__(:new, 2,0). instance_eval{[real, image]})
110 assert_equal([2,4], Complex.__send__(:new, 2,4).instance_eval{[real, image]})
111 assert_equal([-2,4], Complex.__send__(:new, -2,4).instance_eval{[real, image]})
112 assert_equal([2,-4], Complex.__send__(:new, 2,-4).instance_eval{[real, image]})
113 assert_equal([-2,-4], Complex.__send__(:new, -2,-4).instance_eval{[real, image]})
115 assert_raise(ArgumentError){Complex.__send__(:new, Complex(1,2),2)}
116 assert_raise(ArgumentError){Complex.__send__(:new, 2,Complex(1,2))}
117 assert_raise(ArgumentError){Complex.__send__(:new, Complex(1,2),Complex(1,2))}
119 assert_raise(ArgumentError){Complex.__send__(:new, '1')}
120 assert_raise(ArgumentError){Complex.__send__(:new, nil)}
122 assert_raise(ArgumentError){Complex.__send__(:new, Complex(1))}
128 assert_equal(Complex.__send__(:new, 0,0), c)
130 c = Complex(2**32, 2**32)
131 assert_equal(Complex.__send__(:new, 2**32,2**32), c)
132 assert_equal([2**32,2**32], [c.real,c.image])
134 c = Complex(-2**32, 2**32)
135 assert_equal(Complex.__send__(:new, -2**32,2**32), c)
136 assert_equal([-2**32,2**32], [c.real,c.image])
138 c = Complex(2**32, -2**32)
139 assert_equal(Complex.__send__(:new, 2**32,-2**32), c)
140 assert_equal([2**32,-2**32], [c.real,c.image])
142 c = Complex(-2**32, -2**32)
143 assert_equal(Complex.__send__(:new, -2**32,-2**32), c)
144 assert_equal([-2**32,-2**32], [c.real,c.image])
146 c = Complex(Complex(1),0)
147 assert_equal(Complex.__send__(:new, 1,0), c)
149 c = Complex(0,Complex(1))
150 assert_equal(Complex.__send__(:new, 0,1), c)
153 assert_equal(Complex.__send__(:new, 5,0), c)
156 assert_equal(Complex.__send__(:new, 1,2), c)
159 assert_equal(Complex.__send__(:new, 0,5), c)
162 assert_equal(Complex.__send__(:new, 0,2), c)
163 assert_raise(ArgumentError){Complex(1,2).im}
166 assert_equal(Complex.__send__(:new, 0,1), c)
168 assert_equal(Complex.__send__(:new, 1),Complex(1))
169 assert_equal(Complex.__send__(:new, 1),Complex('1'))
170 assert_raise(ArgumentError){Complex(nil)}
176 assert_equal(4, c.real)
177 assert_equal(0, c.image)
181 assert_equal(4, c.real)
182 assert_equal(5, c.image)
184 c = Complex(-0.0,-0.0)
186 assert_equal('-0.0', c.real.to_s)
187 assert_equal('-0.0', c.image.to_s)
189 c = Complex.__send__(:new, 4)
191 assert_equal(4, c.real)
192 assert_equal(0, c.image)
193 assert_equal(c.imag, c.image)
195 c = Complex.__send__(:new, 4,5)
197 assert_equal(4, c.real)
198 assert_equal(5, c.image)
199 assert_equal(c.imag, c.image)
201 c = Complex.__send__(:new, -0.0,-0.0)
203 assert_equal('-0.0', c.real.to_s)
204 assert_equal('-0.0', c.image.to_s)
205 assert_equal(c.imag.to_s, c.image.to_s)
207 c = Complex.__send__(:new!, 4)
209 assert_equal(4, c.real)
210 assert_equal(c.imag, c.image)
211 assert_equal(0, c.image)
213 c = Complex.__send__(:new!, 4,5)
215 assert_equal(4, c.real)
216 assert_equal(5, c.image)
217 assert_equal(c.imag, c.image)
219 c = Complex.__send__(:new!, -0.0,-0.0)
221 assert_equal('-0.0', c.real.to_s)
222 assert_equal('-0.0', c.image.to_s)
223 assert_equal(c.imag.to_s, c.image.to_s)
229 if defined?(Complex::Unify)
230 assert_equal(true, c.scalar?)
232 assert_equal(true, c.finite?)
233 assert_equal(false, c.infinite?)
234 assert_equal(false, c.nan?)
235 assert_equal(true, c.integer?)
236 assert_equal(false, c.float?)
237 assert_equal(true, c.rational?)
238 assert_equal(true, c.real?)
239 assert_equal(false, c.complex?)
240 assert_equal(true, c.exact?)
241 assert_equal(false, c.inexact?)
244 assert_equal(false, c.scalar?)
246 assert_equal(true, c.finite?)
247 assert_equal(false, c.infinite?)
248 assert_equal(false, c.nan?)
249 assert_equal(false, c.integer?)
250 assert_equal(false, c.float?)
251 assert_equal(false, c.rational?)
252 assert_equal(false, c.real?)
253 assert_equal(true, c.complex?)
254 assert_equal(true, c.exact?)
255 assert_equal(false, c.inexact?)
260 assert_equal(0, Complex(0).sign)
261 assert_equal(1, Complex(2).sign)
262 assert_equal(-1, Complex(-2).sign)
265 assert_equal(true, Complex(0).zero?)
266 assert_equal(true, Complex(0,0).zero?)
267 assert_equal(false, Complex(1,0).zero?)
268 assert_equal(false, Complex(0,1).zero?)
269 assert_equal(false, Complex(1,1).zero?)
271 assert_equal(nil, Complex(0).nonzero?)
272 assert_equal(nil, Complex(0,0).nonzero?)
273 assert_equal(Complex(1,0), Complex(1,0).nonzero?)
274 assert_equal(Complex(0,1), Complex(0,1).nonzero?)
275 assert_equal(Complex(1,1), Complex(1,1).nonzero?)
279 assert_equal(Complex(1), +Complex(1))
280 assert_equal(Complex(-1), +Complex(-1))
281 assert_equal(Complex(1,1), +Complex(1,1))
282 assert_equal(Complex(-1,1), +Complex(-1,1))
283 assert_equal(Complex(1,-1), +Complex(1,-1))
284 assert_equal(Complex(-1,-1), +Complex(-1,-1))
288 assert_equal(Complex(-1), -Complex(1))
289 assert_equal(Complex(1), -Complex(-1))
290 assert_equal(Complex(-1,-1), -Complex(1,1))
291 assert_equal(Complex(1,-1), -Complex(-1,1))
292 assert_equal(Complex(-1,1), -Complex(1,-1))
293 assert_equal(Complex(1,1), -Complex(-1,-1))
296 assert_equal(0, Complex(0).negate)
297 assert_equal(-2, Complex(2).negate)
298 assert_equal(2, Complex(-2).negate)
306 assert_equal(Complex(3,5), c + c2)
308 assert_equal(Complex(3,2), c + 2)
309 assert_equal(Complex(3.0,2), c + 2.0)
311 if defined?(Rational)
312 assert_equal(Complex(Rational(3,1),Rational(2)), c + Rational(2))
313 assert_equal(Complex(Rational(5,3),Rational(2)), c + Rational(2,3))
321 assert_equal(Complex(-1,-1), c - c2)
323 assert_equal(Complex(-1,2), c - 2)
324 assert_equal(Complex(-1.0,2), c - 2.0)
326 if defined?(Rational)
327 assert_equal(Complex(Rational(-1,1),Rational(2)), c - Rational(2))
328 assert_equal(Complex(Rational(1,3),Rational(2)), c - Rational(2,3))
336 assert_equal(Complex(-4,7), c * c2)
338 assert_equal(Complex(2,4), c * 2)
339 assert_equal(Complex(2.0,4.0), c * 2.0)
341 if defined?(Rational)
342 assert_equal(Complex(Rational(2,1),Rational(4)), c * Rational(2))
343 assert_equal(Complex(Rational(2,3),Rational(4,3)), c * Rational(2,3))
352 if defined?(Complex::Unify)
353 assert_equal(Complex(Rational(8,13),Rational(1,13)), c / c2)
355 assert_equal(Complex(0,0), c / c2)
359 c2 = Complex(2.0,3.0)
362 assert_in_delta(0.615, r.real, 0.001)
363 assert_in_delta(0.076, r.image, 0.001)
368 if defined?(Complex::Unify)
369 assert_equal(Complex(Rational(1,2),1), c / 2)
371 assert_equal(Complex(0,1), c / 2)
373 assert_equal(Complex(0.5,1.0), c / 2.0)
375 if defined?(Rational)
376 assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
377 assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
385 if defined?(Rational)
386 assert_equal(Complex(Rational(8,13),Rational(1,13)), c.quo(c2))
389 assert_in_delta(0.615, r.real, 0.001)
390 assert_in_delta(0.076, r.image, 0.001)
394 c2 = Complex(2.0,3.0)
397 assert_in_delta(0.615, r.real, 0.001)
398 assert_in_delta(0.076, r.image, 0.001)
403 if defined?(Rational)
404 assert_equal(Complex(Rational(1,2),1), c.quo(2))
406 assert_equal(Complex(0.5,1.0), c.quo(2))
408 assert_equal(Complex(0.5,1.0), c.quo(2.0))
410 if defined?(Rational)
411 assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
412 assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
421 assert_in_delta(0.615, r.real, 0.001)
422 assert_in_delta(0.076, r.image, 0.001)
425 c2 = Complex(2.0,3.0)
428 assert_in_delta(0.615, r.real, 0.001)
429 assert_in_delta(0.076, r.image, 0.001)
434 assert_equal(Complex(0.5,1.0), c.fdiv(2))
435 assert_equal(Complex(0.5,1.0), c.fdiv(2.0))
443 assert_in_delta(-0.015, r.real, 0.001)
444 assert_in_delta(-0.179, r.image, 0.001)
446 assert_equal(Complex(-3,4), c ** 2)
447 if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
448 assert_equal(Complex(Rational(-3,25),Rational(-4,25)), c ** -2)
451 assert_in_delta(-0.12, r.real, 0.001)
452 assert_in_delta(-0.16, r.image, 0.001)
455 assert_in_delta(-3.0, r.real, 0.001)
456 assert_in_delta(4.0, r.image, 0.001)
459 assert_in_delta(-0.12, r.real, 0.001)
460 assert_in_delta(-0.16, r.image, 0.001)
462 if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
463 assert_equal(Complex(-3,4), c ** Rational(2))
465 assert_equal(Complex(Rational(-3,25),Rational(-4,25)),
466 c ** Rational(-2)) # why failed?
469 r = c ** Rational(2,3)
470 assert_in_delta(1.264, r.real, 0.001)
471 assert_in_delta(1.150, r.image, 0.001)
473 r = c ** Rational(-2,3)
474 assert_in_delta(0.432, r.real, 0.001)
475 assert_in_delta(-0.393, r.image, 0.001)
480 assert_raise(NoMethodError){1 <=> Complex(1,1)}
481 assert_raise(NoMethodError){Complex(1,1) <=> 1}
482 assert_raise(NoMethodError){Complex(1,1) <=> Complex(1,1)}
486 assert(Complex(1,0) == Complex(1))
487 assert(Complex(1,0) == Complex.__send__(:new, 1))
488 assert(Complex(1,0) == Complex.__send__(:new, 1,0))
489 assert(Complex(1,0) == Complex.__send__(:new!, 1))
490 assert(Complex(1,0) == Complex.__send__(:new!, 1,0))
492 assert(Complex(-1,0) == Complex(-1))
493 assert(Complex(-1,0) == Complex.__send__(:new, -1))
494 assert(Complex(-1,0) == Complex.__send__(:new, -1,0))
495 assert(Complex(-1,0) == Complex.__send__(:new!, -1))
496 assert(Complex(-1,0) == Complex.__send__(:new!, -1,0))
498 assert_equal(false, Complex(2,1) == Complex(1))
499 assert_equal(true, Complex(2,1) != Complex(1))
500 assert_equal(false, Complex(1) == nil)
501 assert_equal(false, Complex(1) == '')
507 assert_in_delta(2.236, c.abs, 0.001)
508 assert_equal(5, c.abs2)
510 assert_equal(c.abs, Math.sqrt(c * c.conj))
511 assert_equal(c.abs, Math.sqrt(c.real**2 + c.image**2))
512 assert_equal(c.abs2, c * c.conj)
513 assert_equal(c.abs2, c.real**2 + c.image**2)
515 assert_in_delta(1.107, c.arg, 0.001)
516 assert_in_delta(1.107, c.angle, 0.001)
519 assert_in_delta(2.236, r[0], 0.001)
520 assert_in_delta(1.107, r[1], 0.001)
521 assert_equal(Complex(1,-2), c.conjugate)
522 assert_equal(Complex(1,-2), c.conj)
523 # assert_equal(Complex(1,-2), ~c)
524 # assert_equal(5, c * ~c)
526 assert_equal(Complex(1,2), c.numerator)
527 assert_equal(1, c.denominator)
533 assert_instance_of(String, c.to_s)
534 assert_equal('1+2i', c.to_s)
536 assert_equal('2i', Complex(0,2).to_s)
537 assert_equal('-2i', Complex(0,-2).to_s)
538 assert_equal('1+2i', Complex(1,2).to_s)
539 assert_equal('-1+2i', Complex(-1,2).to_s)
540 assert_equal('-1-2i', Complex(-1,-2).to_s)
541 assert_equal('1-2i', Complex(1,-2).to_s)
542 assert_equal('-1-2i', Complex(-1,-2).to_s)
544 assert_equal('2.0i', Complex(0,2.0).to_s)
545 assert_equal('-2.0i', Complex(0,-2.0).to_s)
546 assert_equal('1.0+2.0i', Complex(1.0,2.0).to_s)
547 assert_equal('-1.0+2.0i', Complex(-1.0,2.0).to_s)
548 assert_equal('-1.0-2.0i', Complex(-1.0,-2.0).to_s)
549 assert_equal('1.0-2.0i', Complex(1.0,-2.0).to_s)
550 assert_equal('-1.0-2.0i', Complex(-1.0,-2.0).to_s)
552 if defined?(Rational)
553 assert_equal('2i', Complex(0,Rational(2)).to_s)
554 assert_equal('-2i', Complex(0,Rational(-2)).to_s)
555 assert_equal('1+2i', Complex(1,Rational(2)).to_s)
556 assert_equal('-1+2i', Complex(-1,Rational(2)).to_s)
557 assert_equal('-1-2i', Complex(-1,Rational(-2)).to_s)
558 assert_equal('1-2i', Complex(1,Rational(-2)).to_s)
559 assert_equal('-1-2i', Complex(-1,Rational(-2)).to_s)
561 assert_equal('(2/3)i', Complex(0,Rational(2,3)).to_s)
562 assert_equal('(-2/3)i', Complex(0,Rational(-2,3)).to_s)
563 assert_equal('1+(2/3)i', Complex(1,Rational(2,3)).to_s)
564 assert_equal('-1+(2/3)i', Complex(-1,Rational(2,3)).to_s)
565 assert_equal('-1-(2/3)i', Complex(-1,Rational(-2,3)).to_s)
566 assert_equal('1-(2/3)i', Complex(1,Rational(-2,3)).to_s)
567 assert_equal('-1-(2/3)i', Complex(-1,Rational(-2,3)).to_s)
574 assert_instance_of(String, c.inspect)
575 assert_equal('Complex(1, 2)', c.inspect)
584 assert_instance_of(Complex, c2)
586 if defined?(Rational)
587 c = Complex(Rational(1,2),Rational(2,3))
592 assert_instance_of(Complex, c2)
597 assert_equal(Complex(0), ''.to_c)
598 assert_equal(Complex(0), ' '.to_c)
599 assert_equal(Complex(5), '5'.to_c)
600 assert_equal(Complex(-5), '-5'.to_c)
601 assert_equal(Complex(5,3), '5+3i'.to_c)
602 assert_equal(Complex(-5,3), '-5+3i'.to_c)
603 assert_equal(Complex(5,-3), '5-3i'.to_c)
604 assert_equal(Complex(-5,-3), '-5-3i'.to_c)
605 assert_equal(Complex(0,3), '3i'.to_c)
606 assert_equal(Complex(0,-3), '-3i'.to_c)
608 assert_equal(Complex(5,3), '5+3I'.to_c)
609 assert_equal(Complex(5,3), '5+3j'.to_c)
610 assert_equal(Complex(5,3), '5+3J'.to_c)
611 assert_equal(Complex(0,3), '3I'.to_c)
612 assert_equal(Complex(0,3), '3j'.to_c)
613 assert_equal(Complex(0,3), '3J'.to_c)
615 assert_equal(Complex(5.0), '5.0'.to_c)
616 assert_equal(Complex(-5.0), '-5.0'.to_c)
617 assert_equal(Complex(5.0,3.0), '5.0+3.0i'.to_c)
618 assert_equal(Complex(-5.0,3.0), '-5.0+3.0i'.to_c)
619 assert_equal(Complex(5.0,-3.0), '5.0-3.0i'.to_c)
620 assert_equal(Complex(-5.0,-3.0), '-5.0-3.0i'.to_c)
621 assert_equal(Complex(0.0,3.0), '3.0i'.to_c)
622 assert_equal(Complex(0.0,-3.0), '-3.0i'.to_c)
624 assert_equal(Complex(5.0), '5e0'.to_c)
625 assert_equal(Complex(-5.0), '-5e0'.to_c)
626 assert_equal(Complex(5.0,3.0), '5e0+3e0i'.to_c)
627 assert_equal(Complex(-5.0,3.0), '-5e0+3e0i'.to_c)
628 assert_equal(Complex(5.0,-3.0), '5e0-3e0i'.to_c)
629 assert_equal(Complex(-5.0,-3.0), '-5e0-3e0i'.to_c)
630 assert_equal(Complex(0.0,3.0), '3e0i'.to_c)
631 assert_equal(Complex(0.0,-3.0), '-3e0i'.to_c)
633 assert_equal(Complex(5), Complex('5'))
634 assert_equal(Complex(-5), Complex('-5'))
635 assert_equal(Complex(5,3), Complex('5+3i'))
636 assert_equal(Complex(-5,3), Complex('-5+3i'))
637 assert_equal(Complex(5,-3), Complex('5-3i'))
638 assert_equal(Complex(-5,-3), Complex('-5-3i'))
639 assert_equal(Complex(0,3), Complex('3i'))
640 assert_equal(Complex(0,-3), Complex('-3i'))
642 assert_equal(Complex(5,3), Complex('5+3I'))
643 assert_equal(Complex(5,3), Complex('5+3j'))
644 assert_equal(Complex(5,3), Complex('5+3J'))
645 assert_equal(Complex(0,3), Complex('3I'))
646 assert_equal(Complex(0,3), Complex('3j'))
647 assert_equal(Complex(0,3), Complex('3J'))
649 assert_equal(Complex(5.0), Complex('5.0'))
650 assert_equal(Complex(-5.0), Complex('-5.0'))
651 assert_equal(Complex(5.0,3.0), Complex('5.0+3.0i'))
652 assert_equal(Complex(-5.0,3.0), Complex('-5.0+3.0i'))
653 assert_equal(Complex(5.0,-3.0), Complex('5.0-3.0i'))
654 assert_equal(Complex(-5.0,-3.0), Complex('-5.0-3.0i'))
655 assert_equal(Complex(0.0,3.0), Complex('3.0i'))
656 assert_equal(Complex(0.0,-3.0), Complex('-3.0i'))
658 assert_equal(Complex(5.0), Complex('5e0'))
659 assert_equal(Complex(-5.0), Complex('-5e0'))
660 assert_equal(Complex(5.0,3.0), Complex('5e0+3e0i'))
661 assert_equal(Complex(-5.0,3.0), Complex('-5e0+3e0i'))
662 assert_equal(Complex(5.0,-3.0), Complex('5e0-3e0i'))
663 assert_equal(Complex(-5.0,-3.0), Complex('-5e0-3e0i'))
664 assert_equal(Complex(0.0,3.0), Complex('3e0i'))
665 assert_equal(Complex(0.0,-3.0), Complex('-3e0i'))
667 assert_equal(Complex(0), '_'.to_c)
668 assert_equal(Complex(0), '_5'.to_c)
669 assert_equal(Complex(5), '5_'.to_c)
670 assert_equal(Complex(5), '5x'.to_c)
671 assert_equal(Complex(5), '5+_3i'.to_c)
672 assert_equal(Complex(5), '5+3_i'.to_c)
673 assert_equal(Complex(5,3), '5+3i_'.to_c)
674 assert_equal(Complex(5,3), '5+3ix'.to_c)
675 assert_raise(ArgumentError){ Complex('')}
676 assert_raise(ArgumentError){ Complex('_')}
677 assert_raise(ArgumentError){ Complex('_5')}
678 assert_raise(ArgumentError){ Complex('5_')}
679 assert_raise(ArgumentError){ Complex('5x')}
680 assert_raise(ArgumentError){ Complex('5+_3i')}
681 assert_raise(ArgumentError){ Complex('5+3_i')}
682 assert_raise(ArgumentError){ Complex('5+3i_')}
683 assert_raise(ArgumentError){ Complex('5+3ix')}
685 if defined?(Rational) && defined?(''.to_r)
686 assert_equal(Complex(Rational(1,5)), '1/5'.to_c)
687 assert_equal(Complex(Rational(-1,5)), '-1/5'.to_c)
688 assert_equal(Complex(Rational(1,5),3), '1/5+3i'.to_c)
689 assert_equal(Complex(Rational(1,5),-3), '1/5-3i'.to_c)
690 assert_equal(Complex(Rational(-1,5),3), '-1/5+3i'.to_c)
691 assert_equal(Complex(Rational(-1,5),-3), '-1/5-3i'.to_c)
692 assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+3/2i'.to_c)
693 assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-3/2i'.to_c)
694 assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+3/2i'.to_c)
695 assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-3/2i'.to_c)
696 assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+(3/2)i'.to_c)
697 assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-(3/2)i'.to_c)
698 assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+(3/2)i'.to_c)
699 assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-(3/2)i'.to_c)
702 assert_equal(Complex(5, 3), Complex('5', '3'))
707 assert_equal(false, c.respond_to?(:<))
708 assert_equal(false, c.respond_to?(:<=))
709 assert_equal(false, c.respond_to?(:<=>))
710 assert_equal(false, c.respond_to?(:>))
711 assert_equal(false, c.respond_to?(:>=))
712 assert_equal(false, c.respond_to?(:between?))
713 # assert_equal(false, c.respond_to?(:div)) # ?
714 assert_equal(false, c.respond_to?(:divmod))
715 assert_equal(false, c.respond_to?(:floor))
716 assert_equal(false, c.respond_to?(:ceil))
717 assert_equal(false, c.respond_to?(:modulo))
718 assert_equal(false, c.respond_to?(:round))
719 assert_equal(false, c.respond_to?(:step))
720 assert_equal(false, c.respond_to?(:tunrcate))
722 assert_equal(false, c.respond_to?(:positive?))
723 assert_equal(false, c.respond_to?(:negative?))
724 # assert_equal(false, c.respond_to?(:sign))
726 assert_equal(false, c.respond_to?(:quotient))
727 assert_equal(false, c.respond_to?(:quot))
728 assert_equal(false, c.respond_to?(:quotrem))
730 assert_equal(false, c.respond_to?(:gcd))
731 assert_equal(false, c.respond_to?(:lcm))
732 assert_equal(false, c.respond_to?(:gcdlcm))
736 assert_equal(3, Complex(3).to_i)
737 assert_equal(3, Integer(Complex(3)))
738 assert_raise(RangeError){Complex(3,2).to_i}
739 assert_raise(RangeError){Integer(Complex(3,2))}
743 assert_equal(3.0, Complex(3).to_f)
744 assert_equal(3.0, Float(Complex(3)))
745 assert_raise(RangeError){Complex(3,2).to_f}
746 assert_raise(RangeError){Float(Complex(3,2))}
750 if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
751 assert_equal(Rational(3), Complex(3).to_r)
752 assert_equal(Rational(3), Rational(Complex(3)))
753 assert_raise(RangeError){Complex(3,2).to_r}
754 assert_raise(RangeError){Rational(Complex(3,2))}
760 assert_equal([0,0] , [c.real, c.image])
763 assert_equal([0,0] , [c.real, c.image])
766 assert_equal([1,0] , [c.real, c.image])
769 assert_equal([1.1, 0], [c.real, c.image])
771 if defined?(Rational)
772 c = Rational(1,2).to_c
773 assert_equal([Rational(1,2), 0], [c.real, c.image])
776 c = Complex(1,2).to_c
777 assert_equal([1, 2], [c.real, c.image])
781 assert_equal(nil, Complex < Precision)
785 assert_equal(true, 1.scalar?)
786 assert_equal(true, 1.1.scalar?)
788 assert_equal(1, 1.real)
789 assert_equal(0, 1.image)
790 assert_equal(0, 1.imag)
792 assert_equal(1.1, 1.1.real)
793 assert_equal(0, 1.1.image)
794 assert_equal(0, 1.1.imag)
796 assert_equal(0, 1.arg)
797 assert_equal(0, 1.angle)
799 assert_equal(0, 1.0.arg)
800 assert_equal(0, 1.0.angle)
802 assert_equal(Math::PI, -1.arg)
803 assert_equal(Math::PI, -1.angle)
805 assert_equal(Math::PI, -1.0.arg)
806 assert_equal(Math::PI, -1.0.angle)
808 assert_equal([1,0], 1.polar)
809 assert_equal([1, Math::PI], -1.polar)
811 assert_equal([1.0,0], 1.0.polar)
812 assert_equal([1.0, Math::PI], -1.0.polar)
814 assert_equal(1, 1.conjugate)
815 assert_equal(-1, -1.conjugate)
816 assert_equal(1, 1.conj)
817 assert_equal(-1, -1.conj)
819 assert_equal(1.1, 1.1.conjugate)
820 assert_equal(-1.1, -1.1.conjugate)
821 assert_equal(1.1, 1.1.conj)
822 assert_equal(-1.1, -1.1.conj)
824 assert_equal(1, 1.numerator)
825 assert_equal(9, 9.numerator)
826 assert_equal(1, 1.denominator)
827 assert_equal(1, 9.denominator)
829 if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
830 assert_equal(1.0, 1.0.numerator)
831 assert_equal(9.0, 9.0.numerator)
832 assert_equal(1.0, 1.0.denominator)
833 assert_equal(1.0, 9.0.denominator)
837 if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
838 assert_equal(Rational(1,9), 9.reciprocal)
839 assert_equal(Rational(1,9), 9.0.reciprocal)
840 assert_equal(Rational(1,9), 9.inverse)
841 assert_equal(Rational(1,9), 9.0.inverse)
845 if defined?(Rational)
846 assert_equal(Rational(1,2), 1.quo(2))
847 assert_equal(Rational(5000000000), 10000000000.quo(2))
848 assert_equal(0.5, 1.0.quo(2))
849 assert_equal(Rational(1,4), Rational(1,2).quo(2))
850 assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
852 assert_equal(0.5, 1.quo(2))
853 assert_equal(5000000000.0, 10000000000.quo(2))
854 assert_equal(0.5, 1.0.quo(2))
855 assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2))
858 if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
859 assert_equal(Rational(1,2), 1.quo(2))
860 assert_equal(Rational(5000000000), 10000000000.quo(2))
861 assert_equal(Rational(1,2), 1.0.quo(2))
862 assert_equal(Rational(1,4), Rational(1,2).quo(2))
863 assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
866 assert_equal(0.5, 1.fdiv(2))
867 assert_equal(5000000000.0, 10000000000.fdiv(2))
868 assert_equal(0.5, 1.0.fdiv(2))
869 if defined?(Rational)
870 assert_equal(0.25, Rational(1,2).fdiv(2))
872 assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2))
874 unless $".grep(/(\A|\/)complex/).empty?
875 assert_equal(Complex(0,2), Math.sqrt(-4.0))
876 # assert_equal(true, Math.sqrt(-4.0).inexact?)
877 assert_equal(Complex(0,2), Math.sqrt(-4))
878 # assert_equal(true, Math.sqrt(-4).exact?)
879 if defined?(Rational)
880 assert_equal(Complex(0,2), Math.sqrt(Rational(-4)))
881 # assert_equal(true, Math.sqrt(Rational(-4)).exact?)
884 assert_equal(Complex(0,3), Math.sqrt(-9.0))
885 # assert_equal(true, Math.sqrt(-9.0).inexact?)
886 assert_equal(Complex(0,3), Math.sqrt(-9))
887 # assert_equal(true, Math.sqrt(-9).exact?)
888 if defined?(Rational)
889 assert_equal(Complex(0,3), Math.sqrt(Rational(-9)))
890 # assert_equal(true, Math.sqrt(Rational(-9)).exact?)
893 c = Math.sqrt(Complex(1, 2))
894 assert_in_delta(1.272, c.real, 0.001)
895 assert_in_delta(0.786, c.image, 0.001)
898 assert_in_delta(0.0, c.real, 0.001)
899 assert_in_delta(3.0, c.image, 0.001)
901 c = Math.exp(Complex(1, 2))
902 assert_in_delta(-1.131, c.real, 0.001)
903 assert_in_delta(2.471, c.image, 0.001)
905 c = Math.sin(Complex(1, 2))
906 assert_in_delta(3.165, c.real, 0.001)
907 assert_in_delta(1.959, c.image, 0.001)
909 c = Math.cos(Complex(1, 2))
910 assert_in_delta(2.032, c.real, 0.001)
911 assert_in_delta(-3.051, c.image, 0.001)
913 c = Math.tan(Complex(1, 2))
914 assert_in_delta(0.033, c.real, 0.001)
915 assert_in_delta(1.014, c.image, 0.001)
917 c = Math.sinh(Complex(1, 2))
918 assert_in_delta(-0.489, c.real, 0.001)
919 assert_in_delta(1.403, c.image, 0.001)
921 c = Math.cosh(Complex(1, 2))
922 assert_in_delta(-0.642, c.real, 0.001)
923 assert_in_delta(1.068, c.image, 0.001)
925 c = Math.tanh(Complex(1, 2))
926 assert_in_delta(1.166, c.real, 0.001)
927 assert_in_delta(-0.243, c.image, 0.001)
929 c = Math.log(Complex(1, 2))
930 assert_in_delta(0.804, c.real, 0.001)
931 assert_in_delta(1.107, c.image, 0.001)
933 c = Math.log(Complex(1, 2), Math::E)
934 assert_in_delta(0.804, c.real, 0.001)
935 assert_in_delta(1.107, c.image, 0.001)
938 assert_in_delta(0.0, c.real, 0.001)
939 assert_in_delta(Math::PI, c.image, 0.001)
942 assert_in_delta(3.0, c.real, 0.001)
943 assert_in_delta(0.0, c.image, 0.001)
946 assert_in_delta(1.092, c.real, 0.001)
947 assert_in_delta(-0.420, c.image, 0.001)
949 c = Math.log10(Complex(1, 2))
950 assert_in_delta(0.349, c.real, 0.001)
951 assert_in_delta(0.480, c.image, 0.001)
953 c = Math.asin(Complex(1, 2))
954 assert_in_delta(0.427, c.real, 0.001)
955 assert_in_delta(1.528, c.image, 0.001)
957 c = Math.acos(Complex(1, 2))
958 assert_in_delta(1.143, c.real, 0.001)
959 assert_in_delta(-1.528, c.image, 0.001)
961 c = Math.atan(Complex(1, 2))
962 assert_in_delta(1.338, c.real, 0.001)
963 assert_in_delta(0.402, c.image, 0.001)
965 c = Math.atan2(Complex(1, 2), 1)
966 assert_in_delta(1.338, c.real, 0.001)
967 assert_in_delta(0.402, c.image, 0.001)
969 c = Math.asinh(Complex(1, 2))
970 assert_in_delta(1.469, c.real, 0.001)
971 assert_in_delta(1.063, c.image, 0.001)
973 c = Math.acosh(Complex(1, 2))
974 assert_in_delta(1.528, c.real, 0.001)
975 assert_in_delta(1.143, c.image, 0.001)
977 c = Math.atanh(Complex(1, 2))
978 assert_in_delta(0.173, c.real, 0.001)
979 assert_in_delta(1.178, c.image, 0.001)
984 def test_canonicalize
985 f = defined?(Complex::Unify)
986 Complex.const_set(:Unify, true) unless f
988 assert_same(1, Complex.instance_eval { new(1, 0) })
989 assert_not_same(1.0, Complex.instance_eval { new(1.0, 0) })
990 assert_equal(Complex(1.0, 0), Complex.instance_eval { new(1.0, 0) })
992 Complex.instance_eval { remove_const(:Unify) } unless f
996 c = Complex.polar(2, 2)
997 assert_in_delta(2*Math.cos(2), c.real , 0.001)
998 assert_in_delta(2*Math.sin(2), c.image, 0.001)
1000 c = Complex.polar(1, Complex(0, 1))
1001 assert_in_delta(1/Math::E, c.real , 0.001)
1002 assert_in_delta( 0, c.image, 0.001)
1006 assert_equal(true, Complex.generic?(1))
1007 assert_equal(true, Complex.generic?(2**100))
1008 assert_equal(true, Complex.generic?(Rational(1, 2)))
1009 assert_equal(true, Complex.generic?(1.0))
1010 assert_equal(false, Complex.generic?(Complex(1, 1)))
1016 assert_equal(Complex(1, 1), Complex.instance_eval { new!(o, o) })
1019 def test_denominator
1020 f = defined?(Complex::Unify)
1021 unify_val = f && Complex::Unify
1022 Complex.instance_eval { remove_const(:Unify) } if f
1024 dummy_rational = Class.new(Rational)
1025 o1 = dummy_rational.instance_eval { new(1, 1) }
1026 o2 = dummy_rational.instance_eval { new(1, 1) }
1028 class << o1; self; end.instance_eval { define_method(:denominator) { d1 } rescue nil }
1029 class << o2; self; end.instance_eval { define_method(:denominator) { d2 } rescue nil }
1030 # o1.denominator returns d1 and o1.denominator returns d2
1035 assert_equal(0, c.denominator)
1038 assert_equal(1, c.denominator)
1041 assert_equal(256, c.denominator)
1044 assert_equal(512, c.denominator)
1047 assert_equal(512, c.denominator)
1049 d1, d2 = -(2**100), -(3**100)
1050 assert_equal(6**100, c.denominator)
1053 assert_equal(2**100, c.denominator)
1055 Complex.const_set(:Unify, unify_val) if f
1061 def b.*(x); self; end rescue nil
1062 def b.+(x); -1; end rescue nil
1063 assert_equal(Complex(0, 1), Complex(b, 1).abs)
1065 def b.+(x); Complex(0, 1); end rescue nil
1066 c = Complex(b, 1).abs
1067 assert_in_delta(1/Math.sqrt(2), c.real , 0.001)
1068 assert_in_delta(1/Math.sqrt(2), c.image, 0.001)
1070 def b.+(x); Complex(0, -1); end rescue nil
1071 c = Complex(b, 1).abs
1072 assert_in_delta( 1/Math.sqrt(2), c.real , 0.001)
1073 assert_in_delta(-1/Math.sqrt(2), c.image, 0.001)
1077 assert_raise(Errno::EDOM, Errno::ERANGE) { Complex(1, nan).abs }
1083 assert_equal(Complex(42, 0), c.coerce(42).first)
1084 assert_raise(TypeError) { c.coerce(Object.new) }
1087 def o.coerce(x); [x.real, x.image]; end
1088 assert_equal(9, c + o)
1089 assert_equal(3, c - o)
1090 assert_equal(18, c * o)
1091 assert_equal(2, c / o)
1092 assert_equal(216, c ** o)
1096 assert_equal(Complex(2**100, 1), Complex(0, 1) + 2**100)
1100 assert_equal(Complex(0.0, 0.0), Complex(1.0, 1.0) * 0)
1101 assert_equal(Complex(0, 0), Complex(0, 0) * (2**100))
1105 assert_equal(Complex(1, 0), Complex(2, 2) ** 0)
1106 assert_equal(Complex(0, -1), Complex(0, 1) ** (2**100-1))
1107 assert_equal(Complex(1, 0), Complex(1, 0) ** Rational(1, 2**100))
1111 if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
1112 assert_equal(Complex(1), 1 ** Complex(1))
1114 assert_equal('-1.0-0.0i', Complex(-1.0, -0.0).to_s)