How to convert matrix <double> to Matrix <float>?

I want to calculate something like: Matrix<float> * Matrix<double>

Matrix<float>

has about 6M * 3 elements, how can I convert Matrix<double>

to Matrix<float>

to get the result Matrix<float>

.

+3


source to share


2 answers


You can convert your double matrix argument to a floating point matrix using the function Map

:

Matrix<double> m1 = Matrix<double>.Build.Random(6000000,3);
Matrix<float> m2 = m1.Map(x => (float)x);

      



Or alternatively

Matrix<float> m2 = m1.Map(Convert.ToSingle);

      

+2


source


Here's how to convert double array to float array, you just need to convert matrix to array and vice versa



public static float[][] Convert(double[][] mtx)
{
    var floatMtx = new float[mtx.Length][];
    for (int i = 0; i < mtx.Length; i++)
    {
        floatMtx[i] = new float[mtx[i].Length];
        for (int j = 0; j < mtx[i].Length; j++)
            floatMtx[i][j] = (float)mtx[i][j];
    }
    return floatMtx;
}
Or:

public static float[][] Convert(double[][] mtx)
{
    return mtx.Select(i => i.Select(j => (float)j).ToArray()).ToArray();
}

      

+1


source







All Articles