multipoly.c

Go to the documentation of this file.
00001 /*                         M U L T I P O L Y . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2012 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * version 2.1 as published by the Free Software Foundation.
00010  *
00011  * This library is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this file; see the file named COPYING for more
00018  * information.
00019  */
00020 /** @addtogroup multipoly */
00021 /** @{ */
00022 /** @file libbn/multipoly.c
00023  *
00024  *      Library for dealing with bivariate polynomials.
00025  *
00026  */
00027 
00028 #include "common.h"
00029 
00030 #include <stdio.h>
00031 #include <math.h>
00032 #include <signal.h>
00033 
00034 #include "bu.h"
00035 #include "vmath.h"
00036 #include "bn.h"
00037 
00038 
00039 #define FAILSTR "failure in multipoly.c"
00040 /* the Max and Min macros in vmath.h are destructive and not suited for here, should this be
00041  * added somewhere else? */
00042 #define Max(a, b) (((a) > (b)) ? (a) : (b))
00043 #define Min(a, b) (((a) > (b)) ? (a) : (b))
00044 
00045 /**
00046  *        bn_multipoly_new
00047  *
00048  * @brief create new multipoly of a given size with coefficients set to 0
00049  */
00050 struct bn_multipoly *
00051 bn_multipoly_new(int dgrs, int dgrt)
00052 {
00053     struct bn_multipoly *newmp = bu_malloc(sizeof(struct bn_multipoly), FAILSTR);
00054     int    i, s, t;
00055 
00056     newmp->cf = bu_malloc(dgrs * sizeof(double *), FAILSTR);
00057 
00058     for (i = 0; i < dgrs; i++) {
00059         newmp->cf[i] = bu_malloc(dgrt * sizeof(double), FAILSTR);
00060     }
00061 
00062     newmp->dgrs = dgrs;
00063     newmp->dgrt = dgrt;
00064     for (s = 0; s < dgrs; s++) {
00065         for (t = 0; t < dgrt; t++) {
00066             newmp->cf[s][t] = 0;
00067         }
00068     }
00069     return newmp;
00070 }
00071 
00072 /**
00073  *        bn_multipoly_grow
00074  *
00075  * @brief grow the cf array to be at least [dgrx][dgry], sets new entries to 0
00076  */
00077 struct bn_multipoly *
00078 bn_multipoly_grow(register struct bn_multipoly *P, int dgrs, int dgrt)
00079 {
00080     int i, j;
00081     if (dgrs > P->dgrs) {
00082         P->cf = bu_realloc(P->cf, dgrs * sizeof(double *), FAILSTR);
00083         for (i = P->dgrs; i < dgrs; i++) {
00084             P->cf[i] = bu_malloc(Max(P->dgrt, dgrt) * sizeof(double), FAILSTR);
00085             for (j = 0; j < Max(P->dgrt, dgrt); j++) {
00086                 P->cf[i][j] = 0;
00087             }
00088         }
00089     }
00090     if (dgrt > P->dgrt) {
00091         for (i = 0; i < P->dgrt; i++) {
00092             P->cf[i] = bu_realloc(P->cf, dgrt * sizeof(double *), FAILSTR);
00093             for (j = P->dgrt; j < dgrt; j++) {
00094                 P->cf[i][j] = 0;
00095             }
00096         }
00097     }
00098     return P;
00099 }
00100 
00101 /**
00102  *        bn_multipoly_set
00103  *
00104  * @brief set a coefficient growing cf array if needed
00105  */
00106 struct bn_multipoly *
00107 bn_multipoly_set(register struct bn_multipoly *P, int s, int t, double val)
00108 {
00109     bn_multipoly_grow(P, s + 1, t + 1);
00110     P->cf[s][t] = val;
00111     return P;
00112 }
00113 
00114 /**
00115  *        bn_multipoly_add
00116  *
00117  * @brief add two polynomials
00118  */
00119 struct bn_multipoly *
00120 bn_multpoly_add(register struct bn_multipoly *p1, register struct bn_multipoly *p2)
00121 {
00122     struct bn_multipoly *sum = bn_multipoly_new(Max(p1->dgrs, p2->dgrs), Max(p1->dgrt, p2->dgrs));
00123     int s, t;
00124     for (s = 0; s < sum->dgrs; s++) {
00125         for (t = 0; t < sum->dgrt; t++) {
00126             sum->cf[s][t] = p1->cf[s][t] + p2->cf[s][t];
00127         }
00128     }
00129     return sum;
00130 }
00131 
00132 /**
00133  *        bn_multipoly_mul
00134  *
00135  * @brief multiply two polynomials
00136  */
00137 
00138 struct bn_multipoly *
00139 bn_multipoly_mul(register struct bn_multipoly *p1, register struct bn_multipoly *p2)
00140 {
00141     struct bn_multipoly *product = bn_multipoly_new(p1->dgrs + p2->dgrs, p1->dgrt + p2->dgrt);
00142     int s1,s2,t1,t2;
00143     for (s1 = 0; s1 < p1->dgrs; s1++) {
00144         for (t1 = 0; t1 < p1->dgrt; t1++) {
00145             for (s2 = 0; s2 < p2->dgrs; s2++) {
00146                 for (t2 = 0; t2 < p2->dgrt; p2++) {
00147                     product->cf[s1 + s2][t1 + t2] = p1->cf[s1][t1] * p2->cf[s2][t2];
00148                 }
00149             }
00150         }
00151     }
00152     return product;
00153 }
00154 
00155 /** @} */
00156 /*
00157  * Local Variables:
00158  * mode: C
00159  * tab-width: 8
00160  * indent-tabs-mode: t
00161  * c-file-style: "stroustrup"
00162  * End:
00163  * ex: shiftwidth=4 tabstop=8
00164  */
Generated on Tue Dec 11 13:14:27 2012 for LIBBN by  doxygen 1.6.3