matt-weinstein / adigator

Matlab Algorithmic Differentiation Toolbox
22 stars 5 forks source link

Hessian of vector function #4

Closed libaozi1992 closed 4 years ago

libaozi1992 commented 4 years ago

Hi Matt,

Thanks a bunch for developing the super cool package. I have problem using it to compute the Hessian of vector function, though. Something I tried was to slightly modify the logsumexp function example, by replacing it with the following:

function y = logsumexp(x)

y = zeros(2,1);

y(1) = log(sum(exp(x)));

y(2) = log(sum(exp(x)));

Basically I was adding a second dimension, which is exactly the same as the first dimension. If I run the same main code to compute the hessian of this vector function (with number of unknowns n=3), it gave me an error:

`Attempt to grow array along ambiguous dimension.'

Is this the right way to compute the hessian of a vector function with the package? I might be missing something, though I feel this is an issue that is worth your notice.

Thanks for your help! `Harry' Haishi Li

matt-weinstein commented 4 years ago

@libaozi1992 Sorry for the delay. When using vectorized mode, vectorized dimension N, you can only performed operations on the vectorized dimension which maintain N dimension, and do not mix variables across the vectorized dimension (i.e. you can't do Y = A*X, if A is NxN and X is N x 1) there is some more writeup in the user's manual on restrictions. You also need to allocate vectorized variables using the vectorized input.

I can't exactly tell what the dimensions of x are intended to be here.. If x is Nx3, then sum(exp(x)) will fail as it is attempting to sum over the vectorized dimension. If x is 3xN then y(1) will fail because you are assigning a 1xN vector to a scalar.

For the logsumexp example, the sum can only be performed over the non-vectorized dimension (i.e. the sum has to be performed over a fixed/known dimension).