Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cumtrapz/cumtrapz! #24

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FLOWMath"
uuid = "6cb5d3fb-0fe8-4cc2-bd89-9fe0b19a99d3"
authors = ["Andrew Ning <[email protected]>"]
version = "0.4.0"
version = "0.4.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
25 changes: 25 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ z = trapz(x, y)
trapz
```

There is also `cumtrapz` which returns the cumulative integral of `y` with respect to `x`

```@example cumtrapz
using FLOWMath: cumtrapz
x = range(0.0, stop=pi+1e-15, step=pi/100)
y = sin.(x)
z = cumtrapz(x, y)
extrema(z .- ((-cos.(x)) .- (-cos(x[1])))) # compare to the exact answer -cos(x) - -cos(0)
```

and a version `cumtrapz!` that writes the result to the first argument

```@example cumtrapz!
using FLOWMath: cumtrapz!
x = range(0.0, stop=pi+1e-15, step=pi/100)
y = sin.(x)
z = similar(y)
cumtrapz!(z, x, y)
extrema(z .- ((-cos.(x)) .- (-cos(x[1])))) # compare to the exact answer -cos(x) - -cos(0)
```

```@docs
cumtrapz
cumtrapz!
```
## Root Finding

### Brent's Method (1D functions)
Expand Down
2 changes: 1 addition & 1 deletion src/FLOWMath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export dot_cs_safe
export atan_cs_safe

include("quadrature.jl")
export trapz
export trapz, cumtrapz, cumtrapz!

include("roots.jl")
export brent
Expand Down
23 changes: 23 additions & 0 deletions src/quadrature.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,26 @@ function trapz(x, y)
end
return integral
end


"""
cumtrapz(x, y)

Cumulatively integrate `y` w.r.t `x` using the trapezoidal method, returning an array the same size as `x` and `y`.
"""
cumtrapz(x, y) = cumtrapz!(similar(y), x, y)


"""
cumtrapz!(integral, x, y)

Cumulatively integrate `y` w.r.t `x` using the trapezoidal method, writing the result to `integral`.
"""
function cumtrapz!(integral, x, y)
integral[begin] = 0
for i in eachindex(x, y, integral)[begin+1:end]
integral[i] = integral[i-1] + (x[i] - x[i-1])*0.5*(y[i] + y[i-1])
end

return integral
end
36 changes: 35 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,41 @@ y = sin.(x)
z = trapz(x, y)
@test isapprox(z, 1.999835503887444, atol=1e-15)

# -------------------------
# ------ cumtrapz --------
# tests from the matlab cumtrapz docs
# https://www.mathworks.com/help/matlab/ref/cumtrapz.html
y = Float64[1, 4, 9, 16, 25]
x = 1:length(y)
@test all(cumtrapz(x, y) .≈ [0.0, 2.5, 9.0, 21.5, 42.0])
out = similar(y)
cumtrapz!(out, x, y)
@test all(cumtrapz(x, y) .≈ out)

x = range(0, pi; length=6)
y = sin.(x)
@test all(isapprox.(cumtrapz(x, y), [0, 0.1847, 0.6681, 1.2657, 1.7491, 1.9338]; atol=1e-4))
out = similar(y)
cumtrapz!(out, x, y)
@test all(cumtrapz(x, y) .≈ out)

x = [1, 2.5, 7, 10]
y = [5.2, 7.7, 9.6, 13.2]
@test all(cumtrapz(x, y) .≈ [0, 9.6750, 48.6000, 82.8000])
out = similar(y)
cumtrapz!(out, x, y)
@test all(cumtrapz(x, y) .≈ out)

y = [4.8, 7.0, 10.5, 14.5]
@test all(cumtrapz(x, y) .≈ [0, 8.8500, 48.2250, 85.7250])
out = similar(y)
cumtrapz!(out, x, y)
@test all(cumtrapz(x, y) .≈ out)

y = [4.9, 6.5, 10.2, 13.8]
@test all(cumtrapz(x, y) .≈ [0, 8.5500, 46.1250, 82.1250])
out = similar(y)
cumtrapz!(out, x, y)
@test all(cumtrapz(x, y) .≈ out)

# ------ Brent's method ------

Expand Down