1 /*
2  * poly - polynomial functions
3  *
4  * Copyright (C) 1999,2021  Ernest Bowen and Landon Curt Noll
5  *
6  * Primary author:  Ernest Bowen
7  *
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.
11  *
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.
16  *
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.
21  *
22  * Under source code control:	1995/12/02 03:10:28
23  * File existed as early as:	1995
24  *
25  * chongo <was here> /\oo/\	http://www.isthe.com/chongo/
26  * Share and enjoy!  :-)	http://www.isthe.com/chongo/tech/comp/calc/
27  */
28 
29 
30 #include "value.h"
31 
32 
33 #include "banned.h"	/* include after system header <> includes */
34 
35 
36 BOOL
evp(LISTELEM * cp,LISTELEM * x,VALUE * vres)37 evp(LISTELEM *cp, LISTELEM *x, VALUE *vres)
38 {
39 	VALUE v, tmp1, tmp2;
40 	BOOL s;
41 
42 	s = FALSE;
43 	while (cp) {
44 		if (s) {
45 			mulvalue(vres, &x->e_value, &tmp1);
46 			freevalue(vres);
47 			*vres = tmp1;
48 		}
49 		v = cp->e_value;
50 		if (v.v_type == V_LIST) {
51 			if (evalpoly(v.v_list, x->e_next, &tmp1)) {
52 				if (s) {
53 					addvalue(&tmp1, vres, &tmp2);
54 					freevalue(&tmp1);
55 					freevalue(vres);
56 					*vres = tmp2;
57 				} else {
58 					s = TRUE;
59 					*vres = tmp1;
60 				}
61 			}
62 		} else {
63 			if (s) {
64 				addvalue(&v, vres, &tmp1);
65 				freevalue(vres);
66 				*vres = tmp1;
67 			} else {
68 				s = TRUE;
69 				copyvalue(&v, vres);
70 			}
71 		}
72 		cp = cp->e_prev;
73 	}
74 	return s;
75 }
76 
77 
78 BOOL
evalpoly(LIST * clist,LISTELEM * x,VALUE * vres)79 evalpoly(LIST *clist, LISTELEM *x, VALUE *vres)
80 {
81 	LISTELEM *cp;
82 	VALUE v;
83 
84 	cp = clist->l_first;
85 	if (cp == NULL)
86 		return FALSE;
87 	if (x == NULL) {
88 		v = cp->e_value;
89 		if (v.v_type == V_LIST)
90 			return evalpoly(v.v_list, x->e_next, vres);
91 		copyvalue(&v, vres);
92 		return TRUE;
93 	}
94 	return evp(clist->l_last, x, vres);
95 }
96 
97 void
insertitems(LIST * lp1,LIST * lp2)98 insertitems(LIST *lp1, LIST *lp2)
99 {
100 	LISTELEM *ep;
101 
102 	for (ep = lp2->l_first; ep; ep = ep->e_next) {
103 		if (ep->e_value.v_type == V_LIST)
104 			insertitems(lp1, ep->e_value.v_list);
105 		else
106 			insertlistlast(lp1, &ep->e_value);
107 	}
108 }
109 
110 
111 long
countlistitems(LIST * lp)112 countlistitems(LIST *lp)
113 {
114 	LISTELEM *ep;
115 
116 	long n = 0;
117 	for (ep = lp->l_first; ep; ep = ep->e_next) {
118 		if (ep->e_value.v_type == V_LIST)
119 			n += countlistitems(ep->e_value.v_list);
120 		else
121 			n++;
122 	}
123 	return n;
124 }
125 
126 
127 void
addlistitems(LIST * lp,VALUE * vres)128 addlistitems(LIST *lp, VALUE *vres)
129 {
130 	LISTELEM *ep;
131 	VALUE tmp;
132 
133 	for (ep = lp->l_first; ep; ep = ep->e_next) {
134 		addvalue(vres, &ep->e_value, &tmp);
135 		freevalue(vres);
136 		*vres = tmp;
137 		if (vres->v_type < 0)
138 			return;
139 	}
140 }
141 
142 void
addlistinv(LIST * lp,VALUE * vres)143 addlistinv(LIST *lp, VALUE *vres)
144 {
145 	LISTELEM *ep;
146 	VALUE tmp1, tmp2;
147 
148 	for (ep = lp->l_first; ep; ep = ep->e_next) {
149 		if (ep->e_value.v_type == V_LIST) {
150 			addlistinv(ep->e_value.v_list, vres);
151 		} else {
152 			invertvalue(&ep->e_value, &tmp1);
153 			addvalue(vres, &tmp1, &tmp2);
154 			freevalue(&tmp1);
155 			freevalue(vres);
156 			*vres = tmp2;
157 		}
158 		if (vres->v_type < 0)
159 			return;
160 	}
161 }
162