modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / etc / calc / poly.c
bloba179e6d85f6903443e3e8e5ef962eb5ef326a7b6
1 /*
2 * poly - polynomial functions
4 * Copyright (C) 1999 Ernest Bowen and Landon Curt Noll
6 * Primary author: Ernest Bowen
8 * Calc is open software; you can redistribute it and/or modify it under
9 * the terms of the version 2.1 of the GNU Lesser General Public License
10 * as published by the Free Software Foundation.
12 * Calc is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15 * Public License for more details.
17 * A copy of version 2.1 of the GNU Lesser General Public License is
18 * distributed with calc under the filename COPYING-LGPL. You should have
19 * received a copy with calc; if not, write to Free Software Foundation, Inc.
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * @(#) $Revision: 30.1 $
23 * @(#) $Id: poly.c,v 30.1 2007/03/16 11:09:46 chongo Exp $
24 * @(#) $Source: /usr/local/src/bin/calc/RCS/poly.c,v $
26 * Under source code control: 1995/12/02 03:10:28
27 * File existed as early as: 1995
29 * chongo <was here> /\oo/\ http://www.isthe.com/chongo/
30 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
34 #include "value.h"
37 BOOL
38 evp(LISTELEM *cp, LISTELEM *x, VALUE *vres)
40 VALUE v, tmp1, tmp2;
41 BOOL s;
43 s = FALSE;
44 while (cp) {
45 if (s) {
46 mulvalue(vres, &x->e_value, &tmp1);
47 freevalue(vres);
48 *vres = tmp1;
50 v = cp->e_value;
51 if (v.v_type == V_LIST) {
52 if (evalpoly(v.v_list, x->e_next, &tmp1)) {
53 if (s) {
54 addvalue(&tmp1, vres, &tmp2);
55 freevalue(&tmp1);
56 freevalue(vres);
57 *vres = tmp2;
58 } else {
59 s = TRUE;
60 *vres = tmp1;
63 } else {
64 if (s) {
65 addvalue(&v, vres, &tmp1);
66 freevalue(vres);
67 *vres = tmp1;
68 } else {
69 s = TRUE;
70 copyvalue(&v, vres);
73 cp = cp->e_prev;
75 return s;
79 BOOL
80 evalpoly(LIST *clist, LISTELEM *x, VALUE *vres)
82 LISTELEM *cp;
83 VALUE v;
85 cp = clist->l_first;
86 if (cp == NULL)
87 return FALSE;
88 if (x == NULL) {
89 v = cp->e_value;
90 if (v.v_type == V_LIST)
91 return evalpoly(v.v_list, x->e_next, vres);
92 copyvalue(&v, vres);
93 return TRUE;
95 return evp(clist->l_last, x, vres);
98 void
99 insertitems(LIST *lp1, LIST *lp2)
101 LISTELEM *ep;
103 for (ep = lp2->l_first; ep; ep = ep->e_next) {
104 if (ep->e_value.v_type == V_LIST)
105 insertitems(lp1, ep->e_value.v_list);
106 else
107 insertlistlast(lp1, &ep->e_value);
112 long
113 countlistitems(LIST *lp)
115 LISTELEM *ep;
117 long n = 0;
118 for (ep = lp->l_first; ep; ep = ep->e_next) {
119 if (ep->e_value.v_type == V_LIST)
120 n += countlistitems(ep->e_value.v_list);
121 else
122 n++;
124 return n;
128 void
129 addlistitems(LIST *lp, VALUE *vres)
131 LISTELEM *ep;
132 VALUE tmp;
134 for (ep = lp->l_first; ep; ep = ep->e_next) {
135 addvalue(vres, &ep->e_value, &tmp);
136 freevalue(vres);
137 *vres = tmp;
138 if (vres->v_type < 0)
139 return;
143 void
144 addlistinv(LIST *lp, VALUE *vres)
146 LISTELEM *ep;
147 VALUE tmp1, tmp2;
149 for (ep = lp->l_first; ep; ep = ep->e_next) {
150 if (ep->e_value.v_type == V_LIST) {
151 addlistinv(ep->e_value.v_list, vres);
152 } else {
153 invertvalue(&ep->e_value, &tmp1);
154 addvalue(vres, &tmp1, &tmp2);
155 freevalue(&tmp1);
156 freevalue(vres);
157 *vres = tmp2;
159 if (vres->v_type < 0)
160 return;