Re: [Tutor] program hangs in while loop using wx.yield

2010-11-15 Thread Alan Gauld


"Alex Hall"  wrote


Is there a basic tutorial for this sort of thing?


There are a few books on the suvbject but I can't thik of
any titles off the top of my head. And mostly they are
written for C++ rather than Python.

fraction of a second. However, I need to wait for as long as the 
human
takes to make a turn-ending move. Firing ends a turn, while moving 
the

mouse or arrows does not.


Thats OK you just wait till you get a Fire event. Presumably
you have a dedicated "Fire" Button or somesuch?


The computer does not have this problem, but
I still need to know when the computer is done.


What you really need is an indication on the GUI so that
the player knows when the computer is done? All of the
processing of the computers move will be part of the "Fire"
button event handler.

You are right, that was confusing. What I am saying is that, each 
time

a player goes (human or ai) a string gets updated. The opponent can
then examine that string to see what happened, updating the gui (in
the case of the human) accordingly and recording the information for
consideration in future moves (in the case of the ai).


You might find it helpful to write down the system requirements
as a set of "use cases". A use  case is a dialog between user
and system. So your description above would look something like:

1) user clicks cell on system
2) system updates display to indicate hit or miss
3) system displays target cell on GUI
4) system displays whether this is a hit or miss
5) continue at 1

In this example actions 2,3 and 4 can all be part of the same
event handler. Or you could break it into two actions separated
by a timer event. To prevent the user doing anything before the
timer expires simply set a semaphore or disable the UI control.

GUI design often uses a state machine, see the recent thread
on state variables for more info. Keeping the integrity of the
state machine is a large part of successful GUI design.
In complex GUIS I often include a setState function (that
gets called after every event) which simply sets the enabled/disabled
properties for all the controls depending on the current state.
This is usually a big table of states versus controls and I retrieve
the two lists of enabled and disabled controls and iterate over them
setting as appropriate...


Why do you care if the human "takes in the results" - if they want
to trust in randomness surely thats their problem. You as a 
computer

can match them blast for blast...
Part of it is that I hope to eventually add sound, and the other 
part

is that I have always found games that make a move instantly to be a
bit too far from what I am used to. If it is easy to build in a 
delay,

I would rather have it there.


Delays are easier as per my earlier message. Either just add
a sleep(1) to your code or use a timer. I'd suggest the latter,
its more work but it is more flexible and keeps the GUI responsive
where it needs to be (menus, moving, resizeing, etc)

In your case I'd do step 2 first then set a 1 second timer before
doing steps 3 and 4. Disable the fire capability in step 2 and
re-enable it after step 4.

Again: is there a basic tutorial that would explain how control 
works

when dealing with a gui, especially one that is meant to last a long
time but occasionally block input?


You probably can find one on GUI design or event driven programming
but the best advice I can offer is to think about the dialog/use case 
approach.

Every time the user does something you have to think about how
the system will respond and map that to an event handler.

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


[Tutor] Threading and Sockets

2010-11-15 Thread Wayne Werner
Hi,

I'm working on creating a server/client bit of software using threading and
sockets (it's a project so I can't use something like twisted), and I've run
into a slight issue with my server. My server currently looks like this:

class ClientThread(threading.Thread):
def __init__(self, socketdata, server):
threading.Thread.__init__(self)
self.conn = socketdata[0]
self.details = socketdata[1]
self.server = server

def run(self):
''' Method called when the Thread.start() method is called. '''
global CONNCOUNT
CONNCOUNT -= 1
print("Getting data from {0}:{1}: ".format(self.details[0],
 self.details[1]), end='')
data = self.conn.recv(1024)
print(data)
if data == 'quit':
self.server.stop()
self.conn.close()


class Server(threading.Thread):
def __init__(self, port=1500, max_connections=5):
''' Setup the server elements. '''

threading.Thread.__init__(self)
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind(('localhost', 1500))
self.server.listen(5)
self.keeprunning = True

def run(self):
global CONNCOUNT
while self.keeprunning:#CONNCOUNT > 1:
ClientThread(self.server.accept(), self).start()
self.stop()

def stop(self):
''' Stop the server. '''

print("Stopping server... maybe...")
self.keeprunning = False
# Close the socket connection
self.server.close()
print("Server stopped.")

(CONNCOUNT is currently not used - I was using it to stop my server)

