exp(A), A is a matrix
Programe is:
#
# Calculate A**n, A is a matrix, n is an integer
# A has maximum 15 rows and 15 columns
#
AA = [[1,2,3,4],
[2,3,4,5],
[3,4,5,6],
[4,5,6,7]]
AAA = [[2,2],
[2,2]]
BB = [[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[2,2,2,2]]
NN_row = 4
NN_column = 4
def product_AB(A, B, N_row, N_column):
C = [[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[2,2,2,2]]
# C = [[0]*N_row]*N_column
for i in range(N_row):
for j in range(N_column):
C[i][j]=0
for k in range(N_column):
C[i][j] += A[i][k]*B[k][j]
return C
CC = product_AB(AA, BB, NN_row, NN_column)
print(CC)
def power_matrixAn(A,N_row, n):
An = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
for i in range(N_row):
for j in range(N_row):
if i == j:
An[i][j]=1
else:
An[i][j]=0
for i in range(n):
An = product_AB(An, A, N_row, N_row)
# print("delta")
return An
def print_matrix(An,N_row,N_column):
for i in range(N_row):
for j in range (N_column):
print(An[i][j], end = " ")
print(' ')
N = 0
NN_row=2
CC2 = power_matrixAn(AAA,NN_row, N)
print("Power matrix A** ", N)
print_matrix(CC2,NN_row, NN_row)
N = 1
NN_row=2
print("Power matrix A** ", N)
CC2 = power_matrixAn(AAA,NN_row, N)
print_matrix(CC2,NN_row, NN_row)
N = 2
NN_row=2
print("Power matrix A** ", N)
CC2 = power_matrixAn(AAA,NN_row, N)
print_matrix(CC2,NN_row, NN_row)
N = 3
NN_row=2
print("Power matrix A** ", N)
CC2 = power_matrixAn(AAA,NN_row, N)
print_matrix(CC2,NN_row, NN_row)
#
# exp(A) = E/0! + A/1! + A**2/2! + A**3/3! + ...+ A**n/n!
#
def divide_const(AA, N_row, N_column, const):
AX = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
for i in range(N_row):
for j in range (N_column):
AX[i][j] = float(AA[i][j])/float(const)
return AX
CONST = 4
BX = divide_const(CC2, NN_row, NN_row, CONST)
print("Matrix divided by ", CONST)
print_matrix(BX,NN_row, NN_row)
def factorial_n(n):
facto = 1
for i in range(1,n+1):
facto *= i
return facto
def add_mat(A,B,m,n):
ADD = [[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]]
for i in range(m):
for j in range(n):
ADD[i][j] = A[i][j] + B[i][j]
return ADD
R = 5
T = factorial_n(R)
print( R, "! is ", T)
def exp_matrix(A, N_row, N_term): # calculate exp(A), A is matrix
AX = [[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]]
for i in range(1,N_term):
POW_X = power_matrixAn(A,N_row, i)
FACT_i = factorial_n(i)
DIV_X = divide_const(POW_X, N_row,N_row,FACT_i)
AX = AX + DIV_X
AX = add_mat(AX, DIV_X,NN_row, NN_row)
return AX
NN_term = 50
NN_row = 2
DDD = [[2,2],
[2,2]]
EXP_A = exp_matrix(DDD, NN_row, NN_term)
print("Matrix exp(A) wich", NN_term, " terms in sum is: ")
print_matrix(EXP_A,NN_row, NN_row)
#
# Calculate A**n, A is a matrix, n is an integer
# A has maximum 15 rows and 15 columns
#
AA = [[1,2,3,4],
[2,3,4,5],
[3,4,5,6],
[4,5,6,7]]
AAA = [[2,2],
[2,2]]
BB = [[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[2,2,2,2]]
NN_row = 4
NN_column = 4
def product_AB(A, B, N_row, N_column):
C = [[1,1,1,1],
[1,1,1,1],
[1,1,1,1],
[2,2,2,2]]
# C = [[0]*N_row]*N_column
for i in range(N_row):
for j in range(N_column):
C[i][j]=0
for k in range(N_column):
C[i][j] += A[i][k]*B[k][j]
return C
CC = product_AB(AA, BB, NN_row, NN_column)
print(CC)
def power_matrixAn(A,N_row, n):
An = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
for i in range(N_row):
for j in range(N_row):
if i == j:
An[i][j]=1
else:
An[i][j]=0
for i in range(n):
An = product_AB(An, A, N_row, N_row)
# print("delta")
return An
def print_matrix(An,N_row,N_column):
for i in range(N_row):
for j in range (N_column):
print(An[i][j], end = " ")
print(' ')
N = 0
NN_row=2
CC2 = power_matrixAn(AAA,NN_row, N)
print("Power matrix A** ", N)
print_matrix(CC2,NN_row, NN_row)
N = 1
NN_row=2
print("Power matrix A** ", N)
CC2 = power_matrixAn(AAA,NN_row, N)
print_matrix(CC2,NN_row, NN_row)
N = 2
NN_row=2
print("Power matrix A** ", N)
CC2 = power_matrixAn(AAA,NN_row, N)
print_matrix(CC2,NN_row, NN_row)
N = 3
NN_row=2
print("Power matrix A** ", N)
CC2 = power_matrixAn(AAA,NN_row, N)
print_matrix(CC2,NN_row, NN_row)
#
# exp(A) = E/0! + A/1! + A**2/2! + A**3/3! + ...+ A**n/n!
#
def divide_const(AA, N_row, N_column, const):
AX = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
for i in range(N_row):
for j in range (N_column):
AX[i][j] = float(AA[i][j])/float(const)
return AX
CONST = 4
BX = divide_const(CC2, NN_row, NN_row, CONST)
print("Matrix divided by ", CONST)
print_matrix(BX,NN_row, NN_row)
def factorial_n(n):
facto = 1
for i in range(1,n+1):
facto *= i
return facto
def add_mat(A,B,m,n):
ADD = [[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]]
for i in range(m):
for j in range(n):
ADD[i][j] = A[i][j] + B[i][j]
return ADD
R = 5
T = factorial_n(R)
print( R, "! is ", T)
def exp_matrix(A, N_row, N_term): # calculate exp(A), A is matrix
AX = [[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]]
for i in range(1,N_term):
POW_X = power_matrixAn(A,N_row, i)
FACT_i = factorial_n(i)
DIV_X = divide_const(POW_X, N_row,N_row,FACT_i)
AX = AX + DIV_X
AX = add_mat(AX, DIV_X,NN_row, NN_row)
return AX
NN_term = 50
NN_row = 2
DDD = [[2,2],
[2,2]]
EXP_A = exp_matrix(DDD, NN_row, NN_term)
print("Matrix exp(A) wich", NN_term, " terms in sum is: ")
print_matrix(EXP_A,NN_row, NN_row)
Results are:
================ RESTART: /Users/ionivan/Documents/EXP_Matrix.py ===============
[[14, 14, 14, 14], [19, 19, 19, 19], [24, 24, 24, 24], [29, 29, 29, 29]]
Power matrix A** 0
1 0
0 1
Power matrix A** 1
2 2
2 2
Power matrix A** 2
8 8
8 8
Power matrix A** 3
32 32
32 32
Matrix divided by 4
8.0 8.0
8.0 8.0
5 ! is 120
Matrix exp(A) wich 50 terms in sum is:
27.799075016572132 26.799075016572132
26.799075016572132 27.799075016572132
(March 30, 2022)
Comments
Post a Comment