from numpy import array, zeros

a= array([[4,-2,1],\
         [-2,4,-2],\
         [1,-2,4]],float)

b= array([11,-16,17],float)

n=len(b)                #number of characters in list
x=zeros(n,float)            #Returns a matrix of 0 and specified rows and columns (Creating a triangle matrix)

#Elimination

for k in range(len(a)):             #Determining row operations - (range(len(a)) is used to determine the process length for matrix a)
    for i in range(k+1,n):          #i=last row (i=k+1)
        c=a[i,k]/a[k,k]             #multiplication coefficient
        a[i][k]=0;                  #If it's already 0, move on to the next step
        for j in range(k+1,n):      #Determination of column parameter and process length
            a[i,j]=a[i,j]-c*a[k,j]      #The formula to make zero nonzero value zero (number under the pivot)
        b[i] =b[i]-c*b[k]               #Constant value matrix

print("eliminated matrix :\na =",a,"\nb =",b)


#back substitution 
 
x[n-1]=b[n-1] / a[n-1,n-1]          
for i in range(n-2,-1,-1):          #n-2!!  Python starts counting from 0, not 1 so (n-2,-1,-1)  is used instead of (n-1,0,0)      
    sum_ax=0                        #zero matrix definition
    for j in range(i+1,n):
        sum_ax += a[i,j]*x[j]       #The formula for calculating unknown values 
    x[i]=(b[i]-sum_ax)/a[i,i]       #for matrices converted into a triangular matrix form.

print('solution:x1,x2,x3',x)
 


