<commit_before>#! parrot-nqp

INIT {
    pir::load_bytecode('./src/nqp/pla.pbc');
}

main();

sub main() {
    pir::say("Matrix A:");
    my $A := NumMatrix2D.new();
    $A.initialize_from_args(3, 3,
                            1.0, 2.0, 3.0,
                            4.0, 5.0, 6.0,
                            7.0, 8.0, 9.0);
    pir::say($A);
    my $B := pir::clone($A);
    gaussian_elimination($A);
    pir::say("Row Eschelon Form:");
    pir::say($A);

    gauss_jordan_elimination($B);
    pir::say("Reduced Row Eschelon Form:");
    pir::say($B);
}

sub gaussian_elimination($A) {
    my $i := 0;
    my $j := 0;
    my $m := pir::getattribute__PPS($A, "rows");
    my $n := pir::getattribute__PPS($A, "cols");
    while ($i < $m && $j < $n) {
        my $maxi := pir::clone($i);
        my $k := pir::clone($i) + 1;
        while ($k < $m) {
            if ($A.item_at($k, $j) > $A.item_at($maxi, $j)) {
                $maxi := pir::clone($k);
            }
            $k++;
        }
        if ($A.item_at($maxi, $j) != 0) {
            pir::say("row " ~ $maxi ~ " <-> row " ~ $i);
            $A.row_swap($maxi, $i);
            #pir::say($A);

            my $scale := $A.item_at($i, $j);
            pir::say("row " ~ $i ~ " / " ~ $scale);
            $A.row_scale($i, (1/$scale));
            #pir::say($A);

            my $u := pir::clone($i) + 1;
            while ($u < $m) {
                my $gain_u := $A.item_at($u, $j);
                pir::say((-$gain_u) ~ " * row " ~ $u ~ " -> row " ~ $i);
                $A.row_combine($i, $u, -$gain_u);
                #pir::say($A);

                $u++;
            }
            $i++;
        }
        $j++;
    }
}

sub gauss_jordan_elimination($A) {
    my $i := 0;
    my $j := 0;
    my $m := pir::getattribute__PPS($A, "rows");
    my $n := pir::getattribute__PPS($A, "cols");
    while ($i < $m && $j < $n) {
        my $maxi := pir::clone($i);
        my $k := pir::clone($i) + 1;
        while ($k < $m) {
            if ($A.item_at($k, $j) > $A.item_at($maxi, $j)) {
                $maxi := pir::clone($k);
            }
            $k++;
        }
        if ($A.item_at($maxi, $j) != 0) {
            pir::say("row " ~ $maxi ~ " <-> row " ~ $i);
            $A.row_swap($maxi, $i);
            #pir::say($A);

            my $scale := $A.item_at($i, $j);
            pir::say("row " ~ $i ~ " / " ~ $scale);
            $A.row_scale($i, (1/$scale));
            #pir::say($A);

            my $u := pir::clone($i) + 1;
            while ($u < $m) {
                my $gain_u := $A.item_at($u, $j);<commit_msg>fix the gauss elimination example. Something has changed in parrot with respect to int/float algebra, and the results were coming out wrong. Now, force the scale factor to be a Float<commit_after>#! parrot-nqp

INIT {
    pir::load_bytecode('./src/nqp/pla.pbc');
}

main();

sub main() {
    pir::say("Matrix A:");
    my $A := NumMatrix2D.new();
    $A.initialize_from_args(3, 3,
                            1.0, 2.0, 3.0,
                            4.0, 5.0, 6.0,
                            7.0, 8.0, 9.0);
    pir::say($A);
    my $B := pir::clone($A);
    gaussian_elimination($A);
    pir::say("Row Eschelon Form:");
    pir::say($A);

    gauss_jordan_elimination($B);
    pir::say("Reduced Row Eschelon Form:");
    pir::say($B);
}

sub gaussian_elimination($A) {
    my $i := 0;
    my $j := 0;
    my $m := pir::getattribute__PPS($A, "rows");
    my $n := pir::getattribute__PPS($A, "cols");
    while ($i < $m && $j < $n) {
        my $maxi := pir::clone($i);
        my $k := pir::clone($i) + 1;
        while ($k < $m) {
            if ($A.item_at($k, $j) > $A.item_at($maxi, $j)) {
                $maxi := pir::clone($k);
            }
            $k++;
        }
        if ($A.item_at($maxi, $j) != 0) {
            pir::say("row " ~ $maxi ~ " <-> row " ~ $i);
            $A.row_swap($maxi, $i);
            #pir::say($A);

            my $scale := $A.item_at($i, $j);
            $scale := pir::div__NnN(1.0, $scale);
            pir::say("row $i / $scale ($scale)");
            $A.row_scale($i, $scale);
            #pir::say($A);

            my $u := pir::clone($i) + 1;
            while ($u < $m) {
                my $gain_u := $A.item_at($u, $j);
                pir::say((-$gain_u) ~ " * row " ~ $u ~ " -> row " ~ $i);
                $A.row_combine($i, $u, -$gain_u);
                #pir::say($A);

                $u++;
            }
            $i++;
        }
        $j++;
    }
}

sub gauss_jordan_elimination($A) {
    my $i := 0;
    my $j := 0;
    my $m := pir::getattribute__PPS($A, "rows");
    my $n := pir::getattribute__PPS($A, "cols");
    while ($i < $m && $j < $n) {
        my $maxi := pir::clone($i);
        my $k := pir::clone($i) + 1;
        while ($k < $m) {
            if ($A.item_at($k, $j) > $A.item_at($maxi, $j)) {
                $maxi := pir::clone($k);
            }
            $k++;
        }
        if ($A.item_at($maxi, $j) != 0) {
            pir::say("row " ~ $maxi ~ " <-> row " ~ $i);
            $A.row_swap($maxi, $i);
            #pir::say($A);

            my $scale := $A.item_at($i, $j);
            $scale := pir::div__NnN(1.0, $scale);
            pir::say("row $i / $scale ($scale)");
            $A.row_scale($i, $scale);
            #pir::say($A);

            my $u := pir::clone($i) + 1;
            while ($u < $m) {
                my $gain_u := $A.item_at($u, $j);
{'fraction_non_alphanumeric': 0.22811525050467976, 'fraction_numerical': 0.012479354009910076, 'mean_word_length': 1.5115207373271888, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2816160', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2082, 'n_words': 421}