My issue is that when I send 'quit' the server does (sort of) close, but (I
think) the self.server.accept() is blocking, so until I try connecting a few
more times, the server doesn't actually finish.

So that's really my only question - how do I make my server completely stop
when I'm done? Pointing me towards documentation would be perfectly fine,
preferably with a hint about which function(s) I should look at.

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


Re: [Tutor] Threading and Sockets

2010-11-15 Thread Wayne Werner
Well, I solved the issue myself

I changed the server class to the following:

class Server(threading.Thread):
def __init__(self, port=1500, max_connections=5):
''' Setup the server elements. '''

threading.Thread.__init__(self)
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.bind(('localhost', 1500))
self.server.listen(5)
self.server.settimeout(5)
self.keeprunning = True

def run(self):
global CONNCOUNT
while self.keeprunning:#CONNCOUNT > 1:
try:
connection = self.server.accept()
ClientThread(connection, self).start()
except socket.timeout:
# Just keep rolling
pass
self.stop()

def stop(self):
''' Stop the server. '''

print("Stopping server... maybe...")
self.keeprunning = False
# Close the socket connection
self.server.close()
print("Server stopped.")


With the timeout it will stop accepting a connection every 5 seconds, and
then try again for another 5 seconds.

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


[Tutor] Math: integers to a fractional power

2010-11-15 Thread Matthew Denaburg
Hi,

OK, so I have a question for you math people: I am trying to solve a 
quartic equation (Ax^4 + Bx^3 + Cx^2 + Dx + E) using Ferrari's Method, which I 
found on Wikipedia at this location.

I'm using Python 3.1.2 (on Mac OS X 10.6, in case that matters).

First, since I don't know Python very well, I was wondering if there is 
an easier way to do this, without having to install any modules. (I can't 
figure out how to put anything on my PYTHONPATH).

If not there is not an easier way without installing a module, could 
someone recommend a module AND give me a link to detailed instructions for how 
to install it? 

I would PERFER to continue using Ferrari's Method, but am not opposed 
to trying something else.

My only problem when using Ferrari's Method was this error:

Traceback (most recent call last):
  File "Problem309.py", line 135, in 
print(main())
  File "Problem309.py", line 127, in main
math.pow(L1, 2)-math.pow(L2, 2))
  File "Problem309.py", line 73, in sQuartic
W = math.pow(alpha + 2 * y, 1/2)
TypeError: can't convert complex to float

Now, your first thought is probably that I have a negative number in 
the square root, or that I should be using math.sqrt. According to this method, 
I need to use cube roots as well, and I wanted to keep my code consistent. I 
also have checked, and the only variable that could be ending up as a complex 
number is the variable y. So, here is the code that assigns y:

u = math.pow(Q, 2)/4 + math.pow(P, 3)/27
if u < 0:
return None
R = -(Q / 2) + math.pow(u, 1/2)
U = R ** (1/3)
y = -(5/6) * alpha + U
if U == 0:
y = y - Q ** (1/3)
elif U != 0:
y = y - P/(3*U)
W = math.pow(alpha + 2 * y, (1/2))

The problem I am having is that, as far as I can tell,  is that U is 
turning into a complex number, which is impossible! You can take the cube root 
of any number, positive or negative!

So I tried in the Python Interpreter, and got this:

>>> -27**(1/3)
-3.0
>>> math.pow(-27, 1/3)
Traceback (most recent call last):
  File "", line 1, in 
math.pow(-27, 1/3)
ValueError: math domain error
>>>

Is there something going on here that I am unaware of?

Please let me know!! Thanks so much!

---Matthew Denaburg




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


Re: [Tutor] Math: integers to a fractional power

2010-11-15 Thread Evert Rol
>   OK, so I have a question for you math people: I am trying to solve a 
> quartic equation (Ax^4 + Bx^3 + Cx^2 + Dx + E) using Ferrari's Method, which 
> I found on Wikipedia at this location.
> 
>   I'm using Python 3.1.2 (on Mac OS X 10.6, in case that matters).
> 
>   First, since I don't know Python very well, I was wondering if there is 
> an easier way to do this, without having to install any modules. (I can't 
> figure out how to put anything on my PYTHONPATH).
> 
>   If not there is not an easier way without installing a module, could 
> someone recommend a module AND give me a link to detailed instructions for 
> how to install it? 
> 
>   I would PERFER to continue using Ferrari's Method, but am not opposed 
> to trying something else.
> 
>   My only problem when using Ferrari's Method was this error:
> 
> Traceback (most recent call last):
>   File "Problem309.py", line 135, in 
> print(main())
>   File "Problem309.py", line 127, in main
> math.pow(L1, 2)-math.pow(L2, 2))
>   File "Problem309.py", line 73, in sQuartic
> W = math.pow(alpha + 2 * y, 1/2)
> TypeError: can't convert complex to float

