[SciPy-user] using STL in scipy weave inline

eric jones eric@enthought....
Mon Dec 10 17:17:06 CST 2007


Michael ODonnell wrote:
> OS Microsoft XP SP2 x86
> python 2.5.1
> compiler: mingw
>
> Does anyone have experience using multimap or the standard template 
> library within weave inline? I need to create a map that contains 
> numeric keys which are paired to a string, but I get an error when 
> declaring the multimap function. If i include <map> I get other errors 
> about namespace and if don't then I get an error for not declaring the 
> multimap. Neither method works.
There is no problem using the stl from weave.  In fact, it uses 
std::string internally.

You can use

    headers=["<map>"]

as a keyword to the inline call to get map into the namespace.  Once you 
do that, you'll need to either declare "using namespace std;" at the top 
of your code or use std::multimap and std::string to refer to the 
classes in the stl.  Once you've done that, you'll still have some 
issues as multimap doesn't support the operator [], and your C++ strings 
have single quotes around them instead of double quotes.

I've attached an example for your code that instead works using a 
standard python dictionary (use py::dict within weave).  The output is 
shown here:

In [33]: run we_ex.py
<weave: compiling>

In [34]: res
Out[34]:
{0.050000000000000003: 'fESE',
 0.10000000000000001: 'fSSE',
 0.14999999999999999: 'fFlat',
 0.25: 'fNNE',
 0.40000000000000002: 'fSSW',
 0.5: 'fWNW',
 0.55000000000000004: 'fNNW',
 1.1000000000000001: 'fND',
 1.5: 'fWSW'}

If you need to use the stl for some other reason, then refer to this to 
see how to do it: http://www.sgi.com/tech/stl/Multimap.html

Good luck,
eric
>
> TIA,
> Michael
>
> Example of code:
>
> total = (winx * winy * 1.0) #Total number of cells to evaluate in kernel
>
> code = r"""
>             double ND, NNW, WNW, WSW,SSW, SSE, ESE, ENE, NNE, Flat;
>             ND = 22.0;
>             NNW = 11.0;
>             WNW = 10.0;
>             WSW = 30.0;
>             SSW = 8.0;
>             SSE = 2.0;
>             ESE = 1.0;
>             ENE = 5.0;
>             NNE = 5.0;
>             Flat = 3.0;
>
>             //Calculate the frequency of the counts
>             fND = (ND / total);   
>             fNNW = (NNW / total);  
>             fWNW = (WNW / total); 
>             fWSW = (WSW / total); 
>             fSSW = (SSW / total);  
>             fSSE = (SSE / total);  
>             fESE = (ESE / total); 
>             fENE = (ENE / total); 
>             fNNE = (NNE / total);  
>             fFlat = (Flat / total); 
>        
>             //Create a map for pairs: frequency, string
>             multimap<double, string> map_freq; //Create empty map
>        
>             map_freq[fND) = 'fND';
>             map_freq[fNNW] = 'fNNW';
>             map_freq[fWNW] = 'fWNW';
>             map_freq[fWSW] = 'fWSW';
>             map_freq[fSSW] = 'fSSW';
>             map_freq[fSSE] = 'fSSE';
>             map_freq[fESE] = 'fESE';
>             map_freq[fENE] = 'fENE';
>             map_freq[fNNE] = 'fNNE';
>             map_freq[fFlat] = 'fFlat';
>             """
>
> weave.inline(code,['total'], type_converters=converters.blitz, 
> compiler='gcc')
>
> ------------------------------------------------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try 
> it now. 
> <http://us.rd.yahoo.com/evt=51733/*http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ%20> 
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>   

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: we_ex.py
Url: http://projects.scipy.org/pipermail/scipy-user/attachments/20071210/992ed42f/attachment.pl 


More information about the SciPy-user mailing list