4. arraytools — A collection of numerical utilities.

This module contains a large collection of numerical utilities. Many of them are related to processing arrays. Some are similar to existing NumPy functions but offer some extended functionality.

Note

While these functions were historically developed for pyFormex, this module only depends on numpy and can be used outside of pyFormex without changes.

4.1. Variables defined in module arraytools

arraytools.Float = <class 'numpy.float32'>

Single-precision floating-point number type, compatible with C float. Character code: 'f'. Canonical name: np.single. Alias on this platform: np.float32: 32-bit-precision floating-point number type: sign bit, 8 bits exponent, 23 bits mantissa.

arraytools.Int = <class 'numpy.int32'>

Signed integer type, compatible with C int. Character code: 'i'. Canonical name: np.intc. Alias on this platform: np.int32: 32-bit signed integer (-2147483648 to 2147483647).

arraytools.DEG = 0.017453292519943295

multiplier to transform degrees to radians = pi/180.

Type:float
arraytools.RAD = 1.0

multiplier to transform radians to radians

Type:float
arraytools.golden_ratio = 1.618033988749895

golden ratio is defined as 0.5 * (1.0 + sqrt(5.))

4.2. Functions defined in module arraytools

arraytools.isInt(obj)[source]

Test if an object is an integer number.

Returns:bool – True if the object is a single integer number (a Python int or a numpy.integer type), else False.

Examples

>>> isInt(1)
True
>>> isInt(np.arange(3)[1])
True
arraytools.isFloat(obj)[source]

Test if an object is a floating point number.

Returns:bool – True if the object is a single floating point number (a Python float or a numpy.floating type), else False.

Examples

>>> isFloat(1.)
True
>>> isFloat(np.array([1,2],dtype=np.float32)[1])
True
arraytools.isNum(obj)[source]

Test if an object is an integer or a floating point number.

Returns:bool – True if the object is a single integer or floating point number, else False. The type of the object can be either a Python int or float or a numpy integer or floating.

Examples

>>> isNum(1)
True
>>> isNum(1.0)
True
>>> isNum(np.array([1,2],dtype=np.int32)[1])
True
>>> isNum(np.array([1,2],dtype=np.float32)[1])
True
arraytools.checkInt(value, min=None, max=None)[source]

Check that a value is an int in the range min..max.

Parameters:
  • value (int-like) – The value to check.
  • min (int, optional) – If provided, minimal value to be accepted.
  • max (int, optional) – If provided, maximal value to be accepted.
Returns:

checked_int (int) – An integer not exceeding the provided boundaries.

Raises:

ValueError: – If the value is not convertible to an integer type or exceeds one of the specified boundaries.

Examples

>>> checkInt(1)
1
>>> checkInt(1,min=0,max=1)
1
>>> checkInt('2',min=0)
2
arraytools.checkFloat(value, min=None, max=None)[source]

Check that a value is a float in the range min..max.

Parameters:
  • value (float-like) – The value to check
  • min (float-like, optional) – If provided, minimal value to be accepted.
  • max (float-like, optional) – If provided, maximal value to be accepted.
Returns:

checked_float (float) – A float not exceeding the provided boundaries.

Raises:

ValueError: – If the value is not convertible to a float type or exceeds one of the specified boundaries.

Examples

>>> checkFloat(1)
1.0
>>> checkFloat(1,min=0,max=1)
1.0
>>> checkFloat('2',min=0)
2.0
arraytools.checkBroadcast(shape1, shape2)[source]

Check that two array shapes are broadcast compatible.

In many numerical operations, NumPy will automatically broadcast arrays of different shapes to a single shape, if they have broadcast compatible shapes. Two array shapes are broadcast compatible if, in all the last dimensions that exist in both arrays, either the shape of both arrays has the same length, or one of the shapes has a length 1.

Parameters:
  • shape1 (tuple of ints) – Shape of first array
  • shape2 (tuple of ints) – Shape of second array
Returns:

tuple of ints – The broadcasted shape of the arrays.

Raises:

ValueError: Shapes are not broadcast compatible – If the two shapes can not be broadcast to a single one.

Examples

>>> checkBroadcast((8,1,6,1),(7,1,5))
(8, 7, 6, 5)
>>> checkBroadcast((5,4),(1,))
(5, 4)
>>> checkBroadcast((5,4),(4,))
(5, 4)
>>> checkBroadcast((15,3,5),(15,1,5))
(15, 3, 5)
>>> checkBroadcast((15,3,5),(3,5))
(15, 3, 5)
>>> checkBroadcast((15,3,5),(3,1))
(15, 3, 5)
>>> checkBroadcast((7,1,5),(8,1,6,1))
(8, 7, 6, 5)
arraytools.checkArray(a, shape=None, kind=None, allow=None, size=None, ndim=None, bcast=None, subok=False, addaxis=False)[source]

Check that an array a has the correct shape, type and/or size.

Parameters:
  • a (array_like) – An instance of a numpy.ndarray or a subclass thereof, or anything that can be converted into a numpy array.
  • shape (tuple of ints, optional) – If provided, the shape of the array should match this value along each axis for which a nonzero value is specified. The length of the shape tuple should also match (unless addaxis=True is provided, see below).
  • kind (dtype.kind character code, optional) – If provided, the array’s dtype.kind should match this value, or one of the values in allow, if provided.
  • allow (string of dtype.kind character codes, optional) – If provided, and kind is also specified, any of the specified array types will also be accepted if it is convertible to the specified kind. See also Notes below.
  • size (int, optional) – If provided, the total array size should match this value.
  • ndim (int, optional) – If provided the input array should have exactly ndim dimensions (unless addaxis=True is provided, see below).
  • bcast (tuple of ints, optional) – If provided, the array’s shape should be broadcast comaptible with the specified shape.
  • subok (bool, optional) – If True, the returned array is of the same class as the input array a, if possible. If False (default), the returned array is always of the base type numpy.ndarray.
  • addaxis (bool, optional) – If False (default), and either ndim or shape are specified, the input array should have precisely the number of dimensions specified by ndim or the length of shape. If True, an input array with less dimensions will automatically be transformed by adding length 1 axes at the start of the shape tuple until the correct dimension is reached.
Returns:

array – The checked_array is equivalent to the input data. It has the same contents and shape. It also has the same type, unless kind is is provided, in which case the result is converted to this type. If subok=True was provided, the returned array will be of the same array subclass as the input a, if possible.

Raises:

ValueError: invalid array – The input data failed for one of more of the tests requested by the provided parameters.

Notes

Currently, the only allowed conversion from an allow type to kind type, is to ‘f’. Thus specifiying kind='f', allow='i' will accept integer input but return float32 output.

See also

checkArray1D()

Examples

>>> checkArray([1,2])
array([1, 2])
>>> checkArray([1,2],shape=(2,))
array([1, 2])
>>> checkArray([[1,2],[3,4]],shape=(2,-1))
array([[1, 2], [3, 4]])
>>> checkArray([1,2],kind='i')
array([1, 2])
>>> checkArray([1,2],kind='f',allow='i')
array([ 1.,  2.])
>>> checkArray([1,2],size=2)
array([1, 2])
>>> checkArray([1,2],ndim=1)
array([1, 2])
>>> checkArray([[1,2],[3,4]],bcast=(3,1,2))
array([[1, 2], [3, 4]])
>>> checkArray([[1,2],[3,4]],ndim=3,addaxis=True)
array([[[1, 2],
        [3, 4]]])
>>> checkArray([[1,2],[3,4]],shape=(-1,-1,2),addaxis=True)
array([[[1, 2],
        [3, 4]]])
arraytools.checkArray1D(a, kind=None, allow=None, size=None)[source]

Check and force an array to be 1D.

This is equivalent to calling checkArray() without the shape and ndim parameters, and then turning the result into a 1D array.

:param See checkArray().:

Returns:1D array – The checked_array holds the same data as the input, but the shape is rveled to 1D. It also has the same type, unless kind is is provided, in which case the result is converted to this type.

Examples

>>> checkArray1D([[1,2],[3,4]],size=4)
array([1, 2, 3, 4])
arraytools.checkUniqueNumbers(nrs, nmin=0, nmax=None)[source]

Check that an array contains a set of unique integers in a given range.

This functions tests that all integer numbers in the array are within the range math:nmin <= i < nmax. Default range is [0,unlimited].

Parameters:
  • nrs (array_like, int) – Input array with integers to check against provided limits.
  • nmin (int or None, optional) – If not None, no value in a should be lower than this.
  • nmax (-) –
  • nmax – If provided, no value in a should be higher than this.
Returns:

1D int array – Containing the sorted unique numbers from the input.

Raises:

ValueError – If the numbers are not unique or some input value surpasses one of the specified limmits.

Examples

>>> checkUniqueNumbers([0,5,1,7,2])
array([0, 1, 2, 5, 7])
>>> checkUniqueNumbers([0,5,1,7,-2],nmin=None)
array([-2, 0, 1, 5, 7])
arraytools.growAxis(a, add, axis=-1, fill=0)[source]

Increase the length of an array axis.

Parameters:
  • a (array_like) – The array in which to extend n axis.
  • add (int) – The length over which the specified axis should grow. If add<=0, the array is returned unchanged.
  • axis (int) – Position of the target axis in the array. Default is last (-1).
  • fill (int or float) – Value to set the new elements along the grown axis to.
Returns:

array – Same type and data as a, but length of specified axis has been increased with a value add and the new elements are filled with the value fill.

Raises:

ValueError: – If the specified axis exceeds the array dimensions.

Examples

>>> growAxis([[1,2,3],[4,5,6]],2)
array([[1, 2, 3, 0, 0],
       [4, 5, 6, 0, 0]])
>>> growAxis([[1,2,3],[4,5,6]],1,axis=0,fill=-3)
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [-3, -3, -3]])
>>> growAxis([[1,2,3],[4,5,6]],-1)
array([[1, 2, 3],
       [4, 5, 6]])
arraytools.reorderAxis(a, order, axis=-1)[source]

Reorder the planes of an array along the specified axis.

Parameters:
  • a (array_like) – The array in which to reorder the elements.
  • order (int array_like | str) –

    Specifies how to reorder the elements. It can be an integer index which should be a permutation of arange(a.shape[axis]). Each value in the index specified the old index of the elements that should be placed at its position. This is equivalent to a.take(order,axis).

    order can also be one of the following predefined sting values, resulting in the corresponding renumbering scheme being generated:

    • ’reverse’: the elements along axis are placed in reverse order
    • ’random’: the elements along axis are placed in random order
  • axis (int) – The axis of the array along which the elements are to be reordered. Default is last (-1).
Returns:

array – Same type and data as a, but the element planes are along axis have been reordered.

Examples

>>> reorderAxis([[1,2,3],[4,5,6]], [2,0,1])
array([[3, 1, 2],
       [6, 4, 5]])
arraytools.reverseAxis(a, axis=-1)[source]

Reverse the order of the elements along an axis.

Parameters:
  • a (array_like) – The array in which to reorder the elements.
  • axis (int) – The axis of the array along which the elements are to be reordered. Default is last (-1).
Returns:

array – Same type and data as a, but the elements along axis are now in reversed order.

Note

This function is especially useful if axis has a computed value. If the axis is known in advance, it is more efficient to use an indexing operation. Thus reverseAxis(A,-1) is equivalent to A[…,::-1].

Examples

>>> A = np.array([[1,2,3],[4,5,6]])
>>> reverseAxis(A)
array([[3, 2, 1],
       [6, 5, 4]])
>>> A[...,::-1]
array([[3, 2, 1],
       [6, 5, 4]])
arraytools.interleave(a, b)[source]

Interleave two arrays along their first axis.

Parameters:
  • a (array_like) – First array
  • b (array_like) – Second array, with same type and shape as a, except that the first dimension may be one less than that of a, and if data type of b can be one that is convertible to that of a.
Returns:

array – An array with interleaved rows from a and b. The array has the datatype of a and its first axis has the combined length of that of a and b.

Examples

>>> interleave(np.arange(4), 10*np.arange(3))
array([ 0,  0,  1, 10,  2, 20,  3])
>>> a = np.arange(8).reshape(2,4)
>>> print(interleave(a,10*a))
[[ 0  1  2  3]
 [ 0 10 20 30]
 [ 4  5  6  7]
 [40 50 60 70]]
arraytools.multiplex(a, n, axis, warn=True)[source]

Multiplex an array over a length n in direction of a new axis.

Inserts a new axis in the array at the specified position and repeats the data of the array n times in the direction of the new axis.

Parameters:
  • a (array_like) – The input array.
  • n (int) – Number of times to repeat the data in direction of axis.
  • axis (int, optional) – Position of the new axis in the expanded array. Should be in the range -a.ndim..a.ndim.
Returns:

array – An array with n times the original data repeated in the direction of the specified axis.

See also

repeatValues()
Repeat values in a 1-dim array a number of times

Examples

