Sign in to follow this  
Followers 0
Nahuel

Multiplying matrixes?

14 posts in this topic

#1 ·  Posted (edited)

This script:

Dim $A[5][5]
Dim $B[5][5]

$A[1][1]=1
$A[1][2]=2
$A[2][1]=3
$A[2][2]=4

$B[1][1]=5
$B[1][2]=6
$B[1][3]=0
$B[2][1]=-1
$B[2][2]=7
$B[2][3]=-2

For $a=1 to 2
    For $i=1 to 3
        $el = $A[$a][$a]        
        $el1 = $B[$a][$i]
        $el2 = $A[$a+1][$i]
        $el3 = $B[$a+1][$i]
        $11 = ($el*$el1) + ($el2*$el3)
        MsgBox(0,"",$11)
    Next
Next

keeps telling me this:

line 22 : ==> Subscript used with non-Array variable.:

$el = $A[$a][$a]

$el = $A^ ERROR

I really don't get it.. Edited by Nahuel

Share this post


Link to post
Share on other sites



This script:

Dim $A[5][5]
Dim $B[5][5]

$A[1][1]=1
$A[1][2]=2
$A[2][1]=3
$A[2][2]=4

$B[1][1]=5
$B[1][2]=6
$B[1][3]=0
$B[2][1]=-1
$B[2][2]=7
$B[2][3]=-2

For $a=1 to 2
    For $i=1 to 3
        $el = $A[$a][$a]        
        $el1 = $B[$a][$i]
        $el2 = $A[$a+1][$i]
        $el3 = $B[$a+1][$i]
        $11 = ($el*$el1) + ($el2*$el3)
        MsgBox(0,"",$11)
    Next
Next

keeps telling me this:

I really don't get it..

Well $A is technically the same as $a so if you do this it should work... :)

Dim $A[5][5]
Dim $B[5][5]

$A[1][1]=1
$A[1][2]=2
$A[2][1]=3
$A[2][2]=4

$B[1][1]=5
$B[1][2]=6
$B[1][3]=0
$B[2][1]=-1
$B[2][2]=7
$B[2][3]=-2

For $x=1 to 2
    For $i=1 to 3
        $el = $A[$x][$x]       
        $el1 = $B[$x][$i]
        $el2 = $A[$x+1][$i]
        $el3 = $B[$x+1][$i]
        $11 = ($el*$el1) + ($el2*$el3)
        MsgBox(0,"",$11)
    Next
Next

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Well $A is technically the same as $a

Bert won :)

Took me 10 minutes to figure it ...

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Well $A is technically the same as $a so if you do this it should work... :)

Nice job Bert! Just wondering if its always been this way in AutoIt or something new? For the life of me I could have sworn I read somewhere that Variables were case sensitive, but you're right on the money! Even the following code shows this to be the case.

$z = "Little zee"
$Z = "Big Zee"
MsgBox(0,"","$z = " & $z & " .... " & "$Z = " & $Z)

I'm wondering of this is a bug or how Jon's intended the program to function? Seems really odd since AutoIt is written in C++ and that treats $A and $a as two technically different variables. Weird stuff, but good to know!

Share this post


Link to post
Share on other sites

Well $A is technically the same as $a so if you do this it should work... ;)

:)

Thanks Bert... That works perfectly. I was trying to figure it out for a long while last night and didn't think of that.. haha.

And this is all it says in the help file:

Each variable has a name (again, similar to a mailbox) and must start with the $ character and may only contain letters, numbers and the underscore _ character. Here are some example names:

$var1

$my_variable

I guess it makes it a bit easier that it's not case sensitive... even for functions (unlike Java). But in my opinion, variables should be case sensitive :P

Share this post


Link to post
Share on other sites

Notice how you have used

For ... to

..:)

I think (a long shot maybe) the case insensitivity is part of the BASIC "specification".

Share this post


Link to post
Share on other sites

Notice how you have used

For ... to

..;)

I think (a long shot maybe) the case insensitivity is part of the BASIC "specification".

Mmm.. true.

One more thing, does anyone know how to multiply matrixes?

Here it's exaplained:

http://en.wikipedia.org/wiki/Matrix_multip..._matrix_product

Posted Image

Posted Image

I made this script to multiply two matrixes ( A & B ):

A=

-1  3
4   -5
0   2
3x2

B=

3   4   1
0   1   2
2x3

The A*B matrix would be:

-3  -1  5
12  11  -6
0   2   4
3x3

Here's the script:

Dim $A[5][5]
Dim $B[5][5]
Dim $11[6][6]

