/*
 * 我刚学JAVA看了一天才看懂这个程序!!好好学以后把它的功能扩大再做个界面^^
 * 一个计算三元方程组的程序2x+4y+6z=10
 *                      x-2y-3z=6
 *                      2x-2y+4z=8
 *                    把方程组变为2维矩阵  matrix[][]={{2,4,6,10},
 *                                     {1,-2,-3,6},
*                                      {2,-2,4,8}};
* 通过程序把2维矩阵变为主对角线都为1  matrix[][]={{1.0, 0.0, 0.0, 5.5},
*                                    {-0.0, 1.0, 0.0, 0.5},
*                                     {0.0, 0.0, 1.0, -0.5}};   
*                 方程的解就为x=5.5,y=0.5,z=-0.5                                                                
 */class juzhen
{public static void swapRows(double matrix[][],int row1,int row2 )       //数组的两行交换
{
for(int col=0;col<matrix[row1].length;col++)
{
double temp=matrix[row1][col];
matrix[row1][col]=matrix[row2][col];
       matrix[row2][col]=temp; 
     }
}
public static void mulRow(double matrix[][],double numble,int row)       //数组的一行同时*number
{
for(int col=0;col<matrix[row].length;col++ )
{
    matrix[row][col]*=numble;
}
}   
public static void addRow(double matrix[][],double number,int row1,int row2)    //数组的row1行同时*number再与row2行相加赋给row2,
{                                                                                      //row1不变,row2被替换
for(int col=0;col<matrix[row1].length;col++)
{

matrix[row2][col]+=number*matrix[row1][col];
}
}
public static void showMatrix(double matrix[][])             //显示数组
{
for(int row=0;row<matrix.length;row++)
{
for(int col=0;col<matrix[row].length;col++)
    {
System.out.print(matrix[row][col]+"\t");
    }
System.out.println();
}
}
public static double[][] creatMatrix()                        //创建数组
{
double matrix[][]={{2,4,6,10},
              {1,-2,-3,6},
              {2,-2,4,8}};
       return matrix;           
}
public static void colMethod(double matrix[][],int col)     //把一列变为1,0
{
mulRow(matrix,1/matrix[col][col],col);
for(int row=0;row<matrix.length;row++)
{
   if(row!=col)
   {
addRow(matrix,-matrix[row][col],col,row);
    }
}

}
public static boolean foundZero(double matrix[][],int diag)           //判断矩阵主对角线有没有为0的数,有的话两行替换
{
for(int col=diag;col<matrix.length;col++)
{
if (matrix[col][diag]!=0)
{
swapRows(matrix,col,diag);
return true;

             }


}
return false;
}
public static boolean allColMethod(double matrix[][])    //把矩阵的多列变为1,0
{
for(int col=0;col<matrix.length;col++)
{
if(foundZero(matrix,col))
        colMethod(matrix,col);
else
return false;
}
return true;
}public static void main(String args[])      //主程序
{
double matrix[][]=creatMatrix();
showMatrix(matrix);
System.out.println();
if(allColMethod(matrix))
  showMatrix(matrix);
else
System.out.println("no sole");
}
}

解决方案 »

  1.   

    要这么麻烦吗?我记着《数值分析》这本书中说只要搞搞上三角,下三角就出来了,可惜当时编的Matlab程序了。。
      

  2.   

    <<数值分析>>上有很多解方程组的方法,其实用什么语言编程并不重要,重要的是它用到的是什么算法.
      

  3.   

    啊,这个用不着自己实现的,Apache Commons Math有现成的工具包,直接可以用的。Apache Commons Math的链接
    http://commons.apache.org/mathCommons Math线性代数包的参考,里面就有解方程组的
    http://commons.apache.org/math/userguide/linear.html