<div dir="ltr">If that is really all you need, then the version in python is: <div><br></div><div>def convert_one(a):<br>    """<br>    Converts input with arbitrary layout and dtype to a blas/lapack<br>    compatible dtype with either C or F order.  Acceptable objects are passed<br>    through without making copies.<br>    """<br><br>    a_arr = np.asarray(a)<br>    dtype = np.result_type(a_arr, 1.0)<br><br>    # need to handle these separately<br>    if dtype == np.longdouble:<br>        dtype = np.dtype('d')<br>    elif dtype == np.clongdouble:<br>        dtype = np.dtype('D')<br>    elif dtype == np.float16:<br>        dtype = np.dtype('f')<br><br>    # explicitly force a copy if a_arr isn't one segment<br>    return np.array(a_arr, dtype, copy=not a_arr.flags.forc, order='K')<br></div><div><br></div><div>In Cython, you could just run exactly this code and it's probably fine.  The could also be rewritten using the C calls if you really wanted. </div><div><br></div><div>You need to either provide your own or use a casting table and the copy / conversion routines from somewhere.  Cython, to my knowledge, doesn't provide these things, but Numpy does.</div><div><br></div><div>Eric</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Aug 9, 2020 at 6:16 PM Ilhan Polat <<a href="mailto:ilhanpolat@gmail.com" target="_blank">ilhanpolat@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi all,</div><div><br></div><div>As you might have seen my recent mails in Cython list, I'm trying to cook up an input validator for the linalg.solve() function. The machinery of SciPy linalg is as follows: <br></div><div><br></div><div>Some input comes in passes through np.asarray() then depending on the resulting dtype of the numpy array we choose a LAPACK flavor (s,d,c,z) and off it goes through f2py to lalaland and comes back with some result. <br></div><div><br></div><div>For the backslash polyalgorithm I need the arrays to be contiguous (C- or F- doesn't matter) and any of the four (possibly via making new copies) float, double, float complex, double complex after the intake because we are using wrapped fortran code (LAPACK) in SciPy. So my difficulty is how to type such function input, say, <br></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">ctypedef fused numeric_numpy_t:<br>    bint<br>    cnp.npy_bool<br>    cnp.int_t       <br>    cnp.intp_t      <br>    cnp.int8_t      <br>    cnp.int16_t     <br>    cnp.int32_t     <br>    cnp.int64_t     <br>    cnp.uint8_t     <br>    cnp.uint16_t    <br>    cnp.uint32_t    <br>    cnp.uint64_t<br>    cnp.float32_t   <br>    cnp.float64_t   <br>    cnp.complex64_t <br>    cnp.complex128_t</span><br></div><div><br></div><div>Is this acceptable or something else needs to be used? Then there is the storyof np.complex256 and mysterious np.float16. Then there is the Linux vs Windows platform dependence issue and possibly some more that I can't comprehend. Then there are datetime, str, unicode etc. that need to be rejected. So this is quickly getting out of hand for my small brain.<br></div><div><br></div><div>To be honest, I am a bit running out of steam working with this issue even though I managed to finish the actual difficult algorithmic part but got stuck here. I am quite surprised how fantastically complicated and confusing both NumPy and Cython docs about this stuff. Shouldn't we keep a generic fused type for such usage? Or maybe there already exists 

but I don't know and would be really grateful for pointers.</div><div><br></div><div>Here I wrote a dummy typed Cython function just for type checking:</div><div><br></div><div><span style="font-family:monospace">cpdef inline bint ncc(
numeric_numpy_t[:, :] a):<br>    print(a.is_f_contig())<br>    print(a.is_c_contig())<br><br>    return a.is_f_contig() or a.is_c_contig()</span></div><div><br></div><div>And this is a dummy loop (with aliases) just to check whether fused type is working or not (on windows I couldn't make it work for float16).<br></div><div><br></div><div><span style="font-family:monospace">for x in (np.uint, np.uintc, np.uintp, np.uint0, np.uint8, np.uint16, np.uint32,<br>          np.uint64, <a href="http://np.int" target="_blank">np.int</a>, np.intc, np.intp, np.int0, np.int8, np.int16,<br>          np.int32,np.int64, np.float, np.float32, np.float64, np.float_,<br>          np.complex, np.complex64, np.complex128, np.complex_):<br>    print(x)<br>    C = np.arange(25., dtype=x).reshape(5, 5)<br>    ncc(C)</span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:arial,sans-serif">Thanks in advance,</span></div><div><span style="font-family:monospace"><span style="font-family:arial,sans-serif">ilhan</span><br></span></div><div><br></div></div>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>