程序代码:
using System; using System.Collections.Generic; using System.Text; namespace ArrRev { public static class Arr_Rev//假如不存在逆矩阵,将数组清空为0。高手注意数组是引用,dll返回的是逆矩阵,原来的数据被覆盖了!为了简单起见,请先将矩阵从左到右,从上到下转换成一维的数组 { private static double Mul_TWO(params double[] m) //二阶行列式的值 { double Result = m[0] * m[3] - m[1] * m[2]; return Result; } private static double Mul_THREE(params double[] m) //三阶行列式的值 { double temp1 = m[0] * Mul_TWO(m[4], m[5], m[7], m[8]); double temp2 = m[1] * Mul_TWO(m[3], m[5], m[6], m[8]); double temp3 = m[2] * Mul_TWO(m[3], m[4], m[6], m[7]); double Result = temp1 + temp3 - temp2; return Result; } public static void Rev_TWO(double[] Arr) //求二维矩阵的逆 { double Result = Arr[0] * Arr[3] - Arr[1] * Arr[2]; if (Result != 0) { double Num1=Arr[0]; double Num2=Arr[1]; double Num3=Arr[2]; double Num4=Arr[3]; Arr[0] = Num4 / Result; Arr[1] = -Num2 / Result; //求伴随矩阵 Arr[2] = -Num3 / Result; Arr[3] = Num1 / Result; } else { for (int i = 0; i < 4; i++) Arr[i] = 0; } } public static void Rev_THREE(double[] Arr) //求三维矩阵的逆 { double Result = Mul_Three(Arr); if (Result != 0) { double[] m = new double[9]; m[0] = Mul_TWO(Arr[4], Arr[5], Arr[7], Arr[8]); //求伴随矩阵 m[1] = -Mul_TWO(Arr[1], Arr[2], Arr[7], Arr[8]); m[2] = Mul_TWO(Arr[1], Arr[2], Arr[4], Arr[5]); m[3] = -Mul_TWO(Arr[3], Arr[5], Arr[6], Arr[8]); m[4] = Mul_TWO(Arr[0], Arr[2], Arr[6], Arr[8]); m[5] = -Mul_TWO(Arr[0], Arr[2], Arr[3], Arr[5]); m[6] = Mul_TWO(Arr[3], Arr[4], Arr[6], Arr[7]); m[7] = -Mul_TWO(Arr[0], Arr[1], Arr[6], Arr[7]); m[8] = Mul_TWO(Arr[0], Arr[1], Arr[3], Arr[4]); for (int i = 0; i < 9; i++) { Arr[i] = m[i] / Result; } } else { for (int i = 0; i < 9; i++) { Arr[i] = 0; } } } } }
[ 本帖最后由 qq1023569223 于 2011-4-23 00:53 编辑 ]