>>> a = np.arange(6).reshape(2,3)
>>> print(a)
[[0 1 2]
 [3 4 5]]
>>> print(multiplex(a,4,-1))
[[[0 0 0 0]
  [1 1 1 1]
  [2 2 2 2]]
<BLANKLINE>
 [[3 3 3 3]
  [4 4 4 4]
  [5 5 5 5]]]
>>> print(multiplex(a,4,-2))
[[[0 1 2]
  [0 1 2]
  [0 1 2]
  [0 1 2]]
<BLANKLINE>
 [[3 4 5]
  [3 4 5]
  [3 4 5]
  [3 4 5]]]
arraytools.repeatValues(a, n)[source]

Repeat values in a 1-dim array a number of times.

Parameters:
  • a (array_like, 1-dim) – The input array. Can be a list or a single element.
  • n (int array_like, 1-dim) – Number of times to repeat the corresponding value of a. If n has less elements than a, it is reused until the end of a is reached.
Returns:

array – An 1-dim array of the same dtype as a with the value a[i] repeated n[i] times.

See also

multiplex()
Multiplex an array over a length n in direction of a new axis

Examples

>>> repeatValues(2,3)
array([2, 2, 2])
>>> repeatValues([2,3],2)
array([2, 2, 3, 3])
>>> repeatValues([2,3,4],[2,3])
array([2, 2, 3, 3, 3, 4, 4])
>>> repeatValues(1.6,[3,5])
array([ 1.6,  1.6,  1.6])
arraytools.concat(al, axis=0)[source]

Smart array concatenation ignoring empty arrays.

Parameters:
  • al (list of arrays) – All arrays should have same shape except for the length of the concatenation axis, or be empty arrays.
  • axis (int) – The axis along which the arrays are concatenated.
Returns:

array – The concatenation of all non-empty arrays in the list, or an empty array if all arrays in the list are empty.

Note

This is just like numpy.concatenate, but allows empty arrays in the list and silently ignores them.

Examples

>>> concat([np.array([0,1]),np.array([]),np.array([2,3])])
array([0, 1, 2, 3])
arraytools.splitrange(n, nblk)[source]

Split a range of integers 0..n in almost equal sized slices.

Parameters:
  • n (int) – Highest integer value in the range.
  • nblk (int) – Number of blocks to split into. Should be <= n to allow splitting.
Returns:

1-dim int array – If nblk <= n, returns the boundaries that divide the integers in the range 0..n in nblk almost equal slices. The outer boundaries 0 and n are included, so the length of the array is nblk+1. If nblk >= n, returns range(n+1), thus all slices have length 1.

Examples

>>> splitrange(7,3)
array([0, 2, 5, 7])
arraytools.splitar(a, nblk, axis=0, close=False)[source]

Split an array in nblk subarrays along a given axis.

Parameters:
  • a (array_like) – Array to be divided in subarrays.
  • nblk (int) – Number of subarrays to obtain. The subarrays will be of almost the same size.
  • axis (int:) – Axis along which to split the array (default 0)
  • close (bool) – If True, the last item of each block will be repeated as the first item of the next block.
Returns:

list of arrays – A list of subarrays obtained by splitting a along the specified axis. All arrays have almost the same shape. The number of arrays is equal to nblk, unless nblk is larger than a.shape[axis], in which case a a list with only the original array is returned.

Examples

>>> splitar(np.arange(7),3)
[array([0, 1]), array([2, 3, 4]), array([5, 6])]
>>> splitar(np.arange(7),3,close=True)
[array([0, 1, 2]), array([2, 3, 4]), array([4, 5, 6])]
>>> X = np.array([[0.,1.,2.],[3.,4.,5.]])
>>> splitar(X,2)
[array([[ 0.,  1.,  2.]]), array([[ 3.,  4.,  5.]])]
>>> splitar(np.matrix(X),2,axis=-1)
[matrix([[ 0.,  1.],
         [ 3.,  4.]]), matrix([[ 2.],
         [ 5.]])]
>>> splitar(X,3)
[array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.]])]
arraytools.minmax(a, axis=-1)[source]

Compute the minimum and maximum along an axis.

Parameters:
  • a (array_like) – The data array for which to compute the minimum and maximum.
  • axis (int) – The array axis along which to compute the minimum and maximum.
Returns:

array – The array has the same dtype as a. It also has the same shape, except for the specified axis, which will have a length of 2. The first value along this axis holds the minimum value of the input, the second holds the maximum value.

Examples

>>> a = np.array([[ [1.,0.,0.], [0.,1.,0.] ],
...            [ [2.,0.,0.], [0.,2.,0.] ] ])
>>> print(minmax(a,axis=1))
[[[ 0. 0. 0.]
  [ 1. 1. 0.]]
<BLANKLINE>
 [[ 0. 0. 0.]
  [ 2. 2. 0.]]]
arraytools.stretch(a, min=None, max=None, axis=None)[source]

Scale the values of an array to fill a given range.

Parameters:
  • a (array_like, int or float) – Input data.
  • min (int or float, optional) – The targeted minimum value in the array. Same type as a. If not provided, the minimum of a is used.
  • max (int or float, optional) – The targeted maximum value in the array. Same type as a. If not provided, the maximum of a is used.
  • axis (int, optional) – If provided, each slice along the specified axis is independently scaled.
Returns:

array – Array of the same type and size as the input array, but in which the values have been linearly scaled to fill the specified range.

Examples

>>> stretch([1.,2.,3.],min=0,max=1)
array([ 0. , 0.5, 1. ])
>>> A = np.arange(6).reshape(2,3)
>>> stretch(A,min=20,max=30)
array([[20, 22, 24],
       [26, 28, 30]])
>>> stretch(A,min=20,max=30,axis=1)
array([[20, 25, 30],
       [20, 25, 30]])
>>> stretch(A,max=30)
array([[ 0,  6, 12],
       [18, 24, 30]])
>>> stretch(A,min=2,axis=1)
array([[2, 4, 5],
       [2, 4, 5]])
>>> stretch(A.astype(Float),min=2,axis=1)
array([[ 2. ,  3.5,  5. ],
       [ 2. ,  3.5,  5. ]])
arraytools.stringar(s, a)[source]

Nicely format a string followed by an array.

Parameters:
  • s (str) – String to appear before the formatted array
  • a (array) – Array to be formatted after the string, with proper vertical alignment
Returns:

str – A multiline string where the first line consists of the string s and the first line of the formatted array, and the next lines hold the remainder of the array lines, properly indented to align with the first line of the array.

Examples

>>> print(stringar("Indented array: ",np.arange(4).reshape(2,2)))
Indented array: [[0 1]
                 [2 3]]
arraytools.array2str(a)[source]

String representation of an array.

This creates a string representation of an array. It is visually equivalent with numpy.ndarray.__repr__ without the dtype, except for ‘uint.’ types.

Note

This function can be used to set the default string representation of numpy arrays, using the following:

import numpy as np
np.set_string_function(array2str)

To reset it to the default, do:

np.set_string_function(None)

Because this reference manual was created with the default numpy routine replaced with ours, you will never see the dtype, except for uint types. See also the examples below.

Parameters:a (array) – Any numpy.ndarray object.
Returns:
  • The string representation of the array as created by its
  • __repr__ method, except that the dtype is left away.

Examples

>>> a = np.arange(5).astype(np.int8)
>>> print(array2str(a))
array([0, 1, 2, 3, 4])
>>> a
array([0, 1, 2, 3, 4])

Reset the numpy string function to its default. >>> np.set_string_function(None) >>> a array([0, 1, 2, 3, 4], dtype=int8)

Change back to ours. >>> np.set_string_function(array2str) >>> a array([0, 1, 2, 3, 4])

arraytools.printar(s, a)[source]

Print a string followed by a vertically aligned array.

Parameters:
  • s (str) – String to appear before the formatted array
  • a (array) – Array to be formatted after the string, with proper vertical alignment

Note

This is a shorthand for print(stringar(s,a)).

Examples

>>> printar("Indented array: ",np.arange(4).reshape(2,2))
Indented array: [[0 1]
                 [2 3]]
arraytools.writeArray(fil, array, sep=' ')[source]

Write an array to an open file.

This uses numpy.tofile() to write an array to an open file.

Parameters:
  • fil (file or str) – Open file object or filename.
  • array (array_like) – The array to write to the file.
  • sep (str) – If empty, the array is written in binary mode. If not empty, the array is written in text mode, with this string as separator between the elements.

See also

readArray()

arraytools.readArray(fil, dtype, shape, sep=' ')[source]

Read data for an array with known size and type from an open file.

This uses numpy.fromfile() to read an array with known shape and data type from an open file.

Parameters:
  • fil (file or str) – Open file object or filename.
  • dtype (data-type) – Data type of the array to be read.
  • shape (tuple of ints) – The shape of the array to be read.
  • sep (str) – If not empty, the array is read in text mode, with this string as separator between the elements. If empty, the array is read in binary mode and an extra ‘n’ after the data will be stripped off

See also

writeArray()

arraytools.powers(x, n)[source]

Compute all the powers of x from zero up to n.

Parameters:
  • x (int, float or array (int,float)) – The number or numbers to be raised to the specified powers.
  • n (int) – Maximal power to raise the numbers to.
Returns:

powers (list) – A list of numbers or arrays of the same shape and type as the input. The list contains N+1 items, being the input raised to the powers in range(n+1).

Examples

>>> powers(2,5)
[1, 2, 4, 8, 16, 32]
>>> powers(np.array([1.0,2.0]),5)
[array([ 1.,  1.]), array([ 1.,  2.]), array([ 1.,  4.]),     array([ 1.,  8.]), array([  1.,  16.]), array([  1.,  32.])]
arraytools.sind(arg, angle_spec=0.017453292519943295)[source]

Return the sine of an angle in degrees.

Parameters:
  • arg (float number or array) – Angle(s) for which the sine is to be returned. By default, angles are specified in degrees (see angle_spec).
  • angle_spec (DEG, RAD or float) – Multiplier to apply to arg before taking the sine. The default multiplier DEG makes the argument being intrepreted as an angle in degrees. Use RAD when angles are specified in radians.
Returns:

float number or array – The sine of the input angle(s)

Examples

>>> print(sind(30), sind(pi/6,RAD))
0.5 0.5
>>> sind(np.array([0.,30.,45.,60.,90.]))
array([ 0.  ,  0.5 ,  0.71,  0.87,  1.  ])
arraytools.cosd(arg, angle_spec=0.017453292519943295)[source]

Return the cosine of an angle in degrees.

Parameters:
  • arg (float number or array) – Angle(s) for which the cosine is to be returned. By default, angles are specified in degrees (see angle_spec).
  • angle_spec (DEG, RAD or float) – Multiplier to apply to arg before taking the sine. The default multiplier DEG makes the argument being intrepreted as an angle in degrees. Use RAD when angles are specified in radians.
Returns:

float number or array – The cosine of the input angle(s)

Examples

>>> print(cosd(60), cosd(pi/3,RAD))
0.5 0.5
arraytools.tand(arg, angle_spec=0.017453292519943295)[source]

Return the tangens of an angle in degrees.

Parameters:
  • arg (float number or array) – Angle(s) for which the tangens is to be returned. By default, angles are specified in degrees (see angle_spec).
  • angle_spec (DEG, RAD or float) – Multiplier to apply to arg before taking the sine. The default multiplier DEG makes the argument being intrepreted as an angle in degrees. Use RAD when angles are specified in radians.
Returns:

float number or array – The tangens of the input angle(s)

Examples

>>> print(tand(45), tand(pi/4,RAD))
1.0 1.0
arraytools.arcsind(arg, angle_spec=0.017453292519943295)[source]

Return the angle whose sine is equal to the argument.

Parameters:
  • arg (float number or array, in the range -1.0 to 1.0.) – Value(s) for which to return the arcsine.
  • angle_spec (DEG, RAD or float, nonzero.) – Divisor applied to the resulting angles before returning. The default divisor DEG makes the angles be returned in degrees. Use RAD to get angles in radians.
Returns:

float number or array – The angle(s) for which the input value(s) is/are the cosine. The default angle_spec=DEG returns values in the range -90 to +90.

Examples

>>> print(f"{arcsind(0.5):.1f} {arcsind(1.0,RAD):.4f}")
30.0 1.5708
>>> arcsind(-1)
-90.0
>>> arcsind(1)
90.0
arraytools.arccosd(arg, angle_spec=0.017453292519943295)[source]

Return the angle whose cosine is equal to the argument.

Parameters:
  • arg (float number or array, in the range -1.0 to 1.0.) – Value(s) for which to return the arccos.
  • angle_spec (DEG, RAD or float, nonzero.) – Divisor applied to the resulting angles before returning. The default divisor DEG makes the angles be returned in degrees. Use RAD to get angles in radians.
Returns:

float number or array – The angle(s) for which the input value(s) is/are the cosine. The default angle_spec=DEG returns values in the range 0 to 180.

Examples

>>> print(f"{arccosd(0.5):.1f} {arccosd(-1.0,RAD):.4f}")
60.0 3.1416
>>> arccosd(np.array([-1,0,1]))
array([ 180.,   90.,    0.])
arraytools.arctand(arg, angle_spec=0.017453292519943295)[source]

Return the angle whose tangens is equal to the argument.

Parameters:
  • arg (float number or array.) – Value(s) for which to return the arctan.
  • angle_spec (DEG, RAD or float, nonzero.) – Divisor applied to the resulting angles before returning. The default divisor DEG makes the angles be returned in degrees. Use RAD to get angles in radians.
Returns:

float number or array – The angle(s) for which the input value(s) is/are the tangens. The default angle_spec=DEG returns values in the range -90 to +90.

Examples

>>> print(f"{arctand(1.0):.1f} {arctand(-1.0,RAD):.4f}")
45.0 -0.7854
>>> arctand(np.array([-np.inf,-1,0,1,np.inf]))
array([-90., -45.,   0.,  45.,  90.])
arraytools.arctand2(sin, cos, angle_spec=0.017453292519943295)[source]

Return the angle whose sine and cosine values are given.

Parameters:
  • sin (float number or array with same shape as cos.) – Sine value(s) for which to return the corresponding angle.
  • cos (float number or array with same shape as sin) – Cosine value(s) for which to return the corresponding angle.
  • angle_spec (DEG, RAD or float, nonzero.) – Divisor applied to the resulting angles before returning. The default divisor DEG makes the angles be returned in degrees. Use RAD to get angles in radians.
Returns:

float number or array with same shape as sin and cos. – The angle(s) for which the input value(s) are the sine and cosine. The default angle_spec=DEG returns values in the range [-180, 180].

Note

The input values sin and cos are not restricted to the [-1.,1.] range. The returned angle is that for which the tangens is given by sin/cos, but with a sine and cosine that have the same sign as the sin and cos values.

Examples

>>> print(f"{arctand2(0.0,-1.0):.1f} "
...       f"{arctand2(-sqrt(0.5),-sqrt(0.5),RAD):.4f}")
180.0 -2.3562
>>> arctand2(np.array([0., 1., 0., -1.]), np.array([1., 0., -1., 0.]))
array([   0.,   90.,  180.,  -90.])
>>> arctand2(2.,2.)
45.0
arraytools.niceLogSize(f)[source]

Return an integer estimate of the magnitude of a float number.

Parameters:f (float) – Value for which the integer magnitude has to be computed. The sign of the value is disregarded.
Returns:int – An integer magnitude estimator for the input.

Note

The returned value is the smallest integer e such that 10**e > abs(f). If positive, it is equal to the number of digits before the decimal point; if negative, it is equal to the number of leading zeros after the decimal point.

See also

nicenumber()

Examples

>>> print([ niceLogSize(a) for a in [1.3, 35679.23, 0.4, 0.0004567, -1.3] ])
[1, 5, 0, -3, 1]
arraytools.niceNumber(f, round=<ufunc 'ceil'>)[source]

Return a nice number close to abs(f).

A nice number is a number which only has only one significant digit (in the decimal system).

Parameters:
  • f (float) – A float number to approximate with a nice number. The sign of f is disregarded.
  • round (callable) – A function that rounds a float to the nearest integer. Useful functions are ceil, floor and round from either NumPy or Python’s math module. Default is numpy.ceil.
Returns:

float – A float value close to the input value, but having only a single decimal digit.

Examples

>>> numbers = [ 0.0837, 0.867, 8.5, 83.7, 93.7]
>>> [ str(niceNumber(f)) for f in numbers ]
['0.09', '0.9', '9.0', '90.0', '100.0']
>>> [ str(niceNumber(f,round=np.floor)) for f in numbers ]
['0.08', '0.8', '8.0', '80.0', '90.0']
>>> [ str(niceNumber(f,round=np.round)) for f in numbers ]
['0.08', '0.9', '8.0', '80.0', '90.0']
arraytools.isqrt(n)[source]

Compute the square root of an integer number.

Parameters:n (int) – An integer number that is a perfect square.
Returns:int – The square root from the input number
Raises:ValueError: – If the input integer is not a perfect square.

Examples

>>> isqrt(36)
6
arraytools.dotpr(A, B, axis=-1)[source]

Return the dot product of vectors of A and B in the direction of axis.

Parameters:
  • A (float array_like) – Array containing vectors in the direction of axis.
  • B (float array_like) – Array containing vectors in the direction of axis. Same shape as A, or broadcast-compatible.
  • axis (int) – Axis of A and B in which direction the vectors are layed out. Default is the last axis. A and B should have the same length along this axis.
Returns:

float array, shape as A and B with axis direction removed. – The elements contain the dot product of the vectors of A and B at that position.

Note

This multiplies the elements of the A and B and then sums them in the direction of the specified axis.

Examples

>>> A = np.array( [[1.0, 1.0], [1.0,-1.0], [0.0, 5.0]] )
>>> B = np.array( [[5.0, 3.0], [2.0, 3.0], [1.33,2.0]] )
>>> print(dotpr(A,B))
[ 8. -1. 10.]
>>> print(dotpr(A,B,0))
[ 7. 10.]
arraytools.length(A, axis=-1)[source]

Returns the length of the vectors of A in the direction of axis.

Parameters:
  • A (float array_like) – Array containing vectors in the direction of axis.
  • axis (int) – Axis of A in which direction the vectors are layed out. Default is the last axis. A and B shoud have the same length along this axis.
Returns:

float array, shape of A with axis direction removed. – The elements contain the length of the vector in A at that position.

Note

This is equivalent with sqrt(dotpr(A,A)).

Examples

>>> A = np.array( [[1.0, 1.0], [1.0,-1.0], [0.0, 5.0]] )
>>> print(length(A))
[ 1.41 1.41 5.  ]
>>> print(length(A,0))
[ 1.41 5.2 ]
arraytools.normalize(A, axis=-1, on_zeros='n', return_length=False, ignore_zeros=False)[source]

Normalize the vectors of A in the direction of axis.

Parameters:
  • A (float array_like) – Array containing vectors in the direction of axis.
  • axis (int) – Axis of A in which direction the vectors are layed out.
  • on_zeros ('n', 'e' or 'i') –

    Specifies how to treat occurrences of zero length vectors (having all components equal to zero):

    • ’n’: return a vector of nan values
    • ’e’: raise a ValueError
    • ’i’: ignore zero vectors and return them as such.
  • return_length (bool) – If True, also returns also the length of the original vectors.
  • ignore_zeros (bool) – (Deprecated) Equivalent to specifying on_zeros='i'.
Returns:

  • norm (float array) – Array with same shape as A but where each vector along axis has been rescaled so that its length is 1.
  • len (float array, optional) – Array with shape like A but with axis removed. The length of the original vectors in the direction of axis. Only returned if return_length=True provided.

Raises:

ValueError: Can not normalize zero-length vector – If any of the vectors of B is a zero vector.

Examples

>>> A = np.array( [[3.0, 3.0], [4.0,-3.0], [0.0, 0.0]] )
>>> print(normalize(A))
[[ 0.71  0.71]
 [ 0.8  -0.6 ]
 [  nan   nan]]
>>> print(normalize(A,on_zeros='i'))
[[ 0.71  0.71]
 [ 0.8  -0.6 ]
 [ 0.    0.  ]]
>>> print(normalize(A,0))
[[ 0.6   0.71]
 [ 0.8  -0.71]
 [ 0.    0.  ]]
>>> n,l = normalize(A,return_length=True)
>>> print(n)
[[ 0.71  0.71]
 [ 0.8  -0.6 ]
 [  nan   nan]]
>>> print(l)
[ 4.24  5.    0.  ]
arraytools.projection(A, B, axis=-1)[source]

Return the (signed) length of the projection of vectors of A on B.

Parameters:
  • A (float array_like) – Array containing vectors in the direction of axis.
  • B (float array_like) – Array containing vectors in the direction of axis. Same shape as A, or broadcast-compatible.
  • axis (int) – Axis of A and B in which direction the vectors are layed out. Default is the last axis. A and B should have the same length along this axis.
Returns:

float array, shape as A and B with axis direction removed. – The elements contain the length of the projections of vectors of A on the directions of the corresponding vectors of B.

Raises:

ValueError: Can not normalize zero-length vector – If any of the vectors of B is a zero vector.

Note

This returns dotpr(A,normalize(B)).

Examples

>>> A = [[2.,0.],[1.,1.],[0.,1.]]
>>> projection(A,[1.,0.])
array([ 2., 1., 0.])
>>> projection(A,[1.,1.])
array([ 1.41, 1.41, 0.71])
>>> projection(A,[[1.],[1.],[0.]],axis=0)
array([ 2.12,  0.71])
arraytools.parallel(A, B, axis=-1)[source]

Return the component of vector of A that is parallel to B.

Parameters:
  • B (A,) – Broadcast compatible arrays containing vectors in the direction of axis.
  • axis (int) – Axis of A and B in which direction the vectors are layed out. Default is the last axis. A and B should have the same dimension along this axis.
Returns:

float array, same shape as A and B. – The vectors in the axis direction are the vectors of A projected on the direction of the corresponding vectors of B.

See also

orthog()

Examples

>>> A = [[2.,0.],[1.,1.],[0.,1.]]
>>> parallel(A,[1.,0.])
array([[ 2.,  0.],
       [ 1.,  0.],
       [ 0.,  0.]])
>>> parallel(A,A)
array([[ 2.,  0.],
       [ 1.,  1.],
       [ 0.,  1.]])
>>> parallel(A,[[1.],[1.],[0.]],axis=0)
array([[ 1.5,  0.5],
       [ 1.5,  0.5],
       [ 0. ,  0. ]])
arraytools.orthog(A, B, axis=-1)[source]

Return the component of vector of A that is orthogonal to B.

Parameters:
  • A (float array_like) – Array containing vectors in the direction of axis.
  • B (float array_like) – Array containing vectors in the direction of axis. Same shape as A, or broadcast-compatible.
  • axis (int) – Axis of A and B in which direction the vectors are layed out. Default is the last axis. A and B should have the same length along this axis.
Returns:

float array, same shape as A and B. – The vectors in the axis direction are the components of the vectors of A orthogonal to the direction of the corresponding vectors of B.

See also

parallel()

Examples

>>> A = [[2.,0.],[1.,1.],[0.,1.]]
>>> orthog(A,[1.,0.])
array([[ 0.,  0.],
       [ 0.,  1.],
       [ 0.,  1.]])
>>> orthog(A,[[1.],[1.],[0.]],axis=0)
array([[ 0.5, -0.5],
       [-0.5,  0.5],
       [ 0. ,  1. ]])
arraytools.inside(p, mi, ma)[source]

Return true if point p is inside bbox defined by points mi and ma.

Parameters:
  • p (float array_like with shape (ndim,)) – Point to test against the boundaries.
  • mi (float array_like with shape (ndim,)) – Minimum values for the components of p
  • ma (float array_like with shape (ndim,)) – Maximum values for the components of p
Returns:

bool – True is all components are inside the specified limits, limits included. This means that the n-dimensional point p lies within the n-dimensional rectangular bounding box defined by the two n-dimensional points (mi,ma).

Examples

>>> inside([0.5,0.5],[0.,0.],[1.,1.])
True
>>> inside([0.,1.],[0.,0.],[1.,1.])
True
>>> inside([0.,1.1],[0.,0.],[1.,1.])
False
arraytools.unitVector(v)[source]

Return a unit vector in the direction of v.

Parameters:v (a single integer or a (3,) shaped float array_like) – If an int, it specifies one of the global axes (0,1,2). Else, it is a vector in 3D space.
Returns:(3,) shaped float array – A unit vector along the specified direction.

Examples

>>> unitVector(1)
array([ 0.,  1.,  0.])
>>> unitVector([0.,3.,4.])
array([ 0. ,  0.6,  0.8])
arraytools.rotationMatrix(angle, axis=None, angle_spec=0.017453292519943295)[source]

Create a 2D or 3D rotation matrix over angle, optionally around axis.

Parameters:
  • angle (float) – Rotation angle, by default in degrees.
  • axis (int or (3,) float array_like, optional) – If not provided, a 2D rotation matrix is returned. If provided, it specifies the rotation axis in a 3D world. It is either one of 0,1,2, specifying a global axis, or a vector with 3 components specifying an axis through the origin. The returned matrix is 3D.
  • angle_spec (float, DEG or RAD, optional) – The default (DEG) interpretes the angle in degrees. Use RAD to specify the angle in radians.
Returns:

float array – Rotation matrix which will rotate a vector over the specified angle. Shape is (3,3) if axis is specified, or (2,2) if not.

See also

