1

So I'm trying to write a recursion module in Mathematica (specifically for strassen multiplication but this doesn't matter). My issue is I don't really know how If statements work in Mathematica and if I can program recursive functions in the same way as I once learned in Java.

My (incomplete) code is

Strassen[A_, B_, p_] := 
Module[
{A11, A12, A21, A22, B11, B12, B21, B22, Return, 
originalDim = Dimensions[A][[1]], dim, extraDim, cutoff = 40, 
PaddedA, PaddedB},

//IMPORTANT STUFF
If[originalDim <= cutoff, 
Return = mm[A, B, p]
];
//IMPORTANT STUFF


//UNIMPORTANT STUFF
dim = If[NumberIsPowerOfTwo[originalDim], originalDim, 
MinimalPowerOfTwo[originalDim]];
PaddedA = PadRight[A, {Automatic, dim}];
PaddedA = PadRight[Transpose[PaddedA], {Automatic, dim}];
PaddedB = PadRight[B, {Automatic, dim}];
PaddedB = PadRight[Transpose[PaddedB], {Automatic, dim}];
A11 = PaddedA[[1 ;; dim/2, 1 ;; dim/2]];
A12 = PaddedA[[dim/2 + 1 ;; dim, 1 ;; dim/2]];
A21 = PaddedA[[1 ;; dim/2, dim/2 + 1 ;; dim]];
A22 = PaddedA[[dim/2 + 1 ;; dim, dim/2 + 1 ;; dim]];
B11 = PaddedB[[1 ;; dim/2, 1 ;; dim/2]];
B12 = PaddedB[[dim/2 + 1 ;; dim, 1 ;; dim/2]];
B21 = PaddedB[[1 ;; dim/2, dim/2 + 1 ;; dim]];
B22 = PaddedB[[dim/2 + 1 ;; dim, dim/2 + 1 ;; dim]];

]

Now the point of my function is that at some point I will call it again to do some multiplications on smaller matrices. Eventually when my matrices are small enough I want them to enter the If-statement and return the value "Return", but before that I want it to just skip that part and execute the code below.

Is this how if statements work in Mathematica?

corey979
  • 23,947
  • 7
  • 58
  • 101
Sertii
  • 135
  • 7
  • did you see that? – garej Dec 07 '15 at 21:22
  • Also, you are using Return as a variable (by assigning a value to it). Return is a function and should not be used as a variable. Try ?Return. – bill s Dec 07 '15 at 21:23
  • it is not advisable to start your own symbols with caps. Particulartly Return conflicts with a built in. – george2079 Dec 07 '15 at 21:24
  • Start simple! Write a much simpler little example recursive function with some Print statements and try to figure out how recursion and If and Mathematica work. – Bill Dec 07 '15 at 21:24
  • yes i read through that quite a few times. I don't really understand what it means that the function returns "Null". Will the module return Null and exit as soon as the if statement is false? Or will it continue? And how does the semi-colon affect the output of the If statement? Will it return "Return" if it's true or the output be suppressed?

    Good point about Return, will change that

    – Sertii Dec 07 '15 at 21:24
  • Do you know how I could return my "return" value? The semi-collon surpresses the output yes? – Sertii Dec 07 '15 at 21:33
  • The semi-colon on the If statement isn't the problem. (In fact, you need it.) Remove Return from the list of variables, and change the If statement to If[originalDim <= cutoff, Return[mm[A, B, p]]]. In addition, I am assuming that you want an output from this function even when the If statement is not satisfied. If I am right, what is that output supposed to be? – march Dec 07 '15 at 21:35
  • No, the only way i want output is if I enter the if statement, the rest of the function calls the Strasser function again until the if-statement is entered – Sertii Dec 07 '15 at 21:36
  • But you need an output of the function Strassen that will act as input for the recursive call of Strassen, don't you? – march Dec 07 '15 at 21:37
  • As far as the semicolon goes, this post on the meaning of the semi-colon in Mathematica is tangentially related to your questions. – march Dec 07 '15 at 21:38
  • yes but this is how recursive functions work, the function will keep calling the Strassen function and then use the output from the if-statement for all the previous occurences of Strassen (I'm 99% certain this made zero sense but my english isn't the best, sorry) – Sertii Dec 07 '15 at 21:38
  • I think I see (yes: I was thinking about a different recursion construct I think). Anyway, I think my suggestion above should fix the problem. – march Dec 07 '15 at 21:40
  • yes, it would indeed :) thanks for the link as well, that looks really helpfull! – Sertii Dec 07 '15 at 21:41
  • 1
    Instead of the If you can create separate functions and use pattern matching: Strassen[A_List /; Dimensions[A][[1]] < 40, B_, p_] := .. – george2079 Dec 07 '15 at 21:50

0 Answers0