BRL-CAD
bn_poly_synthetic_div.c
Go to the documentation of this file.
1 /* B N _ P O L Y _ S Y N T H E T I C _ D I V . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 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 #include "common.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
27 #include <signal.h>
28 
29 #include "bu.h"
30 #include "vmath.h"
31 #include "bn.h"
32 
33 
34 /* holds three polynomials to be used in test. */
38 
39 
40 /* Initializes polynomial storing a negative, positive and zero
41  * coefficients. Test polynomials will be of 4th, 3rd, and 2nd
42  * degrees.
43  */
44 void
45 poly_init(void)
46 {
47  /* initializes a 3rd degree polynomial with negative
48  * coefficients.
49  */
50  input[0].dgr = 3;
51  quo[0].dgr = rem[0].dgr = 4;
52 
53  input[0].cf[0] = -4, input[0].cf[1] = -3, input[0].cf[2] = -2, input[0].cf[3] = -38;/* input coeff */
54 
55  /**
56  * The known output values used for these tests were generated from
57  * GNU Octave, version 3.4.3
58  */
59  quo[0].cf[0] = -1369.500000, quo[0].cf[1] = -344.125000, quo[0].cf[2] = quo[0].cf[3] = quo[0].cf[4] = 0.000000; /* quotient coeff */
60  rem[0].cf[0] = -3313.375000, rem[0].cf[1] = 205834.750000, rem[0].cf[2] = 41708.250000, rem[0].cf[3] = 0.0; /* remainder coeff */
61 
62  /* initializes a 4th degree positive polynomial */
63  input[1].dgr = 4;
64 
65  input[1].cf[0] = 5478, input[1].cf[1] = 5485, input[1].cf[2] = 458, input[1].cf[3] = 258564, input[1].cf[4] = 54785;/* input coeff */
66 
67  return;
68 }
69 
70 
71 /* compares the values of the array and returns 0 if they all match */
72 int
73 check_results(fastf_t a[], fastf_t b[], int n)
74 {
75  int i;
76 
77  for (i = 0; i < n; i++) {
78  if (!EQUAL(a[i], b[i]))
79  return -1;
80  }
81 
82  return 0;
83 }
84 
85 
86 /* tests the polynomials to make sure bn_poly_mul() works properly. */
87 int
89 {
90  /* variables to store results for comparison */
91  int val1[2];
94 
95  bn_poly_synthetic_division(&q2, &r2, &input[1], &input[0]);
96 
97  /*checks the quotients */
98  val1[0] = check_results(q2.cf, quo[0].cf, quo[0].dgr + 1);
99  val1[1] = check_results(r2.cf, rem[0].cf, rem[0].dgr + 1);
100 
101  if (val1[0] == 0 && val1[1] == 0)
102  return val1[0];
103 
104  return -1;
105 }
106 
107 
108 int
109 main(void)
110 {
111  int ret;
112 
113  poly_init();
114  ret = test_bn_poly_syn_div();
115 
116  if (ret)
117  bu_log("[FAIL] %s test failed\n", __FILE__);
118  else
119  bu_log("[PASS] %s test passed\n", __FILE__);
120 
121  return ret;
122 }
123 
124 
125 /*
126  * Local Variables:
127  * mode: C
128  * tab-width: 8
129  * indent-tabs-mode: t
130  * c-file-style: "stroustrup"
131  * End:
132  * ex: shiftwidth=4 tabstop=8
133  */
fastf_t cf[BN_MAX_POLY_DEGREE+1]
Definition: poly.h:50
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int test_bn_poly_syn_div(void)
Header file for the BRL-CAD common definitions.
Definition: poly.h:47
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 check_results(fastf_t a[], fastf_t b[], int n)
size_t dgr
Definition: poly.h:49
bn_poly_t quo[1]
#define BN_POLY_INIT_ZERO
Definition: poly.h:55
int main(void)
bn_poly_t input[2]
void poly_init(void)
bn_poly_t rem[1]
double fastf_t
Definition: defines.h:300