mirror of
https://github.com/DeaDvey/mathgenerator.git
synced 2025-11-28 06:25:23 +01:00
added matrix Inversion
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
pytest
|
pytest
|
||||||
hypothesis
|
hypothesis
|
||||||
flake8
|
flake8
|
||||||
autopep8
|
autopep8
|
||||||
|
sympy
|
||||||
@@ -71,3 +71,4 @@ from .multiplyComplexNumbersFunc import *
|
|||||||
from .geomProgrFunc import *
|
from .geomProgrFunc import *
|
||||||
from .geometricMeanFunc import *
|
from .geometricMeanFunc import *
|
||||||
from .harmonicMeanFunc import *
|
from .harmonicMeanFunc import *
|
||||||
|
from .matrixInversion import *
|
||||||
|
|||||||
68
mathgenerator/funcs/matrixInversion.py
Normal file
68
mathgenerator/funcs/matrixInversion.py
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
from .__init__ import *
|
||||||
|
|
||||||
|
def matrixInversion(SquareMatrixDimension=3, MaxMatrixElement=99, OnlyIntegerElementsInInvertedMatrix=False):
|
||||||
|
if OnlyIntegerElementsInInvertedMatrix is True:
|
||||||
|
isItOk = False
|
||||||
|
Mat = list()
|
||||||
|
while (isItOk is False):
|
||||||
|
Mat = list()
|
||||||
|
for i in range(0, SquareMatrixDimension):
|
||||||
|
z = list()
|
||||||
|
for j in range(0, SquareMatrixDimension):
|
||||||
|
z.append(0)
|
||||||
|
z[i] = 1
|
||||||
|
Mat.append(z)
|
||||||
|
MaxAllowedMatrixElement = math.ceil(
|
||||||
|
pow(MaxMatrixElement, 1 / (SquareMatrixDimension)))
|
||||||
|
randomlist = random.sample(
|
||||||
|
range(0, MaxAllowedMatrixElement + 1), SquareMatrixDimension)
|
||||||
|
|
||||||
|
for i in range(0, SquareMatrixDimension):
|
||||||
|
if i == SquareMatrixDimension - 1:
|
||||||
|
Mat[0] = [j + (k * randomlist[i])
|
||||||
|
for j, k in zip(Mat[0], Mat[i])]
|
||||||
|
else:
|
||||||
|
Mat[i + 1] = [j + (k * randomlist[i])
|
||||||
|
for j, k in zip(Mat[i + 1], Mat[i])]
|
||||||
|
|
||||||
|
for i in range(1, SquareMatrixDimension - 1):
|
||||||
|
Mat[i] = [sum(i)
|
||||||
|
for i in zip(Mat[SquareMatrixDimension - 1], Mat[i])]
|
||||||
|
|
||||||
|
isItOk = True
|
||||||
|
for i in Mat:
|
||||||
|
for j in i:
|
||||||
|
if j > MaxMatrixElement:
|
||||||
|
isItOk = False
|
||||||
|
break
|
||||||
|
if isItOk is False:
|
||||||
|
break
|
||||||
|
|
||||||
|
random.shuffle(Mat)
|
||||||
|
Mat = sympy.Matrix(Mat)
|
||||||
|
Mat = sympy.Matrix.transpose(Mat)
|
||||||
|
Mat = Mat.tolist()
|
||||||
|
random.shuffle(Mat)
|
||||||
|
Mat = sympy.Matrix(Mat)
|
||||||
|
Mat = sympy.Matrix.transpose(Mat)
|
||||||
|
|
||||||
|
else:
|
||||||
|
randomlist = list(sympy.primerange(0, MaxMatrixElement + 1))
|
||||||
|
plist = random.sample(randomlist, SquareMatrixDimension)
|
||||||
|
randomlist = random.sample(
|
||||||
|
range(0, MaxMatrixElement + 1), SquareMatrixDimension * SquareMatrixDimension)
|
||||||
|
randomlist = list(set(randomlist) - set(plist))
|
||||||
|
n_list = random.sample(
|
||||||
|
randomlist, SquareMatrixDimension * (SquareMatrixDimension - 1))
|
||||||
|
Mat = list()
|
||||||
|
for i in range(0, SquareMatrixDimension):
|
||||||
|
z = list()
|
||||||
|
z.append(plist[i])
|
||||||
|
for j in range(0, SquareMatrixDimension - 1):
|
||||||
|
z.append(n_list[(i * SquareMatrixDimension) + j - i])
|
||||||
|
random.shuffle(z)
|
||||||
|
Mat.append(z)
|
||||||
|
Mat = sympy.Matrix(Mat)
|
||||||
|
problem = 'Inverse of Matrix ' + str(Mat) + ' is:'
|
||||||
|
solution = str(sympy.Matrix.inv(Mat))
|
||||||
|
return problem, solution
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import random
|
import random
|
||||||
import math
|
import math
|
||||||
import fractions
|
import fractions
|
||||||
|
import sympy
|
||||||
from .funcs import *
|
from .funcs import *
|
||||||
|
|
||||||
genList = []
|
genList = []
|
||||||
@@ -156,3 +157,4 @@ complexNumMultiply = Generator("Multiplication of 2 complex numbers", 64, "(x +
|
|||||||
geometricprogression=Generator("Geometric Progression", 65, "Initial value,Common Ratio,nth Term,Sum till nth term =", "a,r,ar^n-1,sum(ar^n-1", geomProgrFunc)
|
geometricprogression=Generator("Geometric Progression", 65, "Initial value,Common Ratio,nth Term,Sum till nth term =", "a,r,ar^n-1,sum(ar^n-1", geomProgrFunc)
|
||||||
geometricMean=Generator("Geometric Mean of N Numbers",66,"Geometric mean of n numbers A1 , A2 , ... , An = ","(A1*A2*...An)^(1/n) = ans",geometricMeanFunc)
|
geometricMean=Generator("Geometric Mean of N Numbers",66,"Geometric mean of n numbers A1 , A2 , ... , An = ","(A1*A2*...An)^(1/n) = ans",geometricMeanFunc)
|
||||||
harmonicMean=Generator("Harmonic Mean of N Numbers",67,"Harmonic mean of n numbers A1 , A2 , ... , An = "," n/((1/A1) + (1/A2) + ... + (1/An)) = ans",harmonicMeanFunc)
|
harmonicMean=Generator("Harmonic Mean of N Numbers",67,"Harmonic mean of n numbers A1 , A2 , ... , An = "," n/((1/A1) + (1/A2) + ... + (1/An)) = ans",harmonicMeanFunc)
|
||||||
|
invertmatrix = Generator("Inverse of a Matrix", 68, "Inverse of a matrix A is", "A^(-1)", matrixInversion)
|
||||||
Reference in New Issue
Block a user