[MATRIX-SIG] Type questions

Janko Hauser jhauser@ifm.uni-kiel.de
Tue, 9 Sep 1997 15:15:34 +0200 (CEST)


I have soem problems to hold the type of my arrays because of the
coercion-rules with regard to simple float numbers.

Example:
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> b=a*1.2  # this changes to a double array because 1.2 is a double 
>>> b
array([[ 0. ,  1.2,  2.4],
       [ 3.6,  4.8,  6. ],
       [ 7.2,  8.4,  9.6]])
>>> b.typecode()
'd'
>>> b=a*array([1.2],'f') # why is this a double?
>>> b.typecode()
'd'
>>> b=a.astype('f')*array([1.2],'f')
>>> b.typecode()
'f'
>>> 


I see no way to say python that a float is not double like the
``12L''. The simple way would be to have something like
asarray(number) (code appended). But isn't it possible to do it
directly in array?

Or is there another way to hold the type of arrays? This can give
memory-problems with huge fields if suddently all fields are coerced
into double.

__Janko

###################### myasarray.py

def masarray(a,typecode=None):
    if typecode == None:
	if ((type(a) == type(1)) or (type(a) == type(1.2))):
	    return array([a])
	else:
	    return array(a, copy=0)
    else:
	if ((type(a) == type(1)) or (type(a) == type(1.2))):
	    return array([a],typecode)
	else:
	    return array(a, typecode, copy=0)



_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________