Declaring new variable in recursive function

John Kim

I'm trying to implement Strassen's matrix multiplication algorithm in VBScript. However I'm having a lot of issues with the recursion.

In the following recursive algorithm:

Function multiply(matrixA, matrixB)
    Dim n
    n = UBound(matrixA) + 1
    ReDim R(n - 1, n - 1)

    If (n = 1) Then
        R(0,0) = matrixA(0,0) * matrixB(0,0)
    Else
        ReDim A11(n/2 - 1, n/2 - 1)
        ReDim A12(n/2 - 1, n/2 - 1)
        ReDim A21(n/2 - 1, n/2 - 1)
        ReDim A22(n/2 - 1, n/2 - 1)
        ReDim B11(n/2 - 1, n/2 - 1)
        ReDim B12(n/2 - 1, n/2 - 1)
        ReDim B21(n/2 - 1, n/2 - 1)
        ReDim B22(n/2 - 1, n/2 - 1)

        Call PartitionMatrix(matrixA, A11, 0, 0)
        Call PartitionMatrix(matrixA, A11, 0, n/2 - 1)
        Call PartitionMatrix(matrixA, A21, n/2 - 1, 0)
        Call PartitionMatrix(matrixA, A22, n/2 - 1, n/2 - 1)
        Call PartitionMatrix(matrixB, B11, 0, 0)
        Call PartitionMatrix(matrixB, B12, 0, n/2 - 1)
        Call PartitionMatrix(matrixB, B21, n/2 - 1, 0)
        Call PartitionMatrix(matrixB, B22, n/2 - 1, n/2 - 1)

        Dim M1, M2, M3, M4, M5, M6, M7
        M1 = multiply(AddMatrix(A11, A22), AddMatrix(B11, B22))
        M2 = multiply(AddMatrix(A21, A22), B11)
        M3 = multiply(A11, SubMatrix(B12, B22))
        M4 = multiply(A22, SubMatrix(B21, B11))
        M5 = multiply(AddMatrix(A11, A12), B22)
        M6 = multiply(SubMatrix(A21, A11), AddMatrix(B11, B12))
        M7 = multiply(SubMatrix(A12, A22), AddMatrix(B21, B22))
        Wscript.Echo UBound(R) 'Returns only 0's in console

        Dim C11, C12, C21, C22
        C11 = AddMatrix(SubMatrix(AddMatrix(M1, M4), M5), M7)
        C12 = AddMatrix(M3, M5)
        C21 = AddMatrix(M2, M4)
        C22 = AddMatrix(SubMatrix(AddMatrix(M1, M3), M2), M6)

        Call JoinMatrix(C11, R, 0 , 0)
        Call JoinMatrix(C12, R, 0 , n/2 - 1)
        Call JoinMatrix(C21, R, n/2 - 1, 0)
        Call JoinMatrix(C22, R, n/2 - 1, n/2 - 1)
    End If
    multiply = R
End Function

I am assigning the return value of the multiply function into variables M1, M2, and so forth. However, it seems that the variable R that I am returning in multiply is being carried over from the recursive function - as in, it's not declaring a new matrix R. In theory, if R was getting a new memory location every time it was being declared in the function it shouldn't be affected by the recursive calls to the multiply function at all (where I am echo'ing UBound(R)), but it is. How can I make sure R is actually getting a new declaration every time the function is called in the recursion?

Unhandled Exception

R it is not explicitly dimensioned anywhere, so it is implicitly dimensioned out of the function and you just re-dimension it in the function.

Insert Dim R() directly above ReDim R(n - 1, n - 1) to dimension it in the function explicitly and it should work as you expect.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Declaring variable outside a function

Declaring variable within a function

Using a function directly or by declaring it to a variable

Is declaring a function variable required (in Javascript)?

Calling a function like declaring a variable

Are we declaring a function as a method on a variable?

javascript - what is the purpose of declaring a variable a function first? (declaring variable twice)

Recursive function with outer variable

recursive function variable initialization

recursive function affecting variable

Recursive function variable confusion

Haskell "fix" keyword failed on declaring a recursive lambda function

Declaring INT variable to return in MYSQL Function problem

Declaring macro variable with evaluation of function as value

Declaring a variable in a public function (Excel VBA)

Declaring a set of Variables as a Function of Another Variable

New line after a recursive function

In JavaScript: Is a function parameter variable (at the moment of function declaration) equivalent to declaring a variable?

Recursive function not iterating variable as expected

Value of variable on recursive calling of a function

static variable in recursive function in c

Local variable returned in recursive function

Minimizing variable copies in recursive function

Variable in recursive function keeps it value

is there a way to declare a variable with the value of a function which takes this declaring variable as a parameter?

Replace global variable with static variable in recursive function

Declaring variable (difference between int c; and int c=new int();

Using inline object method call vs. declaring a new variable

Recursive call to templatized function with new template arguments