Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Sturla Molden <sturlamolden <at> yahoo.no>
Subject: Re: From numpy array to C array
Newsgroups: gmane.comp.python.cython.user
Date: Thursday 2nd February 2012 12:25:56 UTC (over 5 years ago)
On 01.02.2012 20:28, Nikolaus Rath wrote:
> Hello,
>
> I have a 2d numpy array, which I would like to pass to a C function
> which expects a double*.


cdef extern void cfoobar(double*)

import numpy as np
cimport numpy as np

def foobar(object array):

    # declare a 2d NumPy array in C order

    np.ndarray[double, ndim=2, mode="c"] x

    # unbox NumPy array into local variable x
    # make sure we have a contiguous array in C order
    # this might produce a temporary copy

    x = np.ascontiguousarray(array, dtype=np.float64)

    # call C function with the address of x[0,0],
    # that is &x[0,0]

    cfoobar(&x[0,0])



The new memoryview syntax could also be used.


Sturla Molden
 
CD: 2ms