Matrix calculation with c++

# Matrix calculation c++ code
I'll post the whole code first and then explain.
[Inverse Matrix](#inverse-matrix)
[Transposed Matrix](#transposed-matrix)
[Identity Matrix](#identity-matrix)
[Matrix Addition](#3-x-3-matrix-addition)
[3X3 Matrix Multiplication](#3-x-3-matrix-multiplication)
[3X3 Matrix 3X1 Matrix Multiplication](#3-x-3-matrix-3-x-1-matrix-multiplication)
[COUT 3X3 Matrix](#cout-3-x-3-matrix)
[COUT 3X1 Matrix](#cout-3-x-1-matrix)

```c++
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

/*----------------------- Display on terminal -------------------------*/
void cout_33matrix(double A[3][3])
{
  int i, j; 
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      cout << scientific<< setprecision(2)<< A[i][j] << " ";
    }
    cout << endl;
  }
}

void cout_31matrix(double ACmul[3][1])
{
  int i, j;
 
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 1; j++) {
      cout << scientific<< setprecision(2)<< ACmul[i][j] << " ";
    }
    cout << endl;
  }
}

/*--------------------------- Calculation -----------------------------*/
void calculator(void)
{   //For example
    double A[3][3]={
                        { 1, 3, 2},
                        {-1, 0, 1},
                        { 2, 3, 0},
    };
    double B[3][3]={
                        { 7, 1, 5},
                        { 4, 3, 1},
                        { 0, 2, 1},
    };
    double C[3][1]={
                      {2},
                      {3},
                      {5},
    };
    cout << "A = "<<endl;
    cout_33matrix(A);
    cout << "B = "<<endl;
    cout_33matrix(B);
    cout << "C = "<<endl;
    cout_31matrix(C);

    int i,j,k;

/*Inverse matrix*/
    double Ainverse[3][3];
    double det = A[0][0] * A[1][1] * A[2][2]
               + A[0][1] * A[1][2] * A[2][0]
               + A[0][2] * A[1][0] * A[2][1]
               - A[0][2] * A[1][1] * A[2][0]
               - A[0][1] * A[1][0] * A[2][2]
               - A[0][0] * A[1][2] * A[2][1];
    Ainverse[0][0] = (A[1][1] * A[2][2] - A[1][2] * A[2][1]) / det;
    Ainverse[0][1] = - (A[0][1] * A[2][2] - A[0][2] * A[2][1]) / det;
    Ainverse[0][2] = (A[0][1] * A[1][2] - A[0][2] * A[1][1]) / det;
    Ainverse[1][0] = - (A[1][0] * A[2][2] - A[1][2] * A[2][0]) / det;
    Ainverse[1][1] = (A[0][0] * A[2][2] - A[0][2] * A[2][0]) / det;
    Ainverse[1][2] = - (A[0][0] * A[1][2] - A[0][2] * A[1][0]) / det;
    Ainverse[2][0] = (A[1][0] * A[2][1] - A[1][1] * A[2][0]) / det;
    Ainverse[2][1] = - (A[0][0] * A[2][1] - A[0][1] * A[2][0]) / det;
    Ainverse[2][2] = (A[0][0] * A[1][1] - A[0][1] * A[1][0]) / det;
    cout << "Ainverse = "<<endl;
    cout_33matrix(Ainverse);

/*Transposed matrix*/
    double Atransposed[3][3];
    for (i = 0; i < 3; i++){
        for (j = 0; j < 3; j++){
            Atransposed[j][i]=A[i][j];
        }
    }
    cout << "Atransposed = "<<endl;
    cout_33matrix(Atransposed);

/*Identity matrix*/
    double Aidentity[3][3];
    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        Aidentity[i][j]=0.0;
        for(k=0;k<3;k++){
          Aidentity[i][j]+=A[i][k]*Ainverse[k][j];
        }
      }
    }
    cout << "Aidentity = "<<endl;
    cout_33matrix(Aidentity);

/*A + B*/
    double ABsum[3][3];  
    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        ABsum[i][j]=0.0;
        ABsum[i][j]=A[i][j]+B[i][j];
      }
    }
    cout << "ABsum = "<<endl;
    cout_33matrix(ABsum);

/*A X B*/
    double ABmul[3][3];  
    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        ABmul[i][j]=0.0;
        for(k=0;k<3;k++){
          ABmul[i][j]+=A[i][k]*B[k][j];
        }
      }
    }
    cout << "ABmul = "<<endl;
    cout_33matrix(ABmul);

/*A X C*/
    double ACmul[3][1]; 
    for(i=0;i<3;i++){
      for(j=0;j<1;j++){
        ACmul[i][j]=0.0;
        for(k=0;k<3;k++){
          ACmul[i][j]+=A[i][k]*C[k][j];
        }
      }
    }
    cout << "ACmul = "<<endl;
    cout_31matrix(ACmul);

}

int main(void)
{   
    calculator();
    return 0;
}
```

# Inverse Matrix
```c++
    double det = A[0][0] * A[1][1] * A[2][2]
               + A[0][1] * A[1][2] * A[2][0]
               + A[0][2] * A[1][0] * A[2][1]
               - A[0][2] * A[1][1] * A[2][0]
               - A[0][1] * A[1][0] * A[2][2]
               - A[0][0] * A[1][2] * A[2][1];
    Ainverse[0][0] = (A[1][1] * A[2][2] - A[1][2] * A[2][1]) / det;
    Ainverse[0][1] = - (A[0][1] * A[2][2] - A[0][2] * A[2][1]) / det;
    Ainverse[0][2] = (A[0][1] * A[1][2] - A[0][2] * A[1][1]) / det;
    Ainverse[1][0] = - (A[1][0] * A[2][2] - A[1][2] * A[2][0]) / det;
    Ainverse[1][1] = (A[0][0] * A[2][2] - A[0][2] * A[2][0]) / det;
    Ainverse[1][2] = - (A[0][0] * A[1][2] - A[0][2] * A[1][0]) / det;
    Ainverse[2][0] = (A[1][0] * A[2][1] - A[1][1] * A[2][0]) / det;
    Ainverse[2][1] = - (A[0][0] * A[2][1] - A[0][1] * A[2][0]) / det;
    Ainverse[2][2] = (A[0][0] * A[1][1] - A[0][1] * A[1][0]) / det;
```
# Transposed Matrix
```cpp
    for (i = 0; i < 3; i++){
        for (j = 0; j < 3; j++){
            Atransposed[j][i]=A[i][j];
        }
    }
```

# Identity Matrix
```cpp
    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        Aidentity[i][j]=0.0;
        for(k=0;k<3;k++){
          Aidentity[i][j]+=A[i][k]*Ainverse[k][j];
        }
      }
    }
```
# 3 X 3 Matrix Addition
```cpp
    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        ABsum[i][j]=0.0;
        ABsum[i][j]=A[i][j]+B[i][j];
      }
    }
```

# 3 X 3 Matrix Multiplication
```cpp
    for(i=0;i<3;i++){
      for(j=0;j<3;j++){
        ABmul[i][j]=0.0;
        for(k=0;k<3;k++){
          ABmul[i][j]+=A[i][k]*B[k][j];
        }
      }
    }
```

# 3 X 3 Matrix 3 X 1 Matrix Multiplication
```cpp
    for(i=0;i<3;i++){
      for(j=0;j<1;j++){
        ACmul[i][j]=0.0;
        for(k=0;k<3;k++){
          ACmul[i][j]+=A[i][k]*C[k][j];
        }
      }
    }
```

# COUT 3 X 3 Matrix
```cpp
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      cout << scientific<< setprecision(2)<< A[i][j] << " ";
    }
    cout << endl;
  }
```

# COUT 3 X 1 Matrix
```cpp
  for (i = 0; i < 3; i++) {
    for (j = 0; j < 1; j++) {
      cout << scientific<< setprecision(2)<< ACmul[i][j] << " ";
    }
    cout << endl;
  }
```

---
<font color="yellowgreen">**QiitaLink**</font>
<font color="black">**Designed by RENOX**</font>