fipy.tools.numerix

Replacement module for NumPy

Attention

This module should be the only place in the code where numpy is explicitly imported and you should always import this module and not numpy in your own code. The documentation for numpy remains canonical for all functions and classes not explicitly documented here.

The functions provided in this module replace and augment the NumPy module. The functions work with Variables, arrays or numbers. For example, create a Variable.

>>> from fipy.variables.variable import Variable
>>> var = Variable(value=0)

Take the tangent of such a variable. The returned value is itself a Variable.

>>> v = tan(var)
>>> v
tan(Variable(value=array(0)))
>>> print(float(v))
0.0

Take the tangent of a int.

>>> tan(0)
0.0

Take the tangent of an array.

>>> print(tan(array((0, 0, 0))))
[ 0.  0.  0.]

Functions

L1norm(arr)

Taxicab or Manhattan norm of arr

L2norm(arr)

Euclidean norm of arr

LINFnorm(arr)

Infinity norm of arr

all(a[, axis, out])

Test whether all array elements along a given axis evaluate to True.

allclose(first, second[, rtol, atol])

Tests whether or not first and second are equal, subject to the given relative and absolute tolerances, such that.

allequal(first, second)

Returns true if every element of first is equal to the corresponding element of second.

dot(a1, a2[, axis])

return array of vector dot-products of v1 and v2 for arrays a1 and a2 of vectors v1 and v2

getShape(arr)

Return the shape of arr

getUnit(arr)

Return the unit of arr.

isclose(first, second[, rtol, atol])

Returns which elements of first and second are equal, subject to the given relative and absolute tolerances, such that.

nearest(data, points[, max_mem])

find the indices of data that are closest to points

put(arr, ids, values)

The opposite of take.

rank(a)

Get the rank of sequence a (the number of dimensions, not a matrix rank) The rank of a scalar is zero.

reshape(arr, shape)

Change the shape of arr to shape, as long as the product of all the lengths of all the axes is constant (the total number of elements does not change).

sqrtDot(a1, a2)

Return array of square roots of vector dot-products for arrays a1 and a2 of vectors v1 and v2

sum(arr[, axis])

The sum of all the elements of arr along the specified axis.

take(a, indices[, axis, fill_value])

Selects the elements of a corresponding to indices.

tostring(arr[, max_line_width, precision, ...])

Returns a textual representation of a number or field of numbers.

fipy.tools.numerix.L1norm(arr)

Taxicab or Manhattan norm of arr

\(\|\mathtt{arr}\|_1 = \sum_{j=1}^{n} |\mathtt{arr}_j|\) is the \(L^1\) norm of \(\mathtt{arr}\).

Parameters:

arr (ndarray) –

fipy.tools.numerix.L2norm(arr)

Euclidean norm of arr

\(\|\mathtt{arr}\|_2 = \sqrt{\sum_{j=1}^{n} |\mathtt{arr}_j|^2}\) is the \(L^2\) norm of \(\mathtt{arr}\).

Parameters:

arr (ndarray) –

fipy.tools.numerix.LINFnorm(arr)

Infinity norm of arr

\(\|\mathtt{arr}\|_\infty = [\sum_{j=1}^{n} |\mathtt{arr}_j|^\infty]^\infty = \underset{j}{\max} |\mathtt{arr}_j|\) is the \(L^\infty\) norm of \(\mathtt{arr}\).

Parameters:

arr (ndarray) –

fipy.tools.numerix.all(a, axis=None, out=None)

Test whether all array elements along a given axis evaluate to True.

Parameters:
  • a (array_like) – Input array or object that can be converted to an array.

  • axis (int, optional) – Axis along which an logical AND is performed. The default (axis = None) is to perform a logical AND over a flattened input array. axis may be negative, in which case it counts from the last to the first axis.

  • out (ndarray, optional) – Alternative output array in which to place the result. It must have the same shape as the expected output and the type is preserved.

fipy.tools.numerix.allclose(first, second, rtol=1e-05, atol=1e-08)

Tests whether or not first and second are equal, subject to the given relative and absolute tolerances, such that:

|first - second| < atol + rtol * |second|

This means essentially that both elements are small compared to atol or their difference divided by second’s value is small compared to rtol.

fipy.tools.numerix.allequal(first, second)

Returns true if every element of first is equal to the corresponding element of second.

fipy.tools.numerix.dot(a1, a2, axis=0)

return array of vector dot-products of v1 and v2 for arrays a1 and a2 of vectors v1 and v2

We can’t use numpy.dot() on an array of vectors

Test that Variables are returned as Variables.

>>> from fipy.meshes import Grid2D
>>> mesh = Grid2D(nx=2, ny=1)
>>> from fipy.variables.cellVariable import CellVariable
>>> v1 = CellVariable(mesh=mesh, value=((0, 1), (2, 3)), rank=1)
>>> v2 = CellVariable(mesh=mesh, value=((0, 1), (2, 3)), rank=1)
>>> dot(v1, v2)._variableClass
<class 'fipy.variables.cellVariable.CellVariable'>
>>> dot(v2, v1)._variableClass
<class 'fipy.variables.cellVariable.CellVariable'>
>>> print(rank(dot(v2, v1)))
0
>>> print(dot(v1, v2))
[ 4 10]
>>> dot(v1, v1)._variableClass
<class 'fipy.variables.cellVariable.CellVariable'>
>>> print(dot(v1, v1))
[ 4 10]
>>> v3 = array(((0, 1), (2, 3)))
>>> print(isinstance(dot(v3, v3), type(array(1))))
1
>>> print(dot(v3, v3))
[ 4 10]
fipy.tools.numerix.getShape(arr)

