以前无聊的时候搞得个东东(一)
以前用matlab编过计算相平衡的东西,用PR方程编的,还比较准,就是初值对是否收敛太敏感,要手动调整,呵呵前段时间看别人用C# 据说和java有点像,就学习了一下,还是用matlab的思想,自己编了个矩阵类,通过矩阵点乘、点除、转置等来完成PR的混合规则等,静态方法主要用来进行平方等计算。构造函数1三个数组生成一个n×3的矩阵,主要用来罗列混合物的主要物性,很幼稚,现在看起来需要优化的地方还很多
用matlab变成算就方便多了,矩阵功能很强大,若大家有兴趣,我可以贴出以前编的程序 其中有PT闪蒸,求泡点、露点等。跟hysys对比起来,只要基础物性准,Kij准结果还不错也很容易收敛。
今天先贴出C#编的矩阵类,会根据思路贴出其他的代码(matlab或C#) 请指教,如果大家觉得没兴趣洒家就就此打住了
矩阵类:如下
public class mat2
{
//私有成员
private int numColumns = 0;
private int numRows = 0;
private double[] matnum = null;
//属性
public int Columns
{
get { return numColumns; }
}
public int Rows
{
get { return numRows; }
}
#region //构造函数
//构造函数1三个数组生成一个n×3的矩阵
public mat2(double[] Pc, double[] Tc, double[] w)
{
int pcL = Pc.Length;
int tcL = Tc.Length;
int wL = w.Length;
if (pcL == tcL && tcL == wL && pcL != 0)
{
numColumns = 3;
numRows = pcL;
matnum = new double;
for (int index = 0; index < pcL; index++)
{
matnum= Pc;
matnum = Tc;
matnum = w;
}
}
}
//构造函数2一个数组生成n×1的矩阵
public mat2(double[] da)
{
int daL = da.Length;
numColumns = 1;
numRows = daL;
matnum = (double[])da.Clone ();
}
//构造函数3,另外一个矩阵生成矩阵
public mat2(mat2 other)
{
numRows = other.Rows;
numColumns = other.Columns;
matnum = (double[])other.getarray().Clone();
}
//构造函数4,由数组生成指定维数的矩阵
public mat2(int r, int c, double[] darray)
{
numRows = r;
numColumns = c;
matnum = (double[])darray.Clone();
}
#endregion
#region //普通方法
//方法返回表示矩阵的一维数组
public double[] getarray()
{
return (double[])matnum.Clone();
}
public mat2 get_oneColumn(int i)
{
int newR = numRows;
int newC = 1;
double[] temp = new double;
Array.Copy(matnum,numRows*i, temp,0,newR);
return new mat2(newR, newC, temp);
}
public void add_signNum(double d)
{
for (int i = 0; i < matnum.Length; i++)
{
matnum = matnum + d;
}
}
public void cheng_signNum(double d)
{
for (int i = 0; i < matnum.Length; i++)
{
matnum = matnum * d;
}
}
public void chu_signNum(double d)
{
if (d == 0) throw new Exception("不能被零除!");
for (int i = 0; i < matnum.Length; i++)
{
matnum = matnum / d;
}
}
public void beichu_signNum(double d)
{
for (int i = 0; i < matnum.Length; i++)
{
if (matnum == 0) throw new Exception("不能被零除!");
matnum = d/matnum;
}
}
public void jian_signNum(double d)
{
for (int i = 0; i < matnum.Length; i++)
{
matnum = matnum -d;
}
}
public void beijian_signNum(double d)
{
for (int i = 0; i < matnum.Length; i++)
{
matnum = d-matnum;
}
}
public void kaipingfang()
{
for (int i = 0; i < matnum.Length; i++)
{
if (matnum < 0) throw new Exception("不能对负数开平方!");
matnum = System.Math.Sqrt(matnum);
}
}
public double getMaxNum()
{
int numL = matnum.Length;
double temp = matnum;
for (int i = 1; i < numL; i++)
{
temp = System.Math.Max(temp, matnum);
}
return temp;
}
#endregion
#region //静态方法
//静态方法,形成n×n维的单位方阵
public static mat2 ones(int n)
{
int dataL = n * n;
double value = 1d;
double[] mattemp=new double;
for (int i = 0; i < dataL; i++)
{
mattemp = value;
}
return new mat2(n,n,mattemp);
}
//静态方法,由一个一维数组生成一个length*length的方阵
public static mat2 arrayTofz(double[] darray)
{
int darrayL = darray.Length;
double[] newdarray = new double;
for (int i = 0; i < darrayL; i++)
{
darray.CopyTo(newdarray, i * darrayL);
}
return new mat2(darrayL, darrayL, newdarray);
}
//静态方法,由一个n*1的矩阵mat2类生成n×n的方阵
public static mat2 matTofz(mat2 m)
{
return mat2.arrayTofz(m.getarray());
}
//静态方法将m1叠加到m2的后面
public static mat2 addtoRight(mat2 m1,mat2 m2)
{
int m1L = m1.Rows;
int m2L = m2.Rows;
if (m1L!=m2L) throw new Exception("不能addtoRight");
int newR = m1L;
int newC = m1.Columns+m2.Columns;
double[] newdarray = new double;
m1.getarray().CopyTo(newdarray, 0);
m2.getarray().CopyTo(newdarray, m1.getarray().Length);
mat2 temp = new mat2(newR, newC, newdarray);
return temp;
}
//静态方法计算矩阵m的0.5次方
public static mat2 SQRT(mat2 m)
{
mat2 temp = new mat2(m);
temp.kaipingfang();
return temp;
}
//静态方法转置一个矩阵m
public static mat2 zhuanzhi(mat2 m)
{
int newR = m.Columns;
int newC = m.Rows;
double[] oldarr = m.getarray();
double[] newarr = new double;
for (int r = 0; r < newR; r++)
{
for (int c = 0; c < newC; c++)
{
newarr = oldarr;
}
}
return new mat2(newR,newC,newarr);
}
public static double add_All(mat2 m)
{
double[] temp = m.getarray();
double v = 0.0d;
for (int i = 0; i < temp.Length; i++)
{
v += temp;
}
return v;
}
public static mat2 expM(mat2 m)
{
int newR = m.Rows;
int newC = m.Columns;
double[] oldarr = m.getarray();
int arrL = oldarr.Length;
double[] newarr = new double;
for (int i = 0; i < arrL; i++)
{
newarr = System.Math.Exp(oldarr);
}
return new mat2(newR, newC, newarr);
}
public static mat2 YuanZheng(mat2 m)
{
int newR = m.Rows;
int newC = m.Columns;
double[] oldarr = m.getarray();
int arrL = oldarr.Length;
double All = mat2.add_All(m);
double[] newarr = new double;
for (int i = 0; i < arrL; i++)
{
newarr = oldarr/All;
}
return new mat2(newR, newC, newarr);
}
#endregion
字数超限 待续 #region //重载运算符
//重载运算符+
public static mat2 operator +(mat2 m1, mat2 m2)
{
if (m1.Columns != m2.Columns || m1.Rows != m2.Rows) throw new Exception("矩阵维数不相等");
double[] temp = (double[])m1.getarray().Clone();
double[] arr2=(double[])m2.getarray().Clone();
int tempL = m1.getarray().Length;
for (int i = 0; i < tempL; i++)
{
temp = temp + arr2;
}
return new mat2(m1.Rows, m1.Columns, temp);
}
public static mat2 operator +(double d, mat2 m)
{
mat2 temp = new mat2(m);
temp.add_signNum(d);
return temp;
}
public static mat2 operator +(mat2 m,double d)
{
mat2 temp = new mat2(m);
temp.add_signNum(d);
return temp;
}
public static mat2 operator *(mat2 m, double d)
{
mat2 temp = new mat2(m);
temp.cheng_signNum(d);
return temp;
}
public static mat2 operator *(double d,mat2 m)
{
mat2 temp = new mat2(m);
temp.cheng_signNum(d);
return temp;
}
public static mat2 operator *(mat2 m1, mat2 m2)
{
if (m1.Rows != m2.Rows || m1.Columns != m2.Columns) throw new Exception("维数不同不能相乘!");
int newR = m1.Rows;
int newC = m1.Columns;
double[] temp = new double;
double[] arr1 = m1.getarray();
double[] arr2 = m2.getarray();
for (int i = 0; i < newR * newC; i++)
{
temp = arr1 * arr2;
}
return new mat2(newR, newC, temp);
}
public static mat2 operator /(double d, mat2 m)
{
mat2 temp = new mat2(m);
temp.beichu_signNum(d);
return temp;
}
public static mat2 operator /(mat2 m,double d)
{
mat2 temp = new mat2(m);
temp.chu_signNum(d);
return temp;
}
public static mat2 operator /(mat2 m1, mat2 m2)
{
if (m1.Rows != m2.Rows || m1.Columns != m2.Columns) throw new Exception("维数不同不能相乘!");
int newR = m1.Rows;
int newC = m1.Columns;
double[] temp = new double;
double[] arr1 = m1.getarray();
double[] arr2 = m2.getarray();
for (int i = 0; i < newR * newC; i++)
{
temp = arr1 / arr2;
}
return new mat2(newR, newC, temp);
}
public static mat2 operator -(mat2 m, double d)
{
mat2 temp = new mat2(m);
temp.jian_signNum(d);
return temp;
}
public static mat2 operator -(double d, mat2 m)
{
mat2 temp = new mat2(m);
temp.beijian_signNum(d);
return temp;
}
public static mat2 operator -(mat2 m1, mat2 m2)
{
if (m1.Columns != m2.Columns || m1.Rows != m2.Rows) throw new Exception("矩阵维数不相等");
double[] temp = (double[])m1.getarray().Clone();
double[] arr2 = (double[])m2.getarray().Clone();
int tempL = m1.getarray().Length;
for (int i = 0; i < tempL; i++)
{
temp = temp -arr2;
}
return new mat2(m1.Rows, m1.Columns, temp);
}
#endregion
//toString
public override string ToString()
{
String temp = "";
for (int r = 0; r < numRows; r++)
{
for (int c = 0; c < numColumns; c++)
{
temp += matnum;
temp += "";
}
temp += "\r\n";
}
return temp;
}
} 晕呼呼的了。没看懂,,呵呵 abstract?是什么 程序好长啊能不能有matlab 格式的有关计算泡露点的程序呢 谢谢啦 技术流哈{:1106_366:}
页:
[1]