Common Matrix Operations
CoddyのC#ジャーニー「ロジックとフロー」セクションの一部 — レッスン 6/66。
行列は数学とコンピュータサイエンスで一般的に使用されます。2次元配列上のいくつかの一般的な操作を探ってみましょう。
2 つの行列を加算する:
int[][] AddMatrices(int[][] a, int[][] b)
{
int rows = a.Length;
int[][] result = new int[rows][];
for (int i = 0; i < rows; i++)
{
result[i] = new int[a[i].Length];
for (int j = 0; j < a[i].Length; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
return result;
}行列の転置(行と列を入れ替える):
int[][] Transpose(int[][] matrix)
{
int rows = matrix.Length;
int cols = matrix[0].Length;
int[][] result = new int[cols][];
for (int i = 0; i < cols; i++)
{
result[i] = new int[rows];
for (int j = 0; j < rows; j++)
{
result[i][j] = matrix[j][i];
}
}
return result;
}各行の合計を計算:
int[] RowSums(int[][] matrix)
{
int rows = matrix.Length;
int[] sums = new int[rows];
for (int i = 0; i < rows; i++)
{
int sum = 0;
for (int j = 0; j < matrix[i].Length; j++)
{
sum += matrix[i][j];
}
sums[i] = sum;
}
return sums;
}2 つの行列を乗算する:
行列乗算では、各要素
result[i][j] は、最初の行列の行 i と 2 番目の行列の列 j の 内積 として計算されます — すなわち、すべての k について matrix1[i][k] * matrix2[k][j] の合計です。最初の行列は 2 番目の行列の 行 数と同じだけの 列 を持っていなければなりません。int[][] MultiplyMatrices(int[][] a, int[][] b)
{
int rows = a.Length;
int cols = b[0].Length;
int inner = b.Length;
int[][] result = new int[rows][];
for (int i = 0; i < rows; i++)
{
result[i] = new int[cols];
for (int j = 0; j < cols; j++)
{
int sum = 0;
for (int k = 0; k < inner; k++)
{
sum += a[i][k] * b[k][j];
}
result[i][j] = sum;
}
}
return result;
}チャレンジ
難しいmultiplyMatrices という名前のメソッドを作成してください。このメソッドは:
- matrix1 と matrix2 という 2 つの行列(2 次元ジャグ配列)をパラメータとして受け取ります
- 行列乗算のルールに従ってそれらを乗算します
- 結果の行列を返します
行列乗算が有効であるためには:
- matrix1 の列数が matrix2 の行数と等しくなければなりません
- 結果の次元は:[matrix1.rows × matrix2.columns]
行列乗算の仕組み:
結果の位置 [i][j] の各要素は、matrix1 の行 i と matrix2 の列 j を取り、それらの対応する要素を掛け合わせて、そのすべての積を合計することで計算されます:result[i][j] = matrix1[i][0] * matrix2[0][j] + matrix1[i][1] * matrix2[1][j] + ...
言い換えれば:result[i][j] = sum of (matrix1[i][k] * matrix2[k][j]) 各 k について。
例えば、matrix1 が次の場合:
[1, 2]
[3, 4]matrix2 が次の場合:
[5, 6]
[7, 8]result[0][0] = 1*5 + 2*7 = 19、result[0][1] = 1*6 + 2*8 = 22 などとなります。結果は次のようになります:
[19, 22]
[43, 50]行列を乗算できない場合、null を返します。
チートシート
2D ジャグド配列を使用した一般的な行列演算:
2つの行列を加算:
int[][] AddMatrices(int[][] a, int[][] b)
{
int rows = a.Length;
int[][] result = new int[rows][];
for (int i = 0; i < rows; i++)
{
result[i] = new int[a[i].Length];
for (int j = 0; j < a[i].Length; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
return result;
}行列の転置(行と列を交換):
int[][] Transpose(int[][] matrix)
{
int rows = matrix.Length;
int cols = matrix[0].Length;
int[][] result = new int[cols][];
for (int i = 0; i < cols; i++)
{
result[i] = new int[rows];
for (int j = 0; j < rows; j++)
{
result[i][j] = matrix[j][i];
}
}
return result;
}各行の合計を計算:
int[] RowSums(int[][] matrix)
{
int rows = matrix.Length;
int[] sums = new int[rows];
for (int i = 0; i < rows; i++)
{
int sum = 0;
for (int j = 0; j < matrix[i].Length; j++)
{
sum += matrix[i][j];
}
sums[i] = sum;
}
return sums;
}2つの行列を乗算:
各要素 result[i][j] は、最初の行列の行 i と2番目の行列の列 j の積の合計です:
result[i][j] += matrix1[i][k] * matrix2[k][j] を各 k について。
最初の行列の列数は、2番目の行列の行数と同じでなければなりません。
int[][] MultiplyMatrices(int[][] a, int[][] b)
{
int rows = a.Length;
int cols = b[0].Length;
int inner = b.Length;
int[][] result = new int[rows][];
for (int i = 0; i < rows; i++)
{
result[i] = new int[cols];
for (int j = 0; j < cols; j++)
{
for (int k = 0; k < inner; k++)
{
result[i][j] += a[i][k] * b[k][j];
}
}
}
return result;
}自分で試してみよう
public class MultiplyMatrices
{
// MultiplyMatricesメソッドを実装してください
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2)
{
// ここにコードを記述してください
}
}このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
ロジックとフローのすべてのレッスン
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety