File: complex.rb

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (163 lines) | stat: -rw-r--r-- 4,964 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def assert_complex(real, exp)
  assert "assert_complex" do
    assert_float real.real,      exp.real
    assert_float real.imaginary, exp.imaginary
  end
end

assert 'Complex' do
  c = 123i
  assert_equal Complex, c.class
  assert_equal [c.real, c.imaginary], [0, 123]
  c = 123 + -1.23i
  assert_equal Complex, c.class
  assert_equal [c.real, c.imaginary], [123, -1.23]
end

assert 'Complex::polar' do
  assert_complex Complex.polar(3, 0),           (3  +  0i)
  assert_complex Complex.polar(3, Math::PI/2),  (0  +  3i)
  assert_complex Complex.polar(3, Math::PI),    (-3 +  0i)
  assert_complex Complex.polar(3, -Math::PI/2), (0  + -3i)
end

assert 'Complex::rectangular' do
  assert_complex Complex.rectangular(1, 2), (1 + 2i)
end

assert 'Complex#*' do
  assert_complex Complex(2, 3)  * Complex(2, 3),  (-5    + 12i)
  assert_complex Complex(900)   * Complex(1),     (900   + 0i)
  assert_complex Complex(-2, 9) * Complex(-9, 2), (0     - 85i)
  assert_complex Complex(9, 8)  * 4,              (36    + 32i)
  assert_complex Complex(20, 9) * 9.8,            (196.0 + 88.2i)
  assert_complex 4 * Complex(9, 8),               (36    + 32i)
  assert_complex 9.8 * Complex(20, 9),            (196.0 + 88.2i)
end

assert 'Complex#+' do
  assert_complex Complex(2, 3)  + Complex(2, 3) , (4    + 6i)
  assert_complex Complex(900)   + Complex(1)    , (901  + 0i)
  assert_complex Complex(-2, 9) + Complex(-9, 2), (-11  + 11i)
  assert_complex Complex(9, 8)  + 4             , (13   + 8i)
  assert_complex Complex(20, 9) + 9.8           , (29.8 + 9i)
  assert_complex 4 + Complex(9, 8)              , (13   + 8i)
  assert_complex 9.8 + Complex(20, 9)           , (29.8 + 9i)
end

assert 'Complex#-' do
  assert_complex Complex(2, 3)  - Complex(2, 3) , (0    + 0i)
  assert_complex Complex(900)   - Complex(1)    , (899  + 0i)
  assert_complex Complex(-2, 9) - Complex(-9, 2), (7    + 7i)
  assert_complex Complex(9, 8)  - 4             , (5    + 8i)
  assert_complex Complex(20, 9) - 9.8           , (10.2 + 9i)
  assert_complex 4 - Complex(9, 8)              , (-5   - 8i)
  assert_complex 10.5 - Complex(20, 9)          , (-9.5 - 9i)
end

assert 'Complex#-@' do
  assert_complex((-1 - 2i), -Complex(1, 2))
end

assert 'Complex#/' do
  assert_complex Complex(2, 3)  / Complex(2, 3) , (1                  + 0i)
  assert_complex Complex(900)   / Complex(1)    , (900                + 0i)
  assert_complex Complex(-2, 9) / Complex(-9, 2), ((36.0 / 85)        - (77i / 85))
  assert_complex Complex(9, 8)  / 4             , ((9.0 / 4)          + 2i)
  assert_complex Complex(20, 9) / 9.8           , (2.0408163265306123 + 0.9183673469387754i)
  assert_complex 4 / Complex(9, 8)              , (0.2482758620689655 - 0.2206896551724138i)
  assert_complex 9.8 / Complex(20, 9)           , (0.4074844074844075 - 0.1833679833679834i)
  if 1e39.infinite? then
    # MRB_USE_FLOAT32 in effect
    ten = 1e21
    one = 1e20
  else
    ten = 1e201
    one = 1e200
  end
  assert_complex Complex(ten, ten) / Complex(one, one), Complex(10.0, 0.0)
end

assert 'Complex#==' do
  assert_true  Complex(2, 3)  == Complex(2, 3)
  assert_true  Complex(5)     == 5
  assert_true  Complex(0)     == 0.0
  assert_true  5 == Complex(5)
  assert_true  0.0 == Complex(0)
end

assert 'Complex#abs' do
  assert_float Complex(-1).abs,        1
  assert_float Complex(3.0, -4.0).abs, 5.0
  if 1e39.infinite? then
    # MRB_USE_FLOAT32 in effect
    exp = 125
  else
    exp = 1021
  end
  assert_true Complex(3.0*2.0**exp, 4.0*2.0**exp).abs.finite?
  assert_float Complex(3.0*2.0**exp, 4.0*2.0**exp).abs, 5.0*2.0**exp
end

assert 'Complex#abs2' do
  assert_float Complex(-1).abs2,        1
  assert_float Complex(3.0, -4.0).abs2, 25.0
end

assert 'Complex#arg' do
  assert_float Complex.polar(3, Math::PI/2).arg, 1.5707963267948966
end

assert 'Complex#conjugate' do
  assert_complex Complex(1, 2).conjugate, (1 - 2i)
end

assert 'Complex#fdiv' do
  assert_complex Complex(11, 22).fdiv(3), (3.6666666666666665 + 7.333333333333333i)
end

assert 'Complex#imaginary' do
  assert_float Complex(7).imaginary    , 0
  assert_float Complex(9, -4).imaginary, -4
end

assert 'Complex#polar' do
  assert_equal Complex(1, 2).polar, [2.23606797749979, 1.1071487177940904]
end

assert 'Complex#real' do
  assert_float Complex(7).real,     7
  assert_float Complex(9, -4).real, 9
end

assert 'Complex#real?' do
  assert_false Complex(1).real?
end

assert 'Complex::rectangular' do
  assert_equal Complex(1, 2).rectangular, [1, 2]
end

assert 'Complex::to_c' do
  assert_equal Complex(1, 2).to_c, Complex(1, 2)
end

assert 'Complex::to_f' do
  assert_float Complex(1, 0).to_f, 1.0
  assert_raise(RangeError) do
    Complex(1, 2).to_f
  end
end

assert 'Complex::to_i' do
  assert_equal Complex(1, 0).to_i, 1
  assert_raise(RangeError) do
    Complex(1, 2).to_i
  end
end

assert 'Complex#frozen?' do
  assert_predicate(1i, :frozen?)
  assert_predicate(Complex(2,3), :frozen?)
  assert_predicate(4+5i, :frozen?)
end