Module linearalgebra
[frames] | no frames]

Module linearalgebra

source code

Classes
  LAValueError
  InvalidVectorException
  IncompatibleVectorException
  InvalidMatrixException
  IncompatibleMatrixException
Functions
 
is_matrix(A)
Determine whether the given arg is a matrix, which must be a rectangular array of numbers, represented as a list/tuple of list/tuple elements, each of which is a vector of equal size.
source code
 
matrix_dimensions(A)
Calculates the dimensions of the given matrix, returning a pair consisting of the number of rows and the number of columns, raising an exception if any of the rows of the matrix have a different number of elements.
source code
 
matrix_is_square(A)
Determine whether the given matrix is a square matrix, with the same number of rows and columns.
source code
 
matrix_is_diagonal(A)
Determine whether the given matrix is a diagonal matrix -- i.e., a square matrix with all nondiagonal entries equal to 0.
source code
 
matrix_is_scalar(A)
Determine whether the given matrix is a scalar matrix -- i.e., a diagonal matrix with all diagonal entries equal to each other.
source code
 
matrix_is_identity(A)
Determine whether the given matrix is an identity matrix -- i.e., a non-empty scalar matrix with all diagonal entries equal to 1.
source code
 
matrix_is_symmetric(A)
Determine whether the given matrix is symmetric -- i.e., whether it is equal to its own transpose.
source code
 
matrix_plus(A, B)
Add the given matrices, which must have the same number of rows and columns.
source code
 
matrix_minus(A, B)
Subtract the second matrix from the first, both of which must have the same number of rows and columns.
source code
 
matrix_times(s, A)
Multiply the matrix A by the scalar s.
source code
 
matrix_negative(A)
Return the negative of the given matrix, or the matrix multiplied by scalar -1.
source code
 
matrix_transpose(A)
Generate the transpose of the given m x n matrix, which is the n x m matrix with where each (i,j) element of the original matrix is the (j,i) element of the new matrix.
source code
 
matrix_product(A, B)
Calculate the product of matrices A and B, where A must have the same number of columns and B has rows.
source code
 
scalar_equal(s1, s2, eps=epsilon)
Determine whether the two scalars are equal to accuracy of epsilon.
source code
 
vector_equal(u, v)
Determine whether the two vectors are equal by determining if the pairwise components are equal to accuracy of epsilon.
source code
 
matrix_equal(A, B)
Determine whether the two matrices are equal by determining if the corresponding components are equal to accuracy of epsilon.
source code
 
vector_zero(x)
Create a zero vector with same size as the given vector, or having as many elements as the given int.
source code
 
vector_unit_length(v)
Create a unit-length vector in direction of v.
source code
 
vector_times(s, v)
Multiplies the vector by the given scalar.
source code
 
vector_plus(u, v)
Calculate the vector resulting from the addition of the two given vectors.
source code
 
vector_minus(u, v)
Subtract the second vector from the first vector.
source code
 
vector_product(u, v)
Calculate the dot product of the given vectors, which is defined as the sum of the pairwise products of the vectors.
source code
 
vector_norm(v)
Calculate the length, or norm, of the vector, which is defined to be the square root of the sum of the squares of the components of the vector.
source code
 
vector_distance(u, v)
Calculate the distance between the two vectors, which is defined to be the norm of the difference between the vectors.
source code
 
vector_is_orthogonal(u, v)
Determine whether the two vectors are orthogonal, which is defined by whether the dot product is 0.
source code
 
vector_angle(u, v)
Determine the angle between the two vectors, in radians.
source code
 
vector_project(u, v)
Calculate the projection of v onto u.
source code
 
is_vector_type(u)
Determine whether arg is a vector type (list or tuple).
source code
 
is_vector(u)
Determine whether the arg is a vector, or a list or tuple of numbers.
source code
 
is_scalar(s)
Determine whether s is a scalar, which is either a float, int, or long.
source code
Variables
  degrees = math.degrees
  radians = math.radians
  e = math.e
  pi = math.pi
  sqrt = math.sqrt
  cos = math.cos
  sin = math.sin
  tan = math.tan
  acos = math.acos
  asin = math.asin
  atan = math.atan
  epsilon = 0.000000000000001
Function Details

is_matrix(A)

source code 

Determine whether the given arg is a matrix, which must be a rectangular array of numbers, represented as a list/tuple of list/tuple elements, each of which is a vector of equal size. >>> is_matrix([]) True >>> is_matrix([[]]) True >>> is_matrix([1,2]) False >>> is_matrix([[1,2], (3,4)]) True >>> is_matrix([[1,2], [3,4,5]]) False >>> is_matrix([[1,2], ['0', 1]]) False >>> is_matrix([[.5, -1, 9], [4, 3, 19], [3, 6, 8], [0, 0, 1]]) True

