On 10/13/06, Stefan van der Walt <st...@su...> wrote:
>
> Hi all,
>
> I've noticed that 'astype' always forces a copy. Is this
> behaviour intended? It seems to conflict with 'asarray', that
> tries to avoid a copy.
>
> For example, when wrapping code in ctypes, the following snippet
> would have been useful:
>
> def foo(x):
> # ensure x is an array of the right type
> x = N.ascontiguousarray(x).astype(N.intc)
>
> but that will cause a copy, so you'll have to do
>
> def foo(x):
> try:
> x = N.ascontiguousarray(x,N.intc)
> except:
> x = N.ascontiguousarray(x).astype(N.intc)
This seems to work now:
In [21]: a
Out[21]:
array([[ 1., 2.],
[ 3., 4.]])
In [22]: ascontiguousarray(a, dtype=int)
Out[22]:
array([[1, 2],
[3, 4]])
Chuck
|