Sign in to follow this  
Followers 0
IchBistTod

Multiplying Matricies

10 posts in this topic

#1 ·  Posted (edited)

I am trying to write code to automate multiplication of matricies for a school cryptology project.

However turning the visulization on my head in to code is not working at the moment for some reason(probably lack of sleep)

for those of you who dont understand matrix multiplication it works in Rows By Colums

example:

a = |1 2|

......|3 4|

b = |1 2|

.......|3 4|

we will be multiplying matrix a*b

they are both 2x2, the INNER dimensions much match, and the resulting matrix is the outer dimensions

a b

2x2 2x2

|...|V|...|

|.same.|

|-------- |

|....V.....|

...2x2

So since both have inter dimensions of 2 (a has same number of rows as b does columns) the dimensions of the new matrix are 2x2( a's columns by b's rows)

We then multiply the rows in A by the columns in B

|(1*1)+(2*3) (1*2)+(2*4)|

|(3*1)+(4*3) (3*2)+(4*4)|

so then we work is out and a*b =| 7 10|

......................................................|15 22|

Matricies are used for encryption by converting text into numbers and multiplying them by a key matrix(made by turning a password into number)

the code can then be decrypted using the inverse of the key(meaning the password is required) OR may neer be decoded is the inverse = 0 (aka a hash)

You can see how doing this by hand can be tedious.

I can never seen to get it multiply ALL of the numbers right, I have modified the code several different ways, if anyone can get this code to work I would HIGHLY appreciate it, I may even pay money.

#include <array.au3>
Global $matrix1[2][2]
Global $matrix2[2][2]
$matrix1[0][0] = 1
$matrix1[0][1] = 2
$matrix1[1][0] = 3
$matrix1[1][1] = 4


$matrix2[0][0] = 1
$matrix2[0][1] = 2
$matrix2[1][0] = 3
$matrix2[1][1] = 4

_MatrixMultiply($matrix1, $matrix2, "2x2", "2x2")
Func _MatrixMultiply($matrix1, $matrix2, $size1, $size2)
    $demensions1 = StringSplit($size1, "x")
    $demensions2 = StringSplit($size2, "x")

    if $demensions1[2] = $demensions2[1] Then
        ConsoleWrite("Skeleton Is "&$demensions1[1]&"x"&$demensions2[2]&@CrLF)
        Global $Skeleton[$demensions2[2]+1][$demensions1[1]+1]
        for $ix =0 To $demensions2[2]-1

        for $i=0 to $demensions1[1]-1

            $Skeleton[$ix][$i] = ($matrix1[$ix][$i] * $matrix2[1][$ix]) + ($matrix1[$ix][$i] * $matrix2[$i][$i])
        next

        Next
    EndIf
_ArrayDisplay($Skeleton)

EndFunc
Edited by IchBistTod

[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

does this work?

#include <array.au3>

Global $aMatrix[2][2]
Global $bMatrix[2][2]
Global $cMatrix[2][2]

$aMatrix[0][0] = 1
$aMatrix[0][1] = 2
$aMatrix[1][0] = 3
$aMatrix[1][1] = 4


$bMatrix[0][0] = 1
$bMatrix[0][1] = 2
$bMatrix[1][0] = 3
$bMatrix[1][1] = 4

mult_matrix($aMatrix, $bMatrix, $cMatrix)
_ArrayDisplay($cMatrix)

func mult_matrix(ByRef $aMatrix, ByRef $bMatrix, ByRef $cMatrix)
    local $i, $j, $k
    for $i = 0 to UBound($aMatrix) - 1
        for $j = 0 to UBound($bMatrix) - 1
            $cMatrix[$i][$j] = 0
            for $k = 0 to UBound($cMatrix) - 1
                $cMatrix[$i][$j] += $aMatrix[$i][$k] * $bMatrix[$k][$j]
            Next
        Next
    Next
endfunc

also, here's a transpose function that I have lying around:

Func transpose_matrix(ByRef $aMatrix)
    local $i, $j, $temp
    for $i = 0 to UBound($aMatrix) - 1
        for $j = $i + 1 to UBound($aMatrix) - 1
            _ArraySwap($aMatrix[$i][$j], $aMatrix[$j][$i])
        Next
    Next
EndFunc
Edited by ame1011

[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I cant get it to work with the following set of data, which is the form data is usually encrypted in

Maybe its me, or maybe your function can only handle square matrices, or only two matrices with the same dimensions.

however most matrices used for encryption are much higher than they are wide.

#include <array.au3>

Global $aMatrix[5][2]
Global $bMatrix[2][2]
Global $cMatrix[5][2]

$aMatrix[0][0] = 7
$aMatrix[0][1] = 15
$aMatrix[1][0] = 0
$aMatrix[1][1] = 20
$aMatrix[2][0] = 15
$aMatrix[2][1] = 14
$aMatrix[3][0] = 9
$aMatrix[3][1] = 7
$aMatrix[4][0] = 8
$aMatrix[4][1] = 20



$bMatrix[0][0] = 2
$bMatrix[0][1] = 1
$bMatrix[1][0] = 4
$bMatrix[1][1] = 3

mult_matrix($aMatrix, $bMatrix, $cMatrix)
_ArrayDisplay($cMatrix)

func mult_matrix(ByRef $aMatrix, ByRef $bMatrix, ByRef $cMatrix)
    local $i, $j, $k
    for $i = 0 to UBound($aMatrix) - 1
        for $j = 0 to UBound($bMatrix) - 1
            $cMatrix[$i][$j] = 0
            for $k = 0 to UBound($cMatrix) - 1
                $cMatrix[$i][$j] += $aMatrix[$i][$k] * $bMatrix[$k][$j]
            Next
        Next
    Next
endfunc
Edited by IchBistTod

[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I now see the script requires the matrices to be the same size.

This is not practical for use of encryption and hashing.

Edited by IchBistTod

[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

#include <array.au3>

Global $aMatrix[5][2]
Global $bMatrix[2][2]
Global $cMatrix[5][2]

$aMatrix[0][0] = 7
$aMatrix[0][1] = 15
$aMatrix[1][0] = 0
$aMatrix[1][1] = 20
$aMatrix[2][0] = 15
$aMatrix[2][1] = 14
$aMatrix[3][0] = 9
$aMatrix[3][1] = 7
$aMatrix[4][0] = 8
$aMatrix[4][1] = 20

$bMatrix[0][0] = 2
$bMatrix[0][1] = 1
$bMatrix[1][0] = 4
$bMatrix[1][1] = 3

mult_matrix($aMatrix, $bMatrix, $cMatrix)
_ArrayDisplay($cMatrix)

func mult_matrix(ByRef $aMatrix, ByRef $bMatrix, ByRef $cMatrix)
    local $i, $j, $k
    for $i = 0 to UBound($aMatrix) - 1 ;0 to Number of Rows in Matrix A
        for $j = 0 to UBound($bMatrix,2) - 1 ;0 to Number of Columns in Matrix B
            $cMatrix[$i][$j] = 0 ;reset the MatrixC[rowA][colB] value
            for $k = 0 to UBound($aMatrix,2) - 1 ;0 to Number of Columns in Matrix A
                $cMatrix[$i][$j] += $aMatrix[$i][$k] * $bMatrix[$k][$j] ;MatrixC[rowA][ColB] += MatrixA[rowA][colA] X MatrixB[colA][colB]
                ConsoleWrite("cMatrix["&$i&"]["&$j&"] += aMatrix["&$i&"]["&$k&"] MULT BY bMatrix["&$k&"]["&$j&"]  ===> " & $cMatrix[$i][$j] & @CRLF)
            Next
        Next
    Next
endfunc

Edited by ame1011

[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]

Share this post


Link to post
Share on other sites

does this display the desired result:

#include <array.au3>

Global $aMatrix[5][2]
Global $bMatrix[2][2]
Global $cMatrix[5][2]

$aMatrix[0][0] = 7
$aMatrix[0][1] = 15
$aMatrix[1][0] = 0
$aMatrix[1][1] = 20
$aMatrix[2][0] = 15
$aMatrix[2][1] = 14
$aMatrix[3][0] = 9
$aMatrix[3][1] = 7
$aMatrix[4][0] = 8
$aMatrix[4][1] = 20

$bMatrix[0][0] = 2
$bMatrix[0][1] = 1
$bMatrix[1][0] = 4
$bMatrix[1][1] = 3

mult_matrix($aMatrix, $bMatrix, $cMatrix)
_ArrayDisplay($cMatrix)

func mult_matrix(ByRef $aMatrix, ByRef $bMatrix, ByRef $cMatrix)
    local $i, $j, $k
    for $i = 0 to UBound($aMatrix) - 1
        for $j = 0 to UBound($bMatrix,2) - 1
            $cMatrix[$i][$j] = 0
            for $k = 0 to UBound($aMatrix,2) - 1
                $cMatrix[$i][$j] += $aMatrix[$i][$k] * $bMatrix[$k][$j]
            Next
        Next
    Next
endfunc

yes it does, thank you VERY much.

Could you please comment/explain the code so I can learn from it?


[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I added some comments from above, i'm not sure how to comment it better than that.

edit: I also added a consolewrite above, you should be able to see what's going on by running that script

Edited by ame1011

[font="Impact"] I always thought dogs laid eggs, and I learned something today. [/font]

Share this post


Link to post
Share on other sites

I added some comments from above, i'm not sure how to comment it better than that.

edit: I also added a consolewrite above, you should be able to see what's going on by running that script

thanks


[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites

From here, the MatrixUDF.au3 part of the script is reproduced this post.

I originally wrote this with cryptography in mind.

Note: An example is commented out above each function.

MatrixUDF.au3

;
; MatrixUDF.au3
; For these functions, the only difference between an array and a matrix is, a matrix only contains numeric values.
; If a string is used in an element of a matrix or an array, using these functions, its value is evaluated as zero.

#include <Array.au3>

Global $iDF = 0 ; Variable use to calculate determinant and used in Upper Triangle calculations - sign of submatrices

; _MatrixAdjoint
; _MatrixDet
; _MatrixInv
; _MatrixMinusMatrix
; _MatrixMod
; _MatrixPlusMatrix
; _MatrixPlusScalar
; _MatrixProduct
; _MatrixTranspose
; _MatrixScalarProduct
; _MatrixUpperTriangle

; _GCD
; _ModInv
; _Prim

;Local $a,$b[5][5] = [[2, 0, 0, 0, 0],[0, 1, 0, 0, 0],[0, 0, 1, 0, 0],[0, 0, 0, 1 , 0],[0.2, 0.2, 0.2, 0, 3]]
;Local $a,$b[3][3] = [[2,3,8],[2,2,2],[0,2,6]]
;$a = _MatrixAdjoint($b)
;_ArrayDisplay($a)
;===================================================
; Calculates and returns the matrix which is the adjoint of matrix $a
Func _MatrixAdjoint($a)
    Local $tms = UBound($a), $ii = 0, $temp = 0, $jj = 0, $ia = 0, $ja = 0, $det = 0, $m = $a, $tmsp
    ;MsgBox(0,"",$tms)
    For $i = 0 To $tms - 1
        For $j = 0 To $tms - 1
            $ia = 0
            $ja = 0
            Local $ap[$tms - 1][$tms - 1]
            $tmsp = UBound($ap)
            For $ii = 0 To $tms - 1
                For $jj = 0 To $tms - 1
                    If $ii <> $i And $jj <> $j Then
                        $ap[$ia][$ja] = $a[$ii][$jj]
                        $ja = $ja + 1
                        ;MsgBox(0,"Prim", $ja & " " & $a[$ia][$ja])
                    EndIf
                Next
                If $ii <> $i And $jj <> $j Then $ia = $ia + 1
                $ja = 0
            Next
            $det = _MatrixDet($ap)
            $m[$i][$j] = ((-1) ^ ($i + $j)) * $det
            ;ConsoleWrite("$i\$j = " & $i & "\" & $j & " ((-1) ^ ($i + $j)) = " & ((-1) ^ ($i + $j)) & @TAB & _
 ; " Det = " & $det & @TAB & " $m[$i][$j] = " & $m[$i][$j] & @CRLF)
        Next
    Next
    $m = _MatrixTranspose($m)
    Return $m
EndFunc ;==>_MatrixAdjoint

;Local $a,$b[3][3] = [[2,-3,1],[2,"A string value",-7],[1,0,-2]]
;$a = _MatrixDet($b)
;MsgBox(0,"","Determinant = " & $a)
;===================================================
; Calculates and returns the determinant of matrix $a
Func _MatrixDet($a)
    local $temp, $temp1
    If UBound($a) <> UBound($a, 2) Then
        MsgBox(0, "Error", " Matrix must be square to determine Determinant. ROW = " & UBound($a) & " COLUMN = " & UBound($a, 2))
    Else
        Dim $bt, $det = 1
        Local $iRows = UBound($a)
        $temp = _MatrixUpperTriangle($a)
        $temp1 = _MatrixUpperTriangle($temp)
        $bt = _MatrixUpperTriangle($temp1)
        For $r = 0 To $iRows - 1
            $det = $det * $bt[$r][$r]
            ;MsgBox(0,"det", $iRows & " " &$r & " " & $det)
        Next
        $det = $det * $iDF
    EndIf
    ;If $decplaceval <> 0 Then $det = Round($det, $decplaceval)
    Return $det
EndFunc ;==>_MatrixDet

;===== Normal inverse of a matrix ===================
;Local $a, $b[5][5] = [[1, 0, 0, 0, 0],[0, 1, 0, 0, 0],[0, 0, 1, 0, 0],[0, 0, 0, 1 , 0],[0.2, 0.2, 0.2, 0, 1]]
;Local $a,$b[3][3] = [[3,2,-1],[1,6,3],[2,-4,0]], $c
;$a = _MatrixInv($b)
;_ArrayDisplay($a)
;$c = _MatrixProduct($b, $a)
;_ArrayDisplay($c)
;====== Modulo inverse of a matrix ==================
;Local $a, $b[3][3] = [[12, 23, 9],[2, 0, 7],[11, 0, -2]], $c
;$a = _MatrixInv($b, 5)
;_ArrayDisplay($a)
;$c = _MatrixProduct($b, $a, 5)
;_ArrayDisplay($c)
;===================================================
; matrixInv($a) gives the inverse of Matrix $a
; matrixInv($a, $m) gives the Modulo inverse of Matrix $a Modulus $m
Func _MatrixInv($a, $m = 0)
    local $det, $ajnt, $avRET, $mul
    If UBound($a, 2) <> UBound($a) Then
        SetError(0)
        Return -1
    EndIf
    $det = _MatrixDet($a)
    If $det = 0 Then
        MsgBox(0, "", "Determinant is zero. The inverse of this matrix can not be calculated.")
        $avRET = 0
    Else
        $ajnt = _MatrixAdjoint($a)
        ;_ArrayDisplay($ajnt,"Adjoint")
        If $m = 0 Then
            $avRET = _MatrixScalarProduct($ajnt, 1 / $det)
        Else
            ;============== Mod
            If $m > 0 Then
                $mul = _ModInv(Mod($det, $m), $m)
                ;If $decplaceval <> 0 Then $mul = Round($mul, $decplaceval)
                ;MsgBox(0,"",$mul)
                $avRET = _MatrixScalarProduct($ajnt, $mul)
                $avRET = _MatrixMod($avRET, $m)
            EndIf
        EndIf
    EndIf
    ;===============> End of Mod Section
    Return $avRET
EndFunc ;==>_MatrixInv

;Local $a,$b[2][2] = [[2,1],[3,4]], $c[2][2] = [[2,0],[0,2]]
;$a = _MatrixMinusMatrix($b, $c)
;_ArrayDisplay($a)
;===================================================
; matrix $matr1 minus matrix $matr2
Func _MatrixMinusMatrix($matr1, $matr2)
    If UBound($matr1, 2) <> UBound($matr2) And UBound($matr2, 2) <> UBound($matr1) Then
        SetError(0)
        Return -1
    EndIf
    Local $r = UBound($matr1), $cols = UBound($matr1, 2)
    Local $m[$r][$cols]
    For $i = 0 To $r - 1
        For $j = 0 To $cols - 1
            $m[$i][$j] = $matr1[$i][$j] - $matr2[$i][$j]
        Next
    Next
    Return $m
EndFunc ;==>_MatrixMinusMatrix

;Local $a,$b[2][2] = [[5,6],[7,4]], $c = 4
;$a = _MatrixMod($b, $c)
;_ArrayDisplay($a)
;===================================================
;_MatrixMod($a, $m) Returns the the molulo of array $a to modulus $m
;convert negative mod result no. to positive no.
; e.g (-17 Mod 47 ) = (30 Mod 47 )
; i.e. 47 + (-17) = 30
Func _MatrixMod($a, $m)
    ; Check for parameter errors.
    If IsArray($a) = 0 Then Return SetError(1, 1, 0) ; must be array

    Local $iRows = UBound($a), $iCols = UBound($a, 2), $iDepth = UBound($a, 2)
    Local $aRET[$iRows][$iCols]
    For $r = 0 To $iRows - 1
        For $c = 0 To $iCols - 1
            ;If $decplaceval <> 0 Then
            ;   $aRET[$r][$c] = round(mod($a[$r][$c],$m),$decplaceval)
            ;Else
            $aRET[$r][$c] = Mod($a[$r][$c], $m)
            ;EndIf
            If $m > 0 Then
                If $aRET[$r][$c] < 0 Then $aRET[$r][$c] = $m + $aRET[$r][$c] ;convert negative mod result no. to positive no.
            EndIf
        Next
    Next
    Return $aRET
EndFunc ;==>_MatrixMod

;Local $a,$b[2][2] = [[2,1],[3,4]], $c[2][2] = [[2,0],[0,2]]
;$a = _MatrixPlusMatrix($b, $c)
;_ArrayDisplay($a)
;===================================================
; matrix $matr1 plus matrix $matr2
Func _MatrixPlusMatrix($matr1, $matr2)
    If UBound($matr1, 2) <> UBound($matr2) And UBound($matr2, 2) <> UBound($matr1) Then
        SetError(0)
        Return -1
    EndIf
    Local $r = UBound($matr1), $cols = UBound($matr1, 2)
    Local $m[$r][$cols]
    For $i = 0 To $r - 1
        For $j = 0 To $cols - 1
            $m[$i][$j] = $matr1[$i][$j] + $matr2[$i][$j]
        Next
    Next
    Return $m
EndFunc ;==>_MatrixPlusMatrix

;Local $a,$b[2][2] = [[2,1],[3,4]], $c = 5
;$a = _MatrixPlusScalar($b, $c)
;_ArrayDisplay($a)
; #FUNCTION# =====================================================================
; Name...........: _MatrixPlusScalar
; Description ...: Adds a scalar (number) to each element in a matrix (numeric array).
; Syntax.........: _MatrixPlusScalar($a, $k)
; Parameters ....: $aMat - A matrix (numeric array)
; $k    - A scalar (number) that is to be added to each element of the matrix
; Return values .: Success
;   Failure
; Author ........:
; Modified.......:
; Remarks .......:
;
; Related .......:
; Link ..........;
; Example .......;
; ===============================================================================
;Matrix $a plus a Scalar (number) $k
Func _MatrixPlusScalar($aMat, $k)
    If IsArray($aMat) = 0 Then Return SetError(1, 1, 0) ; must be array
    Local $iRows = UBound($aMat), $iCols = UBound($aMat, 2)
    Local $aRET[$iRows][$iCols]
    For $r = 0 To $iRows - 1
        For $c = 0 To $iCols - 1
            $aRET[$r][$c] = $aMat[$r][$c] + $k
        Next
    Next
    Return $aRET
EndFunc ;==>_MatrixPlusScalar

;Local $a,$b[3][3] = [[2,1,2],[10,2,6],[3,4,1]], $c[3][3] = [[2,3,8],[2,2,2],[0,2,6]]
;$a = _MatrixProduct($b, $c)
;_ArrayDisplay($a)
;==========================================================================
; Function: _MatrixProduct (modified mod added)
; Purpose: Calculate the ordinary matrix product of two matricies, as described at:
;   en.wikipedia.org/wiki/Matrix_multiplication#Ordinary_matrix_product
; Call with:    _MatrixProduct( $avA, $avB )
; Where:    $avA and $avB are 2D arrays of numbers where the
;   row count (depth) of $avA matches the column count of $avB
; On success: Returns a 2D array (product matrix)
; On failure: Returns 0 and sets @error and @extended for invalid inputs
;               @extended = 1 - Parameters $avA & $avB, one or both not an array
;               @extended = 2 - Parameters $avA & $avB, both must be 2D arrays
;               @extended = 3 - No. of columns in $avA must equal No. of rows in $avB.
;==========================================================================
Func _MatrixProduct($avA, $avB, $m = 0)
    local $x
    ; Check for parameter errors.
    If IsArray($avA) = 0 Or IsArray($avB) = 0 Then Return SetError(1, 1, 0) ; both must be arrays
    If UBound($avA, 0) <> 2 Or UBound($avB, 0) <> 2 Then Return SetError(1, 2, 0) ; both must be 2D arrays
    If UBound($avA, 2) <> UBound($avB) Then Return SetError(1, 3, 0) ; depth must match

    ; Create return array
    Local $iRows = UBound($avA), $iCols = UBound($avB, 2), $iDepth = UBound($avA, 2)
    Local $avRET[$iRows][$iCols]

    ; Calculate values
    For $r = 0 To $iRows - 1
        For $c = 0 To $iCols - 1
            $x = 0
            For $z = 0 To $iDepth - 1
                $x += ($avA[$r][$z] * $avB[$z][$c])
                If $x < 10 ^ - 11 And $x > -10 ^ - 11 Then $x = Int($x); Rounded at 11 decimal places
            Next
            ;============== Mod ==========
            If $m = 0 Then
                $avRET[$r][$c] = $x
            Else
                If IsNumber($m) Then
                    ;   If $decplaceval <> 0 Then $x = round($x,$decplaceval)
                    $avRET[$r][$c] = Mod($x, $m)
                EndIf
            EndIf
            ;========> End of Mod Section ====
        Next
    Next
    Return $avRET
EndFunc ;==>_MatrixProduct

;Local $a,$b[4][2] = [[2,1],[3,4],[2,0],[0,2]]
;_ArrayDisplay($b)
;$a = _MatrixTranspose($b)
;_ArrayDisplay($a)
;===================================================
; Transpose a matrix columns become rows and rows become columns.
Func _MatrixTranspose(ByRef $a)
    Local $tms = UBound($a), $cols = UBound($a, 2)
    dim $m[UBound($a, 2)][UBound($a,1)]
    For $i = 0 To $tms - 1
        For $j = 0 To $cols - 1
            $m[$j][$i] = $a[$i][$j]
        Next
    Next
    Return $m
EndFunc ;==>_MatrixTranspose

;Local $a,$b[2][2] = [[2,1],[3,4]], $c = 0.5
;$a = _MatrixScalarProduct($b, $c)
;_ArrayDisplay($a)
;===================================================
;Matrix $a multiplied by a Scalar (number) $k
Func _MatrixScalarProduct($a, $k)
    ; Check for parameter errors.
    If IsArray($a) = 0 Then Return SetError(1, 1, 0) ; must be array

    Local $iRows = UBound($a), $iCols = UBound($a, 2)
    Local $aRET[ UBound($a)][UBound($a, 2)]
    For $r = 0 To $iRows - 1
        For $c = 0 To $iCols - 1
            $aRET[$r][$c] = $a[$r][$c] * $k
        Next
    Next
    Return $aRET
EndFunc ;==>_MatrixScalarProduct

;Local $a,$b[5][5] = [[1, 0, 0, 0, 0],[0, 1, 0, 0, 0],[0, 0, 1, 0, 0],[0, 0, 0, 1 , 0],[0.2, 0.2, 0.2, 0, 1]]
;Local $a,$b[3][3] = [[2,-3,1],[2,0,-7],[1,0,-2]]
;$a = _MatrixUpperTriangle($b)
;_ArrayDisplay($a)
;===================================================
; The diagonal elements of the upper triangle matrix multiplied together gives the determinant of the matrix.
Func _MatrixUpperTriangle($a)
    If UBound($a, 2) <> UBound($a) Then Return SetError(1, 3, 0) ; depth must match
    Dim $tms = UBound($a), $f1 = 0, $temp = 0, $v = 1, $mc = $a, $stopLoop = 0
    ;MsgBox(0,"",UBound($a) & " " & UBound($a, 2) )
    $iDF = 1
    ;MsgBox(0,"iNFO"," Matrix must be square uPPER TRI. L223 ROW = "& UBound($a) & " COLUMN = "&UBound($a, 2) )
    For $col = 0 To $tms - 1
        For $row = $col + 1 To $tms - 1
            $v = 1
            $stopLoop = 0
            While $mc[$col][$col] = 0 And $stopLoop = 0

                If $col + $v >= $tms Then
                    $iDF = 0
                    $stopLoop = 1
                Else
                    For $c = 0 To $tms
                        If $col < $tms And $c < $tms Then
                            $temp = $mc[$col][$c]
                            $mc[$col][$c] = $mc[$col + $v][$c]
                            $mc[$col + $v][$c] = $temp
                        EndIf
                    Next
                EndIf
                $v = $v + 1
                $iDF = $iDF * (-1)
            WEnd
            If $mc[$col][$col] <> 0 Then
                $f1 = (-1) * $mc[$row][$col] / $mc[$col][$col]
                For $i = $col To $tms - 1
                    $mc[$row][$i] = $f1 * $mc[$col][$i] + $mc[$row][$i]
                    ;MsgBox(0,"Prim", $col & " " & $mc[$row][$i])
                Next
            EndIf
            $v = 1
        Next
    Next
    Return $mc
EndFunc ;==>_MatrixUpperTriangle

;local $s = 286, $t = 429
;MsgBox(0, "", "Greatest Common Denominator of " & $s & " and " & $t & " = " & _GCD($s, $t))
;===================================================
;Greatest Common Denominator - Euclid's algorithm.
;(Sarah Flannery, "In Code",Profile Books Ltd,p249-250.)
Func _GCD(ByRef $a, ByRef $m)
    Dim $p = 0, $r = 0
    $p = Mod($a, $m)
    ;MsgBox(0,"Prim", $a)
    If $p = 0 Then
        $a = $r
    Else
        $r = $m
        Do
            $b = Mod($r, $p)
            $r = $p
            $p = $b
        Until $b = 0
        ;MsgBox(0,"Prim", $r)
    EndIf
    Return $r
EndFunc ;==>_GCD

;MsgBox(0, "", " Modular inverse of mod(113, 39) = " & _ModInv(113, 39) & @CRLF & _
;       "Mod(" & _ModInv(113, 39) & " * 113, 39) = " & Mod(_ModInv(113, 39) * 113, 39))
;===================================================
;Determines the reciprocal or multiplicative modular inverse of, a Modulo m
; provide a and m have no common factors.
;$a and $m are numbers
Func _ModInv($a, $m)
    Dim $minu = 0
    If $a < 0 Then
        $a = $a * - 1
        $minu = 1 ;flag for negative value
    EndIf
    Dim $p = 0
    Do
        $p = $p + 1
    Until Mod(($p * $a), $m) = 1 Or $p = $m
    If $minu = 1 Then
        $p = $m - $p
    EndIf
    ;If $decplaceval <> 0 Then $p = Round($p, $decplaceval)
    Return $p
EndFunc ;==>_ModInv

;MsgBox(0, "", "Prime factors of 112234 = " & _Prim(182234))
;===================================================
; Given a Number $a returns the factors of that number
Func _Prim($a)
    $factors = ""
    Dim $b, $c
    If $a > 0 Then
        If $a <> 0 Then
            $a = Int($a)
            While $a / 2 - Int($a / 2) = 0
                $a = $a / 2
                $factors = $factors & "2 "
            WEnd
            $b = 3
            $c = ($a) * ($a) + 1
            Do
                If $a / $b - Int($a / $b) = 0 Then
                    If $a / $b * $b - $a = 0 Then
                        $factors = $factors & $b & " "
                        $a = $a / $b
                    EndIf
                EndIf
                If $a / $b - Int($a / $b) <> 0 Then $b = $b + 2
                $c = ($a) * ($a) + 1
            Until $b > $c Or $b = $c

            If $a <> 1 Then $factors = $factors & $a & " "
        EndIf
    EndIf
    ;MsgBox(0,"Prim", $factors)
    Return $factors
EndFunc ;==>_Prim

Hope this helps.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Awesome

Could someone write an example of how to create a hash? (a hash is just an encryption that cant be undone because the inverse of the key is 0 that has a regular format.)

I have got the basic idea down, but have not got it into a format that makes each hash unique of any other hash and have a specific form.

Edited by IchBistTod

[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0