Return the shape of arr

>>> getShape(1)
()
>>> getShape(1.)
()
>>> from fipy.variables.variable import Variable
>>> getShape(Variable(1))
()
>>> getShape(Variable(1.))
()
>>> getShape(Variable(1., unit="m"))
()
>>> getShape(Variable("1 m"))
()
fipy.tools.numerix.getUnit(arr)

Return the unit of arr.

If arr has no units, returns 1.

fipy.tools.numerix.isclose(first, second, rtol=1e-05, atol=1e-08)

Returns which elements of first and second are equal, subject to the given relative and absolute tolerances, such that:

|first - second| < atol + rtol * |second|

This means essentially that both elements are small compared to atol or their difference divided by second’s value is small compared to rtol.

fipy.tools.numerix.nearest(data, points, max_mem=100000000.0)

find the indices of data that are closest to points

>>> from fipy import *
>>> m0 = Grid2D(dx=(.1, 1., 10.), dy=(.1, 1., 10.))
>>> m1 = Grid2D(nx=2, ny=2, dx=5., dy=5.)
>>> print(nearest(m0.cellCenters.globalValue, m1.cellCenters.globalValue))
[4 5 7 8]
>>> print(nearest(m0.cellCenters.globalValue, m1.cellCenters.globalValue, max_mem=100))
[4 5 7 8]
>>> print(nearest(m0.cellCenters.globalValue, m1.cellCenters.globalValue, max_mem=10000))
[4 5 7 8]
fipy.tools.numerix.put(arr, ids, values)

The opposite of take. The values of arr at the locations specified by ids are set to the corresponding value of values.

The following is to test improvements to puts with masked arrays. Places in the code were assuming incorrect put behavior.

>>> maskValue = 999999
>>> arr = zeros(3, 'l')
>>> ids = MA.masked_values((2, maskValue), maskValue)
>>> values = MA.masked_values((4, maskValue), maskValue)
>>> put(arr, ids, values) ## this should work
>>> print(arr)
[0 0 4]
>>> arr = MA.masked_values((maskValue, 5, 10), maskValue)
>>> ids = MA.masked_values((2, maskValue), maskValue)
>>> values = MA.masked_values((4, maskValue), maskValue)
>>> put(arr, ids, values)
>>> print(arr) ## works as expected
[-- 5 4]
>>> arr = MA.masked_values((maskValue, 5, 10), maskValue)
>>> ids = MA.masked_values((maskValue, 2), maskValue)
>>> values = MA.masked_values((4, maskValue), maskValue)
>>> put(arr, ids, values)
>>> print(arr) ## should be [-- 5 --] maybe??
[-- 5 999999]
fipy.tools.numerix.rank(a)

Get the rank of sequence a (the number of dimensions, not a matrix rank) The rank of a scalar is zero.

Note

The rank of a MeshVariable is for any single element. E.g., A CellVariable containing scalars at each cell, and defined on a 9 element Grid1D, has rank 0. If it is defined on a 3x3 Grid2D, it is still rank 0.

fipy.tools.numerix.reshape(arr, shape)

Change the shape of arr to shape, as long as the product of all the lengths of all the axes is constant (the total number of elements does not change).

fipy.tools.numerix.sqrtDot(a1, a2)

Return array of square roots of vector dot-products for arrays a1 and a2 of vectors v1 and v2

Usually used with v1==v2 to return magnitude of v1.

fipy.tools.numerix.sum(arr, axis=0)

The sum of all the elements of arr along the specified axis.

fipy.tools.numerix.take(a, indices, axis=0, fill_value=None)

Selects the elements of a corresponding to indices.

fipy.tools.numerix.tostring(arr, max_line_width=75, precision=8, suppress_small=False, separator=' ', array_output=0)

Returns a textual representation of a number or field of numbers. Each dimension is indicated by a pair of matching square brackets ([]), within which each subset of the field is output. The orientation of the dimensions is as follows: the last (rightmost) dimension is always horizontal, so that the frequent rank-1 fields use a minimum of screen real-estate. The next-to-last dimension is displayed vertically if present and any earlier dimension is displayed with additional bracket divisions.

>>> from fipy import Variable
>>> print(tostring(Variable((1, 0, 11.2345)), precision=1))
[  1.    0.   11.2]
>>> print(tostring(array((1, 2)), precision=5))
[1 2]
>>> print(tostring(array((1.12345, 2.79)), precision=2))
[ 1.12  2.79]
>>> print(tostring(1))
1
>>> print(tostring(array(1)))
1
>>> print(tostring(array([1.23345]), precision=2))
[ 1.23]
>>> print(tostring(array([1]), precision=2))
[1]
>>> print(tostring(1.123456, precision=2))
1.12
>>> print(tostring(array(1.123456), precision=3))
1.123
Parameters:
  • max_line_width (int) – Maximum number of characters used in a single line. Default is sys.output_line_width or 77.

  • precision (int) – Number of digits after the decimal point. Default is sys.float_output_precision or 8.

  • suppress_small (bool) – Whether small values should be suppressed (and output as 0). Default is sys.float_output_suppress_small or False.

  • separator (str) – What character string to place between two numbers.

  • array_output (bool) – unused

Last updated on Jun 26, 2024. Created using Sphinx 7.1.2.