dubfi.linalg.generic ==================== .. py:module:: dubfi.linalg.generic .. autoapi-nested-parse:: Generic linear algebra objects building upon types modules. .. codeauthor:: Valentin Bruch, DWD .. versionadded:: 0.1.0 (initial release) Classes ------- .. autoapisummary:: dubfi.linalg.generic.ParametrizedVector dubfi.linalg.generic.ParametrizedOperator dubfi.linalg.generic.DiagonalOperator dubfi.linalg.generic.IdentityOperator Functions --------- .. autoapisummary:: dubfi.linalg.generic.check_derivatives dubfi.linalg.generic.check_derivatives_np Module Contents --------------- .. py:class:: ParametrizedVector Bases: :py:obj:`abc.ABC` AbstractVector depending on array of parameters. .. py:method:: __call__(s) :abstractmethod: Vector at given parameter array. .. py:method:: grad(s) :abstractmethod: Gradient w.r.t. parameters s. .. py:method:: hess(s) :abstractmethod: Hesse matrix w.r.t. parameters s. .. py:method:: check_derivatives(s, **kwargs) Test derivatives :meth:`~grad` and :meth:`~hess`. .. py:class:: ParametrizedOperator Bases: :py:obj:`abc.ABC` AbstractOperator depending on array of parameters. .. py:method:: __call__(s) :abstractmethod: Operator at given parameter array. .. py:method:: grad(s) :abstractmethod: Gradient w.r.t. parameters s. .. py:method:: grad_vec(s, vec) Equivalent to self.grad(s) @ vec. .. py:method:: vec_grad_vec(s, left, right) Equivalent to left @ (self.grad(s) @ right). .. py:method:: hess(s) :abstractmethod: Hesse matrix w.r.t. parameters s. .. py:method:: hess_vec(s, vec) Equivalent to self.hess(s) @ vec. .. py:method:: vec_hess_vec(s, left, right) Equivalent to left @ (self.hess(s) @ right). .. py:method:: check_derivatives(s, **kwargs) Test derivatives :meth:`~grad` and :meth:`~hess`. .. py:method:: check_derivatives_applied(s, vec, **kwargs) Test derivatives :meth:`~grad` and :meth:`~hess`, applying results on vec. .. py:method:: check_derivatives_sandwich(s, vec, **kwargs) Test derivatives :meth:`~grad` and :meth:`~hess`, with results sandwiched by vec. .. py:method:: check_derivatives_trace(s, **kwargs) Test derivatives :meth:`~grad` and :meth:`~hess`, with trace applied to results. .. py:class:: DiagonalOperator(diagonal) Bases: :py:obj:`dubfi.linalg.types.AbstractOperator` Generic (scalar times) identity operator. Linear operator represented by a diagonal matrix. :param diagonal: last dimension represents the vector space dimension along which this is the diagonal of a linear operator :type diagonal: np.ndarray .. py:method:: diagonal() Return copy of the diagonal of self as vector. .. py:property:: shape Shape of array (matrix) representation of self. .. py:method:: apply(vec) Apply operator from left. .. py:method:: rapply(vec) Apply operator from right. .. py:method:: chain(other) Combine operators: self @ other, may return other or a copy. .. py:method:: tonumpy() Numpy array (matrix) representation of self. .. py:method:: solve(vec) Solve self @ x = vec for x. .. py:method:: inv() Compute (multiplicative) inverse. .. py:method:: __mul__(other) Multiply element-wise. .. py:method:: __add__(other) Add other operator. .. py:method:: __sub__(other) Add other operator. .. py:method:: __iadd__(other) Add other operator. .. py:method:: __isub__(other) Add other operator. .. py:method:: trace() Compute trace of self. .. py:method:: trace_product(other) Compute trace of (self @ other). .. py:method:: logdet() Compute log(det(self)), assuming that all entries are positive. .. py:method:: dot(other) Inner product along non-vector dimensions. .. py:class:: IdentityOperator(size, prefactor=None) Bases: :py:obj:`DiagonalOperator` Generic (scalar times) identity operator. Linear operator represented by a diagonal matrix. :param diagonal: last dimension represents the vector space dimension along which this is the diagonal of a linear operator :type diagonal: np.ndarray .. py:property:: shape Shape of array (matrix) representation of self. .. py:method:: apply(vec) Apply identity operator, may return vec or a copy. .. py:method:: rapply(vec) Apply identity operator, may return the vector itself or a copy. .. py:method:: tonumpy() Numpy array (matrix) representation of self. .. py:method:: solve(vec) Solve self @ x = vec for x, may return vec or a copy. .. py:method:: inv() Compute (multiplicative) inverse, again an identity operator. .. py:method:: __mul__(other) Multiply element-wise. .. py:method:: chain(other) Combine operators: self @ other, may return other or a copy. .. py:method:: trace() Compute trace of self. .. py:method:: trace_product(other) Compute trace of (self @ other). .. py:method:: logdet() Compute log(det(self)) assuming that all prefactors are positive. .. py:function:: check_derivatives(f, f_grad, f_hess, x, eps=1e-05, tol=20, count=10) Test derivatives (gradient and Hesse matrix) of function. This function prints statistics and warning messages if the numerical computed derivatives deviate significantly from the provided gradient and/or Hessian functions. :param f: function mapping a 1d array to a scalar or vector. :type f: callable :param f_grad: gradient of f (maps a 1d array to a 1d array or vector of 1d arrays) :type f_grad: callable :param f_hess: Hesse matrix of f (maps a 1d array to a 2d array or vector of 2d arrays) :type f_hess: callable :param x: parameter array at which the derivatives should be checked :type x: np.ndarray :param eps: scale of variations of x to compute derivatives numerically :type eps: float :param tol: tolerance scale relative to expected orders of eps. :type tol: float :param count: number of directions in which derivatives should be checked. :type count: int :returns: **errors** -- number of errors :rtype: int .. py:function:: check_derivatives_np(f, f_grad, f_hess, x, eps=1e-05, tol=20) Test derivatives for functions returning only numpy arrays.