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