BRL-CAD
multipoly.c
Go to the documentation of this file.
1 /* M U L T I P O L Y . C
2  * BRL-CAD
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
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 /** @addtogroup poly */
22 /** @{ */
23 /** @file libbn/multipoly.c
24  *
25  * Library for dealing with bivariate polynomials.
26  *
27  */
28 
29 #include "common.h"
30 
31 #include <stdio.h>
32 #include <math.h>
33 #include <signal.h>
34 
35 #include "bu/malloc.h"
36 #include "vmath.h"
37 #include "bn/multipoly.h"
38 
39 
40 #define FAILSTR "failure in multipoly.c"
41 /* the Max and Min macros in vmath.h are destructive and not suited for here, should this be
42  * added somewhere else? */
43 #define Max(a, b) (((a) > (b)) ? (a) : (b))
44 #define Min(a, b) (((a) > (b)) ? (a) : (b))
45 
46 /**
47  * bn_multipoly_new
48  *
49  * @brief create new multipoly of a given size with coefficients set to 0
50  */
51 struct bn_multipoly *
53 {
54  struct bn_multipoly *newmp;
55  int i, s, t;
56 
57  BU_ALLOC(newmp, struct bn_multipoly);
58  newmp->cf = (double **)bu_malloc(dgrs * sizeof(double *), FAILSTR);
59 
60  for (i = 0; i < dgrs; i++) {
61  newmp->cf[i] = (double *)bu_malloc(dgrt * sizeof(double), FAILSTR);
62  }
63 
64  newmp->dgrs = dgrs;
65  newmp->dgrt = dgrt;
66  for (s = 0; s < dgrs; s++) {
67  for (t = 0; t < dgrt; t++) {
68  newmp->cf[s][t] = 0;
69  }
70  }
71  return newmp;
72 }
73 
74 /**
75  * bn_multipoly_grow
76  *
77  * @brief grow the cf array to be at least [dgrx][dgry], sets new entries to 0
78  */
79 struct bn_multipoly *
80 bn_multipoly_grow(register struct bn_multipoly *P, int dgrs, int dgrt)
81 {
82  int i, j;
83  if (dgrs > P->dgrs) {
84  P->cf = (double **)bu_realloc(P->cf, dgrs * sizeof(double *), FAILSTR);
85  for (i = P->dgrs; i < dgrs; i++) {
86  P->cf[i] = (double *)bu_malloc(Max(P->dgrt, dgrt) * sizeof(double), FAILSTR);
87  for (j = 0; j < Max(P->dgrt, dgrt); j++) {
88  P->cf[i][j] = 0;
89  }
90  }
91  }
92  if (dgrt > P->dgrt) {
93  for (i = 0; i < P->dgrt; i++) {
94  P->cf[i] = (double *)bu_realloc(P->cf, dgrt * sizeof(double *), FAILSTR);
95  for (j = P->dgrt; j < dgrt; j++) {
96  P->cf[i][j] = 0;
97  }
98  }
99  }
100  return P;
101 }
102 
103 /**
104  * bn_multipoly_set
105  *
106  * @brief set a coefficient growing cf array if needed
107  */
108 struct bn_multipoly *
109 bn_multipoly_set(register struct bn_multipoly *P, int s, int t, double val)
110 {
111  bn_multipoly_grow(P, s + 1, t + 1);
112  P->cf[s][t] = val;
113  return P;
114 }
115 
116 /**
117  * bn_multipoly_add
118  *
119  * @brief add two polynomials
120  */
121 struct bn_multipoly *
122 bn_multipoly_add(register struct bn_multipoly *p1, register struct bn_multipoly *p2)
123 {
124  struct bn_multipoly *sum = bn_multipoly_new(Max(p1->dgrs, p2->dgrs), Max(p1->dgrt, p2->dgrs));
125  int s, t;
126  for (s = 0; s < sum->dgrs; s++) {
127  for (t = 0; t < sum->dgrt; t++) {
128  sum->cf[s][t] = p1->cf[s][t] + p2->cf[s][t];
129  }
130  }
131  return sum;
132 }
133 
134 /**
135  * bn_multipoly_mul
136  *
137  * @brief multiply two polynomials
138  */
139 
140 struct bn_multipoly *
141 bn_multipoly_mul(register struct bn_multipoly *p1, register struct bn_multipoly *p2)
142 {
143  struct bn_multipoly *product = bn_multipoly_new(p1->dgrs + p2->dgrs, p1->dgrt + p2->dgrt);
144  int s1,s2,t1,t2;
145  for (s1 = 0; s1 < p1->dgrs; s1++) {
146  for (t1 = 0; t1 < p1->dgrt; t1++) {
147  for (s2 = 0; s2 < p2->dgrs; s2++) {
148  for (t2 = 0; t2 < p2->dgrt; p2++) {
149  product->cf[s1 + s2][t1 + t2] = p1->cf[s1][t1] * p2->cf[s2][t2];
150  }
151  }
152  }
153  }
154  return product;
155 }
156 
157 /** @} */
158 /*
159  * Local Variables:
160  * mode: C
161  * tab-width: 8
162  * indent-tabs-mode: t
163  * c-file-style: "stroustrup"
164  * End:
165  * ex: shiftwidth=4 tabstop=8
166  */
Definition: db_flip.c:35
#define FAILSTR
Definition: multipoly.c:40
Polynomial data type.
Definition: multipoly.h:38
if lu s
Definition: nmg_mod.c:3860
Header file for the BRL-CAD common definitions.
struct bn_multipoly * bn_multipoly_mul(register struct bn_multipoly *p1, register struct bn_multipoly *p2)
multiply two polynomials
Definition: multipoly.c:141
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
#define Max(a, b)
Definition: multipoly.c:43
struct bn_multipoly * bn_multipoly_set(register struct bn_multipoly *P, int s, int t, double val)
set a coefficient growing cf array if needed
Definition: multipoly.c:109
struct bn_multipoly * bn_multipoly_add(register struct bn_multipoly *p1, register struct bn_multipoly *p2)
add two polynomials
Definition: multipoly.c:122
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
struct bn_multipoly * bn_multipoly_new(int dgrs, int dgrt)
create new multipoly of a given size with coefficients set to 0
Definition: multipoly.c:52
void * bu_realloc(void *ptr, size_t siz, const char *str)
double ** cf
Definition: multipoly.h:42
struct bn_multipoly * bn_multipoly_grow(register struct bn_multipoly *P, int dgrs, int dgrt)
grow the cf array to be at least [dgrx][dgry], sets new entries to 0
Definition: multipoly.c:80