Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

Problem Storing Arrays Inside Arrays

$
0
0

Hello guys, I have a matrix inverse function that I am trying to improve the accuracy of by making it preform math on fractions instead of floats, however is appears I'm having issues accessing an array embedded inside a 2D array ($array[1][1][0]) where [0] would contain a 1D array with 2 values in it.

 

See my code below, and as always thanks a ton! (you will need the fractions UDF http://www.autoitscript.com/forum/topic/165558-fraction/)

AutoIt         
#include "fractions.au3" #include <array.au3> $matricies = _loaddata(_datagen()) _inv_big($matricies[0]) Func _inv_big($m)         Local $n = UBound($m, 1)         Local $a[$n][2 * $n]         Local $inv[$n][$n]         Local $temprow[2 * $n]         Local $pivelt, $tarelt, $pivrow, $tarrow, $k, $i, $j         #Region - Filling matrix A             For $i = 0 To $n - 1                 For $j = 0 To $n - 1                     $a[$i][$j] = $m[$i][$j]                 Next             Next             For $i = 0 To $n - 1                 $a[$i][$n + $i] = 1             Next         #EndRegion - Filling matrix A         #Region - Inverting matrix $M             For $pivrow = 0 To $n - 1                 $pivelt = $a[$pivrow][$pivrow]                 If ($pivelt == 0) Then                     $k = $pivrow + 1                     While ($pivelt == 0 AND $k <= $n)                         $pivelt = $a[$k][$pivrow]                         $k = $k + 1                     WEnd                     If ($pivelt == 0) Then                         SetError(1)                         Return                     Else                         For $i = 0 To 2 * $n - 1                             $temprow[$pivrow][$i] = $a[$pivrow][$i]                             $k = $k - 1                         Next                         For $i = 0 To 2 * $n - 1                             $a[$pivrow][$i] = $a[$k][$i]                         Next                         For $i = 0 To 2 * $n - 1                             $a[$k][$i] = $temprow[$k][$i]                         Next                     EndIf                 EndIf                 For $i = 0 To 2 * $n - 1                     $a[$pivrow][$i] = _fraction(Number($a[$pivrow][$i]), Number($pivelt))                     ;array is accessable here                 Next                 #Region - replace all other rows by target                     For $tarrow = 0 To $n - 1                         If ($tarrow <> $pivrow) Then                             $tarelt = $a[$tarrow][$pivrow]                             For $i = 0 To 2 * $n - 1                                 $a[$tarrow][$i] = _FractionSubtract(_fraction(number($a[$tarrow][$i])), _FractionMultiply($a[$pivrow][$i], _fraction(number($tarelt))))                                 _ArrayDisplay($a[$tarrow][$i])                                 ;array is accessable here via _arraydisplay() but when I try to consolewrite the values it says the dimension range is exceeded                                 ConsoleWrite($a[$tarrow][$i][0]&'/'&$a[$tarrow][$i][1]&@CRLF)                                                             Next                          EndIf                       Next                       ;array is accessable here with _arraydisplay but still not via any other method                 #EndRegion - Now replace all other rows by target row minus pivot row times element in target row and pivot colum              Next                 _ArrayDisplay($a)              ;array is nullified, all values for the array get set to 1             #Region - finally extract the inverse from columns N+1 to 2N                 For $i = 0 To $n - 1                     For $j = 0 To $n - 1                         $inv[$i][$j] = $a[$i][$n + $j]                     Next                 Next             #EndRegion - finally extract the inverse from columns N+1 to 2N             Return $inv         #EndRegion - Inverting matrix $M      EndFunc      Func _datagen()         Local $ret         For $i = 1 To 112             Local $data = ""             For $x = 0 To 7                 For $y = 0 To 7                     $data &= Random(111111111, 99999999999, 1) & ","                 Next                 $data = StringTrimRight($data, 1) & "/"             Next             $ret &= StringTrimRight($data, 1) & "|"         Next         Return $ret      EndFunc     Func _loaddata($data, $z = 112)         Local $dataz[$z]         $array = StringSplit($data, "|")         If $array[0] < $z - 1 Then Return 0         For $i = 0 To $z - 1             $dataz[$i] = _stringtoarray($data, $i)             If $dataz[$i] = 0 Then                 Return 0             EndIf         Next         Return $dataz     EndFunc     Func _stringtoarray($data, $n = 0)         $array = StringSplit($data, "|", 2)         $next = StringSplit($array[$n], "/")         $num2 = StringSplit($next[1], ",")         Local $ar1[$next[0]][$num2[0]]         For $i2 = 1 To $next[0]             $next2 = StringSplit($next[$i2], ",", 2)             For $i3 = 0 To UBound($next2) - 1                 If $i2 - 1 > $next[0] - 1 OR $i3 > $num2[0] - 1 OR $i3 > UBound($next2) Then                     Return 0                 EndIf                 $ar1[$i2 - 1][$i3] = $next2[$i3]             Next         Next         Return $ar1     EndFunc

Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>