Efficient method to lower array order in PHP

Asked

Viewed 111 times

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) 
          );

1 answer

2


I created a solution using the Chiò. For example, I used the following array:

    $matriz = [
        [1,2,5,3,2],
        [1,3,7,3,4], 
        [0,5,2,2,1], 
        [1,3,0,1,2], 
        [0,6,7,4,7] 
    ];

In order to follow this rule, I used the array_column to get the whole first column and used the index 0 from the array to pick up the first line:

inserir a descrição da imagem aqui

In this way, I was able to multiply and subtract each element using recursiveness. Note that the loop starts from 1. Thus both the first column and the first row of the matrix are ignored without the need to use the unset(since you asked for performance)

    $novaMatriz = abaixarOrdem($matriz);
    print_r($novaMatriz);

function abaixarOrdem($matriz) {

    $ordem = count($matriz[0]);
    $coluna = array_column($matriz, 0);
    $linha  = $matriz[0];
    $novaMatriz = [];

    for($x = 1; $x < $ordem; $x++){

        for($y = 1; $y < $ordem; $y++){
            // aqui é calculado o valor do elemento 
            // multiplicando o valor das colunas e subtraindo o número da matriz
            $valor = $matriz[$x][$y] - ($coluna[$x] * $linha[$y]);
            $novaMatriz[$x - 1][$y - 1] = $valor;
        }

    }
    if(count($novaMatriz[0]) > 3)
        $novaMatriz = abaixarOrdem($novaMatriz); // refaz até ser ordem 3
    return $novaMatriz;
}

Upshot:

    [-8, 2,-9]
    [-7,-2,-2]
    [-5, 4,-5]

Observing

I did not create permutation for the index case [0][0] of the matrix no is 1. Just as I did not apply the Jacobi’s theorem if not there is the value 1 in the matrix. This solution is just an example for lowering of the matrix order

  • Our guy was even worth, I gave an improved in the code and is perfect! Thank you very much

  • I was very confused at the time of creating the loops

  • 1

    @Sampaioleal tranquil! If you want to suggest some edition, you can insert!

  • 1

    @Sampaioleal yes... I was also a little confused! This problem gave me a little work to create a solution... =) Hug!

  • @Sampaioleal then take a look at the class I created! https://gist.github.com/andrei-coelho/a355fefa98473371d30827aae01db2f2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.