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