?? matrix.cs
字號(hào):
/*
* 操作矩陣的類 Matrix
*
* 周長發(fā)編制
*/
using System;
namespace 土地適宜評(píng)價(jià)系統(tǒng).Algorithm
{
/**
* 操作矩陣的類 Matrix
* @author 周長發(fā)
* @version 1.0
*/
public class Matrix
{
private int numColumns = 0; // 矩陣列數(shù)
private int numRows = 0; // 矩陣行數(shù)
private double eps = 0.0; // 缺省精度
private double[] elements = null; // 矩陣數(shù)據(jù)緩沖區(qū)
/**
* 屬性: 矩陣列數(shù)
*/
public int Columns
{
get
{
return numColumns;
}
}
/**
* 屬性: 矩陣行數(shù)
*/
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;
}
}
/**
* 屬性: Eps
*/
public double Eps
{
get
{
return eps;
}
set
{
eps = value;
}
}
/**
* 基本構(gòu)造函數(shù)
*/
public Matrix()
{
numColumns = 1;
numRows = 1;
Init(numRows, numColumns);
}
/**
* 指定行列構(gòu)造函數(shù)
*
* @param nRows - 指定的矩陣行數(shù)
* @param nCols - 指定的矩陣列數(shù)
*/
public Matrix(int nRows, int nCols)
{
numRows = nRows;
numColumns = nCols;
Init(numRows, numColumns);
}
/**
* 指定值構(gòu)造函數(shù)
*
* @param value - 二維數(shù)組,存儲(chǔ)矩陣各元素的值
*/
public Matrix(double[,] value)
{
numRows = value.GetLength(0);
numColumns = value.GetLength(1);
double[] data = new double[numRows * numColumns];
int k = 0;
for (int i=0; i<numRows; ++i)
{
for (int j=0; j<numColumns; ++j)
{
data[k++] = value[i, j];
}
}
Init(numRows, numColumns);
SetData(data);
}
/**
* 指定值構(gòu)造函數(shù)
*
* @param nRows - 指定的矩陣行數(shù)
* @param nCols - 指定的矩陣列數(shù)
* @param value - 一維數(shù)組,長度為nRows*nCols,存儲(chǔ)矩陣各元素的值
*/
public Matrix(int nRows, int nCols, double[] value)
{
numRows = nRows;
numColumns = nCols;
Init(numRows, numColumns);
SetData(value);
}
/**
* 方陣構(gòu)造函數(shù)
*
* @param nSize - 方陣行列數(shù)
*/
public Matrix(int nSize)
{
numRows = nSize;
numColumns = nSize;
Init(nSize, nSize);
}
/**
* 方陣構(gòu)造函數(shù)
*
* @param nSize - 方陣行列數(shù)
* @param value - 一維數(shù)組,長度為nRows*nRows,存儲(chǔ)方陣各元素的值
*/
public Matrix(int nSize, double[] value)
{
numRows = nSize;
numColumns = nSize;
Init(nSize, nSize);
SetData(value);
}
/**
* 拷貝構(gòu)造函數(shù)
*
* @param other - 源矩陣
*/
public Matrix( Matrix other)
{
numColumns = other.GetNumColumns();
numRows = other.GetNumRows();
Init(numRows, numColumns);
SetData(other.elements);
}
/**
* 初始化函數(shù)
*
* @param nRows - 指定的矩陣行數(shù)
* @param nCols - 指定的矩陣列數(shù)
* @return bool, 成功返回true, 否則返回false
*/
public bool Init(int nRows, int nCols)
{
numRows = nRows;
numColumns = nCols;
int nSize = nCols*nRows;
if (nSize < 0)
return false;
// 分配內(nèi)存
elements = new double[nSize];
return true;
}
/**
* 設(shè)置矩陣運(yùn)算的精度
*
* @param newEps - 新的精度值
*/
public void SetEps(double newEps)
{
eps = newEps;
}
/**
* 取矩陣的精度值
*
* @return double型,矩陣的精度值
*/
public double GetEps()
{
return eps;
}
/**
* 重載 + 運(yùn)算符
*
* @return Matrix對(duì)象
*/
public static Matrix operator +(Matrix m1, Matrix m2)
{
return m1.Add(m2);
}
/**
* 重載 - 運(yùn)算符
*
* @return Matrix對(duì)象
*/
public static Matrix operator -(Matrix m1, Matrix m2)
{
return m1.Subtract(m2);
}
/**
* 重載 * 運(yùn)算符
*
* @return Matrix對(duì)象
*/
public static Matrix operator *(Matrix m1, Matrix m2)
{
return m1.Multiply(m2);
}
/**
* 重載 double[] 運(yùn)算符
*
* @return double[]對(duì)象
*/
public static implicit operator double[](Matrix m)
{
return m.elements;
}
/**
* 將方陣初始化為單位矩陣
*
* @param nSize - 方陣行列數(shù)
* @return bool 型,初始化是否成功
*/
public bool MakeUnitMatrix(int nSize)
{
if (! Init(nSize, nSize))
return false;
for (int i=0; i<nSize; ++i)
for (int j=0; j<nSize; ++j)
if (i == j)
SetElement(i, j, 1);
return true;
}
/**
* 將矩陣各元素的值轉(zhuǎn)化為字符串, 元素之間的分隔符為",", 行與行之間有回車換行符
* @return string 型,轉(zhuǎn)換得到的字符串
*/
public override string ToString()
{
return ToString(",", true);
}
/**
* 將矩陣各元素的值轉(zhuǎn)化為字符串
*
* @param sDelim - 元素之間的分隔符
* @param bLineBreak - 行與行之間是否有回車換行符
* @return string 型,轉(zhuǎn)換得到的字符串
*/
public string ToString(string sDelim, bool bLineBreak)
{
string s="";
for (int i=0; i<numRows; ++i)
{
for (int j=0; j<numColumns; ++j)
{
string ss = GetElement(i, j).ToString("F");
s += ss;
if (bLineBreak)
{
if (j != numColumns-1)
s += sDelim;
}
else
{
if (i != numRows-1 || j != numColumns-1)
s += sDelim;
}
}
if (bLineBreak)
if (i != numRows-1)
s += "\r\n";
}
return s;
}
/**
* 將矩陣指定行中各元素的值轉(zhuǎn)化為字符串
*
* @param nRow - 指定的矩陣行,nRow = 0表示第一行
* @param sDelim - 元素之間的分隔符
* @return string 型,轉(zhuǎn)換得到的字符串
*/
public string ToStringRow(int nRow, string sDelim)
{
string s = "";
if (nRow >= numRows)
return s;
for (int j=0; j<numColumns; ++j)
{
string ss = GetElement(nRow, j).ToString("F");
s += ss;
if (j != numColumns-1)
s += sDelim;
}
return s;
}
/**
* 將矩陣指定列中各元素的值轉(zhuǎn)化為字符串
*
* @param nCol - 指定的矩陣行,nCol = 0表示第一列
* @param sDelim - 元素之間的分隔符
* @return string 型,轉(zhuǎn)換得到的字符串
*/
public string ToStringCol(int nCol, string sDelim /*= " "*/)
{
string s = "";
if (nCol >= numColumns)
return s;
for (int i=0; i<numRows; ++i)
{
string ss = GetElement(i, nCol).ToString("F");
s += ss;
if (i != numRows-1)
s += sDelim;
}
return s;
}
/**
* 設(shè)置矩陣各元素的值
*
* @param value - 一維數(shù)組,長度為numColumns*numRows,存儲(chǔ)
* 矩陣各元素的值
*/
public void SetData(double[] value)
{
elements = (double[])value.Clone();
}
/**
* 設(shè)置指定元素的值
*
* @param nRow - 元素的行
* @param nCol - 元素的列
* @param value - 指定元素的值
* @return bool 型,說明設(shè)置是否成功
*/
public bool SetElement(int nRow, int nCol, double value)
{
if (nCol < 0 || nCol >= numColumns || nRow < 0 || nRow >= numRows)
return false; // array bounds error
elements[nCol + nRow * numColumns] = value;
return true;
}
/**
* 獲取指定元素的值
*
* @param nRow - 元素的行
* @param nCol - 元素的列
* @return double 型,指定元素的值
*/
public double GetElement(int nRow, int nCol)
{
return elements[nCol + nRow * numColumns] ;
}
/**
* 獲取矩陣的列數(shù)
*
* @return int 型,矩陣的列數(shù)
*/
public int GetNumColumns()
{
return numColumns;
}
/**
* 獲取矩陣的行數(shù)
* @return int 型,矩陣的行數(shù)
*/
public int GetNumRows()
{
return numRows;
}
/**
* 獲取矩陣的數(shù)據(jù)
*
* @return double型數(shù)組,指向矩陣各元素的數(shù)據(jù)緩沖區(qū)
*/
public double[] GetData()
{
return elements;
}
/**
* 獲取指定行的向量
*
* @param nRow - 向量所在的行
* @param pVector - 指向向量中各元素的緩沖區(qū)
* @return int 型,向量中元素的個(gè)數(shù),即矩陣的列數(shù)
*/
public int GetRowVector(int nRow, double[] pVector)
{
for (int j=0; j<numColumns; ++j)
pVector[j] = GetElement(nRow, j);
return numColumns;
}
/**
* 獲取指定列的向量
*
* @param nCol - 向量所在的列
* @param pVector - 指向向量中各元素的緩沖區(qū)
* @return int 型,向量中元素的個(gè)數(shù),即矩陣的行數(shù)
*/
public int GetColVector(int nCol, double[] pVector)
{
for (int i=0; i<numRows; ++i)
pVector[i] = GetElement(i, nCol);
return numRows;
}
/**
* 給矩陣賦值
*
* @param other - 用于給矩陣賦值的源矩陣
* @return Matrix型,陣與other相等
*/
public Matrix SetValue(Matrix other)
{
if (other != this)
{
Init(other.GetNumRows(), other.GetNumColumns());
SetData(other.elements);
}
// finally return a reference to ourselves
return this ;
}
/**
* 判斷矩陣否相等
*
* @param other - 用于比較的矩陣
* @return bool 型,兩個(gè)矩陣相等則為true,否則為false
*/
public override bool Equals(object other)
{
Matrix matrix = other as Matrix;
if (matrix == null)
return false;
// 首先檢查行列數(shù)是否相等
if (numColumns != matrix.GetNumColumns() || numRows != matrix.GetNumRows())
return false;
for (int i=0; i<numRows; ++i)
{
for (int j=0; j<numColumns; ++j)
{
if (Math.Abs(GetElement(i, j) - matrix.GetElement(i, j)) > eps)
return false;
}
}
return true;
}
/**
* 因?yàn)橹貙懥薊quals,因此必須重寫GetHashCode
*
* @return int型,返回復(fù)數(shù)對(duì)象散列碼
*/
public override int GetHashCode()
{
double sum = 0;
for (int i=0; i<numRows; ++i)
{
for (int j=0; j<numColumns; ++j)
{
sum += Math.Abs(GetElement(i, j));
}
}
return (int)Math.Sqrt(sum);
}
/**
* 實(shí)現(xiàn)矩陣的加法
*
* @param other - 與指定矩陣相加的矩陣
* @return Matrix型,指定矩陣與other相加之和
* @如果矩陣的行/列數(shù)不匹配,則會(huì)拋出異常
*/
public Matrix Add(Matrix other)
{
// 首先檢查行列數(shù)是否相等
if (numColumns != other.GetNumColumns() ||
numRows != other.GetNumRows())
throw new Exception("矩陣的行/列數(shù)不匹配。");
// 構(gòu)造結(jié)果矩陣
Matrix result = new Matrix(this) ; // 拷貝構(gòu)造
// 矩陣加法
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 ;
}
/**
* 實(shí)現(xiàn)矩陣的減法
*
* @param other - 與指定矩陣相減的矩陣
* @return Matrix型,指定矩陣與other相減之差
* @如果矩陣的行/列數(shù)不匹配,則會(huì)拋出異常
*/
public Matrix Subtract(Matrix other)
{
if (numColumns != other.GetNumColumns() ||
numRows != other.GetNumRows())
throw new Exception("矩陣的行/列數(shù)不匹配。");
// 構(gòu)造結(jié)果矩陣
Matrix result = new Matrix(this) ; // 拷貝構(gòu)造
// 進(jìn)行減法操作
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 ;
}
/**
* 實(shí)現(xiàn)矩陣的數(shù)乘
*
* @param value - 與指定矩陣相乘的實(shí)數(shù)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -