Welcome, guest | Sign In | My Account | Store | Cart

to calculate determinant and manipulate matrices.

Python, 88 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class Point():
    def __init__(self,x,y,dim):
        self.x = x
        self.y = y
        self.dim = dim
        
class matrix(object):
    def __init__(self,mat):
        self.mat = mat
        self.row = len(mat)
        self.col = len(mat[0])
        self._matRes = []
        self.__s = ''
        
    def __str__(self):
        for i in range(self.row):
        	self.__s += '\n'
        	for j in range(self.col):
        		self.__s += '%g\t' %(self.mat[i][j])
        return self.__s
    
    
    def __mul__(self,other):
        if isinstance(other,int) or isinstance(other,float):
            for i in range(self.row):
                for j in range(self.col):
                    self.mat[i][j] *= other
            return matrix(self.mat)
                
        if self.col != other.row: return 'The number of columns of the first matrix must be equal to the number of rows of the second.'
        self._matRes = [[0 for r in range(other.col)] for c in range(self.row)]
        for i in range(self.row):
            for j in range(other.col):
                for k in range(other.row):
                    self._matRes[i][j] += self.mat[i][k] * other.mat[k][j]               
        return matrix(self._matRes)
    
    def __add__(self,other):
        if not (self.row == other.row) and (self.col == other.col): return 'The number of col is not equal to the number of row'
        self._matRes = [[0 for r in range(self.col)] for c in range(self.row)]
        for i in range(self.row):
            for j in range(self.col):
                self._matRes[i][j] += self.mat[i][j] + other.mat[i][j]
        return matrix(self._matRes)
        
    def __pow__(self,other):
    	if not isinstance(other,int): return 'only int'
    	if other == 0: return 'Prime matrix'
    	if other < 0: return 'only int'
    	for i in range(1,other+1):
    		if i != other:
    			self.__s += 'matrix(self.mat)*'
    		else:
    			self.__s += 'matrix(self.mat)'
    	return eval(self.__s)

    def det(self, point):
    	M = point.dim - 1
    	if len(self.mat) == 2 :
            return (int(self.mat[0][0]) * int(self.mat[1][1])) - (int(self.mat[0][1]) * int(self.mat[1][0]))

    	s = 0
    	for row in range(1, point.dim+1):
        	copyli = []
        	for i in range(1,len(li)):
                    copyli1 = []
            	for j in range(len(li)) :
                	if (row - 1) != j :
                		copyli1.append(li[i][j])
            	copyli.append(copyli1)
                
        	s += (-1) ** (1 + row) * int(li[0][row-1]) * det(copyli, Point(1, row, M))
    	return s
       
		
		
		
def inverse(self):
	pass
    
    
    
print matrix.det(matrix([[1,2],[2,3]]))
   
#print (matrix([[15,24,33],[21,-34,25]]) * matrix([[15,24],[21,-34],[1,3]]))* matrix([[1,2],[2,3]])

#print (matrix([[1,2,12,33,2,2],[1,2,3,22,1,3],[1,21,3,4,2,4],[111,31,34,2,12,1],[2,33,122,1,3,3],[1,19,90,6,2,4]]))**10
#print ( matrix([[1,2],[2,3]]) * matrix([[1,2],[2,3]])) + matrix([[1,2],[2,3]])

1 comment

hosseini 11 years, 3 months ago  # | flag

tank you very much I am a student professor razavian!

Created by Hamidreza Joshaghani on Wed, 27 Apr 2011 (MIT)
Python recipes (4591)
Hamidreza Joshaghani's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks