Re: Vectors

2011-04-20 Thread RJB
On Apr 20, 6:43 am, Andreas Tawn  wrote:
> > Algis Kabaila  writes:
>
> > > Are there any modules for vector algebra (three dimensional
> > > vectors, vector addition, subtraction, multiplication [scalar
> > > and vector]. Could you give me a reference to such module?
>
> > NumPy has array (and matrix) types with support for these basic
> > operations you mention. See the tutorial athttp://numpy.scipy.org/
>
> You might also want to considerhttp://code.google.com/p/pyeuclid/
>
> Cheers,
>
> Drea

Pyeuclid docs don't mention dot or cross products.
RJB

-- 
http://mail.python.org/mailman/listinfo/python-list


Faster Recursive Fibonacci Numbers

2011-05-17 Thread RJB
I noticed some discussion of recursion. the trick is to find a
formula where the arguments are divided, not decremented.
I've had a "divide-and-conquer" recursion for the Fibonacci numbers
for a couple of years in C++ but just for fun rewrote it
in Python.  It was easy.  Enjoy.  And tell me how I can improve it!

def fibo(n):
"""A Faster recursive Fibonaci function
Use a formula from Knuth Vol 1 page 80, section 1.2.8:
   If F[n] is the n'th Fibonaci number then
   F[n+m] = F[m]*F[n+1] + F[m-1]*F[n].
  First set m = n+1
   F[ 2*n+1 ] = F[n+1]**2 + F[n]*2.

  Then put m = n in Knuth's formula,
   F[ 2*n ] = F[n]*F[n+1] + F[n-1]* F[n],
   and replace F[n+1] by F[n]+F[n-1],
   F[ 2*n ] = F[n]*(F[n] + 2*F[n-1]).
"""
if n<=0:
return 0
elif n<=2:
return 1
elif n%2==0:
half=n//2
f1=fibo(half)
f2=fibo(half-1)
return f1*(f1+2*f2)
else:
nearhalf=(n-1)//2
f1=fibo(nearhalf+1)
    f2=fibo(nearhalf)
return f1*f1 + f2*f2


RJB the Lurker
http://www.csci.csusb.edu/dick/cs320/lab/10.html


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Faster Recursive Fibonacci Numbers

2011-05-18 Thread RJB
On May 17, 9:36 am, rusi  wrote:
> On May 17, 8:50 pm, RJB  wrote:
>
>
>
>
>
> > I noticed some discussion of recursion. the trick is to find a
> > formula where the arguments are divided, not decremented.
> > I've had a "divide-and-conquer" recursion for the Fibonacci numbers
> > for a couple of years in C++ but just for fun rewrote it
> > in Python.  It was easy.  Enjoy.  And tell me how I can improve it!
>
> > def fibo(n):
> >         """A Faster recursive Fibonaci function
> > Use a formula from Knuth Vol 1 page 80, section 1.2.8:
> >            If F[n] is the n'th Fibonaci number then
> >                    F[n+m] = F[m]*F[n+1] + F[m-1]*F[n].
> >   First set m = n+1
> >    F[ 2*n+1 ] = F[n+1]**2 + F[n]*2.
>
> >   Then put m = n in Knuth's formula,
> >            F[ 2*n ] = F[n]*F[n+1] + F[n-1]* F[n],
> >    and replace F[n+1] by F[n]+F[n-1],
> >            F[ 2*n ] = F[n]*(F[n] + 2*F[n-1]).
> > """
> >         if n<=0:
> >                 return 0
> >         elif n<=2:
> >                 return 1
> >         elif n%2==0:
> >                 half=n//2
> >                 f1=fibo(half)
> >                 f2=fibo(half-1)
> >                 return f1*(f1+2*f2)
> >         else:
> >                 nearhalf=(n-1)//2
> >                 f1=fibo(nearhalf+1)
> >                 f2=fibo(nearhalf)
> >                 return f1*f1 + f2*f2
>
> > RJB the Lurkerhttp://www.csci.csusb.edu/dick/cs320/lab/10.html
>
> -
> Its an interesting problem and you are 75% there.
> You see the halving gives you logarithmic behavior and the double
> calls give exponential behavior.
>
> So how to get rid of double calls?  Its quite simple: Just define your
> function in terms of return pairs of adjacent pairs ie (fib(n), fib(n
> +1)) for some n rather then a single number fib(n)
>
> Here's a straightforward linear function:
>
> def fp(n):  #fibpair
>     if n==1:
>         return (1,1)
>     else:
>         a,b = fp(n-1)
>         return (b, a+b)
>
> def fib(n):
>     a,b = fp(n)
>     return a
>
> ---
> Now use this (pairing) idea with your (halving) identities and you
> should get a logarithmic algo.
>
> [If you cant do it ask again but yes its fun to work out so do
> try :-) ]

