class Kettenbruch

Attributes

absolute[RW]
canonical[RW]
continuedFraction[RW]
fractionalK[RW]
integralK[RW]
leicht[RW]
longfracK[RW]
patt_succ[RW]

Public Class Methods

Separator() click to toggle source
# File lib/m500.rb, line 2513
def Kettenbruch::Separator
  @@Separator
end
Separator=(a) click to toggle source
# File lib/m500.rb, line 2516
def Kettenbruch::Separator=(a)
  @@Separator = a
end
allseitigfactory(a,y=true) click to toggle source
# File lib/m500.rb, line 2500
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) click to toggle source
# File lib/m500.rb, line 2483
def Kettenbruch::euclid(a)
  a.denominator.divmod(a.numerator)
end
leichtfactory(a,y=true) click to toggle source
# File lib/m500.rb, line 2486
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) click to toggle source
# File lib/m500.rb, line 2519
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) click to toggle source
# File lib/m500.rb, line 2480
def Kettenbruch::precision=(a)
  @@precision = a.to_i
end

Private Class Methods

new(int,cfra,rep='',absolute=1) click to toggle source
# File lib/m500.rb, line 2537
def initialize(int,cfra,rep='',absolute=1)
  @canonical =[]
  @integralK = int
  @fractionalK=[]
  @setdelta = false
  @deltaIter = 10
  @fractionalK_1=[]
  @repetend = rep 
  @leicht = true
  if cfra.kind_of?(Array)
    @fractionalK=cfra
  else
    tmp = -1
    c = cfra
    until tmp == 0
      b = Kettenbruch::euclid(c)
      @fractionalK << b.at(0)
      tmp = b.at(1)
      c = Quotient(tmp,c.numerator)
    end
  end
  @longfracK = @fractionalK.dup
end

Public Instance Methods

*(a) click to toggle source
# File lib/m500.rb, line 2671
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) click to toggle source
# File lib/m500.rb, line 2815
def ** (a)
  self.to_f ** a.to_f
end
+(a) click to toggle source
# File lib/m500.rb, line 2633
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) click to toggle source
# File lib/m500.rb, line 2652
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) click to toggle source
# File lib/m500.rb, line 2690
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) click to toggle source
# File lib/m500.rb, line 2818
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) click to toggle source
# File lib/m500.rb, line 2848
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) click to toggle source
# File lib/m500.rb, line 2932
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() click to toggle source
# File lib/m500.rb, line 2591
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() click to toggle source
# File lib/m500.rb, line 2929
def inspect
  sprintf("Kuttenbruch(%s)", to_a.to_s)
end
irrational?() click to toggle source
# File lib/m500.rb, line 2606
def irrational?
  @repetend == "" ? false : true
end
isRE?(a,re) click to toggle source
# File lib/m500.rb, line 2922
def isRE?(a,re) #helper   =8=
  if a =~ re  
    true
  else  
    false
  end  
end
k2Frac() click to toggle source
# File lib/m500.rb, line 2890
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() click to toggle source
# File lib/m500.rb, line 2604
def last2convergents
end
operation(z,other) click to toggle source
# File lib/m500.rb, line 2711
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) click to toggle source
# File lib/m500.rb, line 2563
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?() click to toggle source
# File lib/m500.rb, line 2609
def rational?
  @repetend == "" ? true : false
end
reciprical() click to toggle source
# File lib/m500.rb, line 2628
def reciprical
  a = self.to_a
  a.shift if @integralK == 0
  @integralK == 0 ? a.to_K :  ([0] + a).to_K
end
showRE(a,re) click to toggle source
# File lib/m500.rb, line 2911
def showRE(a,re)  # helper =8=
  if a =~ re  
    "#{$`}<<#{$&}>>#{$'}"  
  else  
    "no match"  
  end  
end
succ() click to toggle source
# File lib/m500.rb, line 2857
def succ
  self + @@delta
end
to_Dec() click to toggle source
# File lib/m500.rb, line 2906
def to_Dec
  self.to_Frac.to_Dec
end
to_Frac() click to toggle source
# File lib/m500.rb, line 2900
def to_Frac
  @to_Frac ||= eval(k2Frac)
end
to_N() click to toggle source
# File lib/m500.rb, line 2881
def to_N
      @@fractionalK.empty? and   @absolute == 1 and @integralK != 0 ? Natural(@integralK) : EmptySetClass #and repetend.nil?
end
to_N0() click to toggle source
# File lib/m500.rb, line 2884
def to_N0
  @@fractionalK.empty? and   @absolute == 1 ? Counting(@integralK) : EmptySetClass #and repetend.nil?
end
to_Q() click to toggle source
# File lib/m500.rb, line 2903
def to_Q
  self.to_Frac.to_Q
end
to_R() click to toggle source
# File lib/m500.rb, line 2909
def to_R
  self.to_Frac
  def showRE(a,re)  # helper =8=
    if a =~ re  
      "#{$`}<<#{$&}>>#{$'}"  
    else  
      "no match"  
    end  
  end 
end
to_Z() click to toggle source
# File lib/m500.rb, line 2887
def to_Z
  @@fractionalK.empty? and   @absolute == 1 ? Zahlen(@integralK) : EmptySetClass #and repetend.nil?
end
to_a() click to toggle source
# File lib/m500.rb, line 2612
def to_a
  ret = []
  tmp1 = [@integralK.to_i] + @fractionalK
  tmp1 << @repetend unless @repetend == ""
  tmp1.each{|n|
    ret << n
  }
  ret
end
to_f() click to toggle source
# File lib/m500.rb, line 2878
def to_f
  self.to_Q.to_f
end
to_s() click to toggle source
# File lib/m500.rb, line 2621
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() click to toggle source
# File lib/m500.rb, line 2919
def to_sgml
  "<mn class='Kuttenbruch'>#{self.to_s}</mn>"
end