numpy / numpy/numpy

Implementation of matrix norm for order greater than 2

Open
#10,705 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

01 - Enhancement
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

When using numpy.linalg.norm with ord different from 2, for example 3, then I expected the norm to be computed according to the documentation, i.e. as sum(abs(x)**ord)**(1./ord)

But instead I got:

ValueError                                Traceback (most recent call last)
<ipython-input-784-3344f4d8d663> in <module>()
----> 1 nnorm((distpdfs_diverg['BC']/n_samples) - np.eye(n_bundles), 3)

/usr/lib/python3.5/site-packages/numpy/linalg/linalg.py in norm(x, ord, axis, keepdims)
   2247             ret = _multi_svd_norm(x, row_axis, col_axis, sum)
   2248         else:
-> 2249             raise ValueError("Invalid norm order for matrices.")
   2250         if keepdims:
   2251             ret_shape = list(x.shape)

ValueError: Invalid norm order for matrices.

Looking at the source code (numpy 1.13, numpy 1.14, and current) it seems it is simply the norm is simply not implemented for ord greater than 2 contrarily to what the doc says. Would that be something difficult to do?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The relevant implementation is numpy/linalg/linalg.py, identified in the traceback and linked source; start by reading norm's matrix-order handling and the surrounding documentation. Add coverage for matrix ord values above 2 and confirm the documented result against the reported formula.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.