ในบทความนี้ เราจะทำความเข้าใจวิธีค้นหาการติดตามและค่าปกติของเมทริกซ์ที่กำหนด ค่าปกติของเมทริกซ์คือรากที่สองของผลบวกกำลังสองขององค์ประกอบทั้งหมดของเมทริกซ์ การติดตามของเมทริกซ์เป็นผลรวมขององค์ประกอบทั้งหมดที่มีอยู่ในแนวทแยงหลัก (บนซ้ายไปขวาล่าง)
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
สมมติว่าข้อมูลที่เราป้อนคือ −
The matrix is defined as: 2 3 4 5 2 3 4 6 9
ผลลัพธ์ที่ต้องการจะเป็น −
Trace value: 13.0 Normal value: 14.142135623730951
อัลกอริทึม
Step 1 - START Step 2 - Declare an integer matrix namely input_matrix Step 3 - Define the values. Step 4 - To compute trace value, iterate over each element of the matrix using two for-loops, add the diagonal elements and store the value. Step 5 - To compute the normal value, iterate over each element of the matrix using two for-loops, compute the sum of square of each element, them compute the square root of the value and store the value. Step 5 - Display the result Step 6 - Stop
ตัวอย่างที่ 1
ที่นี่ เรารวมการดำเนินการทั้งหมดเข้าด้วยกันภายใต้ฟังก์ชัน 'หลัก'
public class NormalAndTrace {
public static void main(String args[]) {
int[][] input_matrix = {
{2, 3, 4},
{5, 2, 3},
{4, 6, 9}
};
int i, j, matrix_size = 3;
double trace = 0, square = 0, normal = 0;
System.out.println("The matrix is defined as: ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++)
System.out.print(input_matrix[i][j]+" ");
System.out.println(" ");
}
System.out.println("\nThe Trace value of the matrix is ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++) {
if(i == j) {
trace = trace + (input_matrix[i][j]);
}
}
}
System.out.println(trace);
System.out.println("\nThe Normal value of the matrix is ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++) {
square = square + (input_matrix[i][j])*(input_matrix[i][j]);
}
}
normal = Math.sqrt(square);
System.out.println(normal);
}
} ผลลัพธ์
The matrix is defined as: 2 3 4 5 2 3 4 6 9 The Trace value of the matrix is 13.0 The Normal value of the matrix is 14.142135623730951
ตัวอย่างที่ 2
ในที่นี้ เราสรุปการดำเนินการเป็นฟังก์ชันที่แสดงการเขียนโปรแกรมเชิงวัตถุ
public class NormalAndTrace {
static int matrix_size = 3;
static void normal_trace(int input_matrix[][]){
int i, j;
double trace = 0, square = 0, normal = 0;
System.out.println("\nThe Trace value of the matrix is ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++) {
if(i == j) {
trace = trace + (input_matrix[i][j]);
}
}
}
System.out.println(trace);
System.out.println("\nThe Normal value of the matrix is ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++) {
square = square + (input_matrix[i][j])*(input_matrix[i][j]);
}
}
normal = Math.sqrt(square);
System.out.println(normal);
}
public static void main(String args[]) {
int i, j;
int[][] input_matrix = { {2, 3, 4},
{5, 2, 3},
{4, 6, 9}
};
System.out.println("The matrix is defined as: ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++)
System.out.print(input_matrix[i][j]+" ");
System.out.println(" ");
}
normal_trace(input_matrix);
}
} ผลลัพธ์
The matrix is defined as: 2 3 4 5 2 3 4 6 9 The Trace value of the matrix is 13.0 The Normal value of the matrix is 14.142135623730951