rotationMatrix3()
subsequent rotation around 3 axes
rotmat()
rotation matrix specified by three points in space
trfmat()
transformation matrix to transform 3 points
rotMatrix()
rotation matrix transforming global axis 0 into a given vector
rotMatrix2()
rotation matrix that transforms one vector into another

Examples

>>> rotationMatrix(30.,1)
array([[ 0.87,  0.  , -0.5 ],
       [ 0.  ,  1.  ,  0.  ],
       [ 0.5 ,  0.  ,  0.87]])
>>> rotationMatrix(45.,[1.,1.,0.])
array([[ 0.85,  0.15, -0.5 ],
       [ 0.15,  0.85,  0.5 ],
       [ 0.5 , -0.5 ,  0.71]])
arraytools.rotationMatrix3(rx, ry, rz, angle_spec=0.017453292519943295)[source]

Create a rotation matrix defined by three angles.

This applies successive rotations about the 0, 1 and 2 axes, over the angles rx, ry and rz, respectively. These angles are also known as the cardan angles.

Parameters:
  • rx (float) – Rotation angle around the 0 axis.
  • ry (float) – Rotation angle around the 1 axis.
  • rz (float) – Rotation angle around the 2 axis.
  • angle_spec (float, DEG or RAD, optional) – The default (DEG) interpretes the angles in degrees. Use RAD to specify the angle in radians.
Returns:

float array (3,3) – Rotation matrix that performs the combined rotation equivalent to subsequent rotations around the three global axes.

See also

rotationMatrix()
rotation matrix specified by an axis and angle
cardanAngles()
find cardan angles that produce a given rotation matrix

Examples

>>> rotationMatrix3(60,45,30)
array([[ 0.61,  0.35, -0.71],
       [ 0.28,  0.74,  0.61],
       [ 0.74, -0.57,  0.35]])
arraytools.cardanAngles(R, angle_spec=0.017453292519943295)[source]

Compute cardan angles from rotation matrix

Computes the angles over which to rotate subsequently around the 0-axis, the 1-axis and the 2-axis to obtain the rotation corresponding to the given rotation matrix.

Parameters:
  • R ((3,3) float array_like) – Rotation matrix for post multiplication (see Notes)
  • angle_spec (DEG, RAD or float, nonzero.) – Divisor applied to the resulting angles before returning. The default divisor DEG makes the angles be returned in degrees. Use RAD to get angles in radians.
Returns:

(rx,ry,rz) (tuple of floats) – The three rotation angles around that when applied subsequently around the global 0, 1 and 2 axes, yield the same rotation as the input. The default angle_spec=DEG returns the angles in degrees.

Notes

The returned angles are but one of many ways to obtain a given rotation by three subsequent rotations around frame axes. Look on the web for ‘Euler angles’ to get comprehensive information. Different sets of angles can be obtained depending on the sequence of rotation axes used, and whether fixed axes (extrinsic) or rotated axes (intrinsic) are used in subsequent rotations. The here obtained ‘cardan’ angles are commonly denoted as a zy’x’’ system with intrinsic angles or xyz with extrinsic angles. It is the latter angles that are returned.

Because pyFormex stores rotation matrices as post-multiplication matrices (to be applied on row-vectors), the combined rotation around first the 0-axis, then the 1-axis and finally the 2-axis, is found as the matrix product Rx.Ry.Rz. (Traditionally, vectors were often written as column matrices, and rotation matrices were pre-multiplication matrices, so the subsequent rotation matrices would have to be multiplied in reverse order.)

Even if one chooses a single frame system for the subsequent rotations, the resulting angles are not unique. There are infinitely many sets of angles that will result in the same rotation matrix. The implementation here results in angles rx and rz in the range [-pi,pi], while the angle ry will be in [-pi/2,pi/2]. Even then, there remain infinite solutions in the case where the elements R[0,2] == R[2,0] equal +1 or -1 (ry = +pi/2 or -pi/2). The result will then be the solution with rx==0.

Examples

>>> print("%8.2f  "*3 % cardanAngles(rotationMatrix3(60,45,30)))
   60.00     45.00     30.00
>>> print("%8.2f  "*3 % cardanAngles(rotationMatrix3(0,90,77)))
    0.00     90.00     77.00
>>> print("%8.2f  "*3 % cardanAngles(rotationMatrix3(0,-90,30)))
    0.00    -90.00     30.00

But:

>>> print("%8.2f  "*3 % cardanAngles(rotationMatrix3(20,-90,30)))
    0.00    -90.00     50.00
arraytools.rotmat(x)[source]

Create a rotation matrix defined by 3 points in space.

Parameters:x (array_like (3,3)) – The rows contain the coordinates in 3D space of three non-colinear points x0, x1, x2.
Returns:rotmat (matrix(3,3)) – Rotation matrix which transforms the global axes into a new (orthonormal) coordinate system with the following properties:
  • the origin is at point x0,
  • the 0 axis is along the direction x1-x0
  • the 1 axis is in the plane (x0,x1,x2) with x2 lying at the positive side.

Notes

The rows of the rotation matrix represent the unit vectors of the resulting coordinate system. The coodinates in the rotated axes of any point are obtained by the reverse transformation, i.e. multiplying the point with the transpose of the rotation matrix.

See also

rotationMatrix()
rotation matrix specified by angle and axis
trfmat()
transformation matrices defined by 2 sets of 3 points
rotMatrix()
rotation matrix transforming global axis 0 into a given vector
rotMatrix2()
rotation matrix that transforms one vector into another

Examples

>>> rotmat([[0,0,0],[1,0,0],[0,1,0]])
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> rotmat(np.eye(3,3))
array([[-0.71,  0.71,  0.  ],
       [-0.41, -0.41,  0.82],
       [ 0.58,  0.58,  0.58]])
>>> s,c = sind(30),cosd(30)
>>> R = rotmat([[0,0,0],[c,s,0],[0,1,0]])
>>> print(R)
[[ 0.87  0.5   0.  ]
 [-0.5   0.87  0.  ]
 [ 0.   -0.    1.  ]]
>>> B = np.array([[2.,0.,0.],[3*s,3*c,3]])
>>> D = np.dot(B,R)    # Rotate some vectors with the matrix R
>>> print(D)
[[ 1.73  1.    0.  ]
 [-0.    3.    3.  ]]
arraytools.trfmat(x, y)[source]

Find the transformation matrices from 3 points x into y.

Constructs the rotation matrix and translation vector that will transform the points x thus that:

  • point x0 coincides with point y0,
  • line x0,x1 coincides with line y0,y1
  • plane x0,x1,x2 coincides with plane y0,y1,y2
Parameters:
  • x (float array_like (3,3)) – Original coordinates of three non-colinear points.
  • y (float array_like (3,3)) – Final coordinates of the three points.
Returns:

  • rot (float array (3,3)) – The rotation matrix for the transformation x to y.
  • trf – float array(3,) The translation vector for the transformation x to y, Obviously, this is equal to y0-x0.

The rotation is to be applied first and should be around the first point x0. The full transformation of a Coords object is thus obtained by (coords-x0)*rot+trl+x0 = coords*rot+(trl+x0-x0*rot).

Examples

>>> R,T = trfmat(np.eye(3,3), [[0,0,0],[1,0,0],[0,1,0]])
>>> print(R)
[[-0.71 -0.41  0.58]
 [ 0.71 -0.41  0.58]
 [ 0.    0.82  0.58]]
>>> print(T)
[ 0.71  0.41 -0.58]
arraytools.rotMatrix(u, w=[0.0, 0.0, 1.0])[source]

Create a rotation matrix that rotates global axis 0 to a given vector.

Parameters:
  • u ((3,) array_like) – Vector specifying the direction to which the global axis 0 should be rotated by the returned rotation matrix.
  • w ((3,) array_like) – Vector that is not parallel to u. This vector is used to uniquely define the resulting rotation. It will be equivalen to rotating first around w, until the target u lies in the plane of the rotated axis 0 and w, then rotated in that plane until the rotated axis 0 coincides with u. See also Note. If a parallel w is provided, it will be replaced with a non-parallel one.
Returns:

float array (3,3) – Rotation matrix that transforms a vector [1.,0.,0.] into u. The returned matrix should be used in postmultiplication to the coordinates.

See also

rotMatrix2()
rotation matrix that transforms one vector into another
rotationMatrix()
rotation matrix specified by an axis and angle
rotmat()
rotation matrix specified by three points in space
trfmat()
rotation and translation matrix that transform three points

Examples

>>> rotMatrix([1,0,0])
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> rotMatrix([0,1,0])
array([[ 0.,  1.,  0.],
       [-1.,  0.,  0.],
       [ 0., -0.,  1.]])
>>> rotMatrix([0,0,1])
array([[ 0.,  0.,  1.],
       [ 1.,  0.,  0.],
       [ 0.,  1.,  0.]])
>>> rotMatrix([0,1,1])
array([[ 0.  ,  0.71,  0.71],
       [-1.  ,  0.  ,  0.  ],
       [ 0.  , -0.71,  0.71]])
>>> rotMatrix([1,0,1])
array([[ 0.71,  0.  ,  0.71],
       [ 0.  ,  1.  ,  0.  ],
       [-0.71,  0.  ,  0.71]])
>>> rotMatrix([1,1,0])
array([[ 0.71,  0.71,  0.  ],
       [-0.71,  0.71,  0.  ],
       [ 0.  , -0.  ,  1.  ]])
>>> rotMatrix([1,1,1])
array([[ 0.58,  0.58,  0.58],
       [-0.71,  0.71,  0.  ],
       [-0.41, -0.41,  0.82]])
>>> np.dot([1,0,0], rotMatrix([1,1,1]))
array([ 0.58,  0.58,  0.58])
arraytools.rotMatrix2(vec1, vec2, upvec=None)[source]

Create a rotation matrix that rotates a vector vec1 to vec2.

Parameters:
  • vec1 ((3,) array_like) – Original vector.
  • vec2 ((3,) array_like) – Direction of vec1 after rotation.
  • upvec ((3,) array_like, optional) – If provided, the rotation matrix will be such that the plane of vec2 and the rotated upvec will be parallel to the original upvec. If not provided, the rotation matrix will perform a rotation around the normal to the plane ov the two vectors.
Returns:

float array (3,3) – Rotation matrix that transforms a vector [1.,0.,0.] into u. The returned matrix should be used in postmultiplication to the coordinates.

See also

rotMatrix()
rotation matrix transforming global axis 0 into a given vector
rotationMatrix()
rotation matrix specified by an axis and angle
rotmat()
rotation matrix specified by three points in space
trfmat()
rotation and translation matrix that transform three points

Examples

>>> rotMatrix2([1,0,0],[1,0,0])
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> rotMatrix2([1,0,0],[0,1,0])
array([[ 0.,  1.,  0.],
       [-1.,  0.,  0.],
       [ 0.,  0.,  1.]])
>>> rotMatrix2([1,0,0],[0,0,1])
array([[ 0.,  0.,  1.],
       [ 0.,  1.,  0.],
       [-1.,  0.,  0.]])
>>> rotMatrix2([1,0,0],[0,1,1])
array([[ 0.  ,  0.71,  0.71],
       [-0.71,  0.5 , -0.5 ],
       [-0.71, -0.5 ,  0.5 ]])
>>> rotMatrix2([1,0,0],[1,0,1])
array([[ 0.71,  0.  ,  0.71],
       [ 0.  ,  1.  ,  0.  ],
       [-0.71,  0.  ,  0.71]])
>>> rotMatrix2([1,0,0],[1,1,0])
array([[ 0.71,  0.71,  0.  ],
       [-0.71,  0.71,  0.  ],
       [ 0.  ,  0.  ,  1.  ]])
>>> rotMatrix2([1,0,0],[1,1,1])
array([[ 0.58,  0.58,  0.58],
       [-0.58,  0.79, -0.21],
       [-0.58, -0.21,  0.79]])
>>> rotMatrix2([1,0,0],[1,0,0],[0,0,1])
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> rotMatrix2([1,0,0],[0,1,0],[0,0,1])
array([[ 0.,  1.,  0.],
       [-1.,  0.,  0.],
       [ 0.,  0.,  1.]])
>>> rotMatrix2([1,0,0],[0,0,1],[0,0,1])
array([[ 0.,  0.,  1.],
       [ 1.,  0.,  0.],
       [ 0.,  1.,  0.]])
>>> rotMatrix2([1,0,0],[0,1,1],[0,0,1])
array([[ 0.  ,  0.71,  0.71],
       [-1.  ,  0.  ,  0.  ],
       [ 0.  , -0.71,  0.71]])
>>> rotMatrix2([1,0,0],[1,0,1],[0,0,1])
array([[ 0.71,  0.  ,  0.71],
       [ 0.  ,  1.  ,  0.  ],
       [-0.71,  0.  ,  0.71]])
