[PYTHON MATRIX-SIG] Type Coercion

James Hugunin jjh@Goldilocks.LCS.MIT.EDU
Wed, 7 Feb 96 18:30:50 EST


   From: hinsenk@ere.umontreal.ca (Konrad HINSEN)


      a C plotting array that wants an array of C floats. You might even have
      an array of C floats but in general you don't know it is contiguous. It
      might be a slice of something else, or the result of any number of
      calculations that destroy contiguity (if that is a word!). 

   Believe it or not, it is accepted by "spell" on my system ;-)

   Contiguity might not be an issue when interfacing to Fortran code,
   which can often handle non-contiguous arrays. Anyway, the C-API should
   have the utility functions "copy if not contiguous" and "copy if not
   sufficiently contiguous to be passed to a Fortran function". There
   could be versions that include a cast.

Just to clarify the facts on this issue.

The C-API has the standard function PyArray_ContiguousFromObject which
is the standard method of obtaining an array.

If the object is a contiguous PyArray of the same type you desire, no
memory copying occurs.  Otherwise there is obviously a copying step
and possibly a cast required.

The two other "standard" ways of obtaining an array from a python
object are

PyArray_CopyFromObject which guarantees you get a copy and can do with
the memory area what you please.

PyArray_FromObject which can give you discontiguous arrays if you want
to deal with them (this is what the internal ofuncs use).

So the facts are that there is in fact a cost proportional to the size
of the array when you pass an array of doubles to one expecting
floats.  However, the cast operation is fairly efficient, so I doubt
that this particular case really matters in practice at all.

=================
MATRIX-SIG  - SIG on Matrix Math for Python

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