This may be more a result from the underlying C math library (also the 
exception you got below); I think the functions in that library are called when 
using functions from the math module, and the C library only works for 
non-complex numbers.
You could have a look at the cmath module. However, you won't find a pow() 
function there.

Instead, try using the built-in power operator: **, and use complex():
>>> complex(-27)**1/3.
(-9+0j)
>>> complex(-4)**(1/2.)
(1.2246467991473532e-16+2j)

Note the (classic) inaccuracy problem of floating points: the real part should 
simply be 0.
(Also, create complex numbers with an imaginary part != 0 as:
>>> complex(5, -2)
(5-2j)
)

Would that help you?

Cheers,

  Evert

>   Now, your first thought is probably that I have a negative number in 
> the square root, or that I should be using math.sqrt. According to this 
> method, I need to use cube roots as well, and I wanted to keep my code 
> consistent. I also have checked, and the only variable that could be ending 
> up as a complex number is the variable y. So, here is the code that assigns y:
> 
> u = math.pow(Q, 2)/4 + math.pow(P, 3)/27
> if u < 0:
> return None
> R = -(Q / 2) + math.pow(u, 1/2)
> U = R ** (1/3)
> y = -(5/6) * alpha + U
> if U == 0:
> y = y - Q ** (1/3)
> elif U != 0:
> y = y - P/(3*U)
> W = math.pow(alpha + 2 * y, (1/2))
> 
> 
>   The problem I am having is that, as far as I can tell,  is that U is 
> turning into a complex number, which is impossible! You can take the cube 
> root of any number, positive or negative!
> 
>   So I tried in the Python Interpreter, and got this:
> 
> >>> -27**(1/3)
> -3.0
> >>> math.pow(-27, 1/3)
> Traceback (most recent call last):
>   File "", line 1, in 
> math.pow(-27, 1/3)
> ValueError: math domain error
> 
> >>>
> 
>   Is there something going on here that I am unaware of?
> 
>   Please let me know!! Thanks so much!
> 
> ---Matthew Denaburg
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Math: integers to a fractional power

2010-11-15 Thread Steven D'Aprano

Matthew Denaburg wrote:


-27**(1/3)

-3.0

math.pow(-27, 1/3)

Traceback (most recent call last):
  File "", line 1, in 
math.pow(-27, 1/3)
ValueError: math domain error

Is there something going on here that I am unaware of?


Yes, various things.

The precedence of the exponentiation operator ** is higher than that of 
the negation operator -, so -27**(1/3) is equivalent to:


- (27 ** (1/3))

and there is no attempt to raise a negative number to a float. Thus:

>>> -9**0.5
-3.0

Secondly, there are always N Nth-roots of a number: the familiar real 
root (or roots), and complex roots. For example:


(-1)**2 = 1**2 = 1 so there are two square roots, +1 and -1.

The same holds for higher powers, only the roots can be imaginary or 
complex. (Note: you shouldn't take "real" and "imaginary" in their plain 
English meanings -- imaginary numbers are just as "real" as real 
numbers, it's just a little harder to point to examples.)


Python happens to return a complex root when using the ** operator:

>>> (-27)**(1/3)
(1.5002+2.5980762113533156j)

Note the usual floating point issues: due to rounding, you don't always 
get the *exact* result:


>>> ((-27)**(1/3))**3
(-26.993+1.0220990720455347e-14j)

On the other hand, the pow() function refuses to perform exponentiation 
of a negative integer. I don't know why they have different behaviour -- 
possibly an accident that different people wrote the code for each, or 
possibly there is some deliberate reason for it.


You might find the functions in the cmath (complex math) module useful 
for working with complex numbers.



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


Re: [Tutor] Math: integers to a fractional power

2010-11-15 Thread Dave Angel

On 2:59 PM, Matthew Denaburg wrote:

Hi,

OK, so I have a question for you math people: I am trying to solve a 
quartic equation (Ax^4 + Bx^3 + Cx^2 + Dx + E) using Ferrari's Method, which I 
found on Wikipedia at this location.

I'm using Python 3.1.2 (on Mac OS X 10.6, in case that matters).

First, since I don't know Python very well, I was wondering if there is 
an easier way to do this, without having to install any modules. (I can't 
figure out how to put anything on my PYTHONPATH).

If not there is not an easier way without installing a module, could 
someone recommend a module AND give me a link to detailed instructions for how 
to install it?

I would PERFER to continue using Ferrari's Method, but am not opposed 
to trying something else.

My only problem when using Ferrari's Method was this error:

Traceback (most recent call last):
   File "Problem309.py", line 135, in
 print(main())
   File "Problem309.py", line 127, in main
 math.pow(L1, 2)-math.pow(L2, 2))
   File "Problem309.py", line 73, in sQuartic
 W = math.pow(alpha + 2 * y, 1/2)
