Class: When::Coordinates::Residue::BestRationalApproximations

Inherits:
Parts::Enumerator
  • Object
show all
Defined in:
lib/when_exe/coordinates.rb

Overview

最良近似分数の系列を生成する Enumerator

Instance Attribute Summary

Attributes inherited from Parts::Enumerator

#count, #count_limit, #current, #direction, #exdate, #first, #last, #parent, #processed

Instance Method Summary collapse

Methods inherited from Parts::Enumerator

#each, #exclude_end, #has_next?, #next, #with_index, #with_object

Constructor Details

#initialize(parent, options = {}) ⇒ BestRationalApproximations

オブジェクトの生成

Parameters:

  • parent (When::Coordinates::Residue)

    生成元の剰余類オブジェクト

  • options (Hash) (defaults to: {})

    下記の通り

Options Hash (options):

  • :error (Numeric)

    収束とみなす誤差(デフォルト 1E-5)

  • :count_limit (Integer)

    最大繰り返し回数



591
592
593
594
595
596
597
# File 'lib/when_exe/coordinates.rb', line 591

def initialize(*args)
  @y = args[0].divisor
  @x = args[0].remainder + @y * args[0].carry
  super
  @error       = @options[:error] || 1e-15
  @count_limit = @options[:count_limit]
end

Instance Method Details

#_rewindvoid

This method returns an undefined value.

Enumerator の巻き戻し



546
547
548
549
550
551
552
# File 'lib/when_exe/coordinates.rb', line 546

def _rewind
  @z = @x/@y
  @k = @z.floor
  @p = [1,@k]
  @q = [0, 1]
  super
end

#succArray<Numeric>

最良近似分数を生成する

Returns:

  • (Array<Numeric>)

    ( remainder, divisor, error )

    remainder (Integer) 分子
    divisor (Integer) 分母
    error (Float) 誤差


561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/when_exe/coordinates.rb', line 561

def succ
  value = @current
  if (@count_limit.kind_of?(Numeric) && @count >= @count_limit) ||
     (@error.kind_of?(Numeric) && @e && @error >= @e.abs)
    @current = nil
  else
    if @z==@k
      @e = 0
      @current = [@p[1], @q[1], 0]
    else
      @z = 1.0/(@z-@k)
      @k = @z.floor
      @e = @p[1].to_f/@q[1]-@x.to_f/@y
      @current = [@p[1], @q[1], @e]
      @p = [@p[1], @p[1]*@k + @p[0]]
      @q = [@q[1], @q[1]*@k + @q[0]]
    end
    @count += 1
  end
  return value
end