Module:Rational: Difference between revisions
Add int limit function (prepwork for a separate algorithm) |
Unify the format for S-expressions |
||
| (6 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | |||
local seq = require("Module:Sequence") | local seq = require("Module:Sequence") | ||
local utils = require("Module:Utils") | local utils = require("Module:Utils") | ||
-- enter a numerator n and denominator m | -- enter a numerator n and denominator m | ||
| Line 173: | Line 174: | ||
end | end | ||
-- is it Sk*S(k+1) or Sk/S(k+1) or Sk^2*S(k+1) or Sk*S(k+1)^2? | -- is it Sk*S(k + 1) or Sk/S(k + 1) or Sk^2*S(k + 1) or Sk*S(k + 1)^2? | ||
for _, k in ipairs(superparticular_indices) do | for _, k in ipairs(superparticular_indices) do | ||
local r1 = superparticular_ratios[k] | local r1 = superparticular_ratios[k] | ||
| Line 179: | Line 180: | ||
if r1 and r2 then | if r1 and r2 then | ||
if p.eq(a, p.mul(r1, r2)) then | if p.eq(a, p.mul(r1, r2)) then | ||
table.insert(expressions, "S" .. k .. " | table.insert(expressions, "S" .. k .. "⋅S" .. (k + 1)) | ||
end | end | ||
if p.eq(a, p.div(r1, r2)) then | if p.eq(a, p.div(r1, r2)) then | ||
table.insert(expressions, "S" .. k .. " / S" .. (k + 1)) | table.insert(expressions, "S" .. k .. "/S" .. (k + 1)) | ||
end | end | ||
if p.eq(a, p.mul(p.pow(r1, 2), r2)) then | if p.eq(a, p.mul(p.pow(r1, 2), r2)) then | ||
table.insert(expressions, "S" .. k .. "<sup>2</sup> | table.insert(expressions, "S" .. k .. "<sup>2</sup>⋅S" .. (k + 1)) | ||
end | end | ||
if p.eq(a, p.mul(r1, p.pow(r2, 2))) then | if p.eq(a, p.mul(r1, p.pow(r2, 2))) then | ||
table.insert(expressions, "S" .. k .. " | table.insert(expressions, "S" .. k .. "⋅S" .. (k + 1) .. "<sup>2</sup>") | ||
end | end | ||
end | end | ||
end | end | ||
-- is it Sk/S(k+2)? | -- is it Sk/S(k + 2)? | ||
for _, k in ipairs(superparticular_indices) do | for _, k in ipairs(superparticular_indices) do | ||
local r1 = superparticular_ratios[k] | local r1 = superparticular_ratios[k] | ||
| Line 199: | Line 200: | ||
if r1 and r2 then | if r1 and r2 then | ||
if p.eq(a, p.div(r1, r2)) then | if p.eq(a, p.div(r1, r2)) then | ||
table.insert(expressions, "S" .. k .. " / S" .. (k + 2)) | table.insert(expressions, "S" .. k .. "/S" .. (k + 2)) | ||
end | end | ||
end | end | ||
end | end | ||
-- is it S(k-1)*Sk*S(k+1)? | -- is it S(k - 1)*Sk*S(k + 1)? | ||
for _, k in ipairs(superparticular_indices) do | for _, k in ipairs(superparticular_indices) do | ||
local r1 = superparticular_ratios[k - 1] | local r1 = superparticular_ratios[k - 1] | ||
| Line 211: | Line 212: | ||
if r1 and r2 and r3 then | if r1 and r2 and r3 then | ||
if p.eq(a, p.mul(r1, p.mul(r2, r3))) then | if p.eq(a, p.mul(r1, p.mul(r2, r3))) then | ||
table.insert(expressions, "S" .. (k - 1) .. " | table.insert(expressions, "S" .. (k - 1) .. "⋅S" .. k .. "⋅S" .. (k + 1)) | ||
end | end | ||
end | end | ||
| Line 332: | Line 333: | ||
-- compute a canonical representation of `a` modulo powers of `b` | -- compute a canonical representation of `a` modulo powers of `b` | ||
-- TODO: describe the exact behavior | |||
-- it seems bugged when the equave is a fraction | |||
function p.modulo_mul(a, b) | function p.modulo_mul(a, b) | ||
if type(a) == "number" then | if type(a) == "number" then | ||
| Line 659: | Line 662: | ||
local den = p.mul(p.add(k, 1), p.sub(k, 1)) | local den = p.mul(p.add(k, 1), p.sub(k, 1)) | ||
return p.eq(a, p.div(p.pow(k, 2), den)) | return p.eq(a, p.div(p.pow(k, 2), den)) | ||
end | |||
-- check if an integer is prime | |||
function p.is_prime(a) | |||
if type(a) == "number" then | |||
a = p.new(a) | |||
end | |||
-- nan, inf, zero, and negative numbers are not prime | |||
if a.nan or a.inf or a.zero or a.sign < 0 then | |||
return false | |||
end | |||
local flag = false -- flag for having exactly one prime factor | |||
for factor, power in pairs(a) do | |||
if type(factor) == "number" and power then | |||
if flag or power ~= 1 then | |||
return false | |||
else | |||
flag = true | |||
end | |||
end | |||
end | |||
return flag | |||
end | end | ||
| Line 666: | Line 693: | ||
a = p.new(a) | a = p.new(a) | ||
end | end | ||
-- nan, inf, zero, and negative numbers are not highly composite | |||
if a.nan or a.inf or a.zero or a.sign == -1 then | |||
-- negative numbers are not highly composite | |||
if a.sign == -1 then | |||
return false | return false | ||
end | end | ||
-- non-integers are not highly composite | -- non-integers are not highly composite | ||
for factor, power in pairs(a) do | for factor, power in pairs(a) do | ||
| Line 681: | Line 707: | ||
end | end | ||
end | end | ||
local last_power = 1 / 0 | local last_power = 1 / 0 | ||
local max_prime = p.max_prime(a) | local max_prime = p.max_prime(a) | ||
| Line 869: | Line 896: | ||
end | end | ||
-- | -- Check if ratio is within an int limit; that is, neither its numerator nor | ||
-- | -- denominator exceed that limit. | ||
function p. | function p.is_within_int_limit(a, lim) | ||
return p.int_limit(a) <= lim | |||
end | end | ||
| Line 918: | Line 931: | ||
local num, den = p.as_pair(a_copy) | local num, den = p.as_pair(a_copy) | ||
return math.max(num, den) | return math.max(num, den) | ||
end | |||
-- find max prime involved in the factorisation | |||
-- (a.k.a. prime limit or harmonic class) of a rational number | |||
function p.max_prime(a) | |||
if type(a) == "number" then | |||
a = p.new(a) | |||
end | |||
if a.nan or a.inf or a.zero then | |||
return nil | |||
end | |||
local max_factor = 0 | |||
for factor, _ in pairs(a) do | |||
if type(factor) == "number" then | |||
if factor > max_factor then | |||
max_factor = factor | |||
end | |||
end | |||
end | |||
return max_factor | |||
end | end | ||
| Line 1,053: | Line 1,086: | ||
-- FJS representation of a rational number | -- FJS representation of a rational number | ||
-- | -- NOTE: incorrect for prime 127 and possibly other rare cases | ||
-- TODO: | -- TODO: identify the exact problem and fix it | ||
function p.as_FJS(a) | function p.as_FJS(a) | ||
if type(a) == "number" then | if type(a) == "number" then | ||
| Line 1,502: | Line 1,535: | ||
function p.ket(frame) | function p.ket(frame) | ||
local unparsed = frame.args[1] or "1" | local unparsed = frame.args[1] or "1" | ||
local result = "" | |||
local a = p.parse(unparsed) | local a = p.parse(unparsed) | ||
if a == nil then | if a == nil then | ||
result = '{{error|Invalid rational number: ' .. unparsed .. ".}}" | |||
else | |||
result = p.as_ket(a, frame) | |||
end | end | ||
return | |||
return frame:preprocess(result) | |||
end | end | ||
p.monzo = p.ket | p.monzo = p.ket | ||
return p | return p | ||