Пример #1
0
def tridiagonal_solver(d, e, eigenvectors=True):
    """Calculates the eigenvalues and eigenvectors of a tridiagonal and
    symmetric matrix.

    Parameters
    ----------
    d : a numpy array with ndim = 1.
        The elements of the diagonal of the tridiagonal matrix. 
    e : a numpy array with ndim = 1.
        The off-diagonal elements of the tridiagonal matrix. 
    eigenvectors : a bool (optional).
        Whether you want to calculate the eigenvectors.

    Returns
    -------
    evals : a numpy array with ndim = 1.
        The eigenvalues.
    evecs : a numpy array with ndim = 2.
        The eigenvectors.

    Raises
    ------
    TridiagonalException 
        if `d` and `e` have different sizes.
    """
    if (d.size != e.size + 1):
        raise TridiagonalException("d, and e have different sizes")
    num_evals = d.size
    evals = np.empty(num_evals)
    evecs = np.empty((num_evals, num_evals))

    r = lamRange(d, e, num_evals)
    assert (len(r) == num_evals + 1)

    evals = eigenvals3(d, e, num_evals)

    if eigenvectors:
        for i in range(num_evals):
            evals[i], evecs[:, i] = inversePower3(d, e, 1.00000001 * evals[i])
#
#    An alternative that uses the brakets from lamRange:
#
#    if eigenvectors:
#        for i in range(num_evals):
#            s = (r[i] + r[i+1])/2.0
#    	    evals[i], evecs[:, i] = inversePower3(d, e, s)
#    else:
#	evals = eigenvals3(d, e, num_evals)

    return evals, evecs
Пример #2
0
def tridiagonal_solver(d, e, eigenvectors = True):
    """Calculates the eigenvalues and eigenvectors of a tridiagonal and
    symmetric matrix.

    Parameters
    ----------
    d : a numpy array with ndim = 1.
        The elements of the diagonal of the tridiagonal matrix. 
    e : a numpy array with ndim = 1.
        The off-diagonal elements of the tridiagonal matrix. 
    eigenvectors : a bool (optional).
        Whether you want to calculate the eigenvectors.

    Returns
    -------
    evals : a numpy array with ndim = 1.
        The eigenvalues.
    evecs : a numpy array with ndim = 2.
        The eigenvectors.

    Raises
    ------
    TridiagonalException 
        if `d` and `e` have different sizes.
    """
    if (d.size != e.size + 1):
        raise TridiagonalException("d, and e have different sizes")
    num_evals = d.size
    evals = np.empty(num_evals)
    evecs = np.empty((num_evals, num_evals))

    r = lamRange(d, e, num_evals)
    assert(len(r) == num_evals +1)

    evals = eigenvals3(d, e, num_evals)

    if eigenvectors:
        for i in range(num_evals):
    	    evals[i], evecs[:, i] = inversePower3(d, e, 1.00000001*evals[i])
#
#    An alternative that uses the brakets from lamRange:
#
#    if eigenvectors:
#        for i in range(num_evals):
#            s = (r[i] + r[i+1])/2.0
#    	    evals[i], evecs[:, i] = inversePower3(d, e, s)
#    else:
#	evals = eigenvals3(d, e, num_evals)

    return evals, evecs
Пример #3
0
## example9_14
from householder import *
from eigenvals3 import *
from inversePower3 import *
from numarray import array, zeros, Float64, matrixmultiply

N = 3  # Number of eigenvalues requested
a = array([[ 11.0, 2.0,  3.0,  1.0,  4.0],  \
           [  2.0, 9.0,  3.0,  5.0,  2.0],  \
           [  3.0, 3.0, 15.0,  4.0,  3.0],  \
           [  1.0, 5.0,  4.0, 12.0,  4.0],  \
           [  4.0, 2.0,  3.0,  4.0, 17.0]])
xx = zeros((len(a), N), type=Float64)
d, c = householder(a)  # Tridiagonalize [A]
p = computeP(a)  # Compute transformation matrix
lambdas = eigenvals3(d, c, N)  # Compute eigenvalues
for i in range(N):
    s = lambdas[i] * 1.0000001  # Shift very close to eigenvalue
    lam, x = inversePower3(d, c, s)  # Compute eigenvector [x]
    xx[:, i] = x  # Place [x] in array [xx]
xx = matrixmultiply(p, xx)  # Recover eigenvectors of [A]
print "Eigenvalues:\n", lambdas
print "\nEigenvectors:\n", xx
raw_input("Press return to exit")
Пример #4
0
#!/usr/bin/python
## example9_12

import numpy as np
from eigenvals3 import *

N = 3
n = 100
d = np.ones(n) * 2.0
c = np.ones(n - 1) * (-1.0)
lambdas = eigenvals3(d, c, N)
print(lambdas)
raw_input("\nPress return to exit")
Пример #5
0
## example9_12
from numarray import ones, Float64
from eigenvals3 import *

N = 3
n = 100
d = ones((n)) * 2.0
c = ones((n - 1)) * (-1.0)
lambdas = eigenvals3(d, c, N)
print lambdas
raw_input("\nPress return to exit")
Пример #6
0
## example9_14
from householder import *
from eigenvals3 import *
from inversePower3 import *
from numarray import array,zeros,Float64,matrixmultiply

N = 3   # Number of eigenvalues requested
a = array([[ 11.0, 2.0,  3.0,  1.0,  4.0],  \
           [  2.0, 9.0,  3.0,  5.0,  2.0],  \
           [  3.0, 3.0, 15.0,  4.0,  3.0],  \
           [  1.0, 5.0,  4.0, 12.0,  4.0],  \
           [  4.0, 2.0,  3.0,  4.0, 17.0]])
xx = zeros((len(a),N),type=Float64)
d,c = householder(a)             # Tridiagonalize [A]
p = computeP(a)                  # Compute transformation matrix
lambdas = eigenvals3(d,c,N)      # Compute eigenvalues
for i in range(N):            
    s = lambdas[i]*1.0000001     # Shift very close to eigenvalue
    lam,x = inversePower3(d,c,s) # Compute eigenvector [x]
    xx[:,i] = x                  # Place [x] in array [xx]
xx = matrixmultiply(p,xx)        # Recover eigenvectors of [A]
print "Eigenvalues:\n",lambdas
print "\nEigenvectors:\n",xx
raw_input("Press return to exit")