2
I’m developing a determinant calculator in PHP
In the Matrix class I created some functions, among them the function Calc(order, matrix). This function calls another to lower the array order to $order=3, and then executes the sarrus($matrix).
Note: This matrix variable will be changed whenever the order is lowered, that is, the original matrix will be saved in another variable!
Well, I wish to know the best method to lower the order of the matrix up to 3, if possible an example of code, I tried to use Flat but I got very confused in the loops q decided to give up for a while.
public function calc($ordem, $matriz)
{
//Se a ordem for igual a 1: o elemento é o determinante
if ($ordem == 1) {
$this->det = $matriz[0][0];
}
//Se a ordem for igual a 2: chama a função @segOrdem
else if ($ordem == 2) {
segOrdem($matriz);
}
//Se a ordem for 3: chama a função @sarrus
else if ($ordem == 3) {
sarrus($matriz);
}
//Se a ordem for maior que 3: chama a função @abaixarOrdem para abaixar a ordem da matriz até 3 e logo após usar @sarrus para se ter o determinante
else if ($ordem > 3) {
$matriz = abaixarOrdem($matriz, $ordem);
sarrus($matriz);
}
return $this->det;
}
The layout of the matrix:
$matriz = array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
Our guy was even worth, I gave an improved in the code and is perfect! Thank you very much
– Sampaio Leal
I was very confused at the time of creating the loops
– Sampaio Leal
@Sampaioleal tranquil! If you want to suggest some edition, you can insert!
– Andrei Coelho
@Sampaioleal yes... I was also a little confused! This problem gave me a little work to create a solution... =) Hug!
– Andrei Coelho
@Sampaioleal then take a look at the class I created! https://gist.github.com/andrei-coelho/a355fefa98473371d30827aae01db2f2
– Andrei Coelho