Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit a86e7e3

Browse files
author
Ubuntu
committed
[Numpy] Add linalg.eigvals/eig/eigvalsh/eigh op
* add source file * fix cudaRunError
1 parent 1cfaf3c commit a86e7e3

File tree

12 files changed

+2345
-3
lines changed

12 files changed

+2345
-3
lines changed

python/mxnet/ndarray/numpy/linalg.py

Lines changed: 264 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from . import _op as _mx_nd_np
2222
from . import _internal as _npi
2323

24-
__all__ = ['norm', 'svd', 'cholesky', 'inv', 'det', 'slogdet', 'solve', 'tensorinv', 'tensorsolve']
24+
__all__ = ['norm', 'svd', 'cholesky', 'inv', 'det', 'slogdet', 'solve', 'tensorinv', 'tensorsolve',
25+
'eigvals', 'eig', 'eigvalsh', 'eigh']
2526

2627

2728
def norm(x, ord=None, axis=None, keepdims=False):
@@ -509,3 +510,265 @@ def tensorsolve(a, b, axes=None):
509510
True
510511
"""
511512
return _npi.tensorsolve(a, b, axes)
513+
514+
515+
def eigvals(a):
516+
r"""
517+
Compute the eigenvalues of a general matrix.
518+
519+
Main difference between `eigvals` and `eig`: the eigenvectors aren't
520+
returned.
521+
522+
Parameters
523+
----------
524+
a : (..., M, M) ndarray
525+
A real-valued matrix whose eigenvalues will be computed.
526+
527+
Returns
528+
-------
529+
w : (..., M,) ndarray
530+
The eigenvalues, each repeated according to its multiplicity.
531+
They are not necessarily ordered.
532+
533+
Raises
534+
------
535+
MXNetError
536+
If the eigenvalue computation does not converge.
537+
538+
See Also
539+
--------
540+
eig : eigenvalues and right eigenvectors of general arrays
541+
eigh : eigenvalues and eigenvectors of a real symmetric array.
542+
eigvalsh : eigenvalues of a real symmetric.
543+
544+
Notes
545+
-----
546+
Broadcasting rules apply, see the `numpy.linalg` documentation for
547+
details.
548+
549+
This is implemented using the ``_geev`` LAPACK routines which compute
550+
the eigenvalues and eigenvectors of general square arrays.
551+
552+
This function differs from the original `numpy.linalg.eigvals
553+
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eigvals.html>`_ in
554+
the following way(s):
555+
- Does not support complex input and output.
556+
557+
Examples
558+
--------
559+
Illustration, using the fact that the eigenvalues of a diagonal matrix
560+
are its diagonal elements, that multiplying a matrix on the left
561+
by an orthogonal matrix, `Q`, and on the right by `Q.T` (the transpose
562+
of `Q`), preserves the eigenvalues of the "middle" matrix. In other words,
563+
if `Q` is orthogonal, then ``Q * A * Q.T`` has the same eigenvalues as
564+
``A``:
565+
>>> from numpy import linalg as LA
566+
>>> x = np.random.random()
567+
>>> Q = np.array([[np.cos(x), -np.sin(x)], [np.sin(x), np.cos(x)]])
568+
>>> LA.norm(Q[0, :]), LA.norm(Q[1, :]), np.dot(Q[0, :],Q[1, :])
569+
(1.0, 1.0, 0.0)
570+
571+
Now multiply a diagonal matrix by ``Q`` on one side and by ``Q.T`` on the other:
572+
>>> D = np.diag((-1,1))
573+
>>> LA.eigvals(D)
574+
array([-1., 1.])
575+
>>> A = np.dot(Q, D)
576+
>>> A = np.dot(A, Q.T)
577+
>>> LA.eigvals(A)
578+
array([ 1., -1.]) # random
579+
"""
580+
return _npi.eigvals(a)
581+
582+
583+
def eigvalsh(a, UPLO='L'):
584+
r"""
585+
Compute the eigenvalues real symmetric matrix.
586+
587+
Main difference from eigh: the eigenvectors are not computed.
588+
589+
Parameters
590+
----------
591+
a : (..., M, M) ndarray
592+
A real-valued matrix whose eigenvalues are to be computed.
593+
UPLO : {'L', 'U'}, optional
594+
Specifies whether the calculation is done with the lower triangular
595+
part of `a` ('L', default) or the upper triangular part ('U').
596+
Irrespective of this value only the real parts of the diagonal will
597+
be considered in the computation to preserve the notion of a Hermitian
598+
matrix. It therefore follows that the imaginary part of the diagonal
599+
will always be treated as zero.
600+
601+
Returns
602+
-------
603+
w : (..., M,) ndarray
604+
The eigenvalues in ascending order, each repeated according to
605+
its multiplicity.
606+
607+
Raises
608+
------
609+
MXNetError
610+
If the eigenvalue computation does not converge.
611+
612+
See Also
613+
--------
614+
eig : eigenvalues and right eigenvectors of general arrays
615+
eigvals : eigenvalues of a non-symmetric array.
616+
eigh : eigenvalues and eigenvectors of a real symmetric array.
617+
618+
Notes
619+
-----
620+
Broadcasting rules apply, see the `numpy.linalg` documentation for
621+
details.
622+
623+
The eigenvalues are computed using LAPACK routines ``_syevd``.
624+
625+
This function differs from the original `numpy.linalg.eigvalsh
626+
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eigvalsh.html>`_ in
627+
the following way(s):
628+
- Does not support complex input and output.
629+
630+
Examples
631+
--------
632+
>>> from numpy import linalg as LA
633+
>>> a = np.array([[ 5.4119368 , 8.996273 , -5.086096 ],
634+
[ 0.8866155 , 1.7490431 , -4.6107802 ],
635+
[-0.08034172, 4.4172044 , 1.4528792 ]])
636+
>>> LA.eigvalsh(a, UPLO='L')
637+
array([-2.87381886, 5.10144682, 6.38623114]) # in ascending order
638+
"""
639+
return _npi.eigvalsh(a, UPLO)
640+
641+
642+
def eig(a):
643+
r"""
644+
Compute the eigenvalues and right eigenvectors of a square array.
645+
646+
Parameters
647+
----------
648+
a : (..., M, M) ndarray
649+
Matrices for which the eigenvalues and right eigenvectors will
650+
be computed
651+
652+
Returns
653+
-------
654+
w : (..., M) ndarray
655+
The eigenvalues, each repeated according to its multiplicity.
656+
The eigenvalues are not necessarily ordered.
657+
v : (..., M, M) ndarray
658+
The normalized (unit "length") eigenvectors, such that the
659+
column ``v[:,i]`` is the eigenvector corresponding to the
660+
eigenvalue ``w[i]``.
661+
662+
Raises
663+
------
664+
MXNetError
665+
If the eigenvalue computation does not converge.
666+
667+
See Also
668+
--------
669+
eigvals : eigenvalues of a non-symmetric array.
670+
eigh : eigenvalues and eigenvectors of a real symmetric array.
671+
eigvalsh : eigenvalues of a real symmetric.
672+
673+
Notes
674+
-----
675+
This is implemented using the ``_geev`` LAPACK routines which compute
676+
the eigenvalues and eigenvectors of general square arrays.
677+
678+
The number `w` is an eigenvalue of `a` if there exists a vector
679+
`v` such that ``dot(a,v) = w * v``. Thus, the arrays `a`, `w`, and
680+
`v` satisfy the equations ``dot(a[:,:], v[:,i]) = w[i] * v[:,i]``
681+
for :math:`i \\in \\{0,...,M-1\\}`.
682+
683+
The array `v` of eigenvectors may not be of maximum rank, that is, some
684+
of the columns may be linearly dependent, although round-off error may
685+
obscure that fact. If the eigenvalues are all different, then theoretically
686+
the eigenvectors are linearly independent.
687+
688+
This function differs from the original `numpy.linalg.eig
689+
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eig.html>`_ in
690+
the following way(s):
691+
- Does not support complex input and output.
692+
693+
Examples
694+
--------
695+
>>> from numpy import linalg as LA
696+
>>> a = np.array([[-1.9147992 , 6.054115 , 18.046988 ],
697+
[ 0.77563655, -4.860152 , 2.1012988 ],
698+
[ 2.6083658 , 2.3705218 , 0.3192524 ]])
699+
>>> w, v = LA.eig(a)
700+
>>> w
701+
array([ 6.9683027, -7.768063 , -5.655937 ])
702+
>>> v
703+
array([[ 0.90617794, 0.9543622 , 0.2492316 ],
704+
[ 0.13086087, -0.04077047, -0.9325615 ],
705+
[ 0.4021404 , -0.29585576, 0.26117516]])
706+
"""
707+
return _npi.eig(a)
708+
709+
710+
def eigh(a, UPLO='L'):
711+
r"""
712+
Return the eigenvalues and eigenvectors real symmetric matrix.
713+
714+
Returns two objects, a 1-D array containing the eigenvalues of `a`, and
715+
a 2-D square array or matrix (depending on the input type) of the
716+
corresponding eigenvectors (in columns).
717+
718+
Parameters
719+
----------
720+
a : (..., M, M) ndarray
721+
real symmetric matrices whose eigenvalues and eigenvectors are to be computed.
722+
UPLO : {'L', 'U'}, optional
723+
Specifies whether the calculation is done with the lower triangular
724+
part of `a` ('L', default) or the upper triangular part ('U').
725+
Irrespective of this value only the real parts of the diagonal will
726+
be considered in the computation to preserve the notion of a Hermitian
727+
matrix. It therefore follows that the imaginary part of the diagonal
728+
will always be treated as zero.
729+
730+
Returns
731+
-------
732+
w : (..., M) ndarray
733+
The eigenvalues in ascending order, each repeated according to
734+
its multiplicity.
735+
v : {(..., M, M) ndarray, (..., M, M) matrix}
736+
The column ``v[:, i]`` is the normalized eigenvector corresponding
737+
to the eigenvalue ``w[i]``. Will return a matrix object if `a` is
738+
a matrix object.
739+
740+
Raises
741+
------
742+
MXNetError
743+
If the eigenvalue computation does not converge.
744+
745+
See Also
746+
--------
747+
eig : eigenvalues and right eigenvectors of general arrays
748+
eigvals : eigenvalues of a non-symmetric array.
749+
eigvalsh : eigenvalues of a real symmetric.
750+
751+
Notes
752+
-----
753+
The eigenvalues/eigenvectors are computed using LAPACK routines ``_syevd``.
754+
755+
This function differs from the original `numpy.linalg.eigh
756+
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eigh.html>`_ in
757+
the following way(s):
758+
- Does not support complex input and output.
759+
760+
Examples
761+
--------
762+
>>> from numpy import linalg as LA
763+
>>> a = np.array([[ 6.8189726 , -3.926585 , 4.3990498 ],
764+
[-0.59656644, -1.9166266 , 9.54532 ],
765+
[ 2.1093285 , 0.19688708, -1.1634291 ]])
766+
>>> w, v = LA.eigh(a, UPLO='L')
767+
>>> w
768+
array([-2.175445 , -1.4581827, 7.3725457])
769+
>>> v
770+
array([[ 0.1805163 , -0.16569263, 0.9695154 ],
771+
[ 0.8242942 , 0.56326365, -0.05721384],
772+
[-0.53661287, 0.80949366, 0.23825769]])
773+
"""
774+
return _npi.eigh(a, UPLO)

0 commit comments

Comments
 (0)