Learning Scientific Programming with Python (2nd edition)
P6.5.6: Conic sections
Question P6.5.6
The implicit formula for a conic section may be written as the second-degree polynomial, $$ Q = Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0, $$ or in matrix form using the homogeneous coordinate vector,: $$ \mathbf{x} = \begin{pmatrix}x \\ y \\ 1\end{pmatrix}, $$
as $\mathbf{x}^T\mathbf{Q}\mathbf{x} = 0$, where
$$ \mathbf{Q} = \begin{pmatrix} A & B/2 & D/2 \\ B/2 & C & E/2\\ D/2 & E/2 & F \end{pmatrix}. $$
Conic sections may be classified according to the following properties of $\mathbf{Q}$, where the submatrix $\mathbf{Q}_{33}$ is $$ \mathbf{Q}_{33} = \begin{pmatrix}A & B/2\\B/2 & C\end{pmatrix}. $$
- If $\mathrm{det} \mathbf{Q} = 0$, the conic is degenerate in one of the following forms:
- if $\mathrm{det} \mathbf{Q}_{33} < 0$, the equation represents two intersecting lines,
- if $\mathrm{det} \mathbf{Q}_{33} = 0$, the equation represents two parallel lines,
- if $\mathrm{det} \mathbf{Q}_{33} > 0$, the equation represents a single point.
- If $\mathrm{det} \mathbf{Q} \ne 0$:
- if $\mathrm{det} \mathbf{Q_{33}} < 0$ the conic is a hyperbola,
- if $\mathrm{det} \mathbf{Q_{33}} = 0$ the conic is a parabola,
- if $\mathrm{det} \mathbf{Q_{33}} > 0$, the conic is an ellipse:
- if $A=C$ and $B=0$, the ellipse is a circle.
Write a program to classify the conic section represented by the 6 coefficients $A, B, C, D, E$ and $F$.
Some test-cases (coefficients not given are zero):
- Hyperbola: $B = 1, F = -9$.
- Parabola: $A = \frac{1}{2}, D = 2, E = -\frac{1}{2}$.
- Circle: $A = \frac{1}{2}, C = \frac{1}{2}, D = -2, E = -3, F = 2$.
- Ellipse: $A = 9, C = 4, F = -36$.
- Two parallel lines: $A = 1, F = -1$.
- A single point: $A = 1, C = 1$.