|
1 | 1 | using Symbolics
|
2 | 2 |
|
| 3 | +const SAFE_ALTERNATIVES = Dict(log => slog, sqrt => ssqrt, cbrt => scbrt) |
| 4 | + |
3 | 5 | function isolate(lhs, var; warns=true, conditions=[])
|
4 | 6 | rhs = Vector{Any}([0])
|
5 | 7 | original_lhs = deepcopy(lhs)
|
@@ -90,57 +92,24 @@ function isolate(lhs, var; warns=true, conditions=[])
|
90 | 92 | lhs = args[2]
|
91 | 93 | rhs = map(sol -> term(/, term(slog, sol), term(slog, args[1])), rhs)
|
92 | 94 | end
|
93 |
| - |
94 |
| - elseif oper === (log) || oper === (slog) |
95 |
| - lhs = args[1] |
96 |
| - rhs = map(sol -> term(^, Base.MathConstants.e, sol), rhs) |
97 |
| - push!(conditions, (args[1], >)) |
98 |
| - |
99 |
| - elseif oper === (log2) |
100 |
| - lhs = args[1] |
101 |
| - rhs = map(sol -> term(^, 2, sol), rhs) |
102 |
| - push!(conditions, (args[1], >)) |
103 |
| - |
104 |
| - elseif oper === (log10) |
105 |
| - lhs = args[1] |
106 |
| - rhs = map(sol -> term(^, 10, sol), rhs) |
107 |
| - push!(conditions, (args[1], >)) |
108 |
| - |
109 |
| - elseif oper === (sqrt) |
110 |
| - lhs = args[1] |
111 |
| - append!(conditions, [(r, >=) for r in rhs]) |
112 |
| - rhs = map(sol -> term(^, sol, 2), rhs) |
113 |
| - |
114 |
| - elseif oper === (cbrt) |
115 |
| - lhs = args[1] |
116 |
| - rhs = map(sol -> term(^, sol, 3), rhs) |
117 |
| - |
118 |
| - elseif oper === (sin) || oper === (cos) || oper === (tan) |
119 |
| - rev_oper = Dict(sin => asin, cos => acos, tan => atan) |
120 |
| - lhs = args[1] |
121 |
| - # make this global somehow so the user doesnt need to declare it on his own |
122 |
| - new_var = gensym() |
123 |
| - new_var = (@variables $new_var)[1] |
124 |
| - rhs = map( |
125 |
| - sol -> term(rev_oper[oper], sol) + |
126 |
| - term(*, Base.MathConstants.pi, new_var), |
127 |
| - rhs) |
128 |
| - @info string(new_var) * " ϵ" * " Ζ" |
129 |
| - |
130 |
| - elseif oper === (asin) |
131 |
| - lhs = args[1] |
132 |
| - rhs = map(sol -> term(sin, sol), rhs) |
133 |
| - |
134 |
| - elseif oper === (acos) |
135 |
| - lhs = args[1] |
136 |
| - rhs = map(sol -> term(cos, sol), rhs) |
137 |
| - |
138 |
| - elseif oper === (atan) |
| 95 | + elseif has_left_inverse(oper) |
139 | 96 | lhs = args[1]
|
140 |
| - rhs = map(sol -> term(tan, sol), rhs) |
141 |
| - elseif oper === (exp) |
142 |
| - lhs = args[1] |
143 |
| - rhs = map(sol -> term(slog, sol), rhs) |
| 97 | + ia_conditions!(oper, lhs, rhs, conditions) |
| 98 | + invop = left_inverse(oper) |
| 99 | + invop = get(SAFE_ALTERNATIVES, invop, invop) |
| 100 | + if is_periodic(oper) |
| 101 | + # make this global somehow so the user doesnt need to declare it on his own |
| 102 | + new_var = gensym() |
| 103 | + new_var = (@variables $new_var)[1] |
| 104 | + period = fundamental_period(oper) |
| 105 | + rhs = map( |
| 106 | + sol -> term(invop, sol) + |
| 107 | + term(*, period, new_var), |
| 108 | + rhs) |
| 109 | + @info string(new_var) * " ϵ" * " Ζ" |
| 110 | + else |
| 111 | + rhs = map(sol -> term(invop, sol), rhs) |
| 112 | + end |
144 | 113 | end
|
145 | 114 |
|
146 | 115 | lhs = simplify(lhs)
|
@@ -256,6 +225,16 @@ julia> RootFinding.ia_solve(expr, x)
|
256 | 225 | -2 + π*2var"##230" + asin((1//2)*(-1 + RootFinding.ssqrt(-39)))
|
257 | 226 | -2 + π*2var"##234" + asin((1//2)*(-1 - RootFinding.ssqrt(-39)))
|
258 | 227 | ```
|
| 228 | +
|
| 229 | +All transcendental functions for which `left_inverse` is defined are supported. |
| 230 | +To enable `ia_solve` to handle custom transcendental functions, define an inverse or |
| 231 | +left inverse. If the function is periodic, `is_periodic` and `fundamental_period` must |
| 232 | +be defined. If the function imposes certain conditions on its input or output (for |
| 233 | +example, `log` requires that its input be positive) define `ia_conditions!`. |
| 234 | +
|
| 235 | +See also: [`left_inverse`](@ref), [`inverse`](@ref), [`is_periodic`](@ref), |
| 236 | +[`fundamental_period`](@ref), [`ia_conditions!`](@ref). |
| 237 | +
|
259 | 238 | # References
|
260 | 239 | [^1]: [R. W. Hamming, Coding and Information Theory, ScienceDirect, 1980](https://www.sciencedirect.com/science/article/pii/S0747717189800070).
|
261 | 240 | """
|
|
0 commit comments