Vous pouvez écrire une extension simple qui fonctionne avec des matrices de n'importe quel rang.
public static class TwodimensionalArrayExtensions
{
public static int[][] MultiplyBy(this int[][] leftMatrix, int[][] rightMatrix)
{
if (leftMatrix[0].Length != rightMatrix.Length)
{
return null; // Matrices are of incompatible dimensions
}
return leftMatrix.Select(// goes through <leftMatrix matrix> row by row
(leftMatrixRow, leftMatrixRowIndexThatIsNotUsed) =>
rightMatrix[0].Select(// goes through first row of <rightMatrix> cell by cell
(rightFirstRow, rightMatrixColumnIndex) =>
rightMatrix
.Select(rightRow => rightRow[rightMatrixColumnIndex]) // selects column from <rightMatrix> for <rightMatrixColumnIndex>
.Zip(leftMatrixRow, (rowCell, columnCell) => rowCell * columnCell) // does scalar product
.Sum() // computes the sum of the products (rowCell * columnCell) sequence.
)
.ToArray() // the new cell within computed matrix
)
.ToArray(); // the computed matrix itself
}
}
Voici quelques valeurs de test:
// Test1
int[][] A = { new[] { 1, 2, 3 } };
int[][] B = { new[] { 1 }, new[] { 2 }, new[] { 3 } };
int[][] result = A.MultiplyBy(B);
// Test2
int[][] A = { new[] { 1 }, new[] { 2 }, new[] { 3 } };
int[][] B = { new[] { 1, 2, 3 } };
int[][] result = A.MultiplyBy(B);
// Test3
int[][] A = new int[][] { new[] { 1, 2 }, new[] { 2, 2 }, new[] { 3, 1 } };
int[][] B = new int[][] { new[] { 1, 1, 1 }, new[] { 2, 3, 2 } };
int[][] result = A.MultiplyBy(B);
mais pourquoi voudriez-vous utiliser LINQ? Qu'est-ce qui ne va pas avec une boucle de confiance que tout le monde peut comprendre? –
... ou simplement utiliser une bibliothèque de classes Matrix ... –
Assez drôle, j'ai [répondu à une autre question aujourd'hui] (http://stackoverflow.com/questions/311710/how-do-i-sum-a-list- of-arrays # 311863) qui somme les nombres en place dans deux séquences. –