Mercurial > repos > public > sbplib_julia
changeset 1780:8ecdc5bb46be feature/grids/manifolds
Allow mixed types in constructors for HyperBox and Simplex
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 16 Sep 2024 08:58:12 +0200 |
parents | 2a8a2b52a112 |
children | a73838c9ef94 |
files | src/Grids/manifolds.jl test/Grids/manifolds_test.jl |
diffstat | 2 files changed, 12 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Grids/manifolds.jl Mon Sep 16 08:33:43 2024 +0200 +++ b/src/Grids/manifolds.jl Mon Sep 16 08:58:12 2024 +0200 @@ -42,7 +42,8 @@ end function HyperBox(a,b) - T = SVector{length(a)} + ET = promote_type(eltype(a),eltype(b)) + T = SVector{length(a),ET} HyperBox(convert(T,a), convert(T,b)) end @@ -62,7 +63,12 @@ verticies::NTuple{NV,SVector{D,T}} end -Simplex(verticies::Vararg{AbstractArray}) = Simplex(Tuple(SVector(v...) for v ∈ verticies)) +function Simplex(verticies::Vararg{AbstractArray}) + ET = mapreduce(eltype,promote_type,verticies) + T = SVector{length(verticies[1]),ET} + + return Simplex(Tuple(convert(T,v) for v ∈ verticies)) +end verticies(s::Simplex) = s.verticies
--- a/test/Grids/manifolds_test.jl Mon Sep 16 08:33:43 2024 +0200 +++ b/test/Grids/manifolds_test.jl Mon Sep 16 08:58:12 2024 +0200 @@ -30,6 +30,8 @@ @test HyperBox{<:Any, 2} <: ParameterSpace{2} @test HyperBox([1,1], [2,2]) isa HyperBox{Int, 2} + @test HyperBox([1,2], [1.,2.]) isa HyperBox{Float64,2} + @test limits(HyperBox([1,2], [3,4])) == ([1,2], [3,4]) @test limits(HyperBox([1,2], [3,4]), 1) == (1,3) @test limits(HyperBox([1,2], [3,4]), 2) == (2,4) @@ -49,6 +51,8 @@ @test Simplex([1,2], [3,4]) isa Simplex{Int, 2} @test Simplex([1,2,3], [4,5,6],[1,1,1]) isa Simplex{Int, 3} + @test Simplex([1,2], [3.,4.]) isa Simplex{Float64, 2} + @test verticies(Simplex([1,2], [3,4])) == ([1,2], [3,4]) @test unittriangle() isa Simplex{Float64,2}