[Tutor] problems with numdifftools

Bolli nicolaiheitz at gmx.de
Tue Oct 26 20:59:40 CEST 2010


First of all thanks for your response. I am also new to this forum so I
expected to receive an email if somebody replies to my question. Therefore I
just read it today....
> I am not sure if you are the right persons to contact but if not I
> would appreciate a short notice and maybe an address where I can find
> help.

I'd try the numpy mailing list:


Alright. I will try that.

> 5) In principal Python can divide those numbers (2.3e-28)/(5.6e-6)**3

I get half the result you suggest above:

>>> (2.3e-28)/(5.6e-6)**3

which agrees with my HP-48GX, and the result after simplifying by hand 
to 2.3e-10/175.616. So I think that is the correct result, not 2.6e-12.

You are totally right. I forgot to write down a factor of 2. I am so sorry
about it!!!!!! 

> My source code is attached. Please keep in mind that I just started
> programing and Python is my first Language. I tried to do my best to
> comment every single step to make the code readable. Here it comes:

Comments are good, but too many comments hurt readability. Don't feel 
that you need to comment every single line. Comments like:

x = x+1  # increment x by 1

are just noise. But this is a good comment:

x = x+1  # add correction for wind-chill factor.

> self.m = 39.96*1.66*10**(-27)                    #m is the mass
> of a single trapped ion in the chain

I wonder why you calculate m with an explicit exponentiation operator, 
instead of a floating point exponent?

The difference is very small, so it probably doesn't matter:

>>> 39.96*1.66*10**(-27)
>>> 39.96*1.66e-27

Haha, you are right. Thats just because I am a physicist and used to write
it down that way. I changed that!

>          for i in range(len(x)):
>              for j in range(len(x)):
>                  if j >i:
>                      Vx = Vx + C * (abs(x[i]-x[j]))**(-1)    #then we
> add the coulomb interaction

You can simplify that nested loop and addition:

for i in range(len(x)):
    for j in range(i+1, len(x)):
        Vx += C * (abs(x[i]-x[j]))**(-1)  # add the coulomb interaction

The function range takes up to three integer arguments:

range(start, end, step)

where start and step are optional, defaulting to 0 and 1 respectively. 
By starting the inner loop at i+1, you guarantee that j is always > i 
and therefore you can skip the test.

The other change is that instead of 

Vx = Vx + something

you can write:

Vx += something

Thanks. I got it. In the beginning it is just hard to see every
simplification. But I understand what you were suggesting and I corrected
it. Sometimes a program evolves also physically. So I added the while loop
because I found out later that I need it. 

Steven D'Aprano
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:

View this message in context: http://old.nabble.com/-Tutor--problems-with-numdifftools-tp30034615p30060567.html
Sent from the Python - tutor mailing list archive at Nabble.com.

More information about the Tutor mailing list