Skip to content

Austerius/matrices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

matrices

matrices repository was created to provide basic operation on matrices: addition, multiplication, transposition, reversion etc(check all operation below). Project, mostly, has educational purpose and will be useful for students(it contains examples how to implement matrices operation in python, and also examples of possible pitfalls due to 'bad' implementation). Need to mention, that main script('matrices.py') follow PEP8 convention.

To use 'matrices' in your own project - just copy 'matrices.py' and 'errors.py' in your project directory. Then, use import statement like this:

import matrices

To provide more acurate calculation, matrices module using decimal type of numbers. So, to initialize a new Matrix instance, you need to pass into constructor a list of lists, that consist of decimal arguments or other numeric type arguments(they will be convert into decimal type inside a constructor). Example:

import matrices
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
A = matrices.Matrix(list_of_lists)

Implemented matrices operations:
  • is_zero_matrix - checking, if a current matrix is a zero matrix. Returns 'True', if so:
  • A.is_zero_matrix()
  • is_square_matrix - checking, if a current matrix is a square matrix(number of rows equal to number of columns) Returns 'True', if so:
  • A.is_square_matrix()
  • is_diagonal_matrix - checking if a matrix is a diagonal matrix: has all elements equal to zero (except of main diagonal). Returns 'True', if so:
  • A.is_diagonal_matrix()
  • is_identity_matrix -Checking if a current matrix is an identity matrix: diagonal, square matrix, where all elements on the main diagonal equals to one. Returns 'True', if so:
  • A.is_identity_matrix()
  • matrix_is_equal - this method comparing matrix A to matrix B. Returns 'True', if matrices are equal:
  • A.matrix_is_equal(B)
  • matrix_transposition - this method will transpose matrix A, and then - will return transposed matrix (Matrix A will be not change by this operation. You can assign result to a new matrix, if needed):
  • B = A.matrix_transposition()
  • matrix_is_transpose - checking, if a current matrix is transpose to a given one. Returns 'True', if so:
  • A.matrix_is_transpose(B)
  • matrix_addition - method for adding one matrix to another, if it's possible. A result of the addition will be returned as a new matrix:
  • C = A.matrix_addition(B)
    C = A + B
  • multiply_by_number - method for multiplying a current matrix by a scalar('k'). Also, it has a supplementary parameter of a precision - how much numbers will be after '.' symbol(default value is 2). Note: try not to go for a precision greater than 20(error exception will be raised). Method returns a new matrix:
  • B = A.multiply_by_number(k)
    B = A.multiply_by_number(k, precision)
    B = A * k - note: cant use precision parameter explicitly(using default value).
  • matrix_subtraction - method for subtracting one matrix from another( if it's possible). Returns a new matrix as a result:
  • C = A.matrix_subtraction(B)
    C = A - B
  • divide_by_number - method for dividing a current matrix by a scalar('k'). It has a supplementary parameter of a precision - how much numbers will be after '.' symbol(default value is 2). Note: try not to go for a precision greater than 20(error exception will be raised). Method returns a new matrix:
  • B = A.divide_by_number(k)
    B = A.divide_by_number(k, precision)
    B = A/k - note: cant use precision parameter explicitly(using default value).
  • matrix_multiplication - method for multiplying a current matrix by a given matrix(which written in parentheses). Result will be returned in a form of a new matrix:
  • C = A.matrix_multiplication(B)
    C = A * B
  • matrix_determinant - calculating matrix determinant, using Gauss-Jordan method(elementary row operations). It has a supplementary parameter of a precision with default value equal to 2. Method returns numeric result:
  • det = A.matrix_determinant()
    det = A.matrix_determinant(precision)
  • matrix_inverse - creating an inverse matrix to the current one, using Gauss-Jordan method(elementary row operations). It also has a supplementary parameter of a precision with a default value equal to 3. Returns an instance of new Matrix()
  • B = A.matrix_inverse()
    B = A.matrix_inverse(precision)
  • add_number - adding a number to a matrix. Number, in this case, could be represent as a matrix, consist of the same number. That matrix, also, will have appropriate dimension for addition operation. Note, that you can add a number to a matrix but not a vise versa. Returning type - Matrix(). Example of calling the method:
  • B = A.add_number(k), where k - numeric type.
    B = A + k
  • subtract_number - method for subtracting number from a matrix. This method is similar to add_number in his concept. Like in add_number you can't subtract matrix from a number. Returning type - Matrix(). Example of calling:
  • B = A.subtract_number(k), where k is a numeric type.
    B = A - k
  • matrix_show - method for printing current matrix
  • A.matrix_show()

    If you want to initialize a Zero matrix with a given sizes - use a child class of Matrix: ZeroMatrix(). It takes 2 'must be' parameters(plus one optional): number of rows('m' - integer number) and number of columns('n' integer number). Example:

    O = matrices.ZeroMatrix(m, n)

    If you need to fill the matrix with same number, different from zero - use optional parameter 'number' of ZeroMatrix() class, like this:
    O = matrices.ZeroMatrix(m, n, number=k), where k - numeric type. For example:
    O = matrices.ZeroMatrix(2, 2, number=-4.2) will create 2 by 2 matrix filled with number -4.2

    For initializing an Identity matrix - use subclass IdentityMatrix(). Here you need to pass one parameter - matrix dimension (n - integer digit, which represent numbers of rows/columns for square matrix). Example:

    I = matrices.IdentityMatrix(n)

    Releases

    No releases published

    Packages

    No packages published

    Languages