Usage

Installation:

At the command line:

$ pip install nullspace

Start by importing nullspace.

import nullspace
nullspace.__init__.autodiff_svd(A, q_star, *, symbolic=False)[source]

Automatic Differentiating of svd.

It takes matrix A, return differentiation of U, S and V with respect to specific q value q_star. U, S and VT(V.T) are result of standard SVD. This method is implemented in reverse mode in terms of auto diff conventions which is more effeciency since all free variables q take advantages of same intermedia svd result, U_star, S_star and V_star. It returns ecomonic result which means ‘full_matrices=False’ in corresponding ‘numpy.linalg.svd’

Parameters:
A(m, n) Matrix

A sympy symbolic 2D Matrix, which should be legal for SVD later.

q_stardict

A dict from sympy symbolic variable to value.

symbolicbool, optional

Control return type. Return numerical result, numpy.ndarray, if true (default), otherwise return symbolic result, sympy.Array.

Returns:
du(Q, M, K) array

Partial derivatives/gradient of U with respect to each variables in q star. Each variable matches a (M, K) matrix. Q is number of variables. Return sympy.Array if symbolic=True.

ds(0, K, K) array

Partial derivatives/gradient of S with respect to each variables in q star, Each variable matches a (M, K) matrix. K = min(M, N). (…, K, K) is a strict diagonal matrix which is quaranteed by Hadamard operation. check examples for details. Return ‘sympy.Array’ if symbolic=True.

dv(Q, N, K) array

Partial derivatives/gradient of V(NOT VT) with respect to each variables in q_star. Each variable matches a (N, K) matrix. Return ‘sympy.Array’ if symbolic=True

See also

numpy.linalg.svd

Similar function in numpy.

sympy.Array

3D tensor in sympy.

sympy.Matrix.jacobian

jacobian calculation in sympy.

References

[1]

James Townsend. Differentiating the Singular Value Decomposition.

[2]

Mike Giles. An extended collection of matrix derivative results for forward and reverse mode algorithmic differentiation.

Examples

>>> from sympy import sin, cos, symbols, Matrix
>>> q0, q1 = symbols ("q0, q1")
... q star = (q0: 1, q1: 2}
... A = Matrix([[2*q0,          1,    q0**3 + 2*q1, sin(q0) + q1],
               [q0**2 - q1**2,  2*q1, 2*q0 + q1**3, cos(q0) + q1],
               [q0**2 + q1**2,  2,    3,            sin(q0) + cos(q1)]])
>>> du, ds, dv = autodiff svd(A, q_star)
>>> du.shape
(2, 3, 3)
>>> ds.shape
(2, 3, 3)
>>>dv.shape
(2, 4, 3)
>>> du_sym, ds_sym, dv_sym = autodiff_svd(A, q_star, symbolic=True)