Re: [Tutor] Hi there, have a question for a side project in physics.....

2017-12-25 Thread Steven D'Aprano
On Mon, Dec 25, 2017 at 09:45:35AM +, Alan Gauld via Tutor wrote:
> On 25/12/17 09:08, Siddharth Sehgal wrote:
> 
> >physics masters student. I am trying to use the Sellmeier Equation
> 
> >I originally state them as floats. However such a process apparently  > 
> >cannot be done with "floats" like these.
> 
> It can be done just with a large error (although as a physics
> grad you will know how to calculate the error I assume)

I don't think the numbers or equation is so ill-conditioned that the 
error will be "large", or at least not larger than the experimental 
uncertainty in the coefficients.

Floating point maths is tricky, but it isn't *that* tricky. Especially 
not for "reasonable" sized numbers, with only nine or ten significant 
figures. This is the huge advantage of IEEE-754 maths using 64-bit 
floats, as Python does: most of the time, the obvious formula "just 
works".



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi there, have a question for a side project in physics.....

2017-12-25 Thread Steven D'Aprano
On Mon, Dec 25, 2017 at 01:08:13PM +0400, Siddharth Sehgal wrote:

> The actual equation is below screen shotted

No it isn't -- either you forgot to attach it, or the mailing list 
removed it.

Do you mean this equation?

https://en.wikipedia.org/wiki/Sellmeier_equation


I suggest you try using Python and compare your results to those from 
here:

http://www.calctool.org/CALC/phys/optics/sellmeier

or from some authoritative source of refractive indexes.


Here is my simple test, for borosilicate glass BK7 using the values from 
Wikipedia. Using the website:

refractive index at 590 nm = 1.51670

Using Python, I get: 1.516698697993053

Here is my code. Feel free to use it for any purpose, no credit required 
(except as needed to meet any academic obligations you may have about 
collaboration and/or plagiarism).


import math

def sellmeier(lambd, B1, B2, B3, C1, C2, C3):
return 1 + B1*f(lambd, C1) + B2*f(lambd, C2) + B3*f(lambd, C3)

def f(x, y):
x2 = x**2
return x2/(x2 - y)


# Coefficients for BK7 (borosilicate crown glass)

result = sellmeier(0.590, # 590nm == 0.590µm
  1.03961212,
  0.231792344,
  1.01046945,
  6.00069867e-3,
  2.00179144e-2,
  103.560653)

print(math.sqrt(result))




-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi there, have a question for a side project in physics.....

2017-12-25 Thread Steven D'Aprano
On Mon, Dec 25, 2017 at 01:08:13PM +0400, Siddharth Sehgal wrote:
> Hi there
> 
> 
> I am a novice python user and am a physics masters student. I am 
> trying to use the Sellmeier Equation to calculate a refractive index. 
> The coefficients of this equation are decimals to a large number of 
> sig figs ( i.e B1 = 1.03961212, B2 = 0.231792344, C1 = 6.00069867×10−3 

That's not really a lot of significant figures. Python floats are C 
64-bit doubles, so they can represent about 15 or 16 significant 
figures. The numbers you show are only 9 or 10.


> ... and so on) in the sellmeier formula there is a lot of fractions, 
> multiplication and squaring of these numbers. I originally state them 
> as floats. However such a process apparently cannot be done with 
> "floats" like these.

What makes you think that you cannot use floats for this?

Of course floating point maths on computers is not the same as real 
arithmetic of the Real numbers in mathematics class, and you may need to 
carefully consider the possible error conditions in your equations, 
round-off error, and so forth, but in general I would expect that simply 
using Python as a calculator will be fine for all but the most precise 
calculations.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi there, have a question for a side project in physics.....

2017-12-25 Thread Alan Gauld via Tutor

On 25/12/17 09:08, Siddharth Sehgal wrote:


physics masters student. I am trying to use the Sellmeier Equation



I originally state them as floats. However such a process apparently  > cannot be done 
with "floats" like these.


It can be done just with a large error (although as a physics
grad you will know how to calculate the error I assume)


What do i do? PLEASE NEED HELP!


There ae several ways and I guess the best will involve using 
SciPy/numpy features.

But since i don't know those I'll suggest the old school way
which is to multiply your numbers up until they become integers
and take advantage of pythons big int feature. You will need
to plug all the multipliers into your formula and work out
the final multiplier - but that is just exponent arithmetic
so should be doable. Finally adjust your answer by the
calculated exponent.

As I say there will probably be better solutions in the
numpy space and hopefully someone else will tell you about
them.


The actual equation is below screen shotted


This list does not permit non-text attachments - the server
throws them away.

Alan G.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Hi there, have a question for a side project in physics.....

2017-12-25 Thread Siddharth Sehgal
Hi there


I am a novice python user and am a physics masters student. I am trying to use 
the Sellmeier Equation to calculate a refractive index. The coefficients of 
this equation are decimals to a large number of sig figs ( i.e B1 = 1.03961212, 
B2 = 0.231792344, C1 = 6.00069867×10−3 ... and so on) in the sellmeier formula 
there is a lot of fractions, multiplication and squaring of these numbers. I 
originally state them as floats. However such a process apparently cannot be 
done with "floats" like these. What do i do? PLEASE NEED HELP!

The actual equation is below screen shotted


Many thanks, I look forward to your response, 

THIS IS NOT HOMEWORK BY THE WAY, I just want to use this program as it saves a 
lot of writing on paper. 


Siddharth Sehgal 
MSc Student in Physics 

SUNY - Stony Brook University 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi there,

2016-01-02 Thread Steven D'Aprano
On Sat, Jan 02, 2016 at 02:45:30PM +0200, yehudak . wrote:
> I'm trying to write a Python 3.5 program to find how many trailing zeros
> are in 100! (factorial of 100).
> I downloaded factorial from Math module, but all my efforts to solve the
> problem failed.
> 
> I know the mathematical way to solve it (resulting in 24), but I want a
> Python solution.

Here are the steps needed:

http://www.purplemath.com/modules/factzero.htm

Try writing some Python code for this, and if you have trouble, show us 
your code and we'll help. But you have to write it first -- we won't do 
your homework for you.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi there,

2016-01-02 Thread Alan Gauld
On 02/01/16 12:45, yehudak . wrote:


> I know the mathematical way to solve it (resulting in 24), but I want a
> Python solution.

Show us your code.

Usually "the mathematical way to do it" works in Python too.
Although there will likely be other ways that may sometimes
run faster or easier to code.

But until we see your code we can't comment on what you
are doing.

One way to try would be continually dividing by 10 until you
get a non-zero remainder. Count the number of divisions
necessary. Hint: The divmod() function may help here.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Hi there,

2016-01-02 Thread yehudak .
I'm trying to write a Python 3.5 program to find how many trailing zeros
are in 100! (factorial of 100).
I downloaded factorial from Math module, but all my efforts to solve the
problem failed.

I know the mathematical way to solve it (resulting in 24), but I want a
Python solution.

Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi there :.)

2010-03-16 Thread Stefan Behnel

Alan Gauld, 15.03.2010 20:28:

 wrote

(apparently python is slow ?!?).


It is all relative. If you want to write fast moving graphics etc then
yes, you probably need C++. For anything else you might find Python is
fast enough.


A good approach tends to be: write it in Python first, benchmark it, find 
out *if* it's too slow. If so, find out *what* exactly is too slow, 
optimise that code, benchmark again. Iterate. If there are still parts that 
are too slow, move the 1-5% of your code that are most time critical into 
Cython. Benchmark, optimise. Iterate. And only *then*, move on to plain 
C/C++ if you can really prove with verified benchmarking numbers that you 
need to. In almost all cases, you don't. In any case, it will safe you all 
of the hassle of writing the entire code in C/C++, which usually safes you 
weeks, months or years of developer time, depending on the complexity of 
your application.


Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Hi there!

2010-03-16 Thread Marco Rompré
Hi! Does anyone of you know where to find all the solutions of Gerard
Swinnen Python tutorial exercises 

In the tutorial we just have the solutions to half of the exercises.

Thank you

-- 
Marc-O. Rompré
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi there :.)

2010-03-15 Thread Alan Gauld
 wrote 


Just introducing myself to say hiHi!


Hi welcome, but please don't include lots of attachments. 
It blows up people's mailboxes and bandwidth allowances.

Better to post them on a website and send a link.


I'm very new to programming, ...
place to start I found out that python and C++ are the usual 
starting place 


Actually there are many: VisualBasic, Lisp/Scheme and Java 
are all common starting points too. And each has its own 
merits depending on what you want to do.


but when I found out that civilisation and eve are programmed 
using python (my favorite type of games), that helped make the 
decision. 


As good a reason as any! :-)

My first aim is to get comfortable with python and move on 
to C++ (apparently python is slow ?!?). 


It is all relative. If you want to write fast moving graphics etc 
then yes, you probably need C++. For anything else you might 
find Python is fast enough.


The problem i'm having with this is with the platform. 
I want the ball to fall if it goes off the edge but be able to 
jump when on the platform. it either will fall off the edge 
but won't jump or it will jump but it won't fall. 
I don't want to change the way the controls are worked 
(with the true/ false).


test.py is the one file that didn't show up (at least for me)
Lots of "demos" but no test.py.

It is probably best if you can create the simplest possible
example that shows the problem rather than expecting folks 
to read through half a dozen fairly big files and try to guess 
what the design looks like etc. (That may have beeen 
what test.py was!)


Try to send some specific code that causes a problem, 
or at least that you think is causing the problem. 
Plus any error messages. It would help to tell us 
which OS and Python version you are using too.


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi there :.)

2010-03-15 Thread Wayne Werner
On Mon, Mar 15, 2010 at 10:23 AM,  wrote:

>  Hey,
> Just introducing myself to say hiHi!
>
> I'm very new to programming, I got interested in it when I decided to have
> a go at html. When looking around for a place to start I found out that
> python and C++ are the usual starting place but when I found out that
> civilisation and eve are programmed using python (my favorite type of
> games), that helped make the decision. so far I've been using Create Your
> Own Games with Python (using python 3.1 and pygame) as a guide and been
> experimenting making snippets of code.
>

Welcome to Python and programming! HTML is what got me started on my path,
too. Pygame is a great package for making games with.


>  My first aim is to get comfortable with python and move on to C++
> (apparently python is slow ?!?). My ultimate aim is to create a couple of
> ideas that I've had floating around for a while.
>

Slow is a relative term. If C++ does something 100x faster than python, and
python can already do it in .5 seconds you won't notice much difference if
you're doing it once. If you're doing 2-d game programming Python is
certainly fast for plenty of stuff. Take a look at the games of pyweek:

http://www.pyweek.org/

Code is available for all the games so you can take a look at how they
program and that might help you with challenges you face.

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor