polynomial_adt.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Polynomial ADT
  2. ** A polynomial module with
  3. ** ability to add,sub,mul derivate/integrate, compose ... polynomials
  4. ** ..expansion in progress ...
  5. * Copyright (c) 2009 I. Soule
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. ** iasoule32@gmail.com
  30. */
  31. #ifndef __POLYNOMIAL_ADT
  32. #define __POLYNOMIAL_ADT
  33. #include <assert.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <math.h>
  37. #define max(a, b) (a) > (b) ? (a) : (b)
  38. #define sgn(a) (a) < 0 ? '+' : '-' //for quadratic factored form
  39. typedef struct node {
  40. int exp;
  41. float coeff;
  42. struct node *next;
  43. }Node;
  44. typedef struct polynomial_adt {
  45. Node *head;
  46. int terms, hp; //hp highest power
  47. }PolyAdt;
  48. /**
  49. * create_adt - create a polynomial on the heap
  50. * @hp: the highest power in the polynomial
  51. */
  52. PolyAdt *create_adt(int hp);
  53. /**
  54. * create_node - creates a Node (exponent, constant and next pointer) on the heap
  55. * @constant: the contant in the term
  56. * @exp: the exponent on the term
  57. * @next: the next pointer to another term in the polynomial
  58. *
  59. * This should not be called by client code (hence marked static)
  60. * used to assist insert_term()
  61. */
  62. static inline Node *create_node(float constant, int exp, Node *next) {
  63. Node *nNode = malloc(sizeof(Node));
  64. assert(nNode != NULL);
  65. nNode->exp = exp;
  66. nNode->coeff = constant;
  67. nNode->next = next;
  68. return nNode;
  69. }
  70. /**
  71. * insert_term - inserts a term into the polynomial
  72. * @pAdt: the polynomial
  73. * @c: constant value on the term
  74. * @e: the exponent on the term
  75. */
  76. void insert_term(PolyAdt *pAdt, float c, int e);
  77. /**
  78. * polyImage - returns an image (direct) copy of the polynomial
  79. * @orig: the polynomial to be duplicated
  80. */
  81. PolyAdt *polyImage(const PolyAdt *orig);
  82. /**
  83. * add - adds two polynomials together, and returns their sum (as a polynomial)
  84. * @a: the 1st polynomial
  85. * @b: the 2nd polynomial
  86. */
  87. PolyAdt *add(const PolyAdt *a, const PolyAdt *b);
  88. /**
  89. * sub - subtracts two polynomials, and returns their difference (as a polynomial)
  90. * @a: the 1st polynomial
  91. * @b: the 2nd polynomial
  92. * Aids in code reuse by negating the terms (b) and then calls the add() function
  93. */
  94. PolyAdt *subtract(const PolyAdt *a, const PolyAdt *b);
  95. /**
  96. * multiply - multiply two polynomials, and returns their product (as a polynomial)
  97. * @a: the 1st polynomial
  98. * @b: the 2nd polynomial
  99. */
  100. PolyAdt *multiply(const PolyAdt *a, const PolyAdt *b);
  101. /**
  102. * derivative - computes the derivative of a polynomial and returns the result
  103. * @a: the polynomial to take the derivative upon
  104. */
  105. PolyAdt *derivative(const PolyAdt *a);
  106. /**
  107. * integrate - computes the integral of a polynomial and returns the result
  108. * @a: the polynomial to take the integral of
  109. *
  110. * Will compute an indefinite integral over a
  111. */
  112. PolyAdt *integrate(const PolyAdt *a);
  113. /**
  114. * quadratic_roots - finds the roots of the polynomial ax^2+bx+c, a != 0 && b != 0
  115. * @a: the polynomial
  116. * @real: a pointer to float of the real(R) part of a
  117. * @cplx: a pointer to float of the imaginary(I) part of a
  118. *
  119. * Usage:
  120. * Two options can be done by the client
  121. * 1. Either pass NULL to real and cplx
  122. * this will display the roots by printf
  123. * quadratic_roots(myPolynomial, NULL, NULL);
  124. *
  125. * 2. Pass in pointers** to type float of the real and complex
  126. * if the discriminant is >0 cplx = -ve root of X
  127. * quadratic_roots(myPolynomial, &realPart, &complexPart);
  128. */
  129. void quadratic_roots(const PolyAdt *a, float *real, float *cplx);
  130. /**
  131. * exponentiate - computes polynomial exponentiation (P(x))^n, n E Z*
  132. * @a: the polynomial
  133. * @n: the exponent
  134. * Works fast for small n (n < 8) currently runs ~ O(n^2 lg n)
  135. */
  136. PolyAdt *exponentiate(const PolyAdt *a, int n);
  137. /**
  138. * compose - computes the composition of two polynomials P(Q(x)) and returns the composition
  139. * @p: polynomial P(x) which will x will be equal to Q(x)
  140. * @q: polynomial Q(x) which is the argument to P(x)
  141. */
  142. PolyAdt *compose(const PolyAdt *p, const PolyAdt *q);
  143. /**
  144. * destroy_poly - completely frees the polynomial from the heap and resets all values
  145. * @poly: the polynomial to release memory back to the heap
  146. * Usage:
  147. * destroy_poly(myPoly); //puts polynomial on free list
  148. */
  149. void destroy_poly(PolyAdt *poly);
  150. /**
  151. * display_poly - displays the polynomial to the console in nice format
  152. * @a: the polynomial to display
  153. */
  154. void display_poly(const PolyAdt *a);
  155. #endif