Module:Genus

From Polytope Wiki
Jump to navigation Jump to search

A module to calculate the genus of a polytope given its Euler characteristic and orientability.

Call with the orientability first (either yes or no) and then the Euler characteristic.


local p = {}

-- Used to call from #invoke.
function p.genus(frame)
  local args = frame.args
  return p._genus(args)
end

-- Used when calling from other Lua modules.
function p._genus(args)
  if (args[1] == "yes") then
    return 1 - args[2] / 2
  elseif (args[1] == "no") then
    return 2 - args[2]
  end
end

-- Used by [[Template:Infobox polytope]] to create the genus
function p.makeGenus(frame)
  local args = frame.args
  return p._makeGenus(args)
end

function p._makeGenus(args)
  local g = nil
  if (args.genus ~= nil and args.genus ~= "") then
    g = args.genus
  elseif (args.rank ~= nil and tonumber(args.rank) == 3) then
    if (args.convex == "yes") then
      g = "0"
    elseif ((args.orientable == "yes" or args.orientable == "no") and (args.euler ~= nil) and (tonumber(args.euler) ~= nil)) then
      g = p._genus(
        { [1] = args.orientable
        , [2] = tonumber(args.euler)
        }
      )
    end
  end
  if (g ~= nil) then
    local cats = ""
    if (tonumber(g) ~= 0) then
      if (args.orientable == "yes") then
        cats = "[[Category:Orientable polyhedra of genus " .. g .. "]]"
      elseif (args.orientable == "no") then
        cats = "[[Category:Non-orientable polyhedra of genus " .. g .. "]]"
      end
    end
    return g .. cats
  end
end

return p