>>> rotMatrix2([1,0,0],[1,1,0],[0,0,1])
array([[ 0.71,  0.71,  0.  ],
       [-0.71,  0.71,  0.  ],
       [ 0.  ,  0.  ,  1.  ]])
>>> rotMatrix2([1,0,0],[1,1,1],[0,0,1])
array([[ 0.58,  0.58,  0.58],
       [-0.71,  0.71,  0.  ],
       [-0.41, -0.41,  0.82]])
arraytools.abat(a, b)[source]

Compute the matrix product a * b * at.

Parameters:
  • a (array_like, 2-dim) – Array with shape (m,n).
  • b (array_like, 2-dim) – Array with square shape (n,n).
Returns:

array – Array with shape (m,m) holding the matrix product a * b * at.

See also

atba()

Examples

>>> abat([[1],[2]],[[3]])
array([[ 3,  6],
       [ 6, 12]])
>>> abat([[1,2]],[[0,1],[2,3]])
array([[18]])
arraytools.atba(a, b)[source]

Compute the matrix product at * b * a

Parameters:
  • a (array_like, 2-dim) – Array with shape (n,m).
  • b (array_like, 2-dim) – Array with square shape (n,n).
Returns:

array – Array with shape (m,m) holding the matrix product at * b * a.

Note

This multiplication typically occurs when rotating a symmetric tensor b to axes defined by the rotation matrix a.

See also

abat()

Examples

>>> atba([[1,2]],[[3]])
array([[ 3,  6],
       [ 6, 12]])
>>> atba([[1],[2]],[[0,1],[2,3]])
array([[18]])
arraytools.horner(a, u)[source]

Compute the value of a polynom using Horner’s rule.

Parameters:
  • a (float array_like (n+1,nd)) – nd-dimensional coefficients of a polynom of degree n in a scalar variable u. The coefficients are in order of increasing degree.
  • u (float array_like (nu)) – Parametric values where the polynom is to be evaluated.
Returns:

float array(nu,nd) – The nd-dimensional values of the polynom at the specified nu parameter values.

Examples

>>> print(horner([[1.,1.,1.],[1.,2.,3.]],[0.5,1.0]))
[[ 1.5 2.  2.5]
 [ 2.  3.  4. ]]
arraytools.solveMany(A, B)[source]

Solve many systems of linear equations.

Parameters:
  • A (float array_like (nsys,ndof,ndof)) – Coefficient matrices for nsys systems of ndof linear equations in ndof unknowns.
  • B (float array_like (nsys,ndof,nrhs)) – Right hand sides for the nsys systems of linear equations in ndof unkn owns. Each of the nsys systems is solved simultaneously for nrhs right hand sides.
Returns:

X (float array (nsys,ndof,nrhs)) – The set of values X(nsys,ndof,nrhs) that solve the systems of linear equations A @ X = B, where @ is the Python matrix multiplication operator. Thus for each set of submatrices A[i], B[i], X[i], the normal matrix multiplication holds: A[i] . X[i] = B[i].

Notes

For values of ndof >= 4, a general linear system soultion method is used. For values 1, 2 or 3 however, a direct solution method is used which is much faster.

Examples

This example creates random systems and random values for the variables, then computes the right hand sides, and solves the equations. Finally the found solution is compared with the original values of the unknowns.

>>> def solveRandom(nsys, ndof, nrhs):
...     A = np.random.rand(nsys, ndof, ndof)
...     X = np.random.rand(nsys, ndof, nrhs)
...     B = np.stack([np.dot(a,x) for a,x in zip(A,X)])
...     Y = solveMany(A,B)
...     return np.allclose(X,Y,atol=1.e-4)
>>> nsys, nrhs = 10, 5
>>> print([solveRandom(nsys,ndof,nrhs) for ndof in [1,2,3,4]])
[True, True, True, True]
arraytools.cubicEquation(a, b, c, d)[source]

Solve a cubiq equation using a direct method.

Given a polynomial equation of the third degree with real coefficients:

a*x**3 + b*x**2 + c*x + d = 0

Such an equation (with a non-zero) always has exactly three roots, with some possibly being complex, or identical. This function computes all three roots of the equation and returns full information about their nature, multiplicity and sorting order. It uses scaling of the variables to enhance the accuracy.

Parameters:
  • a (float) – Coefficient of the third degree term.
  • b (float) – Coefficient of the second degree term.
  • c (float) – Coefficient of the first degree term.
  • d (float) – Constant in the third degree polynom.
Returns:

  • r1 (float) – First real root of the cubiq equation

  • r2 (float) – Second real root of the cubiq equation or real part of the complex conjugate second and third root.

  • r3 (float) – Third real root of the cubiq equation or imaginary part of the complex conjugate second and third root.

  • kind (int) – A value specifying the nature and ordering of the roots:

    kind roots
    0 three real roots r1 < r2 < r3
    1 three real roots r1 < r2 = r3
    2 three real roots r1 = r2 < r3
    3 three real roots r1 = r2 = r3
    4 one real root r1 and two complex conjugate roots with real part r2 and imaginary part r3; the complex roots are thus: r2+i*r3 en r2-i*r3, where i=sqrt(-1).

Raises:

ValueError: – If the coefficient a==0 and the equation reduces to a second degree.

Examples

>>> cubicEquation(1.,-6.,11.,-6.)
(array([ 1.,  2.,  3.]), 0)
>>> cubicEquation(1.,-2.,1.,0.)
(array([-0.,  1.,  1.]), 1)
>>> cubicEquation(1.,-5.,8.,-4.)
(array([ 1.,  2.,  2.]), 1)
>>> cubicEquation(1.,-4.,5.,-2.)
(array([ 1.,  1.,  2.]), 2)
>>> cubicEquation(1.,-3.,3.,-1.)
(array([ 1.,  1.,  1.]), 3)
>>> cubicEquation(1.,-1.,1.,-1.)
(array([ 1.,  0.,  1.]), 4)
>>> cubicEquation(1.,-3.,4.,-2.)
(array([ 1.,  1.,  1.]), 4)
arraytools.renumberIndex(index)[source]

Renumber an index sequentially.

Given a one-dimensional integer array with only non-negative values, and nval being the number of different values in it, and you want to replace its elements with values in the range 0..nval, such that identical numbers are always replaced with the same number and the new values at their first occurrence form an increasing sequence 0..nval. This function will give you the old numbers corresponding with each position 0..nval.

Parameters:index (1-dim int array_like) – Array with non-negative integer values.
Returns:int array – A 1-dim int array with length equal to nval, where nval is the number of different values in index. The elements are the original values corresponding to the new values 0..nval.

See also

inverseUniqueIndex()
get the inverse mapping.

Examples

>>> renumberIndex([0,5,2,2,6,0])
array([0, 5, 2, 6])
>>> inverseUniqueIndex(renumberIndex([0,5,2,2,6,0]))[[0,5,2,2,6,0]]
array([0, 1, 2, 2, 3, 0])
arraytools.inverseUniqueIndex(index)[source]

Inverse an index.

Given a 1-D integer array with unique non-negative values, and max being the highest value in it, this function returns the position in the array of the values 0..max. Values not occurring in input index get a value -1 in the inverse index.

Parameters:index (1-dim int array_like) – Array with non-negative values, which all have to be unique. It’s highest value is max = index.max().
Returns:1-dim int array – Array with length max+1, with the position in index of each of the values 0..max, or -1 if the value does not occur in index.

Note

This is a low level function that does not check whether the input has indeed all unique values.

The inverse index translates the unique index numbers in a sequential index, so that inverseUniqueIndex(index)[index] == np.arange(1+index.max()).

Examples

>>> inverseUniqueIndex([0,5,2,6])
array([ 0, -1,  2, -1, -1,  1,  3])
>>> inverseUniqueIndex([0,5,2,6])[[0,5,2,6]]
array([0, 1, 2, 3])
arraytools.cumsum0(a)[source]

Cumulative sum of a list of numbers preprended with a 0.

Parameters:a (array_like, int) – List of integers to compute the cumulative sum for.
Returns:array, int – Array with len(a)+1 integers holding the cumulative sum of the integers from a with a 0 prepended.

Examples

>>> cumsum0([2,4,3])
array([0, 2, 6, 9])

A common use case is when concatenating some blocks of different length. If the list a holds the length of each block, the cumsum0(a) holds the start and end of each block in the concatenation.

>>> L = [ [0,1], [2,3,4,5], [6], [7,8,9] ]
>>> n = cumsum0([len(i) for i in L])
>>> print(n)
[ 0  2  6  7 10]
>>> C = np.concatenate(L)
>>> print(C)
[0 1 2 3 4 5 6 7 8 9]
>>> for i,j in zip(n[:-1],n[1:]):
...     print("%s:%s = %s" % (i,j,C[i:j]))
...
0:2 = [0 1]
2:6 = [2 3 4 5]
6:7 = [6]
7:10 = [7 8 9]
arraytools.multiplicity(a)[source]

Return the multiplicity of the numbers in an array.

Parameters:a (array_like, 1-dim) – The data array, will be flattened if it is not 1-dim.
Returns:
  • mult (1-dim int array) – The multiplicity of the unique values in a
  • uniq (1-dim array) – Array of same type as a, with the sorted list of unique values in a.

Examples

>>> multiplicity([0,1,4,3,1,4,3,4,3,3])
(array([1, 2, 4, 3]), array([0, 1, 3, 4]))
>>> multiplicity([[1.0, 0.0, 0.5],[0.2,0.5,1.0]])
(array([1, 1, 2, 2]), array([ 0. ,  0.2,  0.5,  1. ]))
arraytools.subsets(a)[source]

Split an array of integers into subsets.

The subsets of an integer array are sets of elements with the same value.

Parameters:a (int array_like, 1-dim) – Array with integer values to be split in subsets
Returns:
  • val (array of ints) – The unique values in a, sorted in increasing order.
  • ind (varray.Varray) – The Varray has the same number of rows as the number of values in ind. Each row contains the indices in a of the elements with the corresponding value in val.

Examples

>>> A = [0,1,4,3,1,4,3,4,3,3]
>>> val,ind = subsets(A)
>>> print(val)
[0 1 3 4]
>>> print(ind)
Varray (4,4)
  [0]
  [1 4]
  [3 6 8 9]
  [2 5 7]
<BLANKLINE>

The inverse of ind can be used to restore A from val.

>>> inv = ind.inverse().data
>>> print(inv)
[0 1 3 2 1 3 2 3 2 2]
>>> (val[inv] == A).all()
True
arraytools.sortSubsets(a, w=None)[source]

Sort subsets of an integer array a.

Subsets of an array are the sets of elements with equal values. By default the subsets are sorted according to decreasing number of elements in the set, or if a weight for each element is provided, according to decreasing sum of weights in the set.

Parameters:
  • a (1-dim int array_like) – Input array containing non-negative integer sets to be sorted.
  • w (1-dim int or float array_like, optional) – If provided, it should have the same length as a. Each element of a will be attributed the corresponding weight. Specifying no weigth is equivalent to giving all elements the same weight.
Returns:

int array – Array with same size as a, specifying for each element of a the index of its subset in the sorted list of subsets.

Examples

>>> sortSubsets([0,1,3,2,1,3,2,3,2,2])
array([3, 2, 1, 0, 2, 1, 0, 1, 0, 0])
>>> sortSubsets([0,1,4,3,1,4,3,4,3,3])
array([3, 2, 1, 0, 2, 1, 0, 1, 0, 0])
>>> sortSubsets([0,1,4,3,1,4,3,4,3,3],w=[9,8,7,6,5,4,3,2,1,0])
array([3, 1, 0, 2, 1, 0, 2, 0, 2, 2])
arraytools.collectOnLength(items, return_index=False)[source]

Separate items in a list according to their length.

The length of all items in the list are determined and the items are put in separate lists according to their length.

Parameters:
  • items (list) – A list of any items that can be accepted as parameter of the len() function.
  • return_index (bool) – If True, also return an index with the positions of the equal length items in the original iterable.
Returns:

  • col (dict) – A dict whose keys are the item lengths and values are lists of items with this length.
  • ind (dict, optional) – A dict with the same keys as col, and the values being a list of indices in the list where the corresponding item of col appeared.

Examples

>>> collectOnLength(['a','bc','defg','hi','j','kl'])
{1: ['a', 'j'], 2: ['bc', 'hi', 'kl'], 4: ['defg']}
>>> collectOnLength(['a','bc','defg','hi','j','kl'],return_index=True)[1]
{1: [0, 4], 2: [1, 3, 5], 4: [2]}
arraytools.complement(index, n=-1)[source]

Return the complement of an index in a range(0,n).

The complement is the list of numbers from the range(0,n) that are not included in the index.

Parameters:
  • index (1-dim int or bool array_like) – If integer, the array contains non-negative numbers in the range(0,n) and the return value will be the numbers in range(0,n) not included in index. If boolean, False value flag elements to be included (having a value True) in the output.
  • n (int) – Upper limit for the range of numbers. If index is of type integer and n is not specified or is negative, it will be set equal to the largest number in index plus 1. If index is of type boolean and n is larger than the length of index, index will be padded with False values until length n.
