Guiseppe

In addition to using recursion, you can also use a macro to generate the
code.

However, this functionality is available in the "Cartesian" module which is
part of Base Julia. You are probably looking for "@nloops".

-erik


On Mon, Sep 28, 2015 at 11:24 AM, Giuseppe Ragusa <giuseppe.rag...@gmail.com
> wrote:

> I am having problems (serious problems!) to deal with algorithms that boil
> down do nested for loops but whose number of loops is not known at compile
> time. As an example consider this:
>
> ```
>  function tmp()
>      r = 3
>      n = 100
>      λ = zeros(r)
>      u = rand(n, r)
>      counter = 0
>      Y = Array(Float64, n)
>
>      @inbounds for j_1 = 0:k
>
>          for i = 1:n
>              Y[i] = pow(u[i, 1], j_1)
>          end
>
>          λ[1] = j_1
>          for j_2 = 0:(k-j_1)
>              λ[2] = j_2
>              for i = 1:n
>                  Y[i] *= pow(u[i, 2], j_2)
>              end
>
>              for j_3 = 0:(k-j_1-j_2)
>                  λ[3] = j_3
>                  for i = 1:n
>                      Y[i] *= pow(u[i, 3], j_3)
>                  end
>                  counter += 1
>                  println(λ, " ", " => ", j_1, j_2, j_3, " counter =>",
> counter)
>              end
>          end
>      end
>  end
>  ```
>
> This is what I want when `r = 3`. For larger `r = 4` there would be an
> additional loop. etc. Now, everything is complicated by the fact that `r`
> is not know at compile time.
>
> I have tried to generate the loops using the macros in  Base.Cartesian and
> then using generated functions to accomplish what I want, but I cannot get
> what I want. The main difficulty is that Base.Cartesian I can't get the
> ranges I want).
>
> Does anybody have suggestions on how to deal with this?
>
>
>


-- 
Erik Schnetter <schnet...@gmail.com>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to