|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +This tutorial will help you get started with SymbolicIntegration.jl, covering installation, basic usage, and method selection. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Install SymbolicIntegration.jl from the Julia package registry: |
| 8 | + |
| 9 | +```julia |
| 10 | +using Pkg |
| 11 | +Pkg.add("SymbolicIntegration") |
| 12 | +``` |
| 13 | + |
| 14 | +## Basic Setup |
| 15 | + |
| 16 | +To start using the package, import both SymbolicIntegration.jl and Symbolics.jl: |
| 17 | + |
| 18 | +```julia |
| 19 | +using SymbolicIntegration, Symbolics |
| 20 | + |
| 21 | +# Define symbolic variables |
| 22 | +@variables x a b |
| 23 | +``` |
| 24 | + |
| 25 | +## Your First Integration |
| 26 | + |
| 27 | +Let's start with some basic examples: |
| 28 | + |
| 29 | +```julia |
| 30 | +# Basic polynomial |
| 31 | +integrate(x^2, x) |
| 32 | +# Returns: (1//3)*(x^3) |
| 33 | + |
| 34 | +# Rational function |
| 35 | +integrate(1/(x^2 + 1), x) |
| 36 | +# Returns: atan(x) |
| 37 | + |
| 38 | +# More complex rational function |
| 39 | +f = (x^3 + x^2 + x + 2)/(x^4 + 3*x^2 + 2) |
| 40 | +integrate(f, x) |
| 41 | +# Returns: (1//2)*log(2 + x^2) + atan(x) |
| 42 | +``` |
| 43 | + |
| 44 | +## Transcendental Functions |
| 45 | + |
| 46 | +SymbolicIntegration.jl handles exponential, logarithmic, and trigonometric functions: |
| 47 | + |
| 48 | +```julia |
| 49 | +# Exponential functions |
| 50 | +integrate(exp(x), x) # Returns: exp(x) |
| 51 | +integrate(x * exp(x), x) # Returns: -exp(x) + x*exp(x) |
| 52 | + |
| 53 | +# Logarithmic functions |
| 54 | +integrate(log(x), x) # Returns: -x + x*log(x) |
| 55 | +integrate(1/(x*log(x)), x) # Returns: log(log(x)) |
| 56 | + |
| 57 | +# Trigonometric functions |
| 58 | +integrate(sin(x), x) # Returns: -cos(x) |
| 59 | +integrate(cos(x), x) # Returns: sin(x) |
| 60 | +``` |
| 61 | + |
| 62 | +## Method Selection |
| 63 | + |
| 64 | +SymbolicIntegration.jl provides two main integration methods that you can select explicitly: |
| 65 | + |
| 66 | +### Automatic Method Selection (Default) |
| 67 | + |
| 68 | +By default, the package automatically chooses the best method: |
| 69 | + |
| 70 | +```julia |
| 71 | +f = (x^2 + 1)/(x^3 + x) |
| 72 | +integrate(f, x) # Automatically selects the best method |
| 73 | +``` |
| 74 | + |
| 75 | +### RischMethod |
| 76 | + |
| 77 | +The Risch algorithm is based on Bronstein's "Symbolic Integration I" and provides exact algorithmic integration: |
| 78 | + |
| 79 | +```julia |
| 80 | +# Basic usage |
| 81 | +integrate(f, x, RischMethod()) |
| 82 | + |
| 83 | +# With algebraic closure (for complex roots) |
| 84 | +integrate(f, x, RischMethod(use_algebraic_closure=true)) |
| 85 | + |
| 86 | +# With error catching |
| 87 | +integrate(f, x, RischMethod(catch_errors=true)) |
| 88 | +``` |
| 89 | + |
| 90 | +### RuleBasedMethod |
| 91 | + |
| 92 | +The rule-based method uses pattern matching with a large database of integration rules: |
| 93 | + |
| 94 | +```julia |
| 95 | +# Basic usage |
| 96 | +integrate(f, x, RuleBasedMethod()) |
| 97 | + |
| 98 | +# Verbose output (shows integration rules applied) |
| 99 | +integrate(f, x, RuleBasedMethod(verbose=true)) |
| 100 | + |
| 101 | +# Allow gamma functions in results |
| 102 | +integrate(f, x, RuleBasedMethod(use_gamma=true)) |
| 103 | +``` |
| 104 | + |
| 105 | +### Method Comparison Example |
| 106 | + |
| 107 | +```julia |
| 108 | +# Compare methods on the same function |
| 109 | +f = sin(x)^2 * cos(x) |
| 110 | + |
| 111 | +# Risch method |
| 112 | +result_risch = integrate(f, x, RischMethod()) |
| 113 | + |
| 114 | +# Rule-based method |
| 115 | +result_rules = integrate(f, x, RuleBasedMethod(verbose=false)) |
| 116 | + |
| 117 | +println("Risch result: ", result_risch) |
| 118 | +println("Rule-based result: ", result_rules) |
| 119 | +``` |
| 120 | + |
| 121 | +## Error Handling |
| 122 | + |
| 123 | +Sometimes integration may fail or return unevaluated expressions: |
| 124 | + |
| 125 | +```julia |
| 126 | +# This integral has no elementary solution |
| 127 | +difficult_f = exp(x^2) |
| 128 | + |
| 129 | +try |
| 130 | + result = integrate(difficult_f, x) |
| 131 | + println("Result: ", result) |
| 132 | +catch e |
| 133 | + println("Integration failed: ", e) |
| 134 | +end |
| 135 | + |
| 136 | +# Use error catching options |
| 137 | +safe_result = integrate(difficult_f, x, RischMethod(catch_errors=true)) |
| 138 | +``` |
| 139 | + |
| 140 | +## Verification |
| 141 | + |
| 142 | +You can verify integration results by differentiation: |
| 143 | + |
| 144 | +```julia |
| 145 | +using Symbolics: Differential |
| 146 | + |
| 147 | +# Original function |
| 148 | +f = x^3 + 2*x^2 + x + 1 |
| 149 | + |
| 150 | +# Integrate |
| 151 | +F = integrate(f, x) |
| 152 | + |
| 153 | +# Differentiate to verify |
| 154 | +D = Differential(x) |
| 155 | +f_check = expand_derivatives(D(F)) |
| 156 | + |
| 157 | +# Check if they match |
| 158 | +simplify(f - f_check) == 0 # Should be true |
| 159 | +``` |
| 160 | + |
| 161 | + |
| 162 | +## Common Patterns |
| 163 | + |
| 164 | +### Integration by Parts (Automatic) |
| 165 | +```julia |
| 166 | +# The algorithms handle integration by parts automatically |
| 167 | +integrate(x * exp(x), x) # Returns: -exp(x) + x*exp(x) |
| 168 | +integrate(x * log(x), x) # Uses integration by parts internally |
| 169 | +``` |
| 170 | + |
| 171 | +### Substitution (Automatic) |
| 172 | +```julia |
| 173 | +# Chain rule substitutions are handled automatically |
| 174 | +integrate(sin(x^2) * x, x) # Uses u = x^2 substitution |
| 175 | +integrate(exp(x^2) * x, x) # Returns: (1//2)*exp(x^2) |
| 176 | +``` |
| 177 | + |
| 178 | +### Partial Fractions (Automatic) |
| 179 | +```julia |
| 180 | +# Rational functions use partial fraction decomposition |
| 181 | +integrate((x^2 + 1)/((x-1)*(x+1)), x) # Automatically decomposes |
| 182 | +``` |
| 183 | + |
| 184 | +## Next Steps |
| 185 | + |
| 186 | +Now that you understand the basics: |
| 187 | + |
| 188 | +1. Explore the [Integration Methods](../methods/overview.md) documentation for detailed algorithm explanations |
| 189 | +2. Check the [API Reference](../api.md) for complete function documentation |
| 190 | +3. See the [Risch Algorithm](../methods/risch.md) details for advanced usage |
| 191 | + |
| 192 | +## Common Issues |
| 193 | + |
| 194 | +**Issue**: Integration takes too long |
| 195 | +**Solution**: Try simplifying the expression first or switch methods: |
| 196 | +```julia |
| 197 | +f_simplified = simplify(f) |
| 198 | +result = integrate(f_simplified, x) |
| 199 | +# Or try the other method |
| 200 | +result = integrate(f, x, RuleBasedMethod()) |
| 201 | +``` |
| 202 | + |
| 203 | +**Issue**: Method fails with error |
| 204 | +**Solution**: Try the alternative method or enable error catching: |
| 205 | +```julia |
| 206 | +result = integrate(f, x, RischMethod(catch_errors=true)) |
| 207 | +``` |
| 208 | + |
| 209 | +**Issue**: Unexpected result form |
| 210 | +**Solution**: Simplify the result: |
| 211 | +```julia |
| 212 | +result = integrate(f, x) |
| 213 | +simplified_result = simplify(result) |
| 214 | +``` |
0 commit comments