Class: Kettenbruch

Inherits:
Numeric show all
Defined in:
lib/m500.rb

Constant Summary collapse

@@delta =
0
@@precision =
20
@@Separator =
";"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#sgml_id, #to_K, #to_Sig

Methods included from SGML

#sgml_id, #tog_sgml_id

Instance Attribute Details

#absoluteObject

Returns the value of attribute absolute.



2541
2542
2543
# File 'lib/m500.rb', line 2541

def absolute
  @absolute
end

#canonicalObject

Returns the value of attribute canonical.



2541
2542
2543
# File 'lib/m500.rb', line 2541

def canonical
  @canonical
end

#continuedFractionObject

Returns the value of attribute continuedFraction.



2541
2542
2543
# File 'lib/m500.rb', line 2541

def continuedFraction
  @continuedFraction
end

#fractionalKObject

Returns the value of attribute fractionalK.



2541
2542
2543
# File 'lib/m500.rb', line 2541

def fractionalK
  @fractionalK
end

#integralKObject

Returns the value of attribute integralK.



2541
2542
2543
# File 'lib/m500.rb', line 2541

def integralK
  @integralK
end

#leichtObject

Returns the value of attribute leicht.



2541
2542
2543
# File 'lib/m500.rb', line 2541

def leicht
  @leicht
end

#longfracKObject

Returns the value of attribute longfracK.



2541
2542
2543
# File 'lib/m500.rb', line 2541

def longfracK
  @longfracK
end

#patt_succObject

Returns the value of attribute patt_succ.



2541
2542
2543
# File 'lib/m500.rb', line 2541

def patt_succ
  @patt_succ
end

Class Method Details

.allseitigfactory(a, y = true) ⇒ Object



2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
# File 'lib/m500.rb', line 2480

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(#{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



2463
2464
2465
# File 'lib/m500.rb', line 2463

def Kettenbruch::euclid(a)
  a.denominator.divmod(a.numerator)
end

.leichtfactory(a, y = true) ⇒ Object



2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
# File 'lib/m500.rb', line 2466

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 = "&&&&"
  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



2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
# File 'lib/m500.rb', line 2499

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



2460
2461
2462
# File 'lib/m500.rb', line 2460

def Kettenbruch::precision=(a)
  @@precision = a.to_i
end

.SeparatorObject



2493
2494
2495
# File 'lib/m500.rb', line 2493

def Kettenbruch::Separator
  @@Separator
end

.Separator=(a) ⇒ Object



2496
2497
2498
# File 'lib/m500.rb', line 2496

def Kettenbruch::Separator=(a)
  @@Separator = a
end

Instance Method Details

#*(a) ⇒ Object



2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
# File 'lib/m500.rb', line 2651

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



2795
2796
2797
# File 'lib/m500.rb', line 2795

def ** (a)
  self.to_f ** a.to_f
end

#+(a) ⇒ Object



2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
# File 'lib/m500.rb', line 2613

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



2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
# File 'lib/m500.rb', line 2632

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



2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
# File 'lib/m500.rb', line 2670

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



2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
# File 'lib/m500.rb', line 2798

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



2828
2829
2830
2831
2832
2833
2834
2835
2836
# File 'lib/m500.rb', line 2828

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



2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
# File 'lib/m500.rb', line 2912

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

#convergentsObject



2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
# File 'lib/m500.rb', line 2571

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

#inspectObject



2909
2910
2911
# File 'lib/m500.rb', line 2909

def inspect
  sprintf("Kuttenbruch(%s)", to_a.to_s)
end

#irrational?Boolean

Returns:

  • (Boolean)


2586
2587
2588
# File 'lib/m500.rb', line 2586

def irrational?
  @repetend == "" ? false : true
end

#isRE?(a, re) ⇒ Boolean

helper =8=

Returns:

  • (Boolean)


2902
2903
2904
2905
2906
2907
2908
# File 'lib/m500.rb', line 2902

def isRE?(a,re) #helper   =8=
  if a =~ re  
    true
  else  
    false
  end  
end

#k2FracObject



2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
# File 'lib/m500.rb', line 2870

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

#last2convergentsObject



2584
2585
# File 'lib/m500.rb', line 2584

def last2convergents
end

#operation(z, other) ⇒ Object



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
2742
2743
2744
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
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
# File 'lib/m500.rb', line 2691

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)
  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
      eggs = false if a0.divmod(e0).at(0) == c0.divmod(g0).at(0)
    end
    if eggs then # "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   # "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
      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



2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
# File 'lib/m500.rb', line 2543

def partition(size=9,length=100)
  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)
  zz = t.delete_at(t.length - 1)
  zz =  Kettenbruch::leichtfactory(zz)
  t.map!{|x|
    Kettenbruch::leichtfactory(x,false)
  }
  t << zz
end

#rational?Boolean

Returns:

  • (Boolean)


2589
2590
2591
# File 'lib/m500.rb', line 2589

def rational?
  @repetend == "" ? true : false
end

#recipricalObject



2608
2609
2610
2611
2612
# File 'lib/m500.rb', line 2608

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=



2891
2892
2893
2894
2895
2896
2897
# File 'lib/m500.rb', line 2891

def showRE(a,re)  # helper =8=
  if a =~ re  
    "#{$`}<<#{$&}>>#{$'}"  
  else  
    "no match"  
  end  
end

#succObject



2837
2838
2839
# File 'lib/m500.rb', line 2837

def succ
  self + @@delta
end

#to_aObject



2592
2593
2594
2595
2596
2597
2598
2599
2600
# File 'lib/m500.rb', line 2592

def to_a
  ret = []
  tmp1 = [@integralK.to_i] + @fractionalK
  tmp1 << @repetend unless @repetend == ""
  tmp1.each{|n|
    ret << n
  }
  ret
end

#to_DecObject



2886
2887
2888
# File 'lib/m500.rb', line 2886

def to_Dec
  self.to_Frac.to_Dec
end

#to_fObject



2858
2859
2860
# File 'lib/m500.rb', line 2858

def to_f
  self.to_Q.to_f
end

#to_FracObject



2880
2881
2882
# File 'lib/m500.rb', line 2880

def to_Frac
  @to_Frac ||= eval(k2Frac)
end

#to_NObject



2861
2862
2863
# File 'lib/m500.rb', line 2861

def to_N
      @@fractionalK.empty? and   @absolute == 1 and @integralK != 0 ? Natural(@integralK) : EmptySetClass #and repetend.nil?
end

#to_N0Object



2864
2865
2866
# File 'lib/m500.rb', line 2864

def to_N0
  @@fractionalK.empty? and   @absolute == 1 ? Counting(@integralK) : EmptySetClass #and repetend.nil?
end

#to_QObject



2883
2884
2885
# File 'lib/m500.rb', line 2883

def to_Q
  self.to_Frac.to_Q
end

#to_RObject



2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
# File 'lib/m500.rb', line 2889

def to_R
  self.to_Frac
  def showRE(a,re)  # helper =8=
    if a =~ re  
      "#{$`}<<#{$&}>>#{$'}"  
    else  
      "no match"  
    end  
  end 
end

#to_sObject



2601
2602
2603
2604
2605
2606
2607
# File 'lib/m500.rb', line 2601

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_sgmlObject



2899
2900
2901
# File 'lib/m500.rb', line 2899

def to_sgml
  "<mn class='Kuttenbruch'>#{self.to_s}</mn>"
end

#to_ZObject



2867
2868
2869
# File 'lib/m500.rb', line 2867

def to_Z
  @@fractionalK.empty? and   @absolute == 1 ? Zahlen(@integralK) : EmptySetClass #and repetend.nil?
end