各位朋友大家好,最近遇到了一个问题。我需要在C#里面进行一些矩阵运算,我在网上下载到了关于矩阵运算的代码,这些代码放置在一个*.cs文件中,如下所示:using System;namespace CSharpAlgorithm.Algorithm
{
public class Matrix
{
private int numColumns = 0; // 矩阵列数
private int numRows = 0; // 矩阵行数
private double eps = 0.0; // 缺省精度
private double[] elements = null; // 矩阵数据缓冲区 /**
* 属性: 矩阵列数
*/
public int Columns
{
get
{
return numColumns;
}
} /**
* 属性: 矩阵行数
*/
public int Rows
{
get
{
return numRows;
}
} /**
* 索引器: 访问矩阵元素
* @param row - 元素的行
* @param col - 元素的列
*/
public double this[int row, int col]
{
get
{
return elements[col + row * numColumns];
}
set
{
elements[col + row * numColumns] = value;
}
}
/**
* 基本构造函数
*/
public Matrix()
{
numColumns = 1;
numRows = 1;
Init(numRows, numColumns);
} /**
* 指定行列构造函数
*
* @param nRows - 指定的矩阵行数
* @param nCols - 指定的矩阵列数
*/
public Matrix(int nRows, int nCols)
{
numRows = nRows;
numColumns = nCols;
Init(numRows, numColumns);
} public Matrix Add(Matrix other)
{
// 首先检查行列数是否相等
if (numColumns != other.GetNumColumns() ||
numRows != other.GetNumRows())
throw new Exception("矩阵的行/列数不匹配。"); // 构造结果矩阵
Matrix result = new Matrix(this) ; // 拷贝构造
// 矩阵加法
for (int i = 0 ; i < numRows ; ++i)
{
for (int j = 0 ; j < numColumns; ++j)
result.SetElement(i, j, result.GetElement(i, j) + other.GetElement(i, j)) ;
} return result ;
} /**
* 实现矩阵的减法
*
* @param other - 与指定矩阵相减的矩阵
* @return Matrix型,指定矩阵与other相减之差
* @如果矩阵的行/列数不匹配,则会抛出异常
*/
public Matrix Subtract(Matrix other)
{
if (numColumns != other.GetNumColumns() ||
numRows != other.GetNumRows())
throw new Exception("矩阵的行/列数不匹配。"); // 构造结果矩阵
Matrix result = new Matrix(this) ; // 拷贝构造 // 进行减法操作
for (int i = 0 ; i < numRows ; ++i)
{
for (int j = 0 ; j < numColumns; ++j)
result.SetElement(i, j, result.GetElement(i, j) - other.GetElement(i, j)) ;
} return result ;
} /**
* 实现矩阵的数乘
*
* @param value - 与指定矩阵相乘的实数
* @return Matrix型,指定矩阵与value相乘之积
*/
public Matrix Multiply(double value)
{
// 构造目标矩阵
Matrix result = new Matrix(this) ; // copy ourselves
// 进行数乘
for (int i = 0 ; i < numRows ; ++i)
{
for (int j = 0 ; j < numColumns; ++j)
result.SetElement(i, j, result.GetElement(i, j) * value) ;
} return result ;
}
..............................
}现在我要使用这段其中的矩阵运算如加法,减法,乘法,除法等运算,我需要怎么才能使用。
有的方法是将这个*.cs 编译成dll,然后调用,这种方法应该是可以的。我想问一下还有什么其他的好方法没,谢谢大家。
{
public class Matrix
{
private int numColumns = 0; // 矩阵列数
private int numRows = 0; // 矩阵行数
private double eps = 0.0; // 缺省精度
private double[] elements = null; // 矩阵数据缓冲区 /**
* 属性: 矩阵列数
*/
public int Columns
{
get
{
return numColumns;
}
} /**
* 属性: 矩阵行数
*/
public int Rows
{
get
{
return numRows;
}
} /**
* 索引器: 访问矩阵元素
* @param row - 元素的行
* @param col - 元素的列
*/
public double this[int row, int col]
{
get
{
return elements[col + row * numColumns];
}
set
{
elements[col + row * numColumns] = value;
}
}
/**
* 基本构造函数
*/
public Matrix()
{
numColumns = 1;
numRows = 1;
Init(numRows, numColumns);
} /**
* 指定行列构造函数
*
* @param nRows - 指定的矩阵行数
* @param nCols - 指定的矩阵列数
*/
public Matrix(int nRows, int nCols)
{
numRows = nRows;
numColumns = nCols;
Init(numRows, numColumns);
} public Matrix Add(Matrix other)
{
// 首先检查行列数是否相等
if (numColumns != other.GetNumColumns() ||
numRows != other.GetNumRows())
throw new Exception("矩阵的行/列数不匹配。"); // 构造结果矩阵
Matrix result = new Matrix(this) ; // 拷贝构造
// 矩阵加法
for (int i = 0 ; i < numRows ; ++i)
{
for (int j = 0 ; j < numColumns; ++j)
result.SetElement(i, j, result.GetElement(i, j) + other.GetElement(i, j)) ;
} return result ;
} /**
* 实现矩阵的减法
*
* @param other - 与指定矩阵相减的矩阵
* @return Matrix型,指定矩阵与other相减之差
* @如果矩阵的行/列数不匹配,则会抛出异常
*/
public Matrix Subtract(Matrix other)
{
if (numColumns != other.GetNumColumns() ||
numRows != other.GetNumRows())
throw new Exception("矩阵的行/列数不匹配。"); // 构造结果矩阵
Matrix result = new Matrix(this) ; // 拷贝构造 // 进行减法操作
for (int i = 0 ; i < numRows ; ++i)
{
for (int j = 0 ; j < numColumns; ++j)
result.SetElement(i, j, result.GetElement(i, j) - other.GetElement(i, j)) ;
} return result ;
} /**
* 实现矩阵的数乘
*
* @param value - 与指定矩阵相乘的实数
* @return Matrix型,指定矩阵与value相乘之积
*/
public Matrix Multiply(double value)
{
// 构造目标矩阵
Matrix result = new Matrix(this) ; // copy ourselves
// 进行数乘
for (int i = 0 ; i < numRows ; ++i)
{
for (int j = 0 ; j < numColumns; ++j)
result.SetElement(i, j, result.GetElement(i, j) * value) ;
} return result ;
}
..............................
}现在我要使用这段其中的矩阵运算如加法,减法,乘法,除法等运算,我需要怎么才能使用。
有的方法是将这个*.cs 编译成dll,然后调用,这种方法应该是可以的。我想问一下还有什么其他的好方法没,谢谢大家。
我的做法是:就是把这个cs文件作为project的一个文件,你的程序和这个矩阵类在同一个namespace里,然后就就可以当类型使用了,类似
Matrix matrix =new Matrix()
using CSharpAlgorithm.Algorithm;
Matrix matrix =new Matrix();
在引用它,导入命名空间,使用类
为什么我拷贝到.cs文件中都有问题啊?