You can run the file 'tests.py' to see examples of use of this module 'matrix.py'. Testing every doctests in the module 'matrix'... Trying: from matrix import * Expecting nothing ok Trying: from matrix import Matrix as M # shortcut Expecting nothing ok Trying: A = M([[1, 0], [0, 1]]) Expecting nothing ok Trying: A == eye(A.n) Expecting: True ok Trying: B = 2*(A**2) + 4*A + eye(A.n) Expecting nothing ok Trying: B Expecting: [[7, 0], [0, 7]] ok Trying: B == 7 * eye(A.n) Expecting: True ok Trying: A[1,:] = 2; A Expecting: [[1, 0], [2, 2]] ok Trying: A[0, 0] = -5; A Expecting: [[-5, 0], [2, 2]] ok Trying: C = eye(2); C Expecting: [[1, 0], [0, 1]] ok Trying: C + (3 * C) - C Expecting: [[3, 0], [0, 3]] ok Trying: (4 * C) ** 2 Expecting: [[16, 0], [0, 16]] ok Trying: B = Matrix([[1, 4], [2, 5], [3, 6]]) Expecting nothing ok Trying: B.T Expecting: [[1, 2, 3], [4, 5, 6]] ok Trying: B == B.T.T Expecting: True ok Trying: A = Matrix([[-4, 2+2j], [0, 4j]]) Expecting nothing ok Trying: abs(A) # doctest: +ELLIPSIS Expecting: [[4, 2.828427...], [0, 4.0]] ok Trying: B = -eye(2) Expecting nothing ok Trying: B.abs() Expecting: [[1, 0], [0, 1]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A + A Expecting: [[2, 4, 6], [8, 10, 12]] ok Trying: B = ones(A.n, A.m); B Expecting: [[1, 1, 1], [1, 1, 1]] ok Trying: A + B Expecting: [[2, 3, 4], [5, 6, 7]] ok Trying: B + A Expecting: [[2, 3, 4], [5, 6, 7]] ok Trying: B + B + B + B + B + B + B Expecting: [[7, 7, 7], [7, 7, 7]] ok Trying: B + 4 # Coefficient wise! Expecting: [[5, 5, 5], [5, 5, 5]] ok Trying: B + (-2) # Coefficient wise! Expecting: [[-1, -1, -1], [-1, -1, -1]] ok Trying: B + (-1.0) # Coefficient wise! Expecting: [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] ok Trying: 4 in Matrix([[1, 2], [3, 4]]) Expecting: True ok Trying: 4 in Matrix([[1, 2], [1, 2]]) Expecting: False ok Trying: O, I = zeros(7), eye(7) Expecting nothing ok Trying: 3 * I**2 + 2 * I + O ** 0 Expecting: [[6, 0, 0, 0, 0, 0, 0], [0, 6, 0, 0, 0, 0, 0], [0, 0, 6, 0, 0, 0, 0], [0, 0, 0, 6, 0, 0, 0], [0, 0, 0, 0, 6, 0, 0], [0, 0, 0, 0, 0, 6, 0], [0, 0, 0, 0, 0, 0, 6]] ok Trying: 6 in (3 * I**2 + 2 * I + O ** 0) Expecting: True ok Trying: B = Matrix([[1, 4], [2, 5], [3, 6]]) Expecting nothing ok Trying: B == B Expecting: True ok Trying: B + B + B == 3*B == B + 2*B == 2*B + B Expecting: True ok Trying: B - B + B == 1*B == -B + 2*B == 2*B - B == 2*B + (-B) Expecting: True ok Trying: B != B Expecting: False ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: B = eye(A.n); C = B.map(float) Expecting nothing ok Trying: A // C == A * C == A Expecting: True ok Trying: A // B == A * B == A Expecting: True ok Trying: A // 2 # Coefficient wise! Expecting: [[0, 1], [1, 2]] ok Trying: A // 2.0 # Coefficient wise! Expecting: [[0.0, 1.0], [1.0, 2.0]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A[0, 0] Expecting: 1 ok Trying: A[0, :] Expecting: [[1, 2, 3]] ok Trying: A[-1, :] Expecting: [[4, 5, 6]] ok Trying: A[:, 0] Expecting: [[1], [4]] ok Trying: A[1:, 1:] Expecting: [[5, 6]] ok Trying: A[:, ::2] Expecting: [[1, 3], [4, 6]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A.listrows Expecting: [[1, 2, 3], [4, 5, 6]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Expecting nothing ok Trying: list(A) Expecting: [1, 2, 3, 4, 5, 6, 7, 8, 9] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: len(A) Expecting: 6 ok Trying: len(A) == A.n * A.m Expecting: True ok Trying: B = Matrix([[1, 4], [2, 5], [3, 6]]) Expecting nothing ok Trying: B < B Expecting: False ok Trying: B < B + 4 Expecting: True ok Trying: B > B Expecting: False ok Trying: B > B - 12 Expecting: True ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: A % 2 Expecting: [[1, 0], [1, 0]] ok Trying: (A*100) % 31 Expecting: [[7, 14], [21, 28]] ok Trying: (A*100) % 33 == A # Curious property Expecting: True ok Trying: (A*100) % 35 Expecting: [[30, 25], [20, 15]] ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: B = Matrix([[2, 3], [2, 2]]) Expecting nothing ok Trying: A % B Expecting: [[1, 2], [1, 0]] ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: B = eye(A.n); B Expecting: [[1, 0], [0, 1]] ok Trying: A * B == B * A == A Expecting: True ok Trying: A * A Expecting: [[7, 10], [15, 22]] ok Trying: A * (A * A) == (A * A) * A Expecting: True ok Trying: A * 1 == A # Coefficient wise! Expecting: True ok Trying: A * 12.011993 # Coefficient wise! Expecting: [[12.011993, 24.023986], [36.035979, 48.047972]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: -A Expecting: [[-1, -2, -3], [-4, -5, -6]] ok Trying: A - A == A + (-A) Expecting: True ok Trying: -(-A) == A Expecting: True ok Trying: -------A == -A # Crazy syntax! Expecting: True ok Trying: s = '-------' Expecting nothing ok Trying: len(s) % 2 == 1 # We check that we had an od number of minus symbol Expecting: True ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: +A == A Expecting: True ok Trying: +-+-+-+-+++----+-+-+----++++A == A # Crazy syntax, again! Expecting: True ok Trying: s = '+-+-+-+-+++----+-+-+----++++' Expecting nothing ok Trying: s.count('-') % 2 == 0 # We check that we had an even number of minus symbol Expecting: True ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: A ** 1 == A Expecting: True ok Trying: A ** 2 Expecting: [[7, 10], [15, 22]] ok Trying: A * A == A ** 2 Expecting: True ok Trying: B = eye(A.n) Expecting nothing ok Trying: B == B ** 1 == A ** 0 == B ** 0 Expecting: True ok Trying: divmod(2015, 2) Expecting: (1007, 1) ok Trying: 2015 == 1007*2 + 1 Expecting: True ok Trying: A ** 2015 == ((A ** 1007) ** 2 ) * A Expecting: True ok Trying: C = diag([1, 4]) Expecting nothing ok Trying: C ** 100 Expecting: [[1, 0], [0, 1606938044258990275541962092341162602522202993782792835301376]] ok Trying: C ** 100 == diag([1**100, 4**100]) Expecting: True ok Trying: A ** (-1) == A.inv() Expecting: True ok Trying: C = (A ** (-1)); C Expecting: [[-2.0, 1.0], [1.5, -0.5]] ok Trying: C * A == eye(A.n) == A * C Expecting: True ok Trying: C.listrows # Rounding mistakes can happen (but not here) Expecting: [[-2.0, 1.0], [1.5, -0.5]] ok Trying: D = C.round(); D.listrows Expecting: [[-2.0, 1.0], [1.5, -0.5]] ok Trying: D * A == eye(A.n) == A * D # No rounding mistake! Expecting: True ok Trying: (C * A).almosteq(eye(A.n)) Expecting: True ok Trying: (A ** (-5)) == (A ** 5).inv() == (A.inv()) ** 5 Expecting: False ok Trying: (A ** (-5)).round() == ((A ** 5).inv()).round() == ((A.inv()) ** 5).round() # No rounding mistake! Expecting: True ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: 1 + A Expecting: [[2, 3, 4], [5, 6, 7]] ok Trying: B = ones(A.n, A.m) Expecting nothing ok Trying: 4 + B # Coefficient wise! Expecting: [[5, 5, 5], [5, 5, 5]] ok Trying: (-2) + B # Coefficient wise! Expecting: [[-1, -1, -1], [-1, -1, -1]] ok Trying: (-1.0) + B # Coefficient wise! Expecting: [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: Ainv = Matrix([[-2.0, 1.0], [1.5, -0.5]]) Expecting nothing ok Trying: B = eye(A.n) Expecting nothing ok Trying: B == A * Ainv == Ainv * A Expecting: True ok Trying: 1 / B == B == B / 1 Expecting: True ok Trying: C = B.map(float) Expecting nothing ok Trying: 1 / B == B == B / 1 Expecting: True ok Trying: A.inv() == 1 / A # special case! Expecting: True ok Trying: 1 / A # This is like 1 / A Expecting: [[-2.0, 1.0], [1.5, -0.5]] ok Trying: 2 / (2*A) # Warning This is coefficient wise ! # doctest: +ELLIPSIS Expecting: [[1.0, 0.5], [0.333333..., 0.25]] ok Trying: B = Matrix([[1, 4], [2, 5], [3, 6]]) Expecting nothing ok Trying: repr(B) Expecting: '[[1, 4], [2, 5], [3, 6]]' ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: B = eye(A.n) Expecting nothing ok Trying: 1 // B == B == B // 1 Expecting: True ok Trying: C = B.map(float) Expecting nothing ok Trying: 1 // B == B == B // 1 Expecting: True ok Trying: A.inv() == 1 // A # special case! Expecting: True ok Trying: 2 // (2*A) # XXX This is coefficient wise ! Expecting: [[1, 0], [0, 0]] ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: 1 * A == A # Coefficient wise! Expecting: True ok Trying: 12.011993 * A # Coefficient wise! Expecting: [[12.011993, 24.023986], [36.035979, 48.047972]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: 1 - A # Coefficient wise! Expecting: [[0, -1, -2], [-3, -4, -5]] ok Trying: B = ones(A.n, A.m) Expecting nothing ok Trying: (-1) - B # Coefficient wise! Expecting: [[-2, -2, -2], [-2, -2, -2]] ok Trying: ((-1) - B) == -(1 + B) == -(B + B) Expecting: True ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A[0, 0] = 4; A Expecting: [[4, 2, 3], [4, 5, 6]] ok Trying: A[:, 0] Expecting: [[4], [4]] ok Trying: A[-1, :] = 9; A Expecting: [[4, 2, 3], [9, 9, 9]] ok Trying: A[1, 1] = 3; A Expecting: [[4, 2, 3], [9, 3, 9]] ok Trying: A[0, :] = [3, 2, 1]; A Expecting: [[3, 2, 1], [9, 3, 9]] ok Trying: A[1:, 1:] = -1; A Expecting: [[3, 2, 1], [9, -1, -1]] ok Trying: A[1:, 1:] *= -8; A Expecting: [[3, 2, 1], [9, 8, 8]] ok Trying: B = Matrix([[1, 4], [2, 5], [3, 6]]) Expecting nothing ok Trying: str(B) Expecting: '[[1, 4], [2, 5], [3, 6]]' ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: B = ones(A.n, A.m) Expecting nothing ok Trying: A - B Expecting: [[0, 1, 2], [3, 4, 5]] ok Trying: B - A Expecting: [[0, -1, -2], [-3, -4, -5]] ok Trying: A - 1 # Coefficient wise! Expecting: [[0, 1, 2], [3, 4, 5]] ok Trying: B - 2 # Coefficient wise! Expecting: [[-1, -1, -1], [-1, -1, -1]] ok Trying: (A - 3.14).round() # Coefficient wise! Expecting: [[-2.14, -1.14, -0.14], [0.86, 1.86, 2.86]] ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: B = eye(A.n) Expecting nothing ok Trying: B.almosteq(A / A) Expecting: True ok Trying: C = B.map(float) Expecting nothing ok Trying: A / C == A * C == A Expecting: True ok Trying: A / B == A * B == A Expecting: True ok Trying: A / 2 # Coefficient wise! Expecting: [[0.5, 1.0], [1.5, 2.0]] ok Trying: A / 2.0 # Coefficient wise! Expecting: [[0.5, 1.0], [1.5, 2.0]] ok Trying: A = Matrix([[2, 0], [3, 4]]) Expecting nothing ok Trying: A.adjugate() Expecting: [[4, -3], [0, 2]] ok Trying: A * A.adjugate() == A.det * eye(A.n) Expecting: False ok Trying: A * A.adjugate().T == A.det * eye(A.n) Expecting: True ok Trying: B = Matrix([[1, 4], [2, 5], [3, 6]]) Expecting nothing ok Trying: C = B.copy(); C[0,0] += 4*1e-6 Expecting nothing ok Trying: B == C Expecting: False ok Trying: B.almosteq(C) Expecting: False ok Trying: B.almosteq(C, epsilon=1e-4) Expecting: True ok Trying: B.almosteq(C, epsilon=1e-5) Expecting: True ok Trying: B.almosteq(C, epsilon=1e-6) Expecting: False ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: A.cofactor(0, 0) Expecting: 4 ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Expecting nothing ok Trying: A.cofactor(0, 0) # (-1)**0 * | 5 6 8 9 | = 5 * 9 - 6 * 8 = -3 Expecting: -3.000000000000007 ok Trying: A.cofactor(1, 0) # (-1)**1 * | 2 3 8 9 | = -(2 * 9 - 3 * 8) = 6 Expecting: 6 ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A.col(0) Expecting: [[1], [4]] ok Trying: A.col(2) Expecting: [[3], [6]] ok Trying: c = A.col(1); c *= 6 Expecting nothing ok Trying: A # it has not been modified! Expecting: [[1, 2, 3], [4, 5, 6]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A.m Expecting: 3 ok Trying: A.cols == A.m Expecting: True ok Trying: A = Matrix([[-1j, -2j], [-2j, -1j]]) Expecting nothing ok Trying: A.conjugate() Expecting: [[1j, 2j], [2j, 1j]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: B = A.copy() Expecting nothing ok Trying: A[0, 0] = -10; A Expecting: [[-10, 2, 3], [4, 5, 6]] ok Trying: B # It has not been modified! Expecting: [[1, 2, 3], [4, 5, 6]] ok Trying: Matrix([[1, 2], [3, 4]]).count(2) Expecting: 1 ok Trying: Matrix([[1, 2], [1, 2]]).count(2) Expecting: 2 ok Trying: zeros(7).count(2) Expecting: 0 ok Trying: zeros(7).count(0) Expecting: 49 ok Trying: eye(19).count(1) Expecting: 19 ok Trying: eye(19).count(0) Expecting: 342 ok Trying: Matrix([[1, 2], [3, 4]]).det Expecting: -2 ok Trying: Matrix([[1, 2], [1, 2]]).det Expecting: 0 ok Trying: zeros(7).det Expecting: 0 ok Trying: eye(19).det Expecting: 1 ok Trying: A = Matrix([[1, 1], [1, -1]]) Expecting nothing ok Trying: v = [2, 3] Expecting nothing ok Trying: A.dot(v) Expecting: [[5], [-1]] ok Trying: v = Matrix([[2], [-3]]) Expecting nothing ok Trying: A.dot(v) Expecting: [[-1], [5]] ok Trying: A.dot(v.T) # v.T is not a column vector! Expecting: Traceback (most recent call last): ... ValueError: A.dot(v): the vector v = [[2, -3]] is not a vector: v.m = 2 != 1. ok Trying: v = Matrix([[2], [-3], [7]]) Expecting nothing ok Trying: A.dot(v) Expecting: Traceback (most recent call last): ... ValueError: A.dot(v): the size of the vector v = [[2], [-3], [7]] should be compatible with the size of the matrix self = [[1, 1], [1, -1]]. Here self.m = 2 and v.n = 3, are different. ok Trying: v = [1, 2, 3, 4, 5] Expecting nothing ok Trying: A.dot(v) Expecting: Traceback (most recent call last): ... ValueError: A.dot(v): the size of the vector v = [[1], [2], [3], [4], [5]] should be compatible with the size of the matrix self = [[1, 1], [1, -1]]. Here self.m = 2 and v.n = 5, are different. ok Trying: import math Expecting nothing ok Trying: e = math.e Expecting nothing ok Trying: I = eye(10); I[0, :] Expecting: [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]] ok Trying: I * e == I.exp() == diag([e] * I.n) # Rounding mistakes! Expecting: False ok Trying: (I * e).round() == I.exp().round() == diag([e] * I.n).round() # No more rounding mistakes! Expecting: True ok Trying: C = diag([1, 4]) Expecting nothing ok Trying: C.exp() == diag([e ** 1, e ** 4]) == diag([math.exp(1), math.exp(4)]) # Rounding mistakes! Expecting: False ok Trying: C.exp().almosteq(diag([e ** 1, e ** 4])) # No more rounding mistakes! Expecting: True ok Trying: diag([e ** 1, e ** 4]).almosteq(diag([math.exp(1), math.exp(4)])) Expecting: True ok Trying: Matrix([[1, 2], [3, 4]]).gauss() Expecting: [[1, 2], [0, -2]] ok Trying: Matrix([[1, 2], [1, 2]]).gauss() Expecting: [[1, 2], [0, 0]] ok Trying: Matrix([[1, 2], [-1, -0.5]]).gauss() Expecting: [[1, 2], [0, 1.5]] ok Trying: Matrix([[1, 2], [3, 4]]).gauss(maxpivot=True) Expecting: [[2, 1], [0, 1]] ok Trying: Matrix([[1, 2], [1, 2]]).gauss(maxpivot=True) Expecting: [[2, 1], [0, 0]] ok Trying: Matrix([[1, 2], [3, 4]]).gauss(det=True) Expecting: ([[1, 2], [0, -2]], -2) ok Trying: Matrix([[1, 2], [1, 2]]).gauss(det=True) Expecting: ([[1, 2], [0, 0]], 0) ok Trying: A = Matrix([[-1j, -2j], [-2j, -1j]]) Expecting nothing ok Trying: A.imag Expecting: [[-1.0, -2.0], [-2.0, -1.0]] ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: A.inv() Expecting: [[-2.0, 1.0], [1.5, -0.5]] ok Trying: A * A.inv() == A.inv() * A == eye(A.n) # Rounding mistake can happen (but not here) Expecting: True ok Trying: Ai = A.inv().round() # No more rounding mistake! Expecting nothing ok Trying: A * Ai == Ai * A == eye(A.n) Expecting: True ok Trying: A.det Expecting: -2 ok Trying: O = Matrix([[1, 2], [0, 0]]) # O and not 0 Expecting nothing ok Trying: O.is_singular Expecting: True ok Trying: O.inv() # O is singular! Expecting: Traceback (most recent call last): ... ValueError: A.inv() on a singular matrix (ie. non inversible). ok Trying: O.det Expecting: 0 ok Trying: A = Matrix([[0, 1], [-1, 0]]) Expecting nothing ok Trying: A.is_anti_symetric Expecting: True ok Trying: eye(30).is_anti_symetric Expecting: False ok Trying: eye(40).is_diagonal Expecting: True ok Trying: A = Matrix([[0, 1], [-1, 0]]) Expecting nothing ok Trying: A.is_diagonal Expecting: False ok Trying: A = diag(range(30)) Expecting nothing ok Trying: A.is_diagonal Expecting: True ok Trying: A = Matrix([[1, 2j], [-2j, 1]]) Expecting nothing ok Trying: A.is_hermitian Expecting: True ok Trying: eye(30).is_hermitian Expecting: True ok Trying: (1j * ones(3)).is_hermitian Expecting: False ok Trying: A = Matrix([[8, 1], [0, 7]]) Expecting nothing ok Trying: A.is_lower Expecting: False ok Trying: A.T.is_lower Expecting: True ok Trying: A = Matrix([[2, 0], [3, 4]]) Expecting nothing ok Trying: A.is_singular Expecting: False ok Trying: zeros(3).is_singular Expecting: True ok Trying: (0 * A).is_singular Expecting: True ok Trying: Matrix([[2, 0], [4, 0]]).is_singular Expecting: True ok Trying: A = Matrix([[-4, 2+2j], [0, 4j]]) Expecting nothing ok Trying: A.is_square Expecting: True ok Trying: v = Matrix([[-4], [0]]) Expecting nothing ok Trying: v.is_square Expecting: False ok Trying: A = Matrix([[-4, 2+2j], [0, 4j]]) Expecting nothing ok Trying: A.is_symetric Expecting: False ok Trying: eye(30).is_symetric Expecting: True ok Trying: A = Matrix([[2, 0], [3, 4]]) Expecting nothing ok Trying: A.is_upper Expecting: False ok Trying: A.T.is_upper Expecting: True ok Trying: A = Matrix([[2, 0], [3, 4]]) Expecting nothing ok Trying: A.is_zero Expecting: False ok Trying: zeros(30).is_zero Expecting: True ok Trying: (0 * A).is_zero Expecting: True ok Trying: O, I = zeros(2), eye(2) Expecting nothing ok Trying: I.map(lambda x: x * 4) Expecting: [[4, 0], [0, 4]] ok Trying: O.map(lambda x: x + 6) Expecting: [[6, 6], [6, 6]] ok Trying: A = Matrix([[-1j, -2j], [-2j, -1j]]) Expecting nothing ok Trying: A.map(lambda z: abs(z)) Expecting: [[1.0, 2.0], [2.0, 1.0]] ok Trying: A.map(lambda z: int(abs(z))) Expecting: [[1, 2], [2, 1]] ok Trying: A.map(lambda z: z + 1j) Expecting: [[0j, -1j], [-1j, 0j]] ok Trying: A.map(lambda z: '"%s"' % str(z)) Expecting: [["-1j", "-2j"], ["-2j", "-1j"]] ok Trying: A.map(lambda z: "Look: %s" % str(z)) Expecting: [[Look: -1j, Look: -2j], [Look: -2j, Look: -1j]] ok Trying: def f(x, n, offset=0): return (x ** n) + offset Expecting nothing ok Trying: A = Matrix([[1, 2], [2, 1]]) Expecting nothing ok Trying: A.map(f, 2) Expecting: [[1, 4], [4, 1]] ok Trying: A.map(f, 2, offset=4) Expecting: [[5, 8], [8, 5]] ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: A.minor(0, 0) Expecting: 4 ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Expecting nothing ok Trying: A.minor(0, 0) # | 5 6 8 9 | = 5 * 9 - 6 * 8 = -3 Expecting: -3.000000000000007 ok Trying: A.minor(1, 0) # | 2 3 8 9 | = 2 * 9 - 3 * 8 = -6 Expecting: -6 ok Trying: A = Matrix([[1, 2], [3, 4]]) Expecting nothing ok Trying: B = eye(A.n) Expecting nothing ok Trying: A.multiply_elementwise(B) Expecting: [[1, 0], [0, 4]] ok Trying: A.multiply_elementwise(A) # A .^ 2 in Matlab? Expecting: [[1, 4], [9, 16]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Expecting nothing ok Trying: for x in A: print(x) Expecting: 1 2 3 4 5 6 7 8 9 ok Trying: for i, x in enumerate(A): print(i, "th value of A is", x) Expecting: 0 th value of A is 1 1 th value of A is 2 2 th value of A is 3 3 th value of A is 4 4 th value of A is 5 5 th value of A is 6 6 th value of A is 7 7 th value of A is 8 8 th value of A is 9 ok Trying: A = Matrix([[1, 2], [-3, -1]]) Expecting nothing ok Trying: A.norm() # (1)**2 + (2)**2 + (-3)**2 + (-1)**2 Expecting: 3.872983346207417 ok Trying: 15**0.5 Expecting: 3.872983346207417 ok Trying: A.norm('inf') Expecting: 3 ok Trying: A.norm(1) == 7 # (1) + (2) + (3) + (1) Expecting: True ok Trying: A.norm(3) Expecting: 3.332221851645953 ok Trying: A = Matrix([[1, 2], [-3, -1]]) Expecting nothing ok Trying: A.normalized(p='inf') # doctest: +ELLIPSIS Expecting: [[0.333333..., 1.0], [-1.0, -0.5]] ok Trying: eye(5).normalized(p='inf').map(int) # normalize then round to an int Expecting: [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]] ok Trying: B = -eye(5) Expecting nothing ok Trying: (2*B).normalized() # each vector is divided by its norm = 2 Expecting: [[-1.0, 0.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 0.0, 0.0], [0.0, 0.0, -1.0, 0.0, 0.0], [0.0, 0.0, 0.0, -1.0, 0.0], [0.0, 0.0, 0.0, 0.0, -1.0]] ok Trying: B.normalized(p='inf') Expecting: [[-1.0, 0.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 0.0, 0.0], [0.0, 0.0, -1.0, 0.0, 0.0], [0.0, 0.0, 0.0, -1.0, 0.0], [0.0, 0.0, 0.0, 0.0, -1.0]] ok Trying: v = Matrix([[1], [-2], [3]]) Expecting nothing ok Trying: v.normalized() # doctest: +ELLIPSIS Expecting: [[0.267261...], [-0.534522...], [0.801783...]] ok Trying: v.normalized(p=2) # doctest: +ELLIPSIS Expecting: [[0.267261...], [-0.534522...], [0.801783...]] ok Trying: v.normalized() * (14**0.5) Expecting: [[1.0], [-2.0], [3.0]] ok Trying: v.normalized(p=1) # doctest: +ELLIPSIS Expecting: [[0.166666...], [-0.333333...], [0.5]] ok Trying: v.normalized(p=1) * 6 Expecting: [[1.0], [-2.0], [3.0]] ok Trying: 6 * v.normalized(p=1) Expecting: [[1.0], [-2.0], [3.0]] ok Trying: Matrix([[1, 2], [3, 4]]).rank Expecting: 2 ok Trying: Matrix([[1, 2], [1, 2]]).rank Expecting: 1 ok Trying: zeros(7).rank Expecting: 0 ok Trying: eye(19).rank Expecting: 19 ok Trying: A = Matrix([[1j, 2j], [2j, 1j]]) Expecting nothing ok Trying: A.real Expecting: [[0.0, 0.0], [0.0, 0.0]] ok Trying: A = Matrix([[1+6j, 2], [-1+2j, 1+9j]]) Expecting nothing ok Trying: A.real Expecting: [[1.0, 2], [-1.0, 1.0]] ok Trying: A = (1. / 3.) * eye(2) + 4 Expecting nothing ok Trying: A.round(0) Expecting: [[4.0, 4.0], [4.0, 4.0]] ok Trying: A.round(2) Expecting: [[4.33, 4.0], [4.0, 4.33]] ok Trying: A.round(7) Expecting: [[4.3333333, 4.0], [4.0, 4.3333333]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A.row(0) Expecting: [[1, 2, 3]] ok Trying: A.row(1) Expecting: [[4, 5, 6]] ok Trying: r = A.row(0); r *= 3 Expecting nothing ok Trying: A # it has not been modified! Expecting: [[1, 2, 3], [4, 5, 6]] ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A.n Expecting: 2 ok Trying: A.rows == A.n Expecting: True ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A.shape Expecting: (2, 3) ok Trying: A = Matrix([[2, 0], [3, 4]]); A Expecting: [[2, 0], [3, 4]] ok Trying: A.swap_cols(0, 1); A Expecting: [[0, 2], [4, 3]] ok Trying: A = Matrix([[2, 0], [3, 4]]); A Expecting: [[2, 0], [3, 4]] ok Trying: A.swap_rows(0, 1); A Expecting: [[3, 4], [2, 0]] ok Trying: A = Matrix([[-4, 2+2j], [0, 4j]]) Expecting nothing ok Trying: A.trace() Expecting: (-4+4j) ok Trying: eye(19).trace() Expecting: 19 ok Trying: zeros(20).trace() Expecting: 0 ok Trying: ones(100).trace() Expecting: 100 ok Trying: A = Matrix([[1, 2, 3], [4, 5, 6]]) Expecting nothing ok Trying: A.transpose() Expecting: [[1, 4], [2, 5], [3, 6]] ok Trying: A.transpose().transpose() == A Expecting: True ok Trying: det(eye(2)) Expecting: 1 ok Trying: det((-1) * eye(4)) Expecting: 1 ok Trying: det((-1) * eye(5)) Expecting: -1 ok Trying: D = diag(range(1,6)) Expecting nothing ok Trying: D[2, :] Expecting: [[0, 0, 3, 0, 0]] ok Trying: D.trace() Expecting: 15 ok Trying: D.trace() == sum(range(1,6)) Expecting: True ok Trying: D.det Expecting: 120 ok Trying: from math import factorial Expecting nothing ok Trying: D.det == factorial(5) Expecting: True ok Trying: diag([-1, 1]) Expecting: [[-1, 0], [0, 1]] ok Trying: diag([-4, 1]) + 3 Expecting: [[-1, 3], [3, 4]] ok Trying: diag(3.14, 3) Expecting: [[3.14, 0, 0], [0, 3.14, 0], [0, 0, 3.14]] ok Trying: diag([3.14]*3) # Same ! Expecting: [[3.14, 0, 0], [0, 3.14, 0], [0, 0, 3.14]] ok Trying: import math Expecting nothing ok Trying: e = math.exp(1.0) Expecting nothing ok Trying: C = diag([1, 4]) Expecting nothing ok Trying: exp(C) == diag([e ** 1, e ** 4]) == diag([math.exp(1), math.exp(4)]) # Rounding mistakes! Expecting: False ok Trying: exp(C).almosteq(diag([e ** 1, e ** 4])) # No more rounding mistakes! Expecting: True ok Trying: diag([e ** 1, e ** 4]).almosteq(diag([math.exp(1), math.exp(4)])) Expecting: True ok Trying: eye(2) Expecting: [[1, 0], [0, 1]] ok Trying: zeros(18) == eye(18) * 0 Expecting: True ok Trying: eye(60).is_diagonal Expecting: True ok Trying: eye(40).is_square Expecting: True ok Trying: eye(20).is_singular Expecting: False ok Trying: eye(5).det Expecting: 1 ok Trying: eye(7).trace() Expecting: 7 ok Trying: V = Matrix([[1, 2, 3], [-1, 0, 4]]) Expecting nothing ok Trying: gram_schmidt(V) Expecting: [[1, 2, 3], [-1, 0, 4]] ok Trying: vx = [1, 2, 3]; vy = [-1, 0, 4] Expecting nothing ok Trying: innerproduct(vx, vy) Expecting: 11 ok Trying: vx = [1j, 2j, 3j]; vy = [-1, 0, 4] Expecting nothing ok Trying: (-1j) * (-1) + (-2j) * (0) + (-3j) * (4) Expecting: -11j ok Trying: innerproduct(vx, vy) Expecting: -11j ok Trying: inv(eye(2)) == eye(2) Expecting: True ok Trying: mat_from_f(lambda i, j: 1 if i == j else 0, 3) == eye(3) Expecting: True ok Trying: mat_from_f(lambda i, j: 1, 3) == ones(3) Expecting: True ok Trying: mat_from_f(lambda i, j: i+j, 3) Expecting: [[0, 1, 2], [1, 2, 3], [2, 3, 4]] ok Trying: mat_from_f(lambda i, j: i*j, 3) Expecting: [[0, 0, 0], [0, 1, 2], [0, 2, 4]] ok Trying: def f(i, j, e, offset=0): return (i * e) + offset Expecting nothing ok Trying: mat_from_f(f, 2, 2, 4) # n = 2, m = 2, e = 4 Expecting: [[0, 0], [4, 4]] ok Trying: mat_from_f(f, 2, 2, 4, offset=10) # n = 2, m = 2, e = 4, offset = 10 Expecting: [[10, 10], [14, 14]] ok Trying: u = [1, 2, 3] Expecting nothing ok Trying: norm2(u) Expecting: 3.7416573867739413 ok Trying: u = [1j, -2j, 3j] Expecting nothing ok Trying: norm2(u) Expecting: 3.7416573867739413 ok Trying: u = [1+1j, 2-2j, 3+3j] Expecting nothing ok Trying: norm2(u) Expecting: 5.291502622129181 ok Trying: u = [1, 2, 3] Expecting nothing ok Trying: norm_square(u) Expecting: 14 ok Trying: u = [1j, -2j, 3j] Expecting nothing ok Trying: norm_square(u) Expecting: 14.0 ok Trying: u = [1+1j, 2-2j, 3+3j] Expecting nothing ok Trying: norm_square(u) Expecting: 28.0 ok Trying: ones(3, 2) Expecting: [[1, 1], [1, 1], [1, 1]] ok Trying: ones(2, 3) Expecting: [[1, 1, 1], [1, 1, 1]] ok Trying: ones(2) Expecting: [[1, 1], [1, 1]] ok Trying: ones((2, 3)) Expecting: [[1, 1, 1], [1, 1, 1]] ok Trying: u = [1, 2, 3]; v = [-1, 0, 4] Expecting nothing ok Trying: proj(u, v) # 11/14 * u Expecting: [0.7857142857142857, 1.5714285714285714, 2.357142857142857] ok Trying: proj(u, v) == [(11/14) * x for x in u] Expecting: True ok Trying: from random import seed Expecting nothing ok Trying: seed(0) # We want the examples to always be the same Expecting nothing ok Trying: rand_matrix(2, 3) Expecting: [[7, 5, -2], [-5, 0, -2]] ok Trying: rand_matrix(3, 2, 40) Expecting: [[23, -16], [-2, 7], [33, 0]] ok Trying: rand_matrix(4, 4, 100) Expecting: [[-44, 51, 24, -50], [82, 97, 62, 81], [-38, 46, 80, 37], [-6, -80, -13, 22]] ok Trying: from random import seed Expecting nothing ok Trying: seed(0) # We want the examples to always be the same Expecting nothing ok Trying: rand_matrix_float(2, 3) Expecting: [[6.8884370305, 5.15908805881, -1.58856838338], [-4.82166499414, 0.225494427372, -1.90131725099]] ok Trying: rand_matrix_float(3, 2, 1) Expecting: [[0.56759717807, -0.393374547842], [-0.0468060916953, 0.16676407891], [0.816225770391, 0.00937371163478]] ok Trying: rand_matrix_float(4, 4, 20) Expecting: [[-8.72648622401, 10.2321681663, 4.73475986701, -9.9797463455], [16.3898502387, 19.3114190415, 12.4086894399, 16.0866380176], [-7.59409722723, 9.19326993041, 15.9535315187, 7.35935727662], [-1.11429138189, -15.9719516773, -2.63312658185, 4.43547893775]] ok Trying: rank(eye(2)) Expecting: 2 ok Trying: vx = [1, 2, 3]; vy = [-1, 0, 4] Expecting nothing ok Trying: vect_const_multi(vx, 2) Expecting: [2, 4, 6] ok Trying: vect_const_multi(vy, -4) Expecting: [4, 0, -16] ok Trying: zeros(3, 2) Expecting: [[0, 0], [0, 0], [0, 0]] ok Trying: zeros(2, 3) Expecting: [[0, 0, 0], [0, 0, 0]] ok Trying: ones(2, 3) == zeros(2, 3) + 1 Expecting: True ok Trying: zeros(2, 3) == ones(2, 3) * 0 Expecting: True ok Trying: zeros(2) Expecting: [[0, 0], [0, 0]] ok Trying: zeros((2, 3)) Expecting: [[0, 0, 0], [0, 0, 0]] ok 20 items had no tests: __main__.Decimal __main__.Decimal.__str__ __main__.Fraction __main__.Fraction.__str__ __main__.Matrix __main__.Matrix.__next__ __main__.Matrix.gauss_jordan __main__.Matrix.type __main__.PLUdecomposition __main__._argmax __main__._ifnone __main__._prod __main__._slice_to_range __main__.adjugate __main__.cofactor __main__.gauss __main__.gauss_jordan __main__.minor __main__.norm __main__.trace 83 items passed all tests: 12 tests in __main__ 3 tests in __main__.Matrix.T 4 tests in __main__.Matrix.__abs__ 9 tests in __main__.Matrix.__add__ 5 tests in __main__.Matrix.__contains__ 5 tests in __main__.Matrix.__eq__ 6 tests in __main__.Matrix.__floordiv__ 7 tests in __main__.Matrix.__getitem__ 2 tests in __main__.Matrix.__init__ 2 tests in __main__.Matrix.__iter__ 3 tests in __main__.Matrix.__len__ 5 tests in __main__.Matrix.__lt__ 8 tests in __main__.Matrix.__mod__ 7 tests in __main__.Matrix.__mul__ 7 tests in __main__.Matrix.__neg__ 5 tests in __main__.Matrix.__pos__ 21 tests in __main__.Matrix.__pow__ 6 tests in __main__.Matrix.__radd__ 10 tests in __main__.Matrix.__rdiv__ 2 tests in __main__.Matrix.__repr__ 7 tests in __main__.Matrix.__rfloordiv__ 3 tests in __main__.Matrix.__rmul__ 5 tests in __main__.Matrix.__rsub__ 8 tests in __main__.Matrix.__setitem__ 2 tests in __main__.Matrix.__str__ 7 tests in __main__.Matrix.__sub__ 8 tests in __main__.Matrix.__truediv__ 4 tests in __main__.Matrix.adjugate 7 tests in __main__.Matrix.almosteq 5 tests in __main__.Matrix.cofactor 5 tests in __main__.Matrix.col 3 tests in __main__.Matrix.cols 2 tests in __main__.Matrix.conjugate 4 tests in __main__.Matrix.copy 6 tests in __main__.Matrix.count 4 tests in __main__.Matrix.det 10 tests in __main__.Matrix.dot 9 tests in __main__.Matrix.exp 7 tests in __main__.Matrix.gauss 2 tests in __main__.Matrix.imag 10 tests in __main__.Matrix.inv 3 tests in __main__.Matrix.is_anti_symetric 5 tests in __main__.Matrix.is_diagonal 4 tests in __main__.Matrix.is_hermitian 3 tests in __main__.Matrix.is_lower 5 tests in __main__.Matrix.is_singular 4 tests in __main__.Matrix.is_square 3 tests in __main__.Matrix.is_symetric 3 tests in __main__.Matrix.is_upper 4 tests in __main__.Matrix.is_zero 13 tests in __main__.Matrix.map 5 tests in __main__.Matrix.minor 4 tests in __main__.Matrix.multiply_elementwise 3 tests in __main__.Matrix.next 6 tests in __main__.Matrix.norm 13 tests in __main__.Matrix.normalized 4 tests in __main__.Matrix.rank 4 tests in __main__.Matrix.real 4 tests in __main__.Matrix.round 5 tests in __main__.Matrix.row 3 tests in __main__.Matrix.rows 2 tests in __main__.Matrix.shape 2 tests in __main__.Matrix.swap_cols 2 tests in __main__.Matrix.swap_rows 5 tests in __main__.Matrix.trace 3 tests in __main__.Matrix.transpose 3 tests in __main__.det 11 tests in __main__.diag 6 tests in __main__.exp 7 tests in __main__.eye 2 tests in __main__.gram_schmidt 5 tests in __main__.innerproduct 1 tests in __main__.inv 7 tests in __main__.mat_from_f 6 tests in __main__.norm2 6 tests in __main__.norm_square 4 tests in __main__.ones 3 tests in __main__.proj 5 tests in __main__.rand_matrix 5 tests in __main__.rand_matrix_float 1 tests in __main__.rank 3 tests in __main__.vect_const_multi 6 tests in __main__.zeros 440 tests in 103 items. 440 passed and 0 failed. Test passed. More details about doctest can be found on the Python documentation: https://docs.python.org/2/library/doctest.html