Comments on: PYTHON, WHY U NO HAZ A SIGN FUNCTION ? http://sametmax.com/python-why-u-no-haz-a-sign-function/ Du code, du cul Fri, 06 Sep 2019 09:34:15 +0000 hourly 1 https://wordpress.org/?v=4.9.7 By: kontre http://sametmax.com/python-why-u-no-haz-a-sign-function/#comment-13694 Tue, 27 Aug 2013 11:37:43 +0000 http://sametmax.com/?p=7165#comment-13694 Pour info, voilà la logique du signe des complexes dans numpy : https://github.com/numpy/numpy/issues/3621

]]>
By: Sam http://sametmax.com/python-why-u-no-haz-a-sign-function/#comment-13635 Mon, 26 Aug 2013 14:19:24 +0000 http://sametmax.com/?p=7165#comment-13635 @poulpe : pas mal, ça évite malicieusement la division par zéro.

]]>
By: kontre http://sametmax.com/python-why-u-no-haz-a-sign-function/#comment-13628 Mon, 26 Aug 2013 07:11:06 +0000 http://sametmax.com/?p=7165#comment-13628 Les scientifiques, comme très souvent, utilisent numpy :

import numpy as np
np.sign(-1)  # -1
np.sign(0)  # 0
np.sign(1)  # 1
np.sign(nan)  # nan
np.sign(1j)  # (1+0j)
np.sign(-1+1j)  # (-1+0j)
np.sign(-1j)  # (-1+0j)

Ne me demandez pas la logique pour les complexes, la doc n’en parle pas : http://docs.scipy.org/doc/numpy/reference/generated/numpy.sign.html

]]>
By: poulpe http://sametmax.com/python-why-u-no-haz-a-sign-function/#comment-13624 Mon, 26 Aug 2013 06:26:08 +0000 http://sametmax.com/?p=7165#comment-13624 return x / abs(x or 1)
Surement un poil plus lent que la méthode à entwanne, mais plus rapide que celle de Sam :)

]]>
By: Sam http://sametmax.com/python-why-u-no-haz-a-sign-function/#comment-13595 Sat, 24 Aug 2013 19:10:15 +0000 http://sametmax.com/?p=7165#comment-13595 @entwanne: et ça sera probablement plus rapide à l’éxécution en plus, puisqu’il y a une appel en moins. Copysign reste néanmoins idéal pour le cas le plus courant où on souhaite faire la multiplication derrière.

]]>
By: Pyo http://sametmax.com/python-why-u-no-haz-a-sign-function/#comment-13592 Sat, 24 Aug 2013 16:55:10 +0000 http://sametmax.com/?p=7165#comment-13592 Oups…
Désolé pour le zéro… :-D

]]>
By: Pyo http://sametmax.com/python-why-u-no-haz-a-sign-function/#comment-13591 Sat, 24 Aug 2013 16:51:34 +0000 http://sametmax.com/?p=7165#comment-13591 Coucou,
À quoi sert le “x and” alors??


def sign(x):
return (-1,1)[x>0]

]]>
By: cym13 http://sametmax.com/python-why-u-no-haz-a-sign-function/#comment-13586 Sat, 24 Aug 2013 13:55:33 +0000 http://sametmax.com/?p=7165#comment-13586 @entwanne: Jolie structure, très astucieuse j’aime beaucoup ! Je la garde sous le coude même si elle ne présente pas l’avantage de pouvoir prendre des nombres complexes comme copysign.

]]>
By: entwanne http://sametmax.com/python-why-u-no-haz-a-sign-function/#comment-13585 Sat, 24 Aug 2013 13:29:33 +0000 http://sametmax.com/?p=7165#comment-13585 Quitte à créer soi-même une fonction avec un `and`, je préfère un

def sign(x)
  return x and (-1, 1)[x > 0]

qui est plus lisible je trouve qu’une fonction `copysign` dont le comportement peut sembler flou au premier abord.

]]>