Skip to content

Instantly share code, notes, and snippets.

@ekickx
Last active March 24, 2020 16:57
Show Gist options
  • Save ekickx/93a9bc1c1bafcef9ef3f26de51faa87e to your computer and use it in GitHub Desktop.
Save ekickx/93a9bc1c1bafcef9ef3f26de51faa87e to your computer and use it in GitHub Desktop.
Operasi matriks, seperti penjumlahan, pengurangan, perkalian, dan determinan di bahasa Go
package main
import (
"fmt"
)
func main() {
var ordo, choice int
// Input pilihan
fmt.Println("Apa yang ingin anda lakukan: ")
fmt.Println("1) Penjumlahan\n2) Pengurangan\n3) Perkalian\n4) Determinan")
fmt.Printf("> ")
fmt.Scanf("%d", &choice)
// Jika inputan valid
if(choice == 1 || choice == 2 || choice == 3 || choice == 4) {
fmt.Printf("\nMasukan ordo matriks: ")
fmt.Scanf("%d", &ordo)
fmt.Println("Masukan matriks pertama")
A := addMatrix(ordo)
// Operasi yang membutuhkan dua matriks
if(choice != 4) {
fmt.Println("\nMasukan matriks kedua")
B := addMatrix(ordo)
fmt.Println("\nHasil: ")
switch {
case choice == 1:
showMatrix(sumMatrix(A,B))
case choice == 2:
showMatrix(substractMatrix(A,B))
case choice == 3:
showMatrix(multipleMatrix(A,B))
}
// Determinan hanya membutuhkan satu matrix
} else {
fmt.Println("\nHasil =", determineMatrix(A))
}
// Jika inputan tidak valid
} else {
fmt.Println("Pilihan tersebut tidak ada")
}
}
// Fungsi untuk "mempercantik" output matriks
func showMatrix(matrix [][]int) int {
for col := 0; col < len(matrix); col++ {
for row := 0; row < len(matrix); row++ {
fmt.Printf("%d\t", matrix[col][row])
}
fmt.Println("")
}
return 0
}
// Fungsi untuk menambahkan/menginputkan matriks
func addMatrix(size int) [][]int {
matrix := make([][]int, size) // Inisialisasi matriks
for row := 0; row < size; row++ {
matrix[row] = make([]int, size)
// Memasukan nilai matriks
for col := 0; col < size; col++ {
fmt.Scanf("%d", &matrix[row][col])
}
}
return matrix
}
// Fungsi penjumlahan antar dua matriks
func sumMatrix(matrixA, matrixB [][]int) [][]int {
result := make([][]int, len(matrixA)) // Inisialisasi matriks
for row := 0; row < len(matrixA); row++ {
result[row] = make([]int, len(matrixA))
// Menambahkan matriks
for col := 0; col < len(matrixA); col++{
result[row][col] = matrixA[row][col] + matrixB[row][col]
}
}
return result
}
// Fungsi pengurangan antar dua matriks
func substractMatrix(matrixA, matrixB [][]int) [][]int {
result := make([][]int, len(matrixA)) // Inisialisasi matriks
for row := 0; row < len(matrixA); row++ {
result[row] = make([]int, len(matrixA))
// Mengurangkan matriks
for col := 0; col < len(matrixA); col++ {
result[row][col] = matrixA[row][col] - matrixB[row][col]
}
}
return result
}
// Fungsi perkalian antar matriks
func multipleMatrix(matrixA, matrixB [][]int) [][]int {
// Inisialisasi variable
result := make([][]int, len(matrixA))
var tempResult int
for row := 0; row < len(matrixA); row++ {
result[row] = make([]int, len(matrixA))
for col := 0; col < len(matrixA); col++ {
for i := 0; i < len(matrixA); i++ {
tempResult = matrixA[row][i] * matrixB[i][col] // Mengalikan setiap baris matriks A dgn kolom matriks B
result[row][col] += tempResult // Menambahkan hasil perkalian ke matriks hasil
}
}
}
return result
}
// Fungsi untuk menentukan determinan
func determineMatrix(matrix [][]int) int {
// Inisialisasi variable
var result int
cofactor := make([]int, len(matrix))
minor := make([]int, len(matrix))
// Mencari minor jika ordo matriks 2x2
if(len(matrix) == 2){
for col := 0; col < len(matrix); col++ {
minor[col] = matrix[len(matrix)-1][len(matrix)-1-col]
}
// Mencari minor jika ordo matriks > 2x2
} else {
tempMinor := make([][]int, len(matrix)-1) // Inisialisasi submatriks
for col := 0; col < len(matrix); col++ {
// Mencari submatriks
for rowMin := 0; rowMin < len(matrix)-1; rowMin++ {
tempMinor[rowMin] = make([]int, len(matrix)-1)
var i int
for colMin := 0; colMin < len(matrix)-1; colMin++ {
if(colMin == col){i += 1}
tempMinor[rowMin][colMin] = matrix[rowMin+1][i]
i++
}
}
// Memasukan submatriks ke fungsi untuk mencari minor
minor[col] = determineMatrix(tempMinor)
}
}
// Menghitung kofaktor
for col := 0; col < len(matrix); col++ {
if(col % 2 == 0){
cofactor[col] = matrix[0][col] * minor[col] * 1 // Jika kolom genap, kofaktor * 1
} else {
cofactor[col] = matrix[0][col] * minor[col] * -1 // Jika kolom ganjil, kofaktor * -1
}
result += cofactor[col]
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment