与えられた行列のトレースと法線を見つけるJavaプログラム
この記事では、特定の行列のトレースと法線を見つける方法を理解します。行列の法線は、行列のすべての要素の二乗和の平方根です。行列のトレースは、主対角線(左上から右下)に存在するすべての要素の合計です。
以下は同じのデモンストレーションです-
入力がであると仮定します −
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
ここでは、「main」関数の下ですべての操作をバインドします。
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
-
長方形の周囲を見つけるJavaプログラム
この記事では、長方形の周囲を見つける方法を理解します。長方形の周囲長は、長方形のすべての辺の長さを加算して計算されます。 以下は長方形のデモンストレーションです。長方形の周囲は、長方形の2つの長さと2つの幅の全長です- 入力 入力が-であると仮定します The length of the sides of a rectangle are : 5, 8, 5, 8 出力 必要な出力は-になります Perimeter : 26 アルゴリズム Step 1 – START Step 2 – Declare 5 floating point variabl
-
行列の転置を見つけるPythonプログラム
この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 行列が与えられた場合、転置を同じ行列に格納して表示する必要があります。 行列の転置は、行を列に、列を行に変更することで得られます。つまり、A行列の転置はA[i][j]をA[j][i]に変更することで得られます。 以下に示す実装を見てみましょう- 例 N = 4 def transpose(A): for i in range(N): for j in range(i+1, N): &nbs