Package ComboCode :: Package cc :: Package ivs :: Package units :: Package uncertainties :: Package unumpy :: Module core
[hide private]
[frames] | no frames]

Module core

source code

Core functions used by unumpy and some of its submodules.

(c) 2010 by Eric O. LEBIGOT (EOL).


Author: Eric O. LEBIGOT (EOL)

Classes [hide private]
  matrix
Class equivalent to numpy.matrix, but that behaves better when the matrix contains numbers with uncertainties.
Functions [hide private]
 
unumpy_to_numpy_matrix(arr)
If arr in a unumpy.matrix, it is converted to a numpy.matrix.
source code
 
nominal_values(arr)
Returns the nominal values of the numbers in NumPy array arr.
source code
 
std_devs(arr)
Returns the standard deviations of the numbers in NumPy array arr.
source code
 
derivative(u, var)
Returns the derivative of u along var, if u is an uncertainties.AffineScalarFunc instance, and if var is one of the variables on which it depends.
source code
 
wrap_array_func(func)
Returns a version of the function func() that works even when func() is given a NumPy array that contains numbers with uncertainties.
source code
 
uarray((values, std_devs))
Returns a NumPy array of numbers with uncertainties initialized with the given nominal values and standard deviations.
source code
 
array_derivative(array_like, var)
Returns the derivative of the given array with respect to the given variable.
source code
 
func_with_deriv_to_uncert_func(func_with_derivatives)
Returns a function that can be applied to array-like objects that contain numbers with uncertainties (lists, lists of lists, Numpy arrays, etc.).
source code
 
inv_with_derivatives(arr, input_type, derivatives)
Defines the matrix inverse and its derivatives.
source code
 
_inv(array_like, *args)
Version of numpy.linalg.inv that works with array-like objects that contain numbers with uncertainties.
source code
 
pinv_with_derivatives(arr, input_type, derivatives, rcond)
Defines the matrix pseudo-inverse and its derivatives.
source code
 
_pinv_with_uncert(array_like, *args)
array_like -- array-like object that contains numbers with uncertainties (list, Numpy ndarray or matrix, etc.).
source code
 
_pinv(array_like, rcond=1e-15)
Version of numpy.linalg.pinv that works with array-like objects that contain numbers with uncertainties.
source code
 
umatrix(*args)
Constructs a matrix that contains numbers with uncertainties.
source code
 
define_vectorized_funcs()
Defines vectorized versions of functions from uncertainties.umath.
source code
Variables [hide private]
  to_nominal_values = numpy.vectorize(uncertainties.nominal_valu...
  to_std_devs = numpy.vectorize(uncertainties.std_dev, otypes= [...
  _uarray = numpy.vectorize(lambda v, s: uncertainties.Variable(...
  _pinv_default = 1e-15
  __package__ = 'ComboCode.cc.ivs.units.uncertainties.unumpy'
  arccos = <numpy.lib.function_base.vectorize object at 0x10bb64...
  arccosh = <numpy.lib.function_base.vectorize object at 0x10bb6...
  arcsin = <numpy.lib.function_base.vectorize object at 0x10bb64...
  arctan = <numpy.lib.function_base.vectorize object at 0x10bb64...
  arctan2 = <numpy.lib.function_base.vectorize object at 0x10bb6...
  arctanh = <numpy.lib.function_base.vectorize object at 0x10bb6...
  asinh = <numpy.lib.function_base.vectorize object at 0x10bb64290>
  ceil = <numpy.lib.function_base.vectorize object at 0x10bb64390>
  copysign = <numpy.lib.function_base.vectorize object at 0x10bb...
  cos = <numpy.lib.function_base.vectorize object at 0x10bb64410>
  cosh = <numpy.lib.function_base.vectorize object at 0x10bb64450>
  degrees = <numpy.lib.function_base.vectorize object at 0x10bb6...
  erf = <numpy.lib.function_base.vectorize object at 0x10bb644d0>
  erfc = <numpy.lib.function_base.vectorize object at 0x10bb64510>
  exp = <numpy.lib.function_base.vectorize object at 0x10bb64550>
  expm1 = <numpy.lib.function_base.vectorize object at 0x10bb64590>
  fabs = <numpy.lib.function_base.vectorize object at 0x10bb645d0>
  floor = <numpy.lib.function_base.vectorize object at 0x10bb64610>
  fmod = <numpy.lib.function_base.vectorize object at 0x10bb64650>
  gamma = <numpy.lib.function_base.vectorize object at 0x10bb64690>
  hypot = <numpy.lib.function_base.vectorize object at 0x10bb646d0>
  isinf = <numpy.lib.function_base.vectorize object at 0x10bb64710>
  isnan = <numpy.lib.function_base.vectorize object at 0x10bb64750>
  ldexp = <numpy.lib.function_base.vectorize object at 0x10bb64ad0>
  lgamma = <numpy.lib.function_base.vectorize object at 0x10bb64...
  log = <numpy.lib.function_base.vectorize object at 0x10bb647d0>
  log10 = <numpy.lib.function_base.vectorize object at 0x10bb64810>
  log1p = <numpy.lib.function_base.vectorize object at 0x10bb64850>
  modf = <numpy.lib.function_base.vectorize object at 0x10bb64a90>
  pow = <numpy.lib.function_base.vectorize object at 0x10bb64890>
  radians = <numpy.lib.function_base.vectorize object at 0x10bb6...
  sin = <numpy.lib.function_base.vectorize object at 0x10bb64910>
  sinh = <numpy.lib.function_base.vectorize object at 0x10bb64950>
  sqrt = <numpy.lib.function_base.vectorize object at 0x10bb64990>
  tan = <numpy.lib.function_base.vectorize object at 0x10bb649d0>
  tanh = <numpy.lib.function_base.vectorize object at 0x10bb64a10>
  trunc = <numpy.lib.function_base.vectorize object at 0x10bb64a50>
Function Details [hide private]

unumpy_to_numpy_matrix(arr)

source code 

If arr in a unumpy.matrix, it is converted to a numpy.matrix. Otherwise, it is returned unchanged.

nominal_values(arr)

source code 

Returns the nominal values of the numbers in NumPy array arr.

Elements that are not uncertainties.AffineScalarFunc are passed through untouched (because a numpy.array can contain numbers with uncertainties and pure floats simultaneously).

If arr is of type unumpy.matrix, the returned array is a numpy.matrix, because the resulting matrix does not contain numbers with uncertainties.

std_devs(arr)

source code 

Returns the standard deviations of the numbers in NumPy array arr.

Elements that are not uncertainties.AffineScalarFunc are given a zero uncertainty ((because a numpy.array can contain numbers with uncertainties and pure floats simultaneously)..

If arr is of type unumpy.matrix, the returned array is a numpy.matrix, because the resulting matrix does not contain numbers with uncertainties.

derivative(u, var)

source code 

Returns the derivative of u along var, if u is an uncertainties.AffineScalarFunc instance, and if var is one of the variables on which it depends. Otherwise, return 0.

wrap_array_func(func)

source code 

Returns a version of the function func() that works even when func() is given a NumPy array that contains numbers with uncertainties.

func() is supposed to return a NumPy array.

This wrapper is similar to uncertainties.wrap(), except that it handles an array argument instead of float arguments.

func -- version that takes and returns a single NumPy array.

uarray((values, std_devs))

source code 

Returns a NumPy array of numbers with uncertainties initialized with the given nominal values and standard deviations.

values, std_devs -- valid arguments for numpy.array, with identical shapes (list of numbers, list of lists, numpy.ndarray, etc.).

array_derivative(array_like, var)

source code 

Returns the derivative of the given array with respect to the given variable.

The returned derivative is a Numpy ndarray of the same shape as array_like, that contains floats.

array_like -- array-like object (list, etc.) that contains scalars or numbers with uncertainties.

var -- Variable object.

func_with_deriv_to_uncert_func(func_with_derivatives)

source code 

Returns a function that can be applied to array-like objects that
contain numbers with uncertainties (lists, lists of lists, Numpy
arrays, etc.).

func_with_derivatives -- defines a function that takes array-like
objects containing scalars and returns an array.  Both the value
and the derivatives of this function with respect to multiple
scalar parameters are calculated by func_with_derivatives().

func_with_derivatives(arr, input_type, derivatives, *args) returns
an iterator.  The first element is the value of the function at
point 'arr' (with the correct type).  The following elements are
arrays that represent the derivative of the function for each
derivative array from the iterator 'derivatives'.

  func_with_derivatives takes the following arguments:

  arr -- Numpy ndarray of scalars where the function must be
  evaluated.

  input_type -- type of the input array-like object.  This type is
  used for determining the type that the function should return.

  derivatives -- iterator that returns the derivatives of the
  argument of the function with respect to multiple scalar
  variables.  func_with_derivatives() returns the derivatives of
  the defined function with respect to these variables.

  args -- additional arguments that define the result (example:
  for the pseudo-inverse numpy.linalg.pinv: numerical cutoff).

Examples of func_with_derivatives: inv_with_derivatives().

inv_with_derivatives(arr, input_type, derivatives)

source code 

Defines the matrix inverse and its derivatives.

See the definition of func_with_deriv_to_uncert_func() for its detailed semantics.

_inv(array_like, *args)

source code 
Version of numpy.linalg.inv that works with array-like objects
that contain numbers with uncertainties.

The result is a unumpy.matrix if numpy.linalg.pinv would return a
matrix for the array of nominal values.

Analytical formulas are used.

Original documentation:

Compute the (multiplicative) inverse of a matrix.

Given a square matrix `a`, return the matrix `ainv` satisfying
``dot(a, ainv) = dot(ainv, a) = eye(a.shape[0])``.

Parameters
----------
a : (..., M, M) array_like
    Matrix to be inverted.

Returns
-------
ainv : (..., M, M) ndarray or matrix
    (Multiplicative) inverse of the matrix `a`.

Raises
------
LinAlgError
    If `a` is not square or inversion fails.

Notes
-----

.. versionadded:: 1.8.0

Broadcasting rules apply, see the `numpy.linalg` documentation for
details.

Examples
--------
>>> from numpy.linalg import inv
>>> a = np.array([[1., 2.], [3., 4.]])
>>> ainv = inv(a)
>>> np.allclose(np.dot(a, ainv), np.eye(2))
True
>>> np.allclose(np.dot(ainv, a), np.eye(2))
True

If a is a matrix object, then the return value is a matrix as well:

>>> ainv = inv(np.matrix(a))
>>> ainv
matrix([[-2. ,  1. ],
        [ 1.5, -0.5]])

Inverses of several matrices can be computed at once:

>>> a = np.array([[[1., 2.], [3., 4.]], [[1, 3], [3, 5]]])
>>> inv(a)
array([[[-2. ,  1. ],
        [ 1.5, -0.5]],
       [[-5. ,  2. ],
        [ 3. , -1. ]]])

pinv_with_derivatives(arr, input_type, derivatives, rcond)

source code 

Defines the matrix pseudo-inverse and its derivatives.

Works with real or complex matrices.

See the definition of func_with_deriv_to_uncert_func() for its detailed semantics.

_pinv_with_uncert(array_like, *args)

source code 

array_like -- array-like object that contains numbers with uncertainties (list, Numpy ndarray or matrix, etc.).

args -- additional arguments that are passed directly to func_with_derivatives.

_pinv(array_like, rcond=1e-15)

source code 

Version of numpy.linalg.pinv that works with array-like objects
that contain numbers with uncertainties.

The result is a unumpy.matrix if numpy.linalg.pinv would return a
matrix for the array of nominal values.

Analytical formulas are used.

Original documentation:

Compute the (Moore-Penrose) pseudo-inverse of a matrix.

Calculate the generalized inverse of a matrix using its
singular-value decomposition (SVD) and including all
*large* singular values.

Parameters
----------
a : (M, N) array_like
  Matrix to be pseudo-inverted.
rcond : float
  Cutoff for small singular values.
  Singular values smaller (in modulus) than
  `rcond` * largest_singular_value (again, in modulus)
  are set to zero.

Returns
-------
B : (N, M) ndarray
  The pseudo-inverse of `a`. If `a` is a `matrix` instance, then so
  is `B`.

Raises
------
LinAlgError
  If the SVD computation does not converge.

Notes
-----
The pseudo-inverse of a matrix A, denoted :math:`A^+`, is
defined as: "the matrix that 'solves' [the least-squares problem]
:math:`Ax = b`," i.e., if :math:`\bar{x}` is said solution, then
:math:`A^+` is that matrix such that :math:`\bar{x} = A^+b`.

It can be shown that if :math:`Q_1 \Sigma Q_2^T = A` is the singular
value decomposition of A, then
:math:`A^+ = Q_2 \Sigma^+ Q_1^T`, where :math:`Q_{1,2}` are
orthogonal matrices, :math:`\Sigma` is a diagonal matrix consisting
of A's so-called singular values, (followed, typically, by
zeros), and then :math:`\Sigma^+` is simply the diagonal matrix
consisting of the reciprocals of A's singular values
(again, followed by zeros). [1]_

References
----------
.. [1] G. Strang, *Linear Algebra and Its Applications*, 2nd Ed., Orlando,
       FL, Academic Press, Inc., 1980, pp. 139-142.

Examples
--------
The following example checks that ``a * a+ * a == a`` and
``a+ * a * a+ == a+``:

>>> a = np.random.randn(9, 6)
>>> B = np.linalg.pinv(a)
>>> np.allclose(a, np.dot(a, np.dot(B, a)))
True
>>> np.allclose(B, np.dot(B, np.dot(a, B)))
True

Decorators:
  • @uncertainties.set_doc(""" Version of numpy.linalg.pinv that works with array-like objects that contain numbers with uncertainties. The result is a unumpy.matrix if numpy.linalg.pinv would return a matrix for the array of nominal values. Analytical formulas are used. Original documentation: %s """ % numpy.linalg.pinv.__doc__)

umatrix(*args)

source code 

Constructs a matrix that contains numbers with uncertainties.

The input data is the same as for uarray(...): a tuple with the nominal values, and the standard deviations.

The returned matrix can be inverted, thanks to the fact that it is a unumpy.matrix object instead of a numpy.matrix one.

define_vectorized_funcs()

source code 

Defines vectorized versions of functions from uncertainties.umath.

Some functions have their name translated, so as to follow NumPy's convention (example: math.acos -> numpy.arccos).


Variables Details [hide private]

to_nominal_values

Value:
numpy.vectorize(uncertainties.nominal_value, otypes= [float], doc= ("A\
pplies uncertainties.nominal_value to the elements of" " a NumPy (or u\
numpy) array (this includes matrices)."))

to_std_devs

Value:
numpy.vectorize(uncertainties.std_dev, otypes= [float], doc= ("Returns\
 the standard deviation of the numbers with uncertainties" " contained\
 in a NumPy array, or zero for other objects."))

_uarray

Value:
numpy.vectorize(lambda v, s: uncertainties.Variable(v, s), otypes= [ob\
ject])

arccos

Value:
<numpy.lib.function_base.vectorize object at 0x10bb64190>

arccosh

Value:
<numpy.lib.function_base.vectorize object at 0x10bb64210>

arcsin

Value:
<numpy.lib.function_base.vectorize object at 0x10bb64250>

arctan

Value:
<numpy.lib.function_base.vectorize object at 0x10bb642d0>

arctan2

Value:
<numpy.lib.function_base.vectorize object at 0x10bb64310>

arctanh

Value:
<numpy.lib.function_base.vectorize object at 0x10bb64350>

copysign

Value:
<numpy.lib.function_base.vectorize object at 0x10bb643d0>

degrees

Value:
<numpy.lib.function_base.vectorize object at 0x10bb64490>

lgamma

Value:
<numpy.lib.function_base.vectorize object at 0x10bb64790>

radians

Value:
<numpy.lib.function_base.vectorize object at 0x10bb648d0>