
    -i                         S SK rS SKJr  S SKJrJrJr  S/r " S S\5      r	 " S S\5      r
 " S S	\5      r " S
 S\5      r " S S5      rg)    N)LinearOperator)kron	eye_array	dia_arrayLaplacianNdc                      ^  \ rS rSrSrS\R                  S.U 4S jjrS rSS jr	S r
S	 rSS
 jrS rS rS rS rS rS rSrU =r$ )r   
   aI  
The grid Laplacian in ``N`` dimensions and its eigenvalues/eigenvectors.

Construct Laplacian on a uniform rectangular grid in `N` dimensions
and output its eigenvalues and eigenvectors.
The Laplacian ``L`` is square, negative definite, real symmetric array
with signed integer entries and zeros otherwise.

Parameters
----------
grid_shape : tuple
    A tuple of integers of length ``N`` (corresponding to the dimension of
    the Lapacian), where each entry gives the size of that dimension. The
    Laplacian matrix is square of the size ``np.prod(grid_shape)``.
boundary_conditions : {'neumann', 'dirichlet', 'periodic'}, optional
    The type of the boundary conditions on the boundaries of the grid.
    Valid values are ``'dirichlet'`` or ``'neumann'``(default) or
    ``'periodic'``.
dtype : dtype
    Numerical type of the array. Default is ``np.int8``.

Methods
-------
toarray()
    Construct a dense array from Laplacian data
tosparse()
    Construct a sparse array from Laplacian data
eigenvalues(m=None)
    Construct a 1D array of `m` largest (smallest in absolute value)
    eigenvalues of the Laplacian matrix in ascending order.
eigenvectors(m=None):
    Construct the array with columns made of `m` eigenvectors (``float``)
    of the ``Nd`` Laplacian corresponding to the `m` ordered eigenvalues.

.. versionadded:: 1.12.0

Notes
-----
Compared to the MATLAB/Octave implementation [1] of 1-, 2-, and 3-D
Laplacian, this code allows the arbitrary N-D case and the matrix-free
callable option, but is currently limited to pure Dirichlet, Neumann or
Periodic boundary conditions only.

The Laplacian matrix of a graph (`scipy.sparse.csgraph.laplacian`) of a
rectangular grid corresponds to the negative Laplacian with the Neumann
conditions, i.e., ``boundary_conditions = 'neumann'``.

All eigenvalues and eigenvectors of the discrete Laplacian operator for
an ``N``-dimensional  regular grid of shape `grid_shape` with the grid
step size ``h=1`` are analytically known [2].

References
----------
.. [1] https://github.com/lobpcg/blopex/blob/master/blopex_tools/matlab/laplacian/laplacian.m
.. [2] "Eigenvalues and eigenvectors of the second derivative", Wikipedia
       https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors_of_the_second_derivative

Examples
--------
>>> import numpy as np
>>> from scipy.sparse.linalg import LaplacianNd
>>> from scipy.sparse import diags_array, csgraph
>>> from scipy.linalg import eigvalsh

The one-dimensional Laplacian demonstrated below for pure Neumann boundary
conditions on a regular grid with ``n=6`` grid points is exactly the
negative graph Laplacian for the undirected linear graph with ``n``
vertices using the sparse adjacency matrix ``G`` represented by the
famous tri-diagonal matrix:

>>> n = 6
>>> G = diags_array(np.ones(n - 1), offsets=1, format='csr')
>>> Lf = csgraph.laplacian(G, symmetrized=True, form='function')
>>> grid_shape = (n, )
>>> lap = LaplacianNd(grid_shape, boundary_conditions='neumann')
>>> np.array_equal(lap.matmat(np.eye(n)), -Lf(np.eye(n)))
True

Since all matrix entries of the Laplacian are integers, ``'int8'`` is
the default dtype for storing matrix representations.

>>> lap.tosparse()
<DIAgonal sparse array of dtype 'int8'
    with 16 stored elements (3 diagonals) and shape (6, 6)>
>>> lap.toarray()
array([[-1,  1,  0,  0,  0,  0],
       [ 1, -2,  1,  0,  0,  0],
       [ 0,  1, -2,  1,  0,  0],
       [ 0,  0,  1, -2,  1,  0],
       [ 0,  0,  0,  1, -2,  1],
       [ 0,  0,  0,  0,  1, -1]], dtype=int8)
>>> np.array_equal(lap.matmat(np.eye(n)), lap.toarray())
True
>>> np.array_equal(lap.tosparse().toarray(), lap.toarray())
True

Any number of extreme eigenvalues and/or eigenvectors can be computed.

>>> lap = LaplacianNd(grid_shape, boundary_conditions='periodic')
>>> lap.eigenvalues()
array([-4., -3., -3., -1., -1.,  0.])
>>> lap.eigenvalues()[-2:]
array([-1.,  0.])
>>> lap.eigenvalues(2)
array([-1.,  0.])
>>> lap.eigenvectors(1)
array([[0.40824829],
       [0.40824829],
       [0.40824829],
       [0.40824829],
       [0.40824829],
       [0.40824829]])
