]> git.ozlabs.org Git - ccan/blobdiff - junkcode/iasoule32@gmail.com-polynomial/polynomial_adt_test.c
New polynomial module (not quite CCAN format).
[ccan] / junkcode / iasoule32@gmail.com-polynomial / polynomial_adt_test.c
diff --git a/junkcode/iasoule32@gmail.com-polynomial/polynomial_adt_test.c b/junkcode/iasoule32@gmail.com-polynomial/polynomial_adt_test.c
new file mode 100644 (file)
index 0000000..f7c9c8a
--- /dev/null
@@ -0,0 +1,132 @@
+/* \r
+** polynomial_adt_test.c\r
+** Test (minimalistic) for the polynomial module\r
+ * More of a display of functionality\r
+ * Copyright (c) 2009 I. Soule\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ * 1. Redistributions of source code must retain the above copyright\r
+ *    notice, this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright\r
+ *    notice, this list of conditions and the following disclaimer in the\r
+ *    documentation and/or other materials provided with the distribution.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND\r
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\r
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
+ * SUCH DAMAGE.\r
+ *\r
+**          iasoule32@gmail.com\r
+*/\r
+\r
+#include <stdio.h>\r
+#include "polynomial_adt.h"\r
+\r
+int main()\r
+{\r
+    PolyAdt *p = create_adt(5), *q = create_adt(4);\r
+    PolyAdt *sum, *diff, *prod;\r
+    \r
+    insert_term(p,1,5);\r
+    insert_term(p,3,4);\r
+    insert_term(p,1,3);\r
+    insert_term(p,9,2);\r
+    insert_term(p,8,1);\r
+    \r
+    insert_term(q,2,4);\r
+    insert_term(q,8,3);\r
+    insert_term(q,7,2);\r
+    insert_term(q,6,1);\r
+    \r
+    \r
+    printf("Displaying Polynomials ...\n");\r
+    display_poly(p);\r
+    display_poly(q);\r
+    \r
+    sum = add(p,q);\r
+    printf("P(x) + Q(x) = ");\r
+    display_poly(sum);\r
+    \r
+    diff = subtract(p,q);\r
+    printf("P(x) - Q(x) = ");\r
+    display_poly(diff);\r
+    \r
+    prod = multiply(p,q);\r
+    printf("P(x)*Q(x) = ");\r
+    display_poly(prod);\r
+    \r
+    PolyAdt *quad = create_adt(2);\r
+    insert_term(quad, 10, 2);\r
+    insert_term(quad, 30, 1);\r
+    insert_term(quad, 2, 0);\r
+    \r
+    quadratic_roots(quad, NULL, NULL); //print out the roots\r
+    \r
+    float real, cplx;\r
+    quadratic_roots(quad, &real, &cplx);\r
+    \r
+    printf("X1 = %f, X2 = %f\n\n", real, cplx);\r
+    \r
+    PolyAdt *deriv, *integral;\r
+    \r
+    deriv = derivative(p);\r
+    printf("The derivitive of p = ");\r
+    display_poly(deriv);\r
+    integral = integrate(q);\r
+    \r
+    printf("The integral of q = ");\r
+    display_poly(integral);\r
+    \r
+    printf("\n Computing P(x)^3\n");\r
+    \r
+    PolyAdt *expo;\r
+    expo = exponentiate(p, 3);\r
+    display_poly(expo);\r
+    printf("\n");\r
+    \r
+    printf("Computing Integral[Q(x)^2]\n");\r
+    expo = exponentiate(q, 2);\r
+    integral = integrate(expo);\r
+    display_poly(integral);\r
+    \r
+    \r
+    printf(" Differentiating and Integrating P\n");\r
+    display_poly(integrate(derivative(p))); \r
+    \r
+    PolyAdt *L, *M;\r
+    \r
+    L = create_adt(3), M = create_adt(2);\r
+    \r
+    insert_term(L, 4, 3);\r
+    insert_term(L, 10, 2);\r
+    insert_term(L, 15, 1);\r
+    \r
+    insert_term(M, 4, 2);\r
+    printf("L = ");\r
+    display_poly(L);\r
+    printf("M = ");\r
+    display_poly(M);\r
+    \r
+    \r
+    printf("Computing composition L(M(X))\n");\r
+    display_poly(compose(L, M));\r
+    \r
+    printf("Freed memory back to heap for allocated polys'");\r
+    destroy_poly(sum);\r
+    destroy_poly(diff);\r
+    destroy_poly(prod);\r
+    destroy_poly(L); destroy_poly(M);\r
+    destroy_poly(q); destroy_poly(p);\r
+    \r
+    return 0;\r
+}\r