简体   繁体   English

如何将数据传递给 class:错误“无法将类型‘double’隐式转换为‘CSML.Complex’

[英]How to pass data to a class: Error "Cannot implicitly convert type 'double' to 'CSML.Complex'

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.我下载了一个我正在尝试使用的线性代数 class 库 (CSML)。 I'm trying to store individual values to a Matrix class. I get the error: Cannot implicitly convert type 'double' to 'CSML.Complex'.我正在尝试将单个值存储到矩阵 class。我收到错误消息:无法将类型“double”隐式转换为“CSML.Complex”。

I've tried many different ways to initiate the Matrix and add the array:我尝试了许多不同的方法来启动 Matrix 并添加数组:

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:我什至尝试使用 for 循环来单独添加值:

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这是 DLL 中的代码,用于创建带有双 [,] 输入的矩阵 Class

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.这是 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.");
            }
        }```


The Complex class doesn't contain an implicit conversion from double . Complex class 不包含从double的隐式转换。 If you want to get an instance of Complex , you need to call new Complex(_your_double_here_) .如果你想获得Complex的一个实例,你需要调用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.但是同样,这个库自 2007 年以来就没有更新过,并且会教给您一些不好的和过时的做法。 Have a look at Math.Net , it is up to date and has great documentation看看Math.Net ,它是最新的并且有很好的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;double&#39;隐式转换为&#39;int&#39;。 -错误 - Cannot implicitly convert type 'double' to 'int'. -error 我收到此错误:错误1:无法将类型&#39;double [] []&#39;隐式转换为&#39;double []&#39; - I got this error: Error 1: Cannot implicitly convert type 'double[][]' to 'double[]' 尝试传递表的第一个 DataRow 时出现错误“无法将类型 &#39;System.Data.DataRow&#39; 隐式转换为...” - Error "Cannot implicitly convert type 'System.Data.DataRow' to..." when trying to pass the first DataRow of a table 错误 CS0029 无法将类型“字符串”隐式转换为“双精度” - Error CS0029 Cannot implicitly convert type 'string' to 'double' 无法将类型&#39;double&#39;隐式转换为&#39;float&#39; - Cannot implicitly convert type 'double' to 'float' 无法将类型&#39;double&#39;隐式转换为&#39;RadTextBox&#39; - Cannot implicitly convert type 'double' to 'RadTextBox' 错误:“无法隐式转换类型” - Error: “Cannot implicitly convert type” 错误无法隐式转换类型 - Error Cannot implicitly convert type 如何解决此错误无法隐式转换类型? - How to solve this Error Cannot implicitly convert type? 无法隐式转换int? 到复杂类型 - Cannot implicitly convert int? to complex type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM