2

I am using python3 with np.linalg.norm to calculate the norms for rows in a matrix (norm(axis=1)), Is there a straightforward way, using only np to make it run using multithreading or multicoring?

1

1 Answer 1

1

We can use numexpr module that supports multi-core processing, like so -

import numexpr as ne

def linalg_norm(a):
    sq_norm = ne.evaluate('sum(a**2,1)')
    return ne.evaluate('sqrt(sq_norm)')

To perform norm-reduction along any other axis, replace 1 with that axis number in the evaluate expression - 'sum(a**2,1)'.

Sample run -

In [34]: np.random.seed(0)
    ...: a = np.random.rand(4,5)

In [35]: np.linalg.norm(a,axis=1)
Out[35]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])

In [36]: linalg_norm(a)
Out[36]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])

Related post on how to control multi-core functionality.


For completeness, few alternatives could be proposed.

An efficient solution would be with np.einsum -

In [39]: np.sqrt(np.einsum('ij,ij->i',a, a))
Out[39]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])

With np.matmul/@ operator on Python 3.x -

In [6]: np.sqrt(np.matmul(a[:,None],a[:,:,None])[:,0,0])
Out[6]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])

In [7]: np.sqrt((a[:,None] @ a[:,:,None])[:,0,0])
Out[7]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])
Sign up to request clarification or add additional context in comments.

2 Comments

This is a solid idea, but I am aiming for something that is pure numpy, which is multithreaded in a lot of places.
@thebeancounter If you are looking for a numpy built-in, there isn't any AFAIK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.