السلام عليكم
موضوعي اني اعمل عمليات ضرب وجمع وتغيير والكرونكر واستخدام صيغة رياضية لمصفوفات
انا عملت الجمع والضرب والتغيير....لكن الـ : Kronecker لم افهمه اطلاقا ولا كيفية عمله تماما وللعلم انا مجبر اني اعمله في كلاس واحد فقط
وهذا الكود تبع العمليات اللي سويتها ..ممكن احد يشرحلي كيف اسوي العملية بالمختصر او يساعدني بالكود كرؤوس اقلام فضلا لا امرا
شكرا لكم
الكود
package test344;
import java.util.Scanner;
public class NewClass {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Task 0: Addition Matrices.");
System.out.println("Remember, rows and columns in two matrices should be similar.");
System.out.println("Enter the number of rows and columns of matrix:");
int m = input.nextInt();
int n = input.nextInt();
int [][] t1= new int[m][n];
int [][] t2 = new int[m][n];
System.out.println("Enter the elements of first matrix:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
t1[j] = input.nextInt();
}
}
System.out.println("Enter the elements of second matrix:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
t2[j] = input.nextInt();
}
}
int[][] result1;
int[][] result2 = new int[m][n];
result1 = addMatricies(t1,t2);
//printMatrix(result1);
System.out.println();
addMatricies(t1,t2,result2);
printMatrix(result2);
System.out.println("");
System.out.println("<><><><><>End<>of<>Task(0)<><><><><><><");
System.out.println("");
System.out.println("Task 1: Transpose Matrices. ");
System.out.println("");
transpose();
System.out.println("");
System.out.println("<><><><><>End<>of<>Task(1)<><><><><><><");
System.out.println("");
System.out.println("Task 2: Multiplication Matrices.");
System.out.println("Remember, columns(matrix1) = rows(matrix2).");
multiply();
System.out.println("");
System.out.println("<><><><><>End<>of<>Task2<><><><><><>");
}
static int[][] addMatricies(int[][] m1, int[][] m2) {
int[][] result = new int[m1.length][m1[0].length];
// Go through every row in the matricies...
for (int row=0; row<m1.length; row++) {
// Go through every column in the current row...
for (int col=0; col<m1[row].length; col++) {
// Generate the sum by adding the corresponding
// elements of m1 and m2.
result[row][col] = m1[row][col] + m2[row][col];
}
}
return result;
}
static int[][] addMatricies(int[][] m1, int[][] m2, int[][] result2) {
// Go through every row in the matricies...
for (int row=0; row<m1.length; row++) {
// Go through every column in the current row...
for (int col=0; col<m1[row].length; col++) {
// Generate the sum by adding the corresponding
// elements of m1 and m2.
result2[row][col] = m1[row][col] + m2[row][col];
}
}
return result2;
}
static void printMatrix(int[][] printMe) {
System.out.print("the addition array =");
for (int col=0; col<printMe[0].length; col++) {
}
// Go to the next line before printing the matrix.
System.out.println();
// Go through every row in the matrix...
for (int row=0; row<printMe.length; row++) {
// Display the row number at the left of the row.
// Go through every column in the current row...
for (int col=0; col<printMe[row].length; col++) {
System.out.print(printMe[row][col] + "\t");
}
System.out.println();
}
}
public static void transpose(){
//declare all variables/arrays
int idx1, idx2;
//set up the scanner and get the number of rows and columns in the array
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of rows: ");
idx1 = scan.nextInt();
System.out.print("Enter number of columns: ");
idx2 = scan.nextInt();
//set the size
int [][]array = new int[idx1][idx2];
int [][]matrix = new int[idx2][idx1];
for(int j = 0; j < idx1; j++)
{
System.out.println("Enter " + idx2 + " elements for row " + (j + 1) + ":");
for(int k = 0; k < idx2; k++)
{
array[j][k] = scan.nextInt();
}
}
//matrix = array;
for(int h = 0; h < idx1; h++)
{
for(int i = 0; i < idx2; i++)
{
matrix[h] = array[h];
}
}
System.out.println("The original matrix is ->");
for(int l = 0; l < idx1; l++)
{
for(int m = 0; m < idx2; m++)
{
System.out.print("[" + array[l][m] + "]");
}
System.out.println();
}
System.out.println("The tranpose matrix is ->");
for(int l = 0; l < idx2; l++)
{
for(int m = 0; m < idx1; m++)
{
System.out.print("[" + matrix[l][m] + "]");
}
System.out.println();
}
}
public static void multiply() {
Scanner input = new Scanner(System.in);
System.out.println("Write the rows and columns for matrix A: ");
int m = input.nextInt();
int n = input.nextInt();
System.out.println("Write the rows and columns for matrix B: ");
int f = input.nextInt();
int g = input.nextInt();
int [][] A = new int[m][n];
int [][] B = new int[f][g];
int [][] C = new int[m][g];
System.out.println("Enter elements for matrix A : ");
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
A[j] = input.nextInt();
}
}
System.out.println("Enter elements for matrix B : ");
for (int i = 0; i < B.length; i++) {
for (int j = 0; j < B.length; j++) {
B[j] = input.nextInt();
}
}
System.out.println("Matrix A: ");
for (int i = 0; i < A.length; i++) {
System.out.println();
for (int j = 0; j < A.length; j++) {
System.out.print(A[j] + " ");
}
}
System.out.println();
System.out.println();
System.out.println("Matrix B: ");
for (int i = 0; i < B.length; i++) {
System.out.println();
for (int j = 0; j < B.length; j++) {
System.out.print(B[j] + " ");
}
}
System.out.println();
System.out.println();
System.out.println("Result is: ");
System.out.println("");
for (int i = 0; i <A.length ; i++) {
for (int j = 0; j <B[0].length; j++) {
for ( int k = 0; k < A[0].length; k++)
C[j] += A[k] * B[k][j];
}
}
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B[0].length; j++) {
System.out.print(+C[j]+" ");
}
System.out.println();
}
}
}