Skip to content

Commit caf35e2

Browse files
committed
RAMMatrices: cleanup indexing params in arrays
* introduce ArrayParamsMap type for clarity * annotate types of the corresp. RAMMatrices fields * remove cartesian indexing since it is not used * rename get_parameter_indices() into array_parameters_map() * use the single pass over the array for performance
1 parent 2803b55 commit caf35e2

2 files changed

Lines changed: 24 additions & 17 deletions

File tree

src/additional_functions/parameters.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ function fill_A_S_M(A, S, M, A_indices, S_indices, M_indices, parameters)
1818
end
1919
end
2020

21-
function get_parameter_indices(parameters, M; linear = true, kwargs...)
22-
M_indices = [findall(x -> (x == par), M) for par in parameters]
23-
24-
if linear
25-
M_indices = cartesian2linear.(M_indices, [M])
21+
# build the map from the index of the parameter to the linear indices
22+
# of this parameter occurences in M
23+
# returns ArrayParamsMap object
24+
function array_parameters_map(parameters::AbstractVector, M::AbstractArray)
25+
params_index = Dict(param => i for (i, param) in enumerate(parameters))
26+
T = Base.eltype(eachindex(M))
27+
res = [Vector{T}() for _ in eachindex(parameters)]
28+
for (i, val) in enumerate(M)
29+
par_ind = get(params_index, val, nothing)
30+
if !isnothing(par_ind)
31+
push!(res[par_ind], i)
32+
end
2633
end
27-
28-
return M_indices
34+
return res
2935
end
3036

3137
function eachindex_lower(M; linear_indices = false, kwargs...)
@@ -49,9 +55,6 @@ function linear2cartesian(ind_lin, dims)
4955
return ind_cart
5056
end
5157

52-
cartesian2linear(ind_cart, A::AbstractArray) = cartesian2linear(ind_cart, size(A))
53-
linear2cartesian(ind_linear, A::AbstractArray) = linear2cartesian(ind_linear, size(A))
54-
5558
function set_constants!(M, M_pre)
5659
for index in eachindex(M)
5760
δ = tryparse(Float64, string(M[index]))

src/frontend/specification/RAMMatrices.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
### Type
33
############################################################################################
44

5+
# map from parameter index to linear indices of matrix/vector positions where it occurs
6+
AbstractArrayParamsMap = AbstractVector{<:AbstractVector{<:Integer}}
7+
ArrayParamsMap = Vector{Vector{Int}}
8+
59
struct RAMMatrices
6-
A_ind::Any
7-
S_ind::Any
8-
F_ind::Any
9-
M_ind::Any
10+
A_ind::ArrayParamsMap
11+
S_ind::ArrayParamsMap
12+
F_ind::Vector{Int}
13+
M_ind::Union{ArrayParamsMap, Nothing}
1014
parameters::Any
1115
colnames::Any
1216
constants::Any
@@ -18,9 +22,9 @@ end
1822
############################################################################################
1923

2024
function RAMMatrices(; A, S, F, M = nothing, parameters, colnames)
21-
A_indices = get_parameter_indices(parameters, A)
22-
S_indices = get_parameter_indices(parameters, S)
23-
isnothing(M) ? M_indices = nothing : M_indices = get_parameter_indices(parameters, M)
25+
A_indices = array_parameters_map(parameters, A)
26+
S_indices = array_parameters_map(parameters, S)
27+
M_indices = !isnothing(M) ? array_parameters_map(parameters, M) : nothing
2428
F_indices = findall([any(isone.(col)) for col in eachcol(F)])
2529
constants = get_RAMConstants(A, S, M)
2630
return RAMMatrices(

0 commit comments

Comments
 (0)