Skip to content

AutoDiffXd Operations in Python May Not Preserve Derivative Shape #19584

Description

@cohnt

What happened?

Certain operations in pydrake.math, including max and min, may end up changing the number of derivatives an AutoDiffXd variable has. This can become a major issue down the line -- for example, when jacobian on a function, it expects the number of derivatives to be the same for all output values, in order to construct the Jacobian matrix. Here's a simple example of code that exhibits such a problem:

import numpy as np
from pydrake.all import AutoDiffXd, jacobian
import pydrake.math

def func(q):
	# q should be of length 2
	return np.array([q[0], pydrake.math.min(q[1], 1)])

x = np.array([0, 0])
jacobian(func, x)

y = np.array([0, 2])
jacobian(func, y)

The first jacobian computation works as expected, whereas the second throws an error due to the derivative shapes not lining up.

Here's an example in general of derivative shapes changing when calling min and max, or not being checked:

import numpy as np
from pydrake.all import AutoDiffXd
import pydrake.math

x = AutoDiffXd(0, np.zeros(2))
y = AutoDiffXd(1, np.zeros(3))

print(pydrake.math.min(x, y))
print(pydrake.math.max(x, y))

Version

No response

What operating system are you using?

No response

What installation option are you using?

No response

Relevant log output

No response

Metadata

Metadata

Assignees

Labels

component: mathematical programFormulating and solving mathematical programs; our autodiff and symbolic librariestype: bug

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions