2024-08-31 02:07:24 +05:30
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
class TraceNorm {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
|
|
|
|
System.out.print("Enter the size of the square matrix: ");
|
|
|
|
int n = sc.nextInt();
|
|
|
|
|
|
|
|
double[][] matrix = new double[n][n];
|
|
|
|
|
|
|
|
System.out.println("Enter the elements of the matrix:");
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
for (int j = 0; j < n; j++) {
|
2024-08-31 09:25:41 +05:30
|
|
|
System.out.print(
|
|
|
|
"Enter the element at (" + i + "," + j + "): "
|
|
|
|
);
|
|
|
|
matrix[i][j] = sc.nextInt();
|
2024-08-31 02:07:24 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// trace
|
|
|
|
double trace = 0;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
trace += matrix[i][i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// norm
|
|
|
|
double normSquared = 0;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
normSquared += matrix[i][j] * matrix[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double norm = Math.sqrt(normSquared);
|
|
|
|
|
|
|
|
System.out.println("Trace of the matrix: " + trace);
|
|
|
|
System.out.println("Norm of the matrix: " + norm);
|
|
|
|
|
|
|
|
sc.close();
|
|
|
|
}
|
|
|
|
}
|