matrices & determinantsimetrics.co.jp/mathematics/matricesanddeterminants.pdf · 2016-06-02 ·...

19
Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

Upload: others

Post on 10-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

Matrices & Determinants

行列の積と行列式をc言語で計算S. Kusafusa

Page 2: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

行列式の計算

Page 3: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

Determinant(3 x 3 行列式の計算)

Page 4: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

3x 3det関数

Page 5: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

(4x 4 行列式の計算)

Page 6: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

6

4 x 4 det関数

Page 7: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

A B C D1 1 2 3 -1

2 0 1 -2 5

3 2 3 0 24 -2 2 4 1

EXCELによる 4 x 4 行列式の計算

= MDETERM(A1:D4) = 50

Page 8: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

行列式をC言語で解いてみよう

#include <stdio.h> int main(void) { int a[3][3],i,j; int determinant=0;

printf("Enter the 9 elements of matrix: "); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]);

printf("\nThe First matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",a[i][j]); }

for(i=0;i<3;i++) determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

printf("\nDeterminant of matrix is: %d",determinant);

return 0; }

Page 9: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

3 x 3 Determinant

Page 10: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

行列の積と 逆行列式の計算

Page 11: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

#include <stdio.h> main() { int i, j, matrixA[3][3], matrixB[3][3], sum[3][3];

//行列Aの入力 printf("Input of matrixA \n"); for(i=0;i<3;i++) { printf(" row %d: ",i+1); scanf("%d %d %d",&matrixA[i][0], &matrixA[i][1], &matrixA[i][2]); }

//行列Bの入力 printf("Input of matrixB \n"); for(i=0;i<3;i++) { printf(" row %d: ",i+1); scanf("%d %d %d",&matrixB[i][0], &matrixB[i][1], &matrixB[i][2]); }

//行列の積の計算 for(i=0;i < 3;i++) { for(j=0;j < 3;j++) sum[i][j] = matrixA[i][0]*matrixB[0][j] + matrixA[i][1]*matrixB[1][j] + matrixA[i][2]*matrixB[2][j]; }

//行列の出力 printf("Product AB :\n"); for(i=0;i < 3;i++) { for(j=0;j < 3;j++) printf("%5d",sum[i][j]); printf("\n"); } }

行列の積をC言語で解いてみよう

Page 12: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa
Page 13: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

行列の積

Page 14: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

#include <stdio.h>

#define N 3

void matrixmultiply(double a[N][N],double b[N][N],double c[N][N]);

int main() { int i,j; double A[N][N],B[N][N],C[N][N];

//計算を行う行列の値を格納する。 for(i=0;i<N;i++) { for(j=0;j<N;j++) { A[i][j]=B[i][j]=i+j; C[i][j]=0.0; } }

matrixmultiply(A,B,C); //関数を呼び出し行列の掛け算を行う。

//結果を表示する。 for(i=0;i<N;i++) { for(j=0;j<N;j++) { printf("%f ",C[i][j]); } printf("\n"); }

return 0; }

//掛け算を行う行列2つと、結果を入れる行列を引数として受け取る。 void matrixmultiply(double a[N][N],double b[N][N],double c[N][N]) { int i,j,k;

//受け取った2つの行列の掛け算を行う。 for(i=0;i<N;i++) { for(j=0;j<N;j++) { for(k=0;k<N;k++) { c[i][j]+=a[i][k]*b[k][j]; } } } }

Page 15: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

結果は 5.000000 8.000000 11.000000 8.000000 14.000000 20.000000 11.000000 20.000000 29.000000

Page 16: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

逆行列

det A = 0 を除く

Page 17: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

#include <iostream> #include <math.h> using namespace std;

int main() { double a[4][4]={{1,2,0,-1},{-1,1,2,0},{2,0,1,1},{1,-2,-1,1}}; //入力用の配列 double inv_a[4][4]; //ここに逆行列が入る double buf; //一時的なデータを蓄える int i,j,k; //カウンタ int n=4; //配列の次数

//単位行列を作る for(i=0;i<n;i++){ for(j=0;j<n;j++){ inv_a[i][j]=(i==j)?1.0:0.0; } } //掃き出し法 for(i=0;i<n;i++){ buf=1/a[i][i]; for(j=0;j<n;j++){ a[i][j]*=buf; inv_a[i][j]*=buf; } for(j=0;j<n;j++){ if(i!=j){ buf=a[j][i]; for(k=0;k<n;k++){ a[j][k]-=a[i][k]*buf; inv_a[j][k]-=inv_a[i][k]*buf; } } } }

//逆行列を出力 for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf(" %f",inv_a[i][j]); } printf("\n"); } }

/* 出力 2.000000 2.000000 -1.000000 3.000000 -4.000000 -5.000000 3.000000 -7.000000 3.000000 4.000000 -2.000000 5.000000 -7.000000 -8.000000 5.000000 -11.000000 */

逆行列式の計算

Page 18: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

逆行列式の計算

Page 19: Matrices & Determinantsimetrics.co.jp/mathematics/MatricesAndDeterminants.pdf · 2016-06-02 · Matrices & Determinants 行列の積と行列式をc言語で計算 S. Kusafusa

数学体験塾

問い合わせ

S. Kusafusa

[email protected]