Skip to main content

1) Determinant 2) Inverse Matrix 3) Gaussian Elimination 4) Iterative technique and calculate using three method to find x , y, z method: cramer's rule, gaussian elimination and inverse matrix.

#include <iostream>
#include <vector>
# GaussianElimination
std::vector<std::vector<float>> GaussianElimination(std::vector<std::vector<float>> matrix)
{
  int rows = matrix.size();
  int cols = matrix[0].size();
  for (int i = 0; i < rows; i++)
  {
    float coefficient1 = matrix[i][i];
    for (int j = 0; j < cols; j++)
    {
      matrix[i][j] /= coefficient1;
    }
    for (int j = 0; j < rows; j++)
    {
      if (j == i) continue;
      float coefficient2 = matrix[j][i];
      for (int k = 0; k < cols; k++)
      {
        matrix[j][k] -=  coefficient2 * matrix[i][k];
      }
    }
  }
  return matrix;
}
void PrintMatrix(std::vector<std::vector<float>> matrix)
{
  for (int i = 0; i < matrix.size(); i++)
  {
    for (int j = 0; j < matrix[0].size(); j++)
    {
      std::cout.width(5); std::cout << matrix[i][j] << "    ";
    }
    std::cout << std::endl;
  }
}
void PrintSE(std::vector<std::vector<float>> matrix)
{
  std::cout << "X = ";
  for (int i = 0; i < matrix.size()-1; i++)
  {
    std::cout << matrix[i][matrix[0].size()-1] << ", ";
  }
  std::cout << matrix[matrix.size() - 1][matrix[0].size() - 1];
  std::cout << std::endl;
}
# Inverse matrix
void PrintInverse(std::vector<std::vector<float>> matrix)
{
  std::cout << "The inverse matrix is\n";
  int rows = matrix.size();
  int cols = matrix[0].size();
  for (int i = 0; i < rows; i++)
  {
    for (int j = rows; j < cols; j++)
    {
      std::cout.width(5); std::cout << matrix[i][j] << "    ";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
}
int main()
{
  std::cout << "Result of simultaneous equations with Gaussian Elimination" << std::endl;
  std::vector<std::vector<float>>(matrixA) = { {1, -4, -2, 21}, {2, 1, 2, 3 }, {3, 2, -1, -2} };
  std::vector<std::vector<float>>(ResultMatrixA) = GaussianElimination(matrixA);
  PrintMatrix(ResultMatrixA);
  PrintSE(ResultMatrixA);
  std::cout << std::endl;
  std::cout << std::endl;
  std::cout << "Result of inverse matrix with Gaussian Elimination" << std::endl;
  std::vector<std::vector<float>>(matrixB) = { {-1, 8, -2, 1, 0, 0}, {-6, 49, -10, 0, 1, 0}, {-4, 34, -5, 0, 0, 1} };
  std::vector<std::vector<float>>(ResultMatrixB) = GaussianElimination(matrixB);
  PrintMatrix(ResultMatrixB);
  PrintInverse(ResultMatrixB);
  std::cin.get();

}






# demterminant matrix

from copy import deepcopy
def smaller_matrix(original_matrix, row, column):
    #the new metrix should not affect the original matrix
    new_matrix = deepcopy(original_matrix)
    #the indices to do the removal depend on the original metrix
    new_matrix.remove(original_matrix[row])
    for i in range(len(new_matrix)):
        new_matrix[i].remove(new_matrix[i][column])
    return new_matrix
def determinant(matrix):
    num_rows = len(matrix)
    for row in matrix:
        if len(row) != num_rows:
            print("Not a square matrix.")
            return None
        if len(matrix) == 2:
            simple_determinant = matrix[0][0] *\
                                 matrix[1][1] -\
                                 matrix[1][0] *\
                                 matrix[0][1]
            return simple_determinant
        else:
            # cofactor expansion
            answer = 0
            num_columns = num_rows
            for j in range(num_columns):
                cofactor = (-1) ** (0+j) * matrix[0][j] \
                            * determinant(smaller_matrix(matrix,0,j))
                answer += cofactor
            return answer
a_matrix = [[2, 1, 7, 9], [7, 1, 9, 7],[8, 6, 1, 4], [0, 1, 4, 2]]
print("The answer of matirx is :",determinant(a_matrix))










 




Comments

Popular posts from this blog

AWSD controller with c#

      using   UnityEngine ;   using   System.Collections ;   using   System.Collections.Generic ;     [ RequireComponent  ( typeof ( Rigidbody ) ) ]   public   class  PlayerController  :  MonoBehaviour  {         public   float  movementSpeed  =  5 . 0f ;         void  Start  ( )   {                 }             void  Update  ( )   {           Rigidbody rigidbody  =  GetComponent < Rigidbody > ( ) ;             if ( Input . GetKey ( KeyCode . W ) )   {              transform . position   +=  Vector3 . forward   *  Time . deltaTime   *  movementSpeed ;           }     ...

Project 7 Soy Vitou (ITE G8_A2) in Royal University of Phnom Penh (RUPP)

7-segment Display An LED or Light Emitting Diode, is a solid state optical pn-junction diode which emits light energy in the form of photons. The emission of these photons occurs when the diode junction is forward biased by an external voltage allowing current to flow across its junction, and in Electronics we call this process electroluminescence. The actual colour of the visible light emitted by an LED, ranging from blue to red to orange, is decided by the spectral wavelength of the emitted light which itself is dependent upon the mixture of the various impurities added to the semiconductor materials used to produce it. 7-segment Display Light emitting diodes have many advantages over traditional bulbs and lamps, with the main ones being their small size, long life, various colours, cheapness and are readily available, as well as being easy to interface with various other electronic components and digital circuits. But the main advantage of light emitting diodes is that because of th...

Simple swipe and tap on mobile phone

 Simple swipe and tap on mobile phone   private  Vector3 fp ;     //First touch position      private  Vector3 lp ;     //Last touch position      private   float  dragDistance ;    //minimum distance for a swipe to be registered        void  Start ( )      {         dragDistance  =  Screen . height   *   15   /   100 ;   //dragDistance is 15% height of the screen      }        void  Update ( )      {          if   ( Input . touchCount   ==   1 )   // user is touching the screen with a single touch          {             Touch touch  =  Input . GetTouch ( 0 ) ;   // get the touch              i...