Returns:

1-dim array, type int or bool. – The output array has the same dtype as the input. If index is integer: it is an array with the numbers from range(0,n) that are not included in index. If index is boolean, it is the negated input, padded to or cut at length n.

Examples

>>> print(complement([0,5,2,6]))
[1 3 4]
>>> print(complement([0,5,2,6],10))
[1 3 4 7 8 9]
>>> print(complement([False,True,True,True],6))
[ True False False False  True  True]
arraytools.sortByColumns(a)[source]

Sort an array on all its columns, from left to right.

The rows of a 2-dimensional array are sorted, first on the first column, then on the second to resolve ties, etc..

Parameters:a (array_like, 2-dim) – The array to be sorted
Returns:int array, 1-dim – Index specifying the order in which the rows have to be taken to obtain an array sorted by columns.

Examples

>>> sortByColumns([[1,2],[2,3],[3,2],[1,3],[2,3]])
array([0, 3, 1, 4, 2])
arraytools.minroll(a)[source]

Roll a 1-D array to get the lowest values in front

If the lowest value occurs more than once, the one with the lowest next value is choosen, etcetera.

Parameters:a (array, 1-dim) – The array to roll
Returns:m (int) – The index of the element that should be put in front. This means that the np.roll(a,-m) gives the rolled array with the lowest elements in front.

Examples

>>> minroll([1,3,5,1,2,6])
3
>>> minroll([0,0,2,0,0,1])
3
>>> minroll([0,0,0,0,0,0])
0
arraytools.isroll(a, b)[source]

Check that two 1-dim arrays can be rolled into eachother

Parameters:
  • a (array, 1-dim) – The first array
  • b (array, 1-dim) – The second array, same length and dtype as a to be non-trivial.
Returns:

m (int) – The number of positions (non-negative) that b has to be rolled to be equal to a, or -2 if the two arrays have a different length, or -1 if their elements are not the same or not in the same order.

Examples

>>> isroll(np.array([1,2,3,4]), np.array([2,3,4,1]))
1
>>> isroll(np.array([1,2,3,4]), np.array([2,3,1,4]))
-1
>>> isroll(np.array([1,2,3,4]), np.array([3,2,1,4]))
-1
>>> isroll(np.array([1,2,3,4]), np.array([1,2,3]))
-2
arraytools.findEqualRows(a, permutations='none')[source]

Find equal rows in a 2-dim array.

Parameters:
  • a (array_like, 2-dim) – The array in which to find the equal rows.
  • permutations (str) –

    Defines which permutations of the row data are allowed while still considering the rows equal. Possible values are:

    • ’none’: no permutations are allowed: rows must match the same data at the same positions. This is the default;
    • ’roll’: rolling is allowed. Rows that can be transformed into each other by rolling are considered equal;
    • ’all’: any permutation of the same data will be considered an equal row.
Returns:

  • ind (1-dim int array) – A row index sorting the rows in such order that equal rows are grouped together.
  • ok (1-dim bool array) – An array flagging the rows in the order of index with True if it is the first row of a group of equal rows, or with False if the row is equal to the previous.

Notes

This function provides the functionality for detecting equal rows, but is seldomly used directly. There are wrapper functions providing more practical return values. See below.

See also

equalRows()
return the indices of the grouped equal rows
uniqueRows()
return the indices of the unique rows
uniqueRowsIndex()
like uniqueRows, but also returns index for all rows

Examples

>>> print(*findEqualRows([[1,2],[2,3],[3,2],[1,3],[2,3]]))
[0 3 1 4 2] [ True  True  True False  True]
>>> print(*findEqualRows([[1,2],[2,3],[3,2],[1,3],[2,3]],permutations='all'))
[0 3 1 2 4] [ True  True  True False False]
>>> print(*findEqualRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]]))
[0 3 2 1] [ True False  True  True]
>>> print(*findEqualRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]],permutations='all'))
[0 1 2 3] [ True False False False]
>>> print(*findEqualRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]],permutations='roll'))
[0 2 3 1] [ True False False  True]
arraytools.equalRows(a, permutations='none')[source]

Return equal rows in a 2-dim array.

Parameters: see findEqualRows()

Returns:V (varray.Varray) – A Varray where each row contains a list of the row numbers from a that are considered equal. The entries in each row are sorted and the rows are sorted according to their first element.

Notes

The return Varray holds a lot of information:

  • V.col(0) gives the indices of the unique rows.
  • complement(V.col(0),len(a)) gives the indices of duplicate rows.
  • V.col(0)[V.lengths==1] gives the indices of rows without duplicate.
  • Va.inverse().data gives an index into the unique rows for each of the rows of a.

See also

findEqualRows()
sorts and detects equal rows
uniqueRows()
return the indices of the unique rows
uniqueRowsIndex()
like uniqueRows, but also returns index for all rows

Examples

>>> equalRows([[1,2],[2,3],[3,2],[1,3],[2,3]])
Varray([[0], [1, 4], [2], [3]])
>>> equalRows([[1,2],[2,3],[3,2],[1,3],[2,3]],permutations='all')
Varray([[0], [1, 2, 4], [3]])
>>> equalRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]])
Varray([[0, 3], [1], [2]])
>>> equalRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]],permutations='all')
Varray([[0, 1, 2, 3]])
>>> equalRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]],permutations='roll')
Varray([[0, 2, 3], [1]])
arraytools.uniqueRows(a, permutations='none')[source]

Find the unique rows of a 2-D array.

Parameters: see findEqualRows()

Returns:uniq (1-dim int array) – Contains the indices of the unique rows in a.

See also

equalRows()
return the indices of the grouped equal rows
uniqueRowsIndex()
like uniqueRows, but also returns index for all rows

Examples

>>> uniqueRows([[1,2],[2,3],[3,2],[1,3],[2,3]])
array([0, 1, 2, 3])
>>> uniqueRows([[1,2],[2,3],[3,2],[1,3],[2,3]],permutations='all')
array([0, 1, 3])
>>> uniqueRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]])
array([0, 1, 2])
>>> uniqueRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]],permutations='all')
array([0])
>>> uniqueRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]],permutations='roll')
array([0, 1])
>>> uniqueRows([[1,2,3],[3,2,1],[2,3,1],[1,2,3]])
array([0, 1, 2])
arraytools.uniqueRowsIndex(a, permutations='none')[source]

Return the unique rows of a 2-D array and an index for all rows.

Parameters:
  • a (array_like, 2-dim) – Array from which to find the unique rows.
  • permutations (bool) – If True, rows which are permutations of the same data are considered equal. The default is to consider permutations as different.
  • roll (bool) – If True, rows which can be rolled into the same contents are considered equal.
Returns:

  • uniq (1-dim int array) – Contains the indices of the unique rows in a. The order of the elements in uniq is determined by the sorting procedure: in the current implementation this is sortByColumns(). If permutations==True, a is sorted along its last axis -1 before calling this sorting function. If roll=True, the rows of a are rolled to put the lowest values at the front.
  • ind (1-dim int array) – For each row of a, holds the index in uniq where the row with the same data is found.

See also

equalRows()
return the indices of the grouped equal rows
uniqueRows()
return the indices of the unique rows

Examples

>>> print(*uniqueRowsIndex([[1,2],[2,3],[3,2],[1,3],[2,3]]))
[0 1 2 3] [0 1 2 3 1]
>>> print(*uniqueRowsIndex([[1,2],[2,3],[3,2],[1,3],[2,3]],permutations='all'))
[0 1 3] [0 1 1 2 1]
>>> print(*uniqueRowsIndex([[1,2,3],[3,2,1],[2,3,1],[1,2,3]]))
[0 1 2] [0 1 2 0]
>>> print(*uniqueRowsIndex([[1,2,3],[3,2,1],[2,3,1],[1,2,3]],permutations='all'))
[0] [0 0 0 0]
>>> print(*uniqueRowsIndex([[1,2,3],[3,2,1],[2,3,1],[1,2,3]],permutations='roll'))
[0 1] [0 1 0 0]
>>> print(*uniqueRowsIndex([[1,2,3],[3,2,1],[2,3,1],[1,2,3]]))
[0 1 2] [0 1 2 0]
arraytools.argNearestValue(values, target)[source]

Return the index of the item nearest to target.

Find in a list of floats the position of the value nearest to the target value.

Parameters:
  • values (list) – List of float values.
  • target (float) – Target value to look up in list.
Returns:

int – The index in values of the float value that is closest to target.

See also

nearestValue()

Examples

>>> argNearestValue([0.1,0.5,0.9],0.7)
1
arraytools.nearestValue(values, target)[source]

Return the float nearest to target.

Find in a list of floats the value that is closest to the target value.

Parameters:
  • values (list) – List of float values.
  • target (float) – Target value to look up in list.
Returns:

float – The value from the list that is closest to target.

Examples

>>> nearestValue([0.1,0.5,0.9],0.7)
0.5
arraytools.inverseIndex(a, sort=True)[source]

Create the inverse of a 2D-index array.

A 2D-index array is a 2D integer array where only the nonnegative values. are relevant. Negative values are flagging a non-existent element. This allows for rows with different number of entries. While in most practical cases all (non-negative) values in a row are unique, this is not a requirement.

Parameters:
  • a (varray_like.) – The input index table. This can be anything that is acceptable as data for the Varray constructor.
  • sort (bool.) – If True, the values on each row of the returned index are sorted. The default (False) will leave the values in the order obtained by the algorithm, which depends on Python/numpy sorting.
Returns:

inv (numpy.ndarray) – The inverse index as an array. Each row i of the inverse index contains the numbers of the rows of the input in which a value i appeared, and padded with -1 values to make all rows equal length. With sort=True, the values on each row are guaranteed to be sorted.

See also

Varray.inverse()

Note

If the same value occurs multiple times on the same row of the input, the inverse index will also contain repeated row numbers for that value.

Examples

>>> A = np.array([[0,1],[0,2],[1,2],[0,3]])
>>> print(A)
[[0 1]
 [0 2]
 [1 2]
 [0 3]]
>>> inv = inverseIndex(A)
>>> print(inv)
[[ 0  1  3]
 [-1  0  2]
 [-1  1  2]
 [-1 -1  3]]

The inverse of the inverse returns the original:

>>> (inverseIndex(inv) == A).all()
True
arraytools.findFirst(target, values)[source]

Find first position of values in target.

Find the first position in the array target of all the elements in the array values.

Parameters:
  • target (1-dim int array) – Integer array with all non-negative values. If not 1-dim, it will be flattened.
  • values (1-dim int array) – Array with values to look up in target. If not 1-dim, it will be flattened.
Returns:

int array – Array with same size as values. For each element in values, the return array contains the position of that value in the flattened target, or -1 if that number does not occur in target. If an element from values occurs more than once in target, it is currently undefined which of those positions is returned.

Note

After m = findIndex(target,values) the equality target[m] == values holds for all the non-negative positions of m.

Examples

>>> A = np.array([1,3,4,5,7,3,8,9])
>>> B = np.array([0,7,1,3])
>>> ind = findFirst(A,B)
>>> print(ind)
[-1  4  0 1]
>>> (A[ind[ind>=0]] == B[ind>=0]).all()
True
arraytools.matchLine2(edges, edges1)[source]

Match Line elems in a given Connectivity.

Find the rows in edges that have the same nodes as the rows of edges1.

Parameters:
  • edges (int array_like) – An int array (nedges,2), e.g. a Line2 Connectivity.
  • edges1 (int array_like) – An int array (nedges1,2), e.g. a Line2 Connectivity.
Returns:

int array – An int array (nedges1,) specifying for each row of edges1 which row of edges contains the same two nodes (in any order). Rows that do not occur in edges get a value -1. If multiple rows are matching, the first one is returned.

arraytools.findAll(target, values)[source]

Find all locations of values in target.

Find the position in the array target of all occurrences of the elements in the array values.

Parameters:
  • target (1-dim int array) – Integer array with all non-negative values. If not 1-dim, it will be flattened.
  • values (1-dim int array) – Array with values to look up in target. If not 1-dim, it will be flattened.
Returns:

list of int arrays. – For each element in values, an array is returned with the indices in target of the elements with the same value.

See also

findFirst()

Examples

>>> gid = np.array([ 2, 1, 1, 6, 6, 1 ])
>>> values = np.array([ 1, 2, 6 ])
>>> print(findAll(gid,values))
[array([1, 2, 5]), array([0]), array([3, 4])]
arraytools.groupArgmin(val, gid)[source]

Compute the group minimum.

Computes the minimum value per group of a set of values tagged with a group number.

Parameters:
  • val (1-dim array) – Data values
  • gid (1-dim int array_like) – Array with same length as val, containing the group identifiers.
