Home > front end >  Getting the error "ERROR: array subscript out of range" when implementing Laplace's T
Getting the error "ERROR: array subscript out of range" when implementing Laplace's T

Time:11-18

I'm traiying to code, as exercise, a PL/pgSQL function that implements Laplace's Theorem to calculate determinant then print the solution when called. I thingI'm new in this language and can't find what's wrong with the code. I think the idea is OK, maybe there's a sintax error! Help!

CREATE OR REPLACE FUNCTION remove(matriz float[][], linha integer, coluna integer) RETURNS float[][] AS
$$
    DECLARE
        i integer;
        j integer;
        k integer;
        l integer;
        resultante float[][];
    BEGIN
        resultante := array_fill(0, ARRAY[array_length(matriz, 1)-1, array_length(matriz, 2)-1]);      
        k = 0;
        FOR i IN 1..array_length(matriz, 1) LOOP
            l = 0;
           FOR j IN 1..array_length(matriz, 1) LOOP
                IF j != coluna THEN
                    resultante[k][l] = matriz[i][j];
                    l = l   1;
                END IF;
            END LOOP;

            IF i != linha THEN
                l = l   1;
            END IF;

        END LOOP;  
        RETURN resultante;
    END;

$$ language plpgsql;

CREATE OR REPLACE FUNCTION determinant(matriz float[][], OUT produto float) AS
$$
    DECLARE
        i integer;
    BEGIN

        IF array_length(matriz, 1) <> array_length(matriz, 2) THEN
            RAISE EXCEPTION 'matrix not quadratic.';
        END IF;
        produto = 0;
        
        IF array_length(matriz, 1) = 2 THEN
            produto = matriz[1][1] * matriz[2][2] - matriz[1][2] * matriz[2][1];
        END IF;

        IF array_length(matriz, 1) > 2 THEN
            FOR i IN 1..array_length(matriz, 1) LOOP
                produto = produto   matriz[1][i] * (-1)^(0 (i-1)) * determinant(remove(matriz, 1, i));
            END LOOP;
        END IF;
    END;

$$ language plpgsql;

SELECT determinant('{{1.0,7.0, 2.0, 4.0}, {2.0, 3.0, 5.0, 4.0}, {9.0, 1.0, 8.0, 2.0}, {4.0, 1.0, 1.0, 4.0}}');

As result:

[76:1] Failed in 0 s.
[Exception, Error code 0, SQLState 2202E] ERROR: array subscript out of range
  Onde: função PL/pgSQL remove(double precision[],integer,integer) linha 15 em atribuição
função PL/pgSQL determinante(double precision[]) linha 17 em atribuição
  Line 76, column 1

CodePudding user response:

You never change k. On the other hand, you increment l too much.

One of the l = l 1 (there are two of them) should be k = k 1.

  • Related