|
以前用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[3*pcL];
for (int index = 0; index < pcL; index++)
{
matnum[index]= Pc[index];
matnum[index + pcL] = Tc[index];
matnum[index + 2 * pcL] = w[index];
}
}
}
//构造函数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[newR];
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[0];
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[dataL];
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[darrayL * darrayL];
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[newR * newC];
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[newR * newC];
for (int r = 0; r < newR; r++)
{
for (int c = 0; c < newC; c++)
{
newarr[r + c * newR] = oldarr[c + r * newC];
}
}
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[arrL];
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[arrL];
for (int i = 0; i < arrL; i++)
{
newarr = oldarr/All;
}
return new mat2(newR, newC, newarr);
}
#endregion
字数超限 待续 |
|