Advertisement
enos

MatriksClass.vb

Jul 25th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.29 KB | None | 0 0
  1. Imports Accord.Math
  2. Imports Accord.Math.Decompositions
  3. Public Class MatriksClass
  4.  
  5.     'Anggap Matrik A(m x n) x B (k, l)
  6.     'maka perkalian matrik dapat dilakukan jika m dengan l bernilai sama
  7.     'dam menghasilkan matrik C(k,n)
  8.     Public Function Perkalian(ByVal filter(,) As Double, ByVal image(,) As Double) As Double(,)
  9.         Dim width As Integer = filter.GetLength(1) - 1
  10.         Dim height As Integer = image.GetLength(0) - 1
  11.         Dim kali(filter.GetLength(0) - 1, image.GetLength(1) - 1) As Double
  12.  
  13.         For y As Integer = 0 To filter.GetLength(0) - 1
  14.             For x As Integer = 0 To image.GetLength(1) - 1
  15.                 kali(y, x) = 0
  16.                 For k As Integer = 0 To image.GetLength(0) - 1
  17.                     kali(y, x) = Math.Round(kali(y, x) + filter(y, k) * image(k, x), 2)
  18.                 Next
  19.                 '                kali(y, x) = Math.Round(kali(y, x), 2)
  20.             Next
  21.         Next
  22.         Return kali
  23.  
  24.     End Function
  25.  
  26.     Public Function Perkalian(ByVal koef As Double, ByVal image(,) As Double) As Double(,)
  27.         Dim width As Integer = image.GetLength(1) - 1
  28.         Dim height As Integer = image.GetLength(0) - 1
  29.         Dim kali(image.GetLength(0) - 1, image.GetLength(1) - 1) As Double
  30.  
  31.         For y As Integer = 0 To image.GetLength(0) - 1
  32.             For x As Integer = 0 To image.GetLength(1) - 1
  33.                 kali(y, x) = 3 * image(y, x)
  34.             Next
  35.         Next
  36.         Return kali
  37.  
  38.     End Function
  39.  
  40.     ''Transpose Matriks
  41.     ''Sebuah Proses mengubah baris menjadi kolom atau sebaliknya
  42.     ''Contoh        | 3 4 |              |  3 4 1  |
  43.     ''          A = | 4 5 | menjadi  A = |  4 5 7  |
  44.     ''              | 1 7 |
  45.     Public Function transpose(ByVal matriks(,) As Double) As Double(,)
  46.         Dim width As Integer = matriks.GetLength(1) - 1
  47.         Dim height As Integer = matriks.GetLength(0) - 1
  48.         Dim trpose(width, height) As Double
  49.  
  50.         For Posisi_x As Integer = 0 To height
  51.             For Posisi_y As Integer = 0 To width
  52.                 trpose(Posisi_y, Posisi_x) = matriks(Posisi_x, Posisi_y)
  53.             Next
  54.         Next
  55.         Return trpose
  56.     End Function
  57.  
  58.     ''penambahan matriks
  59.     ''sebuah matriks dapat ditambahkan jika memiliki ordo yang sama mxn + mxn
  60.     ''Contoh        | 3 4 |       | 2 1 |              | 5 5 |
  61.     ''          A = | 4 5 | + B = | 5 2 | menjadi  C = | 9 7 |
  62.     ''              | 1 7 |       | 6 3 |              | 7 10|
  63.     Public Function Penambahan(ByVal matriksA(,) As Double, ByVal matriksB(,) As Double) As Double(,)
  64.         Dim width As Integer = matriksA.GetLength(1) - 1
  65.         Dim height As Integer = matriksA.GetLength(0) - 1
  66.         Dim tmp(height, width) As Double
  67.  
  68.         For Posisi_x As Integer = 0 To height
  69.             For Posisi_y As Integer = 0 To width
  70.                 tmp(Posisi_x, Posisi_y) = Math.Round(matriksA(Posisi_x, Posisi_y) + matriksB(Posisi_x, Posisi_y), 1)
  71.             Next
  72.         Next
  73.         Return tmp
  74.     End Function
  75.  
  76.     ''pengurangan matriks
  77.     ''sebuah matriks dapat dilakukan jika memiliki ordo yang sama mxn + mxn
  78.     ''Contoh        | 3 4 |       | 2 1 |              | 1  3 |
  79.     ''          A = | 4 5 | - B = | 5 2 | menjadi  C = | -1 3 |
  80.     ''              | 1 7 |       | 6 3 |              | -5 4 |
  81.  
  82.     Public Function pengurangan(ByVal matriksA(,) As Double, ByVal matriksB(,) As Double) As Double(,)
  83.         Dim width As Integer = matriksA.GetLength(1) - 1
  84.         Dim height As Integer = matriksA.GetLength(0) - 1
  85.         Dim tmp(height, width) As Double
  86.         '        Dim cmp As Double
  87.  
  88.         For Posisi_x As Integer = 0 To height
  89.             For Posisi_y As Integer = 0 To width
  90.                 tmp(Posisi_x, Posisi_y) = matriksA(Posisi_x, Posisi_y) - matriksB(Posisi_x, Posisi_y)
  91.             Next
  92.         Next
  93.         Return tmp
  94.     End Function
  95.  
  96.     Private Shared Sub Find_R_C(ByVal Mat(,) As Double, ByRef Row As Integer, ByRef Col As Integer)
  97.         Row = Mat.GetLength(0) - 1
  98.         Col = Mat.GetLength(1) - 1
  99.     End Sub
  100.  
  101.     Public Function Determinan(ByVal Mat(,) As Double) As Double
  102.         Dim DArray(,) As Double, S As Integer
  103.         Dim k, k1, i, j As Integer
  104.         Dim save, ArrayK As Double
  105.         Dim M1 As String = ""
  106.         Dim Rows, Cols As Integer
  107.  
  108.         Find_R_C(Mat, Rows, Cols)
  109.         If Rows = Cols Then
  110.  
  111.             S = Rows
  112.             Determinan = 1
  113.             DArray = Mat.Clone()
  114.  
  115.             For k = 0 To S
  116.                 If DArray(k, k) = 0 Then
  117.                     j = k
  118.                     Do While ((j < S) And (DArray(k, j) = 0))
  119.                         j = j + 1
  120.                     Loop
  121.                     If DArray(k, j) = 0 Then
  122.                         Determinan = 0
  123.                         Exit Function
  124.                     Else
  125.                         For i = k To S
  126.                             save = DArray(i, j)
  127.                             DArray(i, j) = DArray(i, k)
  128.                             DArray(i, k) = save
  129.                         Next i
  130.                     End If
  131.  
  132.                     Determinan = -Determinan
  133.                 End If
  134.                 ArrayK = DArray(k, k)
  135.                 Determinan = Determinan * ArrayK
  136.                 If k < S Then
  137.                     k1 = k + 1
  138.                     For i = k1 To S
  139.                         For j = k1 To S
  140.                             DArray(i, j) = DArray(i, j) - DArray(i, k) * (DArray(k, j) / ArrayK)
  141.                         Next j
  142.                     Next i
  143.                 End If
  144.             Next
  145.         Else
  146.             MessageBox.Show("Dimensi Matriks Harus m x m ")
  147.         End If
  148.     End Function
  149.  
  150.  
  151.     Private Function getField(ByVal matriksA(,) As Double, ByVal row As Integer, ByVal cols As Integer) As Double
  152.         Dim tmp(matriksA.GetLength(1) - 2, matriksA.GetLength(0) - 2) As Double
  153.  
  154.         Dim listrow As New List(Of Integer)
  155.         Dim listcols As New List(Of Integer)
  156.  
  157.         For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
  158.             If Posisi_x <> row Then
  159.                 listrow.Add(Posisi_x)
  160.             End If
  161.         Next
  162.         For Posisi_y As Integer = 0 To matriksA.GetLength(1) - 1
  163.             If Posisi_y <> cols Then
  164.                 listcols.Add(Posisi_y)
  165.             End If
  166.         Next
  167.         Dim i As Integer = 0
  168.         For Each itembaris As Integer In listrow
  169.             Dim j As Integer = 0
  170.             For Each itemkolom As Integer In listcols
  171.                 tmp(i, j) = matriksA(itembaris, itemkolom)
  172.                 j += 1
  173.             Next
  174.             i += 1
  175.         Next
  176.         Return Determinan(tmp)
  177.     End Function
  178.     Public Function Adjoin(ByVal matriksA(,) As Double) As Double(,)
  179.         Dim tmp1(matriksA.GetLength(1) - 1, matriksA.GetLength(0) - 1) As Double
  180.         For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
  181.             For Posisi_y As Integer = 0 To matriksA.GetLength(1) - 1
  182.                 If ((Posisi_x + 1) + (Posisi_y + 1)) Mod 2 = 0 Then
  183.                     tmp1(Posisi_x, Posisi_y) = getField(matriksA, Posisi_x, Posisi_y)
  184.                 Else
  185.                     tmp1(Posisi_x, Posisi_y) = -getField(matriksA, Posisi_x, Posisi_y)
  186.                 End If
  187.             Next
  188.         Next
  189.         Return tmp1
  190.     End Function
  191.  
  192.     Public Function Invers(ByVal matriksA(,) As Double) As Double(,)
  193.         Dim tmp As Double = 1 / Determinan(matriksA)
  194.         Dim tmp1(matriksA.GetLength(1), matriksA.GetLength(0)) As Double
  195.         matriksA = transpose(Adjoin(matriksA))
  196.         For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
  197.             For Posisi_y As Integer = 0 To matriksA.GetLength(1) - 1
  198.                 tmp1(Posisi_x, Posisi_y) = matriksA(Posisi_x, Posisi_y) * tmp
  199.             Next
  200.         Next
  201.         Return tmp1
  202.     End Function
  203.  
  204.     Public Function eigenvalue(ByVal matriksA(,) As Double) As Double()
  205.         Dim a As New Accord.Math.Decompositions.EigenvalueDecomposition(matriksA)
  206.         Return a.RealEigenvalues
  207.     End Function
  208.     Public Function eigenVektor(ByVal matriksA(,) As Double) As Double(,)
  209.         Dim a As New Accord.Math.Decompositions.EigenvalueDecomposition(matriksA)
  210.         Return a.Eigenvectors
  211.     End Function
  212.  
  213.     Public Function Diagonalmatriks(ByVal matriksA(,) As Double) As Double(,)
  214.         Dim a As New Accord.Math.Decompositions.EigenvalueDecomposition(matriksA)
  215.         Return a.DiagonalMatrix
  216.     End Function
  217.  
  218.     Public Function Akarmatriks(ByVal matriksA(,) As Double) As Double(,)
  219.         Dim tmp1(matriksA.GetLength(1), matriksA.GetLength(0)) As Double
  220.         For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
  221.             For Posisi_y As Integer = 0 To matriksA.GetLength(1) - 1
  222.                 tmp1(Posisi_x, Posisi_y) = 1 / Math.Sqrt(matriksA(Posisi_x, Posisi_y))
  223.             Next
  224.         Next
  225.         Return tmp1
  226.     End Function
  227.  
  228.     Public Function Pangkatmatriks(ByVal matriksA(,) As Double) As Double(,)
  229.         Dim tmp1(matriksA.GetLength(0) - 1, matriksA.GetLength(1) - 1) As Double
  230.         For Posisi_x As Integer = 0 To matriksA.GetLength(0) - 1
  231.             For Posisi_y As Integer = 0 To (matriksA.GetLength(1) - 1)
  232.                 tmp1(Posisi_x, Posisi_y) = Math.Pow(matriksA(Posisi_x, Posisi_y), 2)
  233.             Next
  234.         Next
  235.         Return tmp1
  236.     End Function
  237.  
  238. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement