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.



2475
2476
2477
# File 'lib/m500.rb', line 2475

def absolute
  @absolute
end

#canonicalObject

Returns the value of attribute canonical.



2475
2476
2477
# File 'lib/m500.rb', line 2475

def canonical
  @canonical
end

#continuedFractionObject

Returns the value of attribute continuedFraction.



2475
2476
2477
# File 'lib/m500.rb', line 2475

def continuedFraction
  @continuedFraction
end

#fractionalKObject

Returns the value of attribute fractionalK.



2475
2476
2477
# File 'lib/m500.rb', line 2475

def fractionalK
  @fractionalK
end

#integralKObject

Returns the value of attribute integralK.



2475
2476
2477
# File 'lib/m500.rb', line 2475

def integralK
  @integralK
end

#leichtObject

Returns the value of attribute leicht.



2475
2476
2477
# File 'lib/m500.rb', line 2475

def leicht
  @leicht
end

#longfracKObject

Returns the value of attribute longfracK.



2475
2476
2477
# File 'lib/m500.rb', line 2475

def longfracK
  @longfracK
end

#patt_succObject

Returns the value of attribute patt_succ.



2475
2476
2477
# File 'lib/m500.rb', line 2475

def patt_succ
  @patt_succ
end

Class Method Details

.allseitigfactory(a, y = '') ⇒ Object



2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
# File 'lib/m500.rb', line 2412

def Kettenbruch::allseitigfactory(a,y='')
  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})))"
  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



2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
# File 'lib/m500.rb', line 2431

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

.SeparatorObject



2425
2426
2427
# File 'lib/m500.rb', line 2425

def Kettenbruch::Separator
  @@Separator
end

.Separator=(a) ⇒ Object



2428
2429
2430
# File 'lib/m500.rb', line 2428

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

Instance Method Details

#*(a) ⇒ Object



2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
# File 'lib/m500.rb', line 2588

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



2741
2742
2743
# File 'lib/m500.rb', line 2741

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

#+(a) ⇒ Object



2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
# File 'lib/m500.rb', line 2550

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



2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
# File 'lib/m500.rb', line 2569

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



2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
# File 'lib/m500.rb', line 2607

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



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
# File 'lib/m500.rb', line 2744

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



2774
2775
2776
2777
2778
2779
2780
2781
2782
# File 'lib/m500.rb', line 2774

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



2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
# File 'lib/m500.rb', line 2858

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



2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
# File 'lib/m500.rb', line 2508

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



2855
2856
2857
# File 'lib/m500.rb', line 2855

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

#irrational?Boolean

Returns:

  • (Boolean)


2523
2524
2525
# File 'lib/m500.rb', line 2523

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

#isRE?(a, re) ⇒ Boolean

helper =8=

Returns:

  • (Boolean)


2848
2849
2850
2851
2852
2853
2854
# File 'lib/m500.rb', line 2848

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

#k2FracObject



2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
# File 'lib/m500.rb', line 2816

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::factory(n)
  end
  ret
end

#last2convergentsObject



2521
2522
# File 'lib/m500.rb', line 2521

def last2convergents
end

#operation(z, other) ⇒ Object



2628
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
# File 'lib/m500.rb', line 2628

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



2477
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
# File 'lib/m500.rb', line 2477

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

Returns:

  • (Boolean)


2526
2527
2528
# File 'lib/m500.rb', line 2526

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

#recipricalObject



2545
2546
2547
2548
2549
# File 'lib/m500.rb', line 2545

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=



2837
2838
2839
2840
2841
2842
2843
# File 'lib/m500.rb', line 2837

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

#succObject



2783
2784
2785
# File 'lib/m500.rb', line 2783

def succ
  self + @@delta
end

#to_aObject



2529
2530
2531
2532
2533
2534
2535
2536
2537
# File 'lib/m500.rb', line 2529

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

#to_DecObject



2832
2833
2834
# File 'lib/m500.rb', line 2832

def to_Dec
  self.to_Frac.to_Dec
end

#to_fObject



2804
2805
2806
# File 'lib/m500.rb', line 2804

def to_f
  self.to_Q.to_f
end

#to_FracObject



2826
2827
2828
# File 'lib/m500.rb', line 2826

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

#to_NObject



2807
2808
2809
# File 'lib/m500.rb', line 2807

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

#to_N0Object



2810
2811
2812
# File 'lib/m500.rb', line 2810

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

#to_QObject



2829
2830
2831
# File 'lib/m500.rb', line 2829

def to_Q
  self.to_Frac.to_Q
end

#to_RObject



2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
# File 'lib/m500.rb', line 2835

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

#to_sObject



2538
2539
2540
2541
2542
2543
2544
# File 'lib/m500.rb', line 2538

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



2845
2846
2847
# File 'lib/m500.rb', line 2845

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

#to_ZObject



2813
2814
2815
# File 'lib/m500.rb', line 2813

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