Skip to content

Commit

Permalink
Merge pull request #18 from MeasureTransport/dannys4/addMultiIndexTests
Browse files Browse the repository at this point in the history
Adding multiindex tests
  • Loading branch information
dannys4 authored Feb 18, 2024
2 parents b114ca9 + 6151e84 commit 71ab4ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/MParT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ MultiIndex(A::AbstractVector{<:Int}) = MultiIndex(StdVector(Cuint.(A)))

# MultiIndex-related exports
export MultiIndex, MultiIndexSet, FixedMultiIndexSet
export Fix, CreateTotalOrder, Size
export Fix, CreateTotalOrder, Size, addto!
# ParameterizedFunctionBase-related exports
export CoeffMap, SetCoeffs, numCoeffs, inputDim, outputDim
export Evaluate, CoeffGrad, Gradient
Expand Down
45 changes: 45 additions & 0 deletions test/multiindex.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# MultiIndex test

multis = [0 1; 2 0]
msetFromArray = MultiIndexSet(multis)

dim = 3
power = 4
msetTotalOrder = CreateTotalOrder(dim, power)

function test_max_degrees()
@test Int.(MParT.MaxOrders(msetFromArray)) == [2, 1]
@test Int.(MParT.MaxOrders(msetTotalOrder)) == [4, 4, 4]
end

function test_reduced_margin()
msetTotalOrder2 = CreateTotalOrder(dim, power+1)
msetTotalOrder_rm = MParT.ReducedMargin(msetTotalOrder)
@test length(msetTotalOrder_rm) == Size(msetTotalOrder2) - Size(msetTotalOrder)
@test all([sum(midx) == power+1 for midx in msetTotalOrder_rm])
msetTotalOrder_rm_dim = MParT.ReducedMarginDim(msetTotalOrder,2)
@test all([sum(midx) == power+1 for midx in msetTotalOrder_rm_dim])
@test length(msetTotalOrder_rm_dim) < length(msetTotalOrder_rm)

# Tests weird memory bug where ReducedDim returns objects that get garbage collected
dims_add = [1, 2]
# Without the additional constructor, the MultiIndex objects get gc'd
msetTotalOrder_rm_dim = reduce(vcat, MultiIndex.(MParT.ReducedMarginDim(msetTotalOrder, d)) for d in dims_add)
mset_strings = string.(string.(msetTotalOrder_rm_dim))
# Just test that things don't crash, i.e. we didn't segfault
@test mset_strings[1] isa AbstractString
end

function test_at()
@test msetFromArray[1] == MultiIndex([0, 1])
@test msetFromArray[2] == MultiIndex([2, 0])

@test msetTotalOrder[1] == MultiIndex([0, 0, 0])
@test msetTotalOrder[2] == MultiIndex([0, 0, 1])
last_idx = Size(msetTotalOrder)
@test msetTotalOrder[last_idx] == MultiIndex([4, 0, 0])
end

test_max_degrees()
test_reduced_margin()
test_at()
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ using Test, MParT
# @testset "Affine, Composed Map" begin # This test doesn't work due to weird constructor
# include("mapTypeTest.jl")
# end
@testset "MultiIndex" begin
include("multiindex.jl")
end
@testset "Monotone Least Squares" begin
include("MLS.jl")
end
Expand Down

0 comments on commit 71ab4ef

Please sign in to comment.