Returns:

  • ugid (1-dim int array) – (ngrp,) shaped array with unique group identifiers.
  • minpos (1-dim int array) – (ngrp,) shaped array giving the position in val of the minimum of all values with the corresponding group identifier in ugid. The minimum values corresponding to the groups in ugid can be obtained with val[minpos].

Examples

>>> val = np.array([ 0.0, 1.0, 2.0, 3.0, 4.0, -5.0 ])
>>> gid = np.array([ 2, 1, 1, 6, 6, 1 ])
>>> print(groupArgmin(val,gid))
(array([1, 2, 6]), array([5, 0, 3]))
arraytools.collectRowsByColumnValue(a, col)[source]

Collects rows of a 2D array by common value in a specified column.

Parameters:
  • a (2-dim array_like) – Any 2-dim array.
  • col (int) – Column number on which values to collect the rows.
Returns:

dict – A dict where the keys are the unique values of the specified column of the array a. The values are int arrays with the indices of the rows that have the key value in their col column.

Examples

>>> a = np.array([[0,0], [1,1], [1,0], [0,1], [4,0]])
>>> print(a)
[[0 0]
 [1 1]
 [1 0]
 [0 1]
 [4 0]]
>>> d = collectRowsByColumnValue(a,0)
>>> print(d)
{0: array([0, 3]), 1: array([1, 2]), 4: array([4])}
>>> d = collectRowsByColumnValue(a,1)
>>> print(d)
{0: array([0, 2, 4]), 1: array([1, 3])}
arraytools.vectorPairAreaNormals(vec1, vec2)[source]

Compute area of and normals on parallellograms formed by vec1 and vec2.

Parameters:
  • vec1 ((3,) or (n,3) shaped float array_like) – Array with 1 or n vectors in 3D space.
  • vec2 ((3,) or (n,3) shaped float array_like) – Array with 1 or n vectors in 3D space.
Returns:

  • area ((n,) shaped float array) – The area of the parallellograms formed by the vectors vec1 and vec2.
  • normal ((n,3) shaped float array) – The unit length vectors normal to each vector pair (vec1,vec2).

Note

This first computes the cross product of vec1 and vec2, which is a normal vector with length equal to the area. Then normalize() produces the required results.

Note that where two vectors are parallel, an area zero results and an axis with components NaN.

See also

vectorPairNormals()
only returns the normal vectors
vectorPairArea()
only returns the area

Examples

>>> a = np.array([[3.,4,0],[1,0,0],[1,-2,1]])
>>> b = np.array([[1.,3.,0],[1,0,1],[-2,4,-2]])
>>> l,v = vectorPairAreaNormals(a,b)
>>> print(l)
[ 5.  1.  0.]
>>> print(v)
[[  0.   0.   1.]
 [  0.  -1.   0.]
 [ nan  nan  nan]]
arraytools.vectorPairNormals(vec1, vec2)[source]

Create unit vectors normal to vec1 and vec2.

Parameters:
  • vec1 ((3,) or (n,3) shaped float array_like) – Array with 1 or n vectors in 3D space.
  • vec2 ((3,) or (n,3) shaped float array_like) – Array with 1 or n vectors in 3D space.
Returns:

normal ((n,3) shaped float array) – The unit length vectors normal to each vector pair (vec1,vec2).

See also

vectorPairAreaNormals()
returns the normals and the area between vectors
vectorPairArea()
only returns the area between vectors

Examples

>>> a = np.array([[3.,4,0],[1,0,0],[1,-2,1]])
>>> b = np.array([[1.,3.,0],[1,0,1],[-2,4,-2]])
>>> v = vectorPairNormals(a,b)
>>> print(v)
[[  0.   0.   1.]
 [  0.  -1.   0.]
 [ nan  nan  nan]]
arraytools.vectorPairArea(vec1, vec2)[source]

Compute area of the parallellogram formed by a vector pair vec1,vec2.

Parameters:
  • vec1 ((3,) or (n,3) shaped float array_like) – Array with 1 or n vectors in 3D space.
  • vec2 ((3,) or (n,3) shaped float array_like) – Array with 1 or n vectors in 3D space.
Returns:

area ((n,) shaped float array) – The area of the parallellograms formed by the vectors vec1 and vec2.

See also

vectorPairAreaNormals()
returns the normals and the area between vectors
vectorPairNormals()
only returns the normal vectors

Examples

>>> a = np.array([[3.,4,0],[1,0,0],[1,-2,1]])
>>> b = np.array([[1.,3.,0],[1,0,1],[-2,4,-2]])
>>> l = vectorPairArea(a,b)
>>> print(l)
[ 5.  1.  0.]
arraytools.vectorTripleProduct(vec1, vec2, vec3)[source]

Compute triple product vec1 . (vec2 x vec3).

