"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "mpmath/tests/test_matrices.py" between
mpmath-1.0.0.tar.gz and mpmath-1.1.0.tar.gz

About: mpmath is a Python library for arbitrary-precision floating-point arithmetic.

test_matrices.py  (mpmath-1.0.0):test_matrices.py  (mpmath-1.1.0)
import pytest
from mpmath import * from mpmath import *
def test_matrix_basic(): def test_matrix_basic():
A1 = matrix(3) A1 = matrix(3)
for i in range(3): for i in range(3):
A1[i,i] = 1 A1[i,i] = 1
assert A1 == eye(3) assert A1 == eye(3)
assert A1 == matrix(A1) assert A1 == matrix(A1)
A2 = matrix(3, 2) A2 = matrix(3, 2)
assert not A2._matrix__data assert not A2._matrix__data
A3 = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) A3 = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
assert list(A3) == list(range(1, 10)) assert list(A3) == list(range(1, 10))
A3[1,1] = 0 A3[1,1] = 0
assert not (1, 1) in A3._matrix__data assert not (1, 1) in A3._matrix__data
A4 = matrix([[1, 2, 3], [4, 5, 6]]) A4 = matrix([[1, 2, 3], [4, 5, 6]])
A5 = matrix([[6, -1], [3, 2], [0, -3]]) A5 = matrix([[6, -1], [3, 2], [0, -3]])
assert A4 * A5 == matrix([[12, -6], [39, -12]]) assert A4 * A5 == matrix([[12, -6], [39, -12]])
assert A1 * A3 == A3 * A1 == A3 assert A1 * A3 == A3 * A1 == A3
try: pytest.raises(ValueError, lambda: A2*A2)
A2 * A2
assert False
except ValueError:
pass
l = [[10, 20, 30], [40, 0, 60], [70, 80, 90]] l = [[10, 20, 30], [40, 0, 60], [70, 80, 90]]
A6 = matrix(l) A6 = matrix(l)
assert A6.tolist() == l assert A6.tolist() == l
assert A6 == eval(repr(A6)) assert A6 == eval(repr(A6))
A6 = matrix(A6, force_type=float) A6 = matrix(A6, force_type=float)
assert A6 == eval(repr(A6)) assert A6 == eval(repr(A6))
assert A6*1j == eval(repr(A6*1j)) assert A6*1j == eval(repr(A6*1j))
assert A3 * 10 == 10 * A3 == A6 assert A3 * 10 == 10 * A3 == A6
assert A2.rows == 3 assert A2.rows == 3
assert A2.cols == 2 assert A2.cols == 2
A3.rows = 2 A3.rows = 2
A3.cols = 2 A3.cols = 2
assert len(A3._matrix__data) == 3 assert len(A3._matrix__data) == 3
assert A4 + A4 == 2*A4 assert A4 + A4 == 2*A4
try: pytest.raises(ValueError, lambda: A4 + A2)
A4 + A2
except ValueError:
pass
assert sum(A1 - A1) == 0 assert sum(A1 - A1) == 0
A7 = matrix([[1, 2], [3, 4], [5, 6], [7, 8]]) A7 = matrix([[1, 2], [3, 4], [5, 6], [7, 8]])
x = matrix([10, -10]) x = matrix([10, -10])
assert A7*x == matrix([-10, -10, -10, -10]) assert A7*x == matrix([-10, -10, -10, -10])
A8 = ones(5) A8 = ones(5)
assert sum((A8 + 1) - (2 - zeros(5))) == 0 assert sum((A8 + 1) - (2 - zeros(5))) == 0
assert (1 + ones(4)) / 2 - 1 == zeros(4) assert (1 + ones(4)) / 2 - 1 == zeros(4)
assert eye(3)**10 == eye(3) assert eye(3)**10 == eye(3)
try: pytest.raises(ValueError, lambda: A7**2)
A7**2
assert False
except ValueError:
pass
A9 = randmatrix(3) A9 = randmatrix(3)
A10 = matrix(A9) A10 = matrix(A9)
A9[0,0] = -100 A9[0,0] = -100
assert A9 != A10 assert A9 != A10
assert nstr(A9) assert nstr(A9)
def test_matrix_slices(): def test_matrix_slices():
A = matrix([ [1, 2, 3], A = matrix([ [1, 2, 3],
[4, 5 ,6], [4, 5 ,6],
[7, 8 ,9]]) [7, 8 ,9]])
V = matrix([1,2,3,4,5]) V = matrix([1,2,3,4,5])
# Get slice # Get slice
assert A[:,:] == A assert A[:,:] == A
assert A[:,1] == matrix([[2],[5],[8]]) assert A[:,1] == matrix([[2],[5],[8]])
assert A[2,:] == matrix([[7, 8 ,9]]) assert A[2,:] == matrix([[7, 8 ,9]])
assert A[1:3,1:3] == matrix([[5,6],[8,9]]) assert A[1:3,1:3] == matrix([[5,6],[8,9]])
assert V[2:4] == matrix([3,4]) assert V[2:4] == matrix([3,4])
try: pytest.raises(IndexError, lambda: A[:,1:6])
A6 = A[:,1:6]
assert False
except IndexError:
pass
# Assign slice with matrix # Assign slice with matrix
A1 = matrix(3) A1 = matrix(3)
A1[:,:] = A A1[:,:] = A
assert A1[:,:] == matrix([[1, 2, 3], assert A1[:,:] == matrix([[1, 2, 3],
[4, 5 ,6], [4, 5 ,6],
[7, 8 ,9]]) [7, 8 ,9]])
A1[0,:] = matrix([[10, 11, 12]]) A1[0,:] = matrix([[10, 11, 12]])
assert A1 == matrix([ [10, 11, 12], assert A1 == matrix([ [10, 11, 12],
[4, 5 ,6], [4, 5 ,6],
skipping to change at line 99 skipping to change at line 85
A1[:,2] = matrix([[13], [14], [15]]) A1[:,2] = matrix([[13], [14], [15]])
assert A1 == matrix([ [10, 11, 13], assert A1 == matrix([ [10, 11, 13],
[4, 5 ,14], [4, 5 ,14],
[7, 8 ,15]]) [7, 8 ,15]])
A1[:2,:2] = matrix([[16, 17], [18 , 19]]) A1[:2,:2] = matrix([[16, 17], [18 , 19]])
assert A1 == matrix([ [16, 17, 13], assert A1 == matrix([ [16, 17, 13],
[18, 19 ,14], [18, 19 ,14],
[7, 8 ,15]]) [7, 8 ,15]])
V[1:3] = 10 V[1:3] = 10
assert V == matrix([1,10,10,4,5]) assert V == matrix([1,10,10,4,5])
try: with pytest.raises(ValueError):
A1[2,:] = A[:,1] A1[2,:] = A[:,1]
assert False
except ValueError:
pass
try: with pytest.raises(IndexError):
A1[2,1:20] = A[:,:] A1[2,1:20] = A[:,:]
assert False
except IndexError:
pass
# Assign slice with scalar # Assign slice with scalar
A1[:,2] = 10 A1[:,2] = 10
assert A1 == matrix([ [16, 17, 10], assert A1 == matrix([ [16, 17, 10],
[18, 19 ,10], [18, 19 ,10],
[7, 8 ,10]]) [7, 8 ,10]])
A1[:,:] = 40 A1[:,:] = 40
for x in A1: for x in A1:
assert x == 40 assert x == 40
skipping to change at line 198 skipping to change at line 178
assert A == B assert A == B
B[0,0] = 0 B[0,0] = 0
assert A != B assert A != B
def test_matrix_numpy(): def test_matrix_numpy():
try: try:
import numpy import numpy
except ImportError: except ImportError:
return return
l = [[1, 2], [3, 4], [5, 6]] l = [[1, 2], [3, 4], [5, 6]]
a = numpy.matrix(l) a = numpy.array(l)
assert matrix(l) == matrix(a) assert matrix(l) == matrix(a)
 End of changes. 10 change blocks. 
28 lines changed or deleted 8 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)