Print real roots only in numpy

前端 未结 3 1439
Happy的楠姐
Happy的楠姐 2021-02-05 13:32

I have something like this:

coefs = [28, -36, 50, -22]
print(numpy.roots(coefs))

Of course the result is:

[ 0.35770550+1.117926         


        
3条回答
  •  时光说笑
    2021-02-05 13:51

    You can do it, using iscomplex as follows:

    r = numpy.roots(coefs)
    
    In [15]: r[~numpy.iscomplex(r)]
    Out[15]: array([ 0.57030329+0.j])
    

    Also you can use isreal as pointed out in comments:

    In [17]: r[numpy.isreal(r)]
    Out[17]: array([ 0.57030329+0.j])
    

提交回复
热议问题