Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
biplab37 committed Oct 6, 2023
1 parent 37433b2 commit 7dadef0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.1.0"
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterTools = "35a29f4d-8980-5a13-9543-d66fff28ecb8"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
IntervalRootFinding = "d2bf35a9-74e0-55ec-b149-d360ff49b807"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
1 change: 1 addition & 0 deletions src/UsefulFunctions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module UsefulFunctions

using LinearAlgebra, IntervalArithmetic, IntervalRootFinding
using ForwardDiff

"""
DiracDelta(input::Float64,δ::Float64 = 1e-3)
Expand Down
39 changes: 33 additions & 6 deletions src/root_finding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,40 @@ end
Finds the zero of a function `f` inside a given interval (`a`,`b`) using interval arithmetic.
"""
function interval_roots(f::Function, a::Number, b::Number, tol::Number=1e-5)
res = IntervalRootFinding.roots(f, a .. b, Newton, tol)
root = Float64[]
for r in res
mid = (r.interval.lo + r.interval.hi) / 2
push!(root, mid)
res = IntervalRootFinding.roots(f, a..b)
if length(res) == 0
@warn "No roots found in the given interval"
return nothing
elseif length(res) == 1
return (res[1].interval.lo + res[1].interval.hi) / 2
else
root = Float64[]
for r in res
mid = (r.interval.lo + r.interval.hi) / 2
push!(root, mid)
end
return root
end
return root
end

# function interval_roots_arg(f::Function, a::Number, b::Number, arg; tol=1e-5)
# deriv = x -> derivative(y->f(y,first(arg)),x)
# initial_roots = interval_roots(x -> f(x, first(arg)), deriv, a, b, tol=tol)
# if initial_roots == nothing
# throw("Choose a different interval or chage the initial value of the argument.")
# elseif length(initial_roots) == 1
# result = zeros(length(arg))
# result[1] = initial_roots
# for i in 2:length(arg)
# result[i] = interval_roots(x->(f(x,arg[i])),intial_roots)
# end
# else
# for i in 2:length(initial_roots)
# if abs(initial_roots[i] - initial_roots[i - 1]) < tol
# return initial_roots[i]
# end
# end
# end
# end

export bisection, NewtonMethod, NewtonRaphson, brent, broyden, interval_roots
6 changes: 5 additions & 1 deletion src/separable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ f(p,p') = ∑ᵢ gᵢ(p)gᵢ(p') where gᵢ are the terms returned by this funct
- `Λ::Float64`: Momentum cutoff.
- `n::Int64`: Number of terms in approximation to be returned.
"""
function terms(f::Function, Λ::Float64, n::Int64)
function terms(f::Function, n::Int64, Λ::Float64)
points = collect(range(0, Λ, length=n))
func_list = []
cur_F(x, y) = f(x, y)
Expand All @@ -21,6 +21,10 @@ function terms(f::Function, Λ::Float64, n::Int64)
return red_arg_list(func_list, points)
end

function terms(f::Function, n::Int64)
return nothing
end

## Helper functions
function next_func(func::Function, p0)
g(x, y) = func(x, y) - func(x, p0) * func(p0, y) / func(p0, p0)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end
@test brent(sq, 0.0, 2.0) 0.0 atol = 1e-5
end
@testset "Broyden" begin
@test broyden(f, df, x0) 1.0 atol = 1e-5
@test broyden(f, df, x1) 1.0 atol = 1e-5
@test broyden(x -> [x[1]^2 - 2, x[2]^2 - 4], x -> [2x[1] 0; 0 2x[2]], [1.0, 3.0]) [2, 2]
end
end
Expand Down

0 comments on commit 7dadef0

Please sign in to comment.