Skip to content

Commit b4f821e

Browse files
author
Brad Carman
committed
new promote_to_concrete
1 parent cee69ed commit b4f821e

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

src/utils.jl

+25-31
Original file line numberDiff line numberDiff line change
@@ -657,46 +657,40 @@ function promote_to_concrete(vs; tofloat = true, use_union = true)
657657
end
658658
T = eltype(vs)
659659
if Base.isconcretetype(T) && (!tofloat || T === float(T)) # nothing to do
660-
vs
660+
return vs
661661
else
662662
sym_vs = filter(x -> SymbolicUtils.issym(x) || SymbolicUtils.istree(x), vs)
663663
isempty(sym_vs) || throw_missingvars_in_sys(sym_vs)
664-
C = typeof(first(vs))
665-
I = Int8
666-
has_int = false
667-
has_array = false
668-
has_bool = false
669-
array_T = nothing
664+
665+
C = nothing
670666
for v in vs
671-
if v isa AbstractArray
672-
has_array = true
673-
array_T = typeof(v)
674-
end
675-
E = eltype(v)
676-
C = promote_type(C, E)
677-
if E <: Integer
678-
has_int = true
679-
I = promote_type(I, E)
667+
E = typeof(v)
668+
if E <: Number
669+
if tofloat
670+
E = float(E)
671+
end
680672
end
681-
if E <: Bool
682-
has_bool = true
673+
if use_union
674+
if C === nothing
675+
C = E
676+
else
677+
C = Union{C, E}
678+
end
679+
else
680+
C = E
683681
end
684682
end
685-
if tofloat && !has_array
686-
C = float(C)
687-
elseif has_array || (use_union && has_int && C !== I)
688-
if has_array
689-
C = Union{C, array_T}
690-
end
691-
if has_int
692-
C = Union{C, I}
693-
end
694-
if has_bool
695-
C = Union{C, Bool}
683+
684+
y = similar(vs, C)
685+
for i in eachindex(vs)
686+
if (vs[i] isa Number) & tofloat
687+
y[i] = float(vs[i])
688+
else
689+
y[i] = vs[i]
696690
end
697-
return copyto!(similar(vs, C), vs)
698691
end
699-
convert.(C, vs)
692+
693+
return y
700694
end
701695
end
702696

test/split_parameters.jl

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ using ModelingToolkit, Test
22
using ModelingToolkitStandardLibrary.Blocks
33
using OrdinaryDiffEq
44

5+
6+
x = [1, 2.0, false, [1,2,3], Parameter(1.0)]
7+
8+
y = ModelingToolkit.promote_to_concrete(x)
9+
@test eltype(y) == Union{Float64, Parameter{Float64}, Vector{Int64}}
10+
11+
y = ModelingToolkit.promote_to_concrete(x; tofloat=false)
12+
@test eltype(y) == Union{Bool, Float64, Int64, Parameter{Float64}, Vector{Int64}}
13+
14+
15+
x = [1, 2.0, false, [1,2,3]]
16+
y = ModelingToolkit.promote_to_concrete(x)
17+
@test eltype(y) == Union{Float64, Vector{Int64}}
18+
19+
x = Any[1, 2.0, false]
20+
y = ModelingToolkit.promote_to_concrete(x; tofloat=false)
21+
@test eltype(y) == Union{Bool, Float64, Int64}
22+
23+
y = ModelingToolkit.promote_to_concrete(x; use_union=false)
24+
@test eltype(y) == Float64
25+
26+
x = Float16[1., 2., 3.]
27+
y = ModelingToolkit.promote_to_concrete(x)
28+
@test eltype(y) == Float16
29+
30+
31+
32+
533
# ------------------------ Mixed Single Values and Vector
634

735
dt = 4e-4

0 commit comments

Comments
 (0)