TypeError: can't convert complex to float

Now, your first thought is probably that I have a negative number in 
the square root, or that I should be using math.sqrt. According to this method, 
I need to use cube roots as well, and I wanted to keep my code consistent. I 
also have checked, and the only variable that could be ending up as a complex 
number is the variable y. So, here is
You don't have to guess.  Once you come to that conclusion, check it.  
Put a print( y), or perhaps print (repr(y)) and check the value.



The problem I am having is that, as far as I can tell,  is that U is 
turning into a complex number, which is impossible! You can take the cube root 
of any number, positive or negative!



I don't know in particular, but if you were to raise a negative number 
to the . power, the answer would be complex.  Perhaps you're getting 
hit with some roundoff/quantization error.  1/3 cannot be exactlly 
represented, of course.  I'd be curious if that's the situation here. 
 If so, you could finesse the problem with the expression:


  sign *  (abs(x) ** (1.0/3))

where sign is +1 or -1 representing the sign of x.

DaveA

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


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-15 Thread Nithya Nisha
Hi Tom,

Your code is almost correct. Little mistake is there.The random number
generate should be in while loop.

> import random
>
> # set the coin

> headsCount = 0
> tailsCount = 0
> count = 0
>
> # the loop
> while count < 100:   #If
you declare count = 0. The while loop condition should be  less than
100.Else you will get 101 counts.
> *coin = random.randrange(2)*
> if coin == 0:
> headsCount += 1
>else:   #Becase We
already declared randrange(2).So the coin value is 0 or 1.So we can use else
condition.
> tailsCount += 1
> count += 1
>
>
> print "The number of heads was", headsCount
> print "The number of tails was", tailsCount
>
> raw_input("\n\nPress the enter key to exit.")

Regards,
Nithya

_
*
Your Description*:

On Mon, Nov 15, 2010 at 7:19 AM, Thomas C. Hicks  wrote:

> On Sun, 14 Nov 2010 17:16:36 -0500
> Dawn Samson  wrote:
>
> > Greetings,
> >
> > I'm a Python beginner and working my way through Michael Dawson's
> > Python Programming for the Absolute Beginner. I'm stuck in a
> > particular challenge that asks me to write a program that "flips a
> > coin 100 times and then tells you the number of heads and tails."
> > I've been trying to work on this challenge for a while now and can't
> > get it to work (either it has 100 heads or 100 tails). I've been
> > reworking this code many times and currently what I have does not do
> > anything at all at IDLE. Please take a look at my code below:
> >
> > import random
> >
> > # set the coin
> > coin = random.randrange(2)
> > headsCount = 0
> > tailsCount = 0
> > count = 0
> >
> > # the loop
> > while count <= 100:
> > coin
> > if coin == 0:
> > headsCount += 1
> > if coin == 1:
> > tailsCount += 1
> > count += 1
> >
> >
> > print "The number of heads was", heads
> > print "The number of tails was", tails
> >
> > raw_input("\n\nPress the enter key to exit.")
> >
> > Thanks,
> > S. Dawn Samson
>
> >From one beginner to another - it looks to me like you set the value of
> coin once then checked it 100 times.  If you want to reset the value of
> coin maybe it (i.e. setting the value of coin, not just calling
> the value of coin) should be in the loop too?
>
> tom
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-15 Thread Luke Pettit
When I run this code (I'm also a noob) I get this result:-

>>> [evaluate lines 1-22 from untitled-1.py]
The number of heads was 73
The number of tails was 100

Press the enter key to exit.

# Surely, if flipping a single coin 100 times your total number of heads and
tails should add up to 100
# not 173 or am I missing the point?



On 16 November 2010 15:08, Nithya Nisha  wrote:

> Hi Tom,
>
> Your code is almost correct. Little mistake is there.The random number
> generate should be in while loop.
>
> > import random
> >
> > # set the coin
>
> > headsCount = 0
> > tailsCount = 0
> > count = 0
> >
> > # the loop
> > while count < 100:   #If
> you declare count = 0. The while loop condition should be  less than
> 100.Else you will get 101 counts.
> > *coin = random.randrange(2)*
> > if coin == 0:
> > headsCount += 1
> >else:   #Becase We
> already declared randrange(2).So the coin value is 0 or 1.So we can use else
> condition.
> > tailsCount += 1
> > count += 1
> >
> >
> > print "The number of heads was", headsCount
> > print "The number of tails was", tailsCount
> >
> > raw_input("\n\nPress the enter key to exit.")
>
> Regards,
> Nithya
>
>
> _
> *
> Your Description*:
>
> On Mon, Nov 15, 2010 at 7:19 AM, Thomas C. Hicks wrote:
>
>> On Sun, 14 Nov 2010 17:16:36 -0500
>> Dawn Samson  wrote:
>>
>> > Greetings,
>> >
>> > I'm a Python beginner and working my way through Michael Dawson's
>> > Python Programming for the Absolute Beginner. I'm stuck in a
>> > particular challenge that asks me to write a program that "flips a
>> > coin 100 times and then tells you the number of heads and tails."
>> > I've been trying to work on this challenge for a while now and can't
>> > get it to work (either it has 100 heads or 100 tails). I've been
>> > reworking this code many times and currently what I have does not do
>> > anything at all at IDLE. Please take a look at my code below:
>> >
>> > import random
>> >
>> > # set the coin
>> > coin = random.randrange(2)
>> > headsCount = 0
>> > tailsCount = 0
>> > count = 0
>> >
>> > # the loop
>> > while count <= 100:
>> > coin
>> > if coin == 0:
>> > headsCount += 1
>> > if coin == 1:
>> > tailsCount += 1
>> > count += 1
>> >
>> >
>> > print "The number of heads was", heads
>> > print "The number of tails was", tails
>> >
>> > raw_input("\n\nPress the enter key to exit.")
>> >
>> > Thanks,
>> > S. Dawn Samson
>>
>> >From one beginner to another - it looks to me like you set the value of
>> coin once then checked it 100 times.  If you want to reset the value of
>> coin maybe it (i.e. setting the value of coin, not just calling
>> the value of coin) should be in the loop too?
>>
>> tom
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


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


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-15 Thread Nithya Nisha
Hi Tom,

Your code is almost correct. Little mistake is there.The random number
generate should be in while loop.


> import random
>
> # set the coin

> headsCount = 0
> tailsCount = 0
> count = 0
>
> # the loop
> while count < 100:
 #If you declare count = 0. The while loop condition
should be  less than 100.Else you will get 101 counts.
> *coin = random.randrange(2)*

> if coin == 0:
> headsCount += 1
>else:   #Becase We
already declared randrange(2).So the coin value is 0 or 1.So we can use else
condition.
> tailsCount += 1
> count += 1
>
>
> print "The number of heads was", headsCount
> print "The number of tails was", tailsCount

>
> raw_input("\n\nPress the enter key to exit.")

Regards,
Nithya

__
*Your Description*

On Mon, Nov 15, 2010 at 7:19 AM, Thomas C. Hicks  wrote:

> On Sun, 14 Nov 2010 17:16:36 -0500
> Dawn Samson  wrote:
>
> > Greetings,
> >
> > I'm a Python beginner and working my way through Michael Dawson's
> > Python Programming for the Absolute Beginner. I'm stuck in a
> > particular challenge that asks me to write a program that "flips a
> > coin 100 times and then tells you the number of heads and tails."
> > I've been trying to work on this challenge for a while now and can't
> > get it to work (either it has 100 heads or 100 tails). I've been
> > reworking this code many times and currently what I have does not do
> > anything at all at IDLE. Please take a look at my code below:
> >
> > import random
> >
> > # set the coin
> > coin = random.randrange(2)
> > headsCount = 0
> > tailsCount = 0
> > count = 0
> >
> > # the loop
> > while count <= 100:
> > coin
> > if coin == 0:
> > headsCount += 1
> > if coin == 1:
> > tailsCount += 1
> > count += 1
> >
> >
> > print "The number of heads was", heads
> > print "The number of tails was", tails
> >
> > raw_input("\n\nPress the enter key to exit.")
> >
> > Thanks,
> > S. Dawn Samson
>
> >From one beginner to another - it looks to me like you set the value of
> coin once then checked it 100 times.  If you want to reset the value of
> coin maybe it (i.e. setting the value of coin, not just calling
> the value of coin) should be in the loop too?
>
> tom
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] Math: integers to a fractional power (Matthew Denaburg)

