Debian Codegraph @

texmf-dist/tex/lualatex/luacas/algebra/luacas-rational.lua

raw
1 --- @class Rational
2 --- Represents an element of the field of rational numbers or rational functions.
3 --- @field numerator Ring
4 --- @field denominator Ring
5 --- @field ring RingIdentifier
6 Rational = {}
7 local __Rational = {}
8
9
10 --------------------------
11 -- Static functionality --
12 --------------------------
13
14 -- Metatable for ring objects.
15 local __obj = {__index = Rational, __eq = function(a, b)
16 return a["ring"] == b["ring"] and
17 (a["child"] == b["child"] or a["child"] == nil or b["child"] == nil) and
18 (a["symbol"] == b["symbol"] or a["child"] == nil or b["child"] == nil)
19 end, __tostring = function(a)
20 if a.symbol then
21 return tostring(a.child.child) .. "(" .. a.symbol .. ")"
22 end
23 if a.child then
24 return "QQ"
25 end
26 return "(Generic Fraction Field)"
27 end}
28
29 --- @param symbol SymbolExpression
30 --- @param child RingIdentifier
31 --- @return RingIdentifier
32 function Rational.makering(symbol, child)
33 local t = {ring = Rational}
34 t.symbol = symbol
35 t.child = child
36 t = setmetatable(t, __obj)
37 return t
38 end
39
40 --- Converts a string of the form -?[0-9]+ or -?[0-9]+\/[0-9]+ to a rational number.
41 --- @param str string
42 --- @return Rational|Integer
43 function Rational.fromstring(str)
44 local divloc = string.find(str, "/");
45 if not divloc then
46 return Integer(str)
47 end
48 return Rational(Integer(string.sub(str, 1, divloc - 1)), Integer(string.sub(str, divloc + 1, #str)))
49 end
50
51
52 ----------------------------
53 -- Instance functionality --
54 ----------------------------
55
56 -- So we don't have to copy the field operations each time.
57 __RationalOperations = Copy(__FieldOperations)
58 __RationalOperations.__index = Rational
59 __RationalOperations.__tostring = function(a)
60 if a.ring.symbol then
61 return "(" .. tostring(a.numerator)..")/("..tostring(a.denominator) .. ")"
62 end
63 return tostring(a.numerator).."/"..tostring(a.denominator)
64 end
65
66 --- Creates a new rational given a numerator and denominator that are part of the same ring.
67 --- Rational numbers are represented uniquely.
68 --- @param n Ring
69 --- @param d Ring
70 --- @param keep boolean
71 function Rational:new(n, d, keep)
72 local o = {}
73 o = setmetatable(o, __RationalOperations)
74
75 if n:getring() == PolynomialRing.getring() then
76 o.symbol = n.symbol
77 end
78
79 if d:getring() == PolynomialRing.getring() then
80 o.symbol = d.symbol
81 end
82
83 if d == Integer(0) then
84 error("Arithmetic error: division by zero")
85 end
86
87 n = n or Integer.zero()
88 d = d or Integer.one()
89 o.numerator = n
90 o.denominator = d
91 if not keep then
92 o:reduce()
93 end
94
95 if o.numerator:getring() == Integer.getring() then
96 o.ring = Integer.getring()
97 elseif o.numerator:getring() == PolynomialRing.getring() then
98 o.ring = Ring.resultantring(o.numerator:getring(), o.denominator:getring())
99 end
100
101 if (not keep) and o.denominator == Integer.one() or (not keep) and o.numerator == Integer.zero() then
102 return o.numerator
103 end
104
105 return o
106 end
107
108 --- Reduces a rational expression to standard form. This method mutates its object.
109 function Rational:reduce()
110 if self.numerator:getring() == Integer.getring() then
111 if self.denominator < Integer.zero() then
112 self.denominator = -self.denominator
113 self.numerator = -self.numerator
114 end
115 local gcd = Integer.gcd(self.numerator, self.denominator)
116 self.numerator = self.numerator//gcd
117 self.denominator = self.denominator//gcd
118 elseif self.numerator:getring() == PolynomialRing.getring() then
119 local lc = self.denominator:lc()
120 self.denominator = self.denominator/lc
121 self.numerator = self.numerator/lc
122 local gcd = PolynomialRing.gcd(self.numerator, self.denominator)
123 self.numerator = self.numerator//gcd
124 self.denominator = self.denominator//gcd
125 end
126 end
127
128
129 --- @return RingIdentifier
130 function Rational:getring()
131 local t = {ring=Rational}
132 if self then
133 t.child = self.ring
134 t.symbol = self.symbol
135 end
136 t = setmetatable(t, __obj)
137 return t
138 end
139
140 --- @param ring RingIdentifier
141 --- @return Ring
142 function Rational:inring(ring)
143 if ring == self:getring() then
144 return self
145 end
146
147 if ring == Rational:getring() and ring.symbol then
148 if not self:getring().symbol then
149 return Rational(self:inring(ring.child), self:inring(ring.child):one(), true)
150 end
151 return Rational(self.numerator:inring(ring.child), self.denominator:inring(ring.child), true)
152 end
153
154 if ring == PolynomialRing:getring() then
155 return PolynomialRing({self:inring(ring.child)}, ring.symbol)
156 end
157
158 error("Unable to convert element to proper ring.")
159 end
160
161 --- @return boolean
162 function Rational:isconstant()
163 if self.symbol then
164 return false
165 end
166 return true
167 end
168
169 --- @return Expression
170 function Rational:tocompoundexpression()
171 return BinaryOperation(BinaryOperation.DIV, {self.numerator:tocompoundexpression(), self.denominator:tocompoundexpression()})
172 end
173
174 --- Returns this rational as a floating point number. Can only approximate the value of most rationals.
175 --- @return number
176 function Rational:asnumber()
177 return self.numerator:asnumber() / self.denominator:asnumber()
178 end
179
180 function Rational:add(b)
181 return Rational(self.numerator * b.denominator + self.denominator * b.numerator, self.denominator * b.denominator)
182 end
183
184 function Rational:neg()
185 return Rational(-self.numerator, self.denominator, true)
186 end
187
188 function Rational:mul(b)
189 return Rational(self.numerator * b.numerator, self.denominator * b.denominator)
190 end
191
192 -- function Rational:inv(b)
193 -- return Rational(self.numerator * b.numerator, self.denominator * b.denominator)
194 -- end
195
196 function Rational:pow(b)
197 return (self.numerator ^ b) / (self.denominator ^ b)
198 end
199
200 function Rational:div(b)
201 return Rational(self.numerator * b.denominator, self.denominator * b.numerator)
202 end
203
204 function Rational:eq(b)
205 return self.numerator == b.numerator and self.denominator == b.denominator
206 end
207
208 function Rational:lt(b)
209 if self.numerator < Integer.zero() and b.numerator > Integer.zero() then
210 return true
211 end
212 if self.numerator > Integer.zero() and b.numerator < Integer.zero() then
213 return false
214 end
215
216 if (self.numerator >= Integer.zero() and b.numerator >= Integer.zero()) or (self.numerator <= Integer.zero() and b.numerator <= Integer.zero()) then
217 return self.numerator * b.denominator < self.denominator * b.numerator
218 end
219 return self.numerator * b.denominator > self.denominator * b.numerator
220 end
221
222 function Rational:le(b)
223 return self:eq(b) or self:lt(b)
224 end
225
226 function Rational:zero()
227 return Integer.zero()
228 end
229
230 function Rational:one()
231 return Integer.one()
232 end
233
234 function Rational:tolatex()
235 if string.sub(self.numerator:tolatex(),1,1) == '-' then
236 return "- \\frac{" .. string.sub(self.numerator:tolatex(),2,-1) .. "}{" .. self.denominator:tolatex() .. "}"
237 end
238 return "\\frac{" .. self.numerator:tolatex() .."}{".. self.denominator:tolatex().. "}"
239 end
240
241 -----------------
242 -- Inheritance --
243 -----------------
244
245 __Rational.__index = Field
246 __Rational.__call = Rational.new
247 Rational = setmetatable(Rational, __Rational)