matrix_dimensions(A)

source code 

Calculates the dimensions of the given matrix, returning a pair consisting of the number of rows and the number of columns, raising an exception if any of the rows of the matrix have a different number of elements.

>>> matrix_dimensions([])
(0, 0)
>>> matrix_dimensions([[1,2,3]])
(1, 3)
>>> matrix_dimensions([[1], [2], [3]])
(3, 1)
>>> matrix_dimensions([[1, 2], [1, 2, 3]])
Traceback (most recent call last):
  ...
InvalidMatrixException: Arg is not a matrix: [[1, 2], [1, 2, 3]]
Decorators:
  • @__check_matrix(0)

matrix_is_square(A)

source code 

Determine whether the given matrix is a square matrix, with the same number of rows and columns.

>>> matrix_is_square([])
True
>>> matrix_is_square([[]])
False
>>> matrix_is_square([[1,1], [2,2], [3,3]])
False
>>> matrix_is_square([[1,2,3], [4,5,6], [7,8,9]])
True
>>> matrix_is_square([[1], [1,2]])
Traceback (most recent call last):
  ...
InvalidMatrixException: Arg is not a matrix: [[1], [1, 2]]
Decorators:
  • @__check_matrix(0)

matrix_is_diagonal(A)

source code 

Determine whether the given matrix is a diagonal matrix -- i.e., a square matrix with all nondiagonal entries equal to 0.

>>> matrix_is_diagonal([])
True
>>> matrix_is_diagonal([[0]])
True
>>> matrix_is_diagonal([[0, 0], [0, 0], [0, 0]])
False
>>> matrix_is_diagonal([[1, 0], [0, 2]])
True
>>> matrix_is_diagonal([[1, 0], [1, 0]])
False
Decorators:
  • @__check_matrix(0)

matrix_is_scalar(A)

source code 

Determine whether the given matrix is a scalar matrix -- i.e., a diagonal matrix with all diagonal entries equal to each other.

>>> matrix_is_scalar([])
True
>>> matrix_is_scalar([[2]])
True
>>> matrix_is_scalar([[3, 0], [0, 3]])
True
>>> matrix_is_scalar([[3, 0], [1, 3]])
False
Decorators:
  • @__check_matrix(0)

matrix_is_identity(A)

source code 

Determine whether the given matrix is an identity matrix -- i.e., a non-empty scalar matrix with all diagonal entries equal to 1.

>>> matrix_is_identity([])
False
>>> matrix_is_identity([[0]])
False
>>> matrix_is_identity([[1]])
True
>>> matrix_is_identity([[1, 1], [1, 1]])
False
>>> matrix_is_identity([[1, 0], [0, 1]])
True
>>> matrix_is_identity([[1, 0], [0, 1], [0, 0]])
False
>>> matrix_is_identity([[2, 0], [0, 2]])
False
Decorators:
  • @__check_matrix(0)

matrix_is_symmetric(A)

source code 

Determine whether the given matrix is symmetric -- i.e., whether it is equal to its own transpose.

>>> matrix_is_symmetric([[1, 0], [0, 1]])
True
>>> matrix_is_symmetric([[0, 1], [1, 0]])
True
>>> matrix_is_symmetric([[1, 2], [2, 0]])
True
>>> matrix_is_symmetric([[1, 2], [1, 2]])
False
>>> matrix_is_symmetric([[1, 3, 2], [3, 5, 0], [2, 0, 4]])
True
Decorators:
  • @__check_matrix(0)

matrix_plus(A, B)

source code 

Add the given matrices, which must have the same number of rows and columns.

>>> matrix_plus([[1, 2], [3, 4]], [[4, 3], [2, 1]])
[[5, 5], [5, 5]]
>>> matrix_plus([[1,2]], [[3, 4]]), matrix_plus([[1,2]], [[3, 4]])[0] == vector_plus([1,2], [3,4])
([[4, 6]], True)
>>> matrix_plus([[1]], [[1, 2]])
Traceback (most recent call last):
  ...
IncompatibleMatrixException: Matrices must have same dimensions. Matrix 1 is 1 x 1, but matrix 2 is 1 x 2.
Decorators:
  • @__check_matrix_same_size(0, 1)

matrix_minus(A, B)

source code 

Subtract the second matrix from the first, both of which must have the same number of rows and columns.

>>> matrix_minus([[3, 4], [1, 2]], [[1, 1], [1, 1]])
[[2, 3], [0, 1]]
>>> matrix_minus([[1,2]], [[3, 4]]), matrix_minus([[1,2]], [[3, 4]])[0] == vector_minus([1,2], [3,4])
([[-2, -2]], True)
>>> matrix_minus([[1]], [[1, 2]])
Traceback (most recent call last):
  ...
IncompatibleMatrixException: Matrices must have same dimensions. Matrix 1 is 1 x 1, but matrix 2 is 1 x 2.
Decorators:
  • @__check_matrix_same_size(0, 1)

matrix_times(s, A)

source code 

Multiply the matrix A by the scalar s.

>>> matrix_times(2, [[0,1], [1, 0]])
[[0, 2], [2, 0]]
>>> matrix_times(-1, [[1,2,3,4]])
[[-1, -2, -3, -4]]
>>> matrix_times(3, [[]])
[[]]
>>> matrix_times(1, [])
[]
>>> matrix_times(3, [[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
[[3, 6, 9], [12, 15, 18], [21, 24, 27], [30, 33, 36]]
Decorators:
  • @__check_scalar(0)
  • @__check_matrix(1)

matrix_negative(A)

source code 

Return the negative of the given matrix, or the matrix multiplied by scalar -1.

>>> matrix_negative([])
[]
>>> matrix_negative([[]])
[[]]
>>> matrix_negative(((-1, 2), (2, -3)))
[[1, -2], [-2, 3]]
Decorators:
  • @__check_matrix(0)

matrix_transpose(A)

source code 

Generate the transpose of the given m x n matrix, which is the n x m matrix with where each (i,j) element of the original matrix is the (j,i) element of the new matrix.

>>> matrix_transpose([[1, 2], [3, 4], [5, 6]])
[[1, 3, 5], [2, 4, 6]]
>>> matrix_transpose([])
[]
>>> matrix_transpose([[]])
[]
>>> matrix_transpose([[1,2,3], [4,5,6], [7,8,9]])
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
>>> matrix_transpose(matrix_transpose([[1,2,3], [4,5,6], [7,8,9]]))
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> matrix_transpose([[1,2,3]])
[[1], [2], [3]]
Decorators:
  • @__check_matrix(0)

matrix_product(A, B)

source code 

Calculate the product of matrices A and B, where A must have the same number of columns and B has rows. The result for A of size m x n and B of size n x r is an m x r matrix.

>>> matrix_product([[1,1], [1,1]], [[1,1], [1,1]])
[[2, 2], [2, 2]]
>>> matrix_product([], [])
[]
>>> matrix_product([[1, 3, -1], [-2, -1, 1]], [[-4, 0, 3, -1], [5, -2, -1, 1], [-1, 2, 0, 6]])
[[12, -8, 0, -4], [2, 4, -5, 7]]
Decorators:
  • @__check_matrix_multipliable(0, 1)
  • @__check_matrix(0, 1)

scalar_equal(s1, s2, eps=epsilon)

source code 

Determine whether the two scalars are equal to accuracy of epsilon.

>>> scalar_equal(0.9000000000000002, 0.9000000000000001)
True
>>> scalar_equal(1, 1)
True
>>> scalar_equal(1, 1.0)
True
>>> scalar_equal(1.0, 1.00000001)
False

vector_equal(u, v)

source code 

Determine whether the two vectors are equal by determining if the pairwise components are equal to accuracy of epsilon.

>>> vector_equal([2], [2])
True
>>> vector_equal([1.0, 0.9000000000000002], [1, 0.9000000000000001])
True
Decorators:
  • @__check_vector_same_size(0, 1)

matrix_equal(A, B)

source code 

Determine whether the two matrices are equal by determining if the corresponding components are equal to accuracy of epsilon.

>>> matrix_equal([], [])
True
>>> matrix_equal([[1, 2, 3], [3.9000000000000001, 2, 1]], [[1, 2, 3], [3.9, 2, 1]])
True
Decorators:
  • @__check_matrix(0, 1)

vector_zero(x)

source code 

Create a zero vector with same size as the given vector, or having as many elements as the given int.

>>> vector_zero(3)
[0, 0, 0]
>>> vector_zero([1,2,3,4])
[0, 0, 0, 0]
>>> vector_zero([])
[]

vector_unit_length(v)

source code 

Create a unit-length vector in direction of v.

>>> vector_unit_length([3,3])
[0.70710678118654757, 0.70710678118654757]
Decorators:
  • @__check_vector(0)

vector_times(s, v)

source code 

Multiplies the vector by the given scalar.

>>> vector_times(3, [1,2])
[3, 6]
>>> vector_times(5, [4,-2,7])
[20, -10, 35]
>>> vector_times(0, [1,2,3])
[0, 0, 0]
Decorators:
  • @__check_vector(1)

vector_plus(u, v)

source code 

Calculate the vector resulting from the addition of the two given vectors.

>>> vector_plus([1,2], [3,4])
[4, 6]
>>> vector_plus([0.5, 1], [2.5, -1])
[3.0, 0]
Decorators:
  • @__check_vector_same_size(0, 1)

vector_minus(u, v)

source code 

Subtract the second vector from the first vector.

>>> vector_minus([2], [-3])
[5]
>>> vector_minus([1,2,3], [2,3,4])
[-1, -1, -1]
Decorators:
  • @__check_vector_same_size(0, 1)

vector_product(u, v)

source code 

Calculate the dot product of the given vectors, which is defined as the sum of the pairwise products of the vectors.

>>> vector_product([0,0], [0,1])
0
>>> vector_product([1,2,-3], [-3,5,2])
1
Decorators:
  • @__check_vector_same_size(0, 1)

vector_norm(v)

source code 

Calculate the length, or norm, of the vector, which is defined to be the square root of the sum of the squares of the components of the vector.

>>> vector_norm([0,1])
1.0
>>> vector_norm([1,1]), scalar_equal(vector_norm([1,1]), sqrt(2))
(1.4142135623730951, True)
>>> vector_norm([2,-1,3]), scalar_equal(vector_norm([2,-1,3]), sqrt(14))
(3.7416573867739413, True)
>>> vector_norm([sqrt(2), -1, 1])
2.0
Decorators:
  • @__check_vector(0)

vector_distance(u, v)

source code 

Calculate the distance between the two vectors, which is defined to be the norm of the difference between the vectors. >>> vector_distance([sqrt(2), 1, -1], [0, 2, -2]) 2.0 >>> vector_distance([0,1], [1,0]) 1.4142135623730951 >>> vector_distance([-1,2], [3,1]) 4.1231056256176606

Decorators:
  • @__check_vector_same_size(0, 1)

vector_is_orthogonal(u, v)

source code 

Determine whether the two vectors are orthogonal, which is defined by whether the dot product is 0.

>>> vector_is_orthogonal([0,1], [1,0])
True
>>> vector_is_orthogonal([2,-1,1], [1,-2,-1])
False
>>> vector_is_orthogonal([1,1,-2], [3,1,2])
True
Decorators:
  • @__check_vector_same_size(0, 1)

vector_angle(u, v)

source code 

Determine the angle between the two vectors, in radians.

>>> vector_angle([0,1,1], [1,0,1]), scalar_equal(vector_angle([0,1,1], [1,0,1]), pi/3)
(1.0471975511965979, True)
>>> vector_angle([0.9, 2.1, 1.2], [-4.5, 2.6, -0.8])
1.5376292299388497
Decorators:
  • @__check_vector_same_size(0, 1)

vector_project(u, v)

source code 

Calculate the projection of v onto u.

>>> vector_project([2,1], [-1, 3]), vector_equal(vector_project([2,1], [-1, 3]), [2.0/5, 1.0/5])
([0.40000000000000002, 0.20000000000000001], True)
>>> vector_project([0,0,1], [1,2,3]), vector_equal(vector_project( [0,0,1], [1,2,3]), [0,0,3])
([0.0, 0.0, 3.0], True)
Decorators:
  • @__check_vector_same_size(0, 1)

is_vector(u)

source code 

Determine whether the arg is a vector, or a list or tuple of numbers.

>>> is_vector('')
False
>>> is_vector([])
True
>>> is_vector(())
True
>>> is_vector((1,))
True
>>> is_vector([1])
True
>>> is_vector([1,2,3,4])
True
>>> is_vector((1,2,3))
True
>>> is_vector([1, 0.5, -9L])
True
>>> is_vector([1, 2, "3"])
False
>>> is_vector([[0]])
False
>>> is_vector([[1,2]])
False

is_scalar(s)

source code 

Determine whether s is a scalar, which is either a float, int, or long. The integral values 0 and 1 may also be given as False and True, respectively.

>>> is_scalar(0)
True
>>> is_scalar(True)
True
>>> is_scalar(-9.2)
True
>>> is_scalar(100L)
True
>>> is_scalar((1,2))
False