
    Cpj                     l    S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r
  SSKJrJr  S/rS rSS	 jrS
 rg)zSparse matrix norms.

    N)issparse)svds)convert_pydata_sparse_to_scipy)sqrtabsnormc                 ~    [         R                  R                  U 5      n[        R                  R                  U5      $ )N)sp_sputils_todatanplinalgr   )xdatas     U/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/scipy/sparse/linalg/_norm.py_sparse_frobenius_normr      s)    ;;q!D99>>$    c                    [        U SS9n [        U 5      (       d  [        S5      eUc  US;   a  [        U 5      $ U R                  S:X  a  U R                  5       n Uc  [        [        U R                  5      5      nO6[        U[        5      (       d!  Sn [        U5      nX$:w  a  [        U5      eU4nU R                  n[        U5      S:X  Ga  Uu  pxU* Us=::  a  U:  a  O  OU* Us=::  a  U:  d!  O  S	U< S
U R                  < 3n	[        U	5      eXv-  X-  :X  a  [        S5      eUS:X  a  [        U SSSS9u  pn
US   $ US:X  a  [        eUS;   a  [        U 5      $ [         R"                  " U R$                  [&        5      (       a  U R)                  [&        SS9n US:X  a&  [+        U 5      R-                  US9R/                  5       $ U[         R0                  :X  a&  [+        U 5      R-                  US9R/                  5       $ US:X  a&  [+        U 5      R-                  US9R3                  5       $ U[         R0                  * :X  a&  [+        U 5      R-                  US9R3                  5       $ [        S5      e[        U5      S:X  Ga  Uu  nU* Us=::  a  U:  d!  O  S	U< S
U R                  < 3n	[        U	5      eUS:X  a  U R5                  US9$ [         R"                  " U R$                  [&        5      (       a  U R)                  [&        SS9n U[         R0                  :X  a!  [7        [+        U 5      R/                  US95      $ U[         R0                  * :X  a!  [7        [+        U 5      R3                  US95      $ US:X  a!  [7        [+        U 5      R-                  US95      $ US;   a9  [7        [9        [+        U 5      R;                  S5      R-                  US95      5      $  US-     [         R:                  " [+        U 5      R;                  U5      R-                  US9SU-  5      $ [        S5      e! [         a  n[        U5      UeSnAff = f! [         a  n[        S5      UeSnAff = f)a	  
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 array
    Input sparse array.
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
    The selected norm of `x`.

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

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

The following norms can be calculated:

=====  ============================
ord    norm for sparse arrays
=====  ============================
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      Spectral norm (the largest singular value)
-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 csr_array, diags_array
>>> 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_array(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

The matrix 2-norm or the spectral norm is the largest singular
value, computed approximately and with limitations.

>>> b = diags_array([-1, 1], offsets=[0, 1], shape=(9, 10))
>>> norm(b, 2)
1.9753...
csr)target_formatz*input is not sparse. use numpy.linalg.normN)Nfrofdiaz6'axis' must be None, an integer or a tuple of integers   zInvalid axis z for an array with shape zDuplicate axes given.   arpack)ksolverrngr   )Nr   r   F)copy)axisz Invalid norm order for matrices.)r   NzInvalid norm order for vectors.z&Improper number of dimensions to norm.)r   r   	TypeErrorr   formattocsrtuplerangendim
isinstanceintlenshape
ValueErrorr   NotImplementedErrorr   can_castdtypefloatastyper   summaxinfmincount_nonzero_ravelr   power)r   ordr"   msgint_axisendrow_axiscol_axismessage_sas                r   r   r      s   ~ 	'q>AA;;DEE |11%a(( 	xx5GGI|U166]#e$$F	(4yH C. {	
B
4yA~!x$"$")=2)=%dX-FqwwkRGW%%=HM)455!81(=GA!Q4K"9%%$$)!,,;;qww&&U+A!8q6::8:,0022BFF]q6::8:,0022BYq6::8:,0022RVVG^q6::8:,0022?@@	Taq2%dX-FqwwkRGW%%!8???**;;qww&&U+A"&&=#a&**!*,--266'>#a&**!*,--!8#a&**!*,--)$s1v||A222:;<<	G!G xxAS)--1-5q3w??ABBw  	(C.a'	(n  	G>?QF	Gs0   P+ Q	 +
Q5QQ	
Q$QQ$c                     [        U S5      (       a  U R                  5       R                  5       $ [        U S5      (       a  U R                  R                  5       $ U R                  5       $ )NtoarrayA)hasattrrG   ravelrH   )Ms    r   r9   r9      sK    q)yy{  ""	Cssyy{wwyr   )NN)__doc__numpyr   scipy.sparser   scipy.sparse.linalgr   scipy.sparse._sputilsr   sparser
   r   r   __all__r   r   r9    r   r   <module>rT      s7     ! $ @  ( 
lC^r   