pysolvegn.BatchParametrization#

class BatchParametrization(parametric_func=None, jacobian_func=None, finite_difference=None, finite_difference_epsilon=None)[source]#

A parametric transformation \(P\) applied to each problem of a batch of independent least squares problems. It maps the input parameters \(\vec{p_{in,k}}\) of each problem \(k\) from an input parametric space \(\mathbb{R}^{n_{\mathrm{parameters}}}\) to output parameters \(\vec{p_{out,k}}\) in an output parametric space \(\mathbb{R}^{n_{\mathrm{p\_outputs}}}\).

This is the batched counterpart of pysolvegn.Parametrization, used by pysolvegn.solve_batch().

\[\vec{p_{out,k}} = P(\vec{p_{in,k}})\]

Important

The same transformation \(P\) is used for all the problems of the batch.

Unlike pysolvegn.BatchTerm, the callables of a BatchParametrization do not receive the indices of the problems: the transformation can not depend on the problem \(k\). Only the input parameters change from one problem to another. The callables are simply vectorized over the first dimension: the row a of every output only depends on the row a of the input parameters.

The number of evaluated problems m is NOT fixed: the solver removes the stopped problems at each iteration, so the callables are called with parameters with shape (m, n_parameters) for any 1 <= m <= k.

Considering the following batch of least-square problems:

\[\min_{\vec{p_{out,k}}} \frac{1}{2} \sum_{i} w_{i,k} \sum_j \rho_i\left(\| \mathbf{r}_{i,j}(\vec{p_{out,k}}) \|^2\right)\]

each problem can be rewritten using the parametric transformation \(P\) as:

\[\min_{\vec{p_{in,k}}} \frac{1}{2} \sum_{i} w_{i,k} \sum_j \rho_i\left( \| \mathbf{r}_{i,j}(P(\vec{p_{in,k}})) \|^2 \right)\]

The parameters \(\vec{p_{in,k}}\) are the parameters actually optimized by the batched Gauss-Newton algorithm, while \(\vec{p_{out,k}}\) are the parameters passed to the residual functions of the pysolvegn.BatchTerm.

This class is used to store the parametric transformation \(P\) and its first derivative, evaluated for each problem:

\[\mathbf{J}_{P,k} = \frac{\partial \vec{p_{out}}} {\partial \vec{p_{in}}}\left(\vec{p_{in,k}}\right).\]
Parameters:
  • parametric_func (Callable) – The function to compute the parametric transformation \(P\). The function should take as input the input parameters of m problems (2D array with shape (m, n_parameters)) and return the output parameters \(\mathbf{p}_{out}\) as a 2D numpy array with shape (m, n_p_outputs), where the row a is \(P\) applied to the row a of the input.

  • jacobian_func (Optional[Callable] (default=None)) – The function to compute the Jacobian matrices of the parametric transformation \(P\) with respect to the input parameters. The function should take as input the input parameters of m problems (2D array with shape (m, n_parameters)) and return the Jacobian matrices as a 3D array with shape (m, n_p_outputs, n_parameters). Each element of the Jacobian is given by \(\mathbf{J}_{P,k,(j,l)}=\frac{\partial p_{out,k,j}}{\partial p_{in,k,l}}\) where \(j=0,...,n_{p_o}-1}\) and \(l=0,...,n_{p}-1}\). If not provided, finite_difference must be specified to numerically compute the Jacobian.

  • finite_difference (Optional[str] (default=None)) – The finite difference method used to numerically compute the Jacobian when jacobian_func is not provided. If None, the Jacobian is not computed numerically. In this case, jacobian_func must be provided. Available methods are "central", "forward", and "backward".

  • finite_difference_epsilon (Optional[Real] (default=None)) – The relative step of the finite differences (only with finite_difference). If None, the default step of the method is used: about 1.5e-8 for "forward" and "backward", about 6.1e-6 for "central" (see pysolvegn.build_numerical_jacobian()).

Examples

Optimize strictly positive output parameters through their logarithm, for all the problems of the batch:

import numpy
from pysolvegn import BatchParametrization

def parametric_func(p_in):  # (m, n) -> (m, n)
    return numpy.exp(p_in)

def jacobian_func(p_in):  # (m, n) -> (m, n, n)
    return numpy.exp(p_in)[:, :, None] * numpy.eye(p_in.shape[1])

parametrization = BatchParametrization(parametric_func, jacobian_func)
__init__(parametric_func=None, jacobian_func=None, finite_difference=None, finite_difference_epsilon=None)[source]#
Parameters:
  • parametric_func (Callable)

  • jacobian_func (Callable | None)

  • finite_difference (str | None)

  • finite_difference_epsilon (Real | None)

Methods

__init__([parametric_func, jacobian_func, ...])

Attributes

J_func

jacobian_func

[Get] the batched Jacobian function \(\mathbf{J}_P: \mathbb{R}^{n_p} \rightarrow \mathbb{M}_{n_{p_o}, n_p}(\mathbb{R})\) of the parametrization.

p_func

parametric_func

[Get] the batched parametric function \(P: \mathbb{R}^{n_p} \rightarrow \mathbb{R}^{n_{p_o}}\) of the parametrization.