Матрицы:
Определитель
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// Calculating det A, where A is matrix N x N
// (c) Johna Smith, 1996
//
// Method description:
// This method based on two statements:
// 1) det A = a11, if A is matrix 1 x 1
// 2) |a11 a12 a13 ... a1n| |a11 a12 .. a1(i-1) a1(i+1) .. a1n |
// |a21 a22 a23 ... a2n| n i+1 |a21 a22 .. a2(i-1) a2(i+1) .. a2n |
// det A=|a31 a32 a33 ... a3n| = SUM (-1) a1i * det |a31 a32 .. a3(i-1) a3(i+1) .. a3n |
// |...................| i=1 |................................ |
// |...................| |an1 an2 .. an(i-1) an(i+1) .. ann |
// |an1 an2 an3 ... ann|
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <alloc.h>
#define N 5 // matrix size
int matrix[N][N]={{1,2,3,4,5},{2,1,3,4,5},{3,2,1,4,5},{4,3,2,1,5},{5,4,3,2,1}};
void show_matrix(int *matrix, int size) // this functions displays matrix
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
printf("%d ",*(matrix+i*size+j));
printf("\n");
}
printf("\n");
}
int Det(int *matrix, int const size)
{
int *submatrix;
int det=0;
if (size>1)
{
submatrix=(int *)malloc(sizeof(int)*(size-1)*(size-1));
for (int i=0;i<size;i++)
{
// creating new array (submatrix)
for (int j=0;j<size-1;j++)
for (int k=0;k<size-1;k++)
*(submatrix+j*(size-1)+k)=*(matrix+(j+1)*size+(k<i?k:k+1));
// calling recursively function Det using submatrix as a parameter
// and adding the result to final value
det+=*(matrix+i)*(i/2.0==i/2?1:-1)*Det(submatrix,size-1);
}
free(submatrix);
} else det=*matrix;
return det;
}
void main(void)
{
// show given matrix
show_matrix(matrix[0],N);
// calculating determinante and displaying the result
printf("det A = %d",Det(matrix[0],N));
Обращение
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// Matrix operations (inversion)
// (c) Johna Smith, 1996
//
// Given: A (N x N), det A != 0 -1
// This algorithm inverts matrix A and returns A .
// -1
// A*A = E
// We simply convert matrix A into matrix E, and do the same operations
// over matrix B (initially B=E). Finally A=E, B=A^(-1).
//
//////////////////////////////////////////////////////////////////////////////
#define N 3
#include <stdio.h>
float a[N][N]={{4,8,0},{8,8,8},{2,0,1}};
void Invert(float *matrix)
{
float e[N][N];
// initializing matrix e
for (int i=0;i<N;i++)
for (int j=0;j<N;j++)
e[i][j]=(i==j?1:0);
// converting matrix to e
for(i=0;i<N;i++)
{
// normalizing row (making first element =1)
float tmp=*(matrix+i*N+i);
for(int j=N-1;j>=0;j--)
{
e[i][j]/=tmp;
*(matrix+i*N+j)/=tmp;
}
// excluding i-th element from each row except i-th one
for(j=0;j<N;j++)
if (j!=i)
{
tmp=*(matrix+j*N+i);
for(int k=N-1;k>=0;k--)
{
e[j][k]-=e[i][k]*tmp;
*(matrix+j*N+k)-=*(matrix+i*N+k)*tmp;
}
}
}
// now e contains inverted matrix so we need only to copy e to matrix
for(i=0;i<N;i++)
for(int j=0;j<N;j++)
*(matrix+i*N+j)=e[i][j];
}
void show_matrix(float *matrix) // this functions displays matrix
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
printf("%f ",*(matrix+i*N+j));
printf("\n");
}
printf("\n");
}
void main(void)
{
// display matrix A
show_matrix(a[0]);
// Invert it
Invert(a[0]);
// display the inverted matrix
show_matrix(a[0]);
// Invert it again
Invert(a[0]);
// display the inversion of the inverted matrix
show_matrix(a[0]);
}
Умножение
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// Matrix operations (multiplication)
// (c) Johna Smith, 1996
//
// Given: A (M x N) and B (N x P)
// This algorithm multiplies these matrixes and display the result.
//
//////////////////////////////////////////////////////////////////////////////
#define M 3
#define N 4
#define P 2
#include <stdio.h>
int a[M][N]={{2,0,3,1},{5,1,2,0},{0,0,4,1}};
int b[N][P]={{1,3},{2,1},{4,0},{3,5}};
int c[M][P];
void show_matrix(int *matrix, int n, int m) // this functions displays matrix
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
printf("%d ",*(matrix+i*m+j));
printf("\n");
}
printf("\n");
}
void main(void)
{
// display matrixes A and B
show_matrix(a[0],M,N);
show_matrix(b[0],N,P);
// substract these matrixes
for(int i=0;i<M;i++)
for(int j=0;j<P;j++)
{
c[i][j]=0;
for (int k=0;k<N;k++) c[i][j]+=a[i][k]*b[k][j];
}
// display the difference
show_matrix(c[0],M,P);
}
Транспонирование
» Кликните сюда для просмотра оффтоп текста.. «
Код
//////////////////////////////////////////////////////////////////////////////
//
// Matrix operations (transponating)
// (c) Johna Smith, 1996
//
// Given: A (N x N), T
// This algorithm transponates matrix A and returns A .
// T
// A(ji)=A (ij)
//
//////////////////////////////////////////////////////////////////////////////
#define N 3
#include <stdio.h>
float a[N][N]={{4,8,0},{8,8,8},{2,0,1}};
void Transponate(float *matrix)
{
float swp;
for (int i=0;i<N;i++)
for (int j=i+1;j<N;j++)
{
swp=*(matrix+i*N+j);
*(matrix+i*N+j)=*(matrix+j*N+i);
*(matrix+j*N+i)=swp;
}
}
void show_matrix(float *matrix) // this functions displays matrix
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
printf("%f ",*(matrix+i*N+j));
printf("\n");
}
printf("\n");
}
void main(void)
{
// display matrix A
show_matrix(a[0]);
// Transponate it
Transponate(a[0]);
// display transponated matrix
show_matrix(a[0]);
// Transponate it again
Transponate(a[0]);
// display the transponation of the transponated matrix
show_matrix(a[0]);
}