Module:Rational: Difference between revisions
m hz() implemented |
find_S_expression() initial implementation |
||
Line 1: | Line 1: | ||
local seq = require('Module:Sequence') | |||
local u = require('Module:Utils') | local u = require('Module:Utils') | ||
local p = {} | local p = {} | ||
Line 123: | Line 124: | ||
end | end | ||
return false | return false | ||
end | |||
-- attempt to identify the ratio as a simple S-expression | |||
function p.find_S_expression(a) | |||
if type(a) == 'number' then | |||
a = p.new(a) | |||
end | |||
if a.nan or a.inf or a.zero or a.sign < 0 then | |||
return {} | |||
end | |||
if p.eq(a, 1) then | |||
return {} | |||
end | |||
local max_prime = p.max_prime(a) | |||
if seq.square_superparticulars[max_prime] == nil then | |||
return {} | |||
end | |||
local expressions = {} | |||
local superparticular_indices = {} | |||
local superparticular_ratios = {} | |||
for prime, k_array in pairs(seq.square_superparticulars) do | |||
if prime <= max_prime + 10 then | |||
for i, k in ipairs(k_array) do | |||
table.insert(superparticular_indices, k) | |||
local Sk_num = p.pow(p.new(k), 2) | |||
local Sk_den = p.sub(Sk_num, 1) | |||
local Sk = p.div(Sk_num, Sk_den) | |||
superparticular_ratios[k] = Sk | |||
end | |||
end | |||
end | |||
-- is it a square superparticular? | |||
for _, k in ipairs(superparticular_indices) do | |||
if p.eq(a, superparticular_ratios[k]) then | |||
table.insert(expressions, 'S' .. k) | |||
end | |||
end | |||
-- is it triangle-particular or ultraparticular? | |||
for _, k in ipairs(superparticular_indices) do | |||
local r1 = superparticular_ratios[k] | |||
local r2 = superparticular_ratios[k + 1] | |||
if r1 and r2 then | |||
if p.eq(a, p.mul(r1, r2)) then | |||
table.insert(expressions, 'S' .. k .. ' × S' .. (k + 1)) | |||
end | |||
if p.eq(a, p.div(r1, r2)) then | |||
table.insert(expressions, 'S' .. k .. ' / S' .. (k + 1)) | |||
end | |||
end | |||
end | |||
-- is it 1/3-square-particular? | |||
for _, k in ipairs(superparticular_indices) do | |||
local r1 = superparticular_ratios[k - 1] | |||
local r2 = superparticular_ratios[k] | |||
local r3 = superparticular_ratios[k + 1] | |||
if r1 and r2 and r3 then | |||
if p.eq(a, p.mul(r1, p.mul(r2, r3))) then | |||
table.insert(expressions, 'S' .. (k - 1) .. ' × S' .. k .. ' × S' .. (k + 1)) | |||
end | |||
end | |||
end | |||
-- is it semiparticular? | |||
for _, k in ipairs(superparticular_indices) do | |||
local r1 = superparticular_ratios[k] | |||
local r2 = superparticular_ratios[k + 2] | |||
if r1 and r2 then | |||
if p.eq(a, p.div(r1, r2)) then | |||
table.insert(expressions, 'S' .. k .. ' / S' .. (k + 2)) | |||
end | |||
end | |||
end | |||
-- is it a product or a ratio of two square superparticulars? | |||
for _, k1 in ipairs(superparticular_indices) do | |||
local r1 = superparticular_ratios[k1] | |||
for _, k2 in ipairs(superparticular_indices) do | |||
local r2 = superparticular_ratios[k2] | |||
if k1 <= k2 and p.eq(a, p.mul(r1, r2)) then | |||
-- check for duplicates | |||
local expr = 'S' .. k1 .. ' × S' .. k2 | |||
local dup = false | |||
for _, expr2 in ipairs(expressions) do | |||
if expr == expr2 then | |||
dup = true | |||
break | |||
end | |||
end | |||
if not dup then | |||
table.insert(expressions, expr) | |||
end | |||
end | |||
if k1 <= k2 and p.eq(a, p.div(r1, r2)) then | |||
-- check for duplicates | |||
local expr = 'S' .. k1 .. ' / S' .. k2 | |||
local dup = false | |||
for _, expr2 in ipairs(expressions) do | |||
if expr == expr2 then | |||
dup = true | |||
break | |||
end | |||
end | |||
if not dup then | |||
table.insert(expressions, expr) | |||
end | |||
end | |||
end | |||
end | |||
return expressions | |||
end | end | ||
Line 792: | Line 907: | ||
end | end | ||
-- find max prime in | -- find max prime involved in the factorisation of a rational number | ||
function p.max_prime(a) | function p.max_prime(a) | ||
if type(a) == 'number' then | if type(a) == 'number' then |