Class: Kettenbruch
Constant Summary collapse
- @@delta =
0- @@precision =
20- @@Separator =
";"
Instance Attribute Summary collapse
-
#absolute ⇒ Object
Returns the value of attribute absolute.
-
#canonical ⇒ Object
Returns the value of attribute canonical.
-
#continuedFraction ⇒ Object
Returns the value of attribute continuedFraction.
-
#fractionalK ⇒ Object
Returns the value of attribute fractionalK.
-
#integralK ⇒ Object
Returns the value of attribute integralK.
-
#leicht ⇒ Object
Returns the value of attribute leicht.
-
#longfracK ⇒ Object
Returns the value of attribute longfracK.
-
#patt_succ ⇒ Object
Returns the value of attribute patt_succ.
Class Method Summary collapse
- .allseitigfactory(a, y = true) ⇒ Object
- .euclid(a) ⇒ Object
- .leichtfactory(a, y = true) ⇒ Object
- .new!(a, b, c = '', d = 1) ⇒ Object
- .precision=(a) ⇒ Object
- .Separator ⇒ Object
- .Separator=(a) ⇒ Object
Instance Method Summary collapse
- #*(a) ⇒ Object
- #**(a) ⇒ Object
- #+(a) ⇒ Object
- #-(a) ⇒ Object
- #/(a) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #coerce(other) ⇒ Object
- #convergents ⇒ Object
- #inspect ⇒ Object
- #irrational? ⇒ Boolean
-
#isRE?(a, re) ⇒ Boolean
helper =8=.
- #k2Frac ⇒ Object
- #last2convergents ⇒ Object
- #operation(z, other) ⇒ Object
- #partition(size = 9, length = 100) ⇒ Object
- #rational? ⇒ Boolean
- #reciprical ⇒ Object
-
#showRE(a, re) ⇒ Object
helper =8=.
- #succ ⇒ Object
- #to_a ⇒ Object
- #to_Dec ⇒ Object
- #to_f ⇒ Object
- #to_Frac ⇒ Object
- #to_N ⇒ Object
- #to_N0 ⇒ Object
- #to_Q ⇒ Object
- #to_R ⇒ Object
- #to_s ⇒ Object
- #to_sgml ⇒ Object
- #to_Z ⇒ Object
Methods inherited from Numeric
Methods included from SGML
Instance Attribute Details
#absolute ⇒ Object
Returns the value of attribute absolute.
2476 2477 2478 |
# File 'lib/m500.rb', line 2476 def absolute @absolute end |
#canonical ⇒ Object
Returns the value of attribute canonical.
2476 2477 2478 |
# File 'lib/m500.rb', line 2476 def canonical @canonical end |
#continuedFraction ⇒ Object
Returns the value of attribute continuedFraction.
2476 2477 2478 |
# File 'lib/m500.rb', line 2476 def continuedFraction @continuedFraction end |
#fractionalK ⇒ Object
Returns the value of attribute fractionalK.
2476 2477 2478 |
# File 'lib/m500.rb', line 2476 def fractionalK @fractionalK end |
#integralK ⇒ Object
Returns the value of attribute integralK.
2476 2477 2478 |
# File 'lib/m500.rb', line 2476 def integralK @integralK end |
#leicht ⇒ Object
Returns the value of attribute leicht.
2476 2477 2478 |
# File 'lib/m500.rb', line 2476 def leicht @leicht end |
#longfracK ⇒ Object
Returns the value of attribute longfracK.
2476 2477 2478 |
# File 'lib/m500.rb', line 2476 def longfracK @longfracK end |
#patt_succ ⇒ Object
Returns the value of attribute patt_succ.
2476 2477 2478 |
# File 'lib/m500.rb', line 2476 def patt_succ @patt_succ end |
Class Method Details
.allseitigfactory(a, y = true) ⇒ Object
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 |
# File 'lib/m500.rb', line 2412 def Kettenbruch::allseitigfactory(a,y=true) p "raise error" unless a.kind_of?(Array) init=true b = a.pop if y == '' # 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})))" y ? tmp1 = "Fraction(Zahlen(#{b.at(0)}),Quotient(#{b.at(1)},#{b.at(2)}))" : tmp1 = "&&&&" tmp0 = "" a.reverse.each{|x| break if x.nil? tmp0 = "Fraction(Zahlen(#{x.at(0)}),Quotient(Zahlen(#{x.at(1)}),(#{tmp1})))" tmp1 = tmp0 tmp0 = ""} tmp1 end |
.euclid(a) ⇒ Object
2394 2395 2396 |
# File 'lib/m500.rb', line 2394 def Kettenbruch::euclid(a) a.denominator.divmod(a.numerator) end |
.leichtfactory(a, y = true) ⇒ Object
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 |
# File 'lib/m500.rb', line 2397 def Kettenbruch::leichtfactory(a,y=true) p "raise error" unless a.kind_of?(Array) init=true b = a.pop(2) if y # 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})))" y ? tmp1 = "Fraction(Zahlen(#{b.at(0)}),Quotient(Zahlen(1),#{b.at(1)}))" : tmp1 = "&&&&" tmp0 = "" a.reverse.each{|x| break if x.nil? tmp0 = "Fraction(Zahlen(#{x}),Quotient(Zahlen(1),(#{tmp1})))" tmp1 = tmp0 tmp0 = "" } tmp1 end |
.new!(a, b, c = '', d = 1) ⇒ Object
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 |
# File 'lib/m500.rb', line 2432 def Kettenbruch.new!(a,b,c='',d=1) if a.kind_of?(Numeric) and b.kind_of?(Array) new(a,b,c,d) elsif a.kind_of?(Fraction) new(a.integer,a.properfraction) elsif a.kind_of?(Quotient) b = a.to_Frac new(b.integer,b.properfraction) elsif a.kind_of?(Natural) or a.kind_of?(Counting) or a.kind_of?(Zahlen) new(a,EmptySetClass) elsif new(a.to_Q,b) elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) or a.kind_of?(NilClass) a elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass) a end end |
.precision=(a) ⇒ Object
2391 2392 2393 |
# File 'lib/m500.rb', line 2391 def Kettenbruch::precision=(a) @@precision = a.to_i end |
.Separator ⇒ Object
2426 2427 2428 |
# File 'lib/m500.rb', line 2426 def Kettenbruch::Separator @@Separator end |
.Separator=(a) ⇒ Object
2429 2430 2431 |
# File 'lib/m500.rb', line 2429 def Kettenbruch::Separator=(a) @@Separator = a end |
Instance Method Details
#*(a) ⇒ Object
2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 |
# File 'lib/m500.rb', line 2589 def * (a) if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then naught elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass) infinity else op = Matrix(2,4) op.at_1_1 = 0 op.at_2_1 = 1 op.at_1_2 = 0 op.at_2_2 = 0 op.at_1_3 = 0 op.at_2_3 = 0 op.at_1_4 = 1 op.at_2_4 = 0 res = self.operation(op,a) res.to_K end end |
#**(a) ⇒ Object
2742 2743 2744 |
# File 'lib/m500.rb', line 2742 def ** (a) self.to_f ** a.to_f end |
#+(a) ⇒ Object
2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 |
# File 'lib/m500.rb', line 2551 def +(a) if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then naught elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass) infinity else op = Matrix(2,4) op.at_1_1 = 1 op.at_2_1 = 0 op.at_1_2 = 0 op.at_2_2 = 1 op.at_1_3 = 0 op.at_2_3 = 0 op.at_1_4 = 1 op.at_2_4 = 0 res = self.operation(op,a) res.to_K end end |
#-(a) ⇒ Object
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 |
# File 'lib/m500.rb', line 2570 def -(a) if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then naught elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass) infinity else op = Matrix(2,4) op.at_1_1 = 1 op.at_2_1 = 0 op.at_1_2 = 0 op.at_2_2 = -1 op.at_1_3 = 0 op.at_2_3 = 0 op.at_1_4 = 1 op.at_2_4 = 0 res = self.operation(op,a) res.to_K end end |
#/(a) ⇒ Object
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 |
# File 'lib/m500.rb', line 2608 def / (a) if a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) then infinity elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass) NAN.Class.new elsif a.kind_of?(Kuttenbruch) op = Matrix(2,4) op.at_1_1 = 1 op.at_2_1 = 0 op.at_1_2 = 0 op.at_2_2 = 0 op.at_1_3 = 0 op.at_2_3 = 0 op.at_1_4 = 0 op.at_2_4 = 1 res = self.operation(op,a) res.to_K else (self.to_Q/a.to_Q).to_K end end |
#<=>(other) ⇒ Object
2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 |
# File 'lib/m500.rb', line 2745 def <=>(other) @@delta = ((other - self) /@deltaIter).to_K if @setdelta @setdelta = false ret = nil if other.kind_of?(ContinuedFrac) if self == other then ret = 0 elsif self.integralK < other.integralK then ret = -1 elsif self.fractionalK > other.fractionalK then ret = 1 elsif self.integralK == other.integralK then s = self.to_s t = other.to_s k = s.length l = t.length if k>l then m = k-l eval("#{m}.times{other.patt_succ(t)}") elsif k<l then m = l-k eval("#{m}.times{self.patt_succ(s)}") end s.gsub("#{@@kettenbruchSeparator}","") <=> t.gsub("#{@kettenbruchSeparator}","") end elsif other.kind_of?(Numeric) ret = self <=> other.to_Q end ret end |
#==(other) ⇒ Object
2775 2776 2777 2778 2779 2780 2781 2782 2783 |
# File 'lib/m500.rb', line 2775 def == (other) if other.kind_of?(Kettenbruch) then self.to_canonical == other.to_canoncical elsif other.kind_of?(Numeric) self == other.to_K else other.to_i == self.to_i end end |
#coerce(other) ⇒ Object
2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 |
# File 'lib/m500.rb', line 2859 def coerce(other) if Natural === other or Counting === other or Zahlen === other [ContinuedFrac(other.to_i),self] elsif Integer === other [Zahlen(other),self] elsif Quotient === other [other,self.to_Q] else [Float(other),self.to_f] end end |
#convergents ⇒ Object
2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 |
# File 'lib/m500.rb', line 2509 def convergents ret = [] tmpz = [] init = true @fractionalK.each_index{|n| n== 0 ? tmpz << [@integralK.to_i,@fractionalK.at(n)] : tmpz << [@integralK.to_i] + @fractionalK.slice(0,n+1) init = false } tmpz.each{|n| @leicht ? ret << eval(Kettenbruch::leichtfactory(n)) : ret << eval(Kettenbruch::factory(n)) } ret end |
#inspect ⇒ Object
2856 2857 2858 |
# File 'lib/m500.rb', line 2856 def inspect sprintf("Kuttenbruch(%s)", to_a.to_s) end |
#irrational? ⇒ Boolean
2524 2525 2526 |
# File 'lib/m500.rb', line 2524 def irrational? @repetend == "" ? false : true end |
#isRE?(a, re) ⇒ Boolean
helper =8=
2849 2850 2851 2852 2853 2854 2855 |
# File 'lib/m500.rb', line 2849 def isRE?(a,re) #helper =8= if a =~ re true else false end end |
#k2Frac ⇒ Object
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 |
# File 'lib/m500.rb', line 2817 def k2Frac ret = "" n = eval("[" + self.to_s.gsub(";",",").gsub("...","") + "]") if n.length == 1 ret = "#{n.at(0)} 0/1".to_Frac.to_s else @leicht ? ret = Kettenbruch::leichtfactory(n) : ret = Kettenbruch::factoryallseitig(n) end ret end |
#last2convergents ⇒ Object
2522 2523 |
# File 'lib/m500.rb', line 2522 def last2convergents end |
#operation(z, other) ⇒ Object
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 |
# File 'lib/m500.rb', line 2629 def operation(z,other) oa = other.to_a sa = self.to_a a0 = nil b0 = nil c0 = nil d0 = nil e0 = nil f0 = nil g0 = nil h0 = nil a1 = nil b1 = nil c1 = nil d1 = nil e1 = nil f1 = nil g1 = nil h1 = nil x = 0 y =0 res_a = [] if z.kind_of?(Matrix) then # check is z = Matrix(2,4) a0 = z.at_1_1 e0 = z.at_2_1 b0 = z.at_1_2 f0 = z.at_2_2 c0 = z.at_1_3 g0 = z.at_2_3 d0 = z.at_1_4 h0 = z.at_2_4 else p "raise an input error NOT a MATRIX 2x4" end tmp = 0 self.to_pix(a0,b0,c0,d0,e0,f0,g0,h0) p "init" while tmp <19 #true # size of k_res is less than epsilon eggs = true if e0 == 0 or f0 == 0 or g0 == 0 or h0 == 0 then eggs = false if f0 == 0 else # all are positive p "positive" #p a0.div(e0).at(0) eggs = false if a0.divmod(e0).at(0) == c0.divmod(g0).at(0) end #p (f > 0 and e > 0 and g > 0 )? (b/f-a/e).abs : f,e,g #p (f > 0 and e > 0 and g > 0 ) ? (c/g-a/e).abs : f,e,g p "### #{eggs}" if eggs then p "to the right" x = sa.shift a1 = (a0*x)+c0 b1 = (b0*x)+d0 c1 = a0 d1 = b0 e1 = (e0*x)+g0 f1 = (f0*x)+h0 g1 = e0 h1 = f0 self.to_pix(a1,b1,c1,d1,e1,f1,g1,h1) else p "downwards" y = oa.shift a1 = (a0*y)+b0 b1 = a0 c1 = (c0*y)+d0 d1 = c0 e1 = (e0*y)+f0 f1 = e0 g1 = (g0*y)+h0 h1 = g0 self.to_pix(a1,b1,c1,d1,e1,f1,g1,h1) end test = false eucint = a0.divmod(e0).at(0) if e0 > 0 if y > 1 and x > 1 and f0 > 0 and g0 > 0 and h0>0 test = eucint == b0.divmod(f0).at(0) and eucint == c0.divmod(g0).at(0) and eucint == d0.divmod(h0).at(0) and eucint > 0 end if test then p "here" res_a << eucint a0 = e1 b0 = f1 c0 = g1 d0 = h1 e0 = a1-(e1*eucint) f0 = b1-(f1*eucint) g0 = c1-(g1*eucint) h0 = d1-(h1*eucint) a1 = a0 b1 = b0 c1 = c0 d1 = d0 e1 = e0 f1 = f0 g1 = g0 h1 = h0 self.to_pix(a1,b1,c1,d1,e1,f1,g1,h1) end tmp += 1 a0 = a1 b0 = b1 c0 = c1 d0 = d1 e0 = e1 f0 = f1 g0 = g1 h0 = h1 end res_a end |
#partition(size = 9, length = 100) ⇒ Object
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 |
# File 'lib/m500.rb', line 2478 def partition(size=9,length=100) # stop if ref e = "" e = "-" if @absolute == -1 rep = @repetend x = [] y = length length.times{ rep.each{|n| x << n y =- 1 break if y <= 0} } res = "[#{e}" + @integralK.to_s.to_s + ", " + (@fractionalK + x ).to_s.gsub("[","") start = 0 res.insert(start,"[") length.times{ start += size break if start > res.length res.insert(start, "],[")} res.gsub!("[, ","[").gsub(",]","]").gsub(",[]","]") t = eval(res + "]") t.delete_at(t.length - 1) p zz = t.delete_at(t.length - 1) zz = Kettenbruch::leichtfactory(zz) t.map!{|x| Kettenbruch::leichtfactory(x,false) # leicht? ? ret = Kettenbruch::leichtfactory(n) : ret = Kettenbruch::factory(n) } t << zz end |
#rational? ⇒ Boolean
2527 2528 2529 |
# File 'lib/m500.rb', line 2527 def rational? @repetend == "" ? true : false end |
#reciprical ⇒ Object
2546 2547 2548 2549 2550 |
# File 'lib/m500.rb', line 2546 def reciprical a = self.to_a a.shift if @integralK == 0 @integralK == 0 ? a.to_K : ([0] + a).to_K end |
#showRE(a, re) ⇒ Object
helper =8=
2838 2839 2840 2841 2842 2843 2844 |
# File 'lib/m500.rb', line 2838 def showRE(a,re) # helper =8= if a =~ re "#{$`}<<#{$&}>>#{$'}" else "no match" end end |
#succ ⇒ Object
2784 2785 2786 |
# File 'lib/m500.rb', line 2784 def succ self + @@delta end |
#to_a ⇒ Object
2530 2531 2532 2533 2534 2535 2536 2537 2538 |
# File 'lib/m500.rb', line 2530 def to_a ret = [] tmp1 = [@integralK.to_i] + @fractionalK tmp1 << @repetend unless @repetend == "" tmp1.each{|n| ret << n } ret end |
#to_Dec ⇒ Object
2833 2834 2835 |
# File 'lib/m500.rb', line 2833 def to_Dec self.to_Frac.to_Dec end |
#to_f ⇒ Object
2805 2806 2807 |
# File 'lib/m500.rb', line 2805 def to_f self.to_Q.to_f end |
#to_Frac ⇒ Object
2827 2828 2829 |
# File 'lib/m500.rb', line 2827 def to_Frac @to_Frac ||= eval(k2Frac) end |
#to_N ⇒ Object
2808 2809 2810 |
# File 'lib/m500.rb', line 2808 def to_N @@fractionalK.empty? and @absolute == 1 and @integralK != 0 ? Natural(@integralK) : EmptySetClass #and repetend.nil? end |
#to_N0 ⇒ Object
2811 2812 2813 |
# File 'lib/m500.rb', line 2811 def to_N0 @@fractionalK.empty? and @absolute == 1 ? Counting(@integralK) : EmptySetClass #and repetend.nil? end |
#to_Q ⇒ Object
2830 2831 2832 |
# File 'lib/m500.rb', line 2830 def to_Q self.to_Frac.to_Q end |
#to_R ⇒ Object
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 |
# File 'lib/m500.rb', line 2836 def to_R self.to_Frac def showRE(a,re) # helper =8= if a =~ re "#{$`}<<#{$&}>>#{$'}" else "no match" end end end |
#to_s ⇒ Object
2539 2540 2541 2542 2543 2544 2545 |
# File 'lib/m500.rb', line 2539 def to_s @@precision.times{patt_succ} @absolute == -1 ? e = "-" : e = "" res = "#{e}" + @integralK.to_s.to_s + @@Separator + " " irrational? ? res += @longfracK.to_s.gsub("[","").gsub("]","") + " ..." : res += @fractionalK.to_s.gsub("[","").gsub("]","") res end |
#to_sgml ⇒ Object
2846 2847 2848 |
# File 'lib/m500.rb', line 2846 def to_sgml "<mn class='Kuttenbruch'>#{self.to_s}</mn>" end |
#to_Z ⇒ Object
2814 2815 2816 |
# File 'lib/m500.rb', line 2814 def to_Z @@fractionalK.empty? and @absolute == 1 ? Zahlen(@integralK) : EmptySetClass #and repetend.nil? end |