How do I use scipy.weave.inline together with external C libraries?

2024/10/3 17:11:29

I am trying to understand weave.inline to wrap C code in my Python programs. The code below simply takes the Numpy array and multiplicates all of its elements by 2.

inl.py

import numpy
import scipy.weavea = numpy.array([1.0, 2.0, 3.0])
N = a.shape[0]print a
code = \"""int i;for(i = 0; i < N; i++){a[i] = a[i] * 2;}"""scipy.weave.inline(code, ['a','N'])
print a

Then I want to carry some functions from inline code to external libraries. Let it be the trivial multiplication by 2. So I create two files:

mult.c

#include "mult.h"float mult(float n)
{return n * 2;
}

mult.h

float inc(float n);

Now I want to use function mult in my inline code. But I don't know how do I link my C files with Python inline code. I tried to compile C files as shared library and pass them as headers and libraries in weave, but that was in vain. Any suggestions?

Answer

I have successfully done this, calling math functions from R via weave.inline() code (under Ubuntu Linux).

First, compile your C functions as a shared library. In my case, I grabbed a recent release of R from CRAN, and did

./configure --enable-R-static-lib --enable-static --with-readline=no
cd src/nmath/standalone/
make

You should now have a file called libRmath.so. If libpath is a string with the directory that holds libRmath.so, you can do something like

code = 'return_val = pbinom(100, 20000, 100./20000., 0, 1);'
support_code = 'extern "C" double pbinom(double x, double n, double p, int lower_tail, int log_p);'
weave.inline(code, support_code=support_code,library_dirs=[libpath], libraries=["Rmath"], runtime_library_dirs=[libpath])

Note a couple things. The header declarations have to go in support_code, not code (I don't know why), and they have to be prefixed with extern "C" because they're C code, not C++ (this is standard). It should be possible to include headers files instead of using support_code (check the docs for weave.inline), but I haven't tried it. The library name is Rmath, but the shared library file is libRmath.so, in the usual Unix convention. And the path to the library is specified twice, once for linking, and once for execution.

Hope this helps!

https://en.xdnf.cn/q/70711.html

Related Q&A

sqlalchemy multiple foreign keys to same table

I have a postgres database that looks something like this:Table "public.entities"Column | Type | Modifiers ---------------+---…

Django - Return a file from Root folder via a URL

I purchased a SSL cert online and now ind the mid of verifying my host. How it works is:It gives me a file I have to make that file accessible through a specific URL on my host. If the content of the f…

Flask deployement on lighttpd and raspberry pi

Im trying to deploy a hello flask app to a raspberry pi using lighttpd fastCGI.I followed the instructions on the http://flask.pocoo.org/docs/0.10/deploying/fastcgi/ to the best of my abilityHere is my…

Django admin asks for login after every click

Im working on a Django app hosted on Heroku. Im able to login to the admin with my username, password. But on every single click (or on each click after a few seconds) it redirects me to the login page…

Change numerical Data to Categorical Data - Pandas [duplicate]

This question already has answers here:How to create new values in a pandas dataframe column based on values from another column(2 answers)Closed 6 years ago.I have a pandas dataframe which has a numer…

Why is dataclasses.astuple returning a deepcopy of class attributes?

In the code below the astuple function is carrying out a deep copy of a class attribute of the dataclass. Why is it not producing the same result as the function my_tuple? import copy import dataclass…

customize dateutil.parser century inference logic

I am working on old text files with 2-digit years where the default century logic in dateutil.parser doesnt seem to work well. For example, the attack on Pearl Harbor was not on dparser.parse("12…

How can I check a Python unicode string to see that it *actually* is proper Unicode?

So I have this page:http://hub.iis.sinica.edu.tw/cytoHubba/Apparently its all kinds of messed up, as it gets decoded properly but when I try to save it in postgres I get:DatabaseError: invalid byte seq…

Test assertions for tuples with floats

I have a function that returns a tuple that, among others, contains a float value. Usually I use assertAlmostEquals to compare those, but this does not work with tuples. Also, the tuple contains other …

Django: Assigning ForeignKey - Unable to get repr for class

I ask this question here because, in my searches, this error has been generally related to queries rather than ForeignKey assignment.The error I am getting occurs in a method of a model. Here is the co…