summaryrefslogtreecommitdiff
path: root/test/ruby/test_complexrational.rb
blob: 31d11fe31796c0c97bec0744a179a856b3689656 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# frozen_string_literal: false
require 'test/unit'

class ComplexRational_Test < Test::Unit::TestCase

  def test_rat_srat
    omit unless defined?(Rational)

    c = SimpleRat(1,3)
    cc = Rational(3,2)

    assert_kind_of(Numeric, c)
    assert_kind_of(Numeric, cc)

    assert_instance_of(SimpleRat, c)
    assert_instance_of(Rational, cc)

    assert_equal(SimpleRat(1,3), +c)
    assert_equal(SimpleRat(-1,3), -c)

    assert_equal(SimpleRat(7,3), c + 2)
    assert_equal(SimpleRat(-5,3), c - 2)
    assert_equal(SimpleRat(2,3), c * 2)
    assert_equal(SimpleRat(1,6), c / 2)
    assert_equal(SimpleRat(1,9), c ** 2)
    assert_equal(-1, c <=> 2)

    assert_equal(SimpleRat(7,3), 2 + c)
    assert_equal(SimpleRat(5,3), 2 - c)
    assert_equal(SimpleRat(2,3), 2 * c)
    assert_equal(SimpleRat(6,1), 2 / c)
    assert_in_delta(1.2599, 2 ** c, 0.001)
    assert_equal(1, 2 <=> c)

    assert_equal(SimpleRat(11,6), c + cc)
    assert_equal(SimpleRat(-7,6), c - cc)
    assert_equal(SimpleRat(1,2), c * cc)
    assert_equal(SimpleRat(2,9), c / cc)
    assert_in_delta(0.1924, c ** cc, 0.001)
    assert_equal(-1, c <=> cc)

    assert_equal(SimpleRat(11,6), cc + c)
    assert_equal(SimpleRat(7,6), cc - c)
    assert_equal(SimpleRat(1,2), cc * c)
    assert_equal(SimpleRat(9,2), cc / c)
    assert_in_delta(1.1447, cc ** c, 0.001)
    assert_equal(1, cc <=> c)

    assert_equal(SimpleRat, (+c).class)
    assert_equal(SimpleRat, (-c).class)

    assert_equal(SimpleRat, (c + 2).class)
    assert_equal(SimpleRat, (c - 2).class)
    assert_equal(SimpleRat, (c * 2).class)
    assert_equal(SimpleRat, (c / 2).class)
    assert_equal(SimpleRat, (c ** 2).class)

    assert_equal(SimpleRat, (2 + c).class)
    assert_equal(SimpleRat, (2 - c).class)
    assert_equal(SimpleRat, (2 * c).class)
    assert_equal(SimpleRat, (2 / c).class)
    assert_equal(Float, (2 ** c).class)

    assert_equal(SimpleRat, (c + cc).class)
    assert_equal(SimpleRat, (c - cc).class)
    assert_equal(SimpleRat, (c * cc).class)
    assert_equal(SimpleRat, (c / cc).class)
    assert_equal(Float, (c ** cc).class)

    assert_equal(SimpleRat, (cc + c).class)
    assert_equal(SimpleRat, (cc - c).class)
    assert_equal(SimpleRat, (cc * c).class)
    assert_equal(SimpleRat, (cc / c).class)
    assert_equal(Float, (cc ** c).class)

    assert_equal(0, Rational(2,3) <=> SimpleRat(2,3))
    assert_equal(0, SimpleRat(2,3) <=> Rational(2,3))
    assert_equal(Rational(2,3), SimpleRat(2,3))
    assert_equal(SimpleRat(2,3), Rational(2,3))

    assert_equal(SimpleRat, (c + 0).class)
    assert_equal(SimpleRat, (c - 0).class)
    assert_equal(SimpleRat, (c * 0).class)
    assert_equal(SimpleRat, (c * 1).class)
    assert_equal(SimpleRat, (0 + c).class)
    assert_equal(SimpleRat, (0 - c).class)
    assert_equal(SimpleRat, (0 * c).class)
    assert_equal(SimpleRat, (1 * c).class)
  end

  def test_comp_srat
    omit unless defined?(Rational)

    c = Complex(SimpleRat(2,3),SimpleRat(1,2))
    cc = Complex(Rational(3,2),Rational(2,1))

    assert_equal(Complex(SimpleRat(2,3),SimpleRat(1,2)), +c)
    assert_equal(Complex(SimpleRat(-2,3),SimpleRat(-1,2)), -c)

    assert_equal(Complex(SimpleRat(8,3),SimpleRat(1,2)), c + 2)
    assert_equal(Complex(SimpleRat(-4,3),SimpleRat(1,2)), c - 2)
    assert_equal(Complex(SimpleRat(4,3),SimpleRat(1,1)), c * 2)
    assert_equal(Complex(SimpleRat(1,3),SimpleRat(1,4)), c / 2)
    assert_equal(Complex(SimpleRat(7,36),SimpleRat(2,3)), c ** 2)
    assert_nil(c <=> 2)

    assert_equal(Complex(SimpleRat(8,3),SimpleRat(1,2)), 2 + c)
    assert_equal(Complex(SimpleRat(4,3),SimpleRat(-1,2)), 2 - c)
    assert_equal(Complex(SimpleRat(4,3),SimpleRat(1,1)), 2 * c)
    assert_equal(Complex(SimpleRat(48,25),SimpleRat(-36,25)), 2 / c)
    r = 2 ** c
    assert_in_delta(1.4940, r.real, 0.001)
    assert_in_delta(0.5392, r.imag, 0.001)
    assert_nil(2 <=> c)

    assert_equal(Complex(SimpleRat(13,6),SimpleRat(5,2)), c + cc)
    assert_equal(Complex(SimpleRat(-5,6),SimpleRat(-3,2)), c - cc)
    assert_equal(Complex(SimpleRat(0,1),SimpleRat(25,12)), c * cc)
    assert_equal(Complex(SimpleRat(8,25),SimpleRat(-7,75)), c / cc)
    r = c ** cc
    assert_in_delta(0.1732, r.real, 0.001)
    assert_in_delta(0.1186, r.imag, 0.001)
    assert_nil(c <=> cc)

    assert_equal(Complex(SimpleRat(13,6),SimpleRat(5,2)), cc + c)
    assert_equal(Complex(SimpleRat(5,6),SimpleRat(3,2)), cc - c)
    assert_equal(Complex(SimpleRat(0,1),SimpleRat(25,12)), cc * c)
    assert_equal(Complex(SimpleRat(72,25),SimpleRat(21,25)), cc / c)
    r = cc ** c
    assert_in_delta(0.5498, r.real, 0.001)
    assert_in_delta(1.0198, r.imag, 0.001)
    assert_nil(cc <=> c)

    assert_equal([SimpleRat,SimpleRat],
		 (+c).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (-c).instance_eval{[real.class, imag.class]})

    assert_equal([SimpleRat,SimpleRat],
		 (c + 2).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c - 2).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c * 2).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c / 2).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c ** 2).instance_eval{[real.class, imag.class]})

    assert_equal([SimpleRat,SimpleRat],
		 (c + cc).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c - cc).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c * cc).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c / cc).instance_eval{[real.class, imag.class]})
    assert_equal([Float,Float],
		 (c ** cc).instance_eval{[real.class, imag.class]})

    assert_equal([SimpleRat,SimpleRat],
		 (cc + c).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (cc - c).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (cc * c).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (cc / c).instance_eval{[real.class, imag.class]})
    assert_equal([Float,Float],
		 (cc ** c).instance_eval{[real.class, imag.class]})

    assert_equal(Complex(SimpleRat(2,3),SimpleRat(3,2)),
                 Complex(Rational(2,3),Rational(3,2)))
    assert_equal(Complex(Rational(2,3),Rational(3,2)),
                 Complex(SimpleRat(2,3),SimpleRat(3,2)))

    assert_equal([SimpleRat,SimpleRat],
		 (c + 0).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c - 0).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c * 0).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (c * 1).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (0 + c).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (0 - c).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (0 * c).instance_eval{[real.class, imag.class]})
    assert_equal([SimpleRat,SimpleRat],
		 (1 * c).instance_eval{[real.class, imag.class]})
  end

end

def SimpleRat(*a) SimpleRat.new(*a) end

class SimpleRat < Numeric

  def initialize(num, den = 1)
    if den == 0
      raise ZeroDivisionError, "divided by zero"
    end
    if den < 0
      num = -num
      den = -den
    end
    gcd = num.gcd(den)
    @num = num.div(gcd)
    @den = den.div(gcd)
  end

  def numerator() @num end
  def denominator() @den end

  def +@() self end
  def -@() self.class.new(-@num, @den) end

  def +(o)
    case o
    when SimpleRat, Rational
      a = @num * o.denominator
      b = o.numerator * @den
      self.class.new(a + b, @den * o.denominator)
    when Integer
      self + self.class.new(o)
    when Float
      to_f + o
    else
      x, y = o.coerce(self)
      x + y
    end
  end

  def -(o)
    case o
    when SimpleRat, Rational
      a = @num * o.denominator
      b = o.numerator * @den
      self.class.new(a - b, @den * o.denominator)
    when Integer
      self - self.class.new(o)
    when Float
      to_f - o
    else
      x, y = o.coerce(self)
      x - y
    end
  end

  def *(o)
    case o
    when SimpleRat, Rational
      a = @num * o.numerator
      b = @den * o.denominator
      self.class.new(a, b)
    when Integer
      self * self.class.new(o)
    when Float
      to_f * o
    else
      x, y = o.coerce(self)
      x * y
    end
  end

  def quo(o)
    case o
    when SimpleRat, Rational
      a = @num * o.denominator
      b = @den * o.numerator
      self.class.new(a, b)
    when Integer
      if o == 0
        raise ZeroDivisionError, "divided by zero"
      end
      self.quo(self.class.new(o))
    when Float
      to_f.quo(o)
    else
      x, y = o.coerce(self)
      x.quo(y)
    end
  end

  alias / quo

  def floor
    @num.div(@den)
  end

  def ceil
    -((-@num).div(@den))
  end

  def truncate
    if @num < 0
      return -((-@num).div(@den))
    end
    @num.div(@den)
  end

  alias to_i truncate

  def round
    if @num < 0
      num = -@num
      num = num * 2 + @den
      den = @den * 2
      -(num.div(den))
    else
      num = @num * 2 + @den
      den = @den * 2
      num.div(den)
    end
  end

  def div(o) (self / o).floor end
  def quot(o) (self / o).truncate end

  def modulo(o)
    q = div(o)
    self - o * q
  end

  def remainder(o)
    q = quot(o)
    self - o * q
  end

  alias % modulo

  def divmod(o) [div(o), modulo(o)] end
  def quotrem(o) [quot(o), remainder(o)] end

  def **(o)
    case o
    when SimpleRat, Rational
      Float(self) ** o
    when Integer
      if o > 0
	a = @num ** o
	b = @den ** o
      elsif o < 0
	a = @den ** -o
	b = @num ** -o
      else
	a = b = 1
      end
      self.class.new(a, b)
    when Float
      to_f ** o
    else
      x, y = o.coerce(self)
      x ** y
    end
  end

  def <=>(o)
    case o
    when SimpleRat, Rational
      a = @num * o.denominator
      b = o.numerator * @den
      return a <=> b
    when Integer
      self <=> self.class.new(o)
    when Float
      to_f <=> o
    else
      x, y = o.coerce(self)
      x <=> y
    end
  end

  def ==(o)
    begin
      (self <=> o) == 0
    rescue
      false
    end
  end

  def coerce(o)
    case o
    when Rational
      [self.class.new(o.numerator, o.denominator), self]
    when Integer
      [self.class.new(o), self]
    when Float
      [o, self.to_f]
    else
      super
    end
  end

  def hash() @num.hash ^ @den.hash end

  def to_f() @num.to_f / @den.to_f end
  def to_r() self end
  def to_s() format('%s/%s', @num, @den) end

  def inspect() format('#SR(%s)', to_s) end

  def marshal_dump() [@num, @den] end
  def marshal_load(a) @num, @den = a end

end