Implementation of inverse matrix and multiplication of two matrices

Source: Internet
Author: User
The following program draws on the code of a number of experts, and is compiled and modified by xuanjicang Yishi.
 
// matrixcomputation.h
// Calculate the determining factor. Parameter 1 is the array storing the determining factor, and parameter 2 is the order.
Double calculatedeterminant (double * P, int N );
// Use the Gauss-Jordan elimination method to obtain the inverse matrix of the real matrix of the N Level
// The returned result is stored in a, and N is the order of the matrix.
Int inversematrix (double * a, int N );
// Multiply the Matrix
// Multiply matrix A [M * n] by matrix B [N * k] to obtain matrix C [M * k]
// Matrix C [M * k] is the returned result
Void multiplymatrix (double A [], double B [], int M, int N, int K, double C []);
 
// matrixcomputation.cpp
#include "matrixcomputation.h"
#include <math.h>
// Calculate the determining factor. Parameter 1 is the array storing the determining factor, and parameter 2 is the order.
Double calculatedeterminant (double * P, int N)
{
If (n <1)
{
Return-1;
}
// If the order of the determining factor is 1, the unique element value is returned.
If (n = 1)
{
Return * P;
}
// Defines the temporary pointer variable pointing to the remainder array of the current Determinant
Double * curr = new double [(n-1) * (n-1)];
Double result = 0;
Int I, R, C, CC;
 
// Calculate the remainder of column I of the first row (expanded by the first row)
For (I = 1; I <= N; ++ I)
{
For (r = 2; r <= N; ++ R)
{
Cc =-1;
For (C = 1; C <= N; ++ C)
{
If (C! = I)
{
* (Curr + (R-2) * (n-1) + (+ CC) = * (p + (R-1) * n + (c-1 ));
}
}
}
// Calculate the sum of the algebraic remainder formula and use recursive calls to calculate the value of its remainder formula
Result + = * (p + I-1) * POW (-1, 1 + I) * calculatedeterminant (curr, n-1 );
}
// Release the storage space occupied by temporary variables
Delete [] curr;
Return result;
}
// Use the Gauss-Jordan elimination method to obtain the inverse matrix of the real matrix of the N Level
// The returned result is stored in a, and N is the order of the matrix.
Int inversematrix (double * a, int N)
{
Int * is, * js, I, J, K, L, U, V;
Double D, P;
Is = new int [N];
JS = new int [N];
// Determine whether the determinant of the matrix is 0,
// If the value is 0, there is no inverse matrix.
// Otherwise, there is an inverse matrix.
Double * q = new double [N * n];
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
{
Q [I * n + J] = A [I * n + J];
}
}
 if(calculateDeterminant(q, n) == 0)
 {
  // printf("This matrix does not have inverse matrix...");
  return 0;
 }
 delete [] q;
// Start to calculate the inverse matrix
For (k = 0; k <= n-1; k ++)
{
D = 0.0;
For (I = K; I <= n-1; I ++)
{
For (j = K; j <= n-1; j ++)
{
L = I * n + J;
P = FABS (A [l]);
If (P> D)
{
D = P;
Is [k] = I;
JS [k] = J;
}
}
}
  if(is[k] != k)
  {
   for(j = 0; j <= n-1; j++)
   {
    u = k * n + j;
    v = is[k] * n + j;
    p = a[u];
    a[u] = a[v];
    a[v] = p;
   }
  }
  if(js[k] != k)
  {
   for(i = 0; i <= n-1; i++)
   {
    u = i * n + k;
    v = i * n + js[k];
    p = a[u];
    a[u] = a[v];
    a[v] = p;
   }
  }
  l = k * n + k;
  a[l] = 1.0/a[l];
  for(j = 0; j <= n-1; j++)
  {
   if(j != k)
   {
    u = k * n + j;
    a[u] = a[u] * a[l];
   }
  }
  for(i = 0; i <= n-1; i++)
  {
   if(i != k)
   {
    for(j = 0; j <= n-1; j++)
    {
     if (j != k)
     {
      u = i * n + j;
      a[u] = a[u] - a[i * n + k] * a[k * n + j];
     }
    }
   }
  }
  for(i = 0; i <= n-1; i++)
  {
   if(i != k)
   {
    u = i * n + k;
    a[u] = -a[u] * a[l];
   }
  }
 }
 for(k = n-1; k >= 0; k--)
 {
  if(js[k] != k)
  {
   for(j = 0; j <= n-1; j++)
   {
    u = k * n + j;
    v = js[k] * n + j;
    p = a[u];
    a[u] = a[v];
    a[v] = p;
   }
  }
  if(is[k] != k)
  for(i = 0; i <= n-1; i++)
  {
   u = i * n + k;
   v = i * n + is[k];
   p = a[u];
   a[u] = a[v];
   a[v] = p;
  }
 }
 delete [] is;
 delete [] js;
 return 1;
// Multiply the Matrix
// Multiply matrix A [M * n] by matrix B [N * k] to obtain matrix C [M * k]
// Matrix C [M * k] is the returned result
Void multiplymatrix (double A [], double B [], int M, int N, int K, double C [])
{
Int I, j, L, U;
For (I = 0; I <= m-1; I ++)
{
For (j = 0; j <= K-1; j ++)
{
U = I * k + J;
C [u] = 0.0;
For (L = 0; L <= n-1; l ++)
{
C [u] = C [u] + A [I * n + L] * B [L * k + J];
}
}
}
Return;
}
 
// Test code: Calculate. cpp
#include <iostream>
#include "matrixcomputation.h"
using namespace std;
int main() 
{
 const int NUM = 8;
 int i, j;
 static double a[NUM][NUM]=
 {
  {16,11,10,16,24,40,51,61},
  {12,12,14,19,26,58,60,55},
  {14,13,16,24,40,57,69,56},
  {14,17,22,29,51,87,80,62},
  {18,22,37,56,68,109,103,77},
  {24,35,55,64,81,104,113,92},
  {49,64,78,87,103,121,120,101},
  {72,92,95,98,112,100,103,99}
 };
 double b[NUM][NUM], c[NUM][NUM]; 
// B is a copy of
Memcpy (B, A, num * sizeof (double ));
// Calculate the inverse matrix
I = inversematrix (& A [0] [0], num );
 
If (I! = 0)
{
Cout <"original matrix is:" <Endl;
For (I = 0; I <num; I ++)
{
For (j = 0; j <num; j ++)
{
Printf ("% 7.2f", B [I] [J]);
}
Cout <Endl;
}
Cout <Endl;
  printf("Inverse matrix of the original matrix is:/n"); 
  for(i = 0; i < NUM; i++)
  {
   for(j = 0; j < NUM; j++)
   {
    printf("%7.2f ",a[i][j]);
   }
   cout << endl;
  }
  cout << endl;
// Output the product of the original matrix and its inverse matrix
Printf ("multiplication of the original matrix and its inverse matrix is:/N ");
Multiplymatrix (& B [0] [0], & A [0] [0], num, & C [0] [0]);
For (I = 0; I <num; I ++)
{
For (j = 0; j <num; j ++)
{
Printf ("% 7.2f", C [I] [J]);
}
Printf ("/N ");
}
}
 return 0; 
}
Calculation Result:
/*
Original matrix is:
16.00 11.00 10.00 16.00 24.00 40.00 51.00
12.00 12.00 14.00 19.00 26.00 58.00 60.00
14.00 13.00 16.00 24.00 40.00 57.00 69.00
14.00 17.00 22.00 29.00 51.00 87.00 80.00
18.00 22.00 37.00 56.00 68.00 109.00 103.00
24.00 35.00 55.00 64.00 81.00 104.00 113.00
49.00 64.00 78.00 87.00 103.00 121.00 120.00
72.00 92.00 95.00 98.00 112.00 100.00 103.00
Inverse matrix of the original matrix is:
   0.10   -0.12    0.14   -0.07   -0.16   -0.27    0.64   -0.31
  -0.16    0.20   -0.09    0.07    0.18    0.23   -0.72    0.37
   0.06   -0.06    0.02   -0.03   -0.21   -0.11    0.53   -0.28
  -0.02    0.04   -0.03   -0.05    0.15    0.05   -0.25    0.13
   0.04   -0.11   -0.00    0.07   -0.00    0.02   -0.04    0.02
   0.01    0.00   -0.05    0.03   -0.00   -0.03    0.05   -0.03
  -0.07    0.07    0.11   -0.06   -0.02   -0.02    0.04   -0.02
   0.05   -0.01   -0.08    0.03    0.03    0.06   -0.11    0.05
Multiplication of the original matrix and its inverse matrix is:
   1.00   -0.00    0.00    0.00    0.00   -0.00    0.00   -0.00
  -0.00    1.00    0.00    0.00    0.00    0.00   -0.00   -0.00
   0.00   -0.00    1.00    0.00    0.00   -0.00    0.00   -0.00
   0.00   -0.00    0.00    1.00   -0.00   -0.00    0.00   -0.00
  -0.00   -0.00    0.00   -0.00    1.00   -0.00    0.00   -0.00
   0.00   -0.00    0.00    0.00   -0.00    1.00    0.00   -0.00
  -0.00   -0.00    0.00    0.00    0.00   -0.00    1.00   -0.00
  -0.00   -0.00   -0.00   -0.00    0.00    0.00    0.00    1.00
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.