Ejemplo n.º 1
0
def test_eig_dyn():
    v = 0
    for i in xrange(5):
        n = 1 + int(mp.rand() * 5)
        if mp.rand() > 0.5:
            # real
            A = 2 * mp.randmatrix(n, n) - 1
            if mp.rand() > 0.5:
                A *= 10
                for x in xrange(n):
                    for y in xrange(n):
                        A[x, y] = int(A[x, y])
        else:
            A = (2 * mp.randmatrix(n, n) -
                 1) + 1j * (2 * mp.randmatrix(n, n) - 1)
            if mp.rand() > 0.5:
                A *= 10
                for x in xrange(n):
                    for y in xrange(n):
                        A[x,
                          y] = int(mp.re(A[x, y])) + 1j * int(mp.im(A[x, y]))

        run_hessenberg(A, verbose=v)
        run_schur(A, verbose=v)
        run_eig(A, verbose=v)
Ejemplo n.º 2
0
def test_eighe_randmatrix():
    N = 5

    for a in xrange(10):
        A = (2 * mp.randmatrix(N, N) - 1) + 1j * (2 * mp.randmatrix(N, N) - 1)

        for i in xrange(0, N):
            A[i, i] = mp.re(A[i, i])
            for j in xrange(i + 1, N):
                A[j, i] = mp.conj(A[i, j])

        run_eighe(A)
Ejemplo n.º 3
0
def test_eighe_randmatrix():
    N = 5

    for a in xrange(10):
        A = (2 * mp.randmatrix(N, N) - 1) + 1j * (2 * mp.randmatrix(N, N) - 1)

        for i in xrange(0, N):
            A[i,i] = mp.re(A[i,i])
            for j in xrange(i + 1, N):
                A[j,i] = mp.conj(A[i,j])

        run_eighe(A)
Ejemplo n.º 4
0
def test_svd_c_rand():
    for i in xrange(5):
        full = mp.rand() > 0.5
        m = 1 + int(mp.rand() * 10)
        n = 1 + int(mp.rand() * 10)
        A = (2 * mp.randmatrix(m, n) - 1) + 1j * (2 * mp.randmatrix(m, n) - 1)
        if mp.rand() > 0.5:
            A *= 10
            for x in xrange(m):
                for y in xrange(n):
                    A[x, y] = int(mp.re(A[x, y])) + 1j * int(mp.im(A[x, y]))

        run_svd_c(A, full_matrices=full, verbose=False)
Ejemplo n.º 5
0
def test_svd_c_rand():
    for i in xrange(5):
        full = mp.rand() > 0.5
        m = 1 + int(mp.rand() * 10)
        n = 1 + int(mp.rand() * 10)
        A = (2 * mp.randmatrix(m, n) - 1) + 1j * (2 * mp.randmatrix(m, n) - 1)
        if mp.rand() > 0.5:
            A *= 10
            for x in xrange(m):
                for y in xrange(n):
                    A[x,y]=int(mp.re(A[x,y])) + 1j * int(mp.im(A[x,y]))

        run_svd_c(A, full_matrices=full, verbose=False)
Ejemplo n.º 6
0
def test_gauss_quadrature_dynamic(verbose = False):
  n = 5

  A = mp.randmatrix(2 * n, 1)

  def F(x):
      r = 0
      for i in xrange(len(A) - 1, -1, -1):
          r = r * x + A[i]
      return r

  def run(qtype, FW, R, alpha = 0, beta = 0):
    X, W = mp.gauss_quadrature(n, qtype, alpha = alpha, beta = beta)

    a = 0
    for i in xrange(len(X)):
      a += W[i] * F(X[i])

    b = mp.quad(lambda x: FW(x) * F(x), R)

    c = mp.fabs(a - b)

    if verbose:
        print(qtype, c, a, b)

    assert c < 1e-5

  run("legendre", lambda x: 1, [-1, 1])
  run("legendre01", lambda x: 1, [0, 1])
  run("hermite", lambda x: mp.exp(-x*x), [-mp.inf, mp.inf])
  run("laguerre", lambda x: mp.exp(-x), [0, mp.inf])
  run("glaguerre", lambda x: mp.sqrt(x)*mp.exp(-x), [0, mp.inf], alpha = 1 / mp.mpf(2))
  run("chebyshev1", lambda x: 1/mp.sqrt(1-x*x), [-1, 1])
  run("chebyshev2", lambda x: mp.sqrt(1-x*x), [-1, 1])
  run("jacobi", lambda x: (1-x)**(1/mp.mpf(3)) * (1+x)**(1/mp.mpf(5)), [-1, 1], alpha = 1 / mp.mpf(3), beta = 1 / mp.mpf(5) )