2010-11-15 Thread C or L Smith
If you are interested in a symbolic solution or numerical solution without 
missing any of the possible roots (real or imaginary) you could check out 
sympy, a CAS that offers a quartic solution. It uses appropriate 
simplifications besides a general solution to the quartic. The code for this is 
in the polys/polyroots.py file; that code makes some calls to other routines in 
sympy so it's not totally self contained.

It can be downloaded from sympy.org. It can be sandboxed at live.sympy.org (but 
that has an error in the quartic solution right now).  It's a pretty quick 
install.

Here it is in use:

>>> from sympy import solve, Rational, var, nsimplify
>>> var('x') # create a variable
x
>>> solve(x**4 + 3*x**3 - 2*x + x/3 + .7) # note the 0.7 float
...
... error message ending with
...
CoercionFailed: can't convert 2.1 of type RR to ZZ

So either use nsimplify to replace floats with rationals
>>> ans = solve(nsimplify(x**4 + 3*x**3 - 2*x + x/3 + .7, rational=1))
>>> ans[0].n()
-2.74495954970930

Or enter floats as Rationals from the start
>>> ans = solve(x**4 + 3*x**3 - 2*x + x/3 + Rational(7, 10))
>>> ans[0].n()
-2.74495954970930

You can use a symbol for the number that will take on a float
>>> var('a')
a
>>> ans = solve(x**4 + 3*x**3 - 2*x + x/3 + a, x) # now indicate you want to 
>>> solve for x

Substitute 'a' with your desired float:
>>> [w.subs(a, .7).n() for w in ans] # .n() means "give a numerical answer"
[0.423047811447149 - 0.229393202547502*I, -2.74495954970930, 0.423047811447149 
+ 0.229393202547502*I, -1.10113607318500]

The answer is there at index 1.

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


[Tutor] I try to convert a MATLAB program to Python...

2010-11-15 Thread Henri Heinonen
Hi!

Can you, please, try to help me with Python? I try to convert a MATLAB
program to Python.

Here are the MATLAB codes:
http://pastebin.com/MbPZ8Z7X
http://pastebin.com/dDnF5AF2

Here is my Python code:
http://pastebin.com/jCPdLHx7

What is wrong with my Python code? The program doesn't produce quite the
same lambdas as the MATLAB program does.

Thanks for all the help!

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


Re: [Tutor] I try to convert a MATLAB program to Python...

2010-11-15 Thread Henri Heinonen
These are the four (out of eight) first results I get with the MATLAB code:

>> nonhomog(0.2)
nonhomog: using 300 grid points.

BC1_disc_left =

  -4.8436e-12


BC1_disc_right =

  -1.1027e-13


BC2_disc_left =

  -1.1269e-11


BC2_disc_right =

  -1.8657e-13


lambda =

0.9976


V0 =

   79.0637

nonhomog: using 300 grid points.

BC1_disc_left =

   8.5855e-12


BC1_disc_right =

  -1.4955e-13


BC2_disc_left =

  -9.4851e-12


BC2_disc_right =

   3.7053e-12


lambda =

0.9969


V0 =

   79.0637

nonhomog: using 300 grid points.

BC1_disc_left =

  -4.2669e-12


BC1_disc_right =

  -3.8908e-15


BC2_disc_left =

  -1.0330e-10


BC2_disc_right =

   8.6403e-13


lambda =

0.5464


V0 =

   79.0606

nonhomog: using 300 grid points.

BC1_disc_left =

   2.9082e-12


BC1_disc_right =

  -5.5045e-15


BC2_disc_left =

  -8.5076e-13


BC2_disc_right =

   3.7712e-13


lambda =

  -54.3440


V0 =

   78.6880


These are the four results I get with my Python code:

python nonhomog.py
kappa =  15.7079632679
alpha =  0
nu =  0.2
nx_ =  300
BC1_disc_left =  (nan+nanj)
BC1_disc_right =  (nan+nanj)
BC2_disc_left =  (nan+nanj)
BC2_disc_right =  (nan+nanj)
lambda =  -0.774244159818
nonhomog.py:81: DeprecationWarning: integer argument expected, got float
  xx = range(-kappa, h, kappa+1)
kappa =  15.7079632679
alpha =  5.80527619798e-06
nu =  0.2
nx_ =  300
BC1_disc_left =  (nan+nanj)
BC1_disc_right =  (nan+nanj)
BC2_disc_left =  (nan+nanj)
BC2_disc_right =  (nan+nanj)
lambda =  -0.774244180107
kappa =  15.7079632679
alpha =  0.000580527619798
nu =  0.2
nx_ =  300
BC1_disc_left =  (nan+nanj)
BC1_disc_right =  (nan+nanj)
BC2_disc_left =  (nan+nanj)
BC2_disc_right =  (nan+nanj)
lambda =  -0.774246188696
kappa =  15.7079632679
alpha =  0.0580527619798
nu =  0.2
nx_ =  300
BC1_disc_left =  (nan+nanj)
BC1_disc_right =  (nan+nanj)
BC2_disc_left =  (nan+nanj)
BC2_disc_right =  (nan+nanj)
lambda =  -0.774447043715


Some questions:

1. For example, the sign of the lambdas are wrong not to mention that the
values are not even near the corresponding MATLAB values. How to fix these?

2. Also, I get those annoying (nan+nanj) messages for BC1s and BC2s. What's
wrong?

3. There is also this error message: "nonhomog.py:81: DeprecationWarning:
integer argument expected, got float
  xx = range(-kappa, h, kappa+1)". How should I fix that?

Thanks!

Henri.

2010/11/16 Henri Heinonen 

> Hi!
>
> Can you, please, try to help me with Python? I try to convert a MATLAB
> program to Python.
>
> Here are the MATLAB codes:
> http://pastebin.com/MbPZ8Z7X
> http://pastebin.com/dDnF5AF2
>
> Here is my Python code:
> http://pastebin.com/jCPdLHx7
>
> What is wrong with my Python code? The program doesn't produce quite the
> same lambdas as the MATLAB program does.
>
> Thanks for all the help!
>
> Henri.
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] program hangs in while loop using wx.yield

2010-11-15 Thread Terry Carroll

On Sun, 14 Nov 2010, Alex Hall wrote:


Is there a basic tutorial for this sort of thing?


Chapter 3 ("Working in an event-driven environment") of the book "wxPython 
in Action" is a pretty good tutorial on event-driven GUI programming in 
wxPython.  The book in general is pretty good; I no longer buy many 
computer books, but this one was worth it.


If you don't want to buy it, if you're in the U.S., you can go to 
http://www.worldcat.org/oclc/67122432 and plug in your zip code to see if 
a library near you[1] has it.


[1] or a library that has inter-library loan arrangements with a library 
near you.  I'm currently reading "Essential SQLAlchemy," courtesy of the 
San Diego State University library.  My local library (San Jose Public 
Library) borrowed it from SDSU and then lent it to me.  It's kind of cool 
that one library will send a book 400 miles to another, just because I'm 
too cheap to buy a copy and asked for it.


Apologies to anyone at SDSU who's learning SQLAlchemy and wondering why 
they can't find this book in their library.

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


Re: [Tutor] While Loops: Coin Flip Game :p:

2010-11-15 Thread Dave Angel

When I run this code (I'm also a noob) I get this result:-


[evaluate lines 1-22 from untitled-1.py]

The number of heads was 73
The number of tails was 100

Press the enter key to exit.

# Surely, if flipping a single coin 100 times your total number of heads and
tails should add up to 100
# not 173 or am I missing the point?


No, you're missing an indentation.  If you check the code you're 
running, I think you'll find that you didn't unindent the line 
incrementing count.


Of course, it's less error prone to simply use
for count in xrange(100):

instead of while count < 100:

and you wouldn't need to increment count.

DaveA

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