Skip to content

Commit c168049

Browse files
committed
added documentation
1 parent 752a2cb commit c168049

File tree

1 file changed

+120
-42
lines changed

1 file changed

+120
-42
lines changed

src/methods.jl

Lines changed: 120 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ end
2929
RuleBasedMethod <: AbstractIntegrationMethod
3030
3131
- `use_gamma::Bool`: Whether to catch and handle algorithm errors gracefully (default: true)
32+
- `verbose::Bool`: Wheter to print or not integration rules applied (default: true)
3233
"""
3334
struct RuleBasedMethod <: AbstractIntegrationMethod
3435
use_gamma::Bool
@@ -39,11 +40,100 @@ struct RuleBasedMethod <: AbstractIntegrationMethod
3940
end
4041
end
4142

43+
44+
45+
46+
47+
48+
49+
50+
"""
51+
integrate(f, x)
52+
53+
Compute the symbolic integral of expression `f` with respect to variable `x`
54+
using all aviable methods.
55+
56+
# Arguments
57+
- `f`: Symbolic expression to integrate (Symbolics.Num)
58+
- `x`: Integration variable (Symbolics.Num)
59+
60+
# Examples
61+
```julia
62+
julia> using SymbolicIntegration, Symbolics
63+
julia> @variables x
64+
julia> integrate(2x)
65+
x^2
66+
67+
julia> integrate(sqrt(x))
68+
┌ Warning: NotImplementedError: integrand contains unsupported expression sqrt(x)
69+
└ @ SymbolicIntegration ~/.julia/dev/SymbolicIntegration.jl_official/src/methods/risch/frontend.jl:826
70+
71+
> RischMethod failed returning ∫(sqrt(x), x)
72+
> Trying with RuleBasedMethod...
73+
74+
┌-------Applied rule 1_1_1_1_2 on ∫(sqrt(x), x)
75+
| ∫(x ^ m, x) => if
76+
| !(contains_var(m, x)) &&
77+
| !(eq(m, -1))
78+
| x ^ (m + 1) / (m + 1)
79+
└-------with result: (2//3)*(x^(3//2))
80+
(2//3)*(x^(3//2))
81+
82+
julia> integrate(abs(x))
83+
┌ Warning: NotImplementedError: integrand contains unsupported expression abs(x)
84+
└ @ SymbolicIntegration ~/.julia/dev/SymbolicIntegration.jl_official/src/methods/risch/frontend.jl:826
85+
86+
> RischMethod failed returning ∫(abs(x), x)
87+
> Trying with RuleBasedMethod...
88+
89+
No rule found for ∫(abs(x), x)
90+
91+
> RuleBasedMethod failed returning ∫(abs(x), x)
92+
> Sorry we cannot integrate this expression :(
93+
94+
```
95+
"""
96+
function integrate(f::Symbolics.Num, x::Symbolics.Num; kwargs...)
97+
result = integrate_risch(f.val, x.val; kwargs...)
98+
!contains_int(result) && return result
99+
100+
printstyled("\n > RischMethod(use_algebraic_closure=false, catch_errors=true) failed, returning $result \n";color=:red)
101+
printstyled(" > Trying with RuleBasedMethod(use_gamma=false, verbose=true)...\n\n"; color=:red)
102+
result = integrate_rule_based(f, x; kwargs...)
103+
!contains_int(result) && return result
104+
105+
printstyled(" > RuleBasedMethod(use_gamma=false, verbose=true) failed, returning $result \n";color=:red)
106+
printstyled(" > Sorry we cannot integrate this expression :(\n";color=:red)
107+
end
108+
109+
"""
110+
integrate(f, method)
111+
112+
If f contains only one symbolic variable, computes the integral of f with
113+
respect to that variable, with the specified method, or tries all aviable
114+
methods if not specified.
115+
"""
116+
function integrate(f::Symbolics.Num, method=nothing; kwargs...)
117+
vars = Symbolics.get_variables(f)
118+
if length(vars) > 1
119+
@warn "Multiple symbolic variables detect. Please pass the integration variable to the `integrate` function as second argument."
120+
return nothing
121+
elseif length(vars) == 1
122+
integration_variable = vars[1]
123+
else
124+
@warn "No integration variable provided"
125+
return nothing
126+
end
127+
128+
method===nothing && return integrate(f, Num(integration_variable); kwargs...)
129+
return integrate(f, Num(integration_variable), method; kwargs...)
130+
end
131+
42132
"""
43133
integrate(f, x, method::AbstractIntegrationMethod=RischMethod(); kwargs...)
44134
45135
Compute the symbolic integral of expression `f` with respect to variable `x`
46-
using the specified integration method.
136+
using Risch integration method.
47137
48138
# Arguments
49139
- `f`: Symbolic expression to integrate (Symbolics.Num)
@@ -58,12 +148,6 @@ using the specified integration method.
58148
59149
# Examples
60150
```julia
61-
using SymbolicIntegration, Symbolics
62-
@variables x
63-
64-
# Using default Risch method
65-
integrate(x^2, x) # (1//3)*(x^3)
66-
67151
# Explicit method with options
68152
integrate(1/(x^2 + 1), x, RischMethod(use_algebraic_closure=true)) # atan(x)
69153
@@ -92,57 +176,51 @@ using rule based method.
92176
- `x`: Integration variable (Symbolics.Num)
93177
- `method`: Integration method to use
94178
95-
# Keyword Arguments
96-
- `verbose`: to print or not the rules applied to solve the integral
97-
- `use_gamma`: to use or not the gamma function in integration results
98-
99179
# Returns
100180
- Symbolic expression representing the antiderivative (Symbolics.Num) (the +c is omitted)
101181
182+
# Examples
183+
```julia
184+
julia> integrate(1/sqrt(1 + x), x, RuleBasedMethod())
185+
┌-------Applied rule 1_1_2_1_33 (change of variables):
186+
| ∫((a + b * v ^ n) ^ p, x) => if
187+
| !(contains_var(a, b, n, p, x)) &&
188+
| (
189+
| linear(v, x) &&
190+
| v !== x
191+
| )
192+
| (1 / ext_coeff(v, x, 1)) * substitute(∫{(a + b * x ^ n) ^ p}dx, x => v)
193+
└-------with result: ∫1 / (u^(1//2)) du where u = 1 + x
194+
┌-------Applied rule 1_1_1_1_2 on ∫(1 / (x^(1//2)), x)
195+
| ∫(x ^ m, x) => if
196+
| !(contains_var(m, x)) &&
197+
| m !== -1
198+
| x ^ (m + 1) / (m + 1)
199+
└-------with result: (2//1)*(x^(1//2))
200+
(2//1)*sqrt(1 + x)
201+
202+
julia> rbm = RuleBasedMethod(verbose=false)
203+
julia> integrate(1/sqrt(1 + x), x, rbm)
204+
205+
(2//1)*sqrt(1 + x)
206+
```
102207
"""
103208
function integrate(f::Symbolics.Num, x::Symbolics.Num, method::RuleBasedMethod; kwargs...)
104209
return integrate_rule_based(f, x;
105210
verbose=method.verbose, use_gamma=method.use_gamma, kwargs...)
106211
end
107212

108-
# If no method tries them both
109-
function integrate(f::Symbolics.Num, x::Symbolics.Num; kwargs...)
110-
result = integrate_risch(f.val, x.val; kwargs...)
111-
!contains_int(result) && return result
112-
113-
printstyled("\n > RischMethod(use_algebraic_closure=false, catch_errors=true) failed, returning $result \n";color=:red)
114-
printstyled(" > Trying with RuleBasedMethod(use_gamma=false, verbose=true)...\n\n"; color=:red)
115-
result = integrate_rule_based(f, x; kwargs...)
116-
!contains_int(result) && return result
117-
118-
printstyled(" > RuleBasedMethod(use_gamma=false, verbose=true) failed, returning $result \n";color=:red)
119-
printstyled(" > Sorry we cannot integrate this expression :(\n";color=:red)
120-
end
121-
122-
# If no integration variable provided
123-
function integrate(f::Symbolics.Num, method=nothing; kwargs...)
124-
vars = Symbolics.get_variables(f)
125-
if length(vars) > 1
126-
@warn "Multiple symbolic variables detect. Please pass the integration variable to the `integrate` function as second argument."
127-
return nothing
128-
elseif length(vars) == 1
129-
integration_variable = vars[1]
130-
else
131-
@warn "No integration variable provided"
132-
return nothing
133-
end
134-
135-
method===nothing && return integrate(f, Num(integration_variable); kwargs...)
136-
return integrate(f, Num(integration_variable), method; kwargs...)
137-
end
138-
139213
function integrate(;kwargs...)
140214
@warn "No integrand provided. Please provide one like this: `integrate(x^2 + 3x + 2)`"
141215
end
142216

143217
# integrate_rule_based(integrand::Number, x::Symbolics.Num; kwargs...) = integrand*x
144218

145219

220+
221+
222+
223+
146224
"""
147225
method_supports_rational(method::RischMethod)
148226

0 commit comments

Comments
 (0)