Matriks dan perkalian antar matriks , Tugas kuliah DAA

3

May 23, 2013 by Prima

Dalam matematika, matriks adalah kumpulan bilangan, simbol, atau ekspresi, berbentuk persegi panjang yang disusun menurut baris dan kolom. Bilangan-bilangan yang terdapat di suatu matriks disebut dengan elemen atau anggota matriks.

oke karena saya waktu pemberian tugas saya gak masuk (lazzysomnia alias keset) jadi saya juga ga begitu ngerti tugasnya itu suruh ngapain makanya saya cuma tau suruh buat 3 matriks dengan matriks A ordo mxn ,matriks B ordo nxo, dan matriks C ordo oxp sehingga nanti bisa dikalikan (AXB)XC dan AX(BXC) cuma itu ya udah saya cuma buat programnya saja dalam bahasa php dimana user menginputkan ordo dan range random untuk elemen pada matriks.

class matriks {

    var $matriks1 = array();
    var $matriks2 = array();
    var $matriks3 = array();
    var $m, $n, $o, $p;
    var $min, $max;

    function buatmatriks($m, $n, $o, $p, $min, $max) {
        $this->m = $m - 1;
        $this->n = $n - 1;
        $this->o = $o - 1;
        $this->p = $p - 1;

        $this->min = $min;
        $this->max = $max;

        $k = 0;
        $l = 0;

        for ($k = 0; $k <= $m; $k++) {
            for ($l = 0; $l <= $n; $l++) {
                $this->matriks1[$k][$l] = rand($min, $max);
            }
        }
        for ($k = 0; $k <= $n; $k++) {
            for ($l = 0; $l <= $o; $l++) {
                $this->matriks2[$k][$l] = rand($min, $max);
            }
        }
        for ($k = 0; $k <= $o; $k++) {
            for ($l = 0; $l <= $p; $l++) {
                $this->matriks3[$k][$l] = rand($min, $max);
            }
        }
    }

    function kali_matriks1() { //perkalian (AxB)Xc
        $matriks1 = $this->matriks1;
        $matriks2 = $this->matriks2;
        $matriks3 = $this->matriks3;
        $m = $this->m;
        $n = $this->n;
        $o = $this->o;
        $p = $this->p;

        for ($i = 0; $i <= $m; $i++) {
            for ($j = 0; $j <= $o; $j++) {
                for ($k = 0; $k <= $n; $k++) {
                    $mathas1[$i][$j] = $mathas1[$i][$j] + $matriks1[$i][$k] * $matriks2[$k][$j];
                }
            }
        }
        $this->cetak_matriksordo($mathas1, $m, $o);

        for ($i = 0; $i <= $m; $i++) {
            for ($j = 0; $j <= $p; $j++) {
                for ($k = 0; $k <= $o; $k++) {
                    $mathas2[$i][$j] = $mathas2[$i][$j] + $mathas1[$i][$k] * $matriks3[$k][$j];
                }
            }
        }

        $this->cetak_mathas($mathas2);
    }

    function kali_matriks2() { // perkalian Ax(BXc)
        $matriks1 = $this->matriks1;
        $matriks2 = $this->matriks2;
        $matriks3 = $this->matriks3;
        $m = $this->m;
        $n = $this->n;
        $o = $this->o;
        $p = $this->p;

        $mathas2 = array();

        for ($i = 0; $i <= $n; $i++) {
            for ($j = 0; $j <= $p; $j++) {
                for ($k = 0; $k <= $o; $k++) {
                    $mathas1[$i][$j] = $mathas1[$i][$j] + $matriks2[$i][$k] * $matriks3[$k][$j]; //mathas = matrik hasil
                }
            }
        }

        $this->cetak_matriksordo($mathas1, $n, $p);

        for ($i = 0; $i <= $m; $i++) {
            for ($j = 0; $j <= $p; $j++) {
                for ($k = 0; $k <= $n; $k++) {
                    $mathas2[$i][$j] = $mathas2[$i][$j] + $matriks1[$i][$k] * $mathas1[$k][$j];
                }
            }
        }

        $this->cetak_mathas($mathas2);
    }

    function cetak_mathas($mathas2) {
        echo '<br>';
        for ($i = 0; $i <= $this->m; $i++) {
            echo "<br>";
            for ($j = 0; $j <= $this->p; $j++) {
                echo "| " . $mathas2[$i][$j] . " |";
            }
        }
        echo '<br>';
    }

    function cetak_matriksordo($mathas, $bar, $col) {
        echo '<br>';
        for ($i = 0; $i <= $bar; $i++) {
            echo "<br>";
            for ($j = 0; $j <= $col; $j++) {
                echo "| " . $mathas[$i][$j] . " |";
            }
        }
        echo '<br>';
    }

    function cetak_matriks() {
        $matriks1 = $this->matriks1;
        $matriks2 = $this->matriks2;
        $matriks3 = $this->matriks3;
        $m = $this->m;
        $n = $this->n;
        $o = $this->o;
        $p = $this->p;

        for ($i = 0; $i <= $m; $i++) {
            echo "<br>";
            for ($j = 0; $j <= $n; $j++) {
                echo "| " . $matriks1[$i][$j] . " |";
            }
        }

        echo "<br><br>";

        for ($i = 0; $i <= $n; $i++) {
            echo "<br>";
            for ($j = 0; $j <= $o; $j++) {
                echo "| " . $matriks2[$i][$j] . " |";
            }
        }

        echo "<br><br>";

        for ($i = 0; $i <= $o; $i++) {
            echo "<br>";
            for ($j = 0; $j <= $p; $j++) {
                echo "| " . $matriks3[$i][$j] . " |";
            }
        }
    }

}

nah disitu karena saya sudah terlalu ngantuk jadi banyak function yang sebenarnya ga berguna tapi biarlah yang penting program bener dan lancar.

untuk yang program djikstra bisa dilihat disini https://prima0009.wordpress.com/2013/05/23/djikstra-algoritma-desain-analisis-algoritma/

berikut link downloadnya class sekaligus penggunaan class nya yang belum ngerti OOP pada php belajar dulu bentar oke . selamat mencoba

Download disini dan maaf ini masih banyak nama ane hhahhahha😀

3 thoughts on “Matriks dan perkalian antar matriks , Tugas kuliah DAA

  1. aufa says:

    Romoo …you’re awesome !😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: