BRL-CAD
poly.h
Go to the documentation of this file.
1/* P O L Y . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21/*----------------------------------------------------------------------*/
22
23/** @addtogroup bn_poly
24 *
25 * @brief Library for dealing with polynomials.
26 */
27/** @{ */
28/** @file poly.h */
29
30#ifndef BN_POLY_H
31#define BN_POLY_H
32
33#include "common.h"
34
35#include "vmath.h"
36
37#include "bu/magic.h"
38#include "bn/defines.h"
39#include "bn/complex.h"
40
41__BEGIN_DECLS
42
43/* This could be larger, or even dynamic... */
44#define BN_MAX_POLY_DEGREE 6 /**< Maximum Poly Order */
45
46/**
47 * Polynomial data type
48 * bn_poly->cf[n] corresponds to X^n
49 */
50typedef struct bn_poly {
51 uint32_t magic;
52 size_t dgr;
55
56#define BN_CK_POLY(_p) BU_CKMAG(_p, BN_POLY_MAGIC, "struct bn_poly")
57#define BN_POLY_NULL ((struct bn_poly *)NULL)
58#define BN_POLY_INIT_ZERO { BN_POLY_MAGIC, 0, {0.0} }
59
60
61/**
62 * bn_poly_mul
63 *
64 * @brief
65 * multiply two polynomials
66 */
67BN_EXPORT extern struct bn_poly *bn_poly_mul(struct bn_poly *product,
68 const struct bn_poly *m1,
69 const struct bn_poly *m2);
70
71/**
72 * bn_poly_scale
73 *
74 * @brief
75 * scale a polynomial
76 */
77BN_EXPORT extern struct bn_poly *bn_poly_scale(struct bn_poly *eqn,
78 double factor);
79
80/**
81 * bn_poly_add
82 * @brief
83 * add two polynomials
84 */
85BN_EXPORT extern struct bn_poly *bn_poly_add(struct bn_poly *sum,
86 const struct bn_poly *poly1,
87 const struct bn_poly *poly2);
88
89/**
90 * bn_poly_sub
91 * @brief
92 * subtract two polynomials
93 */
94BN_EXPORT extern struct bn_poly *bn_poly_sub(struct bn_poly *diff,
95 const struct bn_poly *poly1,
96 const struct bn_poly *poly2);
97
98/**
99 * @brief
100 * Divides any polynomial into any other polynomial using synthetic
101 * division. Both polynomials must have real coefficients.
102 */
103BN_EXPORT extern void bn_poly_synthetic_division(struct bn_poly *quo,
104 struct bn_poly *rem,
105 const struct bn_poly *dvdend,
106 const struct bn_poly *dvsor);
107
108/**
109 *@brief
110 * Uses the quadratic formula to find the roots (in `complex' form) of
111 * any quadratic equation with real coefficients.
112 *
113 * @return 1 for success
114 * @return 0 for fail.
115 */
116BN_EXPORT extern int bn_poly_quadratic_roots(struct bn_complex roots[],
117 const struct bn_poly *quadrat);
118
119/**
120 *@brief
121 * Uses the cubic formula to find the roots (in `complex' form)
122 * of any cubic equation with real coefficients.
123 *
124 * to solve a polynomial of the form:
125 *
126 * X**3 + c1*X**2 + c2*X + c3 = 0,
127 *
128 * first reduce it to the form:
129 *
130 * Y**3 + a*Y + b = 0,
131 *
132 * where
133 * Y = X + c1/3,
134 * and
135 * a = c2 - c1**2/3,
136 * b = (2*c1**3 - 9*c1*c2 + 27*c3)/27.
137 *
138 * Then we define the value delta, D = b**2/4 + a**3/27.
139 *
140 * If D > 0, there will be one real root and two conjugate
141 * complex roots.
142 * If D = 0, there will be three real roots at least two of
143 * which are equal.
144 * If D < 0, there will be three unequal real roots.
145 *
146 * Returns 1 for success, 0 for fail.
147 */
148BN_EXPORT extern int bn_poly_cubic_roots(struct bn_complex roots[],
149 const struct bn_poly *eqn);
150
151/**
152 *@brief
153 * Uses the quartic formula to find the roots (in `complex' form)
154 * of any quartic equation with real coefficients.
155 *
156 * @return 1 for success
157 * @return 0 for fail.
158 */
159BN_EXPORT extern int bn_poly_quartic_roots(struct bn_complex roots[],
160 const struct bn_poly *eqn);
161
162BN_EXPORT extern int bn_poly_findroot(bn_poly_t *eqn,
163 bn_complex_t *nxZ,
164 const char *str);
165
167 bn_poly_t *eqn,
168 bn_complex_t *b,
170 bn_complex_t *d);
171
172BN_EXPORT extern int bn_poly_checkroots(bn_poly_t *eqn,
173 bn_complex_t *roots,
174 int nroots);
175
176BN_EXPORT extern void bn_poly_deflate(bn_poly_t *oldP,
177 bn_complex_t *root);
178
179BN_EXPORT extern int bn_poly_roots(bn_poly_t *eqn,
180 bn_complex_t roots[],
181 const char *name);
182
183/**
184 * Print out the polynomial.
185 */
186BN_EXPORT extern void bn_pr_poly(const char *title,
187 const struct bn_poly *eqn);
188
189/**
190 * Print out the roots of a given polynomial (complex numbers)
191 */
192BN_EXPORT extern void bn_pr_roots(const char *title,
193 const struct bn_complex roots[],
194 int n);
195
196__END_DECLS
197
198#endif /* BN_POLY_H */
199/** @} */
200/*
201 * Local Variables:
202 * mode: C
203 * tab-width: 8
204 * indent-tabs-mode: t
205 * c-file-style: "stroustrup"
206 * End:
207 * ex: shiftwidth=4 tabstop=8
208 */
Header file for the BRL-CAD common definitions.
int bn_poly_checkroots(bn_poly_t *eqn, bn_complex_t *roots, int nroots)
int bn_poly_findroot(bn_poly_t *eqn, bn_complex_t *nxZ, const char *str)
struct bn_poly * bn_poly_add(struct bn_poly *sum, const struct bn_poly *poly1, const struct bn_poly *poly2)
add two polynomials
#define BN_MAX_POLY_DEGREE
Definition: poly.h:44
int bn_poly_quadratic_roots(struct bn_complex roots[], const struct bn_poly *quadrat)
Uses the quadratic formula to find the roots (in ‘complex’ form) of any quadratic equation with real ...
void bn_pr_poly(const char *title, const struct bn_poly *eqn)
void bn_poly_deflate(bn_poly_t *oldP, bn_complex_t *root)
void bn_pr_roots(const char *title, const struct bn_complex roots[], int n)
void bn_poly_synthetic_division(struct bn_poly *quo, struct bn_poly *rem, const struct bn_poly *dvdend, const struct bn_poly *dvsor)
Divides any polynomial into any other polynomial using synthetic division. Both polynomials must have...
int bn_poly_roots(bn_poly_t *eqn, bn_complex_t roots[], const char *name)
struct bn_poly * bn_poly_scale(struct bn_poly *eqn, double factor)
scale a polynomial
int bn_poly_cubic_roots(struct bn_complex roots[], const struct bn_poly *eqn)
Uses the cubic formula to find the roots (in ‘complex’ form) of any cubic equation with real coeffici...
void bn_poly_eval_w_2derivatives(bn_complex_t *cZ, bn_poly_t *eqn, bn_complex_t *b, bn_complex_t *c, bn_complex_t *d)
struct bn_poly * bn_poly_mul(struct bn_poly *product, const struct bn_poly *m1, const struct bn_poly *m2)
multiply two polynomials
struct bn_poly bn_poly_t
struct bn_poly * bn_poly_sub(struct bn_poly *diff, const struct bn_poly *poly1, const struct bn_poly *poly2)
subtract two polynomials
int bn_poly_quartic_roots(struct bn_complex roots[], const struct bn_poly *eqn)
Uses the quartic formula to find the roots (in ‘complex’ form) of any quartic equation with real coef...
void float float int * n
Definition: tig.h:74
void int * c
Definition: tig.h:139
double fastf_t
fastest 64-bit (or larger) floating point type
Definition: vmath.h:330
Global registry of recognized magic numbers.
Definition: poly.h:50
fastf_t cf[BN_MAX_POLY_DEGREE+1]
Definition: poly.h:53
uint32_t magic
Definition: poly.h:51
size_t dgr
Definition: poly.h:52
fundamental vector, matrix, quaternion math macros