Add OOP/Java/Lab/Week4/TraceNorm.java
This commit is contained in:
parent
46fa59b5f4
commit
5d71be06a8
40
OOP/Java/Lab/Week4/TraceNorm.java
Normal file
40
OOP/Java/Lab/Week4/TraceNorm.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
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++) {
|
||||||
|
matrix[i][j] = sc.nextDouble();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user