登录
|
注册
公司
标签
文章
搜索
经典指数
2017
高级结构
类别
公司
职位
年份
其他
添加
原因
删除
1833
浏览数
0
收藏数
设矩阵A 存在可逆矩阵 P ,求出 P 及相应的对角矩阵(体验公式编辑器如何使用)
还没有评论
分享到:
举报
纠错
0
/
512字
选择纠错区域
题目内容有错
题目标签有错
提交纠错
切换
提交评论
请先
登录
后评论.
1 个答案
0
0
publicMatrix inverseMatrix() { if(!this.isSquareMatrix()) { System.out.println("不是方阵没有逆矩阵!"); returnnull; } // 先在右边加上一个单位矩阵。 Matrix tempM =this.appendUnitMatrix(); // 再进行初等变换,把左边部分变成单位矩阵 double[][] tempData = tempM.getMatrixData(); inttempRow = tempData.length; inttempCol = tempData[0].length; // 对角线上数字为0时,用于交换的行号 intline =0; // 对角线上数字的大小 doublebs =0; // 一个临时变量,用于交换数字时做中间结果用 doubleswap =0; for(inti =0; i < tempRow; i++) { // 将左边部分对角线上的数据等于0,与其他行进行交换 if(tempData[i][i] ==0) { if(++line >= tempRow) { System.out.println("此矩阵没有逆矩阵!"); returnnull; } for(intj =0; j < tempCol; j++) { swap = tempData[i][j]; tempData[i][j] = tempData[line][j]; tempData[line][j] = swap; } // 当前行(第i行)与第line行进行交换后,需要重新对第i行进行处理 // 因此,需要将行标i减1,因为在for循环中会将i加1。 i--; // 继续第i行处理,此时第i行的数据是原来第line行的数据。 continue; } // 将左边部分矩阵对角线上的数据变成1.0 if(tempData[i][i] !=1) { bs = tempData[i][i]; for(intj = tempCol -1; j >=0; j--) { tempData[i][j] /= bs; } // 将左边部分矩阵变成上对角矩阵, // 所谓上对角矩阵是矩阵的左下角元素全为0 for(intiNow = i +1; iNow < tempRow; iNow++) { for(intj = tempCol -1; j >= i; j--) { tempData[iNow][j] -= tempData[i][j] * tempData[iNow][i]; } } } } // 将左边部分矩阵从上对角矩阵变成单位矩阵,即将矩阵的右上角元素也变为0 for(inti =0; i < tempRow -1; i++) { for(intiNow = i; iNow < tempRow -1; iNow++) { for(intj = tempCol -1; j >=0; j--) { tempData[i][j] -= tempData[i][iNow +1] * tempData[iNow +1][j]; } } } // 右边部分就是它的逆矩阵 Matrix c =null; intcRow = tempRow; intcColumn = tempCol /2; double[][] cData =newdouble[cRow][cColumn]; // 将右边部分的值赋给cData for(inti =0; i < cRow; i++) { for(intj =0; j < cColumn; j++) { cData[i][j] = tempData[i][cColumn + j]; } } // 得到逆矩阵,返回 c =newMatrix(cData); returnc; }
还没有评论
举报
切换
提交评论
请先
登录
后评论.
撰写答案
提交回答
通往牛逼的路上,请先登录!
扫描后移动端查看本题
我也分享一个题目
×
登录
注册
找回密码
记住登录
登录
快速注册
直接第三方登录
×
保存答案