1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] numpy error in cross() with dtype=object arrays

Discussão em 'Python' iniciado por Stack, Setembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    import numpy as np
    u = np.array([1, 2, -1], dtype=object)
    v = np.array([-1, 1, 0], dtype=object)
    np.cross(u, v)


    fails with numpy._core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'multiply' output from dtype('O') to dtype('int64') with casting rule 'same_kind'.

    np.dot(u, v) works as expected.

    Using dtype=object because calculations with actual data will overflow int64 and give incorrect results (without warning). The actual values are integers with about 15 significant digits (50 binary digits).

    One workaround is to make all values Decimals:

    u = np.array(list(map(Decimal, [1, 2, -1])))
    v = np.array(list(map(Decimal, [-1, 1, 0])))
    np.cross(u, v)


    Is there a better workaround using numpy?

    Continue reading...

Compartilhe esta Página