nums.numpy.api.algebra module

nums.numpy.api.algebra.dot(a, b, out=None)[source]

Dot product of two arrays.

This docstring was copied from numpy.dot.

Some inconsistencies with the NumS version may exist.

  • If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).

  • If both a and b are 2-D arrays, it is matrix multiplication, but using matmul() or a @ b is preferred.

  • If either a or b is 0-D (scalar), it is equivalent to multiply() and using numpy.multiply(a, b) or a * b is preferred.

  • If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.

  • If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:

    dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
    
Parameters
  • a (BlockArray) – First argument.

  • b (BlockArray) – Second argument.

  • out (BlockArray, optional) – Output argument. This must have the exact kind that would be returned if it was not used. In particular, it must have the right type, must be C-contiguous, and its dtype must be the dtype that would be returned for dot(a,b). This is a performance feature. Therefore, if these conditions are not met, an exception is raised, instead of attempting to be flexible.

Returns

output – Returns the dot product of a and b. If a and b are both scalars or both 1-D arrays then a scalar is returned; otherwise an array is returned. If out is given, then it is returned.

Return type

BlockArray

Raises

ValueError – If the last dimension of a is not the same size as the second-to-last dimension of b.

See also

tensordot

Sum products over arbitrary axes.

matmul

‘@’ operator as method with out parameter.

Examples

The doctests shown below are copied from NumPy. They won’t show the correct result until you operate get().

For 2-D arrays it is the matrix product:

>>> a = nps.array([[1, 0], [0, 1]])  
>>> b = nps.array([[4, 1], [2, 2]])  
>>> nps.dot(a, b).get()  
array([[4, 1],
       [2, 2]])
nums.numpy.api.algebra.inner(a, b)[source]

Inner product of two arrays.

This docstring was copied from numpy.inner.

Some inconsistencies with the NumS version may exist.

Ordinary inner product of vectors for 1-D arrays (without complex conjugation), in higher dimensions a sum product over the last axes.

Parameters
  • a (BlockArray) – If a and b are nonscalar, their last dimensions must match.

  • b (BlockArray) – If a and b are nonscalar, their last dimensions must match.

Returns

outout.shape = a.shape[:-1] + b.shape[:-1]

Return type

BlockArray

Raises

ValueError – If the last dimension of a and b has different size.

See also

tensordot

Sum products over arbitrary axes.

dot

Generalised matrix product, using second last dimension of b.

Notes

Only single-axis inputs supported.

Examples

The doctests shown below are copied from NumPy. They won’t show the correct result until you operate get().

Ordinary inner product for vectors:

>>> a = nps.array([1,2,3])  
>>> b = nps.array([0,1,0])  
>>> nps.inner(a, b).get() 
array(2)
nums.numpy.api.algebra.matmul(x1, x2)[source]

Matrix product of two arrays.

This docstring was copied from numpy.matmul.

Some inconsistencies with the NumS version may exist.

Parameters
  • x1 (BlockArray) – Input arrays, scalars not allowed.

  • x2 (BlockArray) – Input arrays, scalars not allowed.

Returns

y – The matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors.

Return type

BlockArray

Raises

ValueError – If the last dimension of a is not the same size as the second-to-last dimension of b. If a scalar value is passed in.

See also

tensordot

Sum products over arbitrary axes.

dot

alternative matrix product with different broadcasting rules.

nums.numpy.api.algebra.outer(a, b)[source]

Compute the outer product of two vectors.

This docstring was copied from numpy.outer.

Some inconsistencies with the NumS version may exist.

Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product [1]_ is:

[[a0*b0  a0*b1 ... a0*bN ]
 [a1*b0    .
 [ ...          .
 [aM*b0            aM*bN ]]
Parameters
  • a ((M,) BlockArray) – First input vector. Input is flattened if not already 1-dimensional.

  • b ((N,) BlockArray) – Second input vector. Input is flattened if not already 1-dimensional.

Returns

outout[i, j] = a[i] * b[j]

Return type

(M, N) BlockArray

See also

inner, outer, tensordot

Notes

Only single-axis inputs supported.

nums.numpy.api.algebra.tensordot(x1, x2, axes=2)[source]

Compute tensor dot product along specified axes.

This docstring was copied from numpy.tensordot.

Some inconsistencies with the NumS version may exist.

Given two tensors, a and b, and an array_like object containing two array_like objects, (a_axes, b_axes), sum the products of a’s and b’s elements (components) over the axes specified by a_axes and b_axes. The third argument can be a single non-negative integer_like scalar, N; if it is such, then the last N dimensions of a and the first N dimensions of b are summed over.

Parameters
  • a (BlockArray) – Tensors to “dot”.

  • b (BlockArray) – Tensors to “dot”.

  • axes (int or (2,) array_like) –

    • integer_like If an int N, sum over the last N axes of a and the first N axes of b in order. The sizes of the corresponding axes must match.

    • (2,) array_like Or, a list of axes to be summed over, first sequence applying to a, second to b. Both elements array_like must be of the same length.

Returns

output – The tensor dot product of the input.

Return type

BlockArray

See also

dot

Notes

Three common use cases are:
  • axes = 0 : tensor product \(a\otimes b\)

  • axes = 1 : tensor dot product \(a\cdot b\)

  • axes = 2 : (default) tensor double contraction \(a:b\)

When axes is integer_like, the sequence for evaluation will be: first the -Nth axis in a and 0th axis in b, and the -1th axis in a and Nth axis in b last.

When there is more than one axis to sum over - and they are not the last (first) axes of a (b) - the argument axes should consist of two sequences of the same length, with the first axis to sum over given first in both sequences, the second axis second, and so forth.

The shape of the result consists of the non-contracted axes of the first tensor, followed by the non-contracted axes of the second.

Non-integer axes is currently not supported.

nums.numpy.api.algebra.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)[source]

Return the sum along diagonals of the array.

This docstring was copied from numpy.trace.

Some inconsistencies with the NumS version may exist.

If a is 2-D, the sum along its diagonal with the given offset is returned, i.e., the sum of elements a[i,i+offset] for all i.

If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-arrays whose traces are returned. The shape of the resulting array is the same as that of a with axis1 and axis2 removed.

Parameters
  • a (BlockArray) – Input array, from which the diagonals are taken.

  • offset (int, optional) – Offset of the diagonal from the main diagonal. Can be both positive and negative. Defaults to 0.

  • axis1 (int, optional) – Axes to be used as the first and second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults are the first two axes of a.

  • axis2 (int, optional) – Axes to be used as the first and second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults are the first two axes of a.

  • dtype (dtype, optional) – Determines the data-type of the returned array and of the accumulator where the elements are summed. If dtype has the value None and a is of integer type of precision less than the default integer precision, then the default integer precision is used. Otherwise, the precision is the same as that of a.

  • out (BlockArray, optional) – Array into which the output is placed. Its type is preserved and it must be of the right shape to hold the output.

Returns

sum_along_diagonals – If a is 2-D, the sum along the diagonal is returned. If a has larger dimensions, then an array of sums along diagonals is returned.

Return type

BlockArray

See also

diag, diagonal

Notes

offset != 0 is currently not supported.

out is currently not supported.

axis1 != 0 or axis2 != 1 is currently not supported.

Examples

The doctests shown below are copied from NumPy. They won’t show the correct result until you operate get().

>>> nps.trace(nps.eye(3)).get()  
array(3.)
>>> a = nps.arange(8).reshape((2,2,2))