Skip to content

Conversation

WarrenWeckesser
Copy link
Member

Example:

In [8]: a = array([[0,1,2],[3,4,0]])

In [9]: a
Out[9]: 
array([[0, 1, 2],
       [3, 4, 0]])

In [10]: norm(a, axis=0)
Out[10]: array([ 3.        ,  4.12310563,  2.        ])

In [11]: norm(a, axis=1)
Out[11]: array([ 2.23606798,  5.        ])

In [12]: norm(a, ord=np.Inf, axis=1)
Out[12]: array([2, 4])

axis can be a 2-tuple:

In [52]: a = np.arange(8).reshape(2,2,2)

In [53]: a
Out[53]: 
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

In [54]: norm(a, axis=(1,2))
Out[54]: array([  3.74165739,  11.22497216])

This PR also fixes a bug that occurs when computing the norm of an integer array with a negative ord. Before this change, norm([1,3], ord=-1) returned the incorrect value 1.0. Now,

In [13]: norm([1,3], ord=-1)
Out[13]: 0.75

Also fixed a bug that occurred with integer arrays and negative ord.  For example,
norm([1, 3], -1) returned 1.0, but the correct value is 0.75.
@njsmith
Copy link
Member

njsmith commented Jun 3, 2013

The vector/matrix dual-nature of this function mean that it has a pretty weird and overloaded calling convention. This is too bad, but, given that it is what it is -- shouldn't this API also support the ability to compute matrix norms over some subset of the axes?

@WarrenWeckesser
Copy link
Member Author

I agree about the "weirdness"--initially I created a new function, norm1d(a, ord, axis), that was solely for computing vector norms. But I figured the first question I'd get was "Why don't you just add an axis argument to the existing function?".

I think of the axis argument as saying "I want the vector norms of a collection of vectors". I guess it could be generalized so that if axis is a 2-tuple, it means "I want the matrix norms of a collection of matrices." So if a has shape (2,3,4), then norm(a, axis=(1,2)) would return an array with shape (2,). That sounds fine to me. Is that what you are suggesting?

@njsmith
Copy link
Member

njsmith commented Jun 3, 2013

Yes.
On 3 Jun 2013 17:51, "Warren Weckesser" notifications@github.com wrote:

I agrees about the "weirdness"--initially I created a new function, norm1d(a,
ord, axis), that was solely for computing vector norms. But I figured the
first question I'd get was "Why don't you just add an axis argument to
the existing function?".

I think of the axis argument as saying "I want the vector norms of a
collection of vectors". I guess it could be generalized so that if axisis a 2-tuple, it means "I want the matrix norm of a collection of
matrices." So if a has shape (2,3,4), then norm(a, axis=(1,2)) would
return an array with shape (2,). That sounds fine to me. Is that what you
are suggesting?


Reply to this email directly or view it on GitHubhttps://github.com//pull/3387#issuecomment-18854585
.

…, in which case matrix norms of the collection of 2-D matrices are computed.
@WarrenWeckesser
Copy link
Member Author

The PR has been updated to allow axis to be a 2-tuple, as described above.

>>> m = np.arange(8).reshape(2,2,2)
>>> norm(m, axis=(1,2))
array([ 3.74165739, 11.22497216])
>>> norm(m[0]), norm(m[1])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's better style (esp. pedagogical style) to replace m[0] with m[0, :, :] or m[0, ...].

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@WarrenWeckesser
Copy link
Member Author

The latest commit makes the changes suggest by @njsmith

@njsmith
Copy link
Member

njsmith commented Jun 5, 2013

+1 from me pending travis results.

njsmith added a commit that referenced this pull request Jun 5, 2013
ENH: linalg: Add an `axis` argument to linalg.norm
@njsmith njsmith merged commit d307ff6 into numpy:master Jun 5, 2013
eric-wieser pushed a commit that referenced this pull request Jan 18, 2018
hanjohn pushed a commit to hanjohn/numpy that referenced this pull request Feb 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants