1

I want to create a function (or anything, really) that is capable of calling its own logic.

The idea is as follows:

Funccall[a_,b_,n_]:=
  For[i=1,i<=n,i++,{Return[i*(4+i/3)*Funccall[a,b,n-1];}

or something like that. Not the exact function; it's too long. How do i achieve this effect?

Edit

Here's the purpose of the code: to generate all products from a matrix from elements which dont share rows or columns

For[i in n, for j in n, for k in in [This goes on till n times i.e. n for loops]
  if j!=i && k!=i &&k!=j &&.....->Multiply[a1i,a2j,a3k,a4l..........]

I need a function to do this.

The If condition I've implemented by creating an empty array used that stores whatever numbers have been used, and then I use FreeQ[used, k].

I hope this gives some clarity.

Ashwin Kumar
  • 185
  • 6
  • 2
  • 4
    f[0] = 1; f[x_] := x f[x - 1]; Factorial implemented recursively. Generally using For and Return are not the best use of Mathematica's capabilities. This may be an X-Y problem, what are you actually trying to accomplish? – eyorble Apr 06 '18 at 18:55
  • @corey979 thanks i think that could help. The problem is that my parameters will also change inside the nesting. WIll have to find a way to work around that – Ashwin Kumar Apr 06 '18 at 18:58
  • @eyorble im trying to implement a product rule, that multiples elements only when they are not from the same row or column. Its a sparse matrix, so dont want to do this for all elements – Ashwin Kumar Apr 06 '18 at 18:59
  • 4
    That sounds like a far more interesting question than how to implement recursive functions in Mathematica, so if you're willing to write up the general idea of what you're trying to accomplish there and a minimal working example with what you already have, it'd probably be more helpful for you. – eyorble Apr 06 '18 at 19:07
  • If it is just getting the products in a sparse matrix, use sparse array properties. See https://mathematica.stackexchange.com/a/83722/34732 – no-one Apr 10 '18 at 19:49

1 Answers1

0

In a simple pseudo code this is a simple Recursive method to visit all nodes in a tree and set the X,Y to known values. It will unwind itself when the maximum depth has been reached. This might help you with some logic. Sorry its not your native format.

    To initialise:
    Call Recurse(RootNode, 1, 1, 0, 20)

    Recurse(TreeNode node, int X, int Y, int depth, int MaxDepth) {

      node.X = X
      node.Y = Y
      depth++
      if (depth == MaxDepth)    
       return

      TreeNode nextNode = node.NextNode
      Recurse(nextNode, X, Y, depth, MaxDepth)

    }