My linear algebra library (the value of the inverse matrix, determinant)

#include "stdafx.h"

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

// N可以随意指定 #define N 4 void main() { double a[N][N]; double L[N][N], U[N][N]; double r[N][N], u[N][N]; double out[N][N], out1[N][N], E[N][N]; memset(a, 0, sizeof(a)); // 初始化 memset(L, 0, sizeof(L)); memset(U, 0, sizeof(U)); memset(r, 0, sizeof(r)); memset(u, 0, sizeof(u)); int n = N; int k,i,j; double s,t; srand(time(0)); // 输入矩阵 printf("input A=\n"); for(i=0;i<n;i++){ for(j=0;j<n;j++){ //scanf("%f", &a[i][j]); do{ a[i][j] = rand() % 100; }while(a[i][j] == 0); } } the printf ( " input matrix: \ n- " ); for (I = 0 ; I <n-; I ++ ) { for (J = 0 ; J <n-; J ++ ) { printf(" %lf", a[i][j]); } printf("\n"); } // first row of the matrix U unchanged // for (J = 0; J <n-; J ++) { // A [0] [J] = A [0] [J]; // Calculation of the matrix U line // } for (I = . 1 ; I <n-; I ++ ) { A [I] [ 0 ] = A [I] [ 0 ] / A [ 0 ] [ 0 ]; // first column of matrix U computing } for(k=1;k<n;k++){ for(j=k;j<n;j++){ s = 0; for(i=0;i<k;i++){ s += a[k][i] * a[i][j]; //累加 } A [K] [J] = A [K] [J] - S; // other matrix elements calculated U } for (I = K + . 1 ; I <n-; I ++ ) { t = 0; for(j=0;j<k;j++){ T + A = [I] [J] * A [J] [K]; // accumulating } A [I] [K] = (A [I] [K] - T) / A [K] [K]; // Other matrix element calculation L } } for (I = 0 ; I <n-; I ++ ) { for (J = 0 ; J <n-; J ++ ) { IF (I> J) { // if i> j, description line is greater than columns, calculated by the following triangular matrix portion, obtained value of L, U is 0 L [I] [J] = a [I] [J]; U[i][j] = 0; }else if(i == j){ U[i][j] = a[i][j]; L[i][j] = 1; } The else { // Otherwise i <j rows than the column description, the upper triangular part of the matrix calculation, the value of U derived, L is 0 U [I] [J] = A [I] [J]; L[i][j] = 0; } } } double temp = 1.0; for(i=0;i<n;i++){ temp *= U[i][i]; } if(temp == 0){ the printf ( " \ n-matrix does not exist " ); return ; } // inverse matrix of L and U // find the inverse matrix U for (I = 0 ; I <n-; I ++ ) { U [I] [I] = . 1 / the U-[I] [I]; // value of the inverse of the diagonal elements of the direct access for (K = I- . 1 ; K> = 0 ; K-- ) { s = 0; for(j=k+1;j<=i;j++){ s += U[k][j] * u[j][i]; } U [K] [I] = -s / the U-[K] [K]; // iteration, once obtained by the reverse of each column value } } // find the inverse matrix L for (I = 0 ; I <n-; I ++ ) { R & lt [I] [I] = . 1 ; // value of the diagonal elements directly taking the reciprocal of, here. 1 for (K = I + . 1 ; K <n-; K ++ ) { for (J = I; J <= - K- . 1 ; J ++ ) { R & lt [K] [I] - = L [K] [J] R & lt * [J] [I]; // iteration, once obtained sequentially according to each of the column value } } } // Print L, the U- the printf ( " \ nL matrix: " ); for (I = 0 ; I <n-; I ++ ) { printf("\n"); for(j=0;j<n;j++){ printf(" %lf", L[i][j]); } } printf("\nU矩阵:"); for(i=0;i<n;i++){ printf("\n"); for(j=0;j<n;j++){ printf(" %lf", U[i][j]); } } printf("\n"); // Print L, U inverse matrix of the printf ( " \ nL inverse of a matrix: " ); for (I = 0 ; I <n-; I ++ ) { printf("\n"); for(j=0;j<n;j++){ printf(" %lf", r[i][j]); } } the printf ( " \ nU inverse of a matrix: " ); for (I = 0 ; I <n-; I ++ ) { printf("\n"); for(j=0;j<n;j++){ printf(" %lf", u[i][j]); } } printf("\n"); // validate multiplying U and L, the original matrix to obtain the printf ( " \ nL matrix and the matrix product U \ n- " ); for (I = 0 ; I <n-; I ++ ) { for (J = 0 ; J <n-; ++ J ) { OUT [I] [J] = 0 ; } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ for(k=0;k<n;k++){ out[i][j] += L[i][k]*U[k][j]; } } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf(" %lf", out[i][j]); } printf("\n"); } // The r and u multiplied by the inverse matrix of the printf ( " \ n inverse matrix of the original matrix \ n " ); for (I = 0 ; I <n; I ++ ) { for (J = 0 ; J <n; ++ J ) { out1[i][j] = 0; } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ for(k=0;k<n;k++){ out1[i][j] += u[i][k]*r[k][j]; } } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf(" %lf", out1[i][j]); } printf("\n"); } // validate the printf ( " \ n-former matrix and the inverse matrix product \ n- " ); for (I = 0 ; I <n-; I ++ ) { for (J = 0 ; J <n-; J ++ ) { E[i][j] = 0; } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ for(k=0;k<n;k++){ E[i][j] += out[i][k]*out1[k][j]; } } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf(" %lf", E[i][j]); } printf("\n"); } double det = 1; for(i=0;i<n;i++){ the * = U [i] [i]; } the printf ( " \ nA% of the determinant F = \ n- " , DET); }

 

#include "stdafx.h"
#include <stdlib.h>#include <stdio.h>#include <time.h>#include <string.h>
#define N 4
void main(){double a[N][N];double L[N][N], U[N][N];double r[N][N], u[N][N];double out[N][N], out1[N][N], E[N][N];memset(a, 0, sizeof(a));// 初始化memset(L, 0, sizeof(L));memset(U, 0, sizeof(U));memset(r, 0, sizeof(r));memset(u, 0, sizeof(u));int n = N;int k,i,j;double s,t;srand(time(0));
// 输入矩阵printf("input A=\n");for(i=0;i<n;i++){for(j=0;j<n;j++){//scanf("%f", &a[i][j]);do{a[i][j] = rand() % 100;}while(a[i][j] == 0);}}printf("输入矩阵:\n");for(i=0;i<n;i++){for(j=0;j<n;j++){printf(" %lf", a[i][j]);}printf("\n");}
The first row of the constant matrix U // // for (j = 0; j <n; j ++) {// a [0] [j] = a [0] [j]; // Calculation of the matrix U line //} for (i = 1; i <n; i ++) {a [i] [0] = a [i] [0] / a [0] [0]; // calculate a first matrix U column} for (k = 1; k <n; k ++) {for (j = k; j <n; j ++) {s = 0; for (i = 0; i <k; i ++) {s + = a [ k] [i] * a [ i] [j]; // accumulating} a [k] [j] = a [k] [j] - s; // calculate other elements of the matrix U} for (i = k +1; i <n; i ++ ) {t = 0; for (j = 0; j <k; j ++) {t + = a [i] [j] * a [j] [k]; // accumulating} a [i] [k] = (a [i] [k] - t) / a [k] [k]; other elements // L is calculated matrices}}
for (I = 0; I <n-; I ++) {for (j = 0; j <n; j ++) {if (i> j) {// if i> j, is greater than the described rows columns, calculated by the following matrix of triangular section, the value of L obtained, U is 0L [i] [j] = a [i] [j]; U [i] [j] = 0;} else if (i == j) {U [i] [j] = a [i] [j] ; L [i] [j] = 1;} else {// otherwise i <j rows than the column description, the triangular part of the matrix calculation, the value of U derived, L is 0U [i] [j] = a [ I] [J]; L [I] [J] = 0;}}}
Double TEMP = 1.0; for (I = 0; I <n-; I ++) {TEMP * = the U-[I] [I];} IF (temp == 0) {printf ( "\ n matrix does not exist"); return;}

// inverse matrices L and U
// find the inverse matrix U for (i = 0; i < n; i ++) {u [i] [i] = 1 / U [i] [i]; // value of the diagonal elements of the inverse of the direct access for ( k = i-1; k> = 0; k -) {s = 0; for (j = k + 1; j <= i; j ++) {s + = U [k] [j] * u [j ] [i];} u [ k] [i] = -s / U [k] [k]; // iteration, once obtained by the reverse of each column value}}
// find the inverse matrix L for (i = 0; i <n; i ++) {r [i] [i] = 1; // value of the diagonal elements directly taking the reciprocal of, here 1for (k = i + 1; k <n; k ++) {for ( j = i; j <= k -1; j ++) {r [k] [i] - = L [k] [j] * r [j] [i]; iteration // in columns sequentially once obtained per a value}}}

// Print L, uprintf ( "\ nL matrix:"); for (i = 0; i <n; i ++) {printf ( "\ n"); for (j = 0; j <n ; j ++) {printf ( " % lf", L [i] [j]);}} printf ( "\ nU matrix:"); for (i = 0; i <n; i ++) {printf ( "\ n "); for (J = 0; J <n-; J ++) {the printf ("% LF ", U [I] [J]);}} the printf (" \ n-");
// Print L, the inverse of U matrix printf ( "\ nL inverse of a matrix:"); for (i = 0; i <n; i ++) {printf ( "\ n"); for (j = 0; j <n; j ++) {printf ( "% lf", r [i ] [j]);}} printf ( "\ nU inverse matrix of the matrix:"); for (i = 0; i <n;i++){printf("\n");for(j=0;j<n;j++){printf(" %lf", u[i][j]);}}printf("\n");
// validate multiplying U and L, to obtain the original matrix printf ( "\ nL matrix and the matrix product U \ n"); for (i = 0; i <n; i ++) {for (j = 0; j <n; j ++) {out [i] [j] = 0;}} for (i = 0; i <n; i ++) {for (j = 0; j <n; j ++) {for (k = 0; k <n ; k ++) {out [i ] [j] + = L [i] [k] * U [k] [j];}}} for (i = 0; i <n; i ++) {for (j = 0 ; J <n-; J ++) {the printf ( "% LF", OUT [I] [J]);} printf ( "\ n-");}
// multiplying r and u, to obtain an inverse matrix printf ( "\ an inverse matrix of n original matrix \ n "); for (i = 0; i <n; i ++) {for (j = 0; j <n; j ++) {out1 [i] [j] = 0;}} for (i = 0; i <n ; i ++) {for (j = 0; j <n; j ++) {for (k = 0; k <n; k ++) {out1 [i] [j] + = u [i ] [k] * r [k ] [j];}}} for (i = 0; i <n; i ++) {for (j = 0; j <n; j ++) {printf ( "% lf", out1 [I] [J]);} printf ( "\ n");}
// verify printf ( "\ n of the original matrix and the inverse matrix product \ n"); for (i = 0; i <n; i ++) {for (j = 0; j <n; j ++) {E [i] [j] = 0;}} for (i = 0; i <n; i ++) {for (j = 0; j <n; j ++ ) {for (k = 0; k <n; k ++) {E [i] [j] + = out [i] [k] * out1 [k] [j];}}} for (i = 0; i <n; i ++) {for (j = 0; j <n;j++){printf(" %lf", E[i][j]);}printf("\n");}
double det = 1; for (i = 0; i <n; i ++) {det * = U [i] [i];} printf ( "\ nA determinant value =% f \ n", det);}

 

Guess you like

Origin www.cnblogs.com/litandy2016/p/11839409.html