Código PHP:
public static matrix multiplicacion(matrix a, matrix b) {
matrix result = new matrix (a.n);
for (int i = 0; i < a.n; i++) {
for (int j = 0; j < a.n; j++) {
result.x[i][j] = a.x[i][j] * b.x[j][i];
}
}
return result;
}
para suma, resta y division solo cambia el signo de la operacion
creas un metodo par cada operacion .
result.x[i][j] = a.x[i][j] + b.x[j][i];
result.x[i][j] = a.x[i][j] - b.x[j][i];
result.x[i][j] = a.x[i][j] / b.x[j][i];
Código PHP:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
//un ejemplo de como podria ser... No es mio pero te servirá
public class program {
public static void main(String args []) {
BufferedReader cin = new BufferedReader (new InputStreamReader (System.in));
String option = " ";
int opt = 0;
double escalar = Math.PI;
double [][]a_1 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
matrix a = new matrix(a_1);
double [][]b_1 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
matrix b = new matrix(b_1);
// menu
System.out.println("*******************************************\n"+
"* ¿ Qué operación deseas hacer ? *\n"+
"* *\n"+
"* 1. Suma de matrices *\n"+
"* 2. Resta de matrices *\n"+
"* 3. Multiplicación de matrices *\n"+
"* 4. Traza de una matriz *\n"+
"* 5. Transpuesta de una matriz *\n"+
"* 6. Multiplicación por un escalar *\n"+
"* *\n"+
"*******************************************");
// end menu
try {
option = cin.readLine();
}
catch (IOException e) {};
opt = Integer.parseInt(option);
//
if (opt == 1) {
matrix r = matrix_operations.plus(a,b);
System.out.println( " El resulatdo es: "+ r );
}
else if (opt == 2) {
matrix r = matrix_operations.minus(a,b);
System.out.println( " El resulatdo es: "+ r );
}
else if (opt == 3) {
matrix r = matrix_operations.multiplication(a,b);
System.out.println( " El resulatdo es: "+ r );
}
else if (opt == 4) {
double r = matrix_operations.trace(a);
System.out.println( " El resulatdo es: "+ r );
}
else if (opt == 5) {
matrix r = matrix_operations.transpouse(a);
System.out.println( " El resulatdo es: "+ r );
}
else if (opt == 6) {
matrix r = matrix_operations.escalar_multiplication(a,escalar);
System.out.println( " El resulatdo es: "+ r );
}
}
}