| 1 |
--- @class Field |
| 2 |
--- Interface for an element of a field. |
| 3 |
Field = {} |
| 4 |
local __Field = {} |
| 5 |
|
| 6 |
---------------------- |
| 7 |
-- Required methods -- |
| 8 |
---------------------- |
| 9 |
|
| 10 |
--- @return Field |
| 11 |
function Field:div(b) |
| 12 |
return self:mul(b:inv()) |
| 13 |
end |
| 14 |
|
| 15 |
--- @return Field |
| 16 |
function Field:inv() |
| 17 |
error("Called unimplemented method: inv()") |
| 18 |
end |
| 19 |
|
| 20 |
---------------------------- |
| 21 |
-- Instance functionality -- |
| 22 |
---------------------------- |
| 23 |
|
| 24 |
--- Field exponentiation based on the definition. Specific rings may implement more efficient methods. |
| 25 |
--- @return Field |
| 26 |
function Field:pow(n) |
| 27 |
local base = self |
| 28 |
if(n < Integer.zero()) then |
| 29 |
n = -n |
| 30 |
base = base:inv() |
| 31 |
end |
| 32 |
local k = Integer.zero() |
| 33 |
local b = self.getring().one() |
| 34 |
while k < n do |
| 35 |
b = b.mul(base) |
| 36 |
k = k + Integer.one() |
| 37 |
end |
| 38 |
return b |
| 39 |
end |
| 40 |
|
| 41 |
-------------------------- |
| 42 |
-- Instance metamethods -- |
| 43 |
-------------------------- |
| 44 |
|
| 45 |
__FieldOperations = Copy(__EuclideanOperations) |
| 46 |
|
| 47 |
__FieldOperations.__div = function(a, b) |
| 48 |
if not b.getring and not b:isconstant() then |
| 49 |
return BinaryOperation.DIVEXP({a, b}) |
| 50 |
end |
| 51 |
|
| 52 |
if(b == b:zero()) then |
| 53 |
error("Arithmetic Error: Cannot divide by zero.") |
| 54 |
end |
| 55 |
|
| 56 |
local aring, bring = a:getring(), b:getring() |
| 57 |
local oring = Ring.resultantring(aring, bring) |
| 58 |
if not oring then |
| 59 |
error("Attempted to divide two elements of incompatable rings") |
| 60 |
end |
| 61 |
return a:inring(oring):div(b:inring(oring)) |
| 62 |
end |
| 63 |
|
| 64 |
----------------- |
| 65 |
-- Inheritance -- |
| 66 |
----------------- |
| 67 |
|
| 68 |
__Field.__index = EuclideanDomain |
| 69 |
Field = setmetatable(Field, __Field) |