Have you thought about using cython to work with the numpy C-API (<a href="http://wiki.cython.org/tutorials/numpy#UsingtheNumpyCAPI">http://wiki.cython.org/tutorials/numpy#UsingtheNumpyCAPI</a>)? This will be fast, simple (you can mix and match Python and Cython). <div>
<br></div><div>As for your specific issue: you can simply cast to all the inputs to numpy arrays (using asarray <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html">http://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html</a>) to deal with scalars. This will make sure they get broadcast correctly.<br>
<br><div class="gmail_quote">On Tue, Feb 1, 2011 at 11:22 AM,  <span dir="ltr"><<a href="mailto:DParker@chromalloy.com">DParker@chromalloy.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<font size="2" face="sans-serif">Thanks for the advice. </font>
<br>
<br><font size="2" face="sans-serif">Using Sebastian's advice I was able
to write a version that worked when the input arguments are both arrays
with the same length. The code provided by eat works when t is an array,
but not for an array of far. </font>
<br>
<br><font size="2" face="sans-serif">The numpy.vectorize version works with
any combination of scalar or array input. I still haven't figured out how
to rewrite my function to be as flexible as the numpy.vectorize version
at accepting either scalars or array inputs and properly broadcasting the
scalar arguments to the array arguments. </font>
<br>
<br><font size="2" face="Century Gothic">David Parker <br>
Chromalloy - TDAG</font>
<br>
<br>
<br>
<br><font size="1" color="#5f5f5f" face="sans-serif">From:      
 </font><font size="1" face="sans-serif">eat <<a href="mailto:e.antero.tammi@gmail.com" target="_blank">e.antero.tammi@gmail.com</a>></font>
<br><font size="1" color="#5f5f5f" face="sans-serif">To:      
 </font><font size="1" face="sans-serif">Discussion of Numerical
Python <<a href="mailto:numpy-discussion@scipy.org" target="_blank">numpy-discussion@scipy.org</a>></font>
<br><font size="1" color="#5f5f5f" face="sans-serif">Date:      
 </font><font size="1" face="sans-serif">01/31/2011 11:37 AM</font>
<br><font size="1" color="#5f5f5f" face="sans-serif">Subject:    
   </font><font size="1" face="sans-serif">Re: [Numpy-discussion]
Vectorize or rewrite function to work with array inputs?</font>
<br><font size="1" color="#5f5f5f" face="sans-serif">Sent by:    
   </font><font size="1" face="sans-serif"><a href="mailto:numpy-discussion-bounces@scipy.org" target="_blank">numpy-discussion-bounces@scipy.org</a></font>
<br>
<hr noshade><div><div></div><div class="h5">
<br>
<br>
<br><font size="3">Hi,<br>
</font>
<br><font size="3">On Mon, Jan 31, 2011 at 5:15 PM, <</font><a href="mailto:DParker@chromalloy.com" target="_blank"><font size="3" color="blue"><u>DParker@chromalloy.com</u></font></a><font size="3">>
wrote:</font>
<br><font size="2" face="sans-serif">I have several functions like the example
below that I would like to make compatible with array inputs. The problem
is the conditional statements give a <i>ValueError: The truth value of
an array with more than one element is ambiguous. Use a.any() or a.all()</i>.
I can use numpy.vectorize, but if possible I'd prefer to rewrite the function.
Does anyone have any advice the best way to modify the code to accept array
inputs? Thanks in advance for any assistance.</font>
<br><font size="3"> </font>
<br><font size="3">If I understod your question correctly, then air_gamma
could be coded as:</font>
<br><font size="3">def air_gamma_0(t, far=0.0): <br>
    """ <br>
    Specific heat ratio (gamma) of Air/JP8 <br>
    t - static temperature, Rankine <br>
    [far] - fuel air ratio [- defaults to 0.0 (dry air)]
<br>
    air_gamma - specific heat ratio <br>
    """ <br>
    if far< 0.: <br>
        return NAN <br>
    elif far < 0.005:<br>
        ag= air_gamma_1(t)<br>
        ag[np.logical_or(t< 379.,
t> 4731.)]= NAN<br>
        return ag<br>
    elif far< 0.069:<br>
        ag= air_gamma_2(t, far)<br>
        ag[np.logical_or(t< 699.,
t> 4731.)]= NAN<br>
        return ag<br>
    else: <br>
        return NAN </font>
<br><font size="3">Rest of the code is in the attachment.</font>
<br><font size="3"> </font>
<br><font size="3"> </font>
<br><font size="3">My two cents,</font>
<br><font size="3">eat</font>
<br><font size="3"><br>
<br>
</font><tt><font size="2" color="#000080"><br>
NAN = float('nan')</font></tt><font size="3"> <br>
</font><tt><font size="2" color="#000080"><br>
def air_gamma(t, far=0.0):</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
    """</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
    Specific heat ratio (gamma) of Air/JP8</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
    t - static temperature, Rankine</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
    [far] - fuel air ratio [- defaults to 0.0 (dry air)]</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
    air_gamma - specific heat ratio</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
    """</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
    if far < 0.:</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
        return NAN</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
    elif far < 0.005:</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
        if t < 379. or t > 4731.:</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
            return NAN</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
        else:</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
            air_gamma = -3.472487e-22 * t
** 6. + 6.218811e-18 * t ** 5. - 4.428098e-14 * t ** 4. + 1.569889e-10
* t ** 3. - 0.0000002753524 * t ** 2. + 0.0001684666 * t + 1.368652</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
    elif far < 0.069:</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
        if t < 699. or t > 4731.:</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
            return NAN</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
        else:</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
            a6 = 4.114808e-20 * far ** 3.
- 1.644588e-20 * far ** 2. + <a href="tel:+13103507" target="_blank">3.103507</a>e-21 * far - 3.391308e-22</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
            a5 = -6.819015e-16 * far ** 3.
+ 2.773945e-16 * far ** 2. - 5.469399e-17 * far + 6.058125e-18</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
            a4 = 4.684637e-12 * far ** 3.
- 1.887227e-12 * far ** 2. + 3.865306e-13 * far - 4.302534e-14</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
            a3 = -0.00000001700602 * far
** 3. + 0.000000006593809 * far ** 2. - 0.000000001392629 * far + 1.520583e-10</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
            a2 = 0.00003431136 * far ** 3.
- 0.00001248285 * far ** 2. + 0.000002688007 * far - 0.0000002651616</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
            a1 = -0.03792449 * far ** 3.
+ 0.01261025 * far ** 2. - 0.002676877 * far + 0.0001580424</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
            a0 = 13.65379 * far ** 3. - 3.311225
* far ** 2. + 0.3573201 * far + 1.372714</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
            air_gamma = a6 * t ** 6. + a5
* t ** 5. + a4 * t ** 4. + a3 * t ** 3. + a2 * t ** 2. + a1 * t + a0</font></tt><font size="3">
</font><tt><font size="2" color="#000080"><br>
    elif far >= 0.069:</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
        return NAN</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
    else:</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
        return NAN</font></tt><font size="3"> </font><tt><font size="2" color="#000080"><br>
    return air_gamma</font></tt><font size="3"> <br>
</font><font size="2" face="Century Gothic"><br>
David Parker <br>
Chromalloy - TDAG</font><font size="3"><br>
_______________________________________________<br>
NumPy-Discussion mailing list</font><font size="3" color="blue"><u><br>
</u></font><a href="mailto:NumPy-Discussion@scipy.org" target="_blank"><font size="3" color="blue"><u>NumPy-Discussion@scipy.org</u></font></a><font size="3" color="blue"><u><br>
</u></font><a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank"><font size="3" color="blue"><u>http://mail.scipy.org/mailman/listinfo/numpy-discussion</u></font></a><font size="3"><br>
</font>
<br></div></div><font size="1" face="sans-serif">[attachment "air_gamma.py"
deleted by Dave Parker/Chromalloy] </font><tt><font size="2">_______________________________________________<div class="im"><br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
</div></font></tt><div><div></div><div class="h5"><a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank"><tt><font size="2">http://mail.scipy.org/mailman/listinfo/numpy-discussion</font></tt></a><tt><font size="2"><br>

</font></tt>
<br></div></div><br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br></div>