Ejemplo n.º 7
0
def test_eigsy_randmatrix():
    N = 5

    for a in xrange(10):
        A = 2 * mp.randmatrix(N, N) - 1

        for i in xrange(0, N):
            for j in xrange(i + 1, N):
                A[j, i] = A[i, j]

        run_eigsy(A)
Ejemplo n.º 8
0
def test_eigsy_randmatrix():
    N = 5

    for a in xrange(10):
        A = 2 * mp.randmatrix(N, N) - 1

        for i in xrange(0, N):
            for j in xrange(i + 1, N):
                A[j,i] = A[i,j]

        run_eigsy(A)
Ejemplo n.º 9
0
def test_svd_r_rand():
    for i in xrange(5):
        full = mp.rand() > 0.5
        m = 1 + int(mp.rand() * 10)
        n = 1 + int(mp.rand() * 10)
        A = 2 * mp.randmatrix(m, n) - 1
        if mp.rand() > 0.5:
            A *= 10
            for x in xrange(m):
                for y in xrange(n):
                    A[x, y] = int(A[x, y])

        run_svd_r(A, full_matrices=full, verbose=False)
Ejemplo n.º 10
0
def test_svd_r_rand():
    for i in xrange(5):
        full = mp.rand() > 0.5
        m = 1 + int(mp.rand() * 10)
        n = 1 + int(mp.rand() * 10)
        A = 2 * mp.randmatrix(m, n) - 1
        if mp.rand() > 0.5:
            A *= 10
            for x in xrange(m):
                for y in xrange(n):
                    A[x,y]=int(A[x,y])

        run_svd_r(A, full_matrices = full, verbose = False)
Ejemplo n.º 11
0
def test_gauss_quadrature_dynamic(verbose=False):
    n = 5

    A = mp.randmatrix(2 * n, 1)

    def F(x):
        r = 0
        for i in xrange(len(A) - 1, -1, -1):
            r = r * x + A[i]
        return r

    def run(qtype, FW, R, alpha=0, beta=0):
        X, W = mp.gauss_quadrature(n, qtype, alpha=alpha, beta=beta)

        a = 0
        for i in xrange(len(X)):
            a += W[i] * F(X[i])

        b = mp.quad(lambda x: FW(x) * F(x), R)

        c = mp.fabs(a - b)

        if verbose:
            print(qtype, c, a, b)

        assert c < 1e-5

    run("legendre", lambda x: 1, [-1, 1])
    run("legendre01", lambda x: 1, [0, 1])
    run("hermite", lambda x: mp.exp(-x * x), [-mp.inf, mp.inf])
    run("laguerre", lambda x: mp.exp(-x), [0, mp.inf])
    run("glaguerre",
        lambda x: mp.sqrt(x) * mp.exp(-x), [0, mp.inf],
        alpha=1 / mp.mpf(2))
    run("chebyshev1", lambda x: 1 / mp.sqrt(1 - x * x), [-1, 1])
    run("chebyshev2", lambda x: mp.sqrt(1 - x * x), [-1, 1])
    run("jacobi",
        lambda x: (1 - x)**(1 / mp.mpf(3)) * (1 + x)**(1 / mp.mpf(5)), [-1, 1],
        alpha=1 / mp.mpf(3),
        beta=1 / mp.mpf(5))