fraction-tree 0.1.2 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/fraction_tree.rb +30 -15
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d13b1fb1ddd328714a431288ee0ec04205fb9001ce4a2d1b339e62bef0ed8e4c
|
4
|
+
data.tar.gz: 55260283f52cfa95dd50ecb02f83baaa436752d85b6fd53b3505d6f752ebb600
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df04fd1122e08f92ff5bb611f82d9ac582ac26c30e02542f02b9030b2ee248a5ba4f9e9080239fed64587bb55f74db80ec6dcb726e5550d881e61a6f54f616ce
|
7
|
+
data.tar.gz: db61b0ede817e7b8c1bac18ec4ab07627c20efaef739d01a8fb06bc49db80b7c3cd9243d79a5cdc873085cc0e8482b07eb528ad7ca1c2fb3c0dcceb6761bb402
|
data/lib/fraction_tree.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require "continued_fractions"
|
2
2
|
|
3
|
+
# @author Jose Hales-Garcia
|
4
|
+
#
|
3
5
|
class FractionTree
|
4
6
|
DEFAULT_TREE_DEPTH = 20
|
5
7
|
|
@@ -47,8 +49,8 @@ class FractionTree
|
|
47
49
|
# FractionTree.sequence(3)
|
48
50
|
# => [(0/1), (1/3), (1/2), (2/3), (1/1), (3/2), (2/1), (3/1), (1/0)]
|
49
51
|
#
|
50
|
-
# @param depth the number of iterations of the algorithm to run. The number of nodes returned will be greater
|
51
|
-
# @param segment a tuple array defining the segment of the tree to collect nodes.
|
52
|
+
# @param depth [Integer] the number of iterations of the algorithm to run. The number of nodes returned will be greater
|
53
|
+
# @param segment [Array] a tuple array of [FractionTree::Node] defining the segment of the tree to collect nodes.
|
52
54
|
#
|
53
55
|
def sequence(depth=5, segment: base_segment)
|
54
56
|
[segment.first]+_sequence(depth, segment:)+[segment.last]
|
@@ -58,7 +60,9 @@ class FractionTree
|
|
58
60
|
# @example
|
59
61
|
# FractionTree.path_to(7/4r) => [(1/1), (2/1), (3/2), (5/3), (7/4)]
|
60
62
|
#
|
61
|
-
# @param number the target the fraction path leads to
|
63
|
+
# @param number [Rational] the target the fraction path leads to
|
64
|
+
# @param find_parents [Boolean] list all ancestors or only immediate parents
|
65
|
+
# @param segment [Array] a tuple of [FractionTree::Node], defining the segment's starting left and right boundaries
|
62
66
|
#
|
63
67
|
def path_to(number, find_parents: false, segment: base_segment)
|
64
68
|
return Node.new(number.numerator, number.denominator) if number.zero?
|
@@ -89,7 +93,7 @@ class FractionTree
|
|
89
93
|
# FractionTree.parents_of(15/13r) => [(8/7), (7/6)]
|
90
94
|
# FractionTree.parents_of(Math::PI) => [(447288330638589/142376297616907), (436991388364966/139098679093749)]
|
91
95
|
#
|
92
|
-
# @param
|
96
|
+
# @param number [Rational] the child number whose parents are being sought
|
93
97
|
#
|
94
98
|
def parents_of(number)
|
95
99
|
path_to(number, find_parents: true)
|
@@ -100,8 +104,8 @@ class FractionTree
|
|
100
104
|
# FractionTree.common_ancestors_between(4/3r, 7/4r)
|
101
105
|
# => [(1/1), (2/1), (3/2)]
|
102
106
|
#
|
103
|
-
# @param number1 one of two descendants
|
104
|
-
# @param number2 two of two descendants
|
107
|
+
# @param number1 [Rational] one of two descendants
|
108
|
+
# @param number2 [Rational] two of two descendants
|
105
109
|
#
|
106
110
|
def common_ancestors_between(number1, number2)
|
107
111
|
path_to(number1) & path_to(number2)
|
@@ -112,8 +116,8 @@ class FractionTree
|
|
112
116
|
# FractionTree.descendancy_from(5/4r, 3)
|
113
117
|
# => [(1/1), (7/6), (6/5), (11/9), (5/4), (14/11), (9/7), (13/10), (4/3)]
|
114
118
|
#
|
115
|
-
# @param number around which descendancy is focused
|
116
|
-
# @param depth how many nodes to collect
|
119
|
+
# @param number [Rational] around which descendancy is focused
|
120
|
+
# @param depth [Integer] how many nodes to collect
|
117
121
|
#
|
118
122
|
def descendancy_from(number, depth=5)
|
119
123
|
parent1, parent2 = parents_of(number)
|
@@ -126,8 +130,9 @@ class FractionTree
|
|
126
130
|
# FractionTree.child_of(1/1r, 4/3r) => (5/4)
|
127
131
|
# FractionTree.child_of(7/4r, 4/3r) => nil
|
128
132
|
#
|
129
|
-
# @param number1 one of two parents
|
130
|
-
# @param number2 two of two parents
|
133
|
+
# @param number1 [Rational] one of two parents
|
134
|
+
# @param number2 [Rational] two of two parents
|
135
|
+
# @param strict_neighbors [Boolean] whether to apply the strict Farey tree neighbor requirement
|
131
136
|
#
|
132
137
|
def child_of(number1, number2, strict_neighbors: true)
|
133
138
|
return nil unless farey_neighbors?(number1, number2) || !strict_neighbors
|
@@ -140,8 +145,10 @@ class FractionTree
|
|
140
145
|
# FractionTree.descendants_of(1/1r, 4/3r, 3)
|
141
146
|
# => [(1/1), (7/6), (6/5), (11/9), (5/4), (14/11), (9/7), (13/10), (4/3)]
|
142
147
|
#
|
143
|
-
# @param parent1 one of two parents
|
144
|
-
# @param parent2 two of two parents
|
148
|
+
# @param parent1 [Rational] one of two parents
|
149
|
+
# @param parent2 [Rational] two of two parents
|
150
|
+
# @param depth [Integer] the depth to collect
|
151
|
+
# @param strict_neighbors [Boolean] whether to apply the strict Farey tree neighbor requirement
|
145
152
|
#
|
146
153
|
def descendants_of(parent1, parent2, depth=5, strict_neighbors: true)
|
147
154
|
return [] unless farey_neighbors?(parent1, parent2) || !strict_neighbors
|
@@ -154,8 +161,9 @@ class FractionTree
|
|
154
161
|
# FractionTree.quotient_walk(15/13r)
|
155
162
|
# => [(1/1), (2/1), (3/2), (4/3), (5/4), (6/5), (7/6), (8/7), (15/13)]
|
156
163
|
#
|
157
|
-
# @param number to walk toward
|
158
|
-
# @param limit the depth of the walk. Useful for irrational numbers
|
164
|
+
# @param number [Numeric] to walk toward
|
165
|
+
# @param limit [Integer] the depth of the walk. Useful for irrational numbers
|
166
|
+
# @param segment [Array] the tuple of [FractionTree::Node] defining the segment of the tree
|
159
167
|
#
|
160
168
|
def quotient_walk(number, limit: 10, segment: computed_base_segment(number))
|
161
169
|
iterating_quotients = ContinuedFraction.new(number, limit).quotients.drop(1)
|
@@ -212,10 +220,17 @@ class FractionTree
|
|
212
220
|
end
|
213
221
|
end
|
214
222
|
|
223
|
+
# @attr_reader numerator [Integer]
|
224
|
+
# The numerator of the node
|
225
|
+
# @attr_reader denominator [Integer]
|
226
|
+
# The denominator of the node
|
227
|
+
# @attr_reader weight [Rational|Infinity]
|
228
|
+
# The value of the node
|
229
|
+
#
|
215
230
|
class Node
|
216
231
|
include Comparable
|
217
232
|
|
218
|
-
|
233
|
+
attr_reader :numerator, :denominator, :weight
|
219
234
|
|
220
235
|
def initialize(n,d)
|
221
236
|
@numerator = n
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fraction-tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jose Hales-Garcia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: continued_fractions
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,14 +86,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
89
|
+
version: '3.1'
|
90
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
91
|
requirements:
|
92
92
|
- - ">="
|
93
93
|
- !ruby/object:Gem::Version
|
94
|
-
version: '
|
94
|
+
version: '3.1'
|
95
95
|
requirements: []
|
96
|
-
rubygems_version: 3.4.
|
96
|
+
rubygems_version: 3.4.22
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: Fraction tree
|