Symbolics.jl
Symbolics.jl copied to clipboard
Terms [non]cancellation in series expansions
Hello,
MNWE for a set of simple test functions. Taylor-Maclaurin series expansion in powers of t with multivariate coefficients. My expectation was that substituting t => -t in these series and adding the resulting series to the original series would result in cancellation of the odd power terms in t: t, t^3. However, that's not the case. Insight in how to do this would be appreciated.
# test_term_cancellation.jl
using Symbolics
begin
@variables θ, p, t
@variables m, l, g
# Mulivariate polynomials in powers of t: 1, t, t^2, t^3
T_θ₊ = θ + (p*t) / (m*(l^2)) + ((-1//2)*g*(t^2)*sin(θ)) / l + (-g*p*(t^3)*cos(θ)) / (6m*(l^3))
T_p₊ = p + (g*(p^2)*(t^3)*sin(θ)) / (6m*(l^3)) + ((-1//2)*g*p*(t^2)*cos(θ)) / l + (1//6)*m*(g^2)*(t^3)*cos(θ)*sin(θ) - g*l*m*t*sin(θ)
@show T_θ₊
@show T_p₊
println()
# Reverse the sign of t: t => -t
# and thus the signs of t, t^3 terms
T_θ₋ = expand(substitute(T_θ₊, Dict(t => -t)))
T_p₋ = expand(substitute(T_p₊, Dict(t => -t)))
@show T_θ₋
@show T_p₋
println()
# Expectation: upon addition all the t and t^3 terms would cancel. They don't
T_θₛ = T_θ₊ + T_θ₋
T_pₛ = T_p₊ + T_p₋
@show T_θₛ
@show T_pₛ
println()
# Expectation: simplify would cancel all the t and t^3 terms. It doesn't
T_θₛ_smp = simplify(T_θ₊ + T_θ₋; expand=true, simplify_fractions=false)
T_pₛ_smp = simplify(T_p₊ + T_p₋; expand=true, simplify_fractions=false)
@show T_θₛ_smp
@show T_pₛ_smp
println()
# ALternate method? Extract the coefficients and retain only the even ones
# Test: throws an error
# Symbolics.coeff(T_θₛ_smp, t)
# Also throws an error
# Symbolics.polynomial_coeffs(T_θₛ_smp, t)
end
This cancels the odd coefficients:
julia> expand(simplify(T_θ₊ + T_θ₋))
((2//1)*l*θ - g*(t^2)*sin(θ)) / l