0

I am new to programming and trying to learn from application. I have a function in python:

def eps(u):         
    return as_vector([u[i].dx(i) for i in range(3)] + [u[i].dx(j) + u[j].dx(i) for i, j in [(0, 1), (0, 2), (1, 2)]])

I understand that in the above function, the statement i in range(3) is going to run a loop for u[0].dx(0)....u[2].dx(2) but I don't understand the for i, j in [(0, 1), (0, 2), (1, 2)]]) part. How is this entire statement going to execute? Can someone pls. explain.

Similarly I have:

def tangent(t):
  return as_matrix([[t[i*6 + j] for j in range(6)] for i in range(6)])

I believe the expression t[i*6 + j] will run 6 times for i = 0:5 and for each value of 'i', we will have 'j = 0:5'. Hence, I will get a 6x6 matrix. Is that correct?

CRG
  • 131
  • 6

1 Answers1

0

A trivial way to determine what a particular piece of code does, is usually to run it and print the values used in that code:

for i, j in [(0, 1), (0, 2), (1, 2)]:
    print (i, j)

This prints

0, 1
0, 2
1, 2

In order words, this creates a list of tuples and the for-loop iterates over each tuple by unpacking each element of the tuple into the variables i and j, which you can use to do what ever you want


Also you are correct in assuming that you get a 6x6 array from the expression:

[[t[i*6 + j] for j in range(6)] for i in range(6)]

Again, you could just print the resulting array to confirm this

smac89
  • 39,374
  • 15
  • 132
  • 179