[SciPy-user] using STL in scipy weave inline

Michael ODonnell odonnems@yahoo....
Wed Dec 12 11:02:45 CST 2007


Vincent and Eric thank you for the information. Sorry I took a while to get back to this, but I was side tracked while building another library that I needed for this program. I do have a couple more questions about headers and includes.

I tried to use #include <map> at the beginning of the script originally but when I did this I got a bunch of errors about the <map>. My question: is there a way to list the headers at the beginning of the script rather than passing through the weave.inline code? I am interested in knowing this because I will need to do this for several things and iI would like to minimize the information  in the command line as much as possible.

Secondly, can someone explain the py:: code. I have seen this but I got an error when I try to include it in my code? I am not getting an error with the code Eric attached.

Third, can someone recommend any documentation or books that may help me with the interface between python and c++? For example, understanding the py::. I have not seen a whole lot on line.

Thanks again for the help and the code!!
Michael


----- Original Message ----
From: eric jones <eric@enthought.com>
To: SciPy Users List <scipy-user@scipy.org>
Sent: Monday, December 10, 2007 4:17:06 PM
Subject: Re: [SciPy-user] using STL in scipy weave inline


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
>   



-----Inline Attachment Follows-----

from scipy import weave
from scipy.weave import converters


total = 20.0 # (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
            double fND, fNNW, fWNW, fWSW,fSSW, fSSE, fESE, fENE, fNNE,
 fFlat;
            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
            //std::multimap<double, std::string> map_freq; //Create
 empty map
            py::dict map_freq;
       
            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";
            
            return_val = map_freq;
            """

res = weave.inline(code,['total'], #headers= ['<map>'],
                   compiler='gcc')






      ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20071212/baf6d42c/attachment.html 


More information about the SciPy-user mailing list