def test_matrix_numpy(): try: import numpy except ImportError: return l = [[1, 2], [3, 4], [5, 6]] a = numpy.matrix(l) assert matrix(l) == matrix(a)
def test_matrix_transform(): A = matrix([[1, 2], [3, 4], [5, 6]]) assert A.T == A.transpose() == matrix([[1, 3, 5], [2, 4, 6]]) swap_row(A, 1, 2) assert A == matrix([[1, 2], [5, 6], [3, 4]]) l = [1, 2] swap_row(l, 0, 1) assert l == [2, 1] assert extend(eye(3), [1,2,3]) == matrix([[1,0,0,1],[0,1,0,2],[0,0,1,3]])
def test_matrix_transform(): A = matrix([[1, 2], [3, 4], [5, 6]]) assert A.T == A.transpose() == matrix([[1, 3, 5], [2, 4, 6]]) swap_row(A, 1, 2) assert A == matrix([[1, 2], [5, 6], [3, 4]]) l = [1, 2] swap_row(l, 0, 1) assert l == [2, 1] assert extend(eye(3), [1, 2, 3]) == matrix([[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3]])
def test_vector(): x = matrix([0, 1, 2, 3, 4]) assert x == matrix([[0], [1], [2], [3], [4]]) assert x[3] == 3 assert len(x._matrix__data) == 4 assert list(x) == range(5) x[0] = -10 x[4] = 0 assert x[0] == -10 assert len(x) == len(x.T) == 5 assert x.T*x == matrix([[114]])
def test_vector(): x = matrix([0, 1, 2, 3, 4]) assert x == matrix([[0], [1], [2], [3], [4]]) assert x[3] == 3 assert len(x._matrix__data) == 4 assert list(x) == range(5) x[0] = -10 x[4] = 0 assert x[0] == -10 assert len(x) == len(x.T) == 5 assert x.T * x == matrix([[114]])
def test_interval_matrix(): a = matrix([['0.1','0.3','1.0'],['7.1','5.5','4.8'],['3.2','4.4','5.6']], force_type=mpi) b = matrix(['4','0.6','0.5'], force_type=mpi) c = lu_solve(a, b) assert c[0].delta < 1e-13 assert c[1].delta < 1e-13 assert c[2].delta < 1e-13 assert 5.25823271130625686059275 in c[0] assert -13.155049396267837541163 in c[1] assert 7.42069154774972557628979 in c[2]
def test_householder(): mp.dps = 15 A, b = A8, b8 H, p, x, r = householder(extend(A, b)) assert H == matrix([[mpf('3.0'), mpf('-2.0'), mpf('-1.0'), 0], [ -1.0, mpf('3.333333333333333'), mpf('-2.9999999999999991'), mpf('2.0') ], [ -1.0, mpf('-0.66666666666666674'), mpf('2.8142135623730948'), mpf('-2.8284271247461898') ], [ 1.0, mpf('-1.3333333333333333'), mpf('-0.20000000000000018'), mpf('4.2426406871192857') ]]) assert p == [-2, -2, mpf('-1.4142135623730949')] assert round(norm(r, 2), 10) == 4.2426406870999998 y = [ 102.102, 58.344, 36.463, 24.310, 17.017, 12.376, 9.282, 7.140, 5.610, 4.488, 3.6465, 3.003 ] def coeff(n): # similiar to Hilbert matrix A = [] for i in xrange(1, 13): A.append([1. / (i + j - 1) for j in xrange(1, n + 1)]) return matrix(A) residuals = [] refres = [] for n in xrange(2, 7): A = coeff(n) H, p, x, r = householder(extend(A, y)) x = matrix(x) y = matrix(y) residuals.append(norm(r, 2)) refres.append(norm(residual(A, x, y), 2)) assert [round(res, 10) for res in residuals] == [ 15.1733888877, 0.82378073210000002, 0.302645887, 0.0260109244, 0.00058653999999999998 ] assert norm(matrix(residuals) - matrix(refres), inf) < 1.e-13
def test_interval_matrix(): a = matrix( [['0.1', '0.3', '1.0'], ['7.1', '5.5', '4.8'], ['3.2', '4.4', '5.6']], force_type=mpi) b = matrix(['4', '0.6', '0.5'], force_type=mpi) c = lu_solve(a, b) assert c[0].delta < 1e-13 assert c[1].delta < 1e-13 assert c[2].delta < 1e-13 assert 5.25823271130625686059275 in c[0] assert -13.155049396267837541163 in c[1] assert 7.42069154774972557628979 in c[2]
def test_matrix_creation(): assert diag([1, 2, 3]) == matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]]) A1 = ones(2, 3) assert A1.rows == 2 and A1.cols == 3 for a in A1: assert a == 1 A2 = zeros(3, 2) assert A2.rows == 3 and A2.cols == 2 for a in A2: assert a == 0 assert randmatrix(10) != randmatrix(10) one = mpf(1) assert hilbert(3) == matrix([[one, one / 2, one / 3], [one / 2, one / 3, one / 4], [one / 3, one / 4, one / 5]])
def test_matrix_creation(): assert diag([1, 2, 3]) == matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]]) A1 = ones(2, 3) assert A1.rows == 2 and A1.cols == 3 for a in A1: assert a == 1 A2 = zeros(3, 2) assert A2.rows == 3 and A2.cols == 2 for a in A2: assert a == 0 assert randmatrix(10) != randmatrix(10)
def test_norms(): # matrix norms A = matrix([[1, -2], [-3, -1], [2, 1]]) assert mnorm_1(A) == 6 assert mnorm_oo(A) == 4 assert mnorm_F(A) == sqrt(20) # vector norms x = [1, -2, 7, -12] assert norm_p(x, 1) == 22 assert round(norm_p(x, 2), 10) == 14.0712472795 assert round(norm_p(x, 10), 10) == 12.0054633727 assert norm_p(x, inf) == 12
def test_householder(): mp.dps = 15 A, b = A8, b8 H, p, x, r = householder(extend(A, b)) assert H == matrix( [[mpf('3.0'), mpf('-2.0'), mpf('-1.0'), 0], [-1.0,mpf('3.333333333333333'),mpf('-2.9999999999999991'),mpf('2.0')], [-1.0, mpf('-0.66666666666666674'),mpf('2.8142135623730948'), mpf('-2.8284271247461898')], [1.0, mpf('-1.3333333333333333'),mpf('-0.20000000000000018'), mpf('4.2426406871192857')]]) assert p == [-2, -2, mpf('-1.4142135623730949')] assert round(norm(r, 2), 10) == 4.2426406870999998 y = [102.102, 58.344, 36.463, 24.310, 17.017, 12.376, 9.282, 7.140, 5.610, 4.488, 3.6465, 3.003] def coeff(n): # similiar to Hilbert matrix A = [] for i in xrange(1, 13): A.append([1. / (i + j - 1) for j in xrange(1, n + 1)]) return matrix(A) residuals = [] refres = [] for n in xrange(2, 7): A = coeff(n) H, p, x, r = householder(extend(A, y)) x = matrix(x) y = matrix(y) residuals.append(norm(r, 2)) refres.append(norm(residual(A, x, y), 2)) assert [round(res, 10) for res in residuals] == [15.1733888877, 0.82378073210000002, 0.302645887, 0.0260109244, 0.00058653999999999998] assert norm(matrix(residuals) - matrix(refres), inf) < 1.e-13
# TODO: don't use round from __future__ import division from sympy.mpmath.matrices import matrix, norm_p, mnorm_1, mnorm_oo, mnorm_F, \ randmatrix, eye, zeros from sympy.mpmath.linalg import * # TODO: absolute imports from sympy.mpmath.mptypes import * A1 = matrix([[3, 1, 6], [2, 1, 3], [1, 1, 1]]) b1 = [2, 7, 4] A2 = matrix([[ 2, -1, -1, 2], [ 6, -2, 3, -1], [-4, 2, 3, -2], [ 2, 0, 4, -3]]) b2 = [3, -3, -2, -1] A3 = matrix([[ 1, 0, -1, -1, 0], [ 0, 1, 1, 0, -1], [ 4, -5, 2, 0, 0], [ 0, 0, -2, 9,-12], [ 0, 5, 0, 0, 12]]) b3 = [0, 0, 0, 0, 50] A4 = matrix([[10.235, -4.56, 0., -0.035, 5.67], [-2.463, 1.27, 3.97, -8.63, 1.08], [-6.58, 0.86, -0.257, 9.32, -43.6 ], [ 9.83, 7.39, -17.25, 0.036, 24.86],
def test_matrix_power(): A = matrix([[1, 2], [3, 4]]) assert A**2 == A*A assert A**3 == A*A*A assert A**-1 == inverse(A) assert A**-2 == inverse(A*A)
def test_matrix_basic(): A1 = matrix(3) for i in xrange(3): A1[i, i] = 1 assert A1 == eye(3) assert A1 == matrix(A1) A2 = matrix(3, 2) assert not A2._matrix__data A3 = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) assert list(A3) == range(1, 10) A3[1, 1] = 0 assert not (1, 1) in A3._matrix__data A4 = matrix([[1, 2, 3], [4, 5, 6]]) A5 = matrix([[6, -1], [3, 2], [0, -3]]) assert A4 * A5 == matrix([[12, -6], [39, -12]]) assert A1 * A3 == A3 * A1 == A3 try: A2 * A2 assert False except ValueError: pass l = [[10, 20, 30], [40, 0, 60], [70, 80, 90]] A6 = matrix(l) assert A6.tolist() == l assert A6 == eval(repr(A6)) A6 = matrix(A6, force_type=float) assert A6 == eval(repr(A6)) assert A3 * 10 == 10 * A3 == A6 assert A2.rows == 3 assert A2.cols == 2 A3.rows = 2 A3.cols = 2 assert len(A3._matrix__data) == 3 assert A4 + A4 == 2 * A4 try: A4 + A2 except ValueError: pass assert sum(A1 - A1) == 0 A7 = matrix([[1, 2], [3, 4], [5, 6], [7, 8]]) x = matrix([10, -10]) assert A7 * x == matrix([-10, -10, -10, -10]) A8 = ones(5) assert sum((A8 + 1) - (2 - zeros(5))) == 0 assert (1 + ones(4)) / 2 - 1 == zeros(4) assert eye(3)**10 == eye(3) try: A7**2 assert False except ValueError: pass A9 = randmatrix(3) A10 = matrix(A9) A9[0, 0] = -100 assert A9 != A10 A11 = matrix(randmatrix(2, 3), force_type=mpi) for a in A11: assert isinstance(a, mpi)
def test_matrix_numpy(): l = [[1, 2], [3, 4], [5, 6]] a = numpy.matrix(l) assert matrix(l) == matrix(a)
def test_matrix_basic(): A1 = matrix(3) for i in xrange(3): A1[i,i] = 1 assert A1 == eye(3) assert A1 == matrix(A1) A2 = matrix(3, 2) assert not A2._matrix__data A3 = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) assert list(A3) == range(1, 10) A3[1,1] = 0 assert not (1, 1) in A3._matrix__data A4 = matrix([[1, 2, 3], [4, 5, 6]]) A5 = matrix([[6, -1], [3, 2], [0, -3]]) assert A4 * A5 == matrix([[12, -6], [39, -12]]) assert A1 * A3 == A3 * A1 == A3 try: A2 * A2 assert False except ValueError: pass l = [[10, 20, 30], [40, 0, 60], [70, 80, 90]] A6 = matrix(l) assert A6.tolist() == l assert A6 == eval(repr(A6)) A6 = matrix(A6, force_type=float) assert A6 == eval(repr(A6)) assert A6*1j == eval(repr(A6*1j)) assert A3 * 10 == 10 * A3 == A6 assert A2.rows == 3 assert A2.cols == 2 A3.rows = 2 A3.cols = 2 assert len(A3._matrix__data) == 3 assert A4 + A4 == 2*A4 try: A4 + A2 except ValueError: pass assert sum(A1 - A1) == 0 A7 = matrix([[1, 2], [3, 4], [5, 6], [7, 8]]) x = matrix([10, -10]) assert A7*x == matrix([-10, -10, -10, -10]) A8 = ones(5) assert sum((A8 + 1) - (2 - zeros(5))) == 0 assert (1 + ones(4)) / 2 - 1 == zeros(4) assert eye(3)**10 == eye(3) try: A7**2 assert False except ValueError: pass A9 = randmatrix(3) A10 = matrix(A9) A9[0,0] = -100 assert A9 != A10 A11 = matrix(randmatrix(2, 3), force_type=mpi) for a in A11: assert isinstance(a, mpi) assert nstr(A9)
def test_cond(): mp.dps = 15 A = matrix([[1.2969, 0.8648], [0.2161, 0.1441]]) assert cond(A, lambda x: mnorm(x,1)) == mpf('327065209.73817754') assert cond(A, lambda x: mnorm(x,inf)) == mpf('327065209.73817754') assert cond(A, lambda x: mnorm(x,'F')) == mpf('249729266.80008656')
def test_matrix_power(): A = matrix([[1, 2], [3, 4]]) assert A**2 == A * A assert A**3 == A * A * A assert A**-1 == inverse(A) assert A**-2 == inverse(A * A)
def coeff(n): # similiar to Hilbert matrix A = [] for i in xrange(1, 13): A.append([1. / (i + j - 1) for j in xrange(1, n + 1)]) return matrix(A)
def test_cond(): mp.dps = 15 A = matrix([[1.2969, 0.8648], [0.2161, 0.1441]]) assert cond(A, lambda x: mnorm(x, 1)) == mpf('327065209.73817754') assert cond(A, lambda x: mnorm(x, inf)) == mpf('327065209.73817754') assert cond(A, lambda x: mnorm(x, 'F')) == mpf('249729266.80008656')
def test_cholesky(): A9.force_type = float assert cholesky(A9) == matrix([[2, 0, 0], [1, 2, 0], [-1, -3 / 2, 3 / 2]]) x = cholesky_solve(A9, b9) assert norm(residual(A9, x, b9), inf) == 0
def test_cond(): A = matrix([[1.2969, 0.8648], [0.2161, 0.1441]]) assert cond(A, mnorm_1) == mpf('327065209.73817754') assert cond(A, mnorm_oo) == mpf('327065209.73817748') assert cond(A, mnorm_F) == mpf('249729266.80008656')
def test_cholesky(): A9.force_type = float assert cholesky(A9) == matrix([[2, 0, 0], [1, 2, 0], [-1, -3/2, 3/2]]) x = cholesky_solve(A9, b9) assert norm_p(residual(A9, x, b9), inf) == 0
# TODO: don't use round from __future__ import division from sympy.mpmath.matrices import matrix, norm, mnorm, randmatrix, eye, zeros, diag from sympy.mpmath.linalg import * # TODO: absolute imports from sympy.mpmath.mptypes import * A1 = matrix([[3, 1, 6], [2, 1, 3], [1, 1, 1]]) b1 = [2, 7, 4] A2 = matrix([[2, -1, -1, 2], [6, -2, 3, -1], [-4, 2, 3, -2], [2, 0, 4, -3]]) b2 = [3, -3, -2, -1] A3 = matrix([[1, 0, -1, -1, 0], [0, 1, 1, 0, -1], [4, -5, 2, 0, 0], [0, 0, -2, 9, -12], [0, 5, 0, 0, 12]]) b3 = [0, 0, 0, 0, 50] A4 = matrix([[10.235, -4.56, 0., -0.035, 5.67], [-2.463, 1.27, 3.97, -8.63, 1.08], [-6.58, 0.86, -0.257, 9.32, -43.6], [9.83, 7.39, -17.25, 0.036, 24.86], [-9.31, 34.9, 78.56, 1.07, 65.8]]) b4 = [8.95, 20.54, 7.42, 5.60, 58.43] A5 = matrix([[1, 2, -4], [-2, -3, 5], [3, 5, -8]]) A6 = matrix([[1.377360, 2.481400, 5.359190], [2.679280, -1.229560, 25.560210], [-1.225280 + 1.e6, 9.910180, -35.049900 - 1.e6]]) b6 = [23.500000, -15.760000, 2.340000]