0

こんにちは、次のような配列操作の拡張機能をいくつか書きたいと思います。

マトリックス製品

public static double[][] operator *(double[][] matrixA, double[][] matrixB)

行列ベクトル積

public static double[] operator *(double[][] matrix, double[] vector)

そしてもう少し

なぜ私はこれが欲しいのですか?ご存知かもしれませんが、現時点ではそのような操作はC#で実装されておらず、代わりに言うことができればより自然に感じるmatrixC = matrixA * matrixB;のでmatrixC = MatrixProduct(matrixA,matrixB);、これを行う方法はありますか?

私がこのようにすれば:

public static class ArrayExtension
{
    public static double[] operator *(double[][] matrix, double[] vector)
    {
        // result of multiplying an n x m matrix by a m x 1 column vector (yielding an n x 1 column vector)
        int mRows = matrix.Length; int mCols = matrix[0].Length;
        int vRows = vector.Length;

        if (mCols != vRows)
            throw new InvalidOperationException("Non-conformable matrix and vector in MatrixVectorProduct");

        double[] result = new double[mRows]; // an n x m matrix times a m x 1 column vector is a n x 1 column vector

        Parallel.For(0, mRows, i =>
        {
            var row = matrix[i];
            for (int j = 0; j < mCols; ++j)
                result[i] += row[j] * vector[j];
        });

        return result;
    }
}

例外により、静的クラスとユーザー定義演算子を組み合わせることができないことがわかります。回避策はありますか?

4

1 に答える 1