AutoDiff icon indicating copy to clipboard operation
AutoDiff copied to clipboard

Add indexing of arrays?

Open federkamm opened this issue 2 years ago • 1 comments

Hi,

this library looks promising, but I couldn't even get my first example running (I use 0.4.1 installed with pip in Python 3.9).

I have a function u, v = f(x, y) from two real variables to two real variables and tried to get its Jacobian matix J at x, y = 0., 0.. I would have expected I could simply do:

import auto_diff, numpy

def helper(z):
    u, v = f(z[0], z[1])
    return numpy.array([u, v])

z = numpy.zeros(2)
with auto_diff.AutoDiff(z) as Z:
    helper_eval = helper(Z)
    w, J = auto_diff.get_value_and_jacobian(helper_eval)

but it fails since Z is not indexable in the call of helper(Z). I would have expect to get a similar interface in Z as z has itself in order to allow Z to be a replacement for z in most cases. Do I do something wrong? Should Z implement indexing in order to allow to differentiate functions that use indexing?

Thank you for your answer.

federkamm avatar Jul 01 '23 17:07 federkamm

Nevermind.

It works with

import auto_diff, numpy

def helper(z):
    u, v = f(z[0,0], z[1,0])
    return numpy.array([[u], [v]])

z = numpy.zeros((2,1))
with auto_diff.AutoDiff(z) as Z:
    helper_eval = helper(Z)
    w, J = auto_diff.get_value_and_jacobian(helper_eval)

auto_diff seems to not work with vectors (1D-arrays) but only with n x 1-matrices (2D-arrays). Maybe, this could be documented in the "Restrictions" section in the README file.

federkamm avatar Jul 01 '23 17:07 federkamm