[SciPy-user] weave question

Christoph Scheit Christoph.Scheit@lstm.uni-erlangen...
Tue Feb 26 08:51:24 CST 2008


Hi everybody,

I have a small sniplet in C++ that I run using weave.inline
in a python-script. Now my problem is, that although it should
be that way, values of mutable arguments passed to the
c++-part are only changed locally, i.e. after the inlined part
are the python-arguments still with the same values... Here
is the sniplet:

    obsvPtsMinRt = ones(nPts, dtype=int)

    code = """
           double rt, rabs;
           int irt, maxrt, minrt;
           int nPts = NobsvPts[0];
           int nElems = NeMidPts[0];
           blitz::TinyVector<double,3> r;

           // initial value
           r(0) = obsvPts(0, 0) - eMidPts(0, 0);
           r(1) = obsvPts(0, 1) - eMidPts(0, 1);
           r(2) = obsvPts(0, 2) - eMidPts(0, 2);
           rabs = sqrt(r[0] * r[0] + r[1] * r[1] + r[2] * r[2]);
           rt = rabs / c0;
           minrt = maxrt = (int) (rt / dt);
           
           // loop over observer pts
           for (int i = 0; i < nPts; i++) {
               for (int j = 0; j < nElems; j++) {
                   r(0) = obsvPts(i, 0) - eMidPts(j, 0);
                   r(1) = obsvPts(i, 1) - eMidPts(j, 1);
                   r(2) = obsvPts(i, 2) - eMidPts(j, 2);
                   rabs = sqrt(r[0] * r[0] + r[1] * r[1] + r[2] * r[2]);
                   rt = rabs / c0;
                   irt = (int) (rt / dt);
                   
                   if (irt > maxrt) {
                       maxrt = irt;
                   } else if (irt < minrt) {
                       minrt = irt;
                   }
               }
               obsvPtsMinRt[i] = minrt;
           }
           obsvPtsMinRt[3] = 10.;
           """
    weave.inline(code,
                ['obsvPtsMinRt', 'obsvPtsMaxRt', 'obsvPts', 'eMidPts', 'dt', 'c0'],
                type_converters=converters.blitz,
                compiler = 'gcc',
                headers=["<ios>", "<ostream>", "<sstream>"])
    
    print obsvPtsMinRt


ok, afer running this code, obsvPtsMinRt should contain values different from
1... shouldn't? For some reason I get [1 1 1 1...] as output.... Does somebody
has an idea what I'm doing wrong?

I tried also a small example, and everything behaved like expected:

def test():
    arr = ones(7)
    print arr
    
    code = """
           int len = Narr[0];
           printf("len: %d\\n", len);
           arr[3] = 5.;
           """
    inline(code, ['arr'], type_converters=converters.blitz)
    print arr

here the I get a five at position 4...

Thank you very much in advance,

christoph


More information about the SciPy-user mailing list