Parameters:vec2, vec3) ((vec1,) – Three arrays with same shape holding 1 or n vectors in 3D space.
Returns:(n,) shaped float array – The triple product of each set of corresponding vectors from vec1, vec2, vec3.

Note

The triple product is the dot product of the first vector(s) and the normal(s) on the second and third vector(s). This is also twice the volume of the parallellepid formed by the 3 vectors.

If vec1 has a unit length, the result is also the area of the parallellogram (vec2,vec3) projected in the direction vec1.

This is functionally equivalent with dotpr(vec1, np.cross(vec2, vec3)) but is implemented in a more efficient way, using the determinant formula.

Examples

>>> vectorTripleProduct([[1.,0.,0.],[2.,0.,0.]],
...                     [[1.,1.,0.],[2.,2.,0.]],
...                     [[1.,1.,1.],[2.,2.,2.]])
array([ 1., 8.])
arraytools.vectorPairCosAngle(v1, v2)[source]

Return the cosinus of the angle between the vectors v1 and v2.

vec1 and vec2 are (n,3) shaped arrays holding collections of vectors. The result is an (n) shaped array with the cosinus of the angle between each pair of vectors (vec1,vec2).

arraytools.vectorPairAngle(v1, v2)[source]

Return the angle (in radians) between the vectors v1 and v2.

vec1 and vec2 are (n,3) shaped arrays holding collections of vectors. The result is an (n) shaped array with the angle between each pair of vectors (vec1,vec2).

>>> vectorPairAngle([1,0,0],[0,1,0]) / DEG
90.0
>>> vectorPairAngle([[1,0,0],[0,1,0]],[[1,1,0],[1,1,1]]) / DEG
array([ 45.  ,  54.74])
arraytools.det2(a)[source]

Compute the determinant of 2x2 matrices.

Parameters:a (int or float array_like (…,2,2)) – Array containing one or more (2,2) square matrices.
Returns:int or float number or array(…) – The determinant(s) of the matrices. The result has the same type as the input array.

Note

This method is faster than the generic numpy.linalg.det.

See also

det3()
determinant of (3,3) matrices
det4()
determinant of (4,4) matrices
numpy.linalg.det()
determinant of any size matrix

Examples

>>> det2([[1,2],[2,1]])
-3
>>> det2([[[1,2],[2,1]],[[4,2],[1,3]]])
array([-3, 10])
arraytools.det3(a)[source]

Compute the determinant of 3x3 matrices.

Parameters:a (int or float array_like (…,3,3)) – Array containing one or more (3,3) square matrices.
Returns:int or float number or array(…) – The determinant(s) of the matrices. The result has the same type as the input array.

Note

This method is faster than the generic numpy.linalg.det.

See also

det2()
determinant of (2,2) matrices
det4()
determinant of (4,4) matrices
numpy.linalg.det()
determinant of any size matrix

Examples

>>> det3([[1,2,3],[2,2,2],[3,2,1]])
0
>>> det3([[[1.,0.,0.],[1.,1.,0.],[1.,1.,1.]],
...       [[2.,0.,0.],[2.,2.,0.],[2.,2.,2.]]])
array([ 1., 8.])
arraytools.det4(a)[source]

Compute the determinant of 4x4 matrices.

Parameters:a (int or float array_like (…,4,4)) – Array containing one or more (4,4) square matrices.
Returns:int or float number or array(…) – The determinant(s) of the matrices. The result has the same type as the input array.

Note

This method is faster than the generic numpy.linalg.det.

See also

det2()
determinant of (2,2) matrices
det3()
determinant of (3,3) matrices
numpy.linalg.det()
determinant of any size matrix

Examples

>>> det4([[[1.,0.,0.,0.],[1.,1.,0.,0.],[1.,1.,1.,0.],[1.,1.,1.,1.]],
...       [[2.,0.,0.,0.],[2.,2.,0.,0.],[2.,2.,2.,0.],[2.,2.,2.,2.]]])
array([ 1., 16.])
arraytools.percentile(values, perc=[25.0, 50.0, 75.0], wts=None)[source]

Return percentiles of a set of values.

A percentiles is the value such that at least a given percent of the values is lower or equal than the value.

Parameters:
  • values (1-dim int or float array_like) – The set of values for which to compute the percentiles.
  • perc (1-dim int or float array_like) – One or multiple percentile values to compute. All values should be in the range [0,100]. By default, the quartiles are computed.
  • wts (1-dim array) – Array with same shape as values and all positive values. These are weights to be assigned to the values.
Returns:

1-dim float array – Array with the percentile value(s) that is/are greater or equal than perc percent of values. If the result lies between two items of values, it is obtained by interpolation.

Examples

>>> percentile(np.arange(100),[10,50,90])
array([  9.,  49.,  89.])
>>> percentile([1,1,1,1,1,2,2,2,3,5])
array([ 1.,  1.,  2.])
arraytools.histogram2(a, bins, range=None)[source]

Compute the histogram of a set of data.

This is similar to the numpy histogram function, but also returns the bin index for each individual entry in the data set.

Parameters:
  • a (array_like) – Input data. The histogram is computed over the flattened array.
  • bins (int or sequence of scalars.) – If bins is an int, it defines the number of equal-width bins in the given range (nbins). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths. The number of bins (nbins) is then equal to len(bins) - 1. A value v will be sorted in bin i if bins[i] <= v < bins[i+1], except for the last bin, which will also contain the values equal to the right bin edge.
  • range` ((float, float), optional.) – The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. This parameter is ignored if bins is a sequence.
Returns:

  • hist (int array) – The number of elements from a sorted in each of the bins.
  • ind (list of nbins int arrays) – Each array holds the indices the elements sorted in the corresponding bin.
  • bin_edges (float array) – The array contains the len(hist)+1 bin edges.

Example

>>> hist,ind,bins = histogram2([1,2,3,4,2,3,1],[1,2,3,4,5])
>>> print(hist)
[2 2 2 1]
>>> for i in ind: print(i)
[0 6]
[1 4]
[2 5]
[3]
>>> print(bins)
[1 2 3 4 5]
>>> hist,bins = np.histogram([1,2,3,4,2,3,1],5)
>>> print(hist)
[2 2 0 2 1]
>>> hist,ind,bins = histogram2([1,2,3,4,2,3,1],5)
>>> print(hist)
[2 2 0 2 1]
>>> for i in ind: print(i)
[0 6]
[1 4]
[]
[2 5]
[3]
arraytools.movingView(a, size)[source]

Create a moving view along the first axis of an array.

A moving view of an array is a view stacking a sequence of subarrays with fixed size along the 0 axis of the array, where each next subarray shifts one position down along the 0 axis.

Parameters:
  • a (array_like) – Array for which to create a moving view.
  • size (int) – Size of the moving view: this is the number of rows to include in the subarray.
Returns:

view of the array a – The view of the original array has an extra first axis with length 1 + a.shape[0] - size, a second axis with length size, and the remaining axes have the same length as those in a.

Note

While this function limits the moving view to the direction of the 0 axis, using swapaxes(0,axis) allows to create moving views over any axis.

See also

movingAverage()
compute moving average values along axis 0

Examples

>>> x=np.arange(10).reshape((5,2))
>>> print(x)
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
>>> print(movingView(x, 3))
[[[0 1]
  [2 3]
  [4 5]]
<BLANKLINE>
 [[2 3]
  [4 5]
  [6 7]]
<BLANKLINE>
 [[4 5]
  [6 7]
  [8 9]]]

Calculate rolling sum of first axis:

>>> print(movingView(x, 3).sum(axis=0))
[[ 6  9]
 [12 15]
 [18 21]]
arraytools.movingAverage(a, n, m0=None, m1=None)[source]

Compute the moving average along the first axis of an array.

Parameters:
  • a (array_like) – The array to be averaged.
  • n (int) – Sample length along axis 0 over which to compute the average.
  • m0 (int, optional) – If provided, the first data row of a will be prepended to a this number of times.
  • m1 (int, optional) – If provided, the last data row of a will be appended to a this number of times.
Returns:

float array – Array containing the moving average over data sets of length n along the first axis of a. The array has a shape like a except for its first axis, which may have a different length. If neither m0 nor m1 are set, the first axis will have a length of 1 + a.shape[0] - n. If both m0 and m1 are given, the first axis will have a length of 1 + a.shape[0] - n + m0 + m1. If either m0 or m1 are set and the other not, the missing value m0 or m1 will be computed thus that the return array has a first axis with length a.shape[0].

Examples

>>> x=np.arange(10).reshape((5,2))
>>> print(x)
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
>>> print(movingAverage(x,3))
[[ 2. 3.]
 [ 4. 5.]
 [ 6. 7.]]
>>> print(movingAverage(x,3,2))
[[ 0.   1.  ]
 [ 0.67 1.67]
 [ 2.   3.  ]
 [ 4.   5.  ]
 [ 6.   7.  ]]
arraytools.randomNoise(shape, min=0.0, max=1.0)[source]

Create an array with random float values between min and max

Parameters:
  • shape (tuple of ints) – Shape of the array to create.
  • min (float) – Minimum value of the random numbers.
  • max (float) – Maximum value of the random numbers.
Returns:

float array – An array of the requested shape filled with random numbers in the specified range.

Examples

>>> x = randomNoise((3,4))
>>> x.shape == (3,4)
True
>>> (x >= 0.0).all()
True
>>> (x <= 1.0).all()
True
arraytools.stuur(x, xval, yval, exp=2.5)[source]

Returns a (non)linear response on the input x.

xval and yval should be lists of 3 values: [xmin,x0,xmax], [ymin,y0,ymax]. Together with the exponent exp, they define the response curve as function of x. With an exponent > 0, the variation will be slow in the neighbourhood of (x0,y0). For values x < xmin or x > xmax, the limit value ymin or ymax is returned.

Examples

>>> x = unitDivisor(4)
>>> x
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
>>> np.array([stuur(xi, (0.,0.5,1.0), (0.,0.5,1.0) ) for xi in x])
array([ 0.  ,  0.41,  0.5 ,  0.59,  1.  ])
arraytools.unitDivisor(div)[source]

Divide a unit interval in equal parts.

This function is intended to be used by interpolation functions that accept an input as either an int or a list of floats.

Parameters:div (int, or list of floats in the range [0.0, 1.0]) – If it is an integer, it specifies the number of equal sized parts in which the interval [0.0, 1.0] is to be divided. If a list of floats, its values should be monotonuously increasing from 0.0 to 1.0. The values are returned unchanged.
Returns:
  • 1-dim float array – The float values that border the parts of the interval. If div is a an integer, returns the floating point values
  • dividing the unit interval in div equal parts. If div is a list,
  • just returns div as a 1D array.

Examples

>>> unitDivisor(4)
array([ 0.  , 0.25, 0.5 , 0.75, 1.  ])
>>> unitDivisor([0., 0.3, 0.7, 1.0])
array([ 0. ,  0.3,  0.7,  1. ])
arraytools.uniformParamValues(n, umin=0.0, umax=1.0)[source]

Create a set of uniformly distributed parameter values in a range.

Parameters:
  • n (int) – Number of intervals in which the range should be divided. The number of values returned is n+1.
  • umin (float) – Starting value of the interval.
  • umax (float) – Ending value of the interval.
Returns:

1-dim float array – The array contains n+1 equidistant values in the range [umin, umax]. For n > 0, both of the endpoints are included. For n=0, a single value at the center of the interval will be returned. For n<0, an empty array is returned.

Examples

>>> uniformParamValues(4).tolist()
[0.0, 0.25, 0.5, 0.75, 1.0]
>>> uniformParamValues(0).tolist()
[0.5]
>>> uniformParamValues(-1).tolist()
[]
>>> uniformParamValues(2,1.5,2.5).tolist()
[1.5, 2.0, 2.5]
arraytools.unitAttractor(x, e0=0.0, e1=0.0)[source]

Moves values in the range 0..1 closer to or away from the limits.

Parameters:
  • x (float array_like) – Values in the range 0.0 to 1.0, to be pulled to/pushed from ends.
  • e0 (float) – Attractor force to the start of the interval (0.0). A negative value will push the values away from this point.
  • e1 (float) – Attractor force to the end of the interval (1.0). A negative value will push the values away from this point.

Note

This function is usually called from the seed() function, passing an initially uniformly distributed set of points.

Examples

>>> np.set_printoptions(precision=4)
>>> print(unitAttractor([0.,0.25,0.5,0.75,1.0], 2.))
[ 0.     0.0039 0.0625 0.3164 1.    ]
>>> np.set_printoptions(precision=2)
>>> unitAttractor([0.,0.25,0.5,0.75,1.0])
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
arraytools.seed(n, e0=0.0, e1=0.0)[source]

Create a list of seed values.

A seed list is a list of float values in the range 0.0 to 1.0. It can be used to subdivide a line segment or to seed nodes along lines for meshing purposes.

This function divides the unit interval in n parts, resulting in n+1 seed values. While the intervals are by default of equal length, the e0 and e1 can be used to create unevenly spaced seed values.

Parameters:
  • n (int) – Positive integer: the number of elements (yielding n+1 parameter values).
  • e0 (float) – Attractor force at the start of the interval. A value larger than zero will attract the points closer to 0.0, while a negative value will repulse them.
  • e1 (float) – Attractor force at the end of the interval. A value larger than zero will attract the points closer to 1.0, while a negative value will repulse them.
Returns:

  • float arraya list of n+1 float values in the range 0.0 to 1.0.
  • The values are in ascending order, starting with 0.0 and ending with 1.0.

See also

seed1()
attractor at one end and equidistant points at the other.
smartSeed()
similar function accepting a variety of input.

Examples

>>> np.set_printoptions(precision=4)
>>> print(seed(5,2.,2.))
[ 0.     0.0639 0.3362 0.6638 0.9361 1.    ]
>>> np.set_printoptions(precision=2)
>>> for e0 in [ 0., 0.1, 0.2, 0.5, 1.0]: print(seed(5,e0))
[ 0.    0.2   0.4   0.6   0.8   1. ]
[ 0.    0.18  0.37  0.58  0.79  1.  ]
[ 0.    0.16  0.35  0.56  0.77  1.  ]
[ 0.    0.1   0.27  0.49  0.73  1.  ]
[ 0.    0.04  0.16  0.36  0.64  1.  ]
arraytools.seed1(n, nuni=0, e0=0.0)[source]

Create a list of seed values.

A seed list is a list of float values in the range 0.0 to 1.0. It can be used to subdivide a line segment or to seed nodes along lines for meshing purposes.

This function divides the unit interval in n parts, resulting in n+1 seed values. While the intervals are by default of equal length, the nuni and e0 can be used to create unevenly spaced seed values.

Parameters:
  • n (int) – The number of intervals in which to divide the range. This will yield n+1 parameter values.
  • nuni (0..n-1) – The number of intervals at the end of the range that will have equal length. If n < 2, this function is equivalent with seed(n,e0,0.0).
  • e0 (float) – Attractor for the start of the range. A value larger than zero will attract the points closer to the startpoint, while a negative value will repulse them.
Returns:

float array – A list of n+1 float values in the range 0.0 to 1.0. The values are in ascending order, starting with 0.0 and ending with 1.0.

See also

seed: an analogue function with attractors at both ends of the range.

Examples

>>> np.set_printoptions(precision=4)
>>> S = seed1(5,0,1.)
>>> print(S)
[ 0.   0.04 0.16 0.36 0.64 1.  ]
>>> print(S[1:]-S[:-1])
[ 0.04 0.12 0.2  0.28 0.36]
>>> S = seed1(5,2,1.)
>>> print(S)
[ 0.     0.0435 0.1739 0.3913 0.6957 1.    ]
>>> print(S[1:]-S[:-1])
[ 0.0435 0.1304 0.2174 0.3043 0.3043]
>>> np.set_printoptions(precision=2)
arraytools.smartSeed(n)[source]

Create a list of seed values.

Like the seed() function, this function creates a list of float values in the range 0.0 to 1.0. It accepts however a variety of inputs, making it the prefered choice when it is not known in advance how the user wants to control the seeds: automatically created or self specified.

Parameters:n (int, tuple or float seed) –

Action depends on the argument:

  • if an int, returns seed(n),
  • if a tuple (n,), (n,e0) or (n,e0,e1): returns seed(*n),
  • if a float array-like, it is normally a sorted list of float values in the range 0.0 to 1.0: the values are returned unchanged in an array.
Returns:float array – The values created depending on the input argument.

Examples

>>> np.set_printoptions(precision=4)
>>> print(smartSeed(5))
[ 0.  0.2 0.4 0.6 0.8 1. ]
>>> print(smartSeed((5,2.,1.)))
[ 0.     0.01   0.1092 0.3701 0.7504 1.    ]
>>> print(smartSeed([0.0,0.2,0.3,0.4,0.8,1.0]))
[ 0.  0.2  0.3  0.4  0.8  1. ]
>>> np.set_printoptions(precision=2)
arraytools.gridpoints(seed0, seed1=None, seed2=None)[source]

Create weights for 1D, 2D or 3D element coordinates.

Parameters:
  • seed0 (int or list of floats) – Subdivisions along the first parametric direction
  • seed1 (int or list of floats) – Subdivisions along the second parametric direction
  • seed2 (int or list of floats) – Subdivisions along the third parametric direction
  • these parameters are integer values the divisions will be equally (If) –
  • between 0 and 1. (spaced) –

Examples

>>> gridpoints(4)
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
>>> gridpoints(4,2)
array([[ 1.  ,  0.  ,  0.  ,  0.  ],
       [ 0.75,  0.25,  0.  ,  0.  ],
       [ 0.5 ,  0.5 ,  0.  ,  0.  ],
       [ 0.25,  0.75,  0.  ,  0.  ],
       [ 0.  ,  1.  ,  0.  ,  0.  ],
       [ 0.5 ,  0.  ,  0.  ,  0.5 ],
       [ 0.38,  0.12,  0.12,  0.38],
       [ 0.25,  0.25,  0.25,  0.25],
       [ 0.12,  0.38,  0.38,  0.12],
       [ 0.  ,  0.5 ,  0.5 ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  1.  ],
       [ 0.  ,  0.  ,  0.25,  0.75],
       [ 0.  ,  0.  ,  0.5 ,  0.5 ],
       [ 0.  ,  0.  ,  0.75,  0.25],
       [ 0.  ,  0.  ,  1.  ,  0.  ]])
arraytools.nodalSum(val, elems, nnod=-1)[source]

Compute the nodal sum of values defined on element nodes.

Parameters:
  • val (float array (nelems,nplex,nval)) – Array with nval values at nplex nodes of nelems elements.
  • elems (int array (nelems,nplex)) – The node indices of the elements.
  • nnod (int, optional) – If provided, the length of the output arrays will be set to this value. It should be higher than the highest node number appering in elems. The default will set it automatically to elems.max() + 1.
Returns:

  • sum (float array (nnod, nval)) – The sum of all the values at the same node.
  • cnt (int array (nnod)) – The number of values summed at each node.

See also

nodalAvg()
compute the nodal average of values defined on element nodes
arraytools.nodalAvg(val, elems, nnod=-1)[source]

Compute the nodal average of values defined on element nodes.

Parameters:
  • val (float array (nelems,nplex,nval)) – Array with nval values at nplex nodes of nelems elements.
  • elems (int array (nelems,nplex)) – The node indices of the elements.
  • nnod (int, optional) – If provided, the length of the output arrays will be set to this value. It should be higher than the highest node number appering in elems. The default will set it automatically to elems.max() + 1.
Returns:

avg (float array (nnod, nval)) – The average of all the values at the same node.

See also

nodalSum()
compute the nodal sum of values defined on element nodes
arraytools.fmtData1d(data, npl=8, sep=', ', linesep='\n', fmt=<class 'str'>)[source]

Format data in lines with maximum npl items.

Formats a list or array of data items in groups containing a maximum number of items. The data items are converted to strings using the fmt function, concatenated in groups of npl items using sep as a separator between them. Finally, the groups are concatenated with a linesep separator.

Parameters:
  • data (list or array.) – List or array with data. If an array, if will be flattened.
  • npl (int) – Maximum number of items per group. Items will be concatenated groups of this number of items. The last group may contain less items.
  • sep (str) – Separator to add between individual items in a group.
  • linesep (str) – Separator to add between groups. The default (newline) will put each group of npl items on a separate line.
  • fmt (callable) – Used to convert a single item to a string. Default is the Python built-in string converter.
Returns:

str – Multiline string with the formatted data.

Examples

>>> print(fmtData1d(np.arange(10)))
0, 1, 2, 3, 4, 5, 6, 7
8, 9
>>> print(fmtData1d([1.25, 3, 'no', 2.50, 4, 'yes'],npl=3))
1.25, 3, no
2.5, 4, yes
>>> myformat = lambda x: "%10s" % str(x)
>>> print(fmtData1d([1.25, 3, 'no', 2.50, 4, 'yes'],npl=3,fmt=myformat))
      1.25,          3,         no
       2.5,          4,        yes