Unpacking Matrix Columns as Vector Arguments

I am trying to unpack the columns of a matrix S into the ForwardDiff.Dual{T} constructor. I have tried numerous different approaches without success.

Specifically, I want a function that does the following:

function gAD(nlp, x, S)
	dual = Dual{DualTag}.(x, S[:, i]...) # for the i \in 1:size(S,2)
	return nlp.g!(dual, x)
end

I can get it working when I explicitly declare the columns, as shown with two columns below

function gAD(nlp, x, S)
	dual = Dual{DualTag}.(x, S[1, :], S[2,:] ) 
	return nlp.g!(dual, x)
end

Thank you in advance for your insight into the problem I am facing!

dual = Dual{DualTag}.(x, [S[:, i] for i in 1:size(S, 2)]....)

Have you considered eachcol?

Dual{DualTag}.(x, eachcol(S)....)
6 Likes

Great both solutions work, with a minor update - there is an extra period in the “splat” for both solutions, arising from a previous typo in my questions. Thanks.

1 Like