I have vector a
.
I want to calculate np.inner(a, a)
But I wonder whether there is prettier way to calc it.
[The disadvantage of this way, that if I want to calculate it for a-b
or a bit more complex expression, I have to do that with one more line. c = a - b
and np.inner(c, c)
instead of somewhat(a - b)
]
Honestly there's probably not going to be anything faster than
np.inner
ornp.dot
. If you find intermediate variables annoying, you could always create a lambda function:np.inner
andnp.dot
leverage BLAS routines, and will almost certainly be faster than standard elementwise multiplication followed by summation.np.linalg.norm(..., ord=2)
usesnp.dot
internally, and gives very similar performance to usingnp.inner
directly.