用java实现矩阵条件数:给定矩阵A。求A的条件数,范数采用无穷范数。 输入格式: 第一行一个整数n,矩阵的行数(n<20) 接着n行,每行是矩阵的一行数据,按行顺序。每行n个实数。 输出格式: 一个实数,矩阵A的条件数,精确到小数点后4位。
时间: 2023-06-16 14:04:06 浏览: 147
以下是Java代码实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
double[][] matrix = new double[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextDouble();
}
}
double norm = 0;
for (int j = 0; j < n; j++) {
double sum = 0;
for (int i = 0; i < n; i++) {
sum += Math.abs(matrix[i][j]);
}
if (sum > norm) {
norm = sum;
}
}
double inverseNorm = 0;
double[][] inverseMatrix = new double[n][n];
for (int i = 0; i < n; i++) {
inverseMatrix[i][i] = 1;
}
for (int j = 0; j < n; j++) {
for (int i = j + 1; i < n; i++) {
double factor = -matrix[i][j] / matrix[j][j];
for (int k = 0; k < n; k++) {
matrix[i][k] += factor * matrix[j][k];
inverseMatrix[i][k] += factor * inverseMatrix[j][k];
}
}
}
for (int j = n - 1; j >= 0; j--) {
for (int i = j - 1; i >= 0; i--) {
double factor = -matrix[i][j] / matrix[j][j];
for (int k = 0; k < n; k++) {
matrix[i][k] += factor * matrix[j][k];
inverseMatrix[i][k] += factor * inverseMatrix[j][k];
}
}
}
for (int i = 0; i < n; i++) {
double sum = 0;
for (int j = 0; j < n; j++) {
sum += Math.abs(inverseMatrix[i][j]);
}
if (sum > inverseNorm) {
inverseNorm = sum;
}
}
System.out.printf("%.4f", norm * inverseNorm);
}
}
```
首先读入矩阵的行数n和矩阵本身,然后计算矩阵的无穷
阅读全文