Robot Control Library
polynomial.h
Go to the documentation of this file.
1 /**
2  * <rc/math/polynomial.h>
3  *
4  * @brief Functions for polynomial manipulation.
5  *
6  * We represent polynomials as a vector of coefficients with the highest power
7  * term on the left at vector index 0. The following polynomial manipulation
8  * functions are designed to behave like their counterparts in the Numerical
9  * Renaissance codebase.
10  *
11  * @author James Strawson
12  * @date 2016
13  *
14  * @addtogroup Polynomial
15  * @ingroup Math
16  * @{
17  */
18 
19 
20 #ifndef RC_POLYNOMIAL_H
21 #define RC_POLYNOMIAL_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include <rc/math/vector.h>
28 
29 /**
30  * @brief Prints a polynomial in human-readable format in one line.
31  *
32  * Like rc_print_vector, but assumes the contents represent a polynomial and
33  * prints the coefficients with trailing powers of x for easier reading. This
34  * relies on your terminal supporting unicode UTF-8. numer of coefficients and
35  * there the length of vector v must be less than or equal to 10.
36  *
37  * @param[in] v polynomial coefficients to be printed
38  *
39  * @return 0 on success or -1 on failure
40  */
42 
43 /**
44  * @brief Convolutes the polynomials a&b and places the result in vector c.
45  *
46  * This finds the coefficients of the polynomials resulting from multiply a*b.
47  * The original contents of c are freed and new memory is allocated if
48  * necessary.
49  *
50  * @param[in] a First set of coefficients
51  * @param[in] b Second set of coefficients
52  * @param[out] c Vector to output resulting coefficients
53  *
54  * @return Returns 0 on success or -1 on failure.
55  */
57 
58 /**
59  * @brief Raises a polynomial a to itself n times where n is greater than
60  * or equal to 0.
61  *
62  * Places the result in vector b, any existing memory allocated for b is freed
63  * and its contents are lost. Returns 0 on success and -1 on failure.
64  *
65  * @param[in] a Initial coefficients
66  * @param[in] n Power, must be >=0
67  * @param[out] b resulting coefficients
68  *
69  * @return Returns 0 on success or -1 on failure.
70  */
71 int rc_poly_power(rc_vector_t a, int n, rc_vector_t* b);
72 
73 /**
74  * @brief Add two polynomials a&b with right justification and place the
75  * result in c.
76  *
77  * Any existing memory allocated for c is freed and its contents are lost.
78  *
79  * @param[in] a First input
80  * @param[in] b second input
81  * @param[out] c output
82  *
83  * @return Returns 0 on success and -1 on failure.
84  */
86 
87 /**
88  * @brief Adds polynomials a&b with right justification.
89  *
90  * The result is placed in vector a and a's original contents are lost. More
91  * memory is allocated for a if necessary.
92  *
93  * @param a First input and where output is written
94  * @param[in] b second input
95  *
96  * @return Returns 0 on success and -1 on failure.
97  */
99 
100 /**
101  * @brief Subtracts two polynomials a-b with right justification and places
102  * the result in c.
103  *
104  * Any existing memory allocated for c is freed and its contents are lost.
105  * Returns 0 on success and -1 on failure.
106  *
107  * @param[in] a First input
108  * @param[in] b second input
109  * @param[out] c output
110  *
111  * @return Returns 0 on success and -1 on failure.
112  */
114 
115 /**
116  * @brief Subtracts b from a with right justification.
117  *
118  * a stays in place and new memory is allocated only if b is longer than a.
119  *
120  * @param a First input and where output is written
121  * @param[in] b second input
122  *
123  * @return Returns 0 on success and -1 on failure.
124  */
126 
127 /**
128  * @brief Calculates the dth derivative of the polynomial a and places the
129  * result in vector b.
130  *
131  * @param[in] a Input polynomial coefficients
132  * @param[in] d which derivative to take (>=0)
133  * @param[out] b result
134  *
135  * @return Returns 0 on success and -1 on failure.
136  */
138 
139 /**
140  * @brief Divides denominator d into numerator n. The remainder is placed
141  * into vector rem and the divisor is placed into vector div.
142  *
143  * @param[in] n numerator
144  * @param[in] d denominator
145  * @param div The resulting divisor
146  * @param rem The resulting remainder
147  *
148  * @return Returns 0 on success and -1 on failure.
149  */
151 
152 /**
153  * @brief Calculates coefficients for continuous-time Butterworth
154  * polynomial of order N and cutoff wc (rad/s) and places them in vector b.
155  *
156  * @param[in] N Order of the polynomial
157  * @param[in] wc cutoff frequency in rad/s
158  * @param[out] b resulting coefficients
159  *
160  * @return Returns 0 on success and -1 on failure.
161  */
162 int rc_poly_butter(int N, double wc, rc_vector_t* b);
163 
164 
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif // RC_POLYNOMIAL_H
171 
172 /** @} end group math*/
int rc_poly_add_inplace(rc_vector_t *a, rc_vector_t b)
Adds polynomials a&b with right justification.
int rc_poly_print(rc_vector_t v)
Prints a polynomial in human-readable format in one line.
int rc_poly_add(rc_vector_t a, rc_vector_t b, rc_vector_t *c)
Add two polynomials a&b with right justification and place the result in c.
int rc_poly_differentiate(rc_vector_t a, int d, rc_vector_t *b)
Calculates the dth derivative of the polynomial a and places the result in vector b...
int rc_poly_conv(rc_vector_t a, rc_vector_t b, rc_vector_t *c)
Convolutes the polynomials a&b and places the result in vector c.
Struct containing the state of a vector and a pointer to dynamically allocated memory to hold its con...
Definition: vector.h:41
int rc_poly_divide(rc_vector_t n, rc_vector_t d, rc_vector_t *div, rc_vector_t *rem)
Divides denominator d into numerator n. The remainder is placed into vector rem and the divisor is pl...
int rc_poly_butter(int N, double wc, rc_vector_t *b)
Calculates coefficients for continuous-time Butterworth polynomial of order N and cutoff wc (rad/s) a...
int rc_poly_power(rc_vector_t a, int n, rc_vector_t *b)
Raises a polynomial a to itself n times where n is greater than or equal to 0.
int rc_poly_subtract_inplace(rc_vector_t *a, rc_vector_t b)
Subtracts b from a with right justification.
int rc_poly_subtract(rc_vector_t a, rc_vector_t b, rc_vector_t *c)
Subtracts two polynomials a-b with right justification and places the result in c.