Skip to content

Commit

Permalink
Merge pull request #51 from ggebbie/50-runtests-breaks-due-to-unitful…
Browse files Browse the repository at this point in the history
…linearalgebraldiv-problem

50 runtests breaks due to unitfullinearalgebraldiv problem
  • Loading branch information
b-r-hamilton authored Oct 23, 2023
2 parents cabf403 + 1a04e96 commit 01ea5fe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
23 changes: 17 additions & 6 deletions src/BLUEs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,22 @@ end
Solve underdetermined problem
"""
function solve(up::UnderdeterminedProblem)

if up.y isa DimArray
y = UnitfulMatrix(ustrip.(vec(up.y)), unit.(vec(up.y)))
else
y = up.y
end

if ismissing(up.x₀)
n = up.y
n = y
else
if up.x₀ isa DimArray
x₀ = UnitfulMatrix(up.x₀[:])
x₀ = UnitfulMatrix(ustrip.(vec(up.x₀)), unit.(vec(up.x₀)))
else
x₀ = up.x₀
end
n = up.y - up.E*x₀

n = y - up.E*x₀
end
Cxy = up.Cxx*transpose(up.E)
Cyy = up.E*Cxy + up.Cnn
Expand Down Expand Up @@ -374,7 +380,12 @@ end
Cost function contribution from observations
"""
function datacost( x̃::Union{Estimate,DimEstimate}, p::Union{OverdeterminedProblem,UnderdeterminedProblem})
n = p.y - p.E*.v
if p.y isa DimArray
y = UnitfulMatrix(ustrip.(vec(p.y)), unit.(vec(p.y)))
else
y = p.y
end
n = y - p.E*.v
if typeof(p) == UnderdeterminedProblem
Cnn⁻¹ = inv(p.Cnn) # not possible for NamedTuple
elseif typeof(p) == OverdeterminedProblem
Expand All @@ -388,7 +399,7 @@ end
"""
function controlcost( x̃::Union{Estimate,DimEstimate}, p::Union{OverdeterminedProblem,UnderdeterminedProblem})
if p.x₀ isa DimArray
Δx =.v - UnitfulMatrix(vec(p.x₀))
Δx =.v - UnitfulMatrix(ustrip.(vec(p.x₀)), unit.(vec(p.x₀)))
else
Δx =.v - p.x₀
end
Expand Down
32 changes: 19 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ include("test_functions.jl")
end

@testset "left-uniform problem with prior info" begin
y = UnitfulMatrix([-1.9permil])
y = UnitfulMatrix([-1.], [permil])
σₙ = 0.2permil
a = -0.24permil*K^-1
γδ = 1.0permil^-2 # to keep units correct, have two γ (tapering) variables
Expand Down Expand Up @@ -320,29 +320,31 @@ include("test_functions.jl")
@test first(E*UnitfulMatrix(vec(x₀))) .== first(predict(x₀))

# Julia doesn't have method to make scalar into vector
!(y isa AbstractVector) && (y = [y])
!(y isa AbstractVector) && (y = UnitfulMatrix(ustrip.([y]), unit.([y])))

# Does E matrix work properly?
= E*UnitfulMatrix(vec(x))
for jj in eachindex(y)
xvec = UnitfulMatrix(ustrip.(vec(x)), unit.(vec(x)))
= E*xvec
for jj in eachindex(vec(y))
@test isapprox(vec(y)[jj],vec(ỹ)[jj])
end
= E\y
for jj in eachindex(y)
@test isapprox(vec(y)[jj],vec(E*x̂)[jj])
yvec = UnitfulMatrix(ustrip.(vec(y)), unit.(vec(y))) #convert from DA
= E\yvec
for jj in eachindex(vec(y))
@test isapprox(vec(y)[jj],getindexqty(E*x̂, jj))
end

# now in a position to use BLUEs to solve
σₙ = 0.01
σₓ = 100.0

Cnn = Diagonal(fill(σₙ^2,length(y)),unit.(y),unit.(y).^-1)
Cnn = Diagonal(fill(σₙ^2,length(y)),unitrange(yvec),unitrange(yvec).^-1)
Cxx = Diagonal(fill(σₓ^2,length(x₀)),vec(unit.(x₀)),vec(unit.(x₀)).^-1)
problem = UnderdeterminedProblem(UnitfulMatrix(y),E,Cnn,Cxx,x₀)
problem = UnderdeterminedProblem(y,E,Cnn,Cxx,x₀)

# when x₀ is a DimArray, then x̃ is a DimEstimate
= solve(problem) # ::DimEstimate
@test within(y[1],vec((E*x̃).v)[1],3σₙ) # within 3-sigma
@test within(getindexqty(yvec, 1),vec((E*x̃).v)[1],3σₙ) # within 3-sigma
@test cost(x̃,problem) < 5e-2 # no noise in ob
@test cost(x̃, problem) == datacost(x̃, problem) + controlcost(x̃, problem)

Expand Down Expand Up @@ -385,10 +387,12 @@ include("test_functions.jl")
y = predict(x)

# test compatibility
@test first(E*UnitfulMatrix(vec(x₀))) .== first(predict(x₀))
x₀vec = UnitfulMatrix(ustrip.(vec(x₀)), unit.(vec(x₀)))
@test first(E*x₀vec) .== first(predict(x₀))

# Does E matrix work properly?
= E*UnitfulMatrix(vec(x))
xvec = UnitfulMatrix(ustrip.(vec(x)), unit.(vec(x)))
= E*UnitfulMatrix(vec(xvec))
for jj in eachindex(y)
@test isapprox(vec(y)[jj],vec(ỹ)[jj])
end
Expand All @@ -405,7 +409,9 @@ include("test_functions.jl")
σₓ = 10.0
Cnn = Diagonal(fill(σₙ^2,length(y)),vec(unit.(y)),vec(unit.(y).^-1))
Cxx = Diagonal(fill(σₓ^2,length(x₀)),vec(unit.(x₀)),vec(unit.(x₀)).^-1)
problem = UnderdeterminedProblem(UnitfulMatrix(vec(y)),E,Cnn,Cxx,x₀)

yvec = UnitfulMatrix(ustrip.(vec(y)), unit.(vec(y)))
problem = UnderdeterminedProblem(yvec,E,Cnn,Cxx,x₀)

# when x₀ is a DimArray, then x̃ is a DimEstimate
= solve(problem)
Expand Down

0 comments on commit 01ea5fe

Please sign in to comment.