Quadratic equation in APL

Example for versions Dyalog APL 13.1

This code defines a named D-function which accepts equation coefficients as a single argument (a three-element array) and returns equation solution. First the argument is split into separate coefficients (N⊃⍵ picks Nth element of the array) and they are assigned names. After this first coefficient and discriminant are checked — if one of them is zero, the function returns a special value. Finally, non-zero discriminant is processed with the same code regardless of its sign due to the fact that APL has built-in complex numbers datatype. A function call

solve 1 0 2

will return:

0J1.4142135623730951 0J¯1.4142135623730951

(J is a separator between real and imaginary parts of the number, and ¯ — is a “high” minus, used for input/output of negative numbers).

Note that this code is not typical for APL due to conditional branching use as well as limiting possible arguments to a one-dimensional array of three elements.

solve←{A0⊃⍵  B1⊃⍵  C2⊃⍵  A=0:'Not a quadratic equation.'  D(B*2)-4×A×C  D=0:-0.5×B÷A  ((-B-D*0.5), -B+D*0.5)×0.5÷A}