Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports Accord.Math
- Imports Accord.Math.Decompositions
- Public Class MatriksClass
- 'Anggap Matrik A(m x n) x B (k, l)
- 'maka perkalian matrik dapat dilakukan jika m dengan l bernilai sama
- 'dam menghasilkan matrik C(k,n)
- Public Function Perkalian(ByVal filter(,) As Double, ByVal image(,) As Double) As Double(,)
- Dim width As Integer = filter.GetLength(1) - 1
- Dim height As Integer = image.GetLength(0) - 1
- Dim kali(filter.GetLength(0) - 1, image.GetLength(1) - 1) As Double
- For y As Integer = 0 To filter.GetLength(0) - 1
- For x As Integer = 0 To image.GetLength(1) - 1
- kali(y, x) = 0
- For k As Integer = 0 To image.GetLength(0) - 1
- kali(y, x) = Math.Round(kali(y, x) + filter(y, k) * image(k, x), 2)
- Next
- ' kali(y, x) = Math.Round(kali(y, x), 2)
- Next
- Next
- Return kali
- End Function
- Public Function Perkalian(ByVal koef As Double, ByVal image(,) As Double) As Double(,)
- Dim width As Integer = image.GetLength(1) - 1
- Dim height As Integer = image.GetLength(0) - 1
- Dim kali(image.GetLength(0) - 1, image.GetLength(1) - 1) As Double
- For y As Integer = 0 To image.GetLength(0) - 1
- For x As Integer = 0 To image.GetLength(1) - 1
- kali(y, x) = 3 * image(y, x)
- Next
- Next
- Return kali
- End Function
- ''Transpose Matriks
- ''Sebuah Proses mengubah baris menjadi kolom atau sebaliknya
- ''Contoh | 3 4 | | 3 4 1 |
- '' A = | 4 5 | menjadi A = | 4 5 7 |
- '' | 1 7 |
- Public Function transpose(ByVal matriks(,) As Double) As Double(,)
- Dim width As Integer = matriks.GetLength(1) - 1
- Dim height As Integer = matriks.GetLength(0) - 1
- Dim trpose(width, height) As Double
- For Posisi_x As Integer = 0 To height
- For Posisi_y As Integer = 0 To width
- trpose(Posisi_y, Posisi_x) = matriks(Posisi_x, Posisi_y)
- Next
- Next
- Return trpose
- End Function
- ''penambahan matriks
- ''sebuah matriks dapat ditambahkan jika memiliki ordo yang sama mxn + mxn
- ''Contoh | 3 4 | | 2 1 | | 5 5 |
- '' A = | 4 5 | + B = | 5 2 | menjadi C = | 9 7 |
- '' | 1 7 | | 6 3 | | 7 10|
- Public Function Penambahan(ByVal matriksA(,) As Double, ByVal matriksB(,) As Double) As Double(,)
- Dim width As Integer = matriksA.GetLength(1) - 1
- Dim height As Integer = matriksA.GetLength(0) - 1
- Dim tmp(height, width) As Double
- For Posisi_x As Integer = 0 To height
- For Posisi_y As Integer = 0 To width
- tmp(Posisi_x, Posisi_y) = Math.Round(matriksA(Posisi_x, Posisi_y) + matriksB(Posisi_x, Posisi_y), 1)
- Next
- Next
- Return tmp
- End Function
- ''pengurangan matriks
- ''sebuah matriks dapat dilakukan jika memiliki ordo yang sama mxn + mxn
- ''Contoh | 3 4 | | 2 1 | | 1 3 |
- '' A = | 4 5 | - B = | 5 2 | menjadi C = | -1 3 |
- '' | 1 7 | | 6 3 | | -5 4 |
- Public Function pengurangan(ByVal matriksA(,) As Double, ByVal matriksB(,) As Double) As Double(,)
- Dim width As Integer = matriksA.GetLength(1) - 1
- Dim height As Integer = matriksA.GetLength(0) - 1
- Dim tmp(height, width) As Double
- ' Dim cmp As Double
- For Posisi_x As Integer = 0 To height
- For Posisi_y As Integer = 0 To width
- tmp(Posisi_x, Posisi_y) = matriksA(Posisi_x, Posisi_y) - matriksB(Posisi_x, Posisi_y)
- Next
- Next
- Return tmp
- End Function
- Private Shared Sub Find_R_C(ByVal Mat(,) As Double, ByRef Row As Integer, ByRef Col As Integer)
- Row = Mat.GetLength(0) - 1
- Col = Mat.GetLength(1) - 1
- End Sub
- Public Function Determinan(ByVal Mat(,) As Double) As Double
- Dim DArray(,) As Double, S As Integer
- Dim k, k1, i, j As Integer
- Dim save, ArrayK As Double
- Dim M1 As String = ""
- Dim Rows, Cols As Integer
- Find_R_C(Mat, Rows, Cols)
- If Rows = Cols Then
- S = Rows
- Determinan = 1
- DArray = Mat.Clone()
- For k = 0 To S
- If DArray(k, k) = 0 Then
- j = k
- Do While ((j < S) And (DArray(k, j) = 0))
- j = j + 1
- Loop
- If DArray(k, j) = 0 Then
- Determinan = 0
- Exit Function
- Else
- For i = k To S
- save = DArray(i, j)
- DArray(i, j) = DArray(i, k)
- DArray(i, k) = save
- Next i
- End If
- Determinan = -Determinan
- End If
- ArrayK = DArray(k, k)
- Determinan = Determinan * ArrayK
- If k < S Then
- k1 = k + 1
- For i = k1 To S
- For j = k1 To S
- DArray(i, j) = DArray(i, j) - DArray(i, k) * (DArray(k, j) / ArrayK)
- Next j
- Next i
- End If
- Next
- Else
- MessageBox.Show("Dimensi Matriks Harus m x m ")
- End If
- End Function
- Private Function getField(ByVal matriksA(,) As Double, ByVal row As Integer, ByVal cols As Integer) As Double
- Dim tmp(matriksA.GetLength(1) - 2, matriksA.GetLength(0) - 2) As Double
- Dim listrow As New List(Of Integer)
- Dim listcols As New List(Of Integer)
- For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
- If Posisi_x <> row Then
- listrow.Add(Posisi_x)
- End If
- Next
- For Posisi_y As Integer = 0 To matriksA.GetLength(1) - 1
- If Posisi_y <> cols Then
- listcols.Add(Posisi_y)
- End If
- Next
- Dim i As Integer = 0
- For Each itembaris As Integer In listrow
- Dim j As Integer = 0
- For Each itemkolom As Integer In listcols
- tmp(i, j) = matriksA(itembaris, itemkolom)
- j += 1
- Next
- i += 1
- Next
- Return Determinan(tmp)
- End Function
- Public Function Adjoin(ByVal matriksA(,) As Double) As Double(,)
- Dim tmp1(matriksA.GetLength(1) - 1, matriksA.GetLength(0) - 1) As Double
- For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
- For Posisi_y As Integer = 0 To matriksA.GetLength(1) - 1
- If ((Posisi_x + 1) + (Posisi_y + 1)) Mod 2 = 0 Then
- tmp1(Posisi_x, Posisi_y) = getField(matriksA, Posisi_x, Posisi_y)
- Else
- tmp1(Posisi_x, Posisi_y) = -getField(matriksA, Posisi_x, Posisi_y)
- End If
- Next
- Next
- Return tmp1
- End Function
- Public Function Invers(ByVal matriksA(,) As Double) As Double(,)
- Dim tmp As Double = 1 / Determinan(matriksA)
- Dim tmp1(matriksA.GetLength(1), matriksA.GetLength(0)) As Double
- matriksA = transpose(Adjoin(matriksA))
- For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
- For Posisi_y As Integer = 0 To matriksA.GetLength(1) - 1
- tmp1(Posisi_x, Posisi_y) = matriksA(Posisi_x, Posisi_y) * tmp
- Next
- Next
- Return tmp1
- End Function
- Public Function eigenvalue(ByVal matriksA(,) As Double) As Double()
- Dim a As New Accord.Math.Decompositions.EigenvalueDecomposition(matriksA)
- Return a.RealEigenvalues
- End Function
- Public Function eigenVektor(ByVal matriksA(,) As Double) As Double(,)
- Dim a As New Accord.Math.Decompositions.EigenvalueDecomposition(matriksA)
- Return a.Eigenvectors
- End Function
- Public Function Diagonalmatriks(ByVal matriksA(,) As Double) As Double(,)
- Dim a As New Accord.Math.Decompositions.EigenvalueDecomposition(matriksA)
- Return a.DiagonalMatrix
- End Function
- Public Function Akarmatriks(ByVal matriksA(,) As Double) As Double(,)
- Dim tmp1(matriksA.GetLength(1), matriksA.GetLength(0)) As Double
- For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
- For Posisi_y As Integer = 0 To matriksA.GetLength(1) - 1
- tmp1(Posisi_x, Posisi_y) = 1 / Math.Sqrt(matriksA(Posisi_x, Posisi_y))
- Next
- Next
- Return tmp1
- End Function
- Public Function Pangkatmatriks(ByVal matriksA(,) As Double) As Double(,)
- Dim tmp1(matriksA.GetLength(0) - 1, matriksA.GetLength(1) - 1) As Double
- For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
- For Posisi_y As Integer = 0 To (matriksA.GetLength(1) - 1)
- tmp1(Posisi_x, Posisi_y) = Math.Pow(matriksA(Posisi_x, Posisi_y), 2)
- Next
- Next
- Return tmp1
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement