Gvar extensions

Index

gvar_gufunc, switchgvar, jacobian, from_jacobian, gvar_format, uformat, fmtspec_kwargs


lsqfitgp.gvar_gufunc(func, *, signature=None)[source]

Wraps a jax-traceable generalized ufunc with one argument to support gvars.

Parameters:
funccallable

A function from one array to one array. It must be a generalized ufunc, and differentiable one time with jax.

signaturestr, optional

The signature of the generalized ufunc. If not specified, it is assumed to be scalar to scalar (normal ufunc).

Returns:
decorated_funccallable

A function that, in addition to numerical arrays, accepts gvars and returns gvars.

See also

numpy.vectorize
lsqfitgp.switchgvar()[source]

Context manager to keep new gvars in a separate pool.

Creating new primary gvars fills up memory permanently. This context manager keeps the gvars created within its context in a separate pool that is freed when all such gvars are deleted. They can not be mixed in operations with other gvars created outside of the context.

Returns:
gvargvar.GVarFactory

The new gvar-creating function that uses a new pool. The change is also reflected in the global gvar.gvar.

Examples

>>> x = gvar.gvar(0, 1)
>>> with lgp.switchgvar():
>>>     y = gvar.gvar(0, 1)
>>>     z = gvar.gvar(0, 1)
>>> w = gvar.gvar(0, 1)
>>> q = y + z  # allowed, y and z created in the same pool
>>> p = x + w  # allowed, x and w created in the same pool
>>> h = x + y  # x and y created in different pools: this will silently
...            # fail and possibly crash python immediately or later on
lsqfitgp.jacobian(g)[source]

Extract the jacobian of gvars w.r.t. primary gvars.

Parameters:
garray_like

An array of numbers or gvars.

Returns:
jacarray

The shape is g.shape + (m,), where m is the total number of primary gvars that g depends on.

indices(m,) int array

The indices that map the last axis of jac to primary gvars in the global covariance matrix.

See also

from_jacobian
lsqfitgp.from_jacobian(mean, jac, indices)[source]

Create new gvars from a jacobian w.r.t. primary gvars.

Parameters:
meanarray_like

An array of numbers with the means of the new gvars.

jacmean.shape + (m,) array

The derivatives of each new gvar w.r.t. m primary gvars.

indices(m,) int array

The indices of the primary gvars.

Returns:
gmean.shape array

The new gvars.

See also

jacobian
lsqfitgp.gvar_format(spec=None, *, lsqfitgp_format=True)[source]

Context manager to set the default format specification of gvars.

Parameters:
specstr, optional

The format specification. If not specified and lsqfitgp_format is True, use '#1.5p'.

lsqfitgp_formatbool, default True

Whether to use a modified version of the gvar formatting specification, provided by lsqfitgp.

Notes

See fmtspec_kwargs for the format specification, and uformat for all details.

lsqfitgp.uformat(mu, s, errdig=2, sep=None, *, shareexp=True, outersign=False, uniexp=False, minnegexp=6, minposexp=4, padzero=None, possign=False)[source]

Format a number with uncertainty.

Parameters:
munumber

The central value.

snumber

The error.

errdignumber

The number of digits of the error to be shown. Must be >= 1. It can be a noninteger, in which case the number of digits switches between the lower nearest integer to the upper nearest integer as the first decimal digit (after rounding) crosses 10 raised to the fractional part of errdig. Default 1.5.

sepNone or str

The separator put between the central value and the error. Eventual spaces must be included. If None, put the error between parentheses, sharing decimal places/exponential notation with the central value. Default None.

shareexpbool, default True

Applies if sep is not None. When using exponential notation, whether to share the exponent between central value and error with outer parentheses.

outersignbool

Applied when sep is not None and shareexp is True. Whether to put the sign outside or within the parentheses. Default False

uniexpbool

When using exponential notation, whether to use unicode characters instead of the standard ASCII notation. Default False.

minnegexpint

The number of places after the comma at which the notation switches to exponential notation. Default 4. The number of places from the greater between central value and error is considered.

minposexpint

The power of ten of the least significant digit at which exponential notation is used. Default 0. Setting higher values may force padding the error with zeros, depending on errdig.

padzerostr, optional

If provided, a character representing 0 to pad with when not using exponential notation due to minposexp even if the least significant digit is not on the units, instead of showing more actual digits than those specified.

possignbool, default False

Whether to put a + before the central value when it is positive.

Returns:
rstr

The quantity (mu +/- s) nicely formatted.

lsqfitgp.fmtspec_kwargs(spec)[source]

Parse a string formatting pattern to be used with uformat.

Parameters:
specstr

The format specification. It must follow the format

[options](error digits)[:minimum exponent](mode)

where brackets indicate optional parts.

Returns:
kwargsdict

The keyword arguments to be passed to uformat.

Notes

Full format:

Options: any combination these characters:

‘+’ :

Put a ‘+’ before positive central values.

‘-’ :

Put the sign outside the parentheses used to group the central value and error mantissas in exponential notation.

‘#’ :

Do not show non-significative digits at all costs, replacing them with lowercase ‘o’, representing a rounding 0 rather than a significative 0.

‘$’ :

In exponential notation, repeat the exponent for the central value and error.

Error digits: a decimal number expressing the number of leading error digits to show. Non-integer values indicate that the number of digits switches from the floor to the ceil at some value of the mantissa.

Minimum exponent: a decimal number expressing the minimum exponent at which exponential notation is used.

Mode: one of these characters:

‘p’ :

Put the error between parentheses.

‘s’ :

Separate the central value from the error with ‘+/-‘.

‘u’ :

Separate the central value from the error with ‘±’.

‘U’ :

Separate the central value from the error with ‘±’, and use unicode superscript characters for exponential notation.