|
| 1 | +using Integrals, Test, FastGaussQuadrature |
| 2 | + |
| 3 | +#= |
| 4 | +f = (x, p) -> x^3 * sin(5x) |
| 5 | +n = 250 |
| 6 | +nodes, weights = gausslegendre(n) |
| 7 | +I = gauss_legendre(f, nothing, -1, 1, nodes, weights) |
| 8 | +@test I ≈ 2 / (625) * (69sin(5) - 95cos(5)) |
| 9 | +I = Integrals.composite_gauss_legendre(f, nothing, -1, 1, nodes, weights, 2) |
| 10 | +@test I ≈ 2 / (625) * (69sin(5) - 95cos(5)) |
| 11 | +
|
| 12 | +f = (x, p) -> (x + p) * abs(x) |
| 13 | +n = 100 |
| 14 | +nodes, weights = gausslegendre(n) |
| 15 | +I = Integrals.gauss_legendre(f, 0.0, -2, 2, nodes, weights) |
| 16 | +Ic = Integrals.composite_gauss_legendre(f, 6, -2, 2, nodes, weights, 5) |
| 17 | +@inferred Integrals.gauss_legendre(f, 0.0, -2, 2, nodes, weights) |
| 18 | +@inferred Integrals.composite_gauss_legendre(f, 6, -2, 2, nodes, weights, 5) |
| 19 | +@test I≈0.0 atol=1e-6 |
| 20 | +@test Ic≈24 rtol=1e-4 |
| 21 | +=# |
| 22 | + |
| 23 | +alg = GaussLegendre() |
| 24 | +n = 250 |
| 25 | +nd, wt = gausslegendre(n) |
| 26 | +@test alg.nodes == nd |
| 27 | +@test alg.weights == wt |
| 28 | +@test alg.subintervals == 1 |
| 29 | +alg = GaussLegendre(n = 125, subintervals = 3) |
| 30 | +n = 125 |
| 31 | +nd, wt = gausslegendre(n) |
| 32 | +@test alg.nodes == nd |
| 33 | +@test alg.weights == wt |
| 34 | +@test alg.subintervals == 3 |
| 35 | +@test typeof(alg).parameters[1] |
| 36 | +nd, wt = gausslegendre(275) |
| 37 | +alg = GaussLegendre(nodes = nd, weights = wt) |
| 38 | +@test !typeof(alg).parameters[1] |
| 39 | +@test alg.nodes == nd |
| 40 | +@test alg.weights == wt |
| 41 | +@test alg.subintervals == 1 |
| 42 | +alg = GaussLegendre(nodes = nd, weights = wt, subintervals = 20) |
| 43 | +@test typeof(alg).parameters[1] |
| 44 | +@test alg.nodes == nd |
| 45 | +@test alg.weights == wt |
| 46 | +@test alg.subintervals == 20 |
| 47 | + |
| 48 | +f = (x, p) -> 5x + sin(x) - p * exp(x) |
| 49 | +prob = IntegralProblem(f, -5, 3, 3.3) |
| 50 | +alg = GaussLegendre() |
| 51 | +sol = solve(prob, alg) |
| 52 | +@test isnothing(sol.chi) |
| 53 | +@test sol.alg === alg |
| 54 | +@test sol.prob === prob |
| 55 | +@test isnothing(sol.resid) |
| 56 | +@test SciMLBase.successful_retcode(sol) |
| 57 | +@test sol.u ≈ -exp(3) * 3.3 + 3.3 / exp(5) - 40 + cos(5) - cos(3) |
| 58 | +alg = GaussLegendre(subintervals = 7) |
| 59 | +sol = solve(prob, alg) |
| 60 | +@test sol.u ≈ -exp(3) * 3.3 + 3.3 / exp(5) - 40 + cos(5) - cos(3) |
| 61 | + |
| 62 | +f = (x, p) -> exp(-x^2) |
| 63 | +prob = IntegralProblem(f, 0.0, Inf) |
| 64 | +alg = GaussLegendre() |
| 65 | +sol = solve(prob, alg) |
| 66 | +@test sol.u ≈ sqrt(π)/2 |
| 67 | +alg = GaussLegendre(subintervals=1) |
| 68 | +@test sol.u ≈ sqrt(π)/2 |
| 69 | +alg = GaussLegendre(subintervals=17) |
| 70 | +@test sol.u ≈ sqrt(π)/2 |
| 71 | + |
| 72 | +prob = IntegralProblem(f, -Inf, Inf) |
| 73 | +alg = GaussLegendre() |
| 74 | +sol = solve(prob, alg) |
| 75 | +@test sol.u ≈ sqrt(π) |
| 76 | +alg = GaussLegendre(subintervals=1) |
| 77 | +@test sol.u ≈ sqrt(π) |
| 78 | +alg = GaussLegendre(subintervals=17) |
| 79 | +@test sol.u ≈ sqrt(π) |
| 80 | + |
| 81 | +prob = IntegralProblem(f, -Inf, 0.0) |
| 82 | +alg = GaussLegendre() |
| 83 | +sol = solve(prob, alg) |
| 84 | +@test sol.u ≈ sqrt(π)/2 |
| 85 | +alg = GaussLegendre(subintervals=1) |
| 86 | +@test sol.u ≈ sqrt(π)/2 |
| 87 | +alg = GaussLegendre(subintervals=17) |
| 88 | +@test sol.u ≈ sqrt(π)/2 |
| 89 | + |
| 90 | +# Make sure broadcasting correctly handles the argument p |
| 91 | +f = (x, p) -> 1 + x + x^p[1] - cos(x*p[2]) + exp(x)*p[3] |
| 92 | +p = [0.3, 1.3, -0.5] |
| 93 | +prob = IntegralProblem(f, 2, 6.3, p) |
| 94 | +alg = GaussLegendre() |
| 95 | +sol = solve(prob, alg) |
| 96 | +@test sol.u ≈ -240.25235266303063249920743158729 |
| 97 | +alg = GaussLegendre(n = 500, subintervals = 17) |
| 98 | +sol = solve(prob, alg) |
| 99 | +@test sol.u ≈ -240.25235266303063249920743158729 |
0 commit comments