ó
î&]\c           @` s   d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l	 m
 Z
 m Z m Z d g Z d „  Z d d d „ Z d S(	   s   Sparse matrix norms.

i    (   t   divisiont   print_functiont   absolute_importN(   t   issparse(   t   Inft   sqrtt   abst   normc         C` sU   t  j |  j t  j ƒ r6 t |  ƒ j d ƒ j ƒ  } n |  j d ƒ j ƒ  } t | ƒ S(   Ni   (   t   npt
   issubdtypet   dtypet   complexfloatingR   t   powert   sumR   (   t   xt   sqnorm(    (    s8   lib/python2.7/site-packages/scipy/sparse/linalg/_norm.pyt   _sparse_frobenius_norm   s    c   
      C` s   t  |  ƒ s t d ƒ ‚ n  | d k r= | d k r= t |  ƒ S|  j ƒ  }  | d k r^ d } nl t | t ƒ sÊ d } y t | ƒ } Wn t k
 r¢ t | ƒ ‚ n X| | k r¾ t | ƒ ‚ n  | f } n  d } t | ƒ d k r| \ } } | | k o| k  n o%| | k o#| k  n sDt	 d | |  j
 f ƒ ‚ n  | | | | k rgt	 d	 ƒ ‚ n  | d k r|t ‚ q| d
 k r‘t ‚ q| d k rÃt |  ƒ j d | ƒ j d | ƒ d S| t k rõt |  ƒ j d | ƒ j d | ƒ d S| d k r't |  ƒ j d | ƒ j d | ƒ d S| t k rZt |  ƒ j d | ƒ j d | ƒ d S| d k rpt |  ƒ St	 d ƒ ‚ nt | ƒ d k r| \ } | | k o²| k  n sÓt	 d | |  j
 f ƒ ‚ n  | t k rút |  ƒ j d | ƒ }	 n	| t k r"t |  ƒ j d | ƒ }	 ná | d k rI|  d k j d | ƒ }	 nº | d k rpt |  ƒ j d | ƒ }	 n“ | d k r¦t t |  ƒ j d ƒ j d | ƒ ƒ }	 n] y | d Wn t k
 rÑt	 d ƒ ‚ n Xt j t |  ƒ j | ƒ j d | ƒ d | ƒ }	 |	 j j ƒ  St	 d ƒ ‚ d S(   sF
  
    Norm of a sparse matrix

    This function is able to return one of seven different matrix norms,
    depending on the value of the ``ord`` parameter.

    Parameters
    ----------
    x : a sparse matrix
        Input sparse matrix.
    ord : {non-zero int, inf, -inf, 'fro'}, optional
        Order of the norm (see table under ``Notes``). inf means numpy's
        `inf` object.
    axis : {int, 2-tuple of ints, None}, optional
        If `axis` is an integer, it specifies the axis of `x` along which to
        compute the vector norms.  If `axis` is a 2-tuple, it specifies the
        axes that hold 2-D matrices, and the matrix norms of these matrices
        are computed.  If `axis` is None then either a vector norm (when `x`
        is 1-D) or a matrix norm (when `x` is 2-D) is returned.

    Returns
    -------
    n : float or ndarray

    Notes
    -----
    Some of the ord are not implemented because some associated functions like, 
    _multi_svd_norm, are not yet available for sparse matrix. 

    This docstring is modified based on numpy.linalg.norm. 
    https://github.com/numpy/numpy/blob/master/numpy/linalg/linalg.py 

    The following norms can be calculated:

    =====  ============================  
    ord    norm for sparse matrices             
    =====  ============================  
    None   Frobenius norm                
    'fro'  Frobenius norm                
    inf    max(sum(abs(x), axis=1))      
    -inf   min(sum(abs(x), axis=1))      
    0      abs(x).sum(axis=axis)                           
    1      max(sum(abs(x), axis=0))      
    -1     min(sum(abs(x), axis=0))      
    2      Not implemented  
    -2     Not implemented      
    other  Not implemented                               
    =====  ============================  

    The Frobenius norm is given by [1]_:

        :math:`||A||_F = [\sum_{i,j} abs(a_{i,j})^2]^{1/2}`

    References
    ----------
    .. [1] G. H. Golub and C. F. Van Loan, *Matrix Computations*,
        Baltimore, MD, Johns Hopkins University Press, 1985, pg. 15

    Examples
    --------
    >>> from scipy.sparse import *
    >>> import numpy as np
    >>> from scipy.sparse.linalg import norm
    >>> a = np.arange(9) - 4
    >>> a
    array([-4, -3, -2, -1, 0, 1, 2, 3, 4])
    >>> b = a.reshape((3, 3))
    >>> b
    array([[-4, -3, -2],
           [-1, 0, 1],
           [ 2, 3, 4]])

    >>> b = csr_matrix(b)
    >>> norm(b)
    7.745966692414834
    >>> norm(b, 'fro')
    7.745966692414834
    >>> norm(b, np.inf)
    9
    >>> norm(b, -np.inf)
    2
    >>> norm(b, 1)
    7
    >>> norm(b, -1)
    6

    s*   input is not sparse. use numpy.linalg.normt   frot   fi    i   s6   'axis' must be None, an integer or a tuple of integersi   s*   Invalid axis %r for an array with shape %rs   Duplicate axes given.iþÿÿÿt   axisiÿÿÿÿs    Invalid norm order for matrices.s   Invalid norm order for vectors.s&   Improper number of dimensions to norm.N(   NR   R   (   i    i   (   i    i    (   i    i    (   i    i    (   i    i    (   NR   R   (   i   N(   R   t	   TypeErrort   NoneR   t   tocsrt
   isinstancet   tuplet   intt   lent
   ValueErrort   shapet   NotImplementedErrorR   R   t   maxR   t   minR   R   R   t   At   ravel(
   R   t   ordR   t   msgt   int_axist   ndt   row_axist   col_axist   at   M(    (    s8   lib/python2.7/site-packages/scipy/sparse/linalg/_norm.pyR      sz    X
	:		&&&&
	*1(   t   __doc__t
   __future__R    R   R   t   numpyR   t   scipy.sparseR   t
   numpy.coreR   R   R   t   __all__R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/sparse/linalg/_norm.pyt   <module>   s   		