Jelly, 8? 11 bytes
Ærṛ/ȧÆr,NċAƲƇN+AƲƇ
A monadic Link that accepts a list of polynomialthe coefficients ([...,d[d,c,b,a]
) and yields a list of the (one or three) real roots, unless a
is zero in which case it yields an empty list.
Try it online!Try it online! Or see the test-suitetest-suite.
...\$8\$ bytes if we can handle a
being zero (in which case it works for all polynomials):
Ær,NċAƲƇ
How?
The heavy lifting is done by a built-in...
Ærṛ/ȧÆr,NċAƲƇN+AƲƇ - Link: coefficients, P = [d,b,c,a]
/ - reduce (P) by:
ṛ - right
-> a
Ær - polynomial roots (of P)
ȧ - logical AND
-> [d,c,b,a] or 0
Ƈ - keep those for which (with 0, keep those of range(0)=[]):
Ʋ - last four links as a monad - f(x):
N - negate (x)
, - (P) paired with (that)
A - absolute value (of x)
ċ - count occurrences (of that in the pair)
-> Truthy (1 or 2*) if x is real, falsey (0) if not
* when x=0