|
| 1 | + |
| 2 | +expand_all(x::Num) = Num(expand_all(x.val)) |
| 3 | +_apply_termwise(f, x::Num) = wrap(_apply_termwise(f, unwrap(x))) |
| 4 | + |
| 5 | +"Expands using SymbolicUtils.expand and expand_exp_power (changes exp(x)^n to exp(x*n)" |
| 6 | +function expand_all(x) |
| 7 | + result = Postwalk(expand_exp_power)(SymbolicUtils.expand(x)) |
| 8 | + return isnothing(result) ? x : result |
| 9 | +end |
| 10 | +expand_all(x::Complex{Num}) = expand_all(x.re) + im * expand_all(x.im) |
| 11 | + |
| 12 | +"Apply a function f on every member of a sum or a product" |
| 13 | +function _apply_termwise(f, x::BasicSymbolic) |
| 14 | + @compactified x::BasicSymbolic begin |
| 15 | + Add => sum([f(arg) for arg in arguments(x)]) |
| 16 | + Mul => prod([f(arg) for arg in arguments(x)]) |
| 17 | + Div => _apply_termwise(f, x.num) / _apply_termwise(f, x.den) |
| 18 | + _ => f(x) |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +simplify_complex(x::Complex) = isequal(x.im, 0) ? x.re : x.re + im * x.im |
| 23 | +simplify_complex(x) = x |
| 24 | +function simplify_complex(x::BasicSymbolic) |
| 25 | + @compactified x::BasicSymbolic begin |
| 26 | + Add => _apply_termwise(simplify_complex, x) |
| 27 | + Mul => _apply_termwise(simplify_complex, x) |
| 28 | + Div => _apply_termwise(simplify_complex, x) |
| 29 | + _ => x |
| 30 | + end |
| 31 | +end |
| 32 | + |
| 33 | +""" |
| 34 | +$(TYPEDSIGNATURES) |
| 35 | +
|
| 36 | +Perform substitutions in `rules` on `x`. |
| 37 | +`include_derivatives=true` also includes all derivatives of the variables of the keys of `rules`. |
| 38 | +""" |
| 39 | +subtype = Union{Num,Equation,BasicSymbolic} |
| 40 | +function substitute_all(x::subtype, rules::Dict; include_derivatives=true) |
| 41 | + if include_derivatives |
| 42 | + rules = merge( |
| 43 | + rules, |
| 44 | + Dict([Differential(var) => Differential(rules[var]) for var in keys(rules)]), |
| 45 | + ) |
| 46 | + end |
| 47 | + return substitute(x, rules) |
| 48 | +end |
| 49 | +"Variable substitution - dictionary" |
| 50 | +function substitute_all(dict::Dict, rules::Dict)::Dict |
| 51 | + new_keys = substitute_all.(keys(dict), rules) |
| 52 | + new_values = substitute_all.(values(dict), rules) |
| 53 | + return Dict(zip(new_keys, new_values)) |
| 54 | +end |
| 55 | +Collections = Union{Dict,Pair,Vector,OrderedDict} |
| 56 | +substitute_all(v::AbstractArray, rules) = [substitute_all(x, rules) for x in v] |
| 57 | +substitute_all(x::subtype, rules::Collections) = substitute_all(x, Dict(rules)) |
| 58 | +# Collections = Union{Dict,OrderedDict} |
| 59 | +# function substitute_all(x, rules::Collections; include_derivatives=true) |
| 60 | +# if include_derivatives |
| 61 | +# rules = merge( |
| 62 | +# rules, |
| 63 | +# Dict([Differential(var) => Differential(rules[var]) for var in keys(rules)]), |
| 64 | +# ) |
| 65 | +# end |
| 66 | +# return substitute(x, rules) |
| 67 | +# end |
| 68 | +# "Variable substitution - dictionary" |
| 69 | +# function substitute_all(dict::Dict, rules::Dict)::Dict |
| 70 | +# new_keys = substitute_all.(keys(dict), rules) |
| 71 | +# new_values = substitute_all.(values(dict), rules) |
| 72 | +# return Dict(zip(new_keys, new_values)) |
| 73 | +# end |
| 74 | +# substitute_all(v::AbstractArray, rules::Collections) = [substitute_all(x, rules) for x in v] |
| 75 | + |
| 76 | +get_independent(x::Num, t::Num) = get_independent(x.val, t) |
| 77 | +function get_independent(x::Complex{Num}, t::Num) |
| 78 | + return get_independent(x.re, t) + im * get_independent(x.im, t) |
| 79 | +end |
| 80 | +get_independent(v::Vector{Num}, t::Num) = [get_independent(el, t) for el in v] |
| 81 | +get_independent(x, t::Num) = x |
| 82 | + |
| 83 | +function get_independent(x::BasicSymbolic, t::Num) |
| 84 | + @compactified x::BasicSymbolic begin |
| 85 | + Add => sum([get_independent(arg, t) for arg in arguments(x)]) |
| 86 | + Mul => prod([get_independent(arg, t) for arg in arguments(x)]) |
| 87 | + Div => !is_function(x.den, t) ? get_independent(x.num, t) / x.den : 0 |
| 88 | + Pow => !is_function(x.base, t) && !is_function(x.exp, t) ? x : 0 |
| 89 | + Term => !is_function(x, t) ? x : 0 |
| 90 | + Sym => !is_function(x, t) ? x : 0 |
| 91 | + _ => x |
| 92 | + end |
| 93 | +end |
| 94 | + |
| 95 | +"Return all the terms contained in `x`" |
| 96 | +get_all_terms(x::Num) = unique(_get_all_terms(Symbolics.expand(x).val)) |
| 97 | +function get_all_terms(x::Equation) |
| 98 | + return unique(cat(get_all_terms(Num(x.lhs)), get_all_terms(Num(x.rhs)); dims=1)) |
| 99 | +end |
| 100 | +function _get_all_terms(x::BasicSymbolic) |
| 101 | + @compactified x::BasicSymbolic begin |
| 102 | + Add => vcat([_get_all_terms(term) for term in SymbolicUtils.arguments(x)]...) |
| 103 | + Mul => Num.(SymbolicUtils.arguments(x)) |
| 104 | + Div => Num.([_get_all_terms(x.num)..., _get_all_terms(x.den)...]) |
| 105 | + _ => Num(x) |
| 106 | + end |
| 107 | +end |
| 108 | +_get_all_terms(x) = Num(x) |
| 109 | + |
| 110 | +function is_harmonic(x::Num, t::Num)::Bool |
| 111 | + all_terms = get_all_terms(x) |
| 112 | + t_terms = setdiff(all_terms, get_independent(all_terms, t)) |
| 113 | + isempty(t_terms) && return true |
| 114 | + trigs = is_trig.(t_terms) |
| 115 | + |
| 116 | + if !prod(trigs) |
| 117 | + return false |
| 118 | + else |
| 119 | + powers = [max_power(first(term.val.arguments), t) for term in t_terms[trigs]] |
| 120 | + return all(isone, powers) |
| 121 | + end |
| 122 | +end |
| 123 | + |
| 124 | +is_harmonic(x::Equation, t::Num) = is_harmonic(x.lhs, t) && is_harmonic(x.rhs, t) |
| 125 | +is_harmonic(x, t) = is_harmonic(Num(x), Num(t)) |
| 126 | + |
| 127 | +"Return true if `f` is a function of `var`." |
| 128 | +is_function(f, var) = any(isequal.(get_variables(f), var)) |
0 commit comments