首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

被线代作业弄急了,写个java算逆矩阵。未遂。怎么把小数转化成分数

2012-04-16 
被线代作业弄急了,写个java算逆矩阵。未遂。如何把小数转化成分数?线代老要写逆矩阵,连错4题,一怒之下写了个

被线代作业弄急了,写个java算逆矩阵。未遂。如何把小数转化成分数?
线代老要写逆矩阵,连错4题,一怒之下写了个java算逆矩阵。代码如下

Java code
/** * Inverse a matrix. *  * @author Small_Light * @version InverseMatrix 0.1 */import java.util.Scanner;public class InverseMatrix {    public static void main(String args[]) {        Scanner s = new Scanner(System.in);//        System.out.println("    Please print the row of your mathix : ");        System.out.println("    请输入矩阵的行数 : ");        int row = s.nextInt();//        System.out.println("    Please print  your mathix : ");        System.out.println("    请输入你的矩阵: ");        double[][] matrix_old = new double[row][row];        double[][] matrix_new = new double[row][row];        for (int i = 0; i < row; i++) {            for (int j = 0; j < row; j++) {                matrix_old[i][j] = s.nextInt();                matrix_new[i][j] = 0;                if (i == j) {                    matrix_new[i][j] = 1;                }            }        }        // **************以上内容完成矩阵的读取并且创建单位矩阵**************        double temp;// col =1;j=2~row        for (int col = 0; col < row; col++) {// col表示为处理到第几列            for (int j = col + 1; j < row; j++) {// J标记为第几行                temp = -matrix_old[j][col] / matrix_old[col][col];                for (int i = 0; i < row; i++) {// 单独处理第J行的每一个元素                    matrix_old[j][i] = matrix_old[j][i] + matrix_old[col][i]                            * temp;                    matrix_new[j][i] = matrix_new[j][i] + matrix_new[col][i]                            * temp;                }            }        }        // 处理成为了上阶梯矩阵        for (int i = 0; i < row; i++) {// i为行数,J为列数            for (int j = 0; j < row; j++) {                if (i < j) {                    temp = matrix_old[i][j];                    matrix_old[i][j] = matrix_old[j][i];                    matrix_old[j][i] = temp;                    temp = matrix_new[i][j];                    matrix_new[i][j] = matrix_new[j][i];                    matrix_new[j][i] = temp;                }            }        }        for (int col = 0; col < row; col++) {// col表示为处理到第几列            for (int j = col + 1; j < row; j++) {// J标记为第几行                temp = -matrix_old[j][col] / matrix_old[col][col];                for (int i = 0; i < row; i++) {// 单独处理第J行的每一个元素                    matrix_old[j][i] = matrix_old[j][i] + matrix_old[col][i]                            * temp;                    matrix_new[j][i] = matrix_new[j][i] + matrix_new[col][i]                            * temp;                }            }        }        for (int i = 0; i < row; i++) {            temp = matrix_old[i][i];            for (int q = 0; q < row; q++) {                matrix_old[i][q] = matrix_old[i][q] / temp;                matrix_new[i][q] = matrix_new[i][q] / temp;            }        }//        System.out.println("The inverse mathix is : ");        System.out.println("该矩阵的逆矩阵是  : ");        show(matrix_new);    }    public static void show(double[][] matrix) {        for (int i = 0; i < matrix.length; i++) {            for (int j = 0; j < matrix[i].length; j++) {                System.out.printf("%.4f\t",matrix[i][j] );            }            System.out.println();        }    }}


写完之后确实能算逆矩阵了,但是算出来的结果是小数,不能直接抄到作业本上啊,急死我了。
后来去图书馆,看到有 矩阵类 好像是 Mathix,但是我还是想用我自己写的这个。
大致求解原理是什么高斯消去……

现在问题是如何把结果的小数转化成分数?
逆矩阵球出来的结果肯定是可以用分数来表示的,也就是说算出来的小数肯定是循环小数。
但是用for来找出循环节然后再转化是不是太烦了。。
然后我想自己写个类,类似于Integer和Double 的分数类,然后补上加减乘除等运算,替换掉一开始定义的int。
想问一下java中有没有什么分数类?或者说有没有什么更好的解决方案?


[解决办法]
符号代数,也算是上了级别的难题了。

老老实实用 玛斯马提克(mathematica) 或者 马普(maple) 吧。
------解决方案--------------------


偶也是新手哦,好像没有分数类哦。自己写了一个。偶没怎么学过矩阵,所以用不上的话楼主不要喷我!

Java code
public class Fraction {    private int numerator;//分子    private int denominator;//分母    public int findGreatestCommonDivisor(int a,int b) //最大公约数    {        while(a%b!=0)        {            int tmp=b;            b=a%b;            a=tmp;        }        return b;    }    public Fraction(int a, int b)//构造函数    {        if(a*b>0)        {            a=Math.abs(a);            b=Math.abs(b);        }        else        {            a=-Math.abs(a);            b=Math.abs(b);        }        int c=findGreatestCommonDivisor(a,b);        numerator=a/c;        denominator=b/c;    }    public Fraction(int a){        this(a,1);    }    public Fraction add(Fraction f)//+    {        int a,b;        int c=findGreatestCommonDivisor(this.denominator ,f.denominator );                b=this.denominator /c*f.denominator ;        a=this.numerator *(f.denominator/c) +f.numerator*(this.denominator /c) ;        return new Fraction(a,b);    }    public Fraction minus(Fraction f)//-    {        int a,b;        int c=findGreatestCommonDivisor(this.denominator ,f.denominator );        b=this.denominator /c*f.denominator ;        a=this.numerator *(f.denominator/c) -f.numerator*(this.denominator /c) ;        return new Fraction(a,b);    }    public Fraction Multiply(Fraction f)//*    {        int a,b;        int c1=findGreatestCommonDivisor(this.numerator ,f.denominator );        int c2=findGreatestCommonDivisor(this.denominator ,f.numerator );        a=this.numerator /c1*f.numerator /c2;        b=this.denominator /c2*f.denominator /c1;        return new Fraction(a,b);    }    public Fraction Divide(Fraction f)    {        return Multiply(new Fraction(f.denominator ,f.numerator ));    }    public String toString()    {        return numerator +"/"+denominator;     }    public static void main(String[] args) {                Fraction f=new Fraction(3,4);        Fraction f1=new Fraction(3,-7);                System.out.println(f.add(f1));        System.out.println(f.minus(f1));        System.out.println(f.Multiply(f1));        System.out.println(f.Divide(f1));    }} 

热点排行