Thank you!  Very cool and clear.  I
hoped that there was something that Python made natural I couldn't see
after 50 years in other languages.

I'd like to work on combining both approaches.  It may take a while...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python without a tty

2011-09-30 Thread RJB
On Sep 29, 3:52 am, Steven D'Aprano  wrote:
> Alain Ketterlin wrote:
> > Steven D'Aprano  writes:
>
> >> I have a Python script which I would like to test without a tty attached
> >> to the process. I could run it as a cron job, but is there an easier way?
>
> >> I am running Linux.
>
> > Isn't os.setsid() what you're looking for? It makes the calling process
> > have no controlling terminal. There's also a user command called setsid
> > that should have the same effect.
>
> It doesn't appear so to me.
>
> [steve@sylar ~]$ tty
> /dev/pts/16
> [steve@sylar ~]$ setsid tty
> /dev/pts/16
>
> [steve@sylar ~]$ python -c "import sys,os; print 
> os.isatty(sys.stdout.fileno())"
> True
> [steve@sylar ~]$ setsid python -c "import sys,os; print 
> os.isatty(sys.stdout.fileno())"
> True
>
> If I run the same Python command (without the setsid) as a cron job, I
> get False emailed to me. That's the effect I'm looking for.
>
> --
> Steven

You could try the old UNIX "nohup ... &" technique for running a
process in the background (the &) with no HangUP if you log out:

$ nohup python -c "import sys,os; print
os.isatty(sys.stdout.fileno())" &
appending output to nohup.out
$ cat nohup.out
False

But that is over kill I guess.

One worrying detail the definition of a running process in UNIX
implies is that it has standard input/output files open.
You'd be wise to make sure that they are connected to things that are
safe /dev/null.

Even so /dev/tty can be opened any way...

Hope this helps.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Raw Unicode docstring

2010-11-17 Thread RJB
On Nov 16, 1:56 pm, Boštjan Mejak  wrote:
> Hello,
>
> how does one write a raw unicode docstring? If I have backslashes in
> the docstring, I must tuck an 'r' in front of it, like this:
> r"""This is a raw docstring."""
>
> If I have foreign letters in the docstring, I must tuck a 'u' in front
> of it, like this:
> u"""This is a Unicode docstring."""
>
> What if I have foreign characters *and* backslashes in my docstring?
> How to write the docstring then?
> ru"""My raw unicode docstring."""
> or
> ur"""My unicode docstring."""
>
> Please answer my question, although it may sound like a noobish one. Thanks.

Check out
  http://cse.csusb.edu/dick/samples/python.syntax.html#stringprefix
which lists alternate string prefixes.

Does any bodyy know if "ur" and "UR" mean the same thing?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's the precision of fractions.Fraction?

2010-11-19 Thread RJB
Does Fractions remove common factors the way it should?

If it does and you want to find the closest fraction with a smaller
denominator i think tou'll need some number theory and continued
fractions.

RJB
On Nov 18, 8:26 pm, Steven D'Aprano  wrote:
> On Thu, 18 Nov 2010 20:08:00 +0100, Stefan Sonnenberg-Carstens wrote:
> > If you need it really *precise*, cast your Fractions into type Decimal:
>
> It's actually the opposite. Decimal has many advantages, but being
> "really precise" is not one of them. It suffers the exact same issues re
> precision and round-off as binary floating point.
>
> It is easy to demonstrate that there are numbers which cannot be
> represented precisely as Decimals no matter how many decimal places you
> use, but can be represented exactly as Fractions. Fraction can exactly
> represent every Decimal, but Decimal cannot represent exactly every
> Fraction.
>
> We're not talking about weird edge cases either, but simple numbers that
> you're likely to come across every day:
>
> >>> from decimal import Decimal
> >>> one_ninth = Decimal(1)/Decimal(9)
> >>> two_thirds = Decimal(2)/Decimal(3)
> >>> one_ninth*6 == two_thirds
>
> False
>
> >>> from fractions import Fraction
> >>> one_ninth = Fraction(1, 9)
> >>> two_thirds = Fraction(2, 3)
> >>> one_ninth*6 == two_thirds
>
> True
>
> Still not convinced?
>
> >>> f = Fraction(1) + Fraction(1, 10**10)
> >>> f != 1
>
> True
>
> (which is virtually instantaneous, by the way)
>
> compared to the *much* slower:
>
> >>> d = Decimal(1) + Decimal(1)/Decimal(10**10)
> >>> d != 1
>
> False
>
> Yes, I could try to set the Decimal context to 100,000 decimal places --
> and just as easily defeat it again by adding one more to the exponent.
>
> In my opinion, the Fraction module is one of the least appreciated and
> underused modules in the standard library -- and I include myself in
> that. It really is a joy, and I don't use it anywhere near enough.
>
> --
> Steven

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Easy function, please help.

