开发者

type conversion error, c#

开发者 https://www.devze.com 2023-04-11 04:32 出处:网络
I am using Advanced Matrix Library in C#. NET@ http://www.codeproject.com/KB/recipes/AdvancedMatrixLibrary.aspx?msg=4042613#xx4042613xx.

I am using Advanced Matrix Library in C#. NET@ http://www.codeproject.com/KB/recipes/AdvancedMatrixLibrary.aspx?msg=4042613#xx4042613xx.

library css file is like

using System;

namespace MatrixLibrary
{
public 开发者_如何学Cclass Matrix
    {
      private double[,] in_Mat;
            public Matrix(int noRows, int noCols)
    {
        this.in_Mat = new double[noRows, noCols];
    }
            public Matrix(double [,] Mat)
    {
        this.in_Mat = (double[,])Mat.Clone();
    }

        public static double[,] Identity(int n)
        {
            double[,] temp = new double[n,n];
            for (int i=0; i<n;i++) temp[i,i] = 1;
            return temp;
        }

public static double[,] ScalarDivide(double Value, double[,] Mat)
    {
        int i, j, Rows, Cols;
        double[,] sol;

        try  {Find_R_C(Mat, out Rows, out Cols);}
        catch{throw new MatrixNullException();}

        sol = new double[Rows+1, Cols+1];

        for (i = 0; i<=Rows;i++)
            for (j = 0; j<=Cols;j++)
                sol[i, j] = Mat[i, j] / Value;

        return sol;
    }


}}

I am trying to get identity matrix and getting error of type conversion. Could some one please guide me.

Matrix id_mat = MatrixLibrary.Matrix.Identity(6);

Can't implicity convert typedouble[,] to Matrix.

But

Matrix B = new Matrix(4, 4);
 Random rnd = new Random();
            for (int i = 0; i < B.NoRows; i++)
                for (int j = 0; j < B.NoCols; j++)
                    B[i, j] = 2 * rnd.NextDouble();

Matrix E = Matrix.ScalarDivide(2, B);

It works and I can Have a matrix. Please guide me?

regards,


Read the error message.

You have a method that returns double[,] and you are trying to store the reference in a variable for Matrix. There is no implicit conversion from a multidimensional array of doubles to a Matrix.

To use that method, you would write

double[,] id_max = MatrixLibrary.Maxtrix.Identify(6);

If you actually need to store it as a Matrix, you need to define the appropriate conversion to do so.


The function returns a 2D array "double[,]" and you're trying to assign it to a "Matrix". There isn't an implicit conversion that does this. Does Matrix have a constructor that accepts a "double[,]"? Maybe you could use that.

EDIT: You might also find that a different matrix library would suit you better than one pulled off of CodeProject. Math.NET Numerics is a good one that I've used that's also open source: http://numerics.mathdotnet.com/


You need an implicit cast.

public static implicit operator Matrix(double[,] m) 
{
  // code to convert 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消