Re: math module for Decimals

2008-12-29 Thread Raymond L. Buvel
Since the interest is more in extended precision than in decimal 
representation, there is another module that may be of interest.


http://calcrpnpy.sourceforge.net/clnum.html

It interfaces to the Class Library for Numbers (CLN) library to provide 
both arbitrary precision floating point and complex floating point 
numbers and the usual math functions.


While I am the author of this module, I agree with Mark that a module 
based on MPFR would be better since you have better control over 
precision and rounding.


I have looked at Sage (which uses MPFR) but it is a huge integrated 
package so you can't just import what you need into one of your usual 
Python scripts.


I wrote the clnum module mainly to support arbitrary precision in an RPN 
calculator available from the same SourceForge project.  However, it 
also works nicely as a stand-alone module.


At this time, there is no Windows installer available for Python 2.6 
because I don't use Windows at home and the person who normally builds 
the installer for me is no longer interested.  If someone wants to 
follow the published instructions and send me the resulting installer, I 
will put it up on SourceForge.


Ray

Jerry Carl wrote:

 1. mpmath?
 2. sympy?
 3. Sage?

Haven't tried those, i guess i have some studying to do.

  x=Decimal.__mod__(x,Decimal('2')*pi())

  Works fine for what i need, but i am sure it's not the right way to do
  it.

 I don't know of any better way to deal with large arguments.
 The main problem is that the reduction step can introduce fairly
 large errors:  for example, if you're using a value of pi
 that's accurate to 10**-20, say, then reducing something of
 magnitude 10**5*pi will give a result with error of around
 10**-15.  As far as I know, this problem is essentially
 unavoidable, and it's the reason why implementing sin for inputs
 like 10**9 isn't feasible.

Good point. No tool will work in all parts of the universe (which is
especially true for the universal ski wax).

Let me check the 3 modules you listed above!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using fractions instead of floats

2007-10-03 Thread Raymond L. Buvel
Neil Cerutti wrote:

 Another guess could be that real numbers being closed under the
 four arithmetic operations, there is no danger to accidentally
 step into complex numbers. OTOH floats and rationals are two
 (conflicting) ways of extending integers.
 
 You would have to adopt a few simple conversion rules, a la
 Scheme. Inexact numbers are contagious, for example. It was
 pretty shocking for a Scheme programmer to see the gmpy package
 break that rule. ;)

There is another package that supports rationals.

http://pypi.python.org/pypi/clnum/1.2

One of the reasons I created this package is that I didn't like
implicit conversion of inexact numbers to exact numbers.  Instead
clnum either raises an exception (with Python floats and complex)
or converts the rational to a compatible inexact type (with mpf
and cmpf).

Other reasons for creating this package are gmpy doesn't support
complex numbers or the functions in the math and cmath modules.

However, gmpy does have many capabilities that are not in clnum so
you need to choose based on the type of problem you want to solve.

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


[ANN] clnum-1.4 Class Library For Numbers Python Binding

2006-11-19 Thread Raymond L. Buvel
The clnum package adds rational numbers and arbitrary precision floating
point numbers in real and complex form to Python. Also provides
arbitrary precision floating point replacements for the functions in the
math and cmath standard library modules.

Home page: http://calcrpnpy.sourceforge.net/clnum.html

Changes in 1.4

* Fixed anomaly where an exact zero was converted using default
precision in multiply and divide routines.

* Added function to approximate a number as a rational with the size of
the numerator and denominator constrained.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] ratfun-2.4 Polynomials and Rational Functions

2006-11-19 Thread Raymond L. Buvel
The ratfun module provides classes for defining polynomial and rational
function (ratio of two polynomials) objects. These objects can be used
in arithmetic expressions and evaluated at a particular point.

Home page:  http://calcrpnpy.sourceforge.net/ratfun.html

Note: If you are using rpncalc-1.2 or later, this module is already
included.  This release is for folks who don't want rpncalc.

Changes in 2.4

* Updated the included clnum package.

* Added uniqueRoots to handle polynomials with multiple roots.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] rpncalc-2.5 RPN Calculator for Python

2006-11-19 Thread Raymond L. Buvel
The rpncalc package adds an interactive Reverse Polish Notation (RPN)
interpreter to Python.  This interpreter allows the use of Python as
an RPN calculator.  You can easily switch between the RPN interpreter
and the standard Python interpreter.

Home page:  http://calcrpnpy.sourceforge.net/

Changes in 2.5

* Update the included clnum package.

* Added ratapx and unique_roots commands.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] clnum-1.3 Class Library For Numbers Python Binding

2006-08-19 Thread Raymond L. Buvel
The clnum package adds rational numbers and arbitrary precision floating
point numbers in real and complex form to Python. Also provides
arbitrary precision floating point replacements for the functions in the
math and cmath standard library modules.

Home page: http://calcrpnpy.sourceforge.net/clnum.html

Changes in 1.3

* Added combinatorial functions.

* Improved the performance of converting very large Python longs.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] ratfun-2.3 Polynomials and Rational Functions

2006-08-19 Thread Raymond L. Buvel
The ratfun module provides classes for defining polynomial and rational
function (ratio of two polynomials) objects. These objects can be used
in arithmetic expressions and evaluated at a particular point.

Home page:  http://calcrpnpy.sourceforge.net/ratfun.html

Note: If you are using rpncalc-1.2 or later, this module is already
included.  This release is for folks who don't want rpncalc.

Changes in 2.3

* Update the included clnum package.

* Improved the formatting of the string representation of a polynomial.

* Polynomial and rational function evaluation now works with array inputs.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] rpncalc-2.4 RPN Calculator for Python

2006-08-19 Thread Raymond L. Buvel
The rpncalc package adds an interactive Reverse Polish Notation (RPN)
interpreter to Python.  This interpreter allows the use of Python as
an RPN calculator.  You can easily switch between the RPN interpreter
and the standard Python interpreter.

Home page:  http://calcrpnpy.sourceforge.net/

Changes in 2.4

* Update the included clnum package.

* Added combinatorial functions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ratfun-2.3 Polynomials and Rational Functions

2006-08-19 Thread Raymond L. Buvel
Bas wrote:
 Are there any differences between this module and the one already
 present in numpy?
 
 http://www.scipy.org/doc/numpy_api_docs/numpy.lib.polynomial.html
 
 Cheers,
 Bas
 

Yes, there are quite a few.  This module uses a multi-precision library
(clnum) to make the calculations more precise.  This makes the root
finder significantly more precise.  There is an example in the user
manual that shows the difference in performance between Numeric
(essentially the same code as in numpy) and the ratfun root finder on an
ill-conditioned polynomial.

If you choose to use exact coefficients (integers and rationals) then
all calculations except for root finding are exact.

Unlike the numpy implementation, arithmetic (+-*/**) uses the standard
Python operators instead of requiring you to call functions.

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


[ANN] clnum-1.2.1 Class Library For Numbers Python Binding

2006-06-11 Thread Raymond L. Buvel
The clnum package adds rational numbers and arbitrary precision floating
point numbers in real and complex form to Python. Also provides
arbitrary precision floating point replacements for the functions in the
math and cmath standard library modules.

Home page: http://calcrpnpy.sourceforge.net/clnum.html

Changes in 1.2.1

Updated the unit test so it works on both 32 and 64 bit platforms.  Only
the source package was updated since there are no other changes.
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations.html


[ANN] clnum-1.2.1 Class Library For Numbers Python Binding

2006-06-11 Thread Raymond L. Buvel
The clnum package adds rational numbers and arbitrary precision floating
point numbers in real and complex form to Python. Also provides
arbitrary precision floating point replacements for the functions in the
math and cmath standard library modules.

Home page: http://calcrpnpy.sourceforge.net/clnum.html

Changes in 1.2.1

Updated the unit test so it works on both 32 and 64 bit platforms.  Only
the source package was updated since there are no other changes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: math.pow(x,y)

2006-06-11 Thread Raymond L. Buvel
Felipe Almeida Lessa wrote:
 Em Dom, 2006-06-11 às 11:19 -0700, fl1p-fl0p escreveu:
 import math
 math.pow(34564323, 456356)

 will give math range error.

 how can i force python to process huge integers without math range
 error? Any modules i can use possibly?
 
 34564323**456356 ?
 

I just tried this and it is taking an extremely long time even on a fast
machine with 4 Gb of RAM.  Killed it after a couple of minutes.  This
sort of calculation can be done with extended precision floating point
(as long as you don't need an exact answer).  For example (using
defaults on a 64-bit machine),

 from clnum import mpf
 mpf(34564323)**456356
mpf('1.39518106833639480699862472257296396643e3440298',36)

compute time is about 160 microseconds.

For more information see

http://calcrpnpy.sourceforge.net/clnumManual.html

For calculations involving large powers, you may still be better off
using logarithms.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: math.pow(x,y)

2006-06-11 Thread Raymond L. Buvel
K.S.Sreeram wrote:
 Raymond L. Buvel wrote:
 I just tried this and it is taking an extremely long time even on a fast
 machine with 4 Gb of RAM.  Killed it after a couple of minutes.
 
 Thats odd.
 34564323**456356 completed on my laptop in 28 seconds.
 [Python 2.4.3, Celeron-M 1.3GHz, WinXP], and max memory consumption
 during the whole process was about 11megs.
 
 What python version are you using?
 
 Regards
 Sreeram
 

Sorry, I tripped over the display problem that Tim Peters has repeatedly
explained on this news group (decimal algorithm is quadratic in the
number of digits)  :-(

On a 64-bit 2.2 GHz Opteron running Debian stable, Python 2.3 took 26
seconds and Python 2.4 took 11 seconds.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Decimal and Exponentiation

2006-05-20 Thread Raymond L. Buvel
elventear wrote:
 Hi,
 
 I am the in the need to do some numerical calculations that involve
 real numbers that are larger than what the native float can handle.
 
 I've tried to use Decimal, but I've found one main obstacle that I
 don't know how to sort. I need to do exponentiation with real
 exponents, but it seems that Decimal does not support non integer
 exponents.
 
 I would appreciate if anyone could recommend a solution for this
 problem.
 
 Thank you.
 
The clnum module has arbitrary precision floating point and complex
numbers with all of the standard math functions.  For example, the cube
root of 2 can be computed to 40 decimal places with the following.

 from clnum import mpf,mpq
 mpf(2,40)**mpq(1,3)
mpf('1.259921049894873164767210607278228350570251464701',46)

For more information see

http://calcrpnpy.sourceforge.net/clnumManual.html

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


Re: Decimal and Exponentiation

2006-05-20 Thread Raymond L. Buvel
Tim Peters wrote:
snip

 The GNU GMP library (for which Python bindings are available) also
 supports big floats, but their power operation is also restricted to
 integer powers and/or exact roots.  This can be painful even to try;
 e.g.,
 
 from gmpy import mpf
 mpf(1e1) ** mpf(3.01)
 
 consumed well over a minute of CPU time (on a 3.4 GHz box) before dying
 with
 
ValueError: mpq.pow fractional exponent, inexact-root
 
snip

The clnum module handles this calculation very quickly:

 from clnum import mpf
 mpf(1e1) ** mpf(3.01)
mpf('9.99932861e30099',26)
 x=_
 x ** (1/mpf(3.01))
mpf('9.99953924e',26)

See http://calcrpnpy.sourceforge.net/clnumManual.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Decimal and Exponentiation

2006-05-20 Thread Raymond L. Buvel
Tim Peters wrote:
 [Raymond L. Buvel, on
http://calcrpnpy.sourceforge.net/clnumManual.html
 ]
 
 The clnum module handles this calculation very quickly:

  from clnum import mpf
  mpf(1e1) ** mpf(3.01)
 mpf('9.99932861e30099',26)
 
 
 That's probably good enough for the OP's needs -- thanks!
 
 OTOH, it's not good enough for the decimal module:
 
(10**1)**3.01 =
10**(1*3.01) =
10**30100
 
 exactly, and the proposed IBM standard for decimal arithmetic requires
 error  1 ULP (which implies that if the mathematical (infinite
 precision) result is exactly representable, then that's the result
 you have to get).  It would take some analysis to figure out how much
 of clnum's error is due to using binary floats instead of decimal, and
 how much due to its pow implementation.

Indeed, it is not clear where the error is comming from especially since
you can increase the precision of the intermediate calculation and get
the exact result.

 mpf(mpf(1e1,30) ** mpf(3.01,30), 20)
mpf('1.0e30100',26)

Is this the kind of thing you will need to do in the Decimal module to
meet the specification?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: installing numpy

2006-05-10 Thread Raymond L. Buvel
Gary Wessle wrote:
 Robert Kern [EMAIL PROTECTED] writes:
 
 
Raymond L. Buvel wrote:


Since you are a new Linux user, you should definitely follow Robert's
advice about building as an ordinary user separately from the install.
I sometimes take a shortcut and just do the install as user root.
However, I then wind up cleaning out the build directory as user root
(not a very safe thing to do).

For small, pure Python packages, that may be fine. numpy's build is 
complicated
enough that you really, *really* want to build as a regular user.

-- 
Robert Kern
 
 
 I have read as much as I can form the python installation manual.
 as a regular user, I created a personal configuration file here
 
 
 :~$ cat .pydistutils.cfg 
 [install]
 prefix=/usr/local
 
 
 now to build as a regular user, I am getting some errors
 
 
 $ cd numpy-0.9.6
 $ python setup.py build
 Running from numpy source directory.
 Warning: not existing path in numpy/distutils: site.cfg
 F2PY Version 2_2236
 Traceback (most recent call last):
   File setup.py, line 76, in ?
 setup_package()
   File setup.py, line 63, in setup_package
 config.add_subpackage('numpy')
   File /home/fred/numpy-0.9.6/numpy/distutils/misc_util.py, line 592, in 
 add_s   ubpackage
 config_list = self.get_subpackage(subpackage_name,subpackage_path)
   File /home/fred/numpy-0.9.6/numpy/distutils/misc_util.py, line 582, in 
 get_s   ubpackage
 subpackage_path)
   File /home/fred/numpy-0.9.6/numpy/distutils/misc_util.py, line 539, in 
 _get_   
 configuration_from_setup_py
 config = setup_module.configuration(*args)
   File /home/fred/numpy-0.9.6/numpy/setup.py, line 10, in configuration
 config.add_subpackage('core')
   File /home/fred/numpy-0.9.6/numpy/distutils/misc_util.py, line 592, in 
 add_s   ubpackage
 config_list = self.get_subpackage(subpackage_name,subpackage_path)
   File /home/fred/numpy-0.9.6/numpy/distutils/misc_util.py, line 582, in 
 get_s   ubpackage
 subpackage_path)
   File /home/fred/numpy-0.9.6/numpy/distutils/misc_util.py, line 539, in 
 _get_   
 configuration_from_setup_py
 config = setup_module.configuration(*args)
   File numpy/core/setup.py, line 11, in configuration
 from numpy.distutils.system_info import get_info, default_lib_dirs
   File /home/fred/numpy-0.9.6/numpy/distutils/system_info.py, line 151, in ?
 so_ext = get_config_vars('SO')[0] or ''
   File /usr/lib/python2.4/distutils/sysconfig.py, line 488, in 
 get_config_vars
 func()
   File /usr/lib/python2.4/distutils/sysconfig.py, line 358, in _init_posix
 raise DistutilsPlatformError(my_msg)
 distutils.errors.DistutilsPlatformError: invalid Python installation: unable 
 toopen 
 /usr/lib/python2.4/config/Makefile (No such file or directory)

I think this is another example where you don't have the -dev package
you need installed on your system.  In order to build just about
anything that requires header files on a Debian system, you need to
install the -dev packages.  Debian usually packages things into a
package for regular users and another package of the same name with a
-dev suffix for developers.  In this case, it looks like distutils is
complaining about a missing Makefile.  But this is just the start of the
problems.  Checking on my system, it looks like you need to install the
python2.4-dev package.  Note that installing python-dev won't work in
your case since that is for the default Python which is still Python2.3
in Debian testing.

Using NumPy as your first introduction to building Python extension
modules is a real challenge.

Good luck!
Ray Buvel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: installing numpy

2006-05-09 Thread Raymond L. Buvel
Gary Wessle wrote:
 Hi
 
 I am trying to install NumPy in my debian/testing linux
 2.6.15-1-686. 
 
snip

When installing from source on a Debian system, you want the installed
package to wind up in /usr/local/lib/python2.x/site-packages (where x
represents the version of Python you are running the installer from).
This allows you to keep it separate from the apt managed directories and
allows for easy removal/upgrade.  So the command you want to execute
from root is

python setup.py install --prefix=/usr/local

By the way, to get NymPy to use the high-performance libraries, you must
install these libraries and the associated -dev packages before running
the Python install.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: installing numpy

2006-05-09 Thread Raymond L. Buvel
Robert Kern wrote:
 Gary Wessle wrote:
 
Raymond L. Buvel [EMAIL PROTECTED] writes:
 
 
When installing from source on a Debian system, you want the installed
package to wind up in /usr/local/lib/python2.x/site-packages (where x
represents the version of Python you are running the installer from).
This allows you to keep it separate from the apt managed directories and
allows for easy removal/upgrade.  So the command you want to execute

from root is

python setup.py install --prefix=/usr/local

sorry if this is boring since I am not a seasoned Linux user.

setup.py isn't located at the root, do you mean, execute the command above
from root, as to do this
:~$ cd /
:/$ python setup.py install --prefix=/usr/local
or 
:/$ python home/fred/numpy-0.9.6/setup.py install --pref...
or AS root
:/# python setup.py install --prefix=/usr/local
or 
:/# python home/fred/numpy-0.9.6/setup.py install --pref...
 
 
 I think he meant as the root user. You will probably want to build numpy as 
 a
 regular user and then only install as the root user. You will probably want to
 use sudo(1) to gain root privileges. You can read the sudo man-page for more
 information on how to do that.
 
 ~$ cd numpy-0.9.6
 ~/numpy-0.9.6$ python setup.py build
 ~/numpy-0.9.6$ sudo python setup.py install --prefix=/usr/local
 
 However, instead of setting --prefix every time you execute setup.py for every
 Python package, it will be easier for you to create the file 
 ~/.pydistutils.cfg
 with the contents
 
 [install]
 prefix=/usr/local
 
 See http://docs.python.org/inst/config-syntax.html for more information.
 
 Then, you can just do
 
 ~$ cd numpy-0.9.6
 ~/numpy-0.9.6$ python setup.py build
 ~/numpy-0.9.6$ sudo python setup.py install
 
 
By the way, to get NymPy to use the high-performance libraries, you must
install these libraries and the associated -dev packages before running
the Python install.

I wish to know the debian names for those packages, my first guess
would be refblas3 under testing since blas is not available for
debian/testing, there is also
refblas3-dev  Basic Linear Algebra Subroutines 3, static library 
which I don't have installed.

would refblas3 be all what NymPy need to the high-performance?
 
 
 No. refblas3 provides the reference (unoptimized) implementation of the BLAS. 
 In
 Ubuntu (a Debian-based distribution) the package that you would want is
 atlas3-base-dev. It should have a similar name in your version of Debian
 (possibly atlas-dev or atlas3-dev or some other variant).
 
 If you need more help, you will probably get more focused help on the
 numpy-discussion mailing list.
 
   http://lists.sourceforge.net/lists/listinfo/numpy-discussion
 

Since you are a new Linux user, you should definitely follow Robert's
advice about building as an ordinary user separately from the install.
I sometimes take a shortcut and just do the install as user root.
However, I then wind up cleaning out the build directory as user root
(not a very safe thing to do).

The Debian/testing package you want to install is indeed
atlas3-base-dev.  This package contains the header files needed to build
against the optimized blas library.  Note that the dependancies listed
in this package will cause the library package to be installed as well.

There are packages that are optimized for the extended instruction sets
available on some processors.  If you want to try and install one of
these instead of atlas3-base, just search the package list for anything
with atlas3 in the name.  Then carefully read the package descriptions
to find one that is optimized for your hardware.  Make sure you install
the -dev package or you will wind up using the unoptimized default in NumPy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: printing list

2006-05-07 Thread Raymond L. Buvel
compboy wrote:
 How do you print elements of the list in one line?
 
 alist = [1, 2, 5, 10, 15]
 
 so it will be like this:
 1, 2, 5, 10, 15
 
 because if I use this code
 
 for i in alist:
 print i
 
 the result would be like this
 
 1
 2
 5
 10
 15
 
 Thanks.
 

There are a number of ways to do it but if you want a one-liner:

print repr(alist)[1:-1]

will meet your spec.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] clnum-1.2 Class Library For Numbers Python Binding

2006-05-06 Thread Raymond L. Buvel
The clnum package adds rational numbers and arbitrary precision floating
point numbers in real and complex form to Python. Also provides
arbitrary precision floating point replacements for the functions in the
math and cmath standard library modules.

Home page: http://calcrpnpy.sourceforge.net/clnum.html

Changes in 1.2

* Make compatible with Python 2.5 on 64-bit platforms.

* Windows installer now available.

* Include documentation for building clnum on Windows.
-- 
http://mail.python.org/mailman/listinfo/python-list


Class Library for Numbers now available for Windows

2006-05-06 Thread Raymond L. Buvel
Due to the contribution of Frank Palazzolo, a Windows binary installer
and build instructions are available for the clnum package. This also
makes ratfun and rpncalc usable on the Windows platform.

The clnum package adds rational numbers and arbitrary precision floating
point numbers in real and complex form to Python. It also provides
arbitrary precision floating point replacements for the functions in the
math and cmath standard library modules.

For more information, see

Home page: http://calcrpnpy.sourceforge.net/clnum.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: CRC calculation

2006-05-05 Thread Raymond L. Buvel
[EMAIL PROTECTED] wrote:
 Does anyone know where I can get python code to perform a CRC
 calculation on an IP packet?
 
Check out http://crcmod.sourceforge.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] crcmod-1.3 CRC Generator

2006-04-23 Thread Raymond L. Buvel
Crcmod is a Python package for creating functions computing the Cyclic
Redundancy Check (CRC). Any generating polynomial producing 8, 16, 32,
or 64 bit CRCs is allowed. Generated functions can be used in Python or
C/C++ source code can be generated.

Home page: http://crcmod.sourceforge.net/

Changes in 1.3

* Make compatible with Python 2.5 on 64-bit platforms.
* Improve the install procedure.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python C/API - *arg,**kwds variable argumnents

2005-12-14 Thread Raymond L. Buvel
[EMAIL PROTECTED] wrote:
 I am writing a C extension with python 2.3.5 and need constructs
 similar to python
func(*args, **kwds)
 What's a neat way to do that?
 I found pyrex has a __Pyx_GetStarArgs -
 is there something I'm missing from the regular C/API maybe using one
 of the PyArg_Parse.. calls ?
 
 Thanks,
 M.
 

It looks like the PyArg_ParseTupleAndKeywords API call is what you are
looking for.  Documentation is

http://python.org/doc/2.4.2/ext/parseTupleAndKeywords.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: binascii.crc32 results not matching

2005-12-10 Thread Raymond L. Buvel
Peter Hansen wrote:
 Larry Bates wrote:
 
 I'm trying to get the results of binascii.crc32
 to match the results of another utility that produces
 32 bit unsigned CRCs.  
 
 
 What other utility?  As Tim says, there are many CRC32s... the
 background notes on this one happen to stumble out at the top of the
 list in response to googling for zip file crc32 checksum polynomial,
 though I'm sure there are easier ways.  The binascii docs say its CRC32
 is compatible with the Zip file checksum, but they don't describe it
 further.
 
 Generally CRCs are described in terms of their polynomial, though just
 quoting that isn't sufficient to describe their behaviour, but if you
 happen to know the polynomial for your utility, someone else can
 probably point you to a more appropriate routine, or perhaps explain
 what you were doing wrong if the binascii one is actually the right one..
 
 -Peter
 

Check out the unit test in the following.

http://sourceforge.net/projects/crcmod/

I went to a lot of trouble to get the results to match the results of
binascii.crc32.  As you will see, there are a couple of extra operations
 even after you get the polynomial and bit ordering correct.

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


Re: binascii.crc32 results not matching

2005-12-10 Thread Raymond L. Buvel
Tim Peters wrote:
 [Raymond L. Buvel]
 
Check out the unit test in the following.

http://sourceforge.net/projects/crcmod/
 
 
 Cool!
 
 
I went to a lot of trouble to get the results to match the results of
binascii.crc32.  As you will see, there are a couple of extra operations
even after you get the polynomial and bit ordering correct.
 
 
 Nevertheless, the purpose of binascii.crc32 is to compute exactly the
 same result as most zip programs give.  All the details (including
 what look to you like extra operations ;-)) were specified by RFC
 1952 (the GZIP file format specification).  As a result,
 binascii.crc32 matches, e.g., the CRCs reported by WinZip on Windows,
 and gives the same results as zlib.crc32 (as implemented by the zlib
 developers).

Since there are probably others following this thread, it should be
pointed out that the specification of those extra operations is to
avoid some pathalogical conditions that you can get with a simplistic
CRC operation.  For example, using 0 as the starting value will give a
value of zero for an arbitrary string of zeros.  Consequently, a file
starting with a string of zeros will have the same CRC as one with the
zeros stripped off.  While starting with 0x will give a non-zero
value.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: binascii.crc32 results not matching

2005-12-10 Thread Raymond L. Buvel
Larry Bates wrote:
snip a lot of code

Looking over the code, it seems very inefficient and hard to understand.
 You really should check out the following.

http://sourceforge.net/projects/crcmod/

It will allow you to generate efficient CRC functions for use in Python
and in C or C++.  The only thing you need to input is the polynomial,
the bit ordering, and the starting value.  The unit test gives a number
of polynomials including the one you are using which is:

polynomial: 0x104C11DB7, bit reverse algorithm

If you are just looking for a utility on Linux to do nightly checking of
files, I strongly recommend md5sum.  My timing tests indicate that the
MD5 algorithm is comparable or slightly faster than a 32-bit CRC and
certainly faster than the code you are trying to port.  It also has the
advantage of being a standard Linux command so you don't need to code
anything.

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


[ANN] rpncalc-2.2 RPN Calculator for Python

2005-12-04 Thread Raymond L. Buvel
The rpncalc package adds an interactive Reverse Polish Notation (RPN)
interpreter to Python.  This interpreter allows the use of Python as
an RPN calculator.  You can easily switch between the RPN interpreter
and the standard Python interpreter.

Home page:  http://calcrpnpy.sourceforge.net/

Changes in 2.2

* Display only 12 digits of a mpf or cmpf value.

* Added stats function.

* Added chebyshev module.

* Added solve command.

* Simplified installing user functions into the calculator.

* Added on-line help.
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations.html


[ANN] rpncalc-2.2 RPN Calculator for Python

2005-12-03 Thread Raymond L. Buvel
The rpncalc package adds an interactive Reverse Polish Notation (RPN)
interpreter to Python.  This interpreter allows the use of Python as
an RPN calculator.  You can easily switch between the RPN interpreter
and the standard Python interpreter.

Home page:  http://calcrpnpy.sourceforge.net/

Changes in 2.2

* Display only 12 digits of a mpf or cmpf value.

* Added stats function.

* Added chebyshev module.

* Added solve command.

* Simplified installing user functions into the calculator.

* Added on-line help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: gmpy/decimal interoperation

2005-11-14 Thread Raymond L. Buvel
Alex Martelli wrote:
 As things stand now (gmpy 1.01), an instance d of decimal.Decimal cannot
 transparently become an instance of any of gmpy.{mpz, mpq, mpf}, nor
 vice versa (the conversions are all possible, but a bit laborious, e.g.
 by explicitly going through string-forms).
 
 I'm thinking about possible ways to fix this, in whole or in part, but,
 before I spend more time on this, I was wondering if there's anybody
 else who'd be interested -- if so, maybe we can discuss which
 conversions should happen implicitly (e.g., if you try to sum a Decimal
 and an mpz, maybe the latter should implicitly become a Decimal, just
 like an int would, so that the result is a Decimal) and which ones
 should only happen on explicit request (e.g., gmpy.mpf(d) should produce
 an mpf instance, just as calling gmpy.mpf on an int instance would).
 
 
 Alex

This is a bit off topic but I would like to know how you would go about
doing an implicit operation with an mpz and Decimal becoming a Decimal.
 I would like to use something like this to make the float and complex
types more interoperable with mpq and cmpq in my clnum module
(http://calcrpnpy.sourceforge.net/clnumManual.html).  In those cases, I
would like the mpq to degrade to float and the cmpq to degrade to
complex (right now they just raise exceptions).  Seems like you need to
either modify the target type to recognize the new one or the code has
to get very complex to handle all the combinations.

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


Re: any python module to calculate sin, cos, arctan?

2005-11-08 Thread Raymond L. Buvel
Shi Mu wrote:
 any python module to calculate sin, cos, arctan?

The other answers in this thread point you to the standard modules.  If
you need arbitrary precision floating point versions of these functions
check out:

http://calcrpnpy.sourceforge.net/clnumManual.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: CRC16

2005-09-23 Thread Raymond L. Buvel
Tuvas wrote:
 Anyone know a module that does CRC16 for Python? I have an aplication
 that I need to run it, and am not having alot of sucess. I have a
 program in C that uses a CRC16 according to CCITT standards, but need
 to get a program that tests it with python as well. Thanks!
 

Try this one

http://crcmod.sourceforge.net/

It will do what you want as long as you know what polynomial to use and
what type of algorithm.  I think you want the CCITT polynomial with a
bit-reverse algorithm.  If that is the case, the following will generate
the Python function you want (assumes you want to seed the CRC
calculation with the starting value of 0x).

import crcmod
crc16 = crcmod.mkCrcFun(0x11021, 0x, True)

Now compute the crc of a data string as

crc = crc16(data)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 2.3 - 2.4: long int too large to convert to int

2005-09-16 Thread Raymond L. Buvel
Grant Edwards wrote:
 I give up, how do I make this not fail under 2.4?
 
   fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pack(HBB,0x1c,0x00,0x00))
 
 I get an OverflowError: long int too large to convert to int
 
 ioctl() is expecting a 32-bit integer value, and 0xc0047a80 has
 the high-order bit set.  I'm assuming Python thinks it's a
 signed value.  How do I tell Python that 0xc0047a80 is an
 unsigned 32-bit value?
   
Everyone seems to be suggesting that the fix for the problem is to
somehow cobble together some way of forcing an unsigned integer into a
signed integer (what you would do with a cast in C).  However, if I
understand the long-int consolidation this is not consistent with that
effort.

As far as I can tell, the underlying problem is that the C routine
fcntl.ioctl is expecting a signed integer.  These are the kinds of
problems that need to be fixed.  The function should be asking for an
unsigned integer.  This is possible with the C API at least since Python
2.3.  Without these fixes, the long-int consolidation is going to
continue to produce frustration.  There are many functions in the
standard library that you would expect to take unsigned integers but
actually specify signed integers.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] rpncalc-2.1 RPN Calculator for Python

2005-09-11 Thread Raymond L. Buvel
The rpncalc package adds an interactive Reverse Polish Notation (RPN)
interpreter to Python.  This interpreter allows the use of Python as
an RPN calculator.  You can easily switch between the RPN interpreter
and the standard Python interpreter.

Home page:  http://calcrpnpy.sourceforge.net/

Changes in 2.1

Upgraded the polynomial root finder so that it handles ill-conditioned
polynomials better.
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations.html


[ANN] ratfun-2.1 Polynomials and Rational Functions

2005-09-11 Thread Raymond L. Buvel
The ratfun module provides classes for defining polynomial and rational
function (ratio of two polynomials) objects. These objects can be used
in arithmetic expressions and evaluated at a particular point.

Home page:  http://calcrpnpy.sourceforge.net/ratfun.html

Note: If you are using rpncalc-1.2 or later, this module is already
included.  This release is for folks who don't want rpncalc.

Changes in 2.1

Upgraded the polynomial root finder so that it handles ill-conditioned
polynomials better.
-- 
http://mail.python.org/mailman/listinfo/python-announce-list

Support the Python Software Foundation:
http://www.python.org/psf/donations.html


Find roots of ill-conditioned polynomials

2005-09-10 Thread Raymond L. Buvel
If you are using the root finder in Numeric, and are having problems,
check out the root finder in the ratfun module.  My testing indicates
that it will give the exact roots of a Wilkinson polynomial of degree
100.  For more information see

http://calcrpnpy.sourceforge.net/ratfun.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Lossless Number Conversion

2005-08-29 Thread Raymond L. Buvel
Chris Spencer wrote:
 Is there any library for Python that implements a kind of universal
 number object. Something that, if you divide two integers, generates a
 ratio instead of a float, or if you take the square root of a negative,
 generates a complex number instead of raising an exception? Lisp has
 something like this, and it makes number crunching much more convenient.
 
 Chris

If you are on any Unix-like platform, you might want to check out clnum.
 It works like the gmpy package but has complex versions of rationals
and arbitrary precision floats.

http://calcrpnpy.sourceforge.net/clnum.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Class Library for Numbers

2005-08-20 Thread Raymond L. Buvel
I have just released a new module that interfaces the Class Library for
Numbers (CLN) to Python.  The CLN library is a C++ library that provides
rational and arbitrary precision floating point numbers in real and
complex form.  The clnum module exposes these types to Python and also
provides arbitrary precision replacements for the standard math
functions in real and complex forms.

For more information see

http://calcrpnpy.sourceforge.net/clnum.html

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


Windows Porting Help Requested

2005-07-23 Thread Raymond L. Buvel
I am preparing to release an extension module that interfaces Python to
the Class Library for Numbers (http://www.ginac.de/CLN/).  This module
will provide Python types for arbitrary precision floating point
numbers, rational numbers, and their complex counterparts.  The module
also includes most of the functions found in the Python math and cmath
libraries.

This module will be useful in applications where gmpy
(http://gmpy.sourceforge.net/) doesn't have enough capability (e.g.
extended precision trig, complex numbers, etc.).

I would like to make this module available to Windows users but have
neither the development environment nor the interest to get the module
to build for the standard Python distribution.  I am requesting the help
of a developer who is familiar with building extension modules and
understands C++ porting issues.

I don't think my code is the issue, since it builds with distutils.
However, it depends on two libraries (CLN and GMP) that look like they
have porting issues to a Windows platform.  I suspect they can be made
to work with some of the GNU tools on Windows.  The challenge is making
an extension module that contains all the required parts for the
standard Python distribution.

If you are a developer with the required skills and the time, I would
appreciate your help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python -i (interactive environment)

2005-03-06 Thread Raymond L. Buvel
I posted the following a while back.  I think this is what you are 
looking for.

This can be done fairly easily by creating a module (lets call it
interactive) with the following code in it.
---
import sys,os
def debug_exception(type, value, traceback):
 # Restore redirected standard I/O
 sys.stdin = sys.__stdin__
 sys.stdout = sys.__stdout__
 sys.stderr = sys.__stderr__
 # Kick the interpreter into interactive mode and call the original
 # exception handler.
 os.environ['PYTHONINSPECT'] = '1'
 sys.__excepthook__(type, value, traceback)
sys.excepthook = debug_exception
---
Now if you import this module and raise an unhandled exception, you will
be in interactive mode.  In other words, I think the following script
does what you are asking for.
---
import interactive
raise RuntimeError('Interactive Mode')
---
This also has the advantage that if there are no unhandled exceptions in
your script, the script runs and terminates normally.
Enjoy,
Ray Buvel
Joe wrote:
Hi Pierre,
Thanks for the reply, but  I am not on Unix and it even if I was that 
solution it does not achieve the desired results.

I want the script to decide whether to fall back to the interactive prompt. 
You solution makes it ALWAYS fall back to the interactive prompt.

I want to do something like:
try:
# execute code
except MyExceptionOccurred, except_msg:
# fall to interactive prompt.
In other words I want to have my program to determine whether the 
interactive prompt to be displayed or not.

Thanks!
Pierre Barbier de Reuille [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

Very simple is you're on UNIX ...
You juste have to put at the beginnin of your file :
#!/usr/bin/python -i
And it juste does what you want :)
Pierre
Joe a écrit :
When you run python -i scriptname.py after the script completes you 
left at the interactive command prompt.

Is there a way to have this occur from a running program?
In other words can I just run scriptname.py (NOT python -i scriptname.py) 
and inside of scriptname.py I decide that I want to fall back to the 
interactive prompt?

I've searched and so far the only thing I've come up with is to use pdb, 
but that is not exactly the same as the interactive prompt.

Is there any way to do it that I have missed?
Thanks. 


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


Re: any Python equivalent of Math::Polynomial::Solve?

2005-02-27 Thread Raymond L. Buvel
Alex Renelt wrote:
Alex Renelt wrote:
in addition:
I'm writing a class for polynomial manipulation. The generalization of 
the above code is:

definitions:
1.) p = array([a_0, a_i, ..., a_n]) represents your polynomial
P(x) = \sum _{i=0} ^n a_i x^i
2.) deg(p) is its degree
3.) monic(p) makes P monic, i.e. monic(p) = p / p[:-1]
then you get:
from numarray import *
import numarray.linear_algebra as la
def roots(p):
p = monic(p); n = deg(p)
M = asarray(zeros((n,n)), typecode = 'f8')
# or 'c16' if you need complex coefficients
M[:-1,1:] = identity(n-1)
M[-1,:] = -p[:-1]
return la.eigenvalues(M)
Alex

uhh, I made a mistake:
under definitions, 3.)
its monic(p) = p / p[-1] of course
Alex
Alex,
If you want a class for polynomial manipulation, you should check out my 
ratfun module.

http://calcrpnpy.sourceforge.net/ratfun.html
Ray
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question on python 2.4 and tkinter

2005-02-19 Thread Raymond L. Buvel
David Joyner wrote:
Hi:
I'm trying to compile python 2.4 with tkinter.
(I'm trying to write a gui interface which
calls a program called GAP - I'm hoping to use
subprocess, a python 2.4 module, since I was getting
deadlocks using popen).
The instructions at the python web site said basically
to edit the /Modules/Setup file and recompile.
The edits consist of determining with location of various
Tcl/Tk libraries, header files, etc. I was wondering if
anyone could help out with how to determine these.
I have a suse 9.1 box and a new debian laptop, both of which
I would like to configure with python 2.4 and tkinter.
- David Joyner
The problem is most likely missing header files for the Tcl/Tk 
libraries.  Most Linux distributions (including SuSE) package the 
required files separately from the libraries.  In Yast, look for 
packages with the same name and a -dev suffix.  Once you have installed 
these, tkinter will be automatically built without any need to edit 
/Modules/Setup.

If you are running Debian testing or unstable, you just need to install 
the python2.4 and python2.4-tk modules.  If you need to compile 
extension modules, you will also need python2.4-dev.

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


[ANN] ratfun-1.0 Polynomials And Rational Functions

2005-01-02 Thread Raymond L. Buvel
The ratfun module provides classes for defining polynomial and rational 
function (ratio of two polynomials) objects. These objects can be used 
in arithmetic expressions and evaluated at a particular point.

Home page:  http://calcrpnpy.sourceforge.net/ratfun.html
Note: If you are using rpncalc-1.2 or later, this module is already 
included.  This release is for folks who don't want rpncalc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: exposing C array to python namespace: NumPy and array module.

2005-01-01 Thread Raymond L. Buvel
Bo Peng wrote:
Dear list,
I am writing a Python extension module that needs a way to expose pieces 
of a big C array to python. Currently, I am using NumPy like the following:

 PyObject* res = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, 
char*buf);

Users will get a Numeric Array object and can change its values (and 
actually change the underlying C array).

This works fine. However, when I deliver my module, I find NumPy is 
unnecessarily large for this simple task. As a matter of fact, I had to 
 build from source NumPy, ATLAS etc on Solaris, Linux, Mac and if a 
user would like to use my module, he has to do the same thing!

Python's array module is built-in, easy to use, but *without* a 
FromLenAndData function! Even the buffer interface provides only 'get 
buffer' but no 'set buffer' functions. Could anyone tell me how I can 
create an array object from existing data? Some vague ideas might be 
used: 1. PyCObject (I do not really understand the manual), 2. copy and 
modify arraymodule.c to my project (doable at all? License issue?) 3. 
Create an array object and hack it. (no api to do this.)

I would strongly suggest an arraymodule.h with Array_FromLenAndData.
Many thanks in advance.
Bo
I don't know how much this will help but when I am faced with a problem 
like this, I use Pyrex and look at the generated code.  All you need to 
do in Pyrex is import the array module and create your array like you 
would in Python.  To get the data into the array you will need to use 
the buffer interface and fill it in from your C code.
--
http://mail.python.org/mailman/listinfo/python-list


Re: A rational proposal

2004-12-18 Thread Raymond L. Buvel
Mike Meyer wrote:
PEP: XXX
Title: A rational number module for Python
snip
I think it is a good idea to have rationals as part of the standard 
distribution but why not base this on the gmpy module 
(https://sourceforge.net/projects/gmpy)?  That module already provides 
good performance.  However, it does a few things that may not be good ideas.

1. Floats are converted to rationals.  I think your proposal of rational 
to float is better.

2. Fails with a TypeError when used with a complex.  Again Your proposal 
provides a better solution.

3. Fractional powers fail with a ValueError if the root is not exact. 
You do not address this in your proposal.  Could silently convert to 
float in this case but is it better to force the user to be explicit and 
use the float() operation?

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