This project has moved. For the latest updates, please go here.

Modulo operator on matrix

Jan 17, 2011 at 3:20 AM

Hi - Is there a modulus operator on a matrix? More specifically, I'm creating a matrix:

 var matrixA = new DenseMatrix(new[,] { { 5.0, 8.0 }, { 17.0, 3.0 } });
and I would like to perform the following operation:
var matrixB = matrixA % 26; 
or something to that effect. The application is for encryption (Hill Cipher).
Thanks,
Mike
Jan 17, 2011 at 11:41 AM

Hi Mike,

There isn't. I'll add it to the to do list.

In the meantime, you could use an extenstion method such as:

public static Matrix Mod(this DenseMatrix matrix, int mod)
{
    var result = (DenseMatrix)matrix.Clone();

    Parallel.For(0, result.Data.Length, index => result.Data[index] %= mod);

    return result;
}
Then the usage would be:

var matrixB = matrixA.Mod(26);

Regards,
Marcus

Jan 17, 2011 at 11:41 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.