| 1 |
--- @class IntegralExpression |
| 2 |
--- An expression for the integral of an expression. |
| 3 |
--- @field symbol SymbolExpression |
| 4 |
--- @field expression Expression |
| 5 |
--- @field upper Expression |
| 6 |
--- @field lower Expression |
| 7 |
--- @field attempts table<number, Expression> |
| 8 |
--- @field results table<number, Expression> |
| 9 |
--- @field enhancedsubstitution Integer |
| 10 |
--- @field recursive boolean |
| 11 |
|
| 12 |
IntegralExpression = {} |
| 13 |
local __IntegralExpression = {} |
| 14 |
|
| 15 |
|
| 16 |
-------------------------- |
| 17 |
-- Static functionality -- |
| 18 |
-------------------------- |
| 19 |
|
| 20 |
--- Recursive part of the indefinite integral operator. Returns nil if the expression could not be integrated. |
| 21 |
--- We switch to prodcedural programming here because it is more natural. |
| 22 |
--- @param integral IntegralExpression |
| 23 |
--- @return Expression|nil |
| 24 |
function IntegralExpression.integrate(integral) |
| 25 |
integral.expression = integral.expression:autosimplify() |
| 26 |
|
| 27 |
if not integral.recursive and #integral.attempts > 0 then |
| 28 |
return Copy(integral:lock(Expression.NIL, true)) |
| 29 |
end |
| 30 |
|
| 31 |
-- print(integral.expression) |
| 32 |
|
| 33 |
local F = IntegralExpression.table(integral) |
| 34 |
if F then return F end |
| 35 |
|
| 36 |
-- If we see the same integrand again, and hasn't been solved already, then the integral can't be solved |
| 37 |
local resultindex = Contains(integral.attempts, integral.expression) |
| 38 |
if resultindex then |
| 39 |
return integral.results[resultindex] |
| 40 |
end |
| 41 |
local newindex = #integral.attempts+1 |
| 42 |
integral.attempts[newindex] = integral.expression |
| 43 |
|
| 44 |
-- print("Evalutaing: " .. tostring(integral.expression)) |
| 45 |
|
| 46 |
F = IntegralExpression.linearproperties(integral) |
| 47 |
if F then |
| 48 |
-- print("Linear Properties") |
| 49 |
integral.results[newindex] = F |
| 50 |
return F |
| 51 |
end |
| 52 |
|
| 53 |
-- local exp = integral.expression |
| 54 |
-- local sym = integral.symbol |
| 55 |
-- local es = integral.enhancedsubstitution |
| 56 |
-- integral.enhancedsubstitution = Integer.zero() |
| 57 |
F = IntegralExpression.substitutionmethod(integral) |
| 58 |
if F then |
| 59 |
-- print("u-Substitution") |
| 60 |
integral.results[newindex] = F |
| 61 |
return F |
| 62 |
end |
| 63 |
-- integral.expression = exp |
| 64 |
-- integral.symbol = sym |
| 65 |
-- integral.enhancedsubstitution = es |
| 66 |
|
| 67 |
|
| 68 |
F = IntegralExpression.rationalfunction(integral) |
| 69 |
if F then |
| 70 |
-- print("Rational Function") |
| 71 |
integral.results[newindex] = F |
| 72 |
return F |
| 73 |
end |
| 74 |
|
| 75 |
F = IntegralExpression.partsmethod(integral) |
| 76 |
if F then |
| 77 |
-- print("Parts") |
| 78 |
integral.results[newindex] = F |
| 79 |
return F |
| 80 |
end |
| 81 |
|
| 82 |
F = IntegralExpression.eulersformula(integral) |
| 83 |
if F then |
| 84 |
-- print("Euler's formula") |
| 85 |
integral.results[newindex] = F |
| 86 |
return F |
| 87 |
end |
| 88 |
|
| 89 |
local expanded = integral.expression:expand() |
| 90 |
if integral.expression ~= expanded then |
| 91 |
integral.expression = expanded |
| 92 |
F = IntegralExpression.integrate(integral) |
| 93 |
if F then |
| 94 |
-- print("Expanded") |
| 95 |
integral.results[newindex] = F |
| 96 |
return F |
| 97 |
end |
| 98 |
end |
| 99 |
|
| 100 |
expanded = (Integer.one()/((Integer.one()/integral.expression):autosimplify():expand())):autosimplify() |
| 101 |
if integral.expression ~= expanded then |
| 102 |
integral.expression = expanded |
| 103 |
F = IntegralExpression.integrate(integral) |
| 104 |
if F then |
| 105 |
-- print("Inverse Expanded") |
| 106 |
integral.results[newindex] = F |
| 107 |
return F |
| 108 |
end |
| 109 |
end |
| 110 |
|
| 111 |
F = IntegralExpression.enhancedsubstitutionmethod(integral) |
| 112 |
if F then |
| 113 |
-- print("Enhanced u-Substitution") |
| 114 |
integral.results[newindex] = F |
| 115 |
return F |
| 116 |
end |
| 117 |
|
| 118 |
return nil |
| 119 |
end |
| 120 |
|
| 121 |
--- A table of basic integrals, returns nil if the integrand isn't in the table. |
| 122 |
--- @param integral IntegralExpression |
| 123 |
--- @return Expression|nil |
| 124 |
function IntegralExpression.table(integral) |
| 125 |
local integrand = integral.expression |
| 126 |
local symbol = integral.symbol |
| 127 |
|
| 128 |
-- Constant integrand rule - int(c, x) = c*x |
| 129 |
if integrand:freeof(symbol) then |
| 130 |
return integrand*symbol |
| 131 |
end |
| 132 |
|
| 133 |
if integrand:type() == SymbolExpression then |
| 134 |
|
| 135 |
-- int(x, x) = x^2/2 |
| 136 |
if integrand == symbol then |
| 137 |
return integrand ^ Integer(2) / Integer(2) |
| 138 |
end |
| 139 |
|
| 140 |
-- Constant integrand rule again |
| 141 |
return integrand*symbol |
| 142 |
end |
| 143 |
|
| 144 |
if integrand:type() == BinaryOperation then |
| 145 |
|
| 146 |
if integrand.operation == BinaryOperation.POW then |
| 147 |
-- int(1/x, x) = ln(x) |
| 148 |
if integrand.expressions[1] == symbol and integrand.expressions[2] == Integer(-1) then |
| 149 |
return LN(symbol) |
| 150 |
end |
| 151 |
|
| 152 |
-- Cavalieri's formula - int(x^n, x) = x^(n+1)/(n+1) |
| 153 |
if integrand.expressions[1] == symbol and integrand.expressions[2]:freeof(symbol) then |
| 154 |
return symbol ^ (integrand.expressions[2] + Integer.one()) / (integrand.expressions[2] + Integer.one()) |
| 155 |
end |
| 156 |
|
| 157 |
-- int(n^x, x) = n^x/ln(n) |
| 158 |
if integrand.expressions[1]:freeof(symbol) and integrand.expressions[2] == symbol then |
| 159 |
return integrand / LN(integrand.expressions[1]) |
| 160 |
end |
| 161 |
|
| 162 |
-- int(csc(x)^2, x) = -cot(x) |
| 163 |
if integrand.expressions[1] == CSC(symbol) and integrand.expressions[2] == Integer(2) then |
| 164 |
return -COT(symbol) |
| 165 |
end |
| 166 |
|
| 167 |
-- int(sec(x)^2, x) = tan(x) |
| 168 |
if integrand.expressions[1] == SEC(symbol) and integrand.expressions[2] == Integer(2) then |
| 169 |
return TAN(symbol) |
| 170 |
end |
| 171 |
end |
| 172 |
|
| 173 |
if integrand.operation == BinaryOperation.MUL and #integrand.expressions == 2 then |
| 174 |
-- int(tan(x)sec(x), x) = sec(x) |
| 175 |
if integrand.expressions[1] == TAN(symbol) and integrand.expressions[2] == SEC(symbol) then |
| 176 |
return SEC(symbol) |
| 177 |
end |
| 178 |
|
| 179 |
-- int(csc(x)cot(x), x) = -csc(x) |
| 180 |
if integrand.expressions[1] == CSC(symbol) and integrand.expressions[2] == COT(symbol) then |
| 181 |
return -CSC(symbol) |
| 182 |
end |
| 183 |
end |
| 184 |
|
| 185 |
return nil |
| 186 |
end |
| 187 |
|
| 188 |
if integrand:type() == Logarithm then |
| 189 |
-- int(log_n(x), x) = (x*ln(x)-x)/ln(n) |
| 190 |
if integrand.base:freeof(symbol) and integrand.expression == symbol then |
| 191 |
return (symbol * LN(symbol) - symbol) / LN(integrand.base) |
| 192 |
end |
| 193 |
|
| 194 |
return nil |
| 195 |
end |
| 196 |
|
| 197 |
if integrand:type() == TrigExpression then |
| 198 |
if integrand == SIN(symbol) then |
| 199 |
return -COS(symbol) |
| 200 |
end |
| 201 |
|
| 202 |
if integrand == COS(symbol) then |
| 203 |
return SIN(symbol) |
| 204 |
end |
| 205 |
|
| 206 |
if integrand == TAN(symbol) then |
| 207 |
return -LN(COS(symbol)) |
| 208 |
end |
| 209 |
|
| 210 |
if integrand == CSC(symbol) then |
| 211 |
return -LN(CSC(symbol)+COT(symbol)) |
| 212 |
end |
| 213 |
|
| 214 |
if integrand == SEC(symbol) then |
| 215 |
return LN(SEC(symbol) + TAN(symbol)) |
| 216 |
end |
| 217 |
|
| 218 |
if integrand == COT(symbol) then |
| 219 |
return LN(SIN(symbol)) |
| 220 |
end |
| 221 |
|
| 222 |
if integrand == ARCSIN(symbol) then |
| 223 |
return symbol*ARCSIN(symbol) + (Integer.one()-symbol^(Integer(2)))^(Integer.one()/Integer(2)) |
| 224 |
end |
| 225 |
|
| 226 |
if integrand == ARCCOS(symbol) then |
| 227 |
return symbol*ARCCOS(symbol) - (Integer.one()-symbol^(Integer(2)))^(Integer.one()/Integer(2)) |
| 228 |
end |
| 229 |
|
| 230 |
if integrand == ARCTAN(symbol) then |
| 231 |
return symbol*ARCTAN(symbol) - (Integer.one()/Integer(2))*LN(Integer.one()+symbol^Integer(2)) |
| 232 |
end |
| 233 |
|
| 234 |
if integrand == ARCCSC(symbol) then |
| 235 |
return symbol*ARCCSC(symbol) + LN(symbol*(Integer.one()+(Integer.one()-symbol^(Integer(-2)))^(Integer.one()/Integer(2)))) |
| 236 |
end |
| 237 |
|
| 238 |
if integrand == ARCSEC(symbol) then |
| 239 |
return symbol*ARCSEC(symbol) - LN(symbol*(Integer.one()+(Integer.one()-symbol^(Integer(-2)))^(Integer.one()/Integer(2)))) |
| 240 |
end |
| 241 |
|
| 242 |
if integrand == ARCCOT(symbol) then |
| 243 |
return symbol*ARCCOT(symbol) + (Integer.one()/Integer(2))*LN(Integer.one()+symbol^Integer(2)) |
| 244 |
end |
| 245 |
end |
| 246 |
|
| 247 |
return nil |
| 248 |
end |
| 249 |
|
| 250 |
--- Uses linearity to break up the integral and integrate each piece. |
| 251 |
--- @param integral IntegralExpression |
| 252 |
--- @return Expression|nil |
| 253 |
function IntegralExpression.linearproperties(integral) |
| 254 |
local expression = integral.expression |
| 255 |
local symbol = integral.symbol |
| 256 |
local es = integral.enhancedsubstitution |
| 257 |
|
| 258 |
if expression:type() == BinaryOperation then |
| 259 |
if expression.operation == BinaryOperation.MUL then |
| 260 |
local freepart = Integer.one() |
| 261 |
local variablepart = Integer.one() |
| 262 |
for _, term in ipairs(expression.expressions) do |
| 263 |
if term:freeof(symbol) then |
| 264 |
freepart = freepart*term |
| 265 |
else |
| 266 |
variablepart = variablepart*term |
| 267 |
end |
| 268 |
end |
| 269 |
if freepart == Integer.one() then |
| 270 |
return nil |
| 271 |
end |
| 272 |
integral.expression = variablepart |
| 273 |
integral.symbol = symbol |
| 274 |
integral.enhancedsubstitution = es |
| 275 |
local F = IntegralExpression.integrate(integral) |
| 276 |
if F then |
| 277 |
return freepart*F |
| 278 |
end |
| 279 |
return nil |
| 280 |
end |
| 281 |
|
| 282 |
if expression.operation == BinaryOperation.ADD then |
| 283 |
local sum = Integer.zero() |
| 284 |
for _, term in ipairs(expression.expressions) do |
| 285 |
integral.expression = term |
| 286 |
integral.symbol = symbol |
| 287 |
integral.enhancedsubstitution = es |
| 288 |
local F = IntegralExpression.integrate(integral) |
| 289 |
if F then |
| 290 |
sum = sum + F |
| 291 |
else |
| 292 |
return nil |
| 293 |
end |
| 294 |
|
| 295 |
end |
| 296 |
return sum |
| 297 |
end |
| 298 |
end |
| 299 |
|
| 300 |
return nil |
| 301 |
end |
| 302 |
|
| 303 |
--- Attempts u-substitutions to evaluate the integral. |
| 304 |
--- @param integral IntegralExpression |
| 305 |
--- @return Expression|nil |
| 306 |
function IntegralExpression.substitutionmethod(integral) |
| 307 |
local expression = integral.expression |
| 308 |
local symbol = integral.symbol |
| 309 |
local es = integral.enhancedsubstitution |
| 310 |
|
| 311 |
local P = IntegralExpression.trialsubstitutions(expression) |
| 312 |
local F = nil |
| 313 |
local i = 1 |
| 314 |
|
| 315 |
while not F and i <= #P do |
| 316 |
local g = P[i] |
| 317 |
if g ~= symbol and not g:freeof(symbol) then |
| 318 |
local subsymbol = SymbolExpression("u") |
| 319 |
if symbol == SymbolExpression("u") then |
| 320 |
subsymbol = SymbolExpression("v") |
| 321 |
end |
| 322 |
local u = (expression / (DerivativeExpression(g, symbol))):autosimplify() |
| 323 |
u = u:substitute({[g]=subsymbol}):autosimplify() |
| 324 |
|
| 325 |
--factor u and cancel like non-constant terms |
| 326 |
u = u:factor():autosimplify() |
| 327 |
|
| 328 |
if u:freeof(symbol) then |
| 329 |
integral.expression = u |
| 330 |
integral.symbol = subsymbol |
| 331 |
integral.enhancedsubstitution = es |
| 332 |
F = IntegralExpression.integrate(integral) |
| 333 |
if F then |
| 334 |
if integral.recursive then |
| 335 |
F = F:substitute({[subsymbol]=g}) |
| 336 |
end |
| 337 |
return F |
| 338 |
end |
| 339 |
end |
| 340 |
end |
| 341 |
i = i + 1 |
| 342 |
end |
| 343 |
|
| 344 |
return F |
| 345 |
end |
| 346 |
|
| 347 |
--- Attempts u-substitutions to evaluate the integral, including solving for the original variable and substituting the result into the expression. |
| 348 |
--- @param integral IntegralExpression |
| 349 |
--- @return Expression|nil |
| 350 |
function IntegralExpression.enhancedsubstitutionmethod(integral) |
| 351 |
local expression = integral.expression |
| 352 |
local symbol = integral.symbol |
| 353 |
local es = integral.enhancedsubstitution |
| 354 |
|
| 355 |
local P = IntegralExpression.trialsubstitutions(expression) |
| 356 |
local F = nil |
| 357 |
local i = 1 |
| 358 |
|
| 359 |
while not F and i <= #P do |
| 360 |
local g = P[i] |
| 361 |
if g ~= symbol and not g:freeof(symbol) then |
| 362 |
local subsymbol = SymbolExpression("u") |
| 363 |
if symbol == SymbolExpression("u") then |
| 364 |
subsymbol = SymbolExpression("v") |
| 365 |
end |
| 366 |
local u = (expression / (DerivativeExpression(g, symbol))):autosimplify() |
| 367 |
u = u:substitute({[g]=subsymbol}):autosimplify() |
| 368 |
|
| 369 |
--factor u and cancel like non-constant terms |
| 370 |
u = u:factor():autosimplify() |
| 371 |
|
| 372 |
if integral.enhancedsubstitution > Integer.zero() then |
| 373 |
local f = Equation(subsymbol, g):solvefor(symbol) |
| 374 |
if f.lhs == symbol then |
| 375 |
u = u:substitute({[symbol]=f.rhs}):autosimplify() |
| 376 |
integral.expression = u |
| 377 |
integral.symbol = subsymbol |
| 378 |
integral.enhancedsubstitution = integral.enhancedsubstitution - Integer.one() |
| 379 |
F = IntegralExpression.integrate(integral) |
| 380 |
if F then |
| 381 |
if integral.recursive then |
| 382 |
F = F:substitute({[subsymbol]=g}) |
| 383 |
end |
| 384 |
return F |
| 385 |
end |
| 386 |
integral.enhancedsubstitution = integral.enhancedsubstitution + Integer.one() |
| 387 |
end |
| 388 |
end |
| 389 |
end |
| 390 |
i = i + 1 |
| 391 |
end |
| 392 |
|
| 393 |
return F |
| 394 |
end |
| 395 |
|
| 396 |
--- Generates a list of possible u-substitutions to attempt |
| 397 |
--- @param expression Expression |
| 398 |
--- @return table<number, Expression> |
| 399 |
function IntegralExpression.trialsubstitutions(expression) |
| 400 |
local substitutions = {} |
| 401 |
|
| 402 |
-- Recursive part - evaluates each term in a product. |
| 403 |
if expression:type() == BinaryOperation and expression.operation == BinaryOperation.MUL then |
| 404 |
substitutions[#substitutions+1] = expression |
| 405 |
for _, term in ipairs(expression.expressions) do |
| 406 |
substitutions = JoinArrays(substitutions, IntegralExpression.trialsubstitutions(term)) |
| 407 |
end |
| 408 |
end |
| 409 |
|
| 410 |
--Recursive part - evaluates each term in a sum. |
| 411 |
if expression:type() == BinaryOperation and expression.operation == BinaryOperation.ADD then |
| 412 |
substitutions[#substitutions+1] = expression |
| 413 |
for _,term in ipairs(expression.expressions) do |
| 414 |
substitutions = JoinArrays(substitutions, IntegralExpression.trialsubstitutions(term)) |
| 415 |
end |
| 416 |
end |
| 417 |
|
| 418 |
-- Function forms and arguments of function forms (includes a recursive part) |
| 419 |
if expression:type() == TrigExpression or expression:type() == Logarithm then |
| 420 |
substitutions[#substitutions+1] = expression |
| 421 |
if not expression.expression:isatomic() then |
| 422 |
substitutions[#substitutions+1] = expression.expression |
| 423 |
end |
| 424 |
substitutions = JoinArrays(substitutions, IntegralExpression.trialsubstitutions(expression.expression)) |
| 425 |
end |
| 426 |
|
| 427 |
-- Bases and exponents of powers |
| 428 |
if expression:type() == BinaryOperation and expression.operation == BinaryOperation.POW then |
| 429 |
substitutions[#substitutions+1] = expression |
| 430 |
-- Atomic expressions are technically valid substitutions, but they won't be useful |
| 431 |
if not expression.expressions[1]:isatomic() then |
| 432 |
--substitutions[#substitutions+1] = expression.expressions[1] |
| 433 |
substitutions = JoinArrays(substitutions, IntegralExpression.trialsubstitutions(expression.expressions[1])) |
| 434 |
end |
| 435 |
if not expression.expressions[2]:isatomic() then |
| 436 |
--substitutions[#substitutions+1] = expression.expressions[2] |
| 437 |
substitutions = JoinArrays(substitutions, IntegralExpression.trialsubstitutions(expression.expressions[2])) |
| 438 |
end |
| 439 |
end |
| 440 |
|
| 441 |
return substitutions |
| 442 |
end |
| 443 |
|
| 444 |
|
| 445 |
--- Uses Lazard, Rioboo, Rothstein, and Trager's method to integrate rational functions. |
| 446 |
--- This is mostly to try to avoid factoring and finding the roots of the full denominator whenever possible. |
| 447 |
--- @param integral IntegralExpression |
| 448 |
--- @return Expression|nil |
| 449 |
function IntegralExpression.rationalfunction(integral) |
| 450 |
local expression = integral.expression |
| 451 |
local symbol = integral.symbol |
| 452 |
local es = integral.enhancedsubstitution |
| 453 |
|
| 454 |
-- Type checking and conversion to polynomial type. |
| 455 |
local f, g, fstat, gstat |
| 456 |
if expression:type() == BinaryOperation and expression.operation == BinaryOperation.POW and expression.expressions[2] == Integer(-1) then |
| 457 |
g, gstat = expression.expressions[1]:topolynomial() |
| 458 |
if not gstat then |
| 459 |
return nil |
| 460 |
end |
| 461 |
f = PolynomialRing({Integer.one()}, g.symbol) |
| 462 |
else |
| 463 |
if expression:type() ~= BinaryOperation or expression.operation ~= BinaryOperation.MUL or expression.expressions[3] then |
| 464 |
return nil |
| 465 |
end |
| 466 |
if expression.expressions[2]:type() == BinaryOperation and expression.expressions[2].operation == BinaryOperation.POW and expression.expressions[2].expressions[2] == Integer(-1) then |
| 467 |
if expression.expressions[1].topolynomial ~=nil and expression.expressions[2].expressions[1].topolynomial ~=nil then |
| 468 |
f, fstat = expression.expressions[1]:topolynomial() |
| 469 |
g, gstat = expression.expressions[2].expressions[1]:topolynomial() |
| 470 |
end |
| 471 |
elseif expression.expressions[1]:type() == BinaryOperation and expression.expressions[1].operation == BinaryOperation.POW and expression.expressions[1].expressions[2] == Integer(-1) then |
| 472 |
if expression.expressions[2].topolynomial ~= nil and expression.expressions[1].expressions[1].topolynomial ~= nil then |
| 473 |
f, fstat = expression.expressions[2]:topolynomial() |
| 474 |
g, gstat = expression.expressions[1].expressions[1]:topolynomial() |
| 475 |
end |
| 476 |
else |
| 477 |
return nil |
| 478 |
end |
| 479 |
|
| 480 |
if not fstat or not gstat or f.symbol ~= symbol.symbol or g.symbol ~= symbol.symbol then |
| 481 |
return nil |
| 482 |
end |
| 483 |
end |
| 484 |
|
| 485 |
-- Explicit handling of degree 1 or less over a binomial. |
| 486 |
do |
| 487 |
local disc = g.coefficients[1]*g.coefficients[1]-Integer(4)*g.coefficients[2]*g.coefficients[0] |
| 488 |
if f.degree <= Integer.one() and g.degree == Integer(2) and disc < Integer.zero() then |
| 489 |
return (f.coefficients[1] * LN(g.coefficients[0] + g.coefficients[1] * symbol + g.coefficients[2] * symbol ^ Integer(2))/(Integer(2) * g.coefficients[2]) + (Integer(2)*f.coefficients[0]*g.coefficients[2] - f.coefficients[1]*g.coefficients[1]) / (g.coefficients[2] * sqrt(Integer(4)*g.coefficients[0]*g.coefficients[2] - g.coefficients[1] ^ Integer(2))) * ARCTAN((Integer(2)*g.coefficients[2]*symbol+g.coefficients[1]) / sqrt(Integer(4)*g.coefficients[0]*g.coefficients[2]-g.coefficients[1] ^ Integer(2)))):autosimplify() |
| 490 |
end |
| 491 |
end |
| 492 |
|
| 493 |
-- If the polynomials are not relatively prime, divides out the common factors. |
| 494 |
local gcd = PolynomialRing.gcd(f, g) |
| 495 |
if gcd ~= Integer.one() then |
| 496 |
f, g = f // gcd, g // gcd |
| 497 |
end |
| 498 |
|
| 499 |
-- Seperates out the polynomial part and rational part and integrates the polynomial part. |
| 500 |
local q, h = f:divremainder(g) |
| 501 |
integral.expression = q |
| 502 |
integral.symbol = symbol |
| 503 |
integral.enhancedsubstitution = es |
| 504 |
U = IntegralExpression.integrate(integral) |
| 505 |
|
| 506 |
if h == Integer.zero() then |
| 507 |
return U |
| 508 |
end |
| 509 |
|
| 510 |
-- Performs partial fraction decomposition into square-free denominators on the rational part. |
| 511 |
local gg = g:squarefreefactorization() |
| 512 |
local pfd = PolynomialRing.partialfractions(h, g, gg) |
| 513 |
|
| 514 |
-- Hermite reduction. |
| 515 |
local V = Integer.zero() |
| 516 |
for _, term in ipairs(pfd.expressions) do |
| 517 |
local i = #term.expressions |
| 518 |
if i > 1 then |
| 519 |
for j = 1, i-1 do |
| 520 |
local n = term.expressions[j].expressions[1] |
| 521 |
local d = term.expressions[j].expressions[2].expressions[1] |
| 522 |
local p = term.expressions[j].expressions[2].expressions[2] |
| 523 |
|
| 524 |
local _, s, t = PolynomialRing.extendedgcd(d, d:derivative()) |
| 525 |
s = s * n |
| 526 |
t = t * n |
| 527 |
V = V - t / ((p-Integer.one()) * BinaryOperation.POWEXP({d, p-Integer.one()})) |
| 528 |
term.expressions[j+1].expressions[1] = term.expressions[j+1].expressions[1] + s + t:derivative() / (p-Integer.one()) |
| 529 |
end |
| 530 |
end |
| 531 |
end |
| 532 |
|
| 533 |
--Lazard-Rioboo-Trager method. |
| 534 |
local W = Integer.zero() |
| 535 |
for _, term in ipairs(pfd.expressions) do |
| 536 |
local a = term.expressions[#term.expressions].expressions[1] |
| 537 |
local b = term.expressions[1].expressions[2].expressions[1] |
| 538 |
local y = a - b:derivative() * PolynomialRing({Integer.zero(), Integer.one()}, "_") |
| 539 |
local r = PolynomialRing.resultant(b, y) |
| 540 |
|
| 541 |
|
| 542 |
local rr = r:squarefreefactorization() |
| 543 |
local remainders = PolynomialRing.monicgcdremainders(b, y) |
| 544 |
for pos, factor in ipairs(rr.expressions) do |
| 545 |
if pos > 1 then |
| 546 |
local re = factor.expressions[1] |
| 547 |
local e = factor.expressions[2] |
| 548 |
local roots = re:roots() |
| 549 |
for _, root in ipairs(roots) do |
| 550 |
local w |
| 551 |
for _, remainder in ipairs(remainders) do |
| 552 |
if remainder.degree == e then |
| 553 |
w = remainder |
| 554 |
break |
| 555 |
end |
| 556 |
end |
| 557 |
W = W + root*LN(w:substitute({[SymbolExpression("_")] = root})) |
| 558 |
end |
| 559 |
end |
| 560 |
end |
| 561 |
end |
| 562 |
|
| 563 |
return U + V + W |
| 564 |
end |
| 565 |
|
| 566 |
|
| 567 |
--- Attempts integration by parts for expressions with a polynomial factor in them. Other product expressions use Euler's formula. |
| 568 |
--- @param integral IntegralExpression |
| 569 |
--- @return Expression|nil |
| 570 |
function IntegralExpression.partsmethod(integral) |
| 571 |
local expression = integral.expression |
| 572 |
local symbol = integral.symbol |
| 573 |
local es = integral.enhancedsubstitution |
| 574 |
|
| 575 |
if expression:type() ~= BinaryOperation or expression.operation ~= BinaryOperation.MUL then |
| 576 |
return |
| 577 |
end |
| 578 |
|
| 579 |
local u |
| 580 |
local vp = Integer.one() |
| 581 |
--looking for ILATE |
| 582 |
for _, exp in ipairs(expression:subexpressions()) do |
| 583 |
if exp:type() == TrigExpression and (exp.name == "arctan" or exp.name == "arccos" or exp.name == "arcsin" or exp.name == "arccot" or exp.name == "arcsec" or exp.name == "arccsc") then |
| 584 |
u = exp |
| 585 |
else |
| 586 |
vp = vp * exp |
| 587 |
end |
| 588 |
end |
| 589 |
|
| 590 |
if not u or u:freeof(symbol) then |
| 591 |
goto skipI |
| 592 |
else |
| 593 |
vp = vp:autosimplify() |
| 594 |
end |
| 595 |
|
| 596 |
--if vp:type() == Logarithm or vp.topolynomial or (vp:type() == TrigExpression and (vp.name == "cos" or vp.name == "sin")) or (vp.operation == BinaryOperation.POW and vp.expressions[1]:freeof(symbol)) then |
| 597 |
if select(2,vp:topolynomial()) then |
| 598 |
integral.expression = vp |
| 599 |
integral.symbol = symbol |
| 600 |
integral.enhancedsubstitution = es |
| 601 |
local v = IntegralExpression.integrate(integral) |
| 602 |
if not v then |
| 603 |
goto skipI |
| 604 |
end |
| 605 |
|
| 606 |
local up = DerivativeExpression(u, symbol):autosimplify() |
| 607 |
|
| 608 |
integral.expression = v*up |
| 609 |
integral.symbol = symbol |
| 610 |
integral.enhancedsubstitution = es |
| 611 |
local vup = IntegralExpression.integrate(integral) |
| 612 |
if not vup then |
| 613 |
goto skipI |
| 614 |
end |
| 615 |
|
| 616 |
local result = u*v - vup |
| 617 |
|
| 618 |
return result:autosimplify() |
| 619 |
end |
| 620 |
::skipI:: |
| 621 |
|
| 622 |
local u |
| 623 |
local vp = Integer.one() |
| 624 |
--looking for LATE |
| 625 |
for _, exp in ipairs(expression:subexpressions()) do |
| 626 |
if exp:type() == Logarithm then |
| 627 |
u = exp |
| 628 |
else |
| 629 |
vp = vp * exp |
| 630 |
end |
| 631 |
end |
| 632 |
|
| 633 |
if not u or u:freeof(symbol) then |
| 634 |
goto skipL |
| 635 |
else |
| 636 |
vp = vp:autosimplify() |
| 637 |
end |
| 638 |
|
| 639 |
--if vp.topolynomial or (vp:type() == TrigExpression and (vp.name == "cos" or vp.name == "sin")) or (vp.operation == BinaryOperation.POW and vp.expressions[1]:freeof(symbol)) then |
| 640 |
if select(2,vp:topolynomial()) then |
| 641 |
integral.expression = vp |
| 642 |
integral.symbol = symbol |
| 643 |
integral.enhancedsubstitution = es |
| 644 |
local v = IntegralExpression.integrate(integral) |
| 645 |
if not v then |
| 646 |
goto skipL |
| 647 |
end |
| 648 |
|
| 649 |
local up = DerivativeExpression(u, symbol):autosimplify() |
| 650 |
|
| 651 |
integral.expression = v*up |
| 652 |
integral.symbol = symbol |
| 653 |
integral.enhancedsubstitution = es |
| 654 |
local vup = IntegralExpression.integrate(integral) |
| 655 |
if not vup then |
| 656 |
goto skipL |
| 657 |
end |
| 658 |
|
| 659 |
local result = u*v - vup |
| 660 |
|
| 661 |
return result:autosimplify() |
| 662 |
end |
| 663 |
::skipL:: |
| 664 |
|
| 665 |
local u |
| 666 |
local vp = Integer.one() |
| 667 |
--looking for ATE |
| 668 |
for _, exp in ipairs(expression:subexpressions()) do |
| 669 |
local _, bool = exp:topolynomial() |
| 670 |
if bool then |
| 671 |
u = exp |
| 672 |
else |
| 673 |
vp = vp * exp |
| 674 |
end |
| 675 |
end |
| 676 |
|
| 677 |
if not u or u:freeof(symbol) then |
| 678 |
return |
| 679 |
else |
| 680 |
vp = vp:autosimplify() |
| 681 |
end |
| 682 |
|
| 683 |
if (vp:type() == TrigExpression and (vp.name == "cos" or vp.name == "sin")) or (vp.operation == BinaryOperation.POW and vp.expressions[1]:freeof(symbol)) then |
| 684 |
local results = {} |
| 685 |
while u ~= Integer.zero() do |
| 686 |
integral.expression = vp |
| 687 |
integral.symbol = symbol |
| 688 |
integral.enhancedsubstitution = es |
| 689 |
local v = IntegralExpression.integrate(integral):unlock():autosimplify() |
| 690 |
if not v then |
| 691 |
return |
| 692 |
end |
| 693 |
local up = DerivativeExpression(u, symbol):autosimplify() |
| 694 |
|
| 695 |
if not integral.recursive then |
| 696 |
return (u*v - IntegralExpression(v*up, symbol):nonrecursive():lock(Expression.NIL, true)):autosimplify() |
| 697 |
end |
| 698 |
|
| 699 |
results[#results+1] = u*v |
| 700 |
u = up |
| 701 |
vp = v |
| 702 |
end |
| 703 |
|
| 704 |
local result = results[#results] |
| 705 |
for i=#results-1,1,-1 do |
| 706 |
result = results[i] - result |
| 707 |
end |
| 708 |
|
| 709 |
return result:autosimplify() |
| 710 |
end |
| 711 |
end |
| 712 |
|
| 713 |
--- Attempts integration using Euler's formula and kind. Alternative for integration by parts for many expressions. |
| 714 |
--- @param integral IntegralExpression |
| 715 |
--- @return Expression|nil |
| 716 |
function IntegralExpression.eulersformula(integral) |
| 717 |
local expression = integral.expression |
| 718 |
local symbol = integral.symbol |
| 719 |
local es = integral.enhancedsubstitution |
| 720 |
|
| 721 |
local new = expression:substitute({[COS(symbol)] = (E^(I*symbol) + E^(-I*symbol))/Integer(2), |
| 722 |
[SIN(symbol)] = (E^(I*symbol) - E^(-I*symbol))/(Integer(2)*I)}) |
| 723 |
|
| 724 |
if new == expression then |
| 725 |
return |
| 726 |
end |
| 727 |
|
| 728 |
integral.expression = new:autosimplify():expand() |
| 729 |
integral.symbol = symbol |
| 730 |
integral.enhancedsubstitution = es |
| 731 |
local complexresult = IntegralExpression.integrate(integral) |
| 732 |
if not complexresult then |
| 733 |
return |
| 734 |
end |
| 735 |
|
| 736 |
-- TODO: Proper complex number conversion methods |
| 737 |
local function converttorectangular(exp) |
| 738 |
exp = exp:expand() |
| 739 |
local results = {} |
| 740 |
for index, sub in ipairs(exp:subexpressions()) do |
| 741 |
results[index] = converttorectangular(sub) |
| 742 |
end |
| 743 |
local converted = exp:setsubexpressions(results) |
| 744 |
|
| 745 |
if converted.operation == BinaryOperation.POW and converted.expressions[1] == E and converted.expressions[2].operation == BinaryOperation.MUL then |
| 746 |
local ipart |
| 747 |
local rest = Integer.one() |
| 748 |
for _, factor in ipairs(converted.expressions[2]:subexpressions()) do |
| 749 |
if factor == I then |
| 750 |
ipart = true |
| 751 |
else |
| 752 |
rest = rest * factor |
| 753 |
end |
| 754 |
end |
| 755 |
if ipart then |
| 756 |
return (COS(rest) + I*SIN(rest)):autosimplify() |
| 757 |
end |
| 758 |
end |
| 759 |
|
| 760 |
return converted |
| 761 |
end |
| 762 |
|
| 763 |
return converttorectangular(complexresult:autosimplify()):expand():autosimplify() |
| 764 |
|
| 765 |
end |
| 766 |
|
| 767 |
---------------------------- |
| 768 |
-- Instance functionality -- |
| 769 |
---------------------------- |
| 770 |
|
| 771 |
--- Creates a new integral operation with the given symbol and expression. |
| 772 |
--- @param expression Expression |
| 773 |
--- @param symbol SymbolExpression |
| 774 |
--- @param lower Expression |
| 775 |
--- @param upper Expression |
| 776 |
function IntegralExpression:new(expression, symbol, lower, upper) |
| 777 |
local o = {} |
| 778 |
local __o = Copy(__ExpressionOperations) |
| 779 |
|
| 780 |
if not symbol or not expression then |
| 781 |
error("Send wrong number of parameters: integrals must have a variable to integrate with respect to and an expression to integrate.") |
| 782 |
end |
| 783 |
|
| 784 |
if lower and not upper then |
| 785 |
error("Send wrong number of parameters: definite integrals must have an upper and a lower bound.") |
| 786 |
end |
| 787 |
|
| 788 |
o.symbol = symbol |
| 789 |
o.expression = Copy(expression) |
| 790 |
o.upper = Copy(upper) |
| 791 |
o.lower = Copy(lower) |
| 792 |
o.recursive = true |
| 793 |
|
| 794 |
o.attempts = {} |
| 795 |
o.results = {} |
| 796 |
o.enhancedsubstitution = IntegralExpression.ENHANCEDSUBSTITUTIONRECURSIONLIMIT |
| 797 |
|
| 798 |
__o.__index = IntegralExpression |
| 799 |
__o.__tostring = function(a) |
| 800 |
if a:isdefinite() then |
| 801 |
return 'int(' .. tostring(a.expression) .. ", " .. tostring(a.symbol) .. ", ".. tostring(a.lower) .. ', ' .. tostring(a.upper) .. ')' |
| 802 |
end |
| 803 |
return 'int(' .. tostring(a.expression) .. ", " .. tostring(a.symbol) .. ')' |
| 804 |
end |
| 805 |
__o.__eq = function(a, b) |
| 806 |
-- This shouldn't be needed, since __eq should only fire if both metamethods have the same function, but for some reason Lua always rungs this anyway |
| 807 |
if not b:type() == IntegralExpression then |
| 808 |
return false |
| 809 |
end |
| 810 |
return a.symbol == b.symbol and a.expression == b.expression and a.upper == b.upper and a.lower == b.lower |
| 811 |
end |
| 812 |
o = setmetatable(o, __o) |
| 813 |
|
| 814 |
return o |
| 815 |
end |
| 816 |
|
| 817 |
--- Returns true if the integral is definite, i.e., has an upper and lower bound. |
| 818 |
--- @return boolean |
| 819 |
function IntegralExpression:isdefinite() |
| 820 |
return self.upper ~= nil |
| 821 |
end |
| 822 |
|
| 823 |
--- Sets the integral to not autosimplify other integral expressions that are produced by the integration process. |
| 824 |
--- THIS METHOD MUTATES THE OBJECT IT IS CALLED ON. |
| 825 |
function IntegralExpression:nonrecursive() |
| 826 |
self.recursive = false |
| 827 |
return self |
| 828 |
end |
| 829 |
|
| 830 |
--- @return Expression |
| 831 |
function IntegralExpression:autosimplify() |
| 832 |
local arg = IntegralExpression(self.expression, self.symbol) |
| 833 |
local integrated = IntegralExpression.integrate(arg) |
| 834 |
|
| 835 |
-- Our expression could not be integrated. |
| 836 |
if not integrated then |
| 837 |
return self |
| 838 |
end |
| 839 |
|
| 840 |
if not self.recursive then |
| 841 |
return integrated:autosimplify():unlock(true) |
| 842 |
end |
| 843 |
|
| 844 |
if self:isdefinite() then |
| 845 |
return (integrated:substitute({[self.symbol]=self.upper}) - integrated:substitute({[self.symbol]=self.lower})):autosimplify() |
| 846 |
end |
| 847 |
|
| 848 |
return integrated:autosimplify() |
| 849 |
end |
| 850 |
|
| 851 |
|
| 852 |
--- @return table<number, Expression> |
| 853 |
function IntegralExpression:subexpressions() |
| 854 |
if self:isdefinite() then |
| 855 |
return {self.expression, self.symbol, self.lower, self.upper} |
| 856 |
end |
| 857 |
|
| 858 |
return {self.expression, self.symbol} |
| 859 |
end |
| 860 |
|
| 861 |
--- @param subexpressions table<number, Expression> |
| 862 |
--- @return IntegralExpression |
| 863 |
function IntegralExpression:setsubexpressions(subexpressions) |
| 864 |
local out = IntegralExpression(subexpressions[1], subexpressions[2], subexpressions[3], subexpressions[4]) |
| 865 |
|
| 866 |
return out; |
| 867 |
end |
| 868 |
|
| 869 |
-- function IntegralExpression:freeof(symbol) |
| 870 |
-- if self:isdefinite() then |
| 871 |
-- return self.expression:freeof(symbol) and self.upper:freeof(symbol) and self.lower:freeof(symbol) |
| 872 |
-- end |
| 873 |
-- return self.expression:freeof(symbol) |
| 874 |
-- end |
| 875 |
|
| 876 |
-- -- Substitutes each expression for a new one. |
| 877 |
-- function IntegralExpression:substitute(map) |
| 878 |
-- for expression, replacement in pairs(map) do |
| 879 |
-- if self == expression then |
| 880 |
-- return replacement |
| 881 |
-- end |
| 882 |
-- end |
| 883 |
-- -- Typically, we only perform substitution on autosimplified expressions, so this won't get called. May give strange results, i.e., |
| 884 |
-- -- substituting and then evaluating the integral may not return the same thing as evaluating the integral and then substituting. |
| 885 |
-- if self:isdefinite() then |
| 886 |
-- return IntegralExpression(self.symbol, self.expression:substitute(map), self.upper:substitute(map), self.lower:substitute(map)) |
| 887 |
-- end |
| 888 |
-- return IntegralExpression(self.symbol, self.expression:substitute(map)) |
| 889 |
-- end |
| 890 |
|
| 891 |
--- @param other Expression |
| 892 |
--- @return boolean |
| 893 |
function IntegralExpression:order(other) |
| 894 |
if other:type() ~= IntegralExpression then |
| 895 |
return false |
| 896 |
end |
| 897 |
|
| 898 |
if self.symbol ~= other.symbol then |
| 899 |
return self.symbol:order(other.symbol) |
| 900 |
end |
| 901 |
|
| 902 |
return self.expression:order(other.expression) |
| 903 |
end |
| 904 |
|
| 905 |
--- @return string |
| 906 |
function IntegralExpression:tolatex() |
| 907 |
if self:isdefinite() then |
| 908 |
return '\\int_{' .. self.lower:tolatex() .. '}^{' .. self.upper:tolatex() .. '}{' .. self.expression:tolatex() .. '\\mathop{d' .. self.symbol:tolatex() .. '}}' |
| 909 |
end |
| 910 |
return '\\int{' .. self.expression:tolatex() .. '\\mathop{d' .. self.symbol:tolatex() .. '}}' |
| 911 |
end |
| 912 |
|
| 913 |
|
| 914 |
----------------- |
| 915 |
-- Inheritance -- |
| 916 |
----------------- |
| 917 |
__IntegralExpression.__index = CompoundExpression |
| 918 |
__IntegralExpression.__call = IntegralExpression.new |
| 919 |
IntegralExpression = setmetatable(IntegralExpression, __IntegralExpression) |
| 920 |
|
| 921 |
---------------------- |
| 922 |
-- Static constants -- |
| 923 |
---------------------- |
| 924 |
INT = function(symbol, expression, lower, upper) |
| 925 |
return IntegralExpression(symbol, expression, lower, upper) |
| 926 |
end |
| 927 |
|
| 928 |
---------------------- |
| 929 |
-- Static constants -- |
| 930 |
---------------------- |
| 931 |
|
| 932 |
-- Limit for the maximum number of full u-subs to attempts for any integral. |
| 933 |
-- This should be low, since integrals are highly unlikely to need more than 1 or 2 u-subs, and gives exponentially worse performance the higher the number is. |
| 934 |
IntegralExpression.ENHANCEDSUBSTITUTIONRECURSIONLIMIT = Integer(2) |