douwulu2576 2019-02-26 09:26
浏览 2030
已采纳

numpy:np.abs到底是如何工作的?

I'm trying to implement my own absolute function for gonum dense vectors in Go. I'm wandering if there's a better way of getting the absolute value of an array than squaring and then square rooting?

My main issue is that I've had to implement my own element wise Newtonian square-root function on these vectors and there's a balance between implementation speed and accuracy. If I could avoid using this square-root function I'd be happy.

  • 写回答

1条回答

  • doutangdan3588 2019-02-26 11:03
    关注

    NumPy source code can be tricky to navigate, because it has so many functions for so many data types. You can find the C-level source code for the absolute value function in the file scalarmath.c.src. This file is actually a template with function definitions that are later replicated by the build system for several data types. Note each function is the "kernel" that is run for each element of the array (looping through the array is done somewhere else). The functions are always called <name of the type>_ctype_absolute, where <name of the type> is the data type it applies to and is generally templated. Let's go through them.

    /**begin repeat
     * #name = ubyte, ushort, uint, ulong, ulonglong#
     */
    
    #define @name@_ctype_absolute @name@_ctype_positive
    
    /**end repeat**/
    

    This one is for unsigned types. In this case, the absolute value is the same as np.positive, which just copies the value without doing anything (it is what you get if you have an array a and you do +a).

    /**begin repeat
     * #name = byte, short, int, long, longlong#
     * #type = npy_byte, npy_short, npy_int, npy_long, npy_longlong#
     */
    static void
    @name@_ctype_absolute(@type@ a, @type@ *out)
    {
        *out = (a < 0 ? -a : a);
    }
    /**end repeat**/
    

    This one is for signed integers. Pretty straightforward.

    /**begin repeat
     * #name = float, double, longdouble#
     * #type = npy_float, npy_double, npy_longdouble#
     * #c = f,,l#
     */
    static void
    @name@_ctype_absolute(@type@ a, @type@ *out)
    {
        *out = npy_fabs@c@(a);
    }
    /**end repeat**/
    

    This is for floating-point values. Here npy_fabsf, npy_fabs and npy_fabsl functions are used. These are declared in npy_math.h, but defined through templated C code in npy_math_internal.h.src, essentially calling the C/C99 counterparts (unless C99 is not available, in which case fabsf and fabsl are emulated with fabs). You might think that the previous code should work as well for floating-point types, but actually these are more complicated, since they have things like NaN, infinity or signed zeros, so it is better to use the standard C functions that deal with everything reliably.

    static void
    half_ctype_absolute(npy_half a, npy_half *out)
    {
        *out = a&0x7fffu;
    }
    

    This is actually not templated, it is the absolute value function for half-precision floating-point values. Turns out you can change sign by just doing that bitwise operation (set the first bit to 0), since half-precision is simpler (if more limited) than other floating-point types (it's usually the same for those, but with special cases).

    /**begin repeat
     * #name = cfloat, cdouble, clongdouble#
     * #type = npy_cfloat, npy_cdouble, npy_clongdouble#
     * #rtype = npy_float, npy_double, npy_longdouble#
     * #c = f,,l#
     */
    static void
    @name@_ctype_absolute(@type@ a, @rtype@ *out)
    {
        *out = npy_cabs@c@(a);
    }
    /**end repeat**/
    

    This last one is for complex types. These use npy_cabsf, npycabs and npy_cabsl functions, again declared in npy_math.h but in this case template-implemented in npy_math_complex.c.src using C99 functions (unless that is not available, in which case it is emulated with np.hypot).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料