2011-02-09 Thread RJB
On Feb 8, 11:08 pm, Paul Rudin  wrote:

> It works because 0 tests false and because integer division yields
> integers... eventually you'll get something like 1/10 giving 0.
>
> It's not necessarily a good thing to rely on. For example if you try it
> after "from __future__ import division" - or in python 3 - you'll get a
> float as the result of the division and it won't test False.

What operator should I use if I want integer division?
Ada and Pascal used "div" if I recall rightly.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another related OO Python ?

2011-02-16 Thread RJB
On Feb 16, 12:48 am, Eric Brunel 
wrote:
> In article ,
>  Doug Epling  wrote:
>
> > hey, does anyone find the UML useful during Python development of larger
> > projects?
>
> Well, UML being very Java/C++ oriented, I found out that Python idioms
> were really difficult to represent in the diagrams. So I'm using it to a
> very small extent and for documentation only, just to give an idea about
> how classes are organized. For the rest, and IMHO, it's really too
> impractical to be of any use.

Which of the 13 diagrams have tried and rejected?-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another related OO Python ?

2011-02-17 Thread RJB
On Feb 16, 6:21 am, Eric Brunel 
wrote:
> In article
> <6849fd3f-5116-4b35-b274-dc76ae39f...@a11g2000pro.googlegroups.com>,
>
>
>
>
>
>  RJB  wrote:
> > On Feb 16, 12:48 am, Eric Brunel 
> > wrote:
> > > In article ,
> > >  Doug Epling  wrote:
>
> > > > hey, does anyone find the UML useful during Python development of larger
> > > > projects?
>
> > > Well, UML being very Java/C++ oriented, I found out that Python idioms
> > > were really difficult to represent in the diagrams. So I'm using it to a
> > > very small extent and for documentation only, just to give an idea about
> > > how classes are organized. For the rest, and IMHO, it's really too
> > > impractical to be of any use.
>
> > Which of the 13 diagrams have tried and rejected?-)
>
> Diagrams that aren't too bound to the language like e.g the deployment
> diagram can still be used, of course. I was mainly talking about the
> class diagram, which is still the central point of a model. But I even
> found sequence diagrams quite hard to write for Python, unless they are
> very simplistic ones.

Yes. Especially if you draw interactions with a tool and make them
tidy.  I believe in very rough and rapid sketches in pencil or chalk.

Complex code either needs rethinking or in the worst case an activity
diagram, IMHO.

Of course doing the diagrams by hand after the code is OK  How do
you justify the time spent doing it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Help Documenting Python Syntax

2010-11-09 Thread RJB
I have been trying to redevelop a syntax page for Python at
 http://cse.csusb.edu/dick/samples/python.syntax.html

I would appreciate and encouragement or corrections -- because I know
there is at least one gross error in there.

By the way, this is part of a suite of language definitions... going
back to the 1960's

RJBotting
Who was PAR in the early 1980's
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help Documenting Python Syntax

2010-11-10 Thread RJB
On Nov 9, 8:14 am, Grant Edwards  wrote:
> On 2010-11-09, Terry Reedy  wrote:
>
> > On 11/9/2010 10:26 AM, RJB wrote:
> >> I have been trying to redevelop a syntax page for Python at
> >>      http://cse.csusb.edu/dick/samples/python.syntax.html
>
> > Page does not load correctly; had to hit refresh to get entire page.
> > It should specify that this is the syntax for Python 2.5. To me "the
> > Python language" is 3.2.
>
> Nope.  I just checked, and it's 2.6.5:
>
>   $ python
>   Python 2.6.5 (release26-maint, Aug  9 2010, 11:06:44)
>
> ;)
>
> My question is why bother with 2.5?
>
> I would think the logical choices would be 3.2 or 2.7 (or maybe 2.6).
>

I'm working from some grammars I've found on the web plus experiments.
RJB
-- 
http://mail.python.org/mailman/listinfo/python-list