>>> lap.eigenvectors(2)
array([[ 0.5       ,  0.40824829],
       [ 0.        ,  0.40824829],
       [-0.5       ,  0.40824829],
       [-0.5       ,  0.40824829],
       [ 0.        ,  0.40824829],
       [ 0.5       ,  0.40824829]])
>>> lap.eigenvectors()
array([[ 0.40824829,  0.28867513,  0.28867513,  0.5       ,  0.5       ,
         0.40824829],
       [-0.40824829, -0.57735027, -0.57735027,  0.        ,  0.        ,
         0.40824829],
       [ 0.40824829,  0.28867513,  0.28867513, -0.5       , -0.5       ,
         0.40824829],
       [-0.40824829,  0.28867513,  0.28867513, -0.5       , -0.5       ,
         0.40824829],
       [ 0.40824829, -0.57735027, -0.57735027,  0.        ,  0.        ,
         0.40824829],
       [-0.40824829,  0.28867513,  0.28867513,  0.5       ,  0.5       ,
         0.40824829]])

The two-dimensional Laplacian is illustrated on a regular grid with
``grid_shape = (2, 3)`` points in each dimension.

>>> grid_shape = (2, 3)
>>> n = np.prod(grid_shape)

Numeration of grid points is as follows:

>>> np.arange(n).reshape(grid_shape + (-1,))
array([[[0],
        [1],
        [2]],
<BLANKLINE>
       [[3],
        [4],
        [5]]])

Each of the boundary conditions ``'dirichlet'``, ``'periodic'``, and
``'neumann'`` is illustrated separately; with ``'dirichlet'``

>>> lap = LaplacianNd(grid_shape, boundary_conditions='dirichlet')
>>> lap.tosparse()
<Compressed Sparse Row sparse array of dtype 'int8'
    with 20 stored elements and shape (6, 6)>
>>> lap.toarray()
array([[-4,  1,  0,  1,  0,  0],
       [ 1, -4,  1,  0,  1,  0],
       [ 0,  1, -4,  0,  0,  1],
       [ 1,  0,  0, -4,  1,  0],
       [ 0,  1,  0,  1, -4,  1],
       [ 0,  0,  1,  0,  1, -4]], dtype=int8)
>>> np.array_equal(lap.matmat(np.eye(n)), lap.toarray())
True
>>> np.array_equal(lap.tosparse().toarray(), lap.toarray())
True
>>> lap.eigenvalues()
array([-6.41421356, -5.        , -4.41421356, -3.58578644, -3.        ,
       -1.58578644])
>>> eigvals = eigvalsh(lap.toarray().astype(np.float64))
>>> np.allclose(lap.eigenvalues(), eigvals)
True
>>> np.allclose(lap.toarray() @ lap.eigenvectors(),
...             lap.eigenvectors() @ np.diag(lap.eigenvalues()))
True

with ``'periodic'``

>>> lap = LaplacianNd(grid_shape, boundary_conditions='periodic')
>>> lap.tosparse()
<Compressed Sparse Row sparse array of dtype 'int8'
    with 24 stored elements and shape (6, 6)>
>>> lap.toarray()
    array([[-4,  1,  1,  2,  0,  0],
           [ 1, -4,  1,  0,  2,  0],
           [ 1,  1, -4,  0,  0,  2],
           [ 2,  0,  0, -4,  1,  1],
           [ 0,  2,  0,  1, -4,  1],
           [ 0,  0,  2,  1,  1, -4]], dtype=int8)
>>> np.array_equal(lap.matmat(np.eye(n)), lap.toarray())
True
>>> np.array_equal(lap.tosparse().toarray(), lap.toarray())
True
>>> lap.eigenvalues()
array([-7., -7., -4., -3., -3.,  0.])
>>> eigvals = eigvalsh(lap.toarray().astype(np.float64))
>>> np.allclose(lap.eigenvalues(), eigvals)
True
>>> np.allclose(lap.toarray() @ lap.eigenvectors(),
...             lap.eigenvectors() @ np.diag(lap.eigenvalues()))
True

and with ``'neumann'``

>>> lap = LaplacianNd(grid_shape, boundary_conditions='neumann')
>>> lap.tosparse()
<Compressed Sparse Row sparse array of dtype 'int8'
    with 20 stored elements and shape (6, 6)>
>>> lap.toarray()
array([[-2,  1,  0,  1,  0,  0],
       [ 1, -3,  1,  0,  1,  0],
       [ 0,  1, -2,  0,  0,  1],
       [ 1,  0,  0, -2,  1,  0],
       [ 0,  1,  0,  1, -3,  1],
       [ 0,  0,  1,  0,  1, -2]], dtype=int8)
>>> np.array_equal(lap.matmat(np.eye(n)), lap.toarray())
True
>>> np.array_equal(lap.tosparse().toarray(), lap.toarray())
True
>>> lap.eigenvalues()
array([-5., -3., -3., -2., -1.,  0.])
>>> eigvals = eigvalsh(lap.toarray().astype(np.float64))
>>> np.allclose(lap.eigenvalues(), eigvals)
True
>>> np.allclose(lap.toarray() @ lap.eigenvectors(),
...             lap.eigenvectors() @ np.diag(lap.eigenvalues()))
True

