Home > Software engineering >  How to pass data to a class: Error "Cannot implicitly convert type 'double' to '
How to pass data to a class: Error "Cannot implicitly convert type 'double' to '

Time:01-23

Long time lurker on this website, because I always find my answer without having to post... I cannot tell what I'm doing in this case... does someone mind to take a look and help with what I am missing?

I downloaded a linear algebra class library (CSML) that I am trying to use. I'm trying to store individual values to a Matrix class. I get the error: Cannot implicitly convert type 'double' to 'CSML.Complex'.

I've tried many different ways to initiate the Matrix and add the array:

Matrix A = new Matrix(B);

Matrix A = new Matrix(double[,] B);

I have even tried to use a for loop to add the values individually:

Matrix A = new Matrix(new double[TotalARows,TotalACols]);

for (int i = 0; i < TotalARows; i  ) {
    for (int j = 0; j < TotalACols; j  ) {              
        A[i,j] = B[i,j];
    }
}

Here is the code in the DLL for the creation of the Matrix Class with double[,] input

public Matrix(double[,] values)
        {
            if (values == null)
            {
                Values = new ArrayList();
                columnCount = 0;
                rowCount = 0;
            }

            rowCount = (int)values.GetLongLength(0);
            columnCount = (int)values.GetLongLength(1);

            Values = new ArrayList(rowCount);

            for (int i = 0; i < rowCount; i  )
            {
                Values.Add(new ArrayList(columnCount));

                for (int j = 0; j < columnCount; j  )
                {
                    ((ArrayList)Values[i]).Add(new Complex(values[i, j]));
                }
            }
        }

Here is the 'get' 'set' of Class Complex.

public virtual Complex this[int i, int j]
        {
            set
            {
                if (i <= 0 || j <= 0)
                    throw new ArgumentOutOfRangeException("Indices must be real positive.");

                if (i > rowCount)
                {
                    // dynamically add i-Rows new rows...
                    for (int k = 0; k < i - rowCount; k  )
                    {
                        this.Values.Add(new ArrayList(columnCount));

                        // ...with Cols columns
                        for (int t = 0; t < columnCount; t  )
                        {
                            ((ArrayList)Values[rowCount   k]).Add(Complex.Zero);
                        }
                    }

                    rowCount = i; // ha!
                }


                if (j > columnCount)
                {
                    // dynamically add j-Cols columns to each row
                    for (int k = 0; k < rowCount; k  )
                    {
                        for (int t = 0; t < j - columnCount; t  )
                        {
                            ((ArrayList)Values[k]).Add(Complex.Zero);
                        }
                    }

                    columnCount = j;
                }

                ((ArrayList)Values[i - 1])[j - 1] = value;
                //this.Values[i - 1, j - 1] = value; 
            }
            get
            {
                if (i > 0 && i <= rowCount && j > 0 && j <= columnCount)
                {                
                    return (Complex)(((ArrayList)Values[i - 1])[j - 1]);
                }
                else
                    throw new ArgumentOutOfRangeException("Indices must not exceed size of matrix.");
            }
        }```


CodePudding user response:

The Complex class doesn't contain an implicit conversion from double. If you want to get an instance of Complex, you need to call new Complex(_your_double_here_). You can initialize a matrix with a double array, and it will perform the conversion for you in the constructor:

var array = new double[,] {
        {1,1,1,1},
        {1,2,3,4},
        {4,3,2,1}
};

var matrix = new Matrix(array);

// Complex has a constructor which a single 'real' value as a double
matrix[1, 2] = new Complex(3);

// It also has a constructor which a 'real' value and an 'imaginary' value
matrix[1, 2] = new Complex(3, 4);

But again, this library hasn't been updated since 2007 and will teach you some bad and obsolete practices. Have a look at Math.Net, it is up to date and has great documentation

  • Related