Error when calculating derivative of CoefficientFunction

I tried to calculate the derivative of the vector valued CoefficientFunction b:

from ngsolve import * 
a = CF((1,1))
b = exp(a)
print(b.Diff(a)) # -> error

While b itself can be calculated, i get the following error at the differentiation:
netgen.libngpy._meshing.NgException: Dimensions don't match, op = * dims1 = 0: 2 , dims2 = 0: 2 1: 2

I use ngsolve version 6.2.2405.
Thank you in advance.

Applying a scalar function (like exp) to a vectorial CF applies it component-wise. As far as I know, this is an undocumented feature.

Computing the Jacobi matrix is not implemented for the vectorial case, and results in that unintuitive exception.

The Jacobi matrix is a diagonal matrix. We could add that with little effort, however the performance would be sub-optimal for large vector dimension, if we don’t treat the diagonal matrix as such.

A directional derivative is implemented, and is efficient. Maybe you can reformulate your problem with directional derivatives?

Joachim

Thank you for your response.
The code I shared is heavily simplified (resulting in a diagonal Jacobian matrix), but in my original code, the Jacobian is generally non-diagonal.

I am trying to define the free energy Psi using a neural network that employs exp in its activation function. This works fine, but when I attempt to compute the derivative Psi.Diff(F), I encounter an error like the one mentioned above.

I found this forum post, where a similar issue is described.
In the reply, you mentioned that the error is caused by a bug in the differentiation of the vectorized exp⁡ function, which will be fixed.

It is working again with the latest pip pre-release.

Explanation: If the total derivative is not implemented, it is composed from the simpler directional derivatives in unit vector directions. After the forum post you cited, the total derivative was implemented, but not for the (not officially supported) vectorial exp.

Be careful with your tests, and let us know how it works for the more complex examples.

Joachim