fast_1d_tensor_mult

odl.util.numerics.fast_1d_tensor_mult(ndarr, onedim_arrs, axes=None, out=None)[source]

Fast multiplication of an n-dim array with an outer product.

This method implements the multiplication of an n-dimensional array with an outer product of one-dimensional arrays, e.g.:

a = np.ones((10, 10, 10))
x = np.random.rand(10)
a *= x[:, None, None] * x[None, :, None] * x[None, None, :]

Basically, there are two ways to do such an operation:

  1. First calculate the factor on the right-hand side and do one “big” multiplication; or

  2. Multiply by one factor at a time.

The procedure of building up the large factor in the first method is relatively cheap if the number of 1d arrays is smaller than the number of dimensions. For exactly n vectors, the second method is faster, although it loops of the array a n times.

This implementation combines the two ideas into a hybrid scheme:

  • If there are less 1d arrays than dimensions, choose 1.

  • Otherwise, calculate the factor array for n-1 arrays and multiply it to the large array. Finally, multiply with the last 1d array.

The advantage of this approach is that it is memory-friendly and loops over the big array only twice.

Parameters
ndarrarray-like

Array to multiply to

onedim_arrssequence of array-like’s

One-dimensional arrays to be multiplied with ndarr. The sequence may not be longer than ndarr.ndim.

axessequence of ints, optional

Take the 1d transform along these axes. None corresponds to the last len(onedim_arrs) axes, in ascending order.

outnumpy.ndarray, optional

Array in which the result is stored

Returns
outnumpy.ndarray

Result of the modification. If out was given, the returned object is a reference to it.