BRL-CAD
bn_poly_scale.c
Go to the documentation of this file.
1 /* T E S T _ B N _ P O L Y _ S C A L E . 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. */
36 static bn_poly_t input[3], output[3];
37 
38 
39 /**
40  * Initialises polynomial storing a negative, positive and zero coefficients.
41  */
42 void
43 poly_init(void)
44 {
45  struct bn_poly bn_Zero_poly = { BN_POLY_MAGIC, 0, {0.0} };
46 
47  /* stores 0 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] = -4;
63  input[1].cf[1] = -3;
64  input[1].cf[2] = -2;
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] = -8000;
71  output[1].cf[1] = -6000;
72  output[1].cf[2] = -4000;
73 
74 
75  /* stores positive coefficients to to polynomial input. */
76  output[2] = bn_Zero_poly;
77  input[2] = bn_Zero_poly;
78  input[2].dgr = 2;
79  output[2].dgr = 2;
80 
81  input[2].cf[0] = 7854;
82  input[2].cf[1] = 2136;
83  input[2].cf[2] = 1450;
84 
85  output[2].cf[0] = -3141600;
86  output[2].cf[1] = -854400;
87  output[2].cf[2] = -580000;
88 
89  return;
90 }
91 
92 
93 /* compares the values of the array and returns 0. */
94 int
95 check_results(fastf_t a[], fastf_t b[], int n)
96 {
97  int i;
98 
99  for (i = 0; i < n; i++) {
100  if (!EQUAL(a[i], b[i]))
101  return -1;
102  }
103 
104  return 0;
105 }
106 
107 
108 /* tests the polynomials to make sure bn_poly_mul() works properly. */
109 int
111 {
112  int val, val1, val2;
113 
114  bn_poly_scale(&input[0], 0);
115  bn_poly_scale(&input[1], 2000);
116  bn_poly_scale(&input[2], -400);
117 
118  val = check_results(input[0].cf, output[0].cf, output[0].dgr + 1);
119  val1 = check_results(input[1].cf, output[1].cf, output[1].dgr + 1);
120  val2 = check_results(input[2].cf, output[2].cf, output[2].dgr + 1);
121 
122  if (val == 0 && val1 == 0 && val2 == 0)
123  return val;
124 
125  return -1;
126 }
127 
128 
129 int
130 main(void)
131 {
132  int ret;
133  poly_init();
134 
135  ret = test_bn_poly_scale();
136 
137  if (ret == 0) {
138  bu_log("\nFunction computes correctly\n");
139  return EXIT_SUCCESS;
140  }
141 
142  return EXIT_FAILURE;
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
fastf_t cf[BN_MAX_POLY_DEGREE+1]
Definition: poly.h:50
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Header file for the BRL-CAD common definitions.
Definition: poly.h:47
#define BN_POLY_MAGIC
Definition: magic.h:70
int main(void)
size_t dgr
Definition: poly.h:49
struct bn_poly * bn_poly_scale(struct bn_poly *eqn, double factor)
scale a polynomial
void poly_init(void)
Definition: bn_poly_scale.c:43
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_scale.c:95
int test_bn_poly_scale(void)
double fastf_t
Definition: defines.h:300