Module:SB tree: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
No edit summary
Line 4: Line 4:
-- Function that constructs a sequence of ratios according to the Stern-Brocot tree
-- Function that constructs a sequence of ratios according to the Stern-Brocot tree
-- Ratios are entered as arrays, such as {1, 1} for the ratio 1/1 and {1, 0} for the ratio 1/0
-- Ratios are entered as arrays, such as {1, 1} for the ratio 1/1 and {1, 0} for the ratio 1/0
-- Mediants are found between adjacent ratio iteratively
-- Mediants are found between adjacent ratios iteratively
-- Start and stop ratio may be any two ratios, but the default values are 1/1 and 1/0
-- Start and stop ratio may be any two ratios, but the default values are 1/1 and 1/0
-- Depth may be specified to be any value, but the default is 4
-- Depth may be specified to be any value, but the default is 4
-- Edge extend iteratively finds the mediants of the first two and last two ratios; default is 0
-- Edge extend iteratively finds the mediants of the first two and last two ratios; default is 0
-- Transpiled from python code to lua with aid of ChatGPT
-- Transpiled from python code to lua with aid of ChatGPT
function p.sb_tree(depth, start_ratio, stop_ratio, edge_extend)
function p.sb_tree_ratios(depth, start_ratio, stop_ratio, edge_extend)
-- Default parameter values
-- Default parameter values
depth = depth or 4
depth = depth or 4
Line 26: Line 26:
         -- Make a new tree that has entries in between existing ratios (the mediants)
         -- Make a new tree that has entries in between existing ratios (the mediants)
         -- For loop needs to make one fewer iteration since the current ratio and its mediant with the next
         -- For loop needs to make one fewer iteration since the current ratio and its mediant with the next
         -- are added; the last ratio in the array is added separately after the loop
         -- are added as a pair; the last ratio in the array is added separately after the loop
         for j = 1, #tree - 1 do
         for j = 1, #tree - 1 do
             local ratio_1 = tree[j]
             local ratio_1 = tree[j]
Line 67: Line 67:
-- Call the sb function
-- Call the sb function
-- Start/stop ratios are the same, depth is deeper, and edge-extend is allowed
-- Start/stop ratios are the same, depth is deeper, and edge-extend is allowed
local sb_tree_ratios = p.sb_tree(5, {1, 1}, {1, 0}, 3)
local sb_tree_ratios = p.sb_tree_ratios(5, {1, 1}, {1, 0}, 3)
-- Create the table
-- Create the table