$A[1][1]=-1
$A[1][2]=3
$A[2][1]=4
$A[2][2]=-5
$A[3][1]=0
$A[3][2]=2

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


;$j=number of columns of A*B=3
;$i=number of rows of A*B=3

for $j=1 to 3
    For $i=1 to 3
        $11 = ($A[$j][1]*$B[1][$i]) + ($A[$j][2]*$B[2][$i])
        MsgBox(0,"",$11)
    Next
Next

You can see that it works great, but I can't generalize it! That script will only work for matrixes like:

A:3x2 and B:2x3.

I made this but it works VERY bad. It can add though...

If someone knows a solution, it'll be highly appreciated :)

Sorry for long post.

Share this post


Link to post
Share on other sites

The thing is, I can't seem to find it in the helpfile... ;) lol. Can anyone find it?

No doubt, that's why I worded my response the way I did. I too double checked the Help file and couldn't find anything saying variables are case sensitive. I must have retained that from recently reading a tutorial on C/C++ :) And of course since SciTE4AutoIt3 has the cool Tidy that correct "foR ... tO" to "For ... To" that only adds to the idea that "proper" code is case sensitive. I mean Tidy doesn't "correct" the case of variables, right???

I think (a long shot maybe) the case insensitivity is part of the BASIC "specification".

I did a quick google search to see if BASIC is really case INsensitive. Here's an interesting April 11, 2006 blog entry from msdn.com talking about VB. The following quote is an excerpt from the article which contains example code that can be compiled and tested if anyone is interested. And just because BASIC is case insensitive doesn't mean AutoIt has to be, after all its a " BASIC-like scripting language" :P

The appearance here is that VB is not only allowing function names that vary in no way other than case to co-exist, but also that will appear to be calling the wrong function sometimes!

So now VB ... has lost its case insensitivity!

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Whoop! I love an array challenge! ;)

This appears to implement the ordinary matrix product as described in the Wikipedia page:

#include <array.au3>

Dim $avFirst[3][2] = [[ -1, 3], [4, -5], [0, 2]]
Dim $avSecond[2][3] = [[3, 4, 1], [0, 1, 2]]

$avAnswer = _ArrayProduct($avFirst, $avSecond)
_ArrayDisplay($avAnswer, "_ArrayProduct()")

;==========================================================================
; Function:     _ArrayProduct
; Purpose:      Calculate the ordinary matrix product of two matricies, as described at:
;                   en.wikipedia.org/wiki/Matrix_multiplication#Ordinary_matrix_product
; Call with:    _ArrayProduct( $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
;==========================================================================
Func _ArrayProduct($avA, $avB)
    ; 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])
            Next
            $avRET[$r][$c] = $x
        Next
    Next
    
    Return $avRET
EndFunc   ;==>_ArrayProduct

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Whoop! I love an array challenge! :P

This appears to implement the ordinary matrix product as described in the Wikipedia page:

:)

;) Awesome! Thank you so much! I must see how you got this working...

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

:) Awesome! Thank you so much! I must see how you got this working...

[[-1, 3], [4, -5], [0, 2]] * [[3, 4, 1], [0, 1, 2]]

[-1*[3, 4, 1] + 3*[0, 1, 2]] ; -1*3, -1*4, -1*1 = [-3, -4, -1]
[4*[3, 4, 1] + -5*[0, 1, 2]]
[0*[3, 4, 1] + 2*[0, 1, 2]]

[-3, -4, -1] + [0, 3, 6] ; -3+0, -4+3, -1+6 = [-3, -1, 5]
[12, 16, 4]  + [0, -5, -10]
[0, 0, 0] + [0, 2, 4]

[-3, -1, 5]
[12, 11, -6]
[0, 2, 4]

I hope that helps ;)

Edited by aslani

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Share this post


Link to post
Share on other sites

[[-1, 3], [4, -5], [0, 2]] * [[3, 4, 1], [0, 1, 2]]

[-1*[3, 4, 1] + 3*[0, 1, 2]] ; -1*3, -1*4, -1*1 = [-3, -4, -1]
[4*[3, 4, 1] + -5*[0, 1, 2]]
[0*[3, 4, 1] + 2*[0, 1, 2]]

[-3, -4, -1] + [0, 3, 6] ; -3+0, -4+3, -1+6 = [-3, -1, 5]
[12, 16, 4]  + [0, -5, -10]
[0, 0, 0] + [0, 2, 4]

[-3, -1, 5]
[12, 11, -6]
[0, 2, 4]

I hope that helps :)

Thank you, but I do know how to operate with matricies. I just couldn't figure out a way to do it with AutoIt.

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