3 Stimmen

Können Sie LINQ oder Lambdas verwenden, um Matrixoperationen durchzuführen?

Ich weiß, wie man das mit for-Schleifen macht. Ist es möglich, etwas wie dies mit LINQ oder Lambdas zu tun?

int[] a = { 10, 20, 30 };
int[] b = { 2, 4, 10 };
int[] c = a * b; //resulting array should be { 20, 80, 300 }

0 Stimmen

Aber warum sollten Sie überhaupt LINQ verwenden? Was ist falsch an einer vertrauten "for-Schleife", die jeder verstehen kann?

0 Stimmen

...oder verwenden Sie einfach eine Matrix-Klassenbibliothek...

0 Stimmen

Lustigerweise habe ich beantwortete heute eine weitere Frage der die Zahlen in zwei Reihenfolgen summiert.

0voto

AlexMelw Punkte 2037

Sie können eine einfache Erweiterung schreiben, die mit Matrizen beliebigen Ranges arbeitet.

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
    }
}

Hier sind einige Testwerte:

// 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);

0voto

Philipp Kramer Punkte 1

Wenn die rechte Matrix vorher transponiert wird, kann die Multiplikation eleganter wie folgt ausgedrückt werden.

int[][] Multiply(int[][] left, int[][] right) =>
    left.Select(lr => 
            right
                .Select(rr => 
                    lr.Zipped(rr, (l, r) => l * r).Sum())
                .ToArray())
        .ToArray();

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X