Skip to content

mafrasi2/pylinalg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

Examples

Import the module:

from pylinalg import *

Fields

The field of rational numbers. Set output_as_fraction=False to get decimal representations, when calling the str() function. Note that all disadvantages of floats apply then.

rat_field = RationalNumbersField(output_as_fraction=True)

a = rat_field.from_repr((9, 6))  # the fraction 9/6
b = rat_field.from_repr(3)
print(a * b)

We will to use 𝔽5 in our following examples.

Create the new residue field 𝔽5 and its transversal {-2,...,2}:

neg_trans_5 = StdNegTransversal(5)
res_field_5 = ResidueField(5, transversal=neg_trans_5)

Find the inverse of 3 in 𝔽5:

a = res_field_5.from_repr(3)
a_i = res_field_5.inverse(a)
msg = "The inverse of {} in F_{} is: {}"
print(msg.format(a, 5, a_i))

Swap the transversal of 𝔽5 to {0,...,4}

print("Switching to standard transversal:")
print(str(a) + " -> ", end='') #  no new line
trans_5 = StdTransversal(5)
res_field_5.transversal = trans_5
print(a)

Basic matrix operations

Create some matrices:

A = [[ 1, -9,  3,  5],
     [ 7, -3, -6,  8],
     [ 5, -2, -4,  6],
     [-5,  1,  2, -8]]

B = [[ 1, -1],
     [-1,  2],
     [ 3, -1]]

C = [[2, 7],
     [5, 4],
     [1, 8]]

A = Matrix(A, res_field_5)
B = Matrix(B, res_field_5)
C = Matrix(C, res_field_5)

Indices start at 0 in all functions calls!

Select the cell at A3,4:

cell = A[2,3]
print("A_(3,4) = {}".format(cell))

Select the 3rd column, A-1,3, ie. at index 2. Rows work analog. The result is a new matrix.

column = A[-1,2]
print("A_(-1,3) = \n{}".format(column))

Replace the 3rd row of A with ones.

print(A)
print(" -> ")
row = Matrix([[1,1,1,1]], res_field_5)
A[2,-1] = row
print(A)

Matrix arithmetic

Append C to B and multiply the result with A.

R = (B | C) * A
print("(B | C) * A = \n{}".format(R))

Solving systems of linear equations

Transform B | C into an upper triangular matrix. Use normalize=False, if you don't want to normalize the diagonal.

D = B | C
D.to_upper_triangular_matrix(normalize=True)

And into a diagonal matrix (the step above is not necessary to do this).

D.to_diagonal_matrix(normalize=True)

Calculate the inverse of A

print(A * A.inverse())

Solve the equation Eb=x for b in 𝔽2.

trans_2 = StdTransversal(2)
res_field_2 = ResidueField(2, transversal=trans_2)

E = [[0, 1, 0],
     [1, 0, 1],
     [1, 1, 0]]

x = [[0],
     [0],
     [1]]
E = Matrix(E, res_field_2)
x = Matrix(x, res_field_2)

print("Extended coefficient matrix for Sol(E,x):")
print(E | x)
R = E.solve(x)
print("Eb=x for b = \n{}".format(R))

Rank and determinant

Calculate the rank and determinant of A

print("rk(A) = {}".format(E.rank()))
print("det(A) = {}".format(E.determinant()))

Misc

Get a nicely formatted python matrix from console input.

print(input_matrix())

The expected syntax is as following:

1 31 1 3
3 1 1 -1
12 1 41 0

which results in:

[[ 1, 31,  1,  3],
 [ 3,  1,  1, -1],
 [12,  1, 41,  0]]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages