Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6634369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:55:17+00:00 2026-05-25T22:55:17+00:00

I’ve been playing with Scipy’s inline tool (via weave) for fun, but I’m running

  • 0

I’ve been playing with Scipy’s inline tool (via weave) for fun, but I’m running into some trouble. My C is rusty and I have a feeling that I’m missing something simple.

The function below is designed to take a 3D float32 numpy array. I’m using a massive set of gridded atmospheric data, but this should work with any 3D array. This then takes the grid and gets the arithmetic mean across axis i, for each point j,k (i.e. if i is the time axis, j and k are lat/lon, then I’m averaging across time for each grid point).

I hope that my code is doing this and avoiding numpy NaNs (I believe that isnan() works in inline C/C++…?). But, whether it does this or not, I’m having trouble getting the code to compile without errors such as:

tools.py: In function ‘PyObject* compiled_func(PyObject*, PyObject*)’:
tools.py:93:45: error: invalid types ‘float[int]’ for array subscript
tools.py:95:51: error: invalid types ‘float[int]’ for array subscript
tools.py: In function ‘PyObject* compiled_func(PyObject*, PyObject*)’:
tools.py:93:45: error: invalid types ‘float[int]’ for array subscript
tools.py:95:51: error: invalid types ‘float[int]’ for array subscript

I think I’m declaring and initializing properly, so perhaps something isn’t being passed to weave in the way I think it is? I would love it if someone could help me out with this. Here is the function:

from scipy.weave import inline

def foo(x):
    xi = np.shape(x)[0]
    xj = np.shape(x)[1]
    xk = np.shape(x)[2]
    code = """
           #line 87 "tools.py"
           int n;
           float out[xj][xk];
           for (int k = 0; k < xk; k++) {
               for (int j = 0; j < xj; j++) {
                   n = 0;
                   for (int i = 0; i < xi; i++) {
                       if (!isnan(x[i][j][k])) {
                           n += 1;
                           out[j][k] += x[i][j][k];
                       }
                   }
                   out[j][k] = out[j][k]/n;
               }
           }
           return_val = out;
           """
    awesomeness = inline(code, ['x', 'xi', 'xj', 'xk'], compiler = 'gcc')
    return(awesomeness)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:55:18+00:00Added an answer on May 25, 2026 at 10:55 pm

    you can create the out array in python first, and pass it to C++. In C++ you can get the shape of x by Nx[0], Nx[1], Nx[2]. And you can use macros defined for array to access it’s elements. For example: X3(k,j,i) is the same as x[k,j,i] in python, and OUT2(j,i) is the same as out[j,i] in python. You can view the automatic created C++ code to know what variables and macros you can use for the arrays. To get the folder of C++ code:

    from scipy import weave
    print weave.catalog.default_dir()
    

    My compiler doesn’t support isnan(), so I use tmp==tmp to check it.

    # -*- coding: utf-8 -*-
    import scipy.weave as weave
    import numpy as np
    
    def foo(x):
        out = np.zeros(x.shape[1:])
        code = """
        int i,j,k,n;
        for(i=0;i<Nx[2];i++)
        {
            for(j=0;j<Nx[1];j++)
            {
                n = 0;
                for(k=0;k<Nx[0];k++)
                {
                    double tmp = X3(k,j,i);
                    if(tmp == tmp) // if isnan() is not available
                    {
                        OUT2(j,i) += tmp;
                        n++;
                    }
                }
                OUT2(j,i) /= n;
            }
        }
        """
        weave.inline(code, ["x","out"], headers=["<math.h>"], compiler="gcc")
        return out
    
    np.random.seed(0)    
    x = np.random.rand(3,4,5)
    x[0,0,0] = np.nan
    mx = np.ma.array(x, mask=np.isnan(x))
    avg1 = foo(x)
    avg2 = np.ma.average(mx, axis=0)
    print np.all(avg1 == avg2)
    

    You can also use blitz converter to access array in C++. For the detail, try google: weave.converters.blitz

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.