diff --git a/docs/FareyTree.html b/docs/FareyTree.html deleted file mode 100644 index 64c0050..0000000 --- a/docs/FareyTree.html +++ /dev/null @@ -1,213 +0,0 @@ - - - - - - - Class: FareyTree - - — Documentation by YARD 0.9.34 - - - - - - - - - - - - - - - - - - - -
- - -

Class: FareyTree - - - -

-
- -
-
Inherits:
-
- FractionTree - - - show all - -
-
- - - - - - - - - - - -
-
Defined in:
-
lib/fraction_tree.rb
-
- -
- -
-

Direct Known Subclasses

-

KeyboardTree, Log2Tree, ScaleStepTree

-
- - - -

Constant Summary

- -

Constants inherited - from FractionTree

-

FractionTree::DEFAULT_TREE_DEPTH

- - - - - - -

- Class Method Summary - collapse -

- - - - - - - - - - - - - -

Methods inherited from FractionTree

-

child_of, common_ancestors_between, descendancy_from, descendants_of, farey_neighbors, farey_neighbors?, numeric_sequence, parents_of, path_to, quotient_walk, sequence, tree

- - -
-

Class Method Details

- - -
-

- - .base_segmentObject - - - - - -

- - - - -
-
-
-
-318
-319
-320
-
-
# File 'lib/fraction_tree.rb', line 318
-
-def self.base_segment
-  [Node.new(0,1), Node.new(1,1)]
-end
-
-
- -
- -
- - - -
- - \ No newline at end of file diff --git a/docs/FractionTree.html b/docs/FractionTree.html index e4853a4..b441513 100644 --- a/docs/FractionTree.html +++ b/docs/FractionTree.html @@ -6,7 +6,7 @@ Class: FractionTree - — Documentation by YARD 0.9.34 + — Documentation by YARD 0.9.36 @@ -94,7 +94,9 @@
Defined in:
-
lib/fraction_tree.rb
+
lib/fraction_tree/node.rb,
+ lib/fraction_tree/fraction_tree.rb
+
@@ -124,11 +126,7 @@

Overview

-
-

Direct Known Subclasses

-

FareyTree, OctaveReducedTree, ScaleTree, SternBrocotTree

-
-

Defined Under Namespace

+

Defined Under Namespace

