How should I simplify nested loops?

yes, I understand the if-else logic of julialang, but still get stuck with rewriting the below:

    Q = [
          ( (i <= n_prev && j <= n_prev) ? Q_prev[i, j] : ( i <= n_prev ? Q_aug[i, j-n_prev] :
          ( j <= n_prev ? Q_aug[j, i-n_prev] : Q_aug_diag[i-n_prev, j-n_prev] ) ) )::Float64
          for i=1:n_const, j=1:n_const]

could you show me the other way around? Thank you!

Did you try our suggestions? How does it look when you do? Please post your attempt and we can suggest how to rework it if necessary.

I don’t intend to write it for you if the problem is simply that you don’t feel like doing it yourself…

yes, I wrote like this by taking above suggestion:

Q was like this:

Q = [
      ( (i <= n_prev && j <= n_prev) ? Q_prev[i, j] : ( i <= n_prev ? Q_aug[i, j-n_prev] :
      ( j <= n_prev ? Q_aug[j, i-n_prev] : Q_aug_diag[i-n_prev, j-n_prev] ) ) )::Float64
      for i=1:n_const, j=1:n_const]

I wrote like this as follow:

Q = []
    for i=1:n_const:
      for j=1:n_const:
        if i <= n_prev && j <= n_prev:
          if i <= n_prev:
            Q_prev[i,j] = Q_aug[i, j-n_prev]
          else j <= n_prev:
            Q_aug[j, i-n_prev] = Q_aug_diag[i-n_prev, j-n_prev]
          end
        end
      end
      push!(Q_aug_diag, Q_aug)
    end

could you correct me if I did it right here? I am not sure about this, and using push!(). Am I right about here? any thoughts? Thanks!

It should be like this instead:

Q = zeros(n_const,n_const)

for i = 1:n_const, j = 1:n_const
    if i <= n_prev && j <= n_prev 
        Q[i,j] = Q_prev[i, j]
    elseif i <= n_prev
        Q[i,j] = Q_aug[i, j-n_prev]
    elseif j <= n_prev
        Q[i,j] = Q_aug[j, i-n_prev]
    else 
        Q[i,j] = Q_aug_diag[i-n_prev, j-n_prev]
    end  
end 
3 Likes

thanks! this is what I want.

I am just curious about this: for i = 1:n_const, j = 1:n_const only works for julialang convention, or it also valid for python?

I mean, is this

        for i = 1:n_const, j = 1:n_const:
            ...
            ...

as same as this:

        for i = 1:n_const:
            for j = 1:n_const:
                ...
                ...

how these two different? any idea?

No, I don’t think you can write the same in Python (not an expert). You need the Pythonic way:

for i in range(1,n_const+1):
    for j in range(1,n_const+1):
        Q[i,j] = ...
1 Like

Thanks a lot. Your comment was very helpful, I would accept it as solution.

Plus, I am curious about this:

is this code below:

    Q_aug = [ ( calc_dot((const_prev[i][1], const_prev[i][2], const_added[j][1], const_added[j][2]), K, y) )::Float64
          for i=1:n_prev, j=1:n_added]

equal to this one which I wrote below:

Q_aug= []
    for i=1:n_prev:
      for j=1:n_added:
        Q_aug[i,j]= calc_dot((const_prev[i][1], const_prev[i][2], const_added[j][1], const_added[j][2]), K, y)
      end
    end

function body of calc_dot is in this gist. Am I right about on Q_aug? could you give me your possible feedback if I was wrong about it? Thanks again for your help in this community!

No, it is not equal. Your code block is neither valid Julia nor python. Look at @Seif_Shebl answer above for proper Julia code.

how that supposed to be corrected? should I use psuh!()? I am not quite sure about what I wrote. How could you write Q_aug in simplistic way? any idea?

Frankly, I think you should take a step back and try to work through the manual. IMO, you seem to be lacking basic understanding of Julia and its syntax (which of course is perfectly fine and expected for a beginner!). I don’t think it is too helpful to successively ask for rewrites of blocks of code here.

Also, I don’t fully understand why you would expect or want the same syntax to work in python and Julia. After all, these are different programming languages.

6 Likes

I looked into manual and followed above useful comments from @ Seif_Shebl about the syntax. But I don’t understand why you think doing this way is wrong. Can you comment what was wrong about this?

Q_aug= []
    for i=1:n_prev:
      for j=1:n_added:
        Q_aug[i,j]= calc_dot((const_prev[i][1], const_prev[i][2], const_added[j][1], const_added[j][2]), K, y)
      end
    end

it is not quite straightforward to me at first place because there is no MWE that I could make. For julia learner, I looked into julialang documentation, and sample project that I am learning. Could you show what’s the valid way to write above? any idea?

As @carstenbauer said, you should go through the manual to get yourself familiar with the main syntax of Julia as you would do when learning C or MATLAB for the first time. You will find useful yet simple examples that will sharpen your skills in Julia. For example, the Repeated Evaluation section will teach you some ways to write and understand loops.

As for your code segment above, here are some comments:

  1. The Q_aug= [] will create a Vector with 0 elements of type Any, this will not be efficient in Julia because it is type-unstable
  2. There is no :s at the end of fors or ifs like in Python
  3. Finally, julia is a different language from Python, so you should learn it and don’t expect Julia code to be identical to Python code. Julia is a newer language with cool modern features not available in Python or even in most similar-age languages, so why not put the effort to learn it and it will pay you off later?
2 Likes

Did you test it? You will get an error message. To learn any programming language you have to code, test your code all the time, work through the errors, and have the manual and/or other references at your side.

2 Likes

could you direct me best sources for beginners like me? I jumped into Julia for project needs and looked into built-in functions and documentation, but I think not everything is crystal clear to newbie. I will take your comment series for learning. Thanks!

3 Likes