はじめに
この実験では、C 言語で行列の乗算プログラムを作成します。行列を作成し、行列演算を行い、行列乗算の標準アルゴリズムについて説明します。2 つの行列を乗算し、結果の行列を表示する C プログラムを作成します。
注:コーディングを練習し、gcc を使ってコンパイルと実行する方法を学ぶために、自分で
~/project/main.cファイルを作成する必要があります。
cd ~/project
## main.cを作成する
touch main.c
## main.cをコンパイルする
gcc main.c -o main
## mainを実行する
./main
行列の作成
まず、行列を作成する方法を学びましょう。行列は二次元配列です。C 言語で 2x2 の行列を宣言および初期化する方法の例を以下に示します。
int matrix[2][2] = {{1, 2}, {3, 4}};
次の例のように、2 つのインデックスを使用して行列の特定の要素にアクセスすることができます。
int element = matrix[1][0];
スカラー値による行列の乗算
1 つの値による行列の乗算は簡単で、以下のように行われます。
int arr[2][2], n;
// スカラー値を入力する
scanf("%d", &n);
// 行列の要素を入力する
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++){
scanf("%d", &arr[i][j]);
}
}
// 行列の各要素にスカラー値を乗算する
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++){
arr[i][j] = arr[i][j] * n;
}
}
// 更新された行列の要素を表示する
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
このプログラムは、ユーザーが入力したスカラー値で行列の各要素を乗算し、その後更新された行列を表示します。
2 つの行列による行列の乗算
2 つの行列を乗算するには、特定のルールに従う必要があります。まず、最初の行列の列数は、2 番目の行列の行数と等しくなければなりません。2 番目に、結果の行列は、最初の行列と同じ行数と、2 番目の行列と同じ列数を持つことになります。
2 つの行列があるとしましょう。
A = {{1, 3, 5},
{4, 2, 6}};
B = {{7, 4},
{3, 1},
{6, 9}};
これら 2 つの行列を乗算するには、行と列のドット積を行います。行列の要素は、次の式を使って乗算されます。
result[row][col] = matrix1[row][0] * matrix2[0][col]
+ matrix1[row][1] * matrix2[1][col]
+ matrix1[row][2] * matrix2[2][col]
+...;
以下は、行列乗算に関する C プログラムです。
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
// 2 つの行列を定義する
int first[10][10], second[10][10], result[10][10];
// 最初の行列の行数と列数を入力する
printf("Enter the number of rows and columns of the first matrix:\n");
scanf("%d %d", &m, &n);
// 最初の行列の要素を入力する
printf("Enter the %d elements of the first matrix:\n", m * n);
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
scanf("%d", &first[c][d]);
}
}
// 2 番目の行列の行数と列数を入力する
printf("Enter the number of rows and columns of the second matrix:\n");
scanf("%d %d", &p, &q);
// 2 番目の行列の要素を入力する
printf("Enter the %d elements of the second matrix:\n", p * q);
for (c = 0; c < p; c++) {
for (d = 0; d < q; d++) {
scanf("%d", &second[c][d]);
}
}
// 行列を乗算できるかどうかを確認する
if (n!= p) {
printf("Matrices cannot be multiplied with each other.\n");
} else {
// 行列を乗算する
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k] * second[k][d];
}
result[c][d] = sum;
sum = 0;
}
}
// 結果の行列を表示する
printf("The resulting matrix is:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
printf("%d\t", result[c][d]);
}
printf("\n");
}
}
return 0;
}
このプログラムは、ユーザーに 2 つの行列の要素を入力してもらい、乗算できるかどうかを確認し、可能であれば乗算し、結果の行列を表示します。
完全なプログラム
以下は、すべての手順を組み合わせた完全なプログラムです。
#include<stdio.h>
int main()
{
int n, m, c, d, p, q, k, first[10][10], second[10][10], pro[10][10], sum = 0;
printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix: \n\n", m*n);
for(c = 0; c < m; c++) {
for(d = 0; d < n; d++) {
scanf("%d", &first[c][d]);
}
}
printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &p, &q);
if(n!= p)
printf("Matrices with the given order cannot be multiplied with each other.\n\n");
else {
printf("\nEnter the %d elements of the second matrix: \n\n",m*n);
for(c = 0; c < p; c++) {
for(d = 0; d < q; d++) {
scanf("%d", &second[c][d]);
}
}
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) {
for(d = 0; d < n; d++) {
printf("%d\t", first[c][d]);
}
printf("\n");
}
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < p; c++) {
for(d = 0; d < q; d++){
printf("%d\t", second[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++) {
for(d = 0; d < q; d++) {
for(k = 0; k < p; k++) {
sum = sum + first[c][k] * second[k][d];
}
pro[c][d] = sum;
sum = 0;
}
}
printf("\n\nThe multiplication of the two entered matrices is: \n\n");
for(c = 0; c < m; c++) {
for(d = 0; d < q; d++) {
printf("%d\t", pro[c][d]);
}
printf("\n");
}
}
printf("\n\n\t\t\tCoding is Fun!\n\n\n");
return 0;
}
まとめ
この実験では、C 言語で行列を作成する方法と、実行できる異なる種類の行列乗算演算を学びました。C 言語で 2 つの行列の乗算を計算するプログラムを作成し、行列乗算の標準アルゴリズムについて説明しました。



