views:

146

answers:

3

Hello,

I am new to c, and the following is giving me some grief:

int i,j,ll,k;
double ddim,ddip,ddjm,ddjp,ddlm,ddlp;
for(i=1; i<(mx-1); i++){
for(j=1; j<(my-1); j++){
 for(ll=1; ll<(mz-1); ll++){

 ddim=0.5*k
 ddip=0.5*k
 ddjm=0.5*k
 ddjp=0.5*k
 ddlm=0.5*k
 ddlp=0.5*k

  Wijl(i,j,ll) =  ((1.0/h_x)*(ddip) \
     ((1.0/h_x)*(ddim))   \
     ((1.0/h_y)*(ddjp))   \
     ((1.0/h_y)*(ddjm))   \
     ((1.0/h_z)*(ddlp))   \
     ((1.0/h_z)*(ddlm)) ; 
          }
     }
}

I then compile this with gcc using python and scipy, passing it everything that is not initialized, but I know the problem is in the 1.0/h_x part of the code. If I compile basic c statements using python/gcc it works, so I am not having a python/gcc issue.

The error I am getting is: "error: ambiguous overload for 'operator/' in '1.0e+0 / h_x'

It seems like it is trying to do assignment overloading, and all I want to do is division!

Any help would be greatly appreciated! :)

Thanks,

Tyler

+1  A: 

I think it's trying to say that it's not clear what type h_x is, so it doesn't know which of the overloaded / operators to use (double/int, double/double, etc). You could try casting it (h_x) to int or double to tell it what version to use.

Andrei Fierbinteanu
That is exactly what the problem is! How do I resolve it?? Is it saying that h_x isn't a double/int/ect??
tylerthemiler
I presume you pass h_x from python? I'm not sure about how you would do this, but shouldn't the C code be inside a function that you call from python, having h_x as parameter (with the type set). I didn't see h_x declared anywhere, so I don't know (like the compiler) what type it should be. In the worst case scenario you should be able to cast it like i suggested in the (edited) answer.
Andrei Fierbinteanu
Andrei Fierbinteanu
So I have it running (or at least that assignment isn't throwing an error and it's compiling)! I am just getting a different python-related error now...If I put a fprint statement in the loop it prints, so it seems like the code is working, but here is how I compile it using pytho/ scipy.code = """ everything in my original post """weave.inline(code,['Wijl','q','diff','mx','my','mz','h_x','h_y','h_z'],type_converters=converters.blitz,compiler='gcc') SystemError: NULL result without error in PyObject_Call
tylerthemiler
Thanks for the help! It turned out to be something completely unrelated to any of that, but they did need to be doubles.
tylerthemiler
A: 

If h_x is float then dividing 1.0 (by default double) by it leaves C wondering whether to do the operation in float or double math.

If you want to do it in floats, change 1.0 to 1.0f; if double, either declare or cast your h_x to double.

If h_x is int (that's what I'm afraid of) you'd probably do well to assign your h_?'s to three corresponding float or double temp variables outside the loop, to save the compiler from (possibly) doing lots of unnecessary int-to-float conversions. As a side effect, this will make your type ambiguity go away.


You could also simplify the code a bit by getting rid of those 1.0's: Instead of multiplying by the reciprocal, you could simply divide those expressions by h_whatever. Especially as the right half of each of those lines is already doing something similar.

Carl Smotricz
C does not "wonder" in mixed-type arithmetical operations. The language specification has precise and explicit rules for what to do in cases like that.
AndreyT
You're right. If the type of h_x was `float` or `double` there shouldn't be any ambiguity about the intended type of the operation. Maybe the issue is being muddled by the linkage from Python.
Carl Smotricz
A: 

I strongly suggest that you carefully peal off all of the utterly redundant parentheses and examine carefully what is left.

For example, this snippet:

((1.0/h_x)*(ddim)*((q(i,j,ll) - q(i-1,j,ll)))/h_x)

boils down to:

ddim * (q(i,j,ll) - q(i-1,j,ll)) / h_x / h_x

Note that the original separation of the two occurrences of / h_x makes one wonder what the original intention was.

John Machin