| 1 |
--- @class TrigExpression |
| 2 |
--- Represents a trigonometric function from one expression to another. |
| 3 |
--- @field name string |
| 4 |
--- @field expression Expression |
| 5 |
TrigExpression = {} |
| 6 |
local __TrigExpression = {} |
| 7 |
|
| 8 |
---------------------------- |
| 9 |
-- Instance functionality -- |
| 10 |
---------------------------- |
| 11 |
|
| 12 |
--- Creates a new trig expression with the given name and expression. |
| 13 |
--- @param name string|SymbolExpression |
| 14 |
--- @param expression Expression |
| 15 |
--- @return TrigExpression |
| 16 |
function TrigExpression:new(name, expression) |
| 17 |
local o = {} |
| 18 |
local __o = Copy(__ExpressionOperations) |
| 19 |
|
| 20 |
if not TrigExpression.NAMES[name] then |
| 21 |
error("Argument error: " .. name .. " is not the name of a trigonometric function.") |
| 22 |
end |
| 23 |
|
| 24 |
o.name = name |
| 25 |
o.expression = expression |
| 26 |
o.expressions = {expression} |
| 27 |
if expression:isatomic() then |
| 28 |
o.variables = {expression} |
| 29 |
else |
| 30 |
o.variables = {SymbolExpression('x')} |
| 31 |
end |
| 32 |
o.derivatives = {Integer.zero()} |
| 33 |
|
| 34 |
__o.__index = TrigExpression |
| 35 |
__o.__tostring = function(a) |
| 36 |
return tostring(a.name) .. '(' .. tostring(a.expression) .. ')' |
| 37 |
end |
| 38 |
__o.__eq = function(a, b) |
| 39 |
-- if b:type() == FunctionExpression then |
| 40 |
-- return a:tofunction() == b |
| 41 |
-- end |
| 42 |
-- This shouldn't be needed, since __eq should only fire if both metamethods have the same function, but for some reason Lua always runs this anyway |
| 43 |
if not b:type() == TrigExpression then |
| 44 |
return false |
| 45 |
end |
| 46 |
return a.name == b.name and a.expression == b.expression |
| 47 |
end |
| 48 |
|
| 49 |
o = setmetatable(o, __o) |
| 50 |
return o |
| 51 |
end |
| 52 |
|
| 53 |
--- @return TrigExpression |
| 54 |
function TrigExpression:evaluate() |
| 55 |
local expression = self.expression:autosimplify() |
| 56 |
|
| 57 |
if expression == Integer.zero() then |
| 58 |
if self.name == "cos" or self.name == "sec" then |
| 59 |
return Integer.one() |
| 60 |
end |
| 61 |
if self.name == "sin" or self.name == "tan" then |
| 62 |
return Integer.zero() |
| 63 |
end |
| 64 |
if self.name == "arctan" or self.name == "arcsin" then |
| 65 |
return Integer.zero() |
| 66 |
end |
| 67 |
if self.name == "arccos" or self.name == "arccot" then |
| 68 |
return PI / Integer(2) |
| 69 |
end |
| 70 |
end |
| 71 |
|
| 72 |
if expression == PI then |
| 73 |
if self.name == "cos" or self.name == "sec" then |
| 74 |
return Integer(-1) |
| 75 |
end |
| 76 |
if self.name == "sin" or self.name == "tan" then |
| 77 |
return Integer.zero() |
| 78 |
end |
| 79 |
end |
| 80 |
|
| 81 |
if expression:ismulratlPI() then |
| 82 |
local coeff = expression.expressions[1] |
| 83 |
if TrigExpression.COSVALUES[tostring(coeff)] ~= nil then |
| 84 |
if self.name == "cos" then |
| 85 |
return TrigExpression.COSVALUES[tostring(coeff)]:autosimplify() |
| 86 |
end |
| 87 |
if self.name == "sin" then |
| 88 |
local sign = Integer.one() |
| 89 |
if coeff > Integer.one() then |
| 90 |
sign = Integer(-1) |
| 91 |
end |
| 92 |
return (sign*sqrt(Integer.one()-cos(expression)^Integer(2))):autosimplify() |
| 93 |
end |
| 94 |
if self.name == "tan" then |
| 95 |
return (sin(expression) / cos(expression)):autosimplify() |
| 96 |
end |
| 97 |
if self.name == "sec" then |
| 98 |
return (Integer.one() / cos(expression)):autosimplify() |
| 99 |
end |
| 100 |
if self.name == "csc" then |
| 101 |
return (Integer.one() / sin(expression)):autosimplify() |
| 102 |
end |
| 103 |
if self.name == "cot" then |
| 104 |
return (cos(expression) / sin(expression)):autosimplify() |
| 105 |
end |
| 106 |
end |
| 107 |
end |
| 108 |
|
| 109 |
if TrigExpression.ACOSVALUES[tostring(expression)] ~= nil then |
| 110 |
if self.name == "arccos" then |
| 111 |
return TrigExpression.ACOSVALUES[tostring(expression)]:autosimplify() |
| 112 |
end |
| 113 |
if self.name == "arcsin" then |
| 114 |
if expression == Integer(-1) then |
| 115 |
return TrigExpression.ACOSVALUES["-1"]:autosimplify() |
| 116 |
elseif expression.expressions and expression.expressions[1] == Integer(-1) then |
| 117 |
local expr = (Integer(-1)*sqrt(Integer.one() - expression ^ Integer(2))):autosimplify() |
| 118 |
return TrigExpression.ACOSVALUES[tostring(expr)]:autosimplify() |
| 119 |
else |
| 120 |
local expr = (sqrt(Integer.one() - expression ^ Integer(2))):autosimplify() |
| 121 |
return TrigExpression.ACOSVALUES[tostring(expr)]:autosimplify() |
| 122 |
end |
| 123 |
end |
| 124 |
end |
| 125 |
|
| 126 |
if self.name == "arctan" and TrigExpression.ATANVALUES[tostring(expression)] ~= nil then |
| 127 |
return TrigExpression.ATANVALUES[tostring(expression)]:autosimplify() |
| 128 |
end |
| 129 |
|
| 130 |
return self |
| 131 |
end |
| 132 |
|
| 133 |
--- checks if expression is a rational multiple of pi |
| 134 |
--- @return boolean |
| 135 |
function Expression:ismulratlPI() |
| 136 |
if self.operation == BinaryOperation.MUL and #self.expressions == 2 and (self.expressions[1]:type() == Integer or self.expressions[1]:type() == Rational) and self.expressions[2] == PI then |
| 137 |
return true |
| 138 |
end |
| 139 |
|
| 140 |
return false |
| 141 |
end |
| 142 |
|
| 143 |
--- @return TrigExpression |
| 144 |
function TrigExpression:autosimplify() |
| 145 |
local expression = self.expression:autosimplify() |
| 146 |
|
| 147 |
-- even and odd properties of trig functions |
| 148 |
if (self.name == "sin" or self.name == "tan" or self.name == "csc" or self.name == "cot") and |
| 149 |
expression.operation == BinaryOperation.MUL and expression.expressions[1]:isconstant() and expression.expressions[1] < Integer(0) then |
| 150 |
return (-Integer.one() * TrigExpression(self.name, -expression)):autosimplify() |
| 151 |
end |
| 152 |
|
| 153 |
if (self.name == "cos" or self.name == "sec") and |
| 154 |
expression.operation == BinaryOperation.MUL and expression.expressions[1]:isconstant() and expression.expressions[1] < Integer(0) then |
| 155 |
expression = (-expression):autosimplify() |
| 156 |
end |
| 157 |
|
| 158 |
-- uses periodicity of sin and cos and friends |
| 159 |
if self.name == "sin" or self.name == "cos" or self.name == "csc" or self.name == "sec" then |
| 160 |
if expression == Integer.zero() or expression == PI then |
| 161 |
goto skip |
| 162 |
end |
| 163 |
if expression.operation ~= BinaryOperation.ADD then |
| 164 |
expression = BinaryOperation(BinaryOperation.ADD,{expression}) |
| 165 |
end |
| 166 |
for index,component in ipairs(expression.expressions) do |
| 167 |
if component:ismulratlPI() then |
| 168 |
local coeff = component.expressions[1] |
| 169 |
if coeff:type() == Integer then |
| 170 |
coeff = coeff % Integer(2) |
| 171 |
coeff = coeff:autosimplify() |
| 172 |
end |
| 173 |
if coeff:type() == Rational then |
| 174 |
local n = coeff.numerator |
| 175 |
local d = coeff.denominator |
| 176 |
local m = {n:divremainder(d)} |
| 177 |
coeff = (m[1] % Integer(2)) + m[2]/d |
| 178 |
coeff = coeff:autosimplify() |
| 179 |
end |
| 180 |
expression.expressions[index].expressions[1] = coeff |
| 181 |
end |
| 182 |
expression = expression:autosimplify() |
| 183 |
end |
| 184 |
::skip:: |
| 185 |
end |
| 186 |
|
| 187 |
-- uses periodicity of tan and cot |
| 188 |
if self.name == "tan" or self.name == "cot" then |
| 189 |
if expression == Integer.zero() or expression == PI then |
| 190 |
goto skip |
| 191 |
end |
| 192 |
if expression.operation ~= BinaryOperation.ADD then |
| 193 |
expression = BinaryOperation(BinaryOperation.ADD,{expression}) |
| 194 |
end |
| 195 |
for index,component in ipairs(expression.expressions) do |
| 196 |
if component:ismulratlPI() then |
| 197 |
local coeff = component.expressions[1] |
| 198 |
if coeff:type() == Integer then |
| 199 |
coeff = Integer.zero() |
| 200 |
end |
| 201 |
if coeff:type() == Rational then |
| 202 |
local n = coeff.numerator |
| 203 |
local d = coeff.denominator |
| 204 |
local m = {n:divremainder(d)} |
| 205 |
coeff = m[2]/d |
| 206 |
coeff = coeff:autosimplify() |
| 207 |
end |
| 208 |
expression.expressions[index].expressions[1] = coeff |
| 209 |
end |
| 210 |
if component == PI then |
| 211 |
expression.expressions[index] = Integer.zero() |
| 212 |
end |
| 213 |
end |
| 214 |
expression = expression:autosimplify() |
| 215 |
::skip:: |
| 216 |
end |
| 217 |
|
| 218 |
return TrigExpression(self.name, expression):evaluate() |
| 219 |
end |
| 220 |
|
| 221 |
--- @return table<number, Expression> |
| 222 |
function TrigExpression:subexpressions() |
| 223 |
return {self.expression} |
| 224 |
end |
| 225 |
|
| 226 |
--- @param subexpressions table<number, Expression> |
| 227 |
--- @return TrigExpression |
| 228 |
function TrigExpression:setsubexpressions(subexpressions) |
| 229 |
return TrigExpression(self.name, subexpressions[1]) |
| 230 |
end |
| 231 |
|
| 232 |
-- function TrigExpression:freeof(symbol) |
| 233 |
-- return self.expression:freeof(symbol) |
| 234 |
-- end |
| 235 |
|
| 236 |
-- function TrigExpression:substitute(map) |
| 237 |
-- for expression, replacement in pairs(map) do |
| 238 |
-- if self == expression then |
| 239 |
-- return replacement |
| 240 |
-- end |
| 241 |
-- end |
| 242 |
-- return TrigExpression(self.name, self.expression:substitute(map)) |
| 243 |
-- end |
| 244 |
|
| 245 |
-- function TrigExpression:order(other) |
| 246 |
-- return self:tofunction():order(other) |
| 247 |
-- end |
| 248 |
|
| 249 |
-- function TrigExpression:tofunction() |
| 250 |
-- return FunctionExpression(self.name, {self.expression}, true) |
| 251 |
-- end |
| 252 |
|
| 253 |
----------------- |
| 254 |
-- Inheritance -- |
| 255 |
----------------- |
| 256 |
|
| 257 |
__TrigExpression.__index = FunctionExpression |
| 258 |
__TrigExpression.__call = TrigExpression.new |
| 259 |
TrigExpression = setmetatable(TrigExpression, __TrigExpression) |
| 260 |
|
| 261 |
---------------------- |
| 262 |
-- Static constants -- |
| 263 |
---------------------- |
| 264 |
TrigExpression.NAMES = {sin=1, cos=2, tan=3, csc=4, sec=5, cot=6, |
| 265 |
arcsin=7, arccos=8, arctan=9, arccsc=10, arcsec=11, arccot=12} |
| 266 |
|
| 267 |
TrigExpression.INVERSES = {sin="arcsin", cos="arccos", tan="arctan", csc="arccsc", sec="arcsec", cot="arccot", |
| 268 |
arcsin="sin", arccos="cos", arctan="tan", arccsc="csc", arcsec="sec", arccot="cot"} |
| 269 |
|
| 270 |
TrigExpression.COSVALUES = { |
| 271 |
["0"] = Integer.one(), |
| 272 |
["1/6"] = sqrt(Integer(3))/Integer(2), |
| 273 |
["1/4"] = sqrt(Integer(2))/Integer(2), |
| 274 |
["1/3"] = Integer.one()/Integer(2), |
| 275 |
["1/2"] = Integer.zero(), |
| 276 |
["2/3"] = -Integer.one()/Integer(2), |
| 277 |
["3/4"] = -sqrt(Integer(2))/Integer(2), |
| 278 |
["5/6"] = -sqrt(Integer(3))/Integer(2), |
| 279 |
["1"] = -Integer.one(), |
| 280 |
["7/6"] = -sqrt(Integer(3))/Integer(2), |
| 281 |
["5/4"] = -sqrt(Integer(2))/Integer(2), |
| 282 |
["4/3"] = -Integer.one()/Integer(2), |
| 283 |
["3/2"] = Integer.zero(), |
| 284 |
["5/3"] = Integer.one()/Integer(2), |
| 285 |
["7/4"] = sqrt(Integer(2))/Integer(2), |
| 286 |
["11/6"] = sqrt(Integer(3))/Integer(2), |
| 287 |
} |
| 288 |
TrigExpression.ACOSVALUES = { |
| 289 |
["1"] = Integer.zero(), |
| 290 |
["(1/2 * sqrt(3,2))"] = PI * Integer(6) ^ Integer(-1), |
| 291 |
["(1/2 * sqrt(2,2))"] = PI * Integer(4) ^ Integer(-1), |
| 292 |
["1/2"] = PI * Integer(3) ^ Integer(-1), |
| 293 |
["0"] = PI * Integer(2) ^ Integer(-1), |
| 294 |
["-1/2"] = PI * Integer(2) * Integer(3) ^ Integer(-1), |
| 295 |
["(-1/2 * sqrt(2,2))"]= PI * Integer(3) * Integer(4) ^ Integer(-1), |
| 296 |
["(-1/2 * sqrt(3,2))"]= PI * Integer(5) * Integer(6) ^ Integer(-1), |
| 297 |
["-1"] = Integer(-1)*PI, |
| 298 |
} |
| 299 |
TrigExpression.ATANVALUES = { |
| 300 |
["(-1 * sqrt(3,2))"] = Integer(-1) * PI * Integer(3) ^ Integer(-1), |
| 301 |
["-1"] = Integer(-1) * PI * Integer(4) ^ Integer(-1), |
| 302 |
["(-1/3 * sqrt(3,2))"] = Integer(-1) * Integer(6) ^ Integer(-1), |
| 303 |
["0"] = Integer.zero(), |
| 304 |
["(1/3 * sqrt(3,2))"] = PI * Integer(6) ^ Integer(-1), |
| 305 |
["1"] = PI * Integer(4) ^ Integer(-1), |
| 306 |
["sqrt(3,2)"] = PI * Integer(3) ^ Integer(-1) |
| 307 |
} |
| 308 |
|
| 309 |
SIN = function (a) |
| 310 |
return TrigExpression("sin", a) |
| 311 |
end |
| 312 |
|
| 313 |
COS = function (a) |
| 314 |
return TrigExpression("cos", a) |
| 315 |
end |
| 316 |
|
| 317 |
TAN = function (a) |
| 318 |
return TrigExpression("tan", a) |
| 319 |
end |
| 320 |
|
| 321 |
CSC = function (a) |
| 322 |
return TrigExpression("csc", a) |
| 323 |
end |
| 324 |
|
| 325 |
SEC = function (a) |
| 326 |
return TrigExpression("sec", a) |
| 327 |
end |
| 328 |
|
| 329 |
COT = function (a) |
| 330 |
return TrigExpression("cot", a) |
| 331 |
end |
| 332 |
|
| 333 |
ARCSIN = function (a) |
| 334 |
return TrigExpression("arcsin", a) |
| 335 |
end |
| 336 |
|
| 337 |
ARCCOS = function (a) |
| 338 |
return TrigExpression("arccos", a) |
| 339 |
end |
| 340 |
|
| 341 |
ARCTAN = function (a) |
| 342 |
return TrigExpression("arctan", a) |
| 343 |
end |
| 344 |
|
| 345 |
ARCCSC = function (a) |
| 346 |
return TrigExpression("arccsc", a) |
| 347 |
end |
| 348 |
|
| 349 |
ARCSEC = function (a) |
| 350 |
return TrigExpression("arcsec", a) |
| 351 |
end |
| 352 |
|
| 353 |
ARCCOT = function (a) |
| 354 |
return TrigExpression("arccot", a) |
| 355 |
end |