neumann)boundary_conditionsdtypec                   > US;  a  [        SU< S35      eXl        X l        [        R                  " U5      n[
        TU ]  X4U4S9  g )N)	dirichletr
   periodiczUnknown value zv is given for 'boundary_conditions' parameter. The valid options are 'dirichlet', 'periodic', and 'neumann' (default).r   shape)
ValueError
grid_shaper   npprodsuper__init__)selfr   r   r   N	__class__s        ]/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/linalg/_special_sparse_arrays.pyr   LaplacianNd.__init__   s_     &JJ !4 7 8D D  %#6 GGJuF3    c           
         U R                   nUc-  [        R                  " U5      n[        R                  " U5      nOX[	        U[        [        R                  " U5      U-  5      5      n[        R                  " U5      n[        R                  " U5      n[        X25       H  u  pgU R                  S:X  a>  US[        R                  " [        R                  US-   -  SUS-   -  -  5      S-  -  -  nMS  U R                  S:X  a8  US[        R                  " [        R                  U-  SU-  -  5      S-  -  -  nM  US[        R                  " [        R                  [        R                  " US-   S-  5      -  U-  5      S-  -  -  nM     UR                  5       n[        R                  " U5      n	X   n
Ub
  X* S n
X* S n	X4$ )zCompute `m` largest eigenvalues in each of the ``N`` directions,
i.e., up to ``m * N`` total, order them and return `m` largest.
Nr         r
   )r   r   indiceszerosmintuple	ones_likezipr   sinpifloorravelargsort)r   mr   r"   Leiggrid_shape_minjn
Leig_ravelindeigenvaluess              r   _eigenvalue_ordering LaplacianNd._eigenvalue_ordering  s    __
9jj,G88J'D !&r||J'?!'C!DFNjj0G88N+D,DA'';6RVVBEEQUOqAE{$CDIII))Y6RVVBEEAIQ$78A===RVVBEEBHHa!eq[,A$AA$EF!KKK - ZZ\
jj$ o=%bc*Kbc(Cr   c                 ,    U R                  U5      u  p#U$ )a>  Return the requested number of eigenvalues.

Parameters
----------
m : int, optional
    The positive number of smallest eigenvalues to return.
    If not provided, then all eigenvalues will be returned.

Returns
-------
eigenvalues : float array
    The requested `m` smallest or all eigenvalues, in ascending order.
)r5   )r   r-   r4   _s       r   r4   LaplacianNd.eigenvalues%  s     2215r   c                 t   U R                   S:X  aj  [        R                  [        R                  " U5      S-   -  US-   -  n[        R                  " SUS-   -  5      [        R
                  " X1S-   -  5      -  nGOuU R                   S:X  ah  [        R                  [        R                  " U5      S-   -  U-  n[        R                  " US:X  a  SOSU-  5      [        R                  " X1-  5      -  nOUS:X  a1  [        R                  " SU-  5      [        R                  " U5      -  nOUS-   U:X  a@  US-  S:X  a7  [        R                  " SU-  5      [        R                  " SS	/US-  5      -  nO}S[        R                  -  [        R                  " U5      S-   -  U-  n[        R                  " SU-  5      [        R                  " U[        R                  " US-   S-  5      -  5      -  nS
U[        R                  " U5      [        R                  " [        R                  5      R                  :  '   U$ )zYReturn 1 eigenvector in 1d with index `j`
and number of grid points `n` where ``j < n``.
r   r    g       @      ?r
         ?r   r!   g        )r   r   r)   arangesqrtr(   cosonestiler*   absfinfofloat64eps)r   r0   r1   ievs        r   _ev1dLaplacianNd._ev1d6  s    ##{21)*a!e4Aq2v'"&&!e*==B%%21+,q0AQ"B!34rvvae}DBAvWWR!V_rwwqz1Q!A
WWR!V_rww2w1'==J"))A,"459WWR!V_rvva"((AEQ;2G.G'HH 57266":,0001	r   c                    [        XR                  5       VVs/ s H  u  p#U R                  X#5      PM     nnnUS   nUSS  H  n[        R                  " XTSS9nM     [        R
                  " U5      R                  5       $ s  snnf )zjReturn 1 eigenvector in Nd with multi-index `j`
as a tensor product of the corresponding 1d eigenvectors.
r   r    N)axes)r'   r   rI   r   	tensordotasarrayr+   )r   kr0   r1   phiresults         r   _one_eveLaplacianNd._one_eveM  su     -0??,CD,CDAtzz!,CDQqr7C\\&A6F zz&!''))	 Es   Bc                    U R                  U5      u  p#Uc  U R                  nO@[        U R                  [        [        R
                  " U R                  5      U-  5      5      n[        R                  " X45      n[        U6  Vs/ s H  n[        U5      PM     nnU Vs/ s H  opR                  U5      PM     nn[        R                  " U5      $ s  snf s  snf )a  Return the requested number of eigenvectors for ordered eigenvalues.

Parameters
----------
m : int, optional
    The positive number of eigenvectors to return. If not provided,
    then all eigenvectors will be returned.

Returns
-------
eigenvectors : float array
    An array with columns made of the requested `m` or all eigenvectors.
    The columns are ordered according to the `m` ordered eigenvalues.
)
r5   r   r$   r%   r   r&   unravel_indexr'   rR   column_stack)	r   r-   r8   r3   r/   	N_indicesxrO   eigenvectors_lists	            r   eigenvectorsLaplacianNd.eigenvectorsW  s     **1-9!__N  %bll4??&Ca&G HJN $$S9	'*I7!U1X	77@Ay!]]1-yA011 8As   CCc           
         U R                   n[        R                  " U5      n[        R                  " X"/[        R                  S9n[        R
                  " U5      n[        R
                  " U5      n[        U5       GH  u  pgSUSS& S[        R                  " SUSU2SU24   5      SS& S[        R                  " SUSUS-
  2SU24   5      SS& S[        R                  " SUSU2SUS-
  24   5      SS& U R                  S:X  a  SUS	'   SXGS-
  US-
  4'   OGU R                  S
:X  a7  US:  a$  USUS-
  4==   S-  ss'   XGS-
  S4==   S-  ss'   OUS	==   S-  ss'   UnUS:  aT  [        R                  " USU 5      n	[        SU	5       H+  n
USU2SU24   XJU-  U
S-   U-  2X-  U
S-   U-  24'   X-  nM-     USU2SU24   USU2SU24'   [        [        R                  " XS-   S 5      5      n	SUSU2SU24'   [        U	5       Vs/ s H  oPM     nnUSU2SU24   UR                  XX45      SS2USS2U4'   X4-  nGM     UR                  U R                  5      $ s  snf )z
Converts the Laplacian data to a dense array.

Returns
-------
L : ndarray
    The shape is ``(N, N)`` where ``N = np.prod(grid_shape)``.

r   r   Nzii->ir    r
   r=   r   r   r   )r   r   r   r#   int8
empty_like	enumerateeinsumr   rangeintreshapeastyper   )r   r   r1   LL_iLtempr3   dimnew_dimtilesr0   rX   idxs                r   toarrayLaplacianNd.toarrayr  s    __
GGJHHaV277+mmAa !*-HCCF 68BIIgs4C4#:/2;<BIIgs9S1W9ae#345a8;<BIIgs1S5)C!G)#345a8''94D	(*!GS1W$%))Z7737
Oq(Oa
Oq(OINI GQw
4C 01q%A<?dsd
OC#qsCi!Sy89NG )
 ),HWHhwh,>(?E(7(HWH$%
q56 234E&'C(7("##El+l1lC+ %*(7(HWH*<$= KK! S!S."
 HAW .Z xx

## ,s   I%c           
         [        U R                  5      n[        R                  " U R                  5      n[	        X"4[        R
                  S9n[        U5       GHa  nU R                  U   n[        R                  " SU/[        R
                  S9nUSSS24==   S-  ss'   U R                  S:X  a
  SUS'   SUS	'   [	        U/ S
Q4XU4[        R
                  S9nU R                  S:X  aF  [	        XU4[        R
                  S9nUR                  S/U* S-   S9  UR                  S/US-
  S9  Xx-  n[        U5       H2  n	[        [        U R                  U	   [        R
                  S9U5      nM4     [        US-   U5       H2  n	[        U[        U R                  U	   [        R
                  S95      nM4     X7-  nGMd     UR                  U R                  5      $ )z
Constructs a sparse array from the Laplacian data. The returned sparse
array format is dependent on the selected boundary conditions.

Returns
-------
L : scipy.sparse.sparray
    The shape is ``(N, N)`` where ``N = np.prod(grid_shape)``.

r]      r    Nr^   r
   r=   r    r   )r    r=   r=   r   r    )r   r   r   )rO   )lenr   r   r   r   r`   rd   rA   r   setdiagr   r   rg   r   )
r   r   prh   rG   rk   datari   tr0   s
             r   tosparseLaplacianNd.tosparse  s     GGDOO$qfBGG,qA//!$C77As82773DAJ"J''94T
 UT:.sj"$''C '':5sj8		1##a	(		1#Q	'1X9T__Q%7rwwGM 1q5!_3	$//!*<BGG LM %HA/ 0 xx

##r   c           
         U R                   n[        U5      nUR                  US-   5      nSU-  U-  n[        U5       GH  nU[        R
                  " USUS9-  nU[        R
                  " USUS9-  nU R                  S;   d  MH  U[        S 5      4U-  S-   [        S 5      4X6-
  S-
  -  -   ==   [        R
                  " USUS9[        S 5      4U-  S-   [        S 5      4X6-
  S-
  -  -      -  ss'   U[        S 5      4U-  S-   [        S 5      4X6-
  S-
  -  -   ==   [        R
                  " USUS9[        S 5      4U-  S-   [        S 5      4X6-
  S-
  -  -      -  ss'   U R                  S:X  d  GM7  U[        S 5      4U-  S-   [        S 5      4X6-
  S-
  -  -   ==   [        R
                  " US	US9[        S 5      4U-  S-   [        S 5      4X6-
  S-
  -  -      -  ss'   U[        S 5      4U-  S-   [        S 5      4X6-
  S-
  -  -   ==   [        R
                  " US	US9[        S 5      4U-  S-   [        S 5      4X6-
  S-
  -  -      -  ss'   GM     UR                  SUR                  S   5      $ )
N)r=   r^   r    )axisr=   )r
   r   )r   r
   r   )	r   ru   rf   rd   r   rollr   slicer   )r   rX   r   r   XYrG   s          r   _matvecLaplacianNd._matvec  s   __

OIIj5()FQJqAAA&&ABQ''A''+CC5;."T)U4[NACE,BB wwq!!,4[NQ&-t!#a%0HH  4[NQ&.%+13q51IIWWQ+4[NQ&.%+13q51II  ++y8t*T1U4[Nac!e4LLAA.t*T1U4[Nac!e4LL 
 t*U2eDk^qs1u5MMAA.t*U2eDk^qs1u5MM ) 4 yyQWWR[))r   c                 $    U R                  U5      $ Nr   r   rX   s     r   _matmatLaplacianNd._matmat  s    ||Ar   c                     U $ r    r   s    r   _adjointLaplacianNd._adjoint      r   c                     U $ r   r   r   s    r   
_transposeLaplacianNd._transpose  r   r   )r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r`   r   r5   r4   rI   rR   rZ   ro   rz   r   r   r   r   __static_attributes____classcell__r   s   @r   r   r   
   s_    hV &/ww4 4" >".*26>$@'$R*B r   c                   z   ^  \ rS rSrSr\R                  4U 4S jjrSS jrS r	S r
S rS rS	 rS
 rS rSrU =r$ )Sakuraii  aT  
Construct a Sakurai matrix in various formats and its eigenvalues.

Constructs the "Sakurai" matrix motivated by reference [1]_:
square real symmetric positive definite and 5-diagonal
with the main diagonal ``[5, 6, 6, ..., 6, 6, 5], the ``+1`` and ``-1``
diagonals filled with ``-4``, and the ``+2`` and ``-2`` diagonals
made of ``1``. Its eigenvalues are analytically known to be
``16. * np.power(np.cos(0.5 * k * np.pi / (n + 1)), 4)``.
The matrix gets ill-conditioned with its size growing.
It is useful for testing and benchmarking sparse eigenvalue solvers
especially those taking advantage of its banded 5-diagonal structure.
See the notes below for details.

Parameters
----------
n : int
    The size of the matrix.
dtype : dtype
    Numerical type of the array. Default is ``np.int8``.

Methods
-------
toarray()
    Construct a dense array from Laplacian data
tosparse()
    Construct a sparse array from Laplacian data
tobanded()
    The Sakurai matrix in the format for banded symmetric matrices,
    i.e., (3, n) ndarray with 3 upper diagonals
    placing the main diagonal at the bottom.
eigenvalues
    All eigenvalues of the Sakurai matrix ordered ascending.

Notes
-----
Reference [1]_ introduces a generalized eigenproblem for the matrix pair
`A` and `B` where `A` is the identity so we turn it into an eigenproblem
just for the matrix `B` that this function outputs in various formats
together with its eigenvalues.

.. versionadded:: 1.12.0

References
----------
.. [1] T. Sakurai, H. Tadano, Y. Inadomi, and U. Nagashima,
   "A moment-based method for large-scale generalized
   eigenvalue problems",
   Appl. Num. Anal. Comp. Math. Vol. 1 No. 2 (2004).

Examples
--------
>>> import numpy as np
>>> from scipy.sparse.linalg._special_sparse_arrays import Sakurai
>>> from scipy.linalg import eig_banded
>>> n = 6
>>> sak = Sakurai(n)

Since all matrix entries are small integers, ``'int8'`` is
the default dtype for storing matrix representations.

>>> sak.toarray()
array([[ 5, -4,  1,  0,  0,  0],
       [-4,  6, -4,  1,  0,  0],
       [ 1, -4,  6, -4,  1,  0],
       [ 0,  1, -4,  6, -4,  1],
       [ 0,  0,  1, -4,  6, -4],
       [ 0,  0,  0,  1, -4,  5]], dtype=int8)
>>> sak.tobanded()
array([[ 1,  1,  1,  1,  1,  1],
       [-4, -4, -4, -4, -4, -4],
       [ 5,  6,  6,  6,  6,  5]], dtype=int8)
>>> sak.tosparse()
<DIAgonal sparse array of dtype 'int8'
    with 24 stored elements (5 diagonals) and shape (6, 6)>
>>> np.array_equal(sak.dot(np.eye(n)), sak.tosparse().toarray())
True
>>> sak.eigenvalues()
array([0.03922866, 0.56703972, 2.41789479, 5.97822974,
       10.54287655, 14.45473055])
>>> sak.eigenvalues(2)
array([0.03922866, 0.56703972])

The banded form can be used in scipy functions for banded matrices, e.g.,

>>> e = eig_banded(sak.tobanded(), eigvals_only=True)
>>> np.allclose(sak.eigenvalues(), e, atol= n * n * n * np.finfo(float).eps)
True

c                 B   > Xl         X l        X4n[        TU ]  X#5        g r   )r1   r   r   r   )r   r1   r   r   r   s       r   r   Sakurai.__init__a  s!    
&r   c           
      T   Uc  U R                   n[        R                  " U R                   S-   U-
  U R                   S-   5      n[        R                  " S[        R                  " [        R
                  " SU-  [        R                  -  U R                   S-   -  5      S5      -  5      $ )aE  Return the requested number of eigenvalues.

Parameters
----------
m : int, optional
    The positive number of smallest eigenvalues to return.
    If not provided, then all eigenvalues will be returned.

Returns
-------
eigenvalues : `np.float64` array
    The requested `m` smallest or all eigenvalues, in ascending order.
r    g      0@r<      )r1   r   r>   flippowerr@   r)   )r   r-   rO   s      r   r4   Sakurai.eigenvaluesg  sw     9AIIdffqj!mTVVaZ0wwsRXXbffS1Wruu_
-K&LaPPQQr   c                    [         R                  SS[         R                  " U R                  S-
  U R                  S9-  S4   nS[         R                  " U R                  U R                  S9-  n[         R                  " U R                  U R                  S9n[         R
                  " X2U/5      R                  U R                  5      $ )z1
Construct the Sakurai matrix as a banded array.
      r!   r]   r   )r   r_rA   r1   r   arrayrg   )r   d0d1d2s       r   tobandedSakurai.tobandedz  s     UU1a"''$&&1*DJJ??BC"''$&&

33WWTVV4::.xx%,,TZZ88r   c                     SSK Jn  U R                  5       nU" US   US   US   US   US   // SQU R                  U R                  4UR                  S9$ )z2
Construct the Sakurai matrix in a sparse format.
r   diags_arrayr    r!   )r^   r=   r   r    r!   offsetsr   r   )scipy.sparser   r   r1   r   )r   r   ds      r   rz   Sakurai.tosparse  s]     	-MMO AaD!A$!adAaD9CT"&&&$&&!1B 	Br   c                 >    U R                  5       R                  5       $ r   rz   ro   r   s    r   ro   Sakurai.toarray      }}&&((r   c                 L   UR                  U R                  S5      n[        R                  " UR                  U R                  5      n[        R
                  " XS9nSUSSS24   -  SUSSS24   -  -
  USSS24   -   USSS24'   SUSSS24   -  SUS	SS24   -  -
  US
SS24   -   USSS24'   SUSS2SS24   -  SUSS	2SS24   USS2SS24   -   -  -
  [        R                  " USS
2SS24   S5      -   [        R                  " USS2SS24   S5      -   USS2SS24'   U$ )z
Construct matrix-free callable banded-matrix-vector multiplication by
the Sakurai matrix without constructing or storing the matrix itself
using the knowledge of its entries and the 5-diagonal format.
r=   r]   r   r   Nr   r    r!   r^   r   )rs   r_   rr   ))r   r    r_   )rf   r1   r   promote_typesr   
zeros_likepad)r   rX   result_dtypesxs       r   r   Sakurai._matvec  s9    IIdffb!''<]]11qAw;Qq!tW,qAw61a4"a%L1qQx</!BE(:2q5	AaeQhK!q"ay1QRU8/C*DDq"ay*:;<qQx)9:;1b5!8 	r   c                 $    U R                  U5      $ )z
Construct matrix-free callable matrix-matrix multiplication by
the Sakurai matrix without constructing or storing the matrix itself
by reusing the ``_matvec(x)`` that supports both 1D and 2D arrays ``x``.
r   r   s     r   r   Sakurai._matmat       ||Ar   c                     U $ r   r   r   s    r   r   Sakurai._adjoint  r   r   c                     U $ r   r   r   s    r   r   Sakurai._transpose  r   r   )r   r1   r   )r   r   r   r   r   r   r`   r   r4   r   rz   ro   r   r   r   r   r   r   r   s   @r   r   r     sG    Yt !# 'R&9	B)  r   r   c                   v   ^  \ rS rSrSr\R                  4U 4S jjrS rS r	S r
S rS rS	 rS
 rS rSrU =r$ )MikotaMi  a'  
Construct a mass matrix in various formats of Mikota pair.

The mass matrix `M` is square real diagonal
positive definite with entries that are reciprocal to integers.

Parameters
----------
shape : tuple of int
    The shape of the matrix.
dtype : dtype
    Numerical type of the array. Default is ``np.float64``.

Methods
-------
toarray()
    Construct a dense array from Mikota data
tosparse()
    Construct a sparse array from Mikota data
tobanded()
    The format for banded symmetric matrices,
    i.e., (1, n) ndarray with the main diagonal.
c                 <   > Xl         X l        [        TU ]  X!5        g r   )r   r   r   r   )r   r   r   r   s      r   r   MikotaM.__init__  s    

&r   c                     S[         R                  " SU R                  S   S-   5      -  R                  U R                  5      $ )Nr;   r    r   )r   r>   r   rg   r   r   s    r   _diagMikotaM._diag  s6     RYYq$**Q-!"344<<TZZHHr   c                 "    U R                  5       $ r   )r   r   s    r   r   MikotaM.tobanded  s    zz|r   c                 h    SSK Jn  U" U R                  5       /S/U R                  U R                  S9$ )Nr   r   r   )r   r   r   r   r   r   r   s     r   rz   MikotaM.tosparse  s-    ,DJJL>A3!%4::? 	?r   c                 |    [         R                  " U R                  5       5      R                  U R                  5      $ r   )r   diagr   rg   r   r   s    r   ro   MikotaM.toarray  s&    wwtzz|$++DJJ77r   c                     UR                  U R                  S   S5      nU R                  5       SS2[        R                  4   U-  $ )z
Construct matrix-free callable banded-matrix-vector multiplication by
the Mikota mass matrix without constructing or storing the matrix itself
using the knowledge of its entries and the diagonal format.
r   r=   N)rf   r   r   r   newaxisr   s     r   r   MikotaM._matvec  s:     IIdjjmR(zz|ArzzM*Q..r   c                 $    U R                  U5      $ )z
Construct matrix-free callable matrix-matrix multiplication by
the Mikota mass matrix without constructing or storing the matrix itself
by reusing the ``_matvec(x)`` that supports both 1D and 2D arrays ``x``.
r   r   s     r   r   MikotaM._matmat  r   r   c                     U $ r   r   r   s    r   r   MikotaM._adjoint  r   r   c                     U $ r   r   r   s    r   r   MikotaM._transpose  r   r   r   )r   r   r   r   r   r   rE   r   r   r   rz   ro   r   r   r   r   r   r   r   s   @r   r   r     sD    . %'JJ '
I
?
8/ r   r   c                   p   ^  \ rS rSrSr\R                  4U 4S jjrS rS r	S r
S rS rS	 rS
 rSrU =r$ )MikotaKi  aQ  
Construct a stiffness matrix in various formats of Mikota pair.

The stiffness matrix `K` is square real tri-diagonal symmetric
positive definite with integer entries.

Parameters
----------
shape : tuple of int
    The shape of the matrix.
dtype : dtype
    Numerical type of the array. Default is ``np.int32``.

Methods
-------
toarray()
    Construct a dense array from Mikota data
tosparse()
    Construct a sparse array from Mikota data
tobanded()
    The format for banded symmetric matrices,
    i.e., (2, n) ndarray with 2 upper diagonals
    placing the main diagonal at the bottom.
c                    > Xl         X l        [        TU ]  X!5        US   n[        R
                  " SU-  S-
  SSU R                  S9U l        [        R
                  " US-
  SSU R                  S9* U l        g )Nr   r!   r    r^   r]   r=   )r   r   r   r   r   r>   _diag0_diag1)r   r   r   r1   r   s       r   r   MikotaK.__init__  sh    

& !HiiA	1b

C		!a%BdjjAAr   c                     [         R                  " [         R                  " U R                  SS5      U R                  /5      $ )Nrs   constant)r   r   r   r   r   r   s    r   r   MikotaK.tobanded  s+    xxVZ@$++NOOr   c                     SSK Jn  U" U R                  U R                  U R                  // SQU R                  U R
                  S9$ )Nr   r   rt   r   )r   r   r   r   r   r   r   s     r   rz   MikotaK.tosparse  s6    ,DKKdkkBJ!%4::? 	?r   c                 >    U R                  5       R                  5       $ r   r   r   s    r   ro   MikotaK.toarray   r   r   c                    UR                  U R                  S   S5      n[        R                  " UR                  U R                  5      n[        R
                  " XS9nU R                  nU R                  nUS   USSS24   -  US   USSS24   -  -   USSS24'   US   USSS24   -  US   USSS24   -  -   USSS24'   USS2S4   USS2SS24   -  USS2S4   USS2SS24   -  -   USS2S4   USS2SS24   -  -   USS2SS24'   U$ )z
Construct matrix-free callable banded-matrix-vector multiplication by
the Mikota stiffness matrix without constructing or storing the matrix
itself using the knowledge of its entries and the 3-diagonal format.
r   r=   r]   Nr    r^   r!   )rf   r   r   r   r   r   r   r   )r   rX   r   kxr   r   s         r   r   MikotaK._matvec#  s7    IIdjjmR(''<]]11[[[[a51QT7?RUQq!tW_41a4rFQr1uX%22q5(992q5	3B39$B$'
2QUD[/AaeQhK78QRX,12q5121b5!8 	r   c                 $    U R                  U5      $ )z
Construct matrix-free callable matrix-matrix multiplication by
the Stiffness mass matrix without constructing or storing the matrix itself
by reusing the ``_matvec(x)`` that supports both 1D and 2D arrays ``x``.
r   r   s     r   r   MikotaK._matmat5  r   r   c                     U $ r   r   r   s    r   r   MikotaK._adjoint=  r   r   c                     U $ r   r   r   s    r   r   MikotaK._transpose@  r   r   )r   r   r   r   )r   r   r   r   r   r   int32r   r   rz   ro   r   r   r   r   r   r   r   s   @r   r   r     s@    0 %'HH BP?
)$ r   r   c                   B    \ rS rSrSr\R                  4S jrSS jrSr	g)
MikotaPairiD  at  
Construct the Mikota pair of matrices in various formats and
eigenvalues of the generalized eigenproblem with them.

The Mikota pair of matrices [1, 2]_ models a vibration problem
of a linear mass-spring system with the ends attached where
the stiffness of the springs and the masses increase along
the system length such that vibration frequencies are subsequent
integers 1, 2, ..., `n` where `n` is the number of the masses. Thus,
eigenvalues of the generalized eigenvalue problem for
the matrix pair `K` and `M` where `K` is the system stiffness matrix
and `M` is the system mass matrix are the squares of the integers,
i.e., 1, 4, 9, ..., ``n * n``.

The stiffness matrix `K` is square real tri-diagonal symmetric
positive definite. The mass matrix `M` is diagonal with diagonal
entries 1, 1/2, 1/3, ...., ``1/n``. Both matrices get
ill-conditioned with `n` growing.

Parameters
----------
n : int
    The size of the matrices of the Mikota pair.
dtype : dtype
    Numerical type of the array. Default is ``np.float64``.

Attributes
----------
eigenvalues : 1D ndarray, ``np.uint64``
    All eigenvalues of the Mikota pair ordered ascending.

Methods
-------
MikotaK()
    A `LinearOperator` custom object for the stiffness matrix.
MikotaM()
    A `LinearOperator` custom object for the mass matrix.

.. versionadded:: 1.12.0

References
----------
.. [1] J. Mikota, "Frequency tuning of chain structure multibody oscillators
   to place the natural frequencies at omega1 and N-1 integer multiples
   omega2,..., omegaN", Z. Angew. Math. Mech. 81 (2001), S2, S201-S202.
   Appl. Num. Anal. Comp. Math. Vol. 1 No. 2 (2004).
.. [2] Peter C. Muller and Metin Gurgoze,
   "Natural frequencies of a multi-degree-of-freedom vibration system",
   Proc. Appl. Math. Mech. 6, 319-320 (2006).
   http://dx.doi.org/10.1002/pamm.200610141.

Examples
--------
>>> import numpy as np
>>> from scipy.sparse.linalg._special_sparse_arrays import MikotaPair
>>> n = 6
>>> mik = MikotaPair(n)
>>> mik_k = mik.k
>>> mik_m = mik.m
>>> mik_k.toarray()
array([[11., -5.,  0.,  0.,  0.,  0.],
       [-5.,  9., -4.,  0.,  0.,  0.],
       [ 0., -4.,  7., -3.,  0.,  0.],
       [ 0.,  0., -3.,  5., -2.,  0.],
       [ 0.,  0.,  0., -2.,  3., -1.],
       [ 0.,  0.,  0.,  0., -1.,  1.]])
>>> mik_k.tobanded()
array([[ 0., -5., -4., -3., -2., -1.],
       [11.,  9.,  7.,  5.,  3.,  1.]])
>>> mik_m.tobanded()
array([1.        , 0.5       , 0.33333333, 0.25      , 0.2       ,
    0.16666667])
>>> mik_k.tosparse()
<DIAgonal sparse array of dtype 'float64'
    with 16 stored elements (3 diagonals) and shape (6, 6)>
>>> mik_m.tosparse()
<DIAgonal sparse array of dtype 'float64'
    with 6 stored elements (1 diagonals) and shape (6, 6)>
>>> np.array_equal(mik_k(np.eye(n)), mik_k.toarray())
True
>>> np.array_equal(mik_m(np.eye(n)), mik_m.toarray())
True
>>> mik.eigenvalues()
array([ 1,  4,  9, 16, 25, 36])
>>> mik.eigenvalues(2)
array([ 1,  4])

c                     Xl         X l        X4U l        [        U R                  U R                  5      U l        [        U R                  U R                  5      U l        g r   )r1   r   r   r   r-   r   rO   )r   r1   r   s      r   r   MikotaPair.__init__  sA    
V
TZZ0TZZ0r   Nc                 v    Uc  U R                   n[        R                  " SUS-   [        R                  S9nX"-  $ )aD  Return the requested number of eigenvalues.

Parameters
----------
m : int, optional
    The positive number of smallest eigenvalues to return.
    If not provided, then all eigenvalues will be returned.

Returns
-------
eigenvalues : `np.uint64` array
    The requested `m` smallest or all eigenvalues, in ascending order.
r    r]   )r1   r   r>   uint64)r   r-   arange_plus1s      r   r4   MikotaPair.eigenvalues  s5     9AyyAE;**r   )r   rO   r-   r1   r   r   )
r   r   r   r   r   r   rE   r   r4   r   r   r   r   r   r   D  s    Wp !#

 1+r   r   )numpyr   scipy.sparse.linalgr   r   r   r   r   __all__r   r   r   r   r   r   r   r   <module>r     s`     . 3 3/
y. yxgn gTBn BJLn L^q+ q+r   