BRL-CAD
bn_poly_sub.c
Go to the documentation of this file.
1 /* T E S T _ B N _ P O L Y _ S U B . 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 
22 #include "common.h"
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <string.h>
28 #include <signal.h>
29 
30 #include "bu.h"
31 #include "vmath.h"
32 #include "bn.h"
33 
34 /* holds three polynomials to be used in test. */
36 
37 struct bn_poly bn_Zero_poly = { BN_POLY_MAGIC, 0, {0.0} };
38 
39 /**
40  * Initialises polynomial storing negative, positive and zero coefficients.
41  */
42 void
43 poly_init(void)
44 {
45  /* stores zero-value coefficients to polynomial for input and output. */
46  output[0] = bn_Zero_poly;
47  input[0] = bn_Zero_poly;
48  input[0].dgr = 2;
49  input[0].cf[0] = input[0].cf[1] = input[0].cf[2] = 0.0;
50 
51  output[0].dgr = 2;
52  output[0].cf[0] = output[0].cf[1] = output[0].cf[2] = 0.0;
53 
54  /* stores negative coefficients to polynomial. */
55  output[1] = bn_Zero_poly;
56  input[1] = bn_Zero_poly;
57  input[1].dgr = 2;
58  output[1].dgr = 2;
59 
60  input[1].cf[0] = -4853;
61  input[1].cf[1] = -324;
62  input[1].cf[2] = -275;
63 
64  /**
65  * The known values used for these tests are generated from
66  * GNU Octave, version 3.4.3
67  */
68  output[1].cf[0] = -9706;
69  output[1].cf[1] = -648;
70  output[1].cf[2] = -550;
71 
72  /* stores positive coefficients to to polynomial input. */
73  output[2] = bn_Zero_poly;
74  input[2] = bn_Zero_poly;
75  input[2].dgr = 2;
76  output[2].dgr = 2;
77 
78  input[2].cf[0] = 61685316;
79  input[2].cf[1] = 33552288;
80  input[2].cf[2] = 27339096;
81 
82  output[2].cf[0] = 61695022;
83  output[2].cf[1] = 33552936;
84  output[2].cf[2] = 27339646;
85 
86  return;
87 }
88 
89 
90 /* compares the values of the array and returns 0. */
91 int
92 check_results(fastf_t a[], fastf_t b[], int n)
93 {
94  int i;
95 
96  for (i = 0; i < n; i++) {
97  if (!EQUAL(a[i], b[i]))
98  return -1;
99  }
100 
101  return 0;
102 }
103 
104 
105 /* tests the polynomials to make sure bn_poly_add() works properly. */
106 int
108 {
109  int val, val1, val2;
110  bn_poly_t a, b, c;
111  a = bn_Zero_poly, b = bn_Zero_poly, c = bn_Zero_poly;
112 
113  bn_poly_sub(&a, &input[0], &input[0]);
114  bn_poly_sub(&b, &input[1], &input[0]);
115  bn_poly_sub(&c, &input[2], &input[1]);
116 
117  val = check_results(a.cf, output[0].cf, output[0].dgr + 1);
118  val1 = check_results(b.cf, output[1].cf, output[1].dgr + 1);
119  val2 = check_results(c.cf, output[2].cf, output[2].dgr + 1);
120 
121  if (val == 0 && val1 == 0 && val2 == 0)
122  return val;
123 
124  return -1;
125 }
126 
127 
128 int
129 main(void)
130 {
131  int ret;
132  poly_init();
133  ret = test_bn_poly_sub();
134 
135  if (ret == 0) {
136 
137  bu_log("Function computes correctly\n");
138  exit(EXIT_SUCCESS);
139  } else
140  exit(EXIT_FAILURE);
141 
142  return 0;
143 }
144 
145 
146 /*
147  * Local Variables:
148  * mode: C
149  * tab-width: 8
150  * indent-tabs-mode: t
151  * c-file-style: "stroustrup"
152  * End:
153  * ex: shiftwidth=4 tabstop=8
154  */
Definition: db_flip.c:35
bn_poly_t output[3]
Definition: bn_poly_sub.c:35
fastf_t cf[BN_MAX_POLY_DEGREE+1]
Definition: poly.h:50
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
struct bn_poly * bn_poly_sub(struct bn_poly *diff, const struct bn_poly *poly1, const struct bn_poly *poly2)
subtract two polynomials
void poly_init(void)
Definition: bn_poly_sub.c:43
int check_results(fastf_t a[], fastf_t b[], int n)
Definition: bn_poly_sub.c:92
Header file for the BRL-CAD common definitions.
Definition: poly.h:47
#define BN_POLY_MAGIC
Definition: magic.h:70
size_t dgr
Definition: poly.h:49
int main(void)
Definition: bn_poly_sub.c:129
struct bn_poly bn_Zero_poly
Definition: bn_poly_sub.c:37
int test_bn_poly_sub(void)
Definition: bn_poly_sub.c:107
bn_poly_t input[3]
Definition: bn_poly_sub.c:35
double fastf_t
Definition: defines.h:300