@@ -86,15 +86,19 @@ function check_constants(M)
8686 return false
8787end
8888
89- function get_matrix_derivative (M_indices, parameters, n_long)
90- ∇M = [
91- sparsevec (M_indices[i], ones (length (M_indices[i])), n_long) for
92- i in 1 : length (parameters)
93- ]
94-
95- ∇M = reduce (hcat, ∇M)
96-
97- return ∇M
89+ # construct length(M)×length(parameters) sparse matrix of 1s at the positions,
90+ # where the corresponding parameter occurs in the M matrix
91+ function matrix_gradient (M_indices:: ArrayParamsMap , M_length:: Integer )
92+ rowval = reduce (vcat, M_indices)
93+ colptr =
94+ pushfirst! (accumulate ((ptr, M_ind) -> ptr + length (M_ind), M_indices, init = 1 ), 1 )
95+ return SparseMatrixCSC (
96+ M_length,
97+ length (M_indices),
98+ colptr,
99+ rowval,
100+ ones (length (rowval)),
101+ )
98102end
99103
100104# fill M with parameters
@@ -111,97 +115,23 @@ function fill_matrix!(
111115 return M
112116end
113117
114- function get_partition (A_indices, S_indices)
115- n_par = length (A_indices)
116-
117- first_A = " a"
118- first_S = " a"
119- last_A = " a"
120- last_S = " a"
121-
122- for i in 1 : n_par
123- if length (A_indices[i]) != 0
124- first_A = i
125- break
126- end
127- end
128-
129- for i in 1 : n_par
130- if length (S_indices[i]) != 0
131- first_S = i
132- break
133- end
134- end
135-
136- for i in n_par + 1 .- (1 : n_par)
137- if length (A_indices[i]) != 0
138- last_A = i
139- break
140- end
141- end
142-
143- for i in n_par + 1 .- (1 : n_par)
144- if length (S_indices[i]) != 0
145- last_S = i
146- break
147- end
148- end
149-
150- for i in first_A: last_A
151- if length (A_indices[i]) == 0
152- throw (
153- ErrorException (
154- " Your parameter vector is not partitioned into directed and undirected effects" ,
155- ),
156- )
157- return nothing
158- end
159- end
160-
161- for i in first_S: last_S
162- if length (S_indices[i]) == 0
163- throw (
164- ErrorException (
165- " Your parameter vector is not partitioned into directed and undirected effects" ,
166- ),
167- )
168- return nothing
169- end
170- end
171-
172- return first_A: last_A, first_S: last_S
173- end
174-
175- function get_partition (M_indices)
176- n_par = length (M_indices)
177-
178- first_M = " a"
179- last_M = " a"
180-
181- for i in 1 : n_par
182- if length (M_indices[i]) != 0
183- first_M = i
184- break
185- end
186- end
187-
188- for i in n_par + 1 .- (1 : n_par)
189- if length (M_indices[i]) != 0
190- last_M = i
191- break
192- end
193- end
194-
195- for i in first_M: last_M
196- if length (M_indices[i]) == 0
197- throw (
198- ErrorException (
199- " Your parameter vector is not partitioned into directed, undirected and mean effects" ,
200- ),
201- )
202- return nothing
118+ # range of parameters that are referenced in the matrix
119+ function param_range (mtx_indices:: AbstractArrayParamsMap )
120+ first_i = findfirst (! isempty, mtx_indices)
121+ last_i = findlast (! isempty, mtx_indices)
122+
123+ if ! isnothing (first_i) && ! isnothing (last_i)
124+ for i in first_i: last_i
125+ if isempty (mtx_indices[i])
126+ # TODO show which parameter is missing in which matrix
127+ throw (
128+ ErrorException (
129+ " Your parameter vector is not partitioned into directed and undirected effects" ,
130+ ),
131+ )
132+ end
203133 end
204134 end
205135
206- return first_M : last_M
136+ return first_i : last_i
207137end
0 commit comments