@@ -172,7 +170,7 @@

  • - .base_segment ⇒ Array + .child_of(number1, number2) ⇒ FractionTree::Node @@ -187,7 +185,7 @@

    -

    The boundary nodes of the tree.

    +

    The mediant child of the given numbers.

  • @@ -196,7 +194,7 @@

  • - .child_of(number1, number2, strict_neighbors: true) ⇒ FractionTree::Node + .decode(str) ⇒ FractionTree::Node @@ -211,7 +209,7 @@

    -

    The mediant child of the given numbers Return nil if bc - ad |= 1, for a/b, c/d.

    +

    The node decoded from the given string.

  • @@ -220,7 +218,7 @@

  • - .common_ancestors_between(number1, number2) ⇒ Array + .descendants_of(parent1, parent2, depth: 5) ⇒ Array @@ -235,7 +233,7 @@

    -

    The ancestors shared by the given descendants.

    +

    Of fraction tree nodes descended from parent1 and parent2 Return empty array if bc - ad |= 1, for a/b, c/d.

  • @@ -244,7 +242,7 @@

  • - .descendancy_from(number, depth = 5) ⇒ Array + .left_node ⇒ Object @@ -259,7 +257,7 @@

    -

    The descendants of number starting at its parents.

    +

    The left-most node of the range of the tree.

  • @@ -268,7 +266,7 @@

  • - .descendants_of(parent1, parent2, depth = 5, strict_neighbors: true) ⇒ Array + .left_node=(rhs) ⇒ Object @@ -283,7 +281,7 @@

    -

    Of nodes descended from parent1 and parent2 Return empty array if bc - ad |= 1, for a/b, c/d.

    +

    Set the left-most node of the range of the tree.

  • @@ -292,7 +290,7 @@

  • - .farey_neighbors(number, range = 10**(decimal_power(number.numerator)+2)) ⇒ Array + .mediant_sum(n1, n2) ⇒ FractionTree::Node @@ -307,7 +305,7 @@

    -

    Of Farey neighbors to the given number.

    +

    The mediant sum of the given numbers.

  • @@ -316,7 +314,7 @@

  • - .farey_neighbors?(num1, num2) ⇒ Boolean + .neighbors?(number1, number2) ⇒ Boolean @@ -331,7 +329,7 @@

    -

    Whether two numbers are Farey neighbors.

    +

    Whether two numbers are neighbors.

  • @@ -340,7 +338,7 @@

  • - .numeric_sequence ⇒ Array + .node(number) ⇒ FractionTree::Node @@ -355,7 +353,7 @@

    -

    Of numerators of the fraction tree nodes.

    +

    The node in the tree representing the given number.

  • @@ -364,7 +362,7 @@

  • - .parents_of(number) ⇒ Array + .nodes ⇒ Object @@ -379,7 +377,7 @@

    -

    A pair of fraction tree nodes leading to the given number.

    +

    The cache of nodes used for faster lookup.

  • @@ -388,7 +386,7 @@

  • - .path_to(number, find_parents: false, segment: base_segment) ⇒ Array + .numeric_sequence ⇒ Array @@ -403,7 +401,7 @@

    -

    Set of fraction nodes leading to the given number.

    +

    Of numerators of the fraction tree nodes.

  • @@ -412,7 +410,7 @@

  • - .quotient_walk(number, limit: 10, segment: computed_base_segment(number)) ⇒ Array + .range ⇒ Object @@ -427,7 +425,7 @@

    -

    Of FractionTree::Nodes leading quotient-wise to number.

    +

    The range of the tree.

  • @@ -436,7 +434,7 @@

  • - .sequence(depth = 5, segment: base_segment) ⇒ Array + .range=(rhs) ⇒ Object @@ -451,7 +449,7 @@

    -

    A sequence of fraction tree nodes.

    +

    Set the range of the tree.

  • @@ -460,7 +458,7 @@

  • - .tree(depth = DEFAULT_TREE_DEPTH) ⇒ Array + .reset_nodes ⇒ Object @@ -475,92 +473,121 @@

    -

    A multi-dimensional array with the elements of fraction tree, organized by level/row.

    +

    Reset the cache of nodes.

  • - - +
  • + + + .right_node ⇒ Object + + + + + + + + + + + +
    +

    The right-most node of the range of the tree.

    +
    + +
  • -
    -

    Class Method Details

    + +
  • + + + .right_node=(rhs) ⇒ Object + -
    -

    + + + + + - .base_segmentArray - -

    -
    +
    +

    Set the right-most node of the range of the tree.

    +
    + +
  • + + +
  • + + + .sequence(depth: 5, left_node: default_left_node, right_node: default_right_node) ⇒ Array -

    Returns the boundary nodes of the tree.

    + +
    + + + + + + + -
  • - -
    -
    -

    Examples:

    - +
    +

    A sequence of fraction tree nodes.

    +
    + + + -
    FractionTree.base_segment => [(0/1), (1/0)]
    +
  • + + + .tree(depth: 10, left_node: default_left_node, right_node: default_right_node) ⇒ Array -
  • -

    Returns:

    - - -
    - - - - -
    -
     
     
    -14
    -15
    -16
    -
    -
    # File 'lib/fraction_tree.rb', line 14
    +  
    +

    Class Method Details

    -def base_segment - [Node.new(0,1), Node.new(1,0)] -end
    -
    - -
    -

    +
    +

    - .child_of(number1, number2, strict_neighbors: true) ⇒ FractionTree::Node + .child_of(number1, number2) ⇒ FractionTree::Node @@ -569,7 +596,15 @@

    -

    The mediant child of the given numbers Return nil if bc - ad |= 1, for a/b, c/d

    +
    + Note: +
    +

    return nil if bc - ad |= 1, for a/b, c/d

    +
    +
    + + +

    Returns the mediant child of the given numbers.

    @@ -615,24 +650,6 @@

    two of two parents

    -
    - - - -
  • - - strict_neighbors - - - (Boolean) - - - (defaults to: true) - - - — -
    -

    whether to apply the strict Farey tree neighbor requirement

  • @@ -651,7 +668,7 @@

    -

    the mediant child of the given numbers Return nil if bc - ad |= 1, for a/b, c/d

    +

    the mediant child of the given numbers

    @@ -664,17 +681,19 @@

     
     
    -140
    -141
    -142
    -143
    +171 +172 +173 +174 +175 -
    # File 'lib/fraction_tree.rb', line 140
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 171
     
    -def child_of(number1, number2, strict_neighbors: true)
    -  return nil unless farey_neighbors?(number1, number2) || !strict_neighbors
    -  Node.new(number1.numerator, number1.denominator) + Node.new(number2.numerator, number2.denominator)
    +def child_of(number1, number2)
    +  return nil unless neighbors?(number1, number2)
    +  # node(number1.numerator, number1.denominator) + node(number2.numerator, number2.denominator)
    +  node(number1) + node(number2)
     end
    @@ -682,9 +701,9 @@

    -

    +

    - .common_ancestors_between(number1, number2) ⇒ Array + .decode(str) ⇒ FractionTree::Node @@ -693,7 +712,7 @@

    -

    Returns the ancestors shared by the given descendants.

    +

    Returns the node decoded from the given string.

    @@ -704,46 +723,9 @@

    Examples:

    -
    FractionTree.common_ancestors_between(4/3r, 7/4r)
    -=> [(1/1), (2/1), (3/2)]
    +
    FractionTree.decode("RLL") => (4/3)

    -

    Parameters:

    -
      - -
    • - - number1 - - - (Rational) - - - - — -
      -

      one of two descendants

      -
      - -
    • - -
    • - - number2 - - - (Rational) - - - - — -
      -

      two of two descendants

      -
      - -
    • - -

    Returns:

      @@ -751,13 +733,13 @@

    • - (Array) + (FractionTree::Node)
      -

      the ancestors shared by the given descendants

      +

      the node decoded from the given string

    • @@ -770,15 +752,17 @@

       
       
      -113
      -114
      -115
      +101 +102 +103 +104 -
      # File 'lib/fraction_tree.rb', line 113
      +      
      # File 'lib/fraction_tree/fraction_tree.rb', line 101
       
      -def common_ancestors_between(number1, number2)
      -  path_to(number1) & path_to(number2)
      +def decode(str)
      +  wrk_node = Node.decode(str)
      +  nodes[wrk_node.number] ||= wrk_node
       end
      @@ -786,9 +770,9 @@

    -

    +

    - .descendancy_from(number, depth = 5) ⇒ Array + .descendants_of(parent1, parent2, depth: 5) ⇒ Array @@ -797,7 +781,7 @@

    -

    Returns the descendants of number starting at its parents.

    +

    Of fraction tree nodes descended from parent1 and parent2 Return empty array if bc - ad |= 1, for a/b, c/d

    @@ -808,7 +792,7 @@

    Examples:

    -
    FractionTree.descendancy_from(5/4r, 3)
    +      
    FractionTree.descendants_of(1/1r, 4/3r)
     => [(1/1), (7/6), (6/5), (11/9), (5/4), (14/11), (9/7), (13/10), (4/3)]

    @@ -817,7 +801,23 @@

  • - number + parent1 + + + (Rational) + + + + — +
    +

    one of two parents

    +
    + +
  • + +
  • + + parent2 (Rational) @@ -826,7 +826,7 @@

    -

    around which descendancy is focused

    +

    two of two parents

  • @@ -844,7 +844,7 @@

    -

    how many nodes to collect

    +

    the depth to collect

    @@ -863,7 +863,7 @@

    -

    the descendants of number starting at its parents

    +

    of fraction tree nodes descended from parent1 and parent2 Return empty array if bc - ad |= 1, for a/b, c/d

    @@ -876,17 +876,17 @@

     
     
    -125
    -126
    -127
    -128
    +187 +188 +189 +190 -
    # File 'lib/fraction_tree.rb', line 125
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 187
     
    -def descendancy_from(number, depth=5)
    -  parent1, parent2 = parents_of(number)
    -  descendants_of(parent1, parent2, depth)
    +def descendants_of(parent1, parent2, depth: 5)
    +  return [] unless neighbors?(parent1, parent2)
    +  sequence(depth:, left_node: Node.new(parent1), right_node: Node.new(parent2))
     end
    @@ -894,9 +894,9 @@

    -

    +

    - .descendants_of(parent1, parent2, depth = 5, strict_neighbors: true) ⇒ Array + .left_nodeObject @@ -905,7 +905,15 @@

    -

    Of nodes descended from parent1 and parent2 Return empty array if bc - ad |= 1, for a/b, c/d

    +
    + Note: +
    +

    defaults to Stern-Brocot left-most range, 0

    +
    +
    + + +

    Returns the left-most node of the range of the tree.

    @@ -916,82 +924,125 @@

    Examples:

    -
    FractionTree.descendants_of(1/1r, 4/3r, 3)
    -=> [(1/1), (7/6), (6/5), (11/9), (5/4), (14/11), (9/7), (13/10), (4/3)]
    +
    FractionTree.left_node => 0/1

    -

    Parameters:

    -
      - -
    • - - parent1 - - - (Rational) - - - - — -
      -

      one of two parents

      -
      - -
    • + +

      Returns:

      +
      • - parent2 - - (Rational) + - — +
        -

        two of two parents

        +

        the left-most node of the range of the tree

      • -
      • - - depth - - - (Integer) - - - (defaults to: 5) - - - — -
        -

        the depth to collect

        +
      + +
    + + + + +
    +
    +
    +
    +14
    +15
    +16
    +
    +
    # File 'lib/fraction_tree/fraction_tree.rb', line 14
    +
    +def left_node
    +  @left_node || 0/1r
    +end
    +
    - - + +
    +

    -
  • - - strict_neighbors - - - (Boolean) - - - (defaults to: true) - - - — -
    -

    whether to apply the strict Farey tree neighbor requirement

    + .left_node=(rhs) ⇒ Object + + + + + +
  • +
    + +

    Set the left-most node of the range of the tree

    + + +
    +
    + +
    +

    Examples:

    + - +
    FractionTree.left_node = 1/1r
    + +
    + + +
    + + + + +
    +
    +
    +
    +31
    +32
    +33
    +
    +
    # File 'lib/fraction_tree/fraction_tree.rb', line 31
    +
    +def left_node=(rhs)
    +  @left_node = rhs
    +end
    +
    +
    + +
    +

    + + .mediant_sum(n1, n2) ⇒ FractionTree::Node + + - + + +

    +
    + +

    Returns the mediant sum of the given numbers.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.mediant_sum(3/2r, 4/3r) => (7/5)
    + +

    Returns:

      @@ -999,13 +1050,13 @@

    • - (Array) + (FractionTree::Node)
      -

      of nodes descended from parent1 and parent2 Return empty array if bc - ad |= 1, for a/b, c/d

      +

      the mediant sum of the given numbers

    • @@ -1018,19 +1069,15 @@

       
       
      -156
      -157
      -158
      -159
      -160
      +110 +111 +112 -
      # File 'lib/fraction_tree.rb', line 156
      +      
      # File 'lib/fraction_tree/fraction_tree.rb', line 110
       
      -def descendants_of(parent1, parent2, depth=5, strict_neighbors: true)
      -  return [] unless farey_neighbors?(parent1, parent2) || !strict_neighbors
      -  segment = [Node.new(parent1.numerator, parent1.denominator), Node.new(parent2.numerator, parent2.denominator)]
      -  sequence(depth, segment: segment)
      +def mediant_sum(n1, n2)
      +  Node.new(n1) + Node.new(n2)
       end
      @@ -1038,9 +1085,9 @@

    -

    +

    - .farey_neighbors(number, range = 10**(decimal_power(number.numerator)+2)) ⇒ Array + .neighbors?(number1, number2) ⇒ Boolean @@ -1049,7 +1096,22 @@

    -

    Returns of Farey neighbors to the given number. A Farey neighbor is a number b/c, who’s relationship to a/b is such that ad − bc = 1, when c/d < a/b and bc − ad = 1 when c/d > a/b.

    +
    + Note: +
    +

    Neighbor definition: abs(a * d - b * c) = 1, for a/b, c/d

    +
    +
    + +
    + Note: +
    +

    Float::INFINITY => 1/0

    +
    +
    + + +

    Returns whether two numbers are neighbors.

    @@ -1060,8 +1122,9 @@

    Examples:

    -
    FractionTree.farey_neighbors(3/2r, 10)
    -=> [(1/1), (2/1), (4/3), (5/3), (7/5), (8/5), (10/7), (11/7), (13/9), (14/9)]
    +
    FractionTree.neighbors?(3/2r, 4/3r) => true
    +FractionTree.neighbors?(3/2r, 7/4r) => false
    +FractionTree.neighbors?(2/1r, Float::INFINITY) => true

    Parameters:

    @@ -1069,7 +1132,7 @@

  • - number + number1 @@ -1078,25 +1141,23 @@

    -

    with neighbors

    +

    of comparison

  • - range + number2 - (defaults to: 10**(decimal_power(number.numerator)+2)) - —
    -

    of harmonic series to search

    +

    of comparison

  • @@ -1109,13 +1170,13 @@

  • - (Array) + (Boolean)
    -

    of Farey neighbors to the given number. A Farey neighbor is a number b/c, who’s relationship to a/b is such that ad − bc = 1, when c/d < a/b and bc − ad = 1 when c/d > a/b.

    +

    whether two numbers are neighbors

  • @@ -1128,33 +1189,19 @@

     
     
    -213
    -214
    -215
    -216
    -217
    -218
    -219
    -220
    -221
    -222
    -223
    -224
    +124 +125 +126 +127 +128 -
    # File 'lib/fraction_tree.rb', line 213
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 124
     
    -def farey_neighbors(number, range = 10**(decimal_power(number.numerator)+2))
    -  ratio = number.to_r
    -  denominator = ratio.denominator
    -
    -  [].tap do |collection|
    -    (1..range-1).each do |i|
    -      lower, upper = plus_minus(ratio, Rational(1,i*denominator))
    -      collection << lower if farey_neighbors?(ratio, lower)
    -      collection << upper if farey_neighbors?(ratio, upper)
    -    end
    -  end
    +def neighbors?(number1, number2)
    +  (a, b) = number1.infinite? ? [1, 0] : [number1.numerator, number1.denominator]
    +  (c, d) = number2.infinite? ? [1, 0] : [number2.numerator, number2.denominator]
    +  (a * d - b * c).abs == 1
     end
    @@ -1162,9 +1209,9 @@

    -

    +

    - .farey_neighbors?(num1, num2) ⇒ Boolean + .node(number) ⇒ FractionTree::Node @@ -1173,7 +1220,7 @@

    -

    Returns whether two numbers are Farey neighbors.

    +

    Returns the node in the tree representing the given number.

    @@ -1184,65 +1231,79 @@

    Examples:

    -
    FractionTree.farey_neighbors?(3/2r, 4/3r) => true
    -FractionTree.farey_neighbors?(3/2r, 7/4r) => false
    +
    FractionTree.node(3/2r) => (3/2)

    -

    Parameters:

    -
      + +

      Returns:

      +
      • - num1 - - + (FractionTree::Node)
        -

        of comparison

        +

        the node in the tree representing the given number

      • -
      • - - num2 - - - - - - - — -
        -

        of comparison

        +
      + +
    + + + + +
    +
    +
    +
    +92
    +93
    +94
    +95
    +
    +
    # File 'lib/fraction_tree/fraction_tree.rb', line 92
    +
    +def node(number)
    +  validate(number)
    +  nodes[number] ||= Node.new(number)
    +end
    +
    - - + +
    +

    + + .nodesObject - -

    Returns:

    -
      -
    • - - - (Boolean) - - - - — -
      -

      whether two numbers are Farey neighbors

      + + +

    +
    + +
    + Note: +
    +

    Intended for internal use.

    - - +
    + + +

    The cache of nodes used for faster lookup

    + + +
    +
    +
    -
    @@ -1250,15 +1311,15 @@

     
     
    -233
    -234
    -235
    +76 +77 +78

    @@ -1277,7 +1338,7 @@

    -

    Returns of numerators of the fraction tree nodes. Also known as the Stern-Brocot sequence.

    +

    Returns of numerators of the fraction tree nodes. Aka the Stern-Brocot sequence.

    @@ -1305,7 +1366,7 @@

    -

    of numerators of the fraction tree nodes. Also known as the Stern-Brocot sequence.

    +

    of numerators of the fraction tree nodes. Aka the Stern-Brocot sequence.

    @@ -1318,18 +1379,18 @@

     
     
    -196
    -197
    -198
    -199
    -200
    -201
    -202
    -203
    -204
    +210 +211 +212 +213 +214 +215 +216 +217 +218

    @@ -1434,9 +1482,9 @@

    -

    +

    - .path_to(number, find_parents: false, segment: base_segment) ⇒ Array + .range=(rhs) ⇒ Object @@ -1445,7 +1493,15 @@

    -

    Returns set of fraction nodes leading to the given number.

    +
    + Note: +
    +

    Accepts keywords: :farey, :keyboard, :scale_step, :log2 => (0/1..1/1) :stern_brocot, :scale => (0/1..1/0) :octave_reduced => (1/1..2/1)

    +
    +
    + + +

    Set the range of the tree

    @@ -1456,84 +1512,78 @@

    Examples:

    -
    FractionTree.path_to(7/4r) => [(1/1), (2/1), (3/2), (5/3), (7/4)]
    +
    FractionTree.range = :farey
    +=> (0/1..1/1)

    -

    Parameters:

    -
      - -
    • - - number - - - (Rational) - - - - — -
      -

      the target the fraction path leads to

      + + +

    -
    # File 'lib/fraction_tree.rb', line 233
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 76
     
    -def farey_neighbors?(num1, num2)
    -  (num1.numerator * num2.denominator - num1.denominator * num2.numerator).abs == 1
    +def nodes
    +  @@nodes ||= {}
     end
    -
    # File 'lib/fraction_tree.rb', line 196
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 210
     
     def numeric_sequence
       return enum_for :numeric_sequence unless block_given?
    @@ -1346,9 +1407,9 @@ 

    -

    +

    - .parents_of(number) ⇒ Array + .rangeObject @@ -1357,7 +1418,15 @@

    -

    Returns a pair of fraction tree nodes leading to the given number. For irrational numbers, the parent nodes are one of an infinite series, whose nearness is determined by the limits of the system.

    +
    + Note: +
    +

    defaults to Stern-Brocot range, (0..Infinity)

    +
    +
    + + +

    Returns the range of the tree.

    @@ -1368,30 +1437,9 @@

    Examples:

    -
    FractionTree.parents_of(15/13r) => [(8/7), (7/6)]
    -FractionTree.parents_of(Math::PI) => [(447288330638589/142376297616907), (436991388364966/139098679093749)]
    +
    FractionTree.range => (0/1..Infinity)

    -

    Parameters:

    -
      - -
    • - - number - - - (Rational) - - - - — -
      -

      the child number whose parents are being sought

      -
      - -
    • - -

    Returns:

      @@ -1399,13 +1447,13 @@

    • - (Array) + - — +
      -

      a pair of fraction tree nodes leading to the given number. For irrational numbers, the parent nodes are one of an infinite series, whose nearness is determined by the limits of the system

      +

      the range of the tree

    • @@ -1418,15 +1466,15 @@

       
       
      -101
      -102
      -103
      +48 +49 +50

    -
    # File 'lib/fraction_tree.rb', line 101
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 48
     
    -def parents_of(number)
    -  path_to(number, find_parents: true)
    +def range
    +  (left_node..right_node)
     end
    + + + + +
    +
    +
    +
    +61
    +62
    +63
    +64
    +65
    +66
    +67
    +68
    +69
    +70
    +71
    +72
    +
    +
    # File 'lib/fraction_tree/fraction_tree.rb', line 61
    +
    +def range=(rhs)
    +  case rhs
    +  when :farey, :keyboard, :scale_step, :log2
    +    @left_node, @right_node = 0/1r, 1/1r
    +  when :stern_brocot, :scale
    +    @left_node, @right_node = 0/1r, Float::INFINITY
    +  when :octave_reduced
    +    @left_node, @right_node = 1/1r, 2/1r
    +  else
    +    @left_node = @right_node = nil
    +  end
    +end
    +
    - - + +
    +

    -
  • - - find_parents - - - (Boolean) - - - (defaults to: false) - - - — -
    -

    list all ancestors or only immediate parents

    -
    - -
  • + .reset_nodesObject -
  • - - segment - - - (Array) - - - (defaults to: base_segment) - - - — -
    -

    a tuple of [FractionTree::Node], defining the segment’s starting left and right boundaries

    -
    - -
  • + - -

    Returns:

    -
      -
    • - - - (Array) - - - - — -
      -

      set of fraction nodes leading to the given number

      +

    +
    + +

    Reset the cache of nodes

    + + +
    - - +
    - +
    +

    Examples:

    + + +
    FractionTree.reset_nodes => {}
    + +
    +
    @@ -1541,57 +1591,15 @@

     
     
    -68
    -69
    -70
    -71
    -72
    -73
    -74
    -75
    -76
    -77
    -78
    -79
    -80
    -81
    -82
    -83
     84
     85
    -86
    -87
    -88
    -89
    -90
    -91
    +86

    @@ -1599,9 +1607,9 @@

    -

    +

    - .quotient_walk(number, limit: 10, segment: computed_base_segment(number)) ⇒ Array + .right_nodeObject @@ -1610,7 +1618,15 @@

    -

    Returns of FractionTree::Nodes leading quotient-wise to number.

    +
    + Note: +
    +

    defaults to Stern-Brocot right-most range, Infinity

    +
    +
    + + +

    Returns the right-most node of the range of the tree.

    @@ -1621,85 +1637,77 @@

    Examples:

    -
    FractionTree.quotient_walk(15/13r)
    -=> [(1/1), (2/1), (3/2), (4/3), (5/4), (6/5), (7/6), (8/7), (15/13)]
    +
    FractionTree.right_node => Infinity

    -

    Parameters:

    -
      + +

      Returns:

      +
      • - number - - (Numeric) + - — +
        -

        to walk toward

        +

        the right-most node of the range of the tree

      • -
      • - - limit - - - (Integer) - - - (defaults to: 10) - - - — -
        -

        the depth of the walk. Useful for irrational numbers

        +
      + +

    -
    # File 'lib/fraction_tree.rb', line 68
    -
    -def path_to(number, find_parents: false, segment: base_segment)
    -  return Node.new(number.numerator, number.denominator) if number.zero?
    -  number = number.kind_of?(Float) ? number.to_d : number
    -
    -  q = Node.new(number.numerator, number.denominator)
    -  l = segment.first
    -  h = segment.last
    -  not_found = true
    -  parents = []
    -  results = []
    -  while not_found
    -    m = (l + h)
    -    if m < q
    -      l = m
    -    elsif m > q
    -      h = m
    -    else
    -      parents << l << h
    -      not_found = false
    -    end
    -    results << m
    -  end
    -  find_parents == false ? results : parents
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 84
    +
    +def reset_nodes
    +  @@nodes = {}
     end
    + + + + +
    +
    +
    +
    +23
    +24
    +25
    +
    +
    # File 'lib/fraction_tree/fraction_tree.rb', line 23
    +
    +def right_node
    +  @right_node || Float::INFINITY
    +end
    +
    - - + +
    +

    -
  • - - segment - - - (Array) - - - (defaults to: computed_base_segment(number)) - - - — -
    -

    the tuple of [FractionTree::Node] defining the segment of the tree

    -
    - -
  • + .right_node=(rhs) ⇒ Object - -

    Returns:

    -
      -
    • - - - (Array) - - - - — -
      -

      of FractionTree::Nodes leading quotient-wise to number

      + + +

    +
    + +

    Set the right-most node of the range of the tree

    + + +
    - - +
    - +
    +

    Examples:

    + + +
    FractionTree.right_node = 2/1r
    + +
    +
    @@ -1707,47 +1715,15 @@

     
     
    -171
    -172
    -173
    -174
    -175
    -176
    -177
    -178
    -179
    -180
    -181
    -182
    -183
    -184
    -185
    -186
    -187
    -188
    -189
    +39 +40 +41

    @@ -1757,7 +1733,7 @@

    - .sequence(depth = 5, segment: base_segment) ⇒ Array + .sequence(depth: 5, left_node: default_left_node, right_node: default_right_node) ⇒ Array @@ -1777,7 +1753,7 @@

    Examples:

    -
    FractionTree.sequence(3)
    +      
    FractionTree.new.sequence(3)
       => [(0/1), (1/3), (1/2), (2/3), (1/1), (3/2), (2/1), (3/1), (1/0)]

    @@ -1804,18 +1780,36 @@

  • - segment + left_node - (Array) + (FractionTree::Node) + + + (defaults to: default_left_node) + + + — +
    +

    the left starting node

    +
    + +
  • + +
  • + + right_node + + + (FractionTree::Node) - (defaults to: base_segment) + (defaults to: default_right_node)
    -

    a tuple array of [FractionTree::Node] defining the segment of the tree to collect nodes.

    +

    the right starting node

  • @@ -1847,15 +1841,15 @@

     
     
    -56
    -57
    -58
    +201 +202 +203

    @@ -1865,7 +1859,7 @@

    - .tree(depth = DEFAULT_TREE_DEPTH) ⇒ Array + .tree(depth: 10, left_node: default_left_node, right_node: default_right_node) ⇒ Array @@ -1874,7 +1868,7 @@

    -

    Returns a multi-dimensional array with the elements of fraction tree, organized by level/row.

    +

    Returns a multi-dimensional array of fraction tree nodes.

    @@ -1885,7 +1879,7 @@

    Examples:

    -
    FractionTree.tree(4)
    +      
    FractionTree.tree(depth: 4)
     => [[(0/1), (1/0)],
         [(1/1)],
         [(1/2), (2/1)],
    @@ -1897,16 +1891,54 @@ 

  • - number + depth (Integer) + (defaults to: 10) + —

    the depth of the tree

    +
    + +
  • + +
  • + + left_node + + + (FractionTree::Node) + + + (defaults to: default_left_node) + + + — +
    +

    the left starting node

    +
    + +
  • + +
  • + + right_node + + + (FractionTree::Node) + + + (defaults to: default_right_node) + + + — +
    +

    the right starting node

  • @@ -1925,7 +1957,7 @@

    -

    a multi-dimensional array with the elements of fraction tree, organized by level/row

    +

    a multi-dimensional array of fraction tree nodes

    @@ -1938,40 +1970,40 @@

     
     
    -28
    -29
    -30
    -31
    -32
    -33
    -34
    -35
    -36
    -37
    -38
    -39
    -40
    -41
    -42
    -43
    -44
    -45
    -46
    +142 +143 +144 +145 +146 +147 +148 +149 +150 +151 +152 +153 +154 +155 +156 +157 +158 +159 +160

    -
    # File 'lib/fraction_tree.rb', line 171
    -
    -def quotient_walk(number, limit: 10, segment: computed_base_segment(number))
    -  iterating_quotients = ContinuedFraction.new(number, limit).quotients.drop(1)
    -  comparing_node = Node.new(number.numerator, number.denominator)
    -
    -  segment.tap do |arr|
    -    held_node = arr[-2]
    -    moving_node = arr[-1]
    -
    -    iterating_quotients.each do |q|
    -      (1..q).each do |i|
    -        arr << held_node + moving_node
    -        # We don't want to walk past the number when it's a rational number and we've reached it
    -        break if arr.include?(comparing_node)
    -        moving_node = arr[-1]
    -      end
    -      held_node = arr[-2]
    -    end
    -  end
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 39
    +
    +def right_node=(rhs)
    +  @right_node = rhs
     end
    -
    # File 'lib/fraction_tree.rb', line 56
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 201
     
    -def sequence(depth=5, segment: base_segment)
    -  [segment.first]+_sequence(depth, segment:)+[segment.last]
    +def sequence(depth: 5, left_node: default_left_node, right_node: default_right_node)
    +  [left_node]+_sequence(depth:, left_node:, right_node:)+[right_node]
     end
    -
    # File 'lib/fraction_tree.rb', line 28
    +      
    # File 'lib/fraction_tree/fraction_tree.rb', line 142
     
    -def tree(depth=DEFAULT_TREE_DEPTH)
    +def tree(depth: 10, left_node: default_left_node, right_node: default_right_node)
       Array.new(depth, 0).tap do |sbt|
         row = 0
    -    sbt[row] = base_segment
    +    sbt[row] = [left_node, right_node]
         i = 2
         while i <= depth do
           figure_from = sbt[0..row].flatten.sort
           new_frow = Array.new(2**(i-2), 0)
           idx = 0
    -      figure_from.each_cons(2) do |left,right|
    -        new_frow[idx] = Node.new(left.numerator+right.numerator, left.denominator+right.denominator)
    +      figure_from.each_cons(2) do |left, right|
    +        new_frow[idx] = left + right
             idx += 1
           end
           row += 1
    @@ -1990,9 +2022,9 @@ 

    diff --git a/docs/FractionTree/Node.html b/docs/FractionTree/Node.html index f8920b3..a33d570 100644 --- a/docs/FractionTree/Node.html +++ b/docs/FractionTree/Node.html @@ -6,7 +6,7 @@ Class: FractionTree::Node - — Documentation by YARD 0.9.34 + — Documentation by YARD 0.9.36 @@ -85,6 +85,11 @@ +
    +
    Extended by:
    +
    Forwardable
    +
    +
    @@ -99,12 +104,38 @@
    Defined in:
    -
    lib/fraction_tree.rb
    +
    lib/fraction_tree/node.rb
    + +

    + Constant Summary + collapse +

    + +
    + +
    IDENTITY_MATRIX = + +
    +
    Matrix.identity(2)
    + +
    LEFT_MATRIX = + +
    +
    Matrix[[1,1]
    + +
    RIGHT_MATRIX = + +
    +
    + +
    + + @@ -143,10 +174,12 @@

    Instance Attribute Summary collaps
  • - #numerator ⇒ Object + #number ⇒ Object + (also: #to_r) + @@ -163,7 +196,7 @@

    Instance Attribute Summary collaps
    -

    Returns the value of attribute numerator.

    +

    Returns the value of attribute number.

  • @@ -172,12 +205,10 @@

    Instance Attribute Summary collaps
  • - #weight ⇒ Object + #numerator ⇒ Object - (also: #to_r) - @@ -194,7 +225,7 @@

    Instance Attribute Summary collaps
    -

    Returns the value of attribute weight.

    +

    Returns the value of attribute numerator.

  • @@ -207,7 +238,7 @@

    Instance Attribute Summary collaps

    - Instance Method Summary + Class Method Summary collapse

    @@ -216,7 +247,7 @@

  • - #+(rhs) ⇒ Object + .decimal_power(logarithmand) ⇒ Integer @@ -230,7 +261,9 @@

    -
    +
    +

    The decimal power of the provided number.

    +

  • @@ -238,7 +271,7 @@

  • - #<=>(rhs) ⇒ Object + .decode(string) ⇒ FractionTree::Node @@ -252,7 +285,9 @@

    -
    +
    +

    The fraction decoded from the given string.

    +

  • @@ -260,7 +295,7 @@

  • - #==(rhs) ⇒ Object + .encode(number) ⇒ String @@ -274,7 +309,9 @@

    -
    +
    +

    The Stern-Brocot encoding of number.

    +

  • @@ -282,7 +319,7 @@

  • - #eql?(rhs) ⇒ Boolean + .plus_minus(num, diff) ⇒ Array @@ -297,16 +334,25 @@

    -

    Needed for intersection operations to work.

    +

    Pair of numbers less and greater than the provided number by provided difference.

  • + + +

    + Instance Method Summary + collapse +

    + + - - +
  • + + + #==(rhs) ⇒ Object + -
    -

    Constructor Details

    + + + -
    -

    - #initialize(n, d) ⇒ Node - - -

    -
    - -

    Returns a new instance of Node.

    - - -
    -
    -
    -
    - - - - -
    -
    -
    -
    -266
    -267
    -268
    -269
    -270
    -
    -
    # File 'lib/fraction_tree.rb', line 266
    -
    -def initialize(n,d)
    -  @numerator = n
    -  @denominator = d
    -  @weight = (d == 0 ? Float::INFINITY : Rational(@numerator, @denominator))
    -end
    -
    -
    -
    +
    + +
  • -
    -

    Instance Attribute Details

    - - -
    -

    +
  • + + + #child_with(num) ⇒ FractionTree::Node + + + + + + - #denominatorObject (readonly) - - -
  • -
    - -

    Returns the value of attribute denominator.

    - - -
    -
    -
    -
    - - - - -
    -
    -
    +  
    +    
    +

    Child of self and given number.

    +
    + + -264 -265 -266
    -
    -
    # File 'lib/fraction_tree.rb', line 264
    +      
    +        
  • + + + #common_ancestors_with(num) ⇒ Array + -def denominator - @denominator -end
  • -
    -
    - - -
    -

    + + + - #numeratorObject (readonly) - - -

    -
    - -

    Returns the value of attribute numerator.

    - - -
    -
    -
    -
    - - - - -
    -
    -
    +  
    +    
    +

    The ancestors shared by self and the given number.

    +
    + + -264 -265 -266
    -
    -
    # File 'lib/fraction_tree.rb', line 264
    +      
    +        
  • + + + #descendancy_from(depth: 5) ⇒ Array + -def numerator - @numerator -end
  • -
    -
    - - -
    -

    + + - #weightObject (readonly) - - Also known as: - to_r - - -

    -
    - -

    Returns the value of attribute weight.

    - - -
    -
    -
    -
    - - - - -
    -
    -
    -
    -264
    -265
    -266
    -
    -
    # File 'lib/fraction_tree.rb', line 264
    +  
    +    
    +

    Of fraction tree nodes, descending from parents of number.

    +
    + + -def weight - @weight -end
    -
    -
    + +
  • + + + #encoding ⇒ String -
  • - - -
    -

    Instance Method Details

    -
    -

    + + + + - #+(rhs) ⇒ Object - - -

    - - -
    -
    -
     
    -302
    -303
    -304
    -
    -
    # File 'lib/fraction_tree.rb', line 302
    +  
    +    
    +

    Encoding of self.

    +
    + + + + +
  • + + + #eql?(rhs) ⇒ Boolean + + + + + + + + + + + + + +
    +

    Needed for intersection operations to work.

    +
    + +
  • + + +
  • + + + #hash ⇒ Object + + + + + + + + + + + + + +
    + +
  • + + +
  • + + + #initialize(num) ⇒ Node + + + + + + + constructor + + + + + + + + +
    +

    A new instance of Node.

    +
    + +
  • + + +
  • + + + #inspect ⇒ Object + + + + (also: #to_s) + + + + + + + + + + + +
    + +
  • + + +
  • + + + #neighbors(r = 10**(self.class.decimal_power(number.numerator)+2)) ⇒ Array + + + + + + + + + + + + + +
    +

    Of [FractionTree::Node], sequence of Farey neighbors to self.

    +
    + +
  • + + +
  • + + + #parents ⇒ Array + + + + + + + + + + + + + +
    +

    A pair of fraction tree nodes leading to the given number.

    +
    + +
  • + + +
  • + + + #path ⇒ Array + + + + + + + + + + + + + +
    +

    Set of fraction tree nodes leading to the given number.

    +
    + +
  • + + + + + + + +
    +

    Constructor Details

    + +
    +

    + + #initialize(num) ⇒ Node + + + + + +

    +
    + +

    Returns a new instance of Node.

    + + +
    +
    +
    + + +
    + + + + +
    +
    +
    +
    +16
    +17
    +18
    +19
    +
    +
    # File 'lib/fraction_tree/node.rb', line 16
    +
    +def initialize(num)
    +  (@numerator, @denominator) = fraction_pair(num)
    +  @number = num
    +end
    +
    +
    + +
    + +
    +

    Instance Attribute Details

    + + + +
    +

    + + #denominatorObject (readonly) + + + + + +

    +
    + +

    Returns the value of attribute denominator.

    + + +
    +
    +
    + + +
    + + + + +
    +
    +
    +
    +10
    +11
    +12
    +
    +
    # File 'lib/fraction_tree/node.rb', line 10
    +
    +def denominator
    +  @denominator
    +end
    +
    +
    + + + +
    +

    + + #numberObject (readonly) + + + + Also known as: + to_r + + + + +

    +
    + +

    Returns the value of attribute number.

    + + +
    +
    +
    + + +
    + + + + +
    +
    +
    +
    +10
    +11
    +12
    +
    +
    # File 'lib/fraction_tree/node.rb', line 10
    +
    +def number
    +  @number
    +end
    +
    +
    + + + +
    +

    + + #numeratorObject (readonly) + + + + + +

    +
    + +

    Returns the value of attribute numerator.

    + + +
    +
    +
    + + +
    + + + + +
    +
    +
    +
    +10
    +11
    +12
    +
    +
    # File 'lib/fraction_tree/node.rb', line 10
    +
    +def numerator
    +  @numerator
    +end
    +
    +
    + +
    + + +
    +

    Class Method Details

    + + +
    +

    + + .decimal_power(logarithmand) ⇒ Integer + + + + + +

    +
    + +

    Returns the decimal power of the provided number.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree::Node.decimal_power(1000) => 3
    + +
    +

    Parameters:

    +
      + +
    • + + logarithmand + + + + + + + — +
      +

      the number from which the log base 10 is obtained

      +
      + +
    • + +
    + +

    Returns:

    +
      + +
    • + + + (Integer) + + + + — +
      +

      the decimal power of the provided number

      +
      + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +84
    +85
    +86
    +
    +
    # File 'lib/fraction_tree/node.rb', line 84
    +
    +def decimal_power(logarithmand)
    +  Math.log10(logarithmand.abs).floor
    +end
    +
    +
    + +
    +

    + + .decode(string) ⇒ FractionTree::Node + + + + + +

    +
    + +

    Returns the fraction decoded from the given string.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree::Node.decode("RLL") => (4/3)
    + +
    + +

    Returns:

    +
      + +
    • + + + (FractionTree::Node) + + + + — +
      +

      the fraction decoded from the given string

      +
      + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +28
    +29
    +30
    +31
    +32
    +33
    +34
    +35
    +36
    +37
    +38
    +39
    +40
    +
    +
    # File 'lib/fraction_tree/node.rb', line 28
    +
    +def decode(string)
    +  result = IDENTITY_MATRIX
    +
    +  string.split("").each do |direction|
    +    case direction
    +    when "L", "0", "l"
    +      result = result * LEFT_MATRIX
    +    when "R", "1", "r"
    +      result = result * RIGHT_MATRIX
    +    end
    +  end
    +  FractionTree.node(Rational(result.row(1).sum, result.row(0).sum))
    +end
    +
    +
    + +
    +

    + + .encode(number) ⇒ String + + + + + +

    +
    + +

    Returns the Stern-Brocot encoding of number.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree::Node.encode(4/3r) => "RLL"
    + +
    + +

    Returns:

    +
      + +
    • + + + (String) + + + + — +
      +

      the Stern-Brocot encoding of number

      +
      + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +46
    +47
    +48
    +49
    +50
    +51
    +52
    +53
    +54
    +55
    +56
    +57
    +58
    +59
    +60
    +61
    +62
    +63
    +64
    +65
    +
    +
    # File 'lib/fraction_tree/node.rb', line 46
    +
    +def encode(number)
    +  return nil if (number.infinite? || number.zero?)
    +
    +  m = number.numerator
    +  n = number.denominator
    +
    +  return "I" if m == n
    +
    +  "".tap do |string|
    +    while m != n
    +      if m < n
    +        string << "L"
    +        n = n - m
    +      else
    +        string << "R"
    +        m = m - n
    +      end
    +    end
    +  end
    +end
    +
    +
    + +
    +

    + + .plus_minus(num, diff) ⇒ Array + + + + + +

    +
    + +

    Returns pair of numbers less and greater than the provided number by provided difference.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree::Node.plus_minus(3, 2) => [1, 5]
    + +
    +

    Parameters:

    +
      + +
    • + + num + + + + + + + — +
      +

      the base diff the number subtracted and added to base

      +
      + +
    • + +
    + +

    Returns:

    +
      + +
    • + + + (Array) + + + + — +
      +

      pair of numbers less and greater than the provided number by provided difference

      +
      + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +74
    +75
    +76
    +
    +
    # File 'lib/fraction_tree/node.rb', line 74
    +
    +def plus_minus(num, diff)
    +  [num - diff, num + diff]
    +end
    +
    +
    + +
    + +
    +

    Instance Method Details

    + + +
    +

    + + #+(rhs) ⇒ FractionTree::Node + + + + + +

    +
    + +

    Returns sum of self and another node.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.node(5/4r) + FractionTree.node(3/2r)
    +=> (4/3)
    + +
    + +

    Returns:

    + + +
    + + + + +
    +
    +
    +
    +195
    +196
    +197
    +
    +
    # File 'lib/fraction_tree/node.rb', line 195
    +
    +def +(rhs)
    +  tree.node(Rational(self.numerator+rhs.numerator, self.denominator+rhs.denominator))
    +end
    +
    +
    + +
    +

    + + #-(rhs) ⇒ FractionTree::Node + + + + + +

    +
    + +

    Returns difference of self and another node.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.node(5/4r) - FractionTree.node(3/2r)
    +=> (1/1)
    + +
    + +

    Returns:

    +
      + +
    • + + + (FractionTree::Node) + + + + — +
      +

      difference of self and another node

      +
      + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +204
    +205
    +206
    +
    +
    # File 'lib/fraction_tree/node.rb', line 204
    +
    +def -(rhs)
    +  tree.node(Rational((self.numerator-rhs.numerator).abs, (self.denominator-rhs.denominator).abs))
    +end
    +
    +
    + +
    +

    + + #<=>(rhs) ⇒ Object + + + + + +

    + + + + +
    +
    +
    +
    +213
    +214
    +215
    +
    +
    # File 'lib/fraction_tree/node.rb', line 213
    +
    +def <=>(rhs)
    +  self.number <=> rhs.number
    +end
    +
    +
    + +
    +

    + + #==(rhs) ⇒ Object + + + + + +

    + + + + +
    +
    +
    +
    +217
    +218
    +219
    +
    +
    # File 'lib/fraction_tree/node.rb', line 217
    +
    +def ==(rhs)
    +  self.number == rhs.number
    +end
    +
    +
    + +
    +

    + + #child_with(num) ⇒ FractionTree::Node + + + + + +

    +
    + +
    + Note: +
    +

    return nil if bc - ad |= 1, for a/b, c/d

    +
    +
    + + +

    Returns child of self and given number.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.node(5/4r).child_with(4/3r)
    +=> (9/7)
    + +
    + +

    Returns:

    + + +
    + + + + +
    +
    +
    +
    +178
    +179
    +180
    +
    +
    # File 'lib/fraction_tree/node.rb', line 178
    +
    +def child_with(num)
    +  tree.child_of(number, num)
    +end
    +
    +
    + +
    +

    + + #common_ancestors_with(num) ⇒ Array + + + + + +

    +
    + +

    Returns the ancestors shared by self and the given number.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.node(4/3r).common_ancestors_with(7/4r)
    +=> [(0/1), (1/0), (1/1), (2/1), (3/2)]
    + +
    +

    Parameters:

    +
      + +
    • + + num + + + (Numeric) + + + + — +
      +

      other number sharing descendants with self

      +
      + +
    • + +
    + +

    Returns:

    +
      + +
    • + + + (Array) + + + + — +
      +

      the ancestors shared by self and the given number

      +
      + +
    • + +
    + +
    + + + @@ -603,29 +1630,91 @@

    -

    +

    - #<=>(rhs) ⇒ Object + #descendancy_from(depth: 5) ⇒ Array -

    +
     
    -def +(rhs)
    -  self.class.new(self.numerator+rhs.numerator, self.denominator+rhs.denominator)
    +
    +156
    +157
    +158
    +
    +
    # File 'lib/fraction_tree/node.rb', line 156
    +
    +def common_ancestors_with(num)
    +  path & tree.node(num).path
     end
    +
    +
    + +

    Returns of fraction tree nodes, descending from parents of number.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.node(5/4r).descendancy_from(depth: 3)
    +=> [(1/1), (7/6), (6/5), (11/9), (5/4), (14/11), (9/7), (13/10), (4/3)]
    + +
    +

    Parameters:

    +
      + +
    • + + depth + + + (Integer) + + + (defaults to: 5) + + + — +
      +

      how many nodes to collect

      +
      + +
    • + +
    + +

    Returns:

    +
      + +
    • + + + (Array) + + + + — +
      +

      of fraction tree nodes, descending from parents of number

      +
      + +
    • + +
    + +
    @@ -633,29 +1722,66 @@

    -

    +

    - #==(rhs) ⇒ Object + #encodingString -

     
     
    -279
    -280
    -281
    +167 +168 +169 +170
    -
    # File 'lib/fraction_tree.rb', line 279
    +      
    # File 'lib/fraction_tree/node.rb', line 167
     
    -def <=>(rhs)
    -  self.weight <=> rhs.weight
    +def descendancy_from(depth: 5)
    +  (parent1, parent2) = parents
    +  tree.descendants_of(parent1.number, parent2.number, depth:)
     end
    +
    +
    + +

    Returns encoding of self.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.node(5/4r).encoding => "RLLL"
    + +
    + +

    Returns:

    +
      + +
    • + + + (String) + + + + — +
      +

      encoding of self

      +
      + +
    • + +
    + +
    @@ -701,15 +1827,15 @@

     
     
    -292
    -293
    -294
    +226 +227 +228

    @@ -731,14 +1857,14 @@

     
     
    -296
    -297
    -298
    -299
    -300
    +230 +231 +232 +233 +234

     
     
    -283
    -284
    -285
    +186 +187 +188
    -
    # File 'lib/fraction_tree.rb', line 283
    +      
    # File 'lib/fraction_tree/node.rb', line 186
     
    -def ==(rhs)
    -  self.weight == rhs.weight
    +def encoding
    +  @encoding ||= self.class.encode(number)
     end
    -
    # File 'lib/fraction_tree.rb', line 292
    +      
    # File 'lib/fraction_tree/node.rb', line 226
     
     def eql?(rhs)
    -   rhs.instance_of?(self.class) && weight == rhs.weight
    +   rhs.instance_of?(self.class) && number == rhs.number
     end
    -
    # File 'lib/fraction_tree.rb', line 296
    +      
    # File 'lib/fraction_tree/node.rb', line 230
     
     def hash
        p, q = 17, 37
    @@ -769,12 +1895,12 @@ 

     
     
    -274
    -275
    -276
    +208 +209 +210

    -
    # File 'lib/fraction_tree.rb', line 274
    +      
    # File 'lib/fraction_tree/node.rb', line 208
     
     def inspect
       "(#{numerator}/#{denominator})"
    @@ -782,6 +1908,296 @@ 

    +
    + +
    +

    + + #neighbors(r = 10**(self.class.decimal_power(number.numerator)+2)) ⇒ Array + + + + + +

    +
    + +

    Returns of [FractionTree::Node], sequence of Farey neighbors to self. A Farey neighbor is a number c/d, who’s relationship to a/b is such that ad − bc = 1, when c/d < a/b and bc − ad = 1 when c/d > a/b.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.node(3/2r).neighbors(10)
    +=> [(1/1), (2/1), (4/3), (5/3), (7/5), (8/5), (10/7), (11/7), (13/9), (14/9)]
    + +
    +

    Parameters:

    +
      + +
    • + + r + + + + + + (defaults to: 10**(self.class.decimal_power(number.numerator)+2)) + + + — +
      +

      range of harmonic series to search

      +
      + +
    • + +
    + +

    Returns:

    +
      + +
    • + + + (Array) + + + + — +
      +

      of [FractionTree::Node], sequence of Farey neighbors to self. A Farey neighbor is a number c/d, who’s relationship to a/b is such that ad − bc = 1, when c/d < a/b and bc − ad = 1 when c/d > a/b.

      +
      + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +136
    +137
    +138
    +139
    +140
    +141
    +142
    +143
    +144
    +145
    +146
    +147
    +
    +
    # File 'lib/fraction_tree/node.rb', line 136
    +
    +def neighbors(r = 10**(self.class.decimal_power(number.numerator)+2))
    +  ratio = number.to_r
    +  denominator = ratio.denominator
    +
    +  [].tap do |collection|
    +    (1..r-1).each do |i|
    +      lower, upper = self.class.plus_minus(ratio, Rational(1,i*denominator))
    +      collection << tree.node(lower) if tree.neighbors?(ratio, lower)
    +      collection << tree.node(upper) if tree.neighbors?(ratio, upper)
    +    end
    +  end
    +end
    +
    +
    + +
    +

    + + #parentsArray + + + + + +

    +
    + +

    Returns a pair of fraction tree nodes leading to the given number.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.node(15/13r).parents => [(8/7), (7/6)]
    +FractionTree.node(Math::PI).parents => [(1181999955934188/376242271442657), (1959592697655605/623757728557343)]
    + +
    + +

    Returns:

    +
      + +
    • + + + (Array) + + + + — +
      +

      a pair of fraction tree nodes leading to the given number.

      +
      + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +125
    +126
    +127
    +128
    +
    +
    # File 'lib/fraction_tree/node.rb', line 125
    +
    +def parents
    +  tmp = path
    +  [tmp[-2], tmp[-2..-1].inject(&:-)].sort
    +end
    +
    +
    + +
    +

    + + #pathArray + + + + + +

    +
    + +

    Returns set of fraction tree nodes leading to the given number.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    FractionTree.node(7/4r).path
    +=> [(0/1), (1/0), (1/1), (2/1), (3/2), (5/3), (7/4)]
    + +
    + +

    Returns:

    +
      + +
    • + + + (Array) + + + + — +
      +

      set of fraction tree nodes leading to the given number

      +
      + +
    • + +
    + +
    + + + + +
    +
    +
    +
    +94
    +95
    +96
    +97
    +98
    +99
    +100
    +101
    +102
    +103
    +104
    +105
    +106
    +107
    +108
    +109
    +110
    +111
    +112
    +113
    +114
    +115
    +116
    +117
    +118
    +
    +
    # File 'lib/fraction_tree/node.rb', line 94
    +
    +def path
    +  return nil if infinite? || zero?
    +
    +  ln = tree.node(FractionTree.left_node)
    +  rn = tree.node(FractionTree.right_node)
    +  mn = ln + rn
    +  return [ln, rn, mn] if mn == tree.node(number)
    +
    +  result = IDENTITY_MATRIX
    +  m = numerator
    +  n = denominator
    +  [].tap do |p|
    +    p << ln << rn << mn
    +    while m != n
    +      if m < n
    +        result = result * LEFT_MATRIX
    +        n = n - m
    +      else
    +        result = result * RIGHT_MATRIX
    +        m = m - n
    +      end
    +      p << tree.node(Rational(result.row(1).sum,result.row(0).sum))
    +    end
    +  end
    +end
    +
    @@ -789,9 +2205,9 @@

    diff --git a/docs/KeyboardTree.html b/docs/KeyboardTree.html deleted file mode 100644 index 6e1b803..0000000 --- a/docs/KeyboardTree.html +++ /dev/null @@ -1,153 +0,0 @@ - - - - - - - Class: KeyboardTree - - — Documentation by YARD 0.9.34 - - - - - - - - - - - - - - - - - - - -
    - - -

    Class: KeyboardTree - - - -

    -
    - -
    -
    Inherits:
    -
    - FareyTree - - - show all - -
    -
    - - - - - - - - - - - -
    -
    Defined in:
    -
    lib/fraction_tree.rb
    -
    - -
    - - - - -

    Constant Summary

    - -

    Constants inherited - from FractionTree

    -

    FractionTree::DEFAULT_TREE_DEPTH

    - - - - - - - - - - - - -

    Method Summary

    - -

    Methods inherited from FareyTree

    -

    base_segment

    - - - - - - - - - -

    Methods inherited from FractionTree

    -

    base_segment, child_of, common_ancestors_between, descendancy_from, descendants_of, farey_neighbors, farey_neighbors?, numeric_sequence, parents_of, path_to, quotient_walk, sequence, tree

    - - -
    - - - -
    - - \ No newline at end of file diff --git a/docs/Log2Tree.html b/docs/Log2Tree.html deleted file mode 100644 index 2564577..0000000 --- a/docs/Log2Tree.html +++ /dev/null @@ -1,153 +0,0 @@ - - - - - - - Class: Log2Tree - - — Documentation by YARD 0.9.34 - - - - - - - - - - - - - - - - - - - -
    - - -

    Class: Log2Tree - - - -

    -
    - -
    -
    Inherits:
    -
    - FareyTree - - - show all - -
    -
    - - - - - - - - - - - -
    -
    Defined in:
    -
    lib/fraction_tree.rb
    -
    - -
    - - - - -

    Constant Summary

    - -

    Constants inherited - from FractionTree

    -

    FractionTree::DEFAULT_TREE_DEPTH

    - - - - - - - - - - - - -

    Method Summary

    - -

    Methods inherited from FareyTree

    -

    base_segment

    - - - - - - - - - -

    Methods inherited from FractionTree

    -

    base_segment, child_of, common_ancestors_between, descendancy_from, descendants_of, farey_neighbors, farey_neighbors?, numeric_sequence, parents_of, path_to, quotient_walk, sequence, tree

    - - -
    - - - -
    - - \ No newline at end of file diff --git a/docs/Numeric.html b/docs/Numeric.html new file mode 100644 index 0000000..ea869e7 --- /dev/null +++ b/docs/Numeric.html @@ -0,0 +1,229 @@ + + + + + + + Class: Numeric + + — Documentation by YARD 0.9.36 + + + + + + + + + + + + + + + + + + + +
    + + +

    Class: Numeric + + + +

    +
    + +
    +
    Inherits:
    +
    + Object + +
      +
    • Object
    • + + + +
    + show all + +
    +
    + + + + + + + + + + + +
    +
    Defined in:
    +
    lib/fraction_tree/extensions.rb
    +
    + +
    + + + + + + + + + +

    + Instance Method Summary + collapse +

    + + + + + + +
    +

    Instance Method Details

    + + +
    +

    + + #to_nodeFractionTree::Node + + + + + +

    +
    + +

    Returns string Stern-Brocot decoded.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    Float::INFINITY.to_node => (1/0)
    + +
    + +

    Returns:

    + + +
    + + + + +
    +
    +
    +
    +26
    +27
    +28
    +
    +
    # File 'lib/fraction_tree/extensions.rb', line 26
    +
    +def to_node
    +  FractionTree.node(self)
    +end
    +
    +
    + +
    + +
    + + + +
    + + \ No newline at end of file diff --git a/docs/OctaveReducedTree.html b/docs/OctaveReducedTree.html deleted file mode 100644 index 27bf738..0000000 --- a/docs/OctaveReducedTree.html +++ /dev/null @@ -1,209 +0,0 @@ - - - - - - - Class: OctaveReducedTree - - — Documentation by YARD 0.9.34 - - - - - - - - - - - - - - - - - - - -
    - - -

    Class: OctaveReducedTree - - - -

    -
    - -
    -
    Inherits:
    -
    - FractionTree - - - show all - -
    -
    - - - - - - - - - - - -
    -
    Defined in:
    -
    lib/fraction_tree.rb
    -
    - -
    - - - - -

    Constant Summary

    - -

    Constants inherited - from FractionTree

    -

    FractionTree::DEFAULT_TREE_DEPTH

    - - - - - - -

    - Class Method Summary - collapse -

    - - - - - - - - - - - - - -

    Methods inherited from FractionTree

    -

    child_of, common_ancestors_between, descendancy_from, descendants_of, farey_neighbors, farey_neighbors?, numeric_sequence, parents_of, path_to, quotient_walk, sequence, tree

    - - -
    -

    Class Method Details

    - - -
    -

    - - .base_segmentObject - - - - - -

    - - - - -
    -
    -
    -
    -312
    -313
    -314
    -
    -
    # File 'lib/fraction_tree.rb', line 312
    -
    -def self.base_segment
    -  [Node.new(1,1), Node.new(2,1)]
    -end
    -
    -
    - -
    - -
    - - - -
    - - \ No newline at end of file diff --git a/docs/ScaleStepTree.html b/docs/ScaleStepTree.html deleted file mode 100644 index da2601c..0000000 --- a/docs/ScaleStepTree.html +++ /dev/null @@ -1,153 +0,0 @@ - - - - - - - Class: ScaleStepTree - - — Documentation by YARD 0.9.34 - - - - - - - - - - - - - - - - - - - -
    - - -

    Class: ScaleStepTree - - - -

    -
    - -
    -
    Inherits:
    -
    - FareyTree - - - show all - -
    -
    - - - - - - - - - - - -
    -
    Defined in:
    -
    lib/fraction_tree.rb
    -
    - -
    - - - - -

    Constant Summary

    - -

    Constants inherited - from FractionTree

    -

    FractionTree::DEFAULT_TREE_DEPTH

    - - - - - - - - - - - - -

    Method Summary

    - -

    Methods inherited from FareyTree

    -

    base_segment

    - - - - - - - - - -

    Methods inherited from FractionTree

    -

    base_segment, child_of, common_ancestors_between, descendancy_from, descendants_of, farey_neighbors, farey_neighbors?, numeric_sequence, parents_of, path_to, quotient_walk, sequence, tree

    - - -
    - - - -
    - - \ No newline at end of file diff --git a/docs/ScaleTree.html b/docs/ScaleTree.html deleted file mode 100644 index a4282a4..0000000 --- a/docs/ScaleTree.html +++ /dev/null @@ -1,140 +0,0 @@ - - - - - - - Class: ScaleTree - - — Documentation by YARD 0.9.34 - - - - - - - - - - - - - - - - - - - -
    - - -

    Class: ScaleTree - - - -

    -
    - -
    -
    Inherits:
    -
    - FractionTree - - - show all - -
    -
    - - - - - - - - - - - -
    -
    Defined in:
    -
    lib/fraction_tree.rb
    -
    - -
    - - - - -

    Constant Summary

    - -

    Constants inherited - from FractionTree

    -

    FractionTree::DEFAULT_TREE_DEPTH

    - - - - - - - - - - - - -

    Method Summary

    - -

    Methods inherited from FractionTree

    -

    base_segment, child_of, common_ancestors_between, descendancy_from, descendants_of, farey_neighbors, farey_neighbors?, numeric_sequence, parents_of, path_to, quotient_walk, sequence, tree

    - - -
    - - - -
    - - \ No newline at end of file diff --git a/docs/SternBrocotTree.html b/docs/SternBrocotTree.html deleted file mode 100644 index 43ba2ea..0000000 --- a/docs/SternBrocotTree.html +++ /dev/null @@ -1,140 +0,0 @@ - - - - - - - Class: SternBrocotTree - - — Documentation by YARD 0.9.34 - - - - - - - - - - - - - - - - - - - -
    - - -

    Class: SternBrocotTree - - - -

    -
    - -
    -
    Inherits:
    -
    - FractionTree - - - show all - -
    -
    - - - - - - - - - - - -
    -
    Defined in:
    -
    lib/fraction_tree.rb
    -
    - -
    - - - - -

    Constant Summary

    - -

    Constants inherited - from FractionTree

    -

    FractionTree::DEFAULT_TREE_DEPTH

    - - - - - - - - - - - - -

    Method Summary

    - -

    Methods inherited from FractionTree

    -

    base_segment, child_of, common_ancestors_between, descendancy_from, descendants_of, farey_neighbors, farey_neighbors?, numeric_sequence, parents_of, path_to, quotient_walk, sequence, tree

    - - -
    - - - -
    - - \ No newline at end of file diff --git a/docs/String.html b/docs/String.html new file mode 100644 index 0000000..adc0f54 --- /dev/null +++ b/docs/String.html @@ -0,0 +1,249 @@ + + + + + + + Class: String + + — Documentation by YARD 0.9.36 + + + + + + + + + + + + + + + + + + + +
    + + +

    Class: String + + + +

    +
    + +
    +
    Inherits:
    +
    + Object + +
      +
    • Object
    • + + + +
    + show all + +
    +
    + + + + + + + + + + + +
    +
    Defined in:
    +
    lib/fraction_tree/extensions.rb
    +
    + +
    + + + + + + + + + +

    + Instance Method Summary + collapse +

    + + + + + + +
    +

    Instance Method Details

    + + +
    +

    + + #to_nodeFractionTree::Node + + + + + +

    +
    + +

    Returns string Stern-Brocot decoded.

    + + +
    +
    +
    + +
    +

    Examples:

    + + +
    "1/0".to_node => (1/0)
    + +
    + +

    Returns:

    + + +
    + + + + +
    +
    +
    +
    +6
    +7
    +8
    +9
    +10
    +11
    +12
    +13
    +14
    +15
    +16
    +17
    +18
    +
    +
    # File 'lib/fraction_tree/extensions.rb', line 6
    +
    +def to_node
    +  if self.include?(".")
    +    number = self.to_d
    +    numerator, denominator = number.numerator, number.denominator
    +  elsif self.include?("/")
    +    (numerator, denominator) = self.split("/").map(&:to_i)
    +  else
    +    number = self.to_r
    +    numerator, denominator = number.numerator, number.denominator
    +  end
    +  number = denominator.zero? ? Float::INFINITY : Rational(numerator, denominator)
    +  FractionTree::Node.new(number)
    +end
    +
    +
    + +
    + +
    + + + +
    + + \ No newline at end of file diff --git a/docs/_index.html b/docs/_index.html index 16046c4..5f7fc49 100644 --- a/docs/_index.html +++ b/docs/_index.html @@ -4,7 +4,7 @@ - Documentation by YARD 0.9.34 + Documentation by YARD 0.9.36 @@ -52,7 +52,7 @@
    -

    Documentation by YARD 0.9.34

    +

    Documentation by YARD 0.9.36

    Alphabetic Index

    @@ -80,11 +80,6 @@

    Namespace Listing A-Z

  • F
  • - - - - - -
    • N
      • @@ -131,16 +100,8 @@

        Namespace Listing A-Z

        -
      -
    - - -
    diff --git a/docs/class_list.html b/docs/class_list.html index d11fc96..bc57e5b 100644 --- a/docs/class_list.html +++ b/docs/class_list.html @@ -43,7 +43,7 @@

    Class List

    diff --git a/docs/file.README.html b/docs/file.README.html index 75ade22..b1a3fcd 100644 --- a/docs/file.README.html +++ b/docs/file.README.html @@ -6,7 +6,7 @@ File: README - — Documentation by YARD 0.9.34 + — Documentation by YARD 0.9.36 @@ -64,6 +64,21 @@

    FractionTree

    A collection of Stern-Brocot based models and methods.

    +

    The Stern-Brocot algorithm describes a way of constructing sets of non-negative fractions arranged in a binary tree.

    + +

    Construction of a SB tree starts by using the fractions 0/1 and 1/0, where 1/0 denotes infinity. Subsequent fractions are derived by the algorithm, (m + m′)/(n + n′), where m/n is the left adjacent fraction and m′/n′ is the right adjacent fraction, and m/n < m′/n′. This sum is called the mediant.

    + +

    Given m/n = 0/1 and m′/n′ = 1/0, the first mediant sum, is:

    + +

    0/1 + 1/0 => (0 + 1)/(1 + 0) = 1/1

    + +

    Fractions constructed in this way, have the following properties:

    +
    1. +

      m/n < (m + m′)/(n + n′) < m′/n′

      +
    2. +

      m’n - mn’ = 1

      +
    +

    Installing

    gem install fraction-tree
    @@ -79,20 +94,26 @@ 

    License

    Acknowledgments

    diff --git a/docs/frames.html b/docs/frames.html index 4f918f5..53734c2 100644 --- a/docs/frames.html +++ b/docs/frames.html @@ -2,13 +2,18 @@ - Documentation by YARD 0.9.34 + Documentation by YARD 0.9.36