symnum.diffops.symbolic module#
Autograd-style functional differential operators for symbolic functions.
- symnum.diffops.symbolic.grad(func, *, wrt=0, return_aux=False)#
Generate a function to evaluate the gradient of a scalar-valued function.
The passed function should take as arguments symbolic arrays and return a symbolic scalar, and likewise the returned function will take symbolic array arguments.
- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated gradient function returns both the gradient and value of
func
as a 2-tuple (True
) or just the gradient (False
).
- Returns:
Generated gradient function.
- Return type:
Callable[…, Union[ArrayLike, tuple[ArrayLike, ScalarLike]]]
- symnum.diffops.symbolic.gradient(func, *, wrt=0, return_aux=False)[source]#
Generate a function to evaluate the gradient of a scalar-valued function.
The passed function should take as arguments symbolic arrays and return a symbolic scalar, and likewise the returned function will take symbolic array arguments.
- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated gradient function returns both the gradient and value of
func
as a 2-tuple (True
) or just the gradient (False
).
- Returns:
Generated gradient function.
- Return type:
Callable[…, Union[ArrayLike, tuple[ArrayLike, ScalarLike]]]
- symnum.diffops.symbolic.hessian(func, *, wrt=0, return_aux=False)[source]#
Generate a function to evaluate the Hessian of a scalar-valued function.
The passed function should take as arguments symbolic arrays and return a symbolic scalar, and likewise the returned function will take symbolic array arguments.
- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated Hessian function returns the Hessian, gradient and value of
func
as a 3-tuple (True
) or just the Hessian (False
).
- Returns:
Generated Hessian function.
- Return type:
Callable[…, Union[ArrayLike, tuple[ArrayLike, ArrayLike, ScalarLike]]]
- symnum.diffops.symbolic.hessian_vector_product(func, *, wrt=0, return_aux=False)[source]#
Generate an operator to evaluate Hessian-vector-products for a function.
The passed function should take as arguments symbolic arrays and return a symbolic scalar, and likewise the returned operator will take symbolic array arguments.
For a single argument function
func
, n-dimensional input arrayx
and n-dimensional ‘vector’ arrayv
of the same shape asx
then we have the following equivalencehessian_vector_product(func)(x)(v) == tensordot(hessian(func)(x), v, n)
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator returns the Hessian-vector-product function, gradient and value of
func
as a 3-tuple (True
) or just the Hessian-vector-product function (False
).
- Returns:
Generated Hessian-vector-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike, ScalarLike]]]
- symnum.diffops.symbolic.jacobian(func, *, wrt=0, return_aux=False)[source]#
Generate a function to evaluate the Jacobian of a function.
The passed function should take as arguments and return symbolic array(s), and likewise the returned function will take symbolic array arguments.
- Parameters:
func (Callable[..., ArrayLike]) – Function which takes one or more arrays as arguments and returns an array.
wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated Jacobian function returns both the Jacobian and value of
func
as a 2-tuple (True
) or just the Jacobian (False
).
- Returns:
Generated Jacobian function.
- Return type:
Callable[…, Union[ArrayLike, tuple[ArrayLike, ArrayLike]]]
- symnum.diffops.symbolic.jacobian_vector_product(func, *, wrt=0, return_aux=False)[source]#
Generate an operator to evaluate Jacobian-vector-products for a function.
The passed function should take as arguments and return symbolic array(s), and likewise the returned operator will take symbolic array arguments.
For a single argument function
func
, n-dimensional input arrayx
and n-dimensional ‘vector’ arrayv
of the same shape asx
then we have the following equivalencejacobian_vector_product(func)(x)(v) == tensordot(jacobian(func)(x), v, n)
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ArrayLike]) – Function which takes one or more arrays as arguments and returns an array.
wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator returns the Jacobian-vector-product function and value of
func
as a 2-tuple (True
) or just the Jacobian-vector-product function (False
).
- Returns:
Generated Jacobian-vector-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike]]]
- symnum.diffops.symbolic.matrix_hessian_product(func, *, wrt=0, return_aux=False)[source]#
Generate an operator to evaluate matrix-Hessian-products for a function.
The passed function should take as arguments and return symbolic array(s), and likewise the returned function will act on symbolic arrays.
For a single argument function
func
, n-dimensional input arrayx
and k + n-dimensional ‘matrix’ arraym
with shapefunc(x).shape + x.shape
then we have the following equivalencematrix_hessian_product(func)(x)(m) == ( tensordot(m, jacobian(jacobian(func))(x), k + n) )
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ArrayLike]) – Function which takes one or more arrays as arguments and returns an array.
wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator return the matrix-Hessian-product function, Jacobian and value of
func
as a 3-tuple (True
) or just the matrix-Hessian-product function (False
).
- Returns:
Generated matrix-Hessian-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike, ArrayLike]]]
- symnum.diffops.symbolic.matrix_tressian_product(func, *, wrt=0, return_aux=False)[source]#
Generate an operator to evaluate matrix-Tressian-products for a function.
The passed function should take as arguments symbolic arrays and return a symbolic scalar, and likewise the returned operator will take symbolic array arguments.
For a single argument function
func
, n-dimensional input arrayx
and (2 * n)-dimensional ‘matrix’ arraym
of shapex.shape + x.shape
then we have the following equivalencematrix_tressian_product(func)(x)(m) == ( tensordot(jacobian(hessian(func))(x), 2 * n) )
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ScalarLike]) – Function which takes one or more arrays as arguments and returns a scalar.
wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator returns the matrix-Tressian-product function, Hessian, gradient and value of func as a 4-tuple (
True
) or just the matrix-Tressian-product function (False
).
- Returns:
Generated matrix-Tressian-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike, ArrayLike, ScalarLike]]]
- symnum.diffops.symbolic.vector_jacobian_product(func, *, wrt=0, return_aux=False)[source]#
Generate an operator to evaluate vector-Jacobian-products for a function.
The passed function should take as arguments and return symbolic array(s), and likewise the returned function will act on symbolic arrays.
For a single argument function
func
, input arrayx
and n-dimensional ‘vector’ array v of the same shape asfunc(x)
then we have the following equivalencevector_jacobian_product(func)(x)(v) == tensordot(v, jacobian(func)(x), n)
where
tensordot
follows its NumPy semantics, i.e.tensordot(a, b, n)
sums the products of components ofa
andb
over the lastn
axes (dimensions) ofa
and firstn
dimensions ofb
.- Parameters:
func (Callable[..., ArrayLike]) – Function which takes one or more arrays as arguments and returns an array.
wrt (int) – Index of argument to take derivatives with respect to.
return_aux (bool) – Whether the generated operator returns the vector-Jacobian-product function and value of
func
as a 2-tuple (True
) or just the vector-Jacobian-product function (False
).
- Returns:
Generated vector-Jacobian-product operator.
- Return type:
Callable[…, Union[Callable[[ArrayLike], ArrayLike], tuple[Callable[[ArrayLike], ArrayLike], ArrayLike]]]