System of Linear Equations in three variables using Cramer 's Rule | This functions finds the determinant of Matrix ; This function finds the solution of system of linear equations using cramer 's rule ; Matrix d using coeff as given in cramer 's rule ; Matrix d1 using coeff as given in cramer 's rule ; Matrix d2 using coeff as given in cramer 's rule ; Matrix d3 using coeff as given in cramer 's rule ; Calculating Determinant of Matrices d , d1 , d2 , d3 ; Case 1 ; Coeff have a unique solution . Apply Cramer 's Rule ; calculating z using cramer 's rule ; Case 2 ; Driver Code ; storing coefficients of linear equations in coeff matrix
def determinantOfMatrix ( mat ) : NEW_LINE INDENT ans = ( mat [ 0 ] [ 0 ] * ( mat [ 1 ] [ 1 ] * mat [ 2 ] [ 2 ] - mat [ 2 ] [ 1 ] * mat [ 1 ] [ 2 ] ) - mat [ 0 ] [ 1 ] * ( mat [ 1 ] [ 0 ] * mat [ 2 ] [ 2 ] - mat [ 1 ] [ 2 ] * mat [ 2 ] [ 0 ] ) + mat [ 0 ] [ 2 ] * ( mat [ 1 ] [ 0 ] * mat [ 2 ] [ 1 ] - mat [ 1 ] [ 1 ] * mat [ 2 ] [ 0 ] ) ) NEW_LINE return ans NEW_LINE DEDENT def findSolution ( coeff ) : NEW_LINE INDENT d = [ [ coeff [ 0 ] [ 0 ] , coeff [ 0 ] [ 1 ] , coeff [ 0 ] [ 2 ] ] , [ coeff [ 1 ] [ 0 ] , coeff [ 1 ] [ 1 ] , coeff [ 1 ] [ 2 ] ] , [ coeff [ 2 ] [ 0 ] , coeff [ 2 ] [ 1 ] , coeff [ 2 ] [ 2 ] ] ] NEW_LINE d1 = [ [ coeff [ 0 ] [ 3 ] , coeff [ 0 ] [ 1 ] , coeff [ 0 ] [ 2 ] ] , [ coeff [ 1 ] [ 3 ] , coeff [ 1 ] [ 1 ] , coeff [ 1 ] [ 2 ] ] , [ coeff [ 2 ] [ 3 ] , coeff [ 2 ] [ 1 ] , coeff [ 2 ] [ 2 ] ] ] NEW_LINE d2 = [ [ coeff [ 0 ] [ 0 ] , coeff [ 0 ] [ 3 ] , coeff [ 0 ] [ 2 ] ] , [ coeff [ 1 ] [ 0 ] , coeff [ 1 ] [ 3 ] , coeff [ 1 ] [ 2 ] ] , [ coeff [ 2 ] [ 0 ] , coeff [ 2 ] [ 3 ] , coeff [ 2 ] [ 2 ] ] ] NEW_LINE d3 = [ [ coeff [ 0 ] [ 0 ] , coeff [ 0 ] [ 1 ] , coeff [ 0 ] [ 3 ] ] , [ coeff [ 1 ] [ 0 ] , coeff [ 1 ] [ 1 ] , coeff [ 1 ] [ 3 ] ] , [ coeff [ 2 ] [ 0 ] , coeff [ 2 ] [ 1 ] , coeff [ 2 ] [ 3 ] ] ] NEW_LINE D = determinantOfMatrix ( d ) NEW_LINE D1 = determinantOfMatrix ( d1 ) NEW_LINE D2 = determinantOfMatrix ( d2 ) NEW_LINE D3 = determinantOfMatrix ( d3 ) NEW_LINE print ( " D ▁ is ▁ : ▁ " , D ) NEW_LINE print ( " D1 ▁ is ▁ : ▁ " , D1 ) NEW_LINE print ( " D2 ▁ is ▁ : ▁ " , D2 ) NEW_LINE print ( " D3 ▁ is ▁ : ▁ " , D3 ) NEW_LINE if ( D != 0 ) : NEW_LINE INDENT x = D1 / D NEW_LINE y = D2 / D NEW_LINE z = D3 / D NEW_LINE print ( " Value ▁ of ▁ x ▁ is ▁ : ▁ " , x ) NEW_LINE print ( " Value ▁ of ▁ y ▁ is ▁ : ▁ " , y ) NEW_LINE print ( " Value ▁ of ▁ z ▁ is ▁ : ▁ " , z ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( D1 == 0 and D2 == 0 and D3 == 0 ) : NEW_LINE INDENT print ( " Infinite ▁ solutions " ) NEW_LINE DEDENT elif ( D1 != 0 or D2 != 0 or D3 != 0 ) : NEW_LINE INDENT print ( " No ▁ solutions " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT coeff = [ [ 2 , - 1 , 3 , 9 ] , [ 1 , 1 , 1 , 6 ] , [ 1 , - 1 , 1 , 2 ] ] NEW_LINE findSolution ( coeff ) NEW_LINE DEDENT