[Numpy-discussion] norm in scipy.linalg but not numpy?

Nils Wagner nwagner at iam.uni-stuttgart.de
Wed May 23 12:00:08 EDT 2007


dmitrey wrote:
> hi all,
> I was very surprised that norm() is present in scipy.linalg but absent 
> in numpy.
> Don't you think it's better to add the one to numpy?
> As for me, I use the func very intensively, and I don't want to write my 
> own (slow) func in Python or use
>
> sqrt(dot(v,v))
> or
> scipy.linalg.norm(v) (i.e. make dependence on scipy because of single 
> function linalg.norm())
> (as it is proposed in http://www.scipy.org/NumPy_for_Matlab_Users)
>
> and what about second argument to norm, as MATLAB provides?
> norm(x) = norm(x,2) by default, norm(x, inf) = max(abs(x)), norm(x, 
> -inf) = min(abs(x)), norm(x,1) = sum(abs(x)), norm(x,p) = 
> (x1^p+...)^(1/p) etc...
> Of course, I could write those 10-15 lines of code in Python, but I 
> think C or Pyrex would be better and faster.
> D.
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   
The second argument is available.
Help on function norm in module scipy.linalg.basic:

norm(x, ord=None)
    norm(x, ord=None) -> n

    Matrix or vector norm.

    Inputs:

      x -- a rank-1 (vector) or rank-2 (matrix) array
      ord -- the order of the norm.

     Comments:
       For arrays of any rank, if ord is None:
         calculate the square norm (Euclidean norm for vectors,
Frobenius norm for matrices)

       For vectors ord can be any real number including Inf or -Inf.
         ord = Inf, computes the maximum of the magnitudes
         ord = -Inf, computes minimum of the magnitudes
         ord is finite, computes sum(abs(x)**ord,axis=0)**(1.0/ord)

       For matrices ord can only be one of the following values:
         ord = 2 computes the largest singular value
         ord = -2 computes the smallest singular value
         ord = 1 computes the largest column sum of absolute values
         ord = -1 computes the smallest column sum of absolute values
         ord = Inf computes the largest row sum of absolute values
         ord = -Inf computes the smallest row sum of absolute values
         ord = 'fro' computes the frobenius norm sqrt(sum(diag(X.H *
X),axis=0))

       For values ord < 0, the result is, strictly speaking, not a
       mathematical 'norm', but it may still be useful for numerical
purposes.

You cannot compute the norm of sparse matrices.
http://projects.scipy.org/scipy/scipy/ticket/18

Nils




More information about the NumPy-Discussion mailing list