M500 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/M500.rb +3006 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2cb0b2efe7e5502e3cb7fac1fc3d1112cf9645ba
4
+ data.tar.gz: 6b2c11cb1099ab9ee98922eb937c094a71ea78e5
5
+ SHA512:
6
+ metadata.gz: 19c82406a324b55fe0d46de4354fb1a083ce733d325a60f7cb52165a089790ed4c08534e7a22687f3479e5bfe06cdab84a921ea85c2e4fe2584f72ae9d4bd1da
7
+ data.tar.gz: c158461ff3b8c4c8b11cbf2d0c0b6b7b29b5651be8792ec4854c73d6e4b37e685115c083bac561de8a33e49adb317399abb2415d0600433c052ac89e181e58c3
@@ -0,0 +1,3006 @@
1
+ #
2
+ # m500.rb -
3
+ # $Release Version: 0.9 $
4
+ # $Revision: 1.44 $
5
+ # by mark ingram
6
+ #
7
+ # --
8
+ #
9
+ #
10
+ #
11
+ module SGML
12
+ @idsgml = true
13
+ def tog_sgml_id
14
+ @idsgml = !@idsgml
15
+ end
16
+ def sgml_id
17
+ ret = ""
18
+ @idsgml ? ret = "id='#{self.class.to_s.downcase}#{self.object_id}' " : ret = ""
19
+ ret
20
+ end
21
+ end
22
+ class Array
23
+ def to_K
24
+ a = self.dup
25
+ b = a.shift
26
+ a.at(a.length-1).kind_of?(Array) ? c = a.delete_at(a.length-1) : c = ""
27
+ tmp = true
28
+ ret = nil
29
+ a.each{|n| tmp = false if n.kind_of?(Numeric) and n<0 }
30
+ if tmp then
31
+ b >= 0 ? ret = Kettenbruch.new!(b,a,c,1) : ret = Kettenbruch.new!(b,a,c,-1)
32
+ else
33
+ p "raise wrong input error"
34
+ end
35
+ ret
36
+ end
37
+ end
38
+ class String
39
+ def is_f?
40
+ t = true
41
+ re = /(\-*\d+).(\d+)e([-|+])(\d+)/
42
+ md = re.match(self)
43
+ t = false if md[1].nil? && md[2].nil? && md[3].nil? && md[4].nil?
44
+ t
45
+ end
46
+ def is_Frac?
47
+ t = true
48
+ re = /(\d+) (\d+)\/(\d+)/
49
+ md = re.match(self)
50
+ t = false if md[1].nil? && md[2].nil? && md[3].nil? && md[4].nil?
51
+ t
52
+ end
53
+ def is_Q?
54
+ t = true
55
+ re = /(\d+)\/(\d+)/
56
+ md = re.match(self)
57
+ t = false if md[1].nil? && md[2].nil? && md[3].nil? && md[4].nil?
58
+ t
59
+ end
60
+ def is_Dec?
61
+ t = true
62
+ re = /(\d+)#{Decimal::decimalSeparator}(\d+)/
63
+ md = re.match(self)
64
+ t = false if md[1].nil? && md[2].nil?
65
+ t
66
+ end
67
+ def is_Sig?
68
+ end
69
+ def is_R?
70
+ false
71
+ end
72
+ def to_N
73
+ re = /(^\d+)/
74
+ md = re.match(self.to_s)
75
+ t = emptySet
76
+ t = md[1].to_i.to_N unless md.nil?
77
+ end
78
+ def to_Z
79
+ re = /(^\d+)/
80
+ md = re.match(self.to_s)
81
+ t = emptySet
82
+ if not md.nil? then
83
+ t = Zahlen(md[1].to_i)
84
+ else
85
+ re = /(^-\d+)/
86
+ md = re.match(self.to_s)
87
+ t = Zahlen((md[1].to_i)) unless md.nil?
88
+ end
89
+ t
90
+ end
91
+ def to_N0
92
+ re = /(\d+)/
93
+ md = re.match(self.to_s)
94
+ t = emptySet
95
+ t = Counting(md[1].to_i) unless md.nil?
96
+ end
97
+ def to_Frac
98
+ re = /(^\d+) (\d+)\/(\d+)/
99
+ md = re.match(self.to_s)
100
+ t = nil
101
+ if not md.nil? then
102
+ t = Fraction(Zahlen(md[1].to_i),Quotient(md[2].to_i, md[3].to_i))
103
+ else
104
+ re = /^-1\((\d+) (\d+)\/(\d+)\)/
105
+ md = re.match(self.to_s)
106
+ if not md.nil? then
107
+ t = Fraction(Zahlen(md[1].to_i),Quotient(md[2].to_i, md[3].to_i),-1) unless md.nil?
108
+ else
109
+ re = /(\-*\d+)\/(\d+)/
110
+ md = re.match(self.to_s)
111
+ if not md.nil? then
112
+ t = Fraction(Quotient(Zahlen(md[1].to_i),Natural(md[2].to_i)))
113
+ else
114
+ t=Fraction(Quotient(self.to_i,1))
115
+ end
116
+ end
117
+ end
118
+ end
119
+ def to_Q
120
+ re = /(\-*\d+)\/(\d+)/
121
+ md = re.match(self.to_s)
122
+ ret = naught
123
+ if md then
124
+ t = 1
125
+ ret = Quotient(Zahlen(md[1].to_i),Natural(md[2].to_i))
126
+ else
127
+ ret = Quotient(Zahlen(md[1].to_i),Natural(md[2].to_i))
128
+ end
129
+ ret
130
+ end
131
+ def to_Dec
132
+ re = /^-/
133
+ md = re.match(self.to_s)
134
+ e = -1
135
+ e = 1 if md.to_a.empty?
136
+ md = nil
137
+ t0 = 0
138
+ t1 = '0'
139
+ t2 = nil
140
+ t3 = nil
141
+ re = /(\d+)\.(\d+)\[(\d+)\]/
142
+ md = re.match(self.to_s)
143
+ if md.to_a.empty? then
144
+ re = /(\d+)\.(\d+)/
145
+ md = re.match(self.to_s)
146
+ if md.to_a.empty? then
147
+ re = /(\d+)\.\[(\d+)\]/
148
+ md = re.match(self.to_s)
149
+ if md.to_a.empty? then
150
+ re = /(\d+)/
151
+ md = re.match(self.to_s)
152
+ t0 = 0
153
+ t1 = '0'
154
+ t2 = nil
155
+ t3 = md[1].to_i
156
+ else
157
+ t0 = 0
158
+ t1 = md[2] #.to_i
159
+ t2 = nil
160
+ t3 = md[1].to_i
161
+ end
162
+ else
163
+ t0 = md[2].scan(/^0+/)[0].length unless md[2].scan(/^0+/).empty?
164
+ t1 = md[3] unless md[3].nil?
165
+ t2 = md[2].to_i
166
+ t3 = md[1].to_i
167
+ end
168
+ else
169
+ t0 = md[2].scan(/^0+/)[0].length unless md[2].scan(/^0+/).empty?
170
+ t1 = md[3] unless md[3].nil?
171
+ t2 = md[2].to_i
172
+ t3 = md[1].to_i
173
+ end
174
+ Decimal(t3,t2,t1,t0,e)
175
+ end
176
+ def is_K?
177
+ t = true
178
+ re = /(\d+)#{Kettenbruch::Separator}(\d+)/
179
+ md = re.match(self)
180
+ t = false if md[1].nil? && md[2].nil?
181
+ t
182
+ end
183
+ def is_K_Leicht?
184
+ t = true
185
+ re = /(\d+)#{Kettenbruch::Separator}(\d+)/
186
+ md = re.match(self)
187
+ t = false if md[1].nil? && md[2].nil?
188
+ t
189
+ end
190
+ def append_K(a)
191
+ a = a.to_Frac
192
+ # "Fraction(Zahlen(#{a.integer.to_s}),Quotient(Zahlen(#{a.properfraction.numerator.to_s}),Zahlen(#{a.properfraction.denominator.to_s})))"
193
+ self.gsub!("&&&&","Fraction(Zahlen(#{a.integer.to_s}),Quotient(Zahlen(#{a.properfraction.numerator.to_s}),Zahlen(#{a.properfraction.denominator.to_s})))")
194
+ end
195
+ def to_K
196
+ a = self.split(";")
197
+ int = a.at(0).to_i
198
+ re = /'...'/
199
+ md = re.match(a.at(1))
200
+ frK = eval("[" + a.at(1).gsub("...","") + "]" )
201
+ ([int] + frK).to_K
202
+ end
203
+ def to_Sig
204
+ end
205
+ def to_R
206
+ self.to_i
207
+ end
208
+ end
209
+ class Numeric
210
+ include SGML
211
+ def sgml_id
212
+ ret = ""
213
+ @idsgml ? ret = "id='#{self.class.to_s.downcase}#{self.object_id}' " : ret = ""
214
+ ret
215
+ end
216
+ def to_sgml
217
+ "<mn #{sgml_id}class='numeric'>#{self.to_s}</mn>"
218
+ end
219
+ def to_N
220
+ self
221
+ end
222
+ def to_N0
223
+ self
224
+ end
225
+ def to_Z
226
+ self
227
+ end
228
+ def to_Frac
229
+ self
230
+ end
231
+ def to_Q
232
+ self
233
+ end
234
+ def to_Dec
235
+ self
236
+ end
237
+ def to_Sig
238
+ self
239
+ end
240
+ def to_K
241
+ self
242
+ end
243
+ def to_R
244
+ self
245
+ end
246
+ def irrational?
247
+ false
248
+ end
249
+ def rational?
250
+ true
251
+ end
252
+ def coerce(other)
253
+ if Natural === other or Counting === other or Zahlen === other or Quotient === other or Fraction === other
254
+ [other.to_i,self]
255
+ else
256
+ super
257
+ end
258
+ end
259
+ end
260
+ class Fixnum
261
+ include SGML
262
+ def to_sgml
263
+ "<mn #{sgml_id}class='fixnum'>#{self.to_s}</mn>"
264
+ end
265
+ undef quo
266
+ def quo(other)
267
+ Quotient.new!(self,1) / other
268
+ end
269
+ alias rdiv quo
270
+ def rpower (other)
271
+ if other >= 0
272
+ self.power!(other)
273
+ else
274
+ Quotient.new!(self,1)**other
275
+ end
276
+ end
277
+ unless defined? 1.power!
278
+ alias power! **
279
+ alias ** rpower
280
+ end
281
+ def is_0?
282
+ self === 0 ? true : false
283
+ end
284
+ def to_N
285
+ self > 0 ? Natural(self) : emptySet
286
+ end
287
+ def to_N0
288
+ self >= 0 ? Counting(self) : emptySet
289
+ end
290
+ def to_Z
291
+ Zahlen(self)
292
+ end
293
+ def to_Frac
294
+ Fraction(self,1)
295
+ end
296
+ def to_Q
297
+ Quotient(self,1)
298
+ end
299
+ def to_Dec
300
+ Decimal(self,0,'0')
301
+ end
302
+ def to_Sig
303
+ Sigma(self.to_Q)
304
+ end
305
+ def to_K
306
+ Kettenbruch(self.to_Frac)
307
+ end
308
+ def to_R
309
+ naught
310
+ end
311
+ end
312
+ class Bignum
313
+ include SGML
314
+ def to_sgml
315
+ "<mn #{sgml_id}class='rational'>#{self.to_s}</mn>"
316
+ end
317
+ unless defined? Complex
318
+ alias power! **
319
+ end
320
+ undef quo
321
+ def quo(other)
322
+ Fraction.new!(self,1) / other
323
+ end
324
+ alias rdiv quo
325
+ def rpower (other)
326
+ if other >= 0
327
+ self.power!(other)
328
+ else
329
+ Fraction.new!(self, 1)**other
330
+ end
331
+ end
332
+ unless defined? Complex
333
+ alias ** rpower
334
+ end
335
+ def is_0?
336
+ self === 0 ? true : false
337
+ end
338
+ def to_N
339
+ self < 0 ? Natural(self) : emptySet
340
+ end
341
+ def to_N0
342
+ self <= 0 ? Counting(self) : emptySet
343
+ end
344
+ def to_Z
345
+ Zahlen(self)
346
+ end
347
+ def to_Frac
348
+ Fraction(self,Quotient(1,1))
349
+ end
350
+ def to_Q
351
+ Quotient(self,1)
352
+ end
353
+ def to_Dec
354
+ Decimal(self,0)
355
+ end
356
+ def to_Sig
357
+ Sigma(self.to_Q)
358
+ end
359
+ def to_K
360
+ Kettenbruch(self.to_Frac)
361
+ end
362
+ def to_R
363
+ naught
364
+ end
365
+ def coerce(other)
366
+ if Natural === other or Counting === other or Zahlen === other or Quotient === other or Fraction === other
367
+ [other,self]
368
+ else
369
+ super
370
+ end
371
+ end
372
+ end
373
+ class Float
374
+ include SGML
375
+ def to_sgml
376
+ "<mn #{sgml_id}class='float'>#{self.to_s}</mn>"
377
+ end
378
+ def to_Z
379
+ if self.to_i then
380
+ Zahlen(self.to_i)
381
+ else
382
+ emptySet
383
+ end
384
+ end
385
+ def is_0?
386
+ self === 0.0 ? true : false
387
+ end
388
+ def to_N
389
+ t = emptySet
390
+ t == Natural(self.truncate) if self.truncate == self.ceil
391
+ t
392
+ end
393
+ def to_N0
394
+ t = emptySet
395
+ t == Counting(self.truncate) if self.truncate == self.ceil
396
+ t
397
+ end
398
+ def to_Frac
399
+ Fraction(Zahlen(0), self.to_Q)
400
+ end
401
+ def to_Q
402
+ re = /^-/
403
+ md = re.match(self.to_s)
404
+ e = -1
405
+ e = 1 if md.to_a.empty?
406
+ md = nil
407
+ re = /(\-*\d+).(\d+)e([-|+])(\d+)/
408
+ md = re.match(self.to_s)
409
+ a = Quotient(md[1].to_i,1)
410
+ b = Quotient(md[2].to_i,10 ** md[2].to_i.to_s.length)
411
+ c = Quotient(1,10 ** md[4].to_i) if md[3] == "-"
412
+ c = Quotient(10 ** md[4].to_i,1) if md[3] == "+"
413
+ c*(b+(a*e))
414
+ end
415
+ def to_Dec
416
+ re = /(\-*\d+).(\d+)e([-|+])(\d+)/
417
+ md = re.match(self.to_s)
418
+ Decimal(md[1].to_i,md[2])
419
+ end
420
+ def to_Sig
421
+ Sigma(self.to_Q)
422
+ end
423
+ def to_K
424
+ Kettenbruch(self.to_Frac)
425
+ end
426
+ def to_R
427
+ emptySet
428
+ end
429
+ end
430
+ class Rational
431
+ include SGML
432
+ def to_sgml
433
+ "<mn #{sgml_id}class='rational'>#{self.to_s}</mn>"
434
+ end
435
+ end
436
+ def naught
437
+ NaughtClass.new!
438
+ end
439
+ def emptySet
440
+ EmptySetClass.new!
441
+ end
442
+ def nan
443
+ NANClass.new!
444
+ end
445
+ def infinity
446
+ InfinityClass.new!
447
+ end
448
+ def Natural(a)
449
+ if (a.kind_of?(Natural))
450
+ a
451
+ elsif (a.kind_of?(NaughtClass)) or (a.kind_of?(EmptySetClass))
452
+ emptySet
453
+ elsif (a.kind_of?(InfinityClass))
454
+ infinity
455
+ elsif (a.kind_of?(String))
456
+ Natural.new!(a.to_i)
457
+ elsif (a.kind_of?(Fixnum) && a >= 1) or (a.kind_of?(Bignum) && a >= 1)
458
+ Natural.new!(a)
459
+ elsif a.kind_of?(Numeric)
460
+ a.send :to_N
461
+ else
462
+ EmptySet
463
+ end
464
+ end
465
+ def Counting(a)
466
+ if (a.kind_of?(Fixnum) && a >= 0) or (a.kind_of?(Bignum) && a >= 0)
467
+ Counting.new!(a)
468
+ elsif (a.kind_of?(EmptySetClass))
469
+ emptySet
470
+ elsif (a.kind_of?(NaughtClass))
471
+ Counting.new!(0)
472
+ elsif (a.kind_of?(InfinityClass))
473
+ infinity
474
+ elsif (a.kind_of?(String))
475
+ Counting.new!(a.to_i)
476
+ elsif a.kind_of?(Numeric)
477
+ a.send :to_N0
478
+ else
479
+ EmptySet
480
+ end
481
+ end
482
+ def Zahlen(a)
483
+ if a.kind_of?(Zahlen)
484
+ a
485
+ elsif (a.kind_of?(EmptySetClass))
486
+ emptySet
487
+ elsif (a.kind_of?(NaughtClass))
488
+ Zahlen.new!(0)
489
+ elsif (a.kind_of?(InfinityClass))
490
+ infinity
491
+ elsif a.kind_of?(Fixnum) or a.kind_of?(Bignum)
492
+ Zahlen.new!(a)
493
+ elsif a.kind_of?(Numeric)
494
+ a.send :to_Z
495
+ else
496
+ emptySet
497
+ end
498
+ end
499
+ def Quotient(a,b=1)
500
+ if b < 0
501
+ a = -a
502
+ b = -b
503
+ end
504
+ if b == 0 or b.kind_of?(NaughtClass)
505
+ return infinity
506
+ elsif (a.kind_of?(NaughtClass))
507
+ Quotient(0,1)
508
+ elsif (a.kind_of?(EmptySetClass))
509
+ emptySet
510
+ elsif (a.kind_of?(InfinityClass))
511
+ infinity
512
+ elsif a.kind_of?(Quotient) && b == 1
513
+ a
514
+ elsif a.kind_of?(Numeric) && (b.kind_of?(Quotient)||b.kind_of?(Fraction)) # =8=
515
+ a/b
516
+ elsif (a.kind_of?(String))
517
+ re = /(\-*\d+)\/(\d+)/
518
+ md = re.match(a)
519
+ Quotient.new!(md[1].to_i,md[2].to_i)
520
+ elsif a.kind_of?(Float)
521
+ tmp = a.to_s.scan(/\d+/)
522
+ m= tmp.at(1).length
523
+ (Quotient.new!(((tmp.at(0).to_i)*(10**m)) + tmp.at(1).to_i,10**m)/(Quotient(b,1)))
524
+ elsif b.kind_of?(Quotient) && a == 1
525
+ Quotient.new!(b.denominator,b.numerator)
526
+ elsif Zahlen(a).kind_of?(Numeric) and Natural(b).kind_of?(Numeric)
527
+ unless a == b
528
+ Quotient.new!(Zahlen(a),Natural(b))
529
+ else
530
+ Quotient.new!(Zahlen(a),Natural(b))
531
+ end
532
+ elsif a.kind_of?(Quotient) && b.kind_of?(Quotient)
533
+ return a/b
534
+ elsif a.kind_of?(Numeric)
535
+ a.send :to_Q
536
+ else
537
+ emptySet
538
+ end
539
+ end
540
+ def Fraction(a,b=Quotient(0,1),c=1)
541
+ if a.kind_of?(Zahlen) && b.kind_of?(Natural) then
542
+ Fraction.new!(a,b,c)
543
+ elsif a.kind_of?(Zahlen) && b.kind_of?(Quotient)
544
+ if a>=0 and b>=0 then
545
+ Fraction.new!(a,b,c)
546
+ elsif a<0 and b<0
547
+ Fraction.new!(a*(-1),b*(-1),-1)
548
+ else
549
+ (a+b).to_Frac
550
+ end
551
+ # elsif a.kind_of?(Zahlen) && b.kind_of?(Fraction)
552
+ # Fraction(a,b.to_Q)
553
+ elsif Integer === a && b.kind_of?(Quotient)
554
+ if a>0 then
555
+ Fraction.new!(Zahlen(a),b,c)
556
+ else
557
+ Fraction.new!(Zahlen(a*(-1)),b,-1*c)
558
+ end
559
+ elsif a.kind_of?(Quotient) && b == Quotient(0,1)
560
+ Fraction.new!(a.numerator,a.denominator,c)
561
+ elsif a.kind_of?(Fraction) && b == Quotient(0,1)
562
+ a
563
+ elsif a.kind_of?(String) && b == Quotient(0,1)
564
+ a.to_Frac
565
+ elsif b == 1
566
+ a
567
+ elsif (a.kind_of?(NaughtClass))
568
+ Fraction.new!(Zahlen(0),b)
569
+ elsif (a.kind_of?(EmptySetClass))
570
+ emptySet
571
+ elsif (a.kind_of?(InfinityClass))
572
+ infinity
573
+ else
574
+ tmp = Quotient(a,b)
575
+ unless a == b or -a==b then
576
+ Fraction.new!(tmp.numerator,tmp.denominator)
577
+ else
578
+ a/b
579
+ end
580
+ end
581
+ end
582
+ def Decimal(a,b=nil,c='0',d=0,e=1)
583
+ if a.kind_of?(Decimal)
584
+ a
585
+ elsif (a.kind_of?(EmptySetClass))
586
+ emptySet
587
+ elsif (a.kind_of?(InfinityClass))
588
+ infinity
589
+ elsif a.kind_of?(String)
590
+ a.to_Dec
591
+ elsif (a.kind_of?(Fixnum) or a.kind_of?(Bignum)) and (b.kind_of?(Fixnum) or b.kind_of?(Bignum))
592
+ if e<0 then
593
+ Decimal.new!(a,b,c,d,-1)
594
+ else
595
+ if a<0 then
596
+ Decimal.new!(a,b,c,d,-1)
597
+ elsif a>0
598
+ Decimal.new!(a,b,c,d,1)
599
+ else
600
+ Decimal.new!(0,b,c,d,1)
601
+ end
602
+ end
603
+ elsif (a.kind_of?(Fixnum) or a.kind_of?(Bignum)) and b.nil?
604
+ if e<0 then
605
+ Decimal.new!(a,b,c,d,-1)
606
+ else
607
+ if a<0 then
608
+ Decimal.new!(a,nil,c,0,-1)
609
+ elsif a>0
610
+ Decimal.new!(a,nil,c,0,1)
611
+ else
612
+ Decimal.new!(0,nil,c,0,1)
613
+ end
614
+ end
615
+ elsif a.kind_of?(Numeric)
616
+ a.send :to_Dec
617
+ end
618
+ end
619
+ def Kettenbruch(a,b='',c=1,leicht=true)
620
+ if a.kind_of?(Kettenbruch)
621
+ a
622
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
623
+ a
624
+ elsif a.kind_of?(String)
625
+ a.to_a.to_K
626
+ elsif a.kind_of?(Proc) or a.kind_of?()
627
+ a
628
+ elsif (a.kind_of?(Fixnum) or a.kind_of?(Bignum)) and b.kind_of?(String)
629
+ if (a>0 and c<0) or (a<0 and c>0) then
630
+ Kettenbruch.new!(a,b,-1)
631
+ else
632
+ Kettenbruch.new!(a,b,1)
633
+ end
634
+ end
635
+ end
636
+ def Real()
637
+ end
638
+ def Matrix(m,n)
639
+ Matrix.instanciate(m,n)
640
+ end
641
+ def Tensor(m,*args)
642
+ Tensor.new!(m,*args)
643
+ end
644
+ class Natural < Numeric
645
+ include SGML
646
+ def to_sgml
647
+ "<mn #{sgml_id}class='natural'>#{@a}</mn>"
648
+ end
649
+ def Natural.new!(num)
650
+ new(num)
651
+ end
652
+ private_class_method :new
653
+ def initialize(a)
654
+ if (a.kind_of?(Fixnum) && a >= 1) or (a.kind_of?(Bignum) && a >= 1)
655
+ @a = a
656
+ else
657
+ emptySet
658
+ end
659
+ end
660
+ private :initialize
661
+ def + (a)
662
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
663
+ Natural(self.to_i + a.to_i)
664
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) or a.kind_of?(NilClass)
665
+ self
666
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
667
+ infinity
668
+ elsif
669
+ infinity
670
+ else
671
+ x, y = a.coerce(self)
672
+ Natural(x + y)
673
+ end
674
+ end
675
+ def - (a)
676
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
677
+ z = @a.to_i - a.to_i
678
+ p z
679
+ p z<=0
680
+ z <= 0 ? emptySet : Natural(z)
681
+
682
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
683
+ self
684
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
685
+ infinity
686
+ else
687
+ x, y = a.coerce(self)
688
+ Natural(x - y)
689
+ end
690
+ end
691
+ def * (a)
692
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
693
+ Natural(@a * a.to_i)
694
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
695
+ naught
696
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
697
+ infinity
698
+ else
699
+ x, y = a.coerce(self)
700
+ Natural(x * y)
701
+ end
702
+ end
703
+ def / (a)
704
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
705
+ Natural(self.to_i / a.to_i)
706
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
707
+ infinity
708
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
709
+ naught
710
+ else
711
+ end
712
+ end
713
+ def ** (a)
714
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
715
+ Natural(self.to_i ** a.to_i)
716
+ else
717
+ x, y = a.coerce(self)
718
+ Natural(x ** y)
719
+ end
720
+ end
721
+ def div(a)
722
+ self.to_i.div(a.to_i)
723
+ end
724
+ def % (a)
725
+ self.to_i.%(a.to_i)
726
+ end
727
+ def divmod(a)
728
+ super
729
+ end
730
+ def abs
731
+ Natural(@a)
732
+ end
733
+ def == (other)
734
+ @a == other
735
+ end
736
+ def next
737
+ self + Natural(1)
738
+ end
739
+ def succ
740
+ self.next
741
+ end
742
+ def <=>(other)
743
+ self.to_i <=> other.to_i
744
+ end
745
+ def coerce(other)
746
+ if Fixnum === other or Bignum === other
747
+ other <= 0 ? [emptySet,emptySet] : [other.to_N,self]
748
+ elsif Natural === other or Counting === other or Zahlen === other
749
+ other <= 0 ? [emptySet,emptySet] : [other,self.to_Z]
750
+ elsif Quotient === other
751
+ other <= 0 ? [emptySet,emptySet] : [other,self.to_Q]
752
+ elsif Fraction === other
753
+ other <= 0 ? [emptySet,emptySet] : [other,self.to_Frac]
754
+ else
755
+ other <= 0 ? [emptySet,emptySet] : [Float(other),@a.to_f]
756
+ end
757
+ end
758
+ def is_0?
759
+ @a === 0 ? true : false
760
+ end
761
+ def to_i
762
+ @a
763
+ end
764
+ def to_f
765
+ @a.to_f
766
+ end
767
+ def to_s!
768
+ "Natural(#{@a})"
769
+ end
770
+ def to_s
771
+ @a.to_s
772
+ end
773
+ def to_Q
774
+ Quotient(self, 1)
775
+ end
776
+ def inspect
777
+ sprintf("Natural(%s)", @a)
778
+ end
779
+ def hash
780
+ @a.hash
781
+ end
782
+ def gcd(n)
783
+ m = @a.to_i.abs
784
+ n = n.to_i.abs
785
+ return Naural(n) if m == 0
786
+ return Natural(m) if n == 0
787
+ b = 0
788
+ while n[0] == 0 && m[0] == 0
789
+ b += 1; n >>= 1; m >>= 1
790
+ end
791
+ m >>= 1 while m[0] == 0
792
+ n >>= 1 while n[0] == 0
793
+ while m != n
794
+ m, n = n, m if n > m
795
+ m -= n; m >>= 1 while m[0] == 0
796
+ end
797
+ # m << b
798
+ Natural(m << b)
799
+ end
800
+ def gcd2(int)
801
+ a = @a.abs
802
+ b = int.abs
803
+ a, b = b, a if a < b
804
+ while b != 0
805
+ void, a = a.divmod(b)
806
+ a, b = b, a
807
+ end
808
+ return a
809
+ end
810
+ def lcm(int)
811
+ a = @a.abs
812
+ b = int.abs
813
+ gcd = a.gcd(b)
814
+ (a.div(gcd)) * b
815
+ end
816
+ def gcdlcm(int)
817
+ a = @a.abs
818
+ b = int.abs
819
+ gcd = a.gcd(b)
820
+ return gcd, (a.div(gcd)) * b
821
+ end
822
+ class Prep
823
+ require "prime"
824
+ def initialize(int)
825
+ @comp = int
826
+ @prep = []
827
+ @max = 50000
828
+ b = []
829
+ @prms = Prime.each(@max){|a| b << a}
830
+ b = nil
831
+ Prime.each(int){|a|
832
+ moduli = int.modulo(a)
833
+ power = 0
834
+ b = int.divmod(a)
835
+ power = (int/b.at(0))/a if b.at(1) == 0
836
+ moduli = [power] if moduli == 0
837
+ @prep << moduli
838
+ }
839
+ end
840
+ attr_accessor :prep, :max
841
+ def max=(m = 500000)
842
+ b = []
843
+ @max = m
844
+ @prms = Prime.each(@max){|a| b << a}
845
+ end
846
+ def next
847
+ b = @prms
848
+ t =[]
849
+ @prep.each_index{|a|
850
+ t[a] = 1 if @prep.at(a).class == Array
851
+ z = (@prep.at(a) + 1).modulo(b.at(a)) unless @prep.at(a).class == Array
852
+ z = [((@comp+1)/(@comp+1).divmod(b.at(a)).at(0))/b.at(a)] if z == 0
853
+ t[a] = z unless @prep.at(a).class == Array
854
+ }
855
+ t << [1] if t == t.flatten
856
+ return t
857
+ end
858
+ def equivClassChck(s)
859
+ r = Array.new
860
+ r = eval(s)
861
+ r = r.uniq
862
+ r = r.sort
863
+ r.collect!{|i| i == r.find_index(i) ? nil : r.find_index(i)}
864
+ r = r.compact
865
+ r.at(0)
866
+ end
867
+ def deltaNextPrime
868
+ a = nil
869
+ res=[]
870
+ r =[]
871
+ r0=[]
872
+ r1=0
873
+ @prep.each_index{|x|
874
+ if @prep.at(x).class == Array then
875
+ t = "0 + #{@prms.at(x)}*k"
876
+ else # @prep.at(x).class == Numeric
877
+ t = "#{@prms.at(x)-@prep.at(x)}+(#{@prms.at(x)}*k)"
878
+ end
879
+ r << t
880
+ }
881
+ m = @prms.at(@prep.length-1)
882
+ rz = 0
883
+ rx = []
884
+ r.each{|r4|
885
+ rx << eval("k=0;#{r4}") }
886
+ ry = rx.max
887
+ (0..m).to_a.each{|k|
888
+ r.each{|r4|
889
+ r0 << eval("k=#{k};#{r4}")
890
+ }
891
+ rz= r0.max
892
+ res = res + r0
893
+ r0=[]
894
+ if ry < rz then
895
+ a = equivClassChck(res.to_s)
896
+ break unless a.nil?
897
+ ry = rz
898
+ else
899
+ end
900
+ }
901
+ a
902
+ end
903
+ def nextP(s)
904
+ return s + deltaNextPrime
905
+ end
906
+ end
907
+ def next_prime
908
+ #require "time"
909
+ a = Prep.new(self.to_i)
910
+ a.max = self.to_i
911
+ # Time.now
912
+ a.nextP(self.to_i)
913
+ end
914
+ end
915
+ class NaughtClass
916
+ def NaughtClass.new!
917
+ new
918
+ end
919
+ private_class_method :new
920
+ def initialize
921
+ end
922
+ private :initialize
923
+ include SGML
924
+ def to_sgml
925
+ "<mn #{sgml_id}class='naught'>0</mn>"
926
+ end
927
+ def div(a)
928
+ 0
929
+ end
930
+ def is_0?
931
+ true
932
+ end
933
+ def + (a)
934
+ return a
935
+ end
936
+ def - (a)
937
+ return a*(-1)
938
+ end
939
+ def * (a)
940
+ return self
941
+ end
942
+ def / (a)
943
+ nan
944
+ end
945
+ def ** (a)
946
+ 1
947
+ end
948
+ def to_i
949
+ 0
950
+ end
951
+ def to_f
952
+ 0.0
953
+ end
954
+ def to_s!
955
+ "naught"
956
+ end
957
+ def to_s
958
+ "0"
959
+ end
960
+ def to_N
961
+ emptySet
962
+ end
963
+ def to_Z
964
+ Zahlen(0)
965
+ end
966
+ def to_Frac
967
+ "0 0/1".to_Frac
968
+ end
969
+ def to_Q
970
+ Quotient(0,1)
971
+ end
972
+ def to_Dec
973
+ "0.0".to_Dec
974
+ end
975
+ def to_R
976
+ emptySet
977
+ end
978
+ def next
979
+ 1
980
+ end
981
+ def succ
982
+ 1
983
+ end
984
+ def <=>(other)
985
+ 0 <=> other.to_i
986
+ end
987
+ def coerce(other)
988
+ [other,0]
989
+ end
990
+ end
991
+ class EmptySetClass < NaughtClass
992
+ include SGML
993
+ def to_sgml
994
+ "<mn #{sgml_id}class='empty'>&empty</mn>"
995
+ end
996
+ def to_s!
997
+ "{}"
998
+ end
999
+ def to_s
1000
+ "\u2205".encode('utf-8')
1001
+ end
1002
+ end
1003
+ class NANClass < NaughtClass
1004
+ include SGML
1005
+ def to_sgml
1006
+ "<code #{sgml_id}class='notanumber'>NAN</code>"
1007
+ end
1008
+ end
1009
+ class InfinityClass
1010
+ include SGML
1011
+ def to_sgml
1012
+ "<mn #{sgml_id}class='infinity'>&infin</mn>"
1013
+ end
1014
+ def InfinityClass.new!
1015
+ new
1016
+ end
1017
+ private_class_method :new
1018
+ def initialize
1019
+ end
1020
+ private :initialize
1021
+ def is_0?
1022
+ false
1023
+ end
1024
+ def + (a)
1025
+ self
1026
+ end
1027
+ def - (a)
1028
+ self
1029
+ end
1030
+ def * (a)
1031
+ a === infinity ? nan : self
1032
+ end
1033
+ def / (a)
1034
+ a === infinity ? nan : 0 # infinitesimal
1035
+ end
1036
+ def ** (a)
1037
+ self
1038
+ end
1039
+ def to_i
1040
+ nil
1041
+ end
1042
+ def to_f
1043
+ nil
1044
+ end
1045
+ def to_s!
1046
+ "infinity"
1047
+ end
1048
+ def to_s
1049
+ "\u221E".encode('utf-8')
1050
+ end
1051
+ def to_N
1052
+ self
1053
+ end
1054
+ def to_Z
1055
+ self
1056
+ end
1057
+ def to_Frac
1058
+ self
1059
+ end
1060
+ def to_Q
1061
+ self
1062
+ end
1063
+ def to_Dec
1064
+ self
1065
+ end
1066
+ def to_R
1067
+ self
1068
+ end
1069
+ def next
1070
+ self
1071
+ end
1072
+ def succ
1073
+ self
1074
+ end
1075
+ def < (other)
1076
+ false
1077
+ end
1078
+ def > (other)
1079
+ true
1080
+ end
1081
+ def == (other)
1082
+ other.kind_of?(InfinityClass) ? true : false
1083
+ end
1084
+ def coerce(a)
1085
+ [infinity, infinity]
1086
+ end
1087
+ end
1088
+ class Counting < Natural
1089
+ include SGML
1090
+ def to_sgml
1091
+ "<mn #{sgml_id}class='counting'>#{@a}</mn>"
1092
+ end
1093
+ def Counting.new!(num)
1094
+ new(num)
1095
+ end
1096
+ private_class_method :new
1097
+ def initialize(a)
1098
+ if (a.kind_of?(Counting))
1099
+ @a = a.to_i
1100
+ elsif a.kind_of?(Fixnum) or a.kind_of?(Bignum)
1101
+ @a = a
1102
+ else
1103
+ end
1104
+ end
1105
+ def + (a)
1106
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
1107
+ Counting(@a+a)
1108
+ elsif a.kind_of?(Decimal) or a.kind_of?(Quotient) or a.kind_of?(Fraction)
1109
+ Counting(@a+a.to_N0)
1110
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
1111
+ self
1112
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1113
+ infinity
1114
+ else
1115
+ x, y = a.coerce(self)
1116
+ Counting(x+y)
1117
+ end
1118
+ end
1119
+ def - (a)
1120
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
1121
+ @a-a < 0 ? emptySet : Counting(@a-a)
1122
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
1123
+ self
1124
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1125
+ infinity
1126
+ else
1127
+ x, y = a.coerce(self)
1128
+ x-y < 0 ? emptySet : Counting(x - y)
1129
+ end
1130
+ end
1131
+ def * (a)
1132
+ t = nil
1133
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
1134
+ a < 0 ? emptySet : Counting(@a*a)
1135
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
1136
+ naught
1137
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1138
+ infinity
1139
+ else
1140
+ x, y = a.coerce(self)
1141
+ y < 0 ? emptySet : Counting(x*y)
1142
+ end
1143
+ end
1144
+ def / (a)
1145
+ if (a.kind_of?(Counting) or a.kind_of?(Natural)) and (self.to_i%(a.to_i) == 0)
1146
+ a < 0 ? emptySet : Counting(@a/ a)
1147
+ else
1148
+ x, y = a.coerce(self)
1149
+ y < 0 ? emptySet : Counting(x/y)
1150
+ end
1151
+ end
1152
+ def ** (a)
1153
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
1154
+ Counting(self.to_i ** a.to_i)
1155
+ else
1156
+ x, y = a.coerce(self)
1157
+ Counting(x ** y)
1158
+ end
1159
+ end
1160
+ def % (a)
1161
+ self.to_i%(a.to_i)
1162
+ end
1163
+ def abs
1164
+ @a.to_i.abs
1165
+ end
1166
+ def <=> (other)
1167
+ unless other.kind_of?(Counting) then other = Counting.new!(other) end
1168
+ if @a > other
1169
+ return 1
1170
+ elsif @a < other
1171
+ return -1
1172
+ elsif @a == other
1173
+ return 0
1174
+ end
1175
+ end
1176
+ def coerce(other)
1177
+ if Integer === other
1178
+ [other, @a]
1179
+ else Float === other
1180
+ return other, self.to_f
1181
+ end
1182
+ end
1183
+ def next
1184
+ self + Counting(1)
1185
+ end
1186
+ def succ
1187
+ self.next
1188
+ end
1189
+ def <=>(other)
1190
+ self.to_i <=> other.to_i
1191
+ end
1192
+ def each
1193
+ yield Counting(@a+1)
1194
+ end
1195
+ def to_i
1196
+ @a.to_i
1197
+ end
1198
+ def to_f
1199
+ @a.to_f
1200
+ end
1201
+ def to_s!
1202
+ "Counting(#{@a})"
1203
+ end
1204
+ def to_s
1205
+ @a.to_s
1206
+ end
1207
+ def to_N
1208
+ Natural(@a)
1209
+ end
1210
+ def to_Z
1211
+ Zahlen(@a)
1212
+ end
1213
+ def to_Frac
1214
+ Fraction(@a,1)
1215
+ end
1216
+ def to_Q
1217
+ Quotient(@a,1)
1218
+ end
1219
+ def to_Dec
1220
+ Decimal(@a)
1221
+ end
1222
+ def to_Sig
1223
+ Sigma(self.to_Q)
1224
+ end
1225
+ def to_K
1226
+ Kettenbruch(self.to_Frac)
1227
+ end
1228
+ def to_R
1229
+ end
1230
+ def inspect
1231
+ sprintf("Counting(%s)", @a)
1232
+ end
1233
+ def hash
1234
+ @a.hash
1235
+ end
1236
+ end
1237
+ class Zahlen < Counting
1238
+ include SGML
1239
+ def to_sgml
1240
+ "<mn #{sgml_id}class='zahlen'>#{@a}</mn>"
1241
+ end
1242
+ def Zahlen.new!(num)
1243
+ new(num)
1244
+ end
1245
+ private_class_method :new
1246
+ def initialize(a)
1247
+ if (a.kind_of?(Zahlen))
1248
+ @a = a.to_i
1249
+ elsif a.kind_of?(Fixnum) or a.kind_of?(Bignum)
1250
+ @a = a
1251
+ else
1252
+ end
1253
+ end
1254
+ def + (a)
1255
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
1256
+ Zahlen(self.to_i + a.to_i)
1257
+ elsif a.kind_of?(Quotient) or a.kind_of?(Fraction)
1258
+ a + Quotient(@a,1)
1259
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
1260
+ self
1261
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1262
+ infinity
1263
+ else
1264
+ x, y = a.coerce(self)
1265
+ x + y
1266
+ end
1267
+ end
1268
+ def - (a)
1269
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
1270
+ Zahlen(self.to_i - a.to_i)
1271
+ elsif a.kind_of?(Quotient) or a.kind_of?(Fraction)
1272
+ a - Quotient(@a,1)
1273
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
1274
+ self
1275
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1276
+ infinity*(-1)
1277
+ else
1278
+ x, y = a.coerce(self)
1279
+ Zahlen(x - y)
1280
+ end
1281
+ end
1282
+ def * (a)
1283
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
1284
+ Zahlen(self.to_i * a.to_i)
1285
+ elsif a.kind_of?(Quotient) or a.kind_of?(Fraction)
1286
+ a * Quotient(@a,1)
1287
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) or a.nil?
1288
+ naught
1289
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1290
+ infinity
1291
+ else
1292
+ x , y = a.coerce(self)
1293
+ Zahlen(x * y)
1294
+ end
1295
+ end
1296
+ def / (a)
1297
+ if (a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)) and self.to_i%(a.to_i) == 0
1298
+ Zahlen(self.to_i / a.to_i)
1299
+ elsif a.kind_of?(Quotient) or a.kind_of?(Fraction)
1300
+ self * Quotient(a.to_Q.denominator,a.to_Q.numerator)
1301
+ else
1302
+ emptySet
1303
+ end
1304
+ end
1305
+ def ** (a)
1306
+ if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural)
1307
+ Zahlen(self.to_i ** a.to_i)
1308
+ else
1309
+ x, y = a.coerce(self)
1310
+ Zahlen(x ** y)
1311
+ end
1312
+ end
1313
+ def % (a)
1314
+ self.to_i%(a.to_i)
1315
+ end
1316
+ def abs
1317
+ @a.to_i.abs
1318
+ end
1319
+ def gcd(n)
1320
+ g = nil
1321
+ if @a>0 then
1322
+ g = Natural( @a).gcd(n)
1323
+ else
1324
+ g = Natural(-1* @a).gcd(n)
1325
+ end
1326
+ g
1327
+ end
1328
+ def coerce(other)
1329
+ # if other.kind_of?(Float) then
1330
+ # other, @a
1331
+ # elsif other.kind_of?(Numeric)
1332
+ # return Zahlen.new!(other.to_i), self
1333
+ # else
1334
+ # other, @a
1335
+ # end
1336
+ [other, @a]
1337
+ end
1338
+ def next
1339
+ self + Zahlen(1)
1340
+ end
1341
+ def succ
1342
+ self.next
1343
+ end
1344
+ def <=>(other)
1345
+ self.to_i <=> other.to_i
1346
+ end
1347
+ def to_i
1348
+ @a.to_i
1349
+ end
1350
+ def to_f
1351
+ @a.to_f
1352
+ end
1353
+ def to_s!
1354
+ "Zahlen(#{@a})"
1355
+ end
1356
+ def to_s
1357
+ @a.to_s
1358
+ end
1359
+ def to_N
1360
+ Natural(@a)
1361
+ end
1362
+ def to_Z
1363
+ Zahlen(@a)
1364
+ end
1365
+ def to_Frac
1366
+ Fraction(@a,Quotient(0,1))
1367
+ end
1368
+ def to_Q
1369
+ Quotient(@a,1)
1370
+ end
1371
+ def to_Dec
1372
+ Decimal(@a,0,'0')
1373
+ end
1374
+ def to_Sig
1375
+ Sigma(self.to_Q)
1376
+ end
1377
+ def to_K
1378
+ Kettenbruch(self.to_Frac)
1379
+ end
1380
+ def to_R
1381
+ end
1382
+ def inspect
1383
+ sprintf("Zahlen(%s)", @a.to_s)
1384
+ end
1385
+ def next_prime
1386
+ emptySet
1387
+ end
1388
+ end
1389
+ class Quotient < Numeric
1390
+ @@delta = 0
1391
+ include SGML
1392
+ def to_sgml
1393
+ "<mfrac #{sgml_id}class='quotient'><mn>#{@numerator*@abs1}</mn> <mn>#{@denominator}</mn></mfrac>"
1394
+ end
1395
+ def Quotient.new!(a, b = 1)
1396
+ if a.kind_of?(Quotient) && b == 1
1397
+ a
1398
+ else
1399
+ new(a, b)
1400
+ end
1401
+ end
1402
+ def initialize(a, b)
1403
+ @to_Dec
1404
+ if a < 0 or b < 0 then
1405
+ a = -1*a if a < 0
1406
+ a = -1*b if b < 0
1407
+ @abs1 = -1
1408
+ elsif (a>= 0 and b >= 0) or (a<0 and b<0)
1409
+ a = -1*a if (a<0 and b<0)
1410
+ b = -1*b if (a<0 and b<0)
1411
+ @abs1 = 1
1412
+ end
1413
+ if a.kind_of?(Numeric) and b.kind_of?(Numeric)
1414
+ a.kind_of?(Zahlen) ? @a = a : @a = Zahlen(a.to_i)
1415
+ b.kind_of?(Natural) ? @b = b : @b = Natural(b.to_i)
1416
+ unless a == Zahlen(0) or a==0 or a == naught then
1417
+ gcd = Natural(a).gcd(b)
1418
+ @numerator = @a.div(gcd)
1419
+ @denominator = @b.div(gcd)
1420
+ else
1421
+ @numerator = 0
1422
+ @denominator = 1
1423
+ end
1424
+ @setdelta = false
1425
+ @deltaIter = 10
1426
+ @remainders = []
1427
+ @reg2 = []
1428
+ end
1429
+ end
1430
+ attr_accessor :a,:b, :abs1, :numerator, :denominator, :deltaIter
1431
+ private_class_method :new
1432
+ private :initialize
1433
+ def setdelta
1434
+ @setdelta = true
1435
+ end
1436
+ def + (a)
1437
+ if a == 0
1438
+ return self
1439
+ elsif @numerator == 0 and @denominator == 1
1440
+ return a.to_Q
1441
+ elsif a.kind_of?(Fraction)
1442
+ self + a.to_Q
1443
+ elsif a.kind_of?(Quotient)
1444
+ num = (@numerator*@abs1) * (a.denominator)
1445
+ num_a = (a.numerator*a.abs1) * @denominator
1446
+ Quotient(num + num_a, @denominator * a.denominator)
1447
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
1448
+ self
1449
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1450
+ infinity
1451
+ elsif (a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum))
1452
+ self + Quotient(a, 1)
1453
+ elsif a.kind_of?(Float)
1454
+ Float(self) + a
1455
+ else
1456
+ x, y = a.coerce(self)
1457
+ x + y
1458
+ end
1459
+ end
1460
+ def - (a)
1461
+ if a == 0
1462
+ return self
1463
+ elsif @numerator == 0 and @denominator == 1
1464
+ return a.to_Q
1465
+ else
1466
+ if a.kind_of?(Quotient)
1467
+ num = (@numerator*@abs1) * (a.denominator)
1468
+ num_a = (a.numerator*a.abs1) * @denominator
1469
+ Quotient(num - num_a, @denominator * a.denominator)
1470
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) or a == 0
1471
+ self
1472
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1473
+ infinity*(-1)
1474
+ elsif (a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum))
1475
+ self - Quotient.new!(a, 1)
1476
+ elsif a.kind_of?(Float)
1477
+ Float(self) - a
1478
+ else
1479
+ x, y = a.coerce(self)
1480
+ x - y
1481
+ end
1482
+ end
1483
+ end
1484
+ def * (a)
1485
+ if a == 0 or ( @numerator == 0 and @denominator == 1)
1486
+ return Quotient(0,1)
1487
+ elsif a.kind_of?(Quotient)
1488
+ abs = @abs1 * a.abs1
1489
+ num = @numerator * a.numerator
1490
+ den = @denominator * a.denominator
1491
+ Quotient(num*abs, den)
1492
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
1493
+ naught
1494
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1495
+ infinity
1496
+ elsif (a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum))
1497
+ if a ==1 then
1498
+ self
1499
+ else
1500
+ self * Quotient(a, 1)
1501
+ end
1502
+ elsif a.kind_of?(Float)
1503
+ self * a.to_Q
1504
+ else
1505
+ x, y = a.coerce(self)
1506
+ x * y
1507
+ end
1508
+ end
1509
+ def / (a)
1510
+ if (@numerator == 0 and @denominator == 1)
1511
+ return Quotient(0,1)
1512
+ elsif a.kind_of?(Quotient)
1513
+ abs = @abs1 * a.abs1
1514
+ num = @numerator * a.denominator
1515
+ den = @denominator * a.numerator
1516
+ Quotient(num*abs, den)
1517
+ elsif a.kind_of?(Integer)
1518
+ raise ZeroDivisionError, "division by zero" if a == 0
1519
+ self / Quotient.new!(a, 1)
1520
+ elsif a.kind_of?(Float)
1521
+ Float(self) / a
1522
+ else
1523
+ x, y = a.coerce(self)
1524
+ x / y
1525
+ end
1526
+ end
1527
+ def ** (other)
1528
+ if other.kind_of?(Quotient)
1529
+ Float(self) ** other
1530
+ elsif other.kind_of?(Integer)
1531
+ if other > 0
1532
+ num = @numerator ** other
1533
+ den = @denominator ** other
1534
+ elsif other < 0
1535
+ num = @denominator ** -other
1536
+ den = @numerator ** -other
1537
+ elsif other == 0
1538
+ num = 1
1539
+ den = 1
1540
+ end
1541
+ tmp = Quotient.new!(num, den)
1542
+ Quotient(tmp.numerator,tmp.denominator)
1543
+ elsif other.kind_of?(Float)
1544
+ Float(self) ** other
1545
+ else
1546
+ x, y = other.coerce(self)
1547
+ x ** y
1548
+ end
1549
+ end
1550
+ def % (other)
1551
+ value = (self / other).to_i
1552
+ return self - other * value
1553
+ end
1554
+ def diavmod(other)
1555
+ value = (self / other).to_i
1556
+ return value, self - other * value
1557
+ end
1558
+ def abs
1559
+ @abs1 = -1 ? Quotient.new!(@numerator, @denominator) : self
1560
+ end
1561
+ def == (other)
1562
+ if other.kind_of?(Quotient)
1563
+ @numerator == other.numerator and @denominator == other.denominator
1564
+ elsif other.kind_of?(Integer)
1565
+ self == Quotient.new!(other, 1)
1566
+ elsif other.kind_of?(Float)
1567
+ Float(self) == other
1568
+ else
1569
+ other.to_i == self.to_i
1570
+ end
1571
+ end
1572
+ def === (other)
1573
+ if other.kind_of?(Quotient)
1574
+ @a == other.a and @b == other.b
1575
+ return 1
1576
+ elsif other.kind_of?(Quotient)
1577
+ @numerator == other.numerator and @denominator == other.denominator
1578
+ return -1
1579
+ else
1580
+ return 0
1581
+ end
1582
+ end
1583
+ def <=> (other)
1584
+ if @setdelta then
1585
+ @@delta = Quotient(((other - self).abs)/@deltaIter) if (other.abs1 == 1 and self.abs1 == 1) or (other.abs1 == -1 and self.abs1 == -1)
1586
+ @@delta = Quotient((Quotient(other.numerator,other.denominator) + Quotient(self.numerator,self.denominator))/@deltaIter) if (other.abs1 == 1 and self.abs1 == -1) or (other.abs1 == -1 and self.abs1 == 1)
1587
+ end
1588
+ @setdelta = false
1589
+ if other.kind_of?(Quotient)
1590
+ num = (@numerator*@abs1) * other.denominator
1591
+ num_o = (other.numerator*other.abs1) * @denominator
1592
+ v = num- num_o
1593
+ if v > 0
1594
+ return 1
1595
+ elsif v < 0
1596
+ return -1
1597
+ else
1598
+ return 0
1599
+ end
1600
+ elsif other.kind_of?(Integer)
1601
+ return self <=> Quotient.new!(other, 1)
1602
+ elsif other.kind_of?(Float)
1603
+ return Float(self) <=> other
1604
+ elsif defined? other.coerce
1605
+ x, y = other.coerce(self)
1606
+ return x <=> y
1607
+ else
1608
+ return nil
1609
+ end
1610
+ end
1611
+ def succ
1612
+ self + @@delta
1613
+ end
1614
+ def coerce(other)
1615
+ if Natural === other or Counting === other or Zahlen === other
1616
+ [Quotient(other,1),self]
1617
+ elsif Fixnum === other
1618
+ [other, self.to_f]
1619
+ else
1620
+ [Float(other),self.to_f]
1621
+ end
1622
+ end
1623
+ def is_0?
1624
+ @numerator === 0 and @denominator === 1 ? true : false
1625
+ end
1626
+ def to_i
1627
+ Integer(@numerator.div(@denominator))
1628
+ end
1629
+ def to_f
1630
+ @numerator.to_f/@denominator.to_f
1631
+ end
1632
+ def to_Frac
1633
+ self >= 0 ? Fraction(Zahlen(@numerator),Natural(@denominator),1) : Fraction(Zahlen(@numerator),Natural(@denominator),-1)
1634
+ end
1635
+ def to_Dec
1636
+ @to_Dec ||= q2Dec(@numerator.remainder(@denominator))
1637
+ end
1638
+ def to_Sig
1639
+ Sigma(self)
1640
+ end
1641
+ def to_K
1642
+ Kettenbruch(self.to_Frac)
1643
+ end
1644
+ def q2Dec(remainder)
1645
+ maxcount =1000
1646
+ kmcount = 0
1647
+ ret = nil
1648
+ until (@remainders.member?(remainder) and @remainders.index(remainder)!= 0) or (kmcount > maxcount + 100) do # =8= KM
1649
+ break if remainder == 0
1650
+ @remainders << remainder
1651
+ @reg2 << remainder.div(@denominator.to_i)
1652
+ remainder = remainder.remainder(@denominator.to_i)*10
1653
+ kmcount += 1 # =8= KM
1654
+ end
1655
+ @reg2.shift
1656
+ if remainder == 0
1657
+ if @abs1 ==1 then
1658
+ ret = "Decimal('#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}')"
1659
+ elsif @abs1 == -1
1660
+ ret = "Decimal('-#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}')"
1661
+ end
1662
+ else
1663
+ # @reg2.insert(@remainders.find_index(remainder)-1,"[")
1664
+ @reg2.insert(@remainders.find_index(remainder)-1,"[") unless (kmcount > maxcount + 100)
1665
+ if @abs1 ==1 then
1666
+ ret = "Decimal('#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}]')" unless (kmcount > maxcount + 100)
1667
+ ret = "Decimal('#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}[0]')" if (kmcount > maxcount + 100) # =8= KM
1668
+ elsif @abs1 == -1
1669
+ ret = "Decimal('-#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}]')" unless (kmcount > maxcount + 100)
1670
+ ret = "Decimal('#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}[0]')" if (kmcount > maxcount + 100) # =8= KM
1671
+ end
1672
+ end
1673
+ eval(ret)
1674
+ end
1675
+ def to_s!
1676
+ "Quotient(#{(@a*@abs1).to_s},#{@b.to_s})"
1677
+ end
1678
+ def to_s
1679
+ if @b == 1
1680
+ (@a*@abs1).to_s
1681
+ else
1682
+ "#{@numerator*@abs1}/#{@denominator}"
1683
+ end
1684
+ end
1685
+ def inspect
1686
+ @abs1 == -1 ? sprintf("Quotient(-%s, %s)", @numerator.inspect, @denominator.inspect) : sprintf("Quotient(%s, %s)", @numerator.inspect, @denominator.inspect)
1687
+ end
1688
+ def hash
1689
+ @numerator.hash ^ @denominator.hash ^ @abs.hash
1690
+ end
1691
+ end
1692
+ class Decimal < Numeric
1693
+ def Decimal.new!(a,b,c=0,d=0,e=1)
1694
+ new(a,b,c,d,e)
1695
+ end
1696
+ @@decimalSeparator = "." #,
1697
+ @@precision = 100
1698
+ include SGML
1699
+ def to_sgml
1700
+ "<mn #{sgml_id}class='decimal'>#{self.to_s}</mn>"
1701
+ end
1702
+ def Decimal::decimalSeparator
1703
+ @@decimalSeparator
1704
+ end
1705
+ def Decimal::precision
1706
+ @@precision
1707
+ end
1708
+ def Decimal::precision=(a)
1709
+ if a.class == Integer then
1710
+ @@precision = a
1711
+ else
1712
+ @@precision = a.to_i
1713
+ end
1714
+ end
1715
+ @@delta = 0
1716
+ def initialize(a,b,c='0',d=0,e=1)
1717
+ @integraldecimal = a #integraldecimal1 is the equivalent representation
1718
+ @decimalfraction = b
1719
+ @integraldecimal_1 = a
1720
+ @repetend = Proc.new{c}
1721
+ @decimalExponent = d
1722
+ @absolute = e
1723
+ @to_Frac
1724
+ if a == 0 and b.nil? and c == 0 and d == 0 then
1725
+ @repetend_1= @repetend
1726
+ @decimalfraction_1 = @decimalfraction
1727
+ else
1728
+ if @repetend.call =="0" then
1729
+ @repetend_1= Proc.new{"9"}
1730
+ @decimalfraction_1 = b-1 unless b.nil?
1731
+ @integraldecimal_1 = a-1 if b.nil?
1732
+ elsif @repetend.call =="9" then
1733
+ @repetend_1= Proc.new{"0"}
1734
+ @decimalfraction_1 = b+1 unless b.nil?
1735
+ @integraldecimal_1 = a+1 if b.nil?
1736
+ else
1737
+ @decimalfraction_1 = b
1738
+ end
1739
+ end
1740
+ @setdelta = false
1741
+ @deltaIter = 10
1742
+ end
1743
+ attr_accessor :precision, :integraldecimal, :decimalfraction, :repetend,:integraldecimal_1, :decimalfraction_1, :repetend_1, :decimalExponent, :patt_succ, :absolute
1744
+ private_class_method :new
1745
+ private :initialize
1746
+ def setdelta
1747
+ @setdelta = true
1748
+ end
1749
+ def to_digits
1750
+ if self.nan? || self.infinite? || self.zero?
1751
+ self.to_s
1752
+ else
1753
+ i = self.to_i.to_s
1754
+ s,f,y,z = self.frac.split
1755
+ i + "." + ("0"*(-z)) + f
1756
+ end
1757
+ end
1758
+ def showRE(a,re)
1759
+ if a =~ re
1760
+ "#{$`}<<#{$&}>>#{$'}"
1761
+ else
1762
+ "no match"
1763
+ end
1764
+ end
1765
+ def isRE?(a,re)
1766
+ if a =~ re
1767
+ true
1768
+ else
1769
+ false
1770
+ end
1771
+ end
1772
+ def +(a)
1773
+ if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
1774
+ naught
1775
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1776
+ infinity
1777
+ else
1778
+ ((self.to_Q) + (a.to_Q)).to_Dec
1779
+ end
1780
+ end
1781
+ def -(a)
1782
+ if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
1783
+ naught
1784
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1785
+ infinity
1786
+ else
1787
+ ((self.to_Q) - (a.to_Q)).to_Dec
1788
+ end
1789
+ end
1790
+ def plus(a) #forces Decimal class addition working
1791
+ ret = nil
1792
+ pat = nil
1793
+ if a.kind_of?(Decimal)
1794
+ if (self.absolute == 1 and a.absolute == 1) then
1795
+ s0 = ""
1796
+ s0 = "0"*a.decimalExponent if a.decimalExponent>0
1797
+ s = "#{s0}#{a.decimalfraction}"
1798
+ t0 = ""
1799
+ t0 = "0"*@decimalExponent if @decimalExponent>0
1800
+ t = "#{t0}#{@decimalfraction}"
1801
+ if s.length > t.length then
1802
+ m = s.length-t.length
1803
+ if m < self.repetend.call.length then
1804
+ t << self.repetend.call.slice(a.repetend.call.length - m)
1805
+ elsif m >= self.repetend.call.length
1806
+ t << self.repetend.call
1807
+ t = eval("#{m-self.repetend.call.length}.times{self.patt_succ(#{t})}")
1808
+ end
1809
+ elsif s.length < t.length then
1810
+ m = t.length-s.length
1811
+ if m < a.repetend.call.length then
1812
+ t << a.repetend.call.slice(a.repetend.call.length - m)
1813
+ elsif m >= a.repetend.call.length
1814
+ t << a.repetend.call
1815
+ t = eval("#{a.repetend.call.length-m}.times{a.patt_succ(#{t})}")
1816
+ end
1817
+ else
1818
+ p "raise an interesting and impossible error" unless s.length == t.length
1819
+ end
1820
+ m = a.repetend.call
1821
+ n = self.repetend.call
1822
+ k=m.length
1823
+ l=n.length
1824
+ (l-1).times{m = m + a.repetend.call }
1825
+ (k-1).times{n = n + self.repetend.call}
1826
+ pat = m.to_i+n.to_i
1827
+ pat = pat.to_s
1828
+ v = "1#{t}".to_i + "1#{s}".to_i
1829
+ v = v.to_s[0].to_i - 2
1830
+ u = (self.integraldecimal + a.integraldecimal + v).to_s.length
1831
+ ret = ("#{self.integraldecimal}#{t}".to_i + "#{a.integraldecimal}#{s}".to_i).to_s
1832
+ ret = ret[0,u] + @@decimalSeparator + ret[u,ret.length]
1833
+ ret = ret + "[" + pat + "]" unless pat.nil?
1834
+ elsif self.absolute == -1 and a.absolute == -1 then
1835
+ t = self
1836
+ t.absolute = 1
1837
+ ret = (t + a)
1838
+ ret.absolute = -1
1839
+ elsif self.absolute == -1 and a.absolute == 1 then
1840
+ ret = a - self
1841
+ elsif self.absolute == 1 and a.absolute == -1 then
1842
+ ret = self - a
1843
+ end
1844
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
1845
+ self
1846
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1847
+ infinity
1848
+ end
1849
+ ret.to_Dec
1850
+ end
1851
+ def minus(a) #forces Decimal class subtraction NOT working
1852
+ ret = nil
1853
+ pat = nil
1854
+ if a.kind_of?(Decimal)
1855
+ s0 = ""
1856
+ s0 = "0"*a.decimalExponent if a.decimalExponent>0
1857
+ s = "#{s0}#{a.decimalfraction}"
1858
+ t0 = ""
1859
+ t0 = "0"*@decimalExponent if @decimalExponent>0
1860
+ t = "#{t0}#{@decimalfraction}"
1861
+ if s.length > t.length then
1862
+ m = s.length+t.length+1
1863
+ if m < self.repetend.call.length then
1864
+ t << self.repetend.call.slice(self.repetend.call.length - m)
1865
+ elsif m >= self.repetend.call.length
1866
+ t << self.repetend.call
1867
+ t = eval("#{self.repetend.call.length-m}.times{self.patt_succ(#{t})}")
1868
+ end
1869
+ elsif s.length < t.length then
1870
+ m = t.length-s.length
1871
+ if m < a.repetend.call.length then
1872
+ t << a.repetend.call.slice(a.repetend.call.length - m)
1873
+ elsif m >= a.repetend.call.length
1874
+ t << a.repetend.call
1875
+ t0 = t.length
1876
+ t = eval("#{t0-m}.times{a.patt_succ('0#{@@decimalSeparator}#{t}')}")
1877
+ end
1878
+ else
1879
+ p "raise an interesting and impossible error" unless s.length == t.length
1880
+ end
1881
+ m = a.repetend.call
1882
+ n = self.repetend.call
1883
+ k=m.length
1884
+ l=n.length
1885
+ (l-1).times{m = m + a.repetend.call }
1886
+ (k-1).times{n = n + self.repetend.call}
1887
+ minus = 1
1888
+ if self.integraldecimal < a.integraldecimal then
1889
+ minus = -1
1890
+ pat = m.to_i-n.to_i
1891
+ ret = "[" + pat.to_s + "]" unless pat.nil?
1892
+ ret = (a.integraldecimal-self.integraldecimal).to_s + @@decimalSeparator + (s.to_i - t.to_i).to_s + ret
1893
+ elsif self.integraldecimal > a.integraldecimal then
1894
+ pat = n.to_i-m.to_i
1895
+ ret = "[" + pat.to_s + "]" unless pat.nil?
1896
+ ret = (self.integraldecimal-a.integraldecimal).to_s + @@decimalSeparator + (t.to_i - s.to_i).to_s + ret
1897
+ else
1898
+ if "1#{t}".to_i < "1#{s}".to_i then
1899
+ minus = -1
1900
+ pat = n.to_i-m.to_i
1901
+ ret = "[" + pat.to_s + "]" unless pat.nil?
1902
+ ret = (a.integraldecimal-self.integraldecimal).to_s + @@decimalSeparator + (s.to_i - t.to_i).to_s + ret
1903
+ elsif "1#{t}#{n}".to_i > "1#{s}#{m}".to_i
1904
+ pat = m.to_i-n.to_i
1905
+ ret = "[" + pat.to_s + "]" unless pat.nil?
1906
+ ret = (self.integraldecimal-a.integraldecimal).to_s + @@decimalSeparator + (t.to_i - s.to_i).to_s + ret
1907
+ else
1908
+ end
1909
+ end
1910
+ ret = ret.to_Dec
1911
+ ret.absolute = -1 if minus == -1
1912
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
1913
+ ret = naught
1914
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1915
+ ret = infinity
1916
+ end
1917
+ ret
1918
+ end
1919
+ def * (a)
1920
+ if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
1921
+ naught
1922
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
1923
+ infinity
1924
+ else
1925
+ ((self.to_Q) * (a.to_Q)).to_Dec
1926
+ end
1927
+ end
1928
+ def / (a)
1929
+ ((self.to_Q) /(a.to_Q)).to_Dec
1930
+ end
1931
+ def ** (a)
1932
+ self.to_f ** a.to_f
1933
+ end
1934
+ def <=>(other)
1935
+ @@delta = Decimal((other - self) /@deltaIter) if @setdelta
1936
+ @setdelta = false
1937
+ ret = nil
1938
+ if other.kind_of?(Decimal)
1939
+ if self == other then
1940
+ ret = 0
1941
+ elsif self.integraldecimal < other.integraldecimal then
1942
+ ret = -1
1943
+ elsif self.integraldecimal > other.integraldecimal then
1944
+ ret = 1
1945
+ elsif self.integraldecimal == other.integraldecimal then
1946
+ s = self.to_s
1947
+ t = other.to_s
1948
+ k = s.length
1949
+ l = t.length
1950
+ if k>l then
1951
+ m = k-l
1952
+ eval("#{m}.times{other.patt_succ(t)}")
1953
+ elsif k<l then
1954
+ m = l-k
1955
+ eval("#{m}.times{self.patt_succ(s)}")
1956
+ end
1957
+ s.gsub("#{@@decimalSeparator}","") <=> t.gsub("#{@@decimalSeparator}","")
1958
+ end
1959
+ elsif other.kind_of?(Numeric)
1960
+ ret = self <=> other.to_Dec
1961
+ end
1962
+ ret
1963
+ end
1964
+ def == (other)
1965
+ if other.kind_of?(Decimal) then
1966
+ (self.integraldecimal == other.integraldecimal or
1967
+ self.integraldecimal == other.integraldecimal_1
1968
+ ) and(
1969
+ ( self.repetend.call == other.repetend.call and
1970
+ self.decimalfraction == other.decimalfraction
1971
+ ) or (self.decimalfraction_1 == other.decimalfraction
1972
+ ) or (self.decimalfraction == other.decimalfraction_1
1973
+ )
1974
+ )
1975
+ elsif other.kind_of?(Float) or other.kind_of?(Quotient) or other.kind_of?(Fraction) # =8= cr
1976
+ self == other.to_Dec
1977
+ else
1978
+ other.to_i == self.to_i
1979
+ end
1980
+ end
1981
+ def succ
1982
+ self + @@delta
1983
+ end
1984
+ def patt_succ(t)
1985
+ e = ""
1986
+ e = "-" if @absolute == -1
1987
+ t0 = ""
1988
+ t0 = "0"*(@decimalExponent - 1) if @decimalExponent>0
1989
+ t1 = "#{e}#{@integraldecimal}\\#{@@decimalSeparator}#{t0}#{@decimalfraction}"
1990
+ re = /#{t1}(\d+)/
1991
+ md = re.match(t)
1992
+ t << @repetend.call.chars.to_a.at(md[1].length.modulo(@repetend.call.length))
1993
+ end
1994
+ def is_0?
1995
+ @integraldecimal === 0 and @decimalfraction.nil? and @repetend.call == "0" and @decimalExponent ==0 ? true : false
1996
+ end
1997
+ def to_f
1998
+ self.to_s.to_f
1999
+ end
2000
+ def to_N
2001
+ @decimalfraction.nil? and @repetend.call == "0" and @decimalExponent ==0 ? Natural(@integraldecimal) : emptySet
2002
+ end
2003
+ def to_N0
2004
+ @decimalfraction.nil? and @repetend.call == "0" and @decimalExponent ==0 ? Counting(@integraldecimal) : emptySet
2005
+ end
2006
+ def to_Z
2007
+ @decimalfraction.nil? and @repetend.call == "0" and @decimalExponent ==0 ? Zahlen(@integraldecimal) : emptySet
2008
+ end
2009
+ def d2Frac
2010
+ t0 = Quotient(0,1)
2011
+ s = @decimalfraction.to_s.length > 0 ? @decimalfraction.to_s.length - 1 : 0
2012
+ #p "decimal fraction part"
2013
+ s == 0 ? t0 = Quotient(@decimalfraction.nil? ? 0 : @decimalfraction,("1" + ("0"*@decimalExponent)).to_i) : t0 = Quotient(@decimalfraction.nil? ? 0 : @decimalfraction,("1" +("0"* (@decimalfraction.to_s.length)) + ("0"*@decimalExponent)).to_i)
2014
+ #p "repetend part"
2015
+ t1 = Quotient(Zahlen(@repetend.call.to_i),Zahlen((("9"*@repetend.call.length)+("0"* s)+("0"*@decimalExponent)).to_i))
2016
+ Fraction(Zahlen(@integraldecimal),(t0 + Fraction(Zahlen(0),t1)),@absolute)
2017
+ #Fraction(Zahlen(@integraldecimal),(t0 + Quotient(Zahlen(0),t1)),@absolute)
2018
+ end
2019
+ def to_Frac
2020
+ @to_Frac ||= d2Frac
2021
+ end
2022
+ def to_Q
2023
+ self.to_Frac.to_Q
2024
+ end
2025
+ def to_Sig
2026
+ Sigma(self.to_Q)
2027
+ end
2028
+ def to_K
2029
+ Kettenbruch(self.to_Frac)
2030
+ end
2031
+ def to_R
2032
+ self.to_Frac
2033
+ end
2034
+ def to_s!
2035
+ # t0 = ""
2036
+ # t0 = "0"*(@decimalExponent - 1) if @decimalExponent>0
2037
+ # print "Decimal(\"-%s%s%s[%s]\")", @integraldecimal.to_s, @@decimalSeparator,t0 + @decimalfraction.to_s , @repetend.call.to_s
2038
+ end
2039
+ def to_s
2040
+ e = ""
2041
+ e = "-" if @absolute == -1
2042
+ t0 = ""
2043
+ t0 = "0"*(@decimalExponent - 1) if @decimalExponent>0
2044
+ t = "#{e}#{@integraldecimal}#{@@decimalSeparator}#{t0}#{@decimalfraction}#{@repetend.call}"
2045
+ eval("#{@@precision}.times{patt_succ(t)}")
2046
+ return t
2047
+ end
2048
+ def inspect
2049
+ t0 = ""
2050
+ t0 = "0"*(@decimalExponent - 1) if @decimalExponent>0
2051
+ if @absolute == -1 then
2052
+ sprintf("Decimal(\"-%s%s%s[%s]\")", @integraldecimal.to_s, @@decimalSeparator,t0 + @decimalfraction.to_s , @repetend.call.to_s)
2053
+ else
2054
+ sprintf("Decimal(\"%s%s%s[%s]\")", @integraldecimal.to_s, @@decimalSeparator,t0 + @decimalfraction.to_s , @repetend.call.to_s)
2055
+ end
2056
+ end
2057
+ def coerce(other)
2058
+ if Natural === other or Counting === other or Zahlen === other
2059
+ [Decimal(other.to_i),self]
2060
+ elsif Integer === other
2061
+ [Zahlen(other),self]
2062
+ elsif Quotient === other
2063
+ [other,self.to_Q]
2064
+ else
2065
+ [Float(other),self.to_f]
2066
+ end
2067
+ end
2068
+ end
2069
+ class Fraction < Numeric #Mixed fraction
2070
+ def Fraction.new!(a, b = Quotient(0,1),c=1)
2071
+ if a.kind_of?(Array)
2072
+ new(a,"canonical form")
2073
+ else
2074
+ new(a, b,c)
2075
+ end
2076
+ end
2077
+ @@fractionSeparator = " " #"+"
2078
+ def Fraction::fractionSeparator
2079
+ @@fractioinSeparator
2080
+ end
2081
+ include SGML
2082
+ def to_sgml
2083
+ "<mn #{sgml_id}class='mixed_fraction'><mn>#{@integer.to_s}</mn>#{@properfraction.to_sgml}<mn>"
2084
+ end
2085
+ def initialize(a, b,c=1)
2086
+ if a.kind_of?(Fraction)
2087
+ a
2088
+ elsif a.kind_of?(Zahlen) and b.kind_of?(Natural)
2089
+ @a = a
2090
+ @b = b
2091
+ t = Quotient(a,b)
2092
+ @absolute = -1 * c if t < 0
2093
+ @absolute = c if t >= 0
2094
+ @integer = t.numerator.divmod(t.denominator).at(0)
2095
+ @properfraction = Quotient(t.numerator.divmod(t.denominator).at(1),t.denominator)
2096
+ elsif a.kind_of?(Zahlen) and b.kind_of?(Quotient) and (c ==1 or c ==-1)
2097
+ @a = nil
2098
+ @b = nil
2099
+ @absolute = c
2100
+ @integer = a + b.numerator.divmod(b.denominator).at(0)
2101
+ @properfraction = Quotient(b.numerator.divmod(b.denominator).at(1),b.denominator)
2102
+ elsif a.kind_of?(Numeric) and b.kind_of?(Quotient)
2103
+ a = a.to_Z
2104
+ if a>0
2105
+ @a = nil
2106
+ @b = nil
2107
+ @absolute = 1
2108
+ @integer = a + b.numerator.divmod(b.denominator).at(0)
2109
+ @properfraction = Quotient(b.numerator.divmod(b.denominator).at(1),b.denominator)
2110
+ elsif a<0
2111
+ a = a*(-1)
2112
+ @a = nil
2113
+ @b = nil
2114
+ @absolute = -1
2115
+ @integer = a + b.numerator.divmod(b.denominator).at(0)
2116
+ @properfraction = Quotient(b.numerator.divmod(b.denominator).at(1),b.denominator)
2117
+ else
2118
+ end
2119
+ elsif a.kind_of?(Array)and b =="canonical form"
2120
+ @a = nil
2121
+ @b = nil
2122
+ @absolute = a.at(0)
2123
+ @integer = a.at(1)
2124
+ @properfraction = a.at(2)
2125
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
2126
+ Fraction(Zahlen(0),Quotient(b))
2127
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2128
+ infinity
2129
+ end
2130
+ @to_Q
2131
+ end
2132
+ attr_accessor :a,:b,:absolute, :integer, :properfraction
2133
+ private_class_method :new
2134
+ private :initialize
2135
+ def + (a)
2136
+ if a.kind_of?(Fraction)
2137
+ if a.integer == 0 and a.properfraction == Quotient(0,1) then
2138
+ return self
2139
+ else
2140
+ (self.to_Q + a.to_Q).to_Frac
2141
+ end
2142
+ elsif a.kind_of?(Quotient)
2143
+ if a.numerator == 0 and a == Quotient(0,1) then
2144
+ return self
2145
+ else
2146
+ (self.to_Q + a).to_Frac
2147
+ end
2148
+ else
2149
+ (self.to_Q + a.to_Q).to_Frac
2150
+ end
2151
+ end
2152
+ def addition (a)
2153
+ if a.kind_of?(Fraction)
2154
+ if a.integer == 0 and a.properfraction == Quotient(0,1) then
2155
+ return self
2156
+ else
2157
+ abs = 0
2158
+ temp = Quotient(@absolute *@properfraction.numerator,@properfraction.denominator) + Quotient(a.absolute * a.properfraction.numerator, a.properfraction.denominator)
2159
+ temp2 = Fraction(temp.numerator,temp.denominator)
2160
+ pfrac = temp2.properfraction
2161
+ int = (@absolute*@integer) + (a.absolute*a.integer) + (temp2.integer)
2162
+ if int.abs>1 and int>0
2163
+ abs = 1
2164
+ elsif int.abs>1 and int<0
2165
+ abs= -1
2166
+ else
2167
+ abs = (@absolute * a.absolute)
2168
+ end
2169
+ Fraction.new!([abs,int.abs.to_i,pfrac],"canonical form")
2170
+ end
2171
+ elsif a.kind_of?(Quotient)
2172
+ num = @properfraction.numerator * a.denominator
2173
+ num_a = a.numerator * @properfraction.denominator
2174
+ Fraction(num + num_a, @properfraction.denominator * a.denominator)
2175
+ elsif a.kind_of?(Integer)or a.kind_of?(Fixnum)
2176
+ a == 0 ? self: self + Fraction.new!(a, Quotient(0,1))
2177
+ elsif a.kind_of?(Float)
2178
+ Float(self) + a
2179
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
2180
+ self
2181
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2182
+ infinity
2183
+ else
2184
+ x, y = a.coerce(self)
2185
+ (x + y).to_Frac
2186
+ end
2187
+ end
2188
+ def - (a)
2189
+ if a.kind_of?(Fraction)
2190
+ if a.integer == 0 and a.properfraction == Quotient(0,1) then
2191
+ return self
2192
+ else
2193
+ (self.to_Q - a.to_Q).to_Frac
2194
+ end
2195
+ elsif a.kind_of?(Quotient)
2196
+ if a.numerator == 0 and a == Quotient(0,1) then
2197
+ return self
2198
+ else
2199
+ (self.to_Q - a).to_Frac
2200
+ end
2201
+ else
2202
+ (self.to_Q - a.to_Q).to_Frac
2203
+ end
2204
+ end
2205
+ def * (a)
2206
+ if a.kind_of?(Fraction)
2207
+ (self.to_Q*a.to_Q).to_Frac
2208
+ elsif a.kind_of?(Quotient)
2209
+ num = @properfraction.numerator * a.numerator
2210
+ den = @properfraction.denominator * a.denominator
2211
+ Fraction(num, den)
2212
+ elsif a.kind_of?(Integer) or a.kind_of?(Fixnum)
2213
+ if a == 0 then
2214
+ 0
2215
+ elsif a == 1
2216
+ self
2217
+ else
2218
+ self * Fraction(Zahlen(a), Quotient(0,1))
2219
+ end
2220
+ elsif a.kind_of?(Float)
2221
+ self.to_f * a
2222
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
2223
+ naught
2224
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2225
+ infinity
2226
+ else
2227
+ x, y = a.coerce(self)
2228
+ (x * y).to_Frac
2229
+ end
2230
+ end
2231
+ def / (a)
2232
+ if a.kind_of?(Fraction)
2233
+ self/a.to_Q
2234
+ elsif a.kind_of?(Quotient)
2235
+ num = @properfraction.numerator * a.denominator
2236
+ den = @properfraction.denominator * a.numerator
2237
+ Fraction(num, den) unless a.numerator == 0
2238
+ nan if a.numerator == 0
2239
+ elsif a.kind_of?(Integer) or a.kind_of?(Fixnum)
2240
+ self * Fraction.new!(1,a)
2241
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
2242
+ nan
2243
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2244
+ nan
2245
+ else
2246
+ x, y = a.coerce(self)
2247
+ (x / y).to_Frac
2248
+ end
2249
+ end
2250
+ def coerce(other)
2251
+ if Natural === other or Counting === other or Zahlen === other
2252
+ [Quotient(other),self]
2253
+ elsif Integer === other
2254
+ [Zahlen(other),self]
2255
+ elsif Quotient === other
2256
+ [other,self.to_Q]
2257
+ else
2258
+ [Float(other),self.to_f]
2259
+ end
2260
+ end
2261
+ def is_0?
2262
+ @integer === 0 and @properfraction.is_0? ? true : false
2263
+ end
2264
+ def to_Q
2265
+ @to_Q ||= Quotient(@absolute*@integer,1) + (@properfraction*@absolute)
2266
+ end
2267
+ def to_Dec
2268
+ self.to_Q.to_Dec
2269
+ end
2270
+ def to_f
2271
+ self.to_Q.to_f
2272
+ end
2273
+ def to_Sig
2274
+ Sigma(self.to_Q)
2275
+ end
2276
+ def to_K
2277
+ Kettenbruch(self)
2278
+ end
2279
+ def to_s
2280
+ t = ""
2281
+ unless a.nil? or b.nil?
2282
+ t = "#{@a.to_s}/#{@b.to_s} = "
2283
+ end
2284
+ if @b == 1
2285
+ @a.to_s
2286
+ elsif @absolute <0
2287
+ "#{t}#{@absolute}*(#{@integer} + (#{@properfraction}))"
2288
+ else
2289
+ "#{t}#{@integer.to_s} + (#{@properfraction.to_s})"
2290
+ end
2291
+ end
2292
+ def to_sgml
2293
+ "<mn class='mixed_fraction'><mn>#{@integer.to_s}</mn>#{@properfraction.to_sgml}<mn>"
2294
+ end
2295
+ def inspect
2296
+ if @absolute == 1
2297
+ sprintf("Fraction(\"%s %s/%s\")",(@integer).to_s,@properfraction.numerator, @properfraction.denominator)
2298
+ elsif @absolute == -1
2299
+ sprintf("Fraction(\"-1(%s %s/%s)\")",(@integer).to_s,@properfraction.numerator, @properfraction.denominator)
2300
+ end
2301
+ end
2302
+ def setdelta
2303
+ self.to_Q.setdelta
2304
+ end
2305
+ def succ
2306
+ self.to_Q.succ
2307
+ end
2308
+ def <=>(other)
2309
+ self.to_Q <=>other.to_Q
2310
+ end
2311
+ def hash
2312
+ @absolute.hash ^ @integer.hash ^ @properfraction.hash
2313
+ end
2314
+ end
2315
+ class Sigma<Numeric
2316
+ end
2317
+ class Kettenbruch<Numeric
2318
+ @@delta = 0
2319
+ @@precision = 20
2320
+ @@Separator = ";"
2321
+ def Kettenbruch::precision=(a)
2322
+ @@precision = a.to_i
2323
+ end
2324
+ def Kettenbruch::euclid(a)
2325
+ a.denominator.divmod(a.numerator)
2326
+ end
2327
+ def Kettenbruch::leichtfactory(a,y=true)
2328
+ p "raise error" unless a.kind_of?(Array)
2329
+ init=true
2330
+ b = a.pop(2) if y
2331
+ # y=="" ? tmp1 = "Fraction(Zahlen(#{b.at(0)}),Quotient(Zahlen(1),#{b.at(1)}))" : tmp1 = "Fraction(Zahlen(\#{y.integer.to_s}),Quotient(Zahlen(#{\y.properfracti#on.numerator.to_s}),Zahlen(#{y.properfraction.denominator.to_s})))"
2332
+ y ? tmp1 = "Fraction(Zahlen(#{b.at(0)}),Quotient(Zahlen(1),#{b.at(1)}))" : tmp1 = "&&&&"
2333
+ tmp0 = ""
2334
+ a.reverse.each{|x|
2335
+ break if x.nil?
2336
+ tmp0 = "Fraction(Zahlen(#{x}),Quotient(Zahlen(1),(#{tmp1})))"
2337
+ tmp1 = tmp0
2338
+ tmp0 = ""
2339
+ }
2340
+ tmp1
2341
+ end
2342
+ def Kettenbruch::allseitigfactory(a,y='')
2343
+ p "raise error" unless a.kind_of?(Array)
2344
+ init=true
2345
+ b = a.pop if y == ''
2346
+ y=='' ? tmp1 = "Fraction(Zahlen(#{b.at(0)}),Quotient(Zahlen(0),Zahlen(#{b.at(1)})))" : tmp1 = "Fraction(Zahlen(#{y.integer.to_s}),Quotient(Zahlen(#{y.properfraction.numerator.to_s}),Zahlen(#{y.properfraction.denominator.to_s})))"
2347
+ tmp0 = ""
2348
+ a.reverse.each{|x|
2349
+ break if x.nil?
2350
+ tmp0 = "Fraction(Zahlen(#{x.at(0)}),Quotient(Zahlen(#{x.at(1)}),(#{tmp1})))"
2351
+ tmp1 = tmp0
2352
+ tmp0 = ""}
2353
+ tmp1
2354
+ end
2355
+ def Kettenbruch::Separator
2356
+ @@Separator
2357
+ end
2358
+ def Kettenbruch::Separator=(a)
2359
+ @@Separator = a
2360
+ end
2361
+ def Kettenbruch.new!(a,b,c='',d=1)
2362
+ if a.kind_of?(Numeric) and b.kind_of?(Array)
2363
+ new(a,b,c,d)
2364
+ elsif a.kind_of?(Fraction)
2365
+ new(a.integer,a.properfraction)
2366
+ elsif a.kind_of?(Quotient)
2367
+ b = a.to_Frac
2368
+ new(b.integer,b.properfraction)
2369
+ elsif a.kind_of?(Natural) or a.kind_of?(Counting) or a.kind_of?(Zahlen)
2370
+ new(a,EmptySetClass)
2371
+ elsif
2372
+ new(a.to_Q,b)
2373
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) or a.kind_of?(NilClass)
2374
+ a
2375
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2376
+ a
2377
+ end
2378
+ end
2379
+ def initialize(int,cfra,rep='',absolute=1)
2380
+ @canonical =[]
2381
+ @integralK = int
2382
+ @fractionalK=[]
2383
+ @setdelta = false
2384
+ @deltaIter = 10
2385
+ @fractionalK_1=[]
2386
+ @repetend = rep
2387
+ @leicht = true
2388
+ if cfra.kind_of?(Array)
2389
+ @fractionalK=cfra
2390
+ else
2391
+ tmp = -1
2392
+ c = cfra
2393
+ until tmp == 0
2394
+ b = Kettenbruch::euclid(c)
2395
+ # p "#{c.denominator} = #{b.at(0)}.#{c.numerator} + #{b.at(1)}" #=8=
2396
+ @fractionalK << b.at(0)
2397
+ #if @fractionalK.length.is_even? then @fractionalK_ << b.at(0) remove last add last - 1 then + [1]
2398
+ tmp = b.at(1)
2399
+ c = Quotient(tmp,c.numerator)
2400
+ end
2401
+ end
2402
+ @longfracK = @fractionalK.dup
2403
+ end
2404
+ private :initialize
2405
+ attr_accessor :integralK, :fractionalK, :continuedFraction, :patt_succ, :absolute, :canonical, :longfracK, :leicht
2406
+ private_class_method :new
2407
+ def partition(size=9,length=100)
2408
+ # stop if ref
2409
+ e = ""
2410
+ e = "-" if @absolute == -1
2411
+ rep = @repetend
2412
+ x = []
2413
+ y = length
2414
+ length.times{
2415
+ rep.each{|n| x << n
2416
+ y =- 1
2417
+ break if y <= 0}
2418
+ }
2419
+ res = "[#{e}" + @integralK.to_s.to_s + ", " + (@fractionalK + x ).to_s.gsub("[","")
2420
+ start = 0
2421
+ res.insert(start,"[")
2422
+ length.times{
2423
+ start += size
2424
+ break if start > res.length
2425
+ res.insert(start, "],[")}
2426
+ res.gsub!("[, ","[").gsub(",]","]").gsub(",[]","]")
2427
+ t = eval(res + "]")
2428
+ t.delete_at(t.length - 1)
2429
+ p zz = t.delete_at(t.length - 1)
2430
+ zz = Kettenbruch::leichtfactory(zz)
2431
+ t.map!{|x|
2432
+ Kettenbruch::leichtfactory(x,false)
2433
+ # leicht? ? ret = Kettenbruch::leichtfactory(n) : ret = Kettenbruch::factory(n)
2434
+
2435
+ }
2436
+ t << zz
2437
+ end
2438
+ def convergents
2439
+ ret = []
2440
+ tmpz = []
2441
+ init = true
2442
+ @fractionalK.each_index{|n|
2443
+ n== 0 ? tmpz << [@integralK.to_i,@fractionalK.at(n)] : tmpz << [@integralK.to_i] + @fractionalK.slice(0,n+1)
2444
+ init = false
2445
+ }
2446
+ tmpz.each{|n|
2447
+ @leicht ? ret << eval(Kettenbruch::leichtfactory(n)) : ret << eval(Kettenbruch::factory(n))
2448
+ }
2449
+ ret
2450
+ end
2451
+ def last2convergents
2452
+ end
2453
+ def irrational?
2454
+ @repetend == "" ? false : true
2455
+ end
2456
+ def rational?
2457
+ @repetend == "" ? true : false
2458
+ end
2459
+ def to_a
2460
+ ret = []
2461
+ tmp1 = [@integralK.to_i] + @fractionalK
2462
+ tmp1 << @repetend unless @repetend == ""
2463
+ tmp1.each{|n|
2464
+ ret << n
2465
+ }
2466
+ ret
2467
+ end
2468
+ def to_s
2469
+ @@precision.times{patt_succ}
2470
+ @absolute == -1 ? e = "-" : e = ""
2471
+ res = "#{e}" + @integralK.to_s.to_s + @@Separator + " "
2472
+ irrational? ? res += @longfracK.to_s.gsub("[","").gsub("]","") + " ..." : res += @fractionalK.to_s.gsub("[","").gsub("]","")
2473
+ res
2474
+ end
2475
+ def reciprical
2476
+ a = self.to_a
2477
+ a.shift if @integralK == 0
2478
+ @integralK == 0 ? a.to_K : ([0] + a).to_K
2479
+ end
2480
+ def +(a)
2481
+ if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
2482
+ naught
2483
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2484
+ infinity
2485
+ else
2486
+ ((self.to_Q) + (a.to_Q)).to_K #not irrational
2487
+ end
2488
+ end
2489
+ def -(a)
2490
+ if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
2491
+ naught
2492
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2493
+ infinity
2494
+ else
2495
+ ((self.to_Q) - (a.to_Q)).to_K #not irrational
2496
+ end
2497
+ end
2498
+ def * (a)
2499
+ if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
2500
+ naught
2501
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2502
+ infinity
2503
+ else
2504
+ ((self.to_Q) * (a.to_Q)).to_K
2505
+ end
2506
+ end
2507
+ def / (a)
2508
+ if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then
2509
+ infinity
2510
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2511
+ NAN.Class.new
2512
+ elsif a.kind_of?(Kuttenbruch)
2513
+ self * a.reciprical
2514
+ else
2515
+ (self.to_Q/a.to_Q).to_K
2516
+ end
2517
+ end
2518
+ def ** (a)
2519
+ self.to_f ** a.to_f
2520
+ end
2521
+ def <=>(other)
2522
+ @@delta = ((other - self) /@deltaIter).to_K if @setdelta
2523
+ @setdelta = false
2524
+ ret = nil
2525
+ if other.kind_of?(ContinuedFrac)
2526
+ if self == other then
2527
+ ret = 0
2528
+ elsif self.integralK < other.integralK then
2529
+ ret = -1
2530
+ elsif self.fractionalK > other.fractionalK then
2531
+ ret = 1
2532
+ elsif self.integralK == other.integralK then
2533
+ s = self.to_s
2534
+ t = other.to_s
2535
+ k = s.length
2536
+ l = t.length
2537
+ if k>l then
2538
+ m = k-l
2539
+ eval("#{m}.times{other.patt_succ(t)}")
2540
+ elsif k<l then
2541
+ m = l-k
2542
+ eval("#{m}.times{self.patt_succ(s)}")
2543
+ end
2544
+ s.gsub("#{@@kettenbruchSeparator}","") <=> t.gsub("#{@kettenbruchSeparator}","")
2545
+ end
2546
+ elsif other.kind_of?(Numeric)
2547
+ ret = self <=> other.to_Q
2548
+ end
2549
+ ret
2550
+ end
2551
+ def == (other)
2552
+ if other.kind_of?(Kettenbruch) then
2553
+ self.to_canonical == other.to_canoncical
2554
+ elsif other.kind_of?(Numeric)
2555
+ self == other.to_K
2556
+ else
2557
+ other.to_i == self.to_i
2558
+ end
2559
+ end
2560
+ def succ
2561
+ self + @@delta
2562
+ end
2563
+ def patt_succ
2564
+ ret = ""
2565
+ if irrational? then
2566
+ arr = self.to_a
2567
+ kint = @fractionalK
2568
+ len = @longfracK.length - kint.length
2569
+ div, mod = len.divmod(@repetend.length)
2570
+ z = @repetend.at(mod)
2571
+ if z.kind_of?(Numeric) then
2572
+ ret = @longfracK << z
2573
+ elsif z.kind_of?(String)
2574
+ # try and coerce in to a proc
2575
+ pr = eval( "Proc.new {|n|;" + z + ";}")
2576
+ ret =@longfracK << pr.call(div)
2577
+ end
2578
+ end
2579
+ ret
2580
+ end
2581
+ def to_f
2582
+ self.to_Q.to_f
2583
+ end
2584
+ def to_N
2585
+ @@fractionalK.empty? and @absolute == 1 and @integralK != 0 ? Natural(@integralK) : EmptySetClass #and repetend.nil?
2586
+ end
2587
+ def to_N0
2588
+ @@fractionalK.empty? and @absolute == 1 ? Counting(@integralK) : EmptySetClass #and repetend.nil?
2589
+ end
2590
+ def to_Z
2591
+ @@fractionalK.empty? and @absolute == 1 ? Zahlen(@integralK) : EmptySetClass #and repetend.nil?
2592
+ end
2593
+ def k2Frac
2594
+ ret = ""
2595
+ n = eval("[" + self.to_s.gsub(";",",").gsub("...","") + "]")
2596
+ if n.length == 1
2597
+ ret = "#{n.at(0)} 0/1".to_Frac.to_s
2598
+ else
2599
+ @leicht ? ret = Kettenbruch::leichtfactory(n) : ret = Kettenbruch::factory(n)
2600
+ end
2601
+ ret
2602
+ end
2603
+ def to_Frac
2604
+ @to_Frac ||= eval(k2Frac)
2605
+ end
2606
+ def to_Q
2607
+ self.to_Frac.to_Q
2608
+ end
2609
+ def to_Dec
2610
+ self.to_Frac.to_Dec
2611
+ end
2612
+ def to_R
2613
+ self.to_Frac
2614
+ def showRE(a,re) # helper =8=
2615
+ if a =~ re
2616
+ "#{$`}<<#{$&}>>#{$'}"
2617
+ else
2618
+ "no match"
2619
+ end
2620
+ end
2621
+ end
2622
+ def to_sgml
2623
+ "<mn class='Kuttenbruch'>#{self.to_s}</mn>"
2624
+ end
2625
+ def isRE?(a,re) #helper =8=
2626
+ if a =~ re
2627
+ true
2628
+ else
2629
+ false
2630
+ end
2631
+ end
2632
+ def inspect
2633
+ sprintf("Kuttenbruch(%s)", to_a.to_s)
2634
+ end
2635
+ def coerce(other)
2636
+ if Natural === other or Counting === other or Zahlen === other
2637
+ [ContinuedFrac(other.to_i),self]
2638
+ elsif Integer === other
2639
+ [Zahlen(other),self]
2640
+ elsif Quotient === other
2641
+ [other,self.to_Q]
2642
+ else
2643
+ [Float(other),self.to_f]
2644
+ end
2645
+ end
2646
+ end
2647
+ class Real < Numeric
2648
+ end
2649
+ class Matrix
2650
+ include SGML
2651
+ def to_sgml
2652
+ tmp = "<mn #{sgml_id}class='matrix'><mrow><mo>(</mo><mtable>"
2653
+ (1..@nsize).to_a.each{|a|
2654
+ (1..@msize).to_a.each{|b|
2655
+ tmp += "<mn>" + eval("at_#{a}_#{b}").to_s + "</mn>"
2656
+ }
2657
+ tmp += "</mtr>"
2658
+ }
2659
+ tmp += "</mtable><mo>)</mo></mrow></mn>"
2660
+ end
2661
+ private_class_method :new
2662
+ def Matrix.instanciate(n,m)
2663
+ new(n,m)
2664
+ end
2665
+ def Matrix.csv(a)
2666
+ t = "[[" << a.strip! << "]]"
2667
+ return Matrix.rows(t.gsub(/\s+/, '],['))
2668
+ end
2669
+ def Matrix.rows(rows)
2670
+ a = instanciate(rows.length,rows.at(0).length)
2671
+ rows.each_index{|x|
2672
+ rows.at(x).each_index{|y|
2673
+ eval("a.at_#{x+1}_#{y+1}= rows.at(#{x}).at(#{y})")
2674
+ }
2675
+ }
2676
+ return a
2677
+ end
2678
+ def Matrix.cols(cols)
2679
+ a = instanciate(cols.length,cols.at(0).length)
2680
+ cols.each_index{|x|
2681
+ cols.at(x).each_index{|y|
2682
+ eval("a.at_#{x+1}_#{y+1}= cols.at(#{y}).at(#{x})")
2683
+ }
2684
+ }
2685
+ return a
2686
+ end
2687
+ def Matrix.diagonal(v)
2688
+ a = Matrix(v.length,v.length)
2689
+ v.each_index{|i|
2690
+ eval("a.at_#{i+1}_#{i+1} = v.at(i)")
2691
+ }
2692
+ return a
2693
+ end
2694
+ def Matrix.scalar(n,v)
2695
+ Matrix.diagonal(Array.new(n).fill(v, 0, n))
2696
+ end
2697
+ def Matrix.identity(n)
2698
+ Matrix.scalar(n, 1)
2699
+ end
2700
+ class << Matrix
2701
+ alias unit identity
2702
+ alias I identity
2703
+ end
2704
+ def Matrix.zero(n)
2705
+ Matrix.scalar(n, 0)
2706
+ end
2707
+ def initialize(a,b)
2708
+ (1..a).to_a.each{|n|
2709
+ (1..b).to_a.each{|m|
2710
+ z = " @at_#{n}_#{m} = 0
2711
+ def at_#{n}_#{m}
2712
+ @at_#{n}_#{m}
2713
+ end
2714
+ def tr_#{m}_#{n}
2715
+ @at_#{n}_#{m}
2716
+ end
2717
+ def at_#{n}_#{m}=(a)
2718
+ @at_#{n}_#{m} = a
2719
+ end
2720
+ "
2721
+ instance_eval(z)
2722
+ }
2723
+ }
2724
+ @nsize = a
2725
+ @msize = b
2726
+ @tr = false
2727
+ end
2728
+ attr_accessor :msize, :nsize
2729
+ def transpose
2730
+ a= Matrix(@msize, @nsize)
2731
+ (1..@msize).to_a.each{|x|
2732
+ (1..@nsize).to_a.each{|y|
2733
+ eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y}")
2734
+ }
2735
+ }
2736
+ return a
2737
+ end
2738
+ def is_tr?
2739
+ @tr
2740
+ end
2741
+ def tr!
2742
+ unless @tr then @tr = true else @tr = false end
2743
+ return self
2744
+ end
2745
+ def og!
2746
+ @tr = false
2747
+ end
2748
+ def +(nm)
2749
+ a = ""
2750
+ if not(nm.is_tr? or @tr) then
2751
+ if nm.msize == @msize and nm.nsize == @nsize then
2752
+ a= Matrix(@nsize, @msize)
2753
+ (1..@nsize).to_a.each{|x|
2754
+ (1..@msize).to_a.each{|y|
2755
+ eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} + nm.at_#{x}_#{y}")
2756
+ }
2757
+ }
2758
+ else
2759
+ end
2760
+ elsif nm.is_tr? and not @tr then
2761
+ if nm.msize == @nsize and nm.nsize == @msize then
2762
+ a= Matrix(@nsize, @msize)
2763
+ (1..@nsize).to_a.each{|x|
2764
+ (1..@msize).to_a.each{|y|
2765
+ eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} + nm.tr_#{x}_#{y}")
2766
+ }
2767
+ }
2768
+ else
2769
+ end
2770
+ elsif not nm.is_tr? and @tr then
2771
+ if nm.msize == @nsize and nm.nsize == @msize then
2772
+ a= Matrix(@msize, @nsize)
2773
+ (1..@msize).to_a.each{|x|
2774
+ (1..@nsize).to_a.each{|y|
2775
+ eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y} + nm.at_#{x}_#{y}")
2776
+ }
2777
+ }
2778
+ else
2779
+ end
2780
+ else
2781
+ end
2782
+ return a
2783
+ end
2784
+ def *(nm)
2785
+ a = ""
2786
+ if not(nm.is_tr? or @tr) then
2787
+ if nm.msize == @msize and nm.nsize == @nsize then
2788
+ a= Matrix(@nsize, @msize)
2789
+ (1..@nsize).to_a.each{|x|
2790
+ (1..@msize).to_a.each{|y|
2791
+ eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.at_#{x}_#{y}")
2792
+ }
2793
+ }
2794
+ else
2795
+ end
2796
+ elsif nm.is_tr? and not @tr then
2797
+ if nm.msize == @nsize and nm.nsize == @msize then
2798
+ a= Matrix(@nsize, @msize)
2799
+ (1..@nsize).to_a.each{|x|
2800
+ (1..@msize).to_a.each{|y|
2801
+ eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.tr_#{x}_#{y}")
2802
+ }
2803
+ }
2804
+ else
2805
+ end
2806
+ elsif not nm.is_tr? and @tr then
2807
+ if nm.msize == @nsize and nm.nsize == @msize then
2808
+ a= Matrix(@msize, @nsize)
2809
+ (1..@msize).to_a.each{|x|
2810
+ (1..@nsize).to_a.each{|y|
2811
+ eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y} + nm.at_#{x}_#{y}")
2812
+ }
2813
+ }
2814
+ else
2815
+ end
2816
+ else
2817
+ end
2818
+ return a
2819
+ end
2820
+ def to_s
2821
+ t = ""
2822
+ (1..@nsize).to_a.each{|a|
2823
+ t << "|"
2824
+ (1..@msize).to_a.each{|b|
2825
+ t << eval("at_#{a}_#{b}").to_s
2826
+ unless b == @msize then t << 9 end
2827
+ }
2828
+ t << "|"
2829
+ unless a == @nsize then t << 10 end
2830
+ }
2831
+ t
2832
+ end
2833
+ def to_csv
2834
+ t = ""
2835
+ (1..@nsize).to_a.each{|a|
2836
+ (1..@msize).to_a.each{|b|
2837
+ t << eval("at_#{a}_#{b}").to_s
2838
+ unless b == @msize then t << 44 end
2839
+ }
2840
+ t << 10
2841
+ }
2842
+ t
2843
+ end
2844
+ end
2845
+ class Tensor
2846
+ @@script = "covarient"
2847
+ @@t1 = "a"
2848
+ private_class_method :new
2849
+ def Tensor.builder(a,*args)
2850
+ tmp = ""
2851
+ script = "_" if @@script == "covarient"
2852
+ script = "U" if @@script == "contravarient"
2853
+ m = [a]+args unless a.class == Array
2854
+ m = a if a.class == Array # assume this is an @order from + call
2855
+ z1 = ""
2856
+ z2 = ""
2857
+ x1, x2 = m, "INNER"
2858
+ m.reverse.to_a.each_index{|c|
2859
+ t2 = @@t1 + "#{m.length-c}"
2860
+ t3 = @@t1 + "#{c+1}"
2861
+ x1 = "#{x2}"
2862
+ x1 , x2 = x2, "(1..#{m.reverse.at(c)}).to_a.each{|#{t2}|\n#{x1}\n}"
2863
+ z1 << "#{script}\#{#{t3}}"
2864
+ z2 << "#{script}\\\#{#{t3}}"
2865
+ }
2866
+ t3 = "@at#{z1} = 0 \n"
2867
+ t4 = "def at#{z1}\n @at#{z1}\nend \n"
2868
+ t5 = "def at#{z1}=(a)\n @at#{z1} = a\nend \n"
2869
+ t6a = "def +(mn)\n if mn.order == @order then
2870
+ a= Tensor(@order)\n"
2871
+ t6b = "\nreturn a\n else\n # #raise error\n end\n end"
2872
+ t6 = t6a + x2.gsub("INNER"," z = \\\"a.at#{z2} = self.at#{z2} + mn.at#{z2}\\\"\n eval(z)") + t6b
2873
+ t7 = [x2,z1]
2874
+ tmp = x2.gsub("INNER","z = \"#{t3}#{t4}#{t5}\"\n instance_eval(z)\n #puts z")
2875
+ return tmp,m,t6,t7
2876
+ end
2877
+ def Tensor.new!(a,*args)
2878
+ t1, t2 = [],[]
2879
+ u1, u2 = [],[]
2880
+ if a.kind_of?(Hash) then
2881
+ a.each{|x,y|
2882
+ x =~ %r{(_(?!_.*)(?!.*=$).*)}
2883
+ unless $1 == nil then t1 << $1 ; t2 << y end
2884
+ x =~ %r{(U(?!U.*)(?!.*=$).*)}
2885
+ unless $1 == nil then u1 << $1 ; u2 << y end
2886
+ }
2887
+ m,n = nil,nil
2888
+ o,p = [],[]
2889
+ t1.each{|a,b|
2890
+ s = a.split('_'); s.shift
2891
+ o << s
2892
+ }
2893
+ u1.each{|a,b|
2894
+ t = a.split('U'); t.shift
2895
+ p << t
2896
+ }
2897
+ m = Array.new(o.at(0).length,Array.new)
2898
+ n = Array.new(p.at(0).length,Array.new)
2899
+ o.each{|t|
2900
+ t.each_index{|h|
2901
+ m[h] += [t.at(h)]
2902
+ }
2903
+ }
2904
+ m.each_index{|i|
2905
+ m[i] = [m.at(i).max]
2906
+ }
2907
+ p.each{|t|
2908
+ t.each_index{|h|
2909
+ n[h] += [t.at(h)]
2910
+ }
2911
+ }
2912
+ n.each_index{|i|
2913
+ n[i] = [n.at(i).max]
2914
+ }
2915
+ tu = Tensor.instanciate(m)
2916
+ t1.each_index{|s|
2917
+ eval("tu.at#{t1.at(s)} = #{t2.at(s)}")
2918
+ }
2919
+ if n then
2920
+ tu.contra(n)
2921
+ u1.each_index{|s|
2922
+ eval("tu.at#{u1.at(s)} = #{u2.at(s)}")
2923
+ }
2924
+ end
2925
+ tu
2926
+ else
2927
+ Tensor.instanciate(a,*args)
2928
+ end
2929
+ end
2930
+ def Tensor.instanciate(a,*args)
2931
+ t = Tensor.builder(a,*args)
2932
+ t6 =t.at(2) # for information o n t6 see T::builder
2933
+ new(t.at(0) + "\n instance_eval(\"#{t6}\")",t.at(1))
2934
+ end
2935
+ def indicies
2936
+ z = self.public_methods
2937
+ x = []
2938
+ z.each{|y|
2939
+ y =~ %r{(_(?!_.*)(?!methods)(?!variable.*)(?!defined.)(?!eval)(?!send)(?!at)(?!get|set)(?!a|s|h)(?!id.*)(?!.*=$)(?!.*\?$).*)}
2940
+ unless $1 == nil then x << $1 end
2941
+ y =~ %r{(U(?!U.*)(?!.*=$).*)}
2942
+ unless $1 == nil then x << $1 end
2943
+ }
2944
+ x.delete("_")
2945
+ return x
2946
+ end
2947
+ def contra(n,*args)
2948
+ @@script = "contravarient"
2949
+ t = Tensor.builder(n,*args)
2950
+ @@script = "covarient"
2951
+ eval(t.at(0))
2952
+ end
2953
+ def initialize(a,p)
2954
+ eval(a)
2955
+ @tr = false
2956
+ @p = @@script == "covarient" ? p.length : 0
2957
+ @q = @@script == "covarient" ? 0 : q.length
2958
+ @order = @@script == "covarient" ? p : []
2959
+ @qorder = @@script == "covarient" ? [] : q
2960
+ @rank = @p + @q
2961
+ @rframe= []
2962
+ end
2963
+ attr_reader :order, :qorder, :rank, :p, :q
2964
+ def +(other)
2965
+ t6a = "if other.order == @order && other.qorder == @qorder then
2966
+ a= Tensor(@order)\n"
2967
+ t6b = "\nreturn a\n else\n # #raise error\n end\n end"
2968
+ end
2969
+ def *(other)
2970
+ ta1 ="a= Tensor(x+y)# @ order+other.order)\n
2971
+ a.contra(s+t) # @qorder+other.qorder)\n #puts a.indicies\n"
2972
+ t1= Tensor.builder(@order+other.order).at(3)
2973
+ t2= Tensor.builder(@order).at(3)
2974
+ t3= Tensor.builder(other.order).at(3)
2975
+ u1= Tensor.builder(@qorder+other.qorder).at(3) if @q+other.q <0
2976
+ u2= Tensor.builder(@qorder).at(3) if @q+other.q <0
2977
+ u3= Tensor.builder(other.qorder).at(3)if @q+other.q <0
2978
+ tu = " Proc.new{|x,y,z,s,t| \n" + ta1 + t1.at(0).gsub("INNER"," z = \"a.at#{t1.at(1)} = self.at#{t2.at(1)} * other.at#{t3.at(1)}\"\n eval(z)")
2979
+ tu += "\n"+ u1.at(0).gsub("INNER"," z = \"a.at#{u1.at(1)} = self.at#{u2.at(1)} * other.at#{u3.at(1)}\"\n #eval(z)") if @q+other.q <0
2980
+ tu += "\na\n}"
2981
+ tp = eval(tu)
2982
+ t = tp.call(@order, other.order,"",@qorder,other.qorder)
2983
+ return t.inspect
2984
+ end
2985
+ def product(&block)
2986
+ a = block.call
2987
+ return a
2988
+ end
2989
+ def to_s
2990
+ tmp = "{"
2991
+ c = 0
2992
+ indicies.each{|q|
2993
+ p = "at#{q}"
2994
+ tmp << "'#{p}' => '#{send(p)}'" if c == 0
2995
+ tmp << ",'#{p}' => '#{send(p)}'" unless c==0
2996
+ c += 1
2997
+ }
2998
+ tmp << "}"
2999
+ return tmp
3000
+ end
3001
+ def to_sgml
3002
+ end
3003
+ def to_h
3004
+ eval(to_s)
3005
+ end
3006
+ end