flopscope.

flopscope.numpy.inner

fnp.inner(a, b)[flopscope source]

Inner product of two arrays.

Adapted from NumPy docs np.inner

Areacore
Typecustom
NumPy Refnp.inner
Cost
nn
Flopscope Context

Inner product; cost = N (FMA=1).

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

Parameters

a, b:array_like

If a and b are nonscalar, their last dimensions must match.

Returns

out:ndarray

If a and b are both scalars or both 1-D arrays then a scalar is returned; otherwise an array is returned. out.shape = (*a.shape[:-1], *b.shape[:-1])

Raises

:ValueError

If both a and b are nonscalar and their last dimensions have different sizes.

See also

Notes

For vectors (1-D arrays) it computes the ordinary inner-product:

flops.inner(a, b) = sum(a[:]*b[:])

More generally, if ndim(a) = r > 0 and ndim(b) = s > 0:

flops.inner(a, b) = flops.tensordot(a, b, axes=(-1,-1))

or explicitly:

flops.inner(a, b)[i0,...,ir-2,j0,...,js-2]
     = sum(a[i0,...,ir-2,:]*b[j0,...,js-2,:])

In addition a or b may be scalars, in which case:

flops.inner(a,b) = a*b

Examples

Ordinary inner product for vectors:

>>> import flopscope.numpy as fnp
>>> a = flops.array([1,2,3])
>>> b = flops.array([0,1,0])
>>> flops.inner(a, b)
2

Some multidimensional examples:

>>> a = flops.arange(24).reshape((2,3,4))
>>> b = flops.arange(4)
>>> c = flops.inner(a, b)
>>> c.shape
(2, 3)
>>> c
array([[ 14,  38,  62],
       [ 86, 110, 134]])
>>> a = flops.arange(2).reshape((1,1,2))
>>> b = flops.arange(6).reshape((3,2))
>>> c = flops.inner(a, b)
>>> c.shape
(1, 1, 3)
>>> c
array([[[1, 3, 5]]])

An example where b is a scalar:

>>> flops.inner(flops.eye(2), 7)
array([[7., 0.],
       [0., 7.]])