[Numpy-discussion] KeyError with float96 in linalg.py: polyfit

David M. Cooke cookedm at physics.mcmaster.ca
Tue Jul 4 15:09:38 CDT 2006

On Tue, 4 Jul 2006 12:10:18 +0200
Jan-Matthias Braun <jan_braun at gmx.net> wrote:

> Hi all,
> I'm testing some computations with float96 at the moment and right now I
> have problems with polyfit raising a KeyError for the keycode 'g', which is 
> floatxx with xx>64.

Use longdouble instead of float96; it'll make your code portable.

> I am getting a KeyError using polyfit on some float96 values. The used 
> Routines seem to know nothing about this type.
> My main question is: have I missed something? Shouldn't this type be used?
> Below is a more detailed descripton.

polyfit uses LAPACK to do least-squares for the fit, and LAPACK doesn't
handle any real types besides single- and double-precision. And Numpy
(as a design decision) doesn't like casting to a lower precision.

> In file numpy/linalg/linalg.py, the following definitions at lines 26ff
> seem to be the offending ones:
> 	# Helper routines
> 	_array_kind = {'i':0, 'l': 0, 'q': 0, 'f': 0, 'd': 0, 'F': 1, 'D':
> 1} _array_precision = {'i': 1, 'l': 1, 'q': 1, 'f': 0, 'd': 1, 'F': 0, 'D':
> 1} _array_type = [['f', 'd'], ['F', 'D']]
> Here the new typecodes are missing. I tried 
> 	# Helper routines
> 	_array_kind = {'i':0, 'l': 0, 'q': 0, 'f': 0, 'd': 0, 'g': '0',
> 'F': 1, 'D':1, 'G':1}
> 	_array_precision = {'i': 1, 'l': 1, 'q': 1, 'f': 0, 'd': 1, 'g': 1, 
> 			'F': 0, 'D': 1, 'G': 1}
> 	_array_type = [['f', 'd', 'g'], ['F', 'D', 'G']]
> which gets me a step further to a TypeError:
>   File "lib/python2.3/site-packages/numpy/linalg/linalg.py", line 454, in 
> lstsq
>     bstar[:b.shape[0],:n_rhs] = b.copy()
> TypeError: array cannot be safely cast to required type

That would be a better error.

I'm going to leave it like this for now, though. Instead, the linalg module
will be converted to use dtypes.

> (Question: Why only one typecode for a type which varies in bitlength on 
> different platforms? On Opteron CPU's I've seen float128 with 'g'?)

Typecodes are deprecated (they date back to Numeric), so we're not bothering
to add new ones. The equivalent to 'g' is the dtype longdouble.

|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca

More information about the Numpy-discussion mailing list