Annotation of OpenXM_contrib2/asir2000/plot/calc.c, Revision 1.4
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.4 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/plot/calc.c,v 1.3 2000/08/22 05:04:30 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "ifplot.h"
52: #include <math.h>
53: #if PARI
54: #include "genpari.h"
55: #endif
56:
57: double usubstrp(P,double);
58:
1.4 ! noro 59: void calc(tab,can,nox)
1.1 noro 60: double **tab;
61: struct canvas *can;
1.4 ! noro 62: int nox;
1.1 noro 63: {
64: double x,y,xmin,ymin,xstep,ystep;
65: int ix,iy;
66: Real r,rx,ry;
67: Obj fr,g;
68: int w,h;
69: V vx,vy;
70: Obj t,s;
71:
1.4 ! noro 72: if ( !nox ) initmarker(can,"Evaluating...");
1.1 noro 73: MKReal(1.0,r); mulr(CO,(Obj)can->formula,(Obj)r,&fr);
74: vx = can->vx;
75: vy = can->vy;
76: w = can->width; h = can->height;
77: xmin = can->xmin; xstep = (can->xmax-can->xmin)/w;
78: ymin = can->ymin; ystep = (can->ymax-can->ymin)/h;
79: MKReal(1.0,rx); MKReal(1.0,ry); /* dummy real */
80: for( ix = 0, x = xmin; ix < w ; ix++, x += xstep ) {
81: #if 0
82: MKReal(x,r); substp(CO,fr,vx,(P)r,&g);
1.4 ! noro 83: if ( !nox ) marker(can,DIR_X,ix);
1.1 noro 84: for( iy = 0, y = ymin; iy < h ; iy++, y += ystep )
85: tab[ix][iy] = usubstrp(g,y);
86: #endif
87: BDY(rx) = x; substr(CO,0,fr,vx,x?(P)rx:0,&t); devalr(CO,t,&g);
1.4 ! noro 88: if ( !nox ) marker(can,DIR_X,ix);
1.1 noro 89: for( iy = 0, y = ymin; iy < h ; iy++, y += ystep ) {
90: BDY(ry) = y;
91: substr(CO,0,g,vy,y?(P)ry:0,&t); devalr(CO,t,&s);
92: tab[ix][iy] = ToReal(s);
93: }
94: }
95: }
96:
97: double usubstrp(p,r)
98: P p;
99: double r;
100: {
101: double t;
102: DCP dc;
103: int d;
104: double pwrreal0();
105:
106: if ( !p )
107: t = 0.0;
108: else if ( NUM(p) )
109: t = BDY((Real)p);
110: else {
111: dc = DC(p); t = BDY((Real)COEF(dc));
112: for ( d = QTOS(DEG(dc)), dc = NEXT(dc); dc;
113: d = QTOS(DEG(dc)), dc = NEXT(dc) ) {
114: t = t*pwrreal0(r,(d-QTOS(DEG(dc))))+BDY((Real)COEF(dc));
115: }
116: if ( d )
117: t *= pwrreal0(r,d);
118: }
119: return t;
120: }
121:
122: void qcalc(tab,can)
123: char **tab;
124: struct canvas *can;
125: {
126: Q dx,dy,w,h,xstep,ystep,c,q1,q2;
127: P g,g1,f1,f2,x,y,t,s;
128: DCP dc;
129: int ix,iy;
130: int *a,*pa;
131: char *px;
132: VECT ss;
133:
134:
135: subq(can->qxmax,can->qxmin,&dx); STOQ(can->width,w); divq(dx,w,&xstep);
136: subq(can->qymax,can->qymin,&dy); STOQ(can->height,h); divq(dy,h,&ystep);
137: MKV(can->vx,x); mulp(CO,(P)xstep,x,&t);
138: addp(CO,(P)can->qxmin,t,&s); substp(CO,can->formula,can->vx,s,&f1);
139: MKV(can->vy,y); mulp(CO,(P)ystep,y,&t);
140: addp(CO,(P)can->qymin,t,&s); substp(CO,f1,can->vy,s,&f2);
141: ptozp(f2,1,&c,&g);
142: a = (int *)ALLOCA((MAX(can->width,can->height)+1)*sizeof(int));
143: initmarker(can,"Horizontal scan...");
144: for( ix = 0; ix < can->width; ix++ ) {
145: marker(can,DIR_X,ix);
146: STOQ(ix,q1); substp(CO,g,can->vx,(P)q1,&t); ptozp(t,1,&c,&g1);
147: if ( !g1 )
148: for ( iy = 0; iy < can->height; iy++ )
149: tab[ix][iy] = 1;
150: else if ( !NUM(g1) ) {
151: strum(CO,g1,&ss); seproot(ss,0,can->height,a);
152: for ( iy = 0, pa = a; iy < can->height; iy++, pa++ )
153: if ( *pa < 0 || (*(pa+1) >= 0 && (*pa > *(pa+1))) )
154: tab[ix][iy] = 1;
155: }
156: }
157: initmarker(can,"Vertical scan...");
158: for( iy = 0; iy < can->height; iy++ ) {
159: marker(can,DIR_Y,iy);
160: STOQ(iy,q1); substp(CO,g,can->vy,(P)q1,&t); ptozp(t,1,&c,&g1);
161: if ( !g1 )
162: for ( ix = 0; ix < can->width; ix++ )
163: tab[ix][iy] = 1;
164: else if ( !NUM(g1) ) {
165: strum(CO,g1,&ss); seproot(ss,0,can->width,a);
166: for ( ix = 0, pa = a; ix < can->width; ix++, pa++ )
167: if ( *pa < 0 || (*(pa+1) >= 0 && (*pa > *(pa+1))) )
168: tab[ix][iy] = 1;
169: }
170: }
171: }
172:
173: strum(vl,p,rp)
174: VL vl;
175: P p;
176: VECT *rp;
177: {
178: P g1,g2,q,r,s;
179: P *t;
180: V v;
181: VECT ret;
182: int i,j;
183: Q a,b,c,d,h,l,m,x;
184:
185: v = VR(p); t = (P *)ALLOCA((deg(v,p)+1)*sizeof(P));
186: g1 = t[0] = p; diffp(vl,p,v,(P *)&a); ptozp((P)a,1,&c,&g2); t[1] = g2;
187: for ( i = 1, h = ONE, x = ONE; ; ) {
188: if ( NUM(g2) )
189: break;
190: subq(DEG(DC(g1)),DEG(DC(g2)),&d);
191: l = (Q)LC(g2);
192: if ( SGN(l) < 0 ) {
193: chsgnq(l,&a); l = a;
194: }
195: addq(d,ONE,&a); pwrq(l,a,&b); mulp(vl,(P)b,g1,(P *)&a);
196: divsrp(vl,(P)a,g2,&q,&r);
197: if ( !r )
198: break;
199: chsgnp(r,&s); r = s; i++;
200: if ( NUM(r) ) {
201: t[i] = r; break;
202: }
203: pwrq(h,d,&m); g1 = g2;
204: mulq(m,x,&a); divsp(vl,r,(P)a,&g2); t[i] = g2;
205: x = (Q)LC(g1);
206: if ( SGN(x) < 0 ) {
207: chsgnq(x,&a); x = a;
208: }
209: pwrq(x,d,&a); mulq(a,h,&b); divq(b,m,&h);
210: }
211: MKVECT(ret,i+1);
212: for ( j = 0; j <= i; j++ )
213: ret->body[j] = (pointer)t[j];
214: *rp = ret;
215: }
216:
217: seproot(s,min,max,ar)
218: VECT s;
219: int min,max;
220: int *ar;
221: {
222: P f;
223: P *ss;
224: Q q,t;
225: int i,j,k;
226:
227: ss = (P *)s->body; f = ss[0];
228: for ( i = min; i <= max; i++ ) {
229: STOQ(i,q); usubstqp(f,q,&t);
230: if ( !t )
231: ar[i] = -1;
232: else {
233: ar[i] = numch(s,q,t); break;
234: }
235: }
236: if ( i > max )
237: return;
238: for ( j = max; j >= min; j-- ) {
239: STOQ(j,q); usubstqp(f,q,&t);
240: if ( !t )
241: ar[j] = -1;
242: else {
243: if ( i != j )
244: ar[j] = numch(s,q,t);
245: break;
246: }
247: }
248: if ( j <= i+1 )
249: return;
250: if ( ar[i] == ar[j] ) {
251: for ( k = i+1; k < j; k++ )
252: ar[k] = ar[i];
253: return;
254: }
255: k = (i+j)/2;
256: seproot(s,i,k,ar);
257: seproot(s,k,j,ar);
258: }
259:
260: numch(s,n,a0)
261: VECT s;
262: Q n,a0;
263: {
264: int len,i,c;
265: Q a;
266: P *ss;
267:
268: len = s->len; ss = (P *)s->body;
269: for ( i = 1, c = 0; i < len; i++ ) {
270: usubstqp(ss[i],n,&a);
271: if ( a ) {
272: if ( (SGN(a)>0 && SGN(a0)<0) || (SGN(a)<0 && SGN(a0)>0) )
273: c++;
274: a0 = a;
275: }
276: }
277: return c;
278: }
279:
280: usubstqp(p,r,v)
281: P p;
282: Q r;
283: Q *v;
284: {
285: Q d,d1,a,b,t;
286: DCP dc;
287:
288: if ( !p )
289: *v = 0;
290: else if ( NUM(p) )
291: *v = (Q)p;
292: else {
293: dc = DC(p); t = (Q)COEF(dc);
294: for ( d = DEG(dc), dc = NEXT(dc); dc;
295: d = DEG(dc), dc = NEXT(dc) ) {
296: subq(d,DEG(dc),&d1); pwrq(r,d1,&a);
297: mulq(t,a,&b); addq(b,(Q)COEF(dc),&t);
298: }
299: if ( d ) {
300: pwrq(r,d,&a); mulq(t,a,&b); t = b;
301: }
302: *v = t;
303: }
304: }
305:
306: void plotcalc(can)
307: struct canvas *can;
308: {
309: double x,xmin,xstep,ymax,ymin,dy;
310: int ix;
311: Real r;
312: Obj fr;
313: double usubstrp();
314: int w,h;
315: double *tab;
316: POINT *pa;
317: Real rx;
318: Obj t,s;
319:
320: MKReal(1.0,r); mulr(CO,(Obj)can->formula,(Obj)r,&fr);
321: w = can->width; h = can->height;
322: xmin = can->xmin; xstep = (can->xmax-can->xmin)/w;
323: tab = (double *)ALLOCA(w*sizeof(double));
324: MKReal(1,rx); /* dummy real number */
325: for( ix = 0, x = xmin; ix < w ; ix++, x += xstep ) {
326: /* full substitution */
327: BDY(rx) = x;
328: substr(CO,0,fr,can->vx,x?(P)rx:0,&s); devalr(CO,(Obj)s,&t);
329: if ( t && (OID(t)!=O_N || NID((Num)t)!=N_R) )
330: error("plotcalc : invalid evaluation");
331: tab[ix] = ToReal((Num)t);
332: #if 0
333: tab[ix] = usubstrp(fr,x);
334: #endif
335: }
336: if ( can->ymax == can->ymin ) {
337: for ( ymax = ymin = tab[0], ix = 1; ix < w; ix++ ) {
338: if ( tab[ix] > ymax )
339: ymax = tab[ix];
340: if ( tab[ix] < ymin )
341: ymin = tab[ix];
342: }
343: can->ymax = ymax; can->ymin = ymin;
344: } else {
345: ymax = can->ymax; ymin = can->ymin;
346: }
347: dy = ymax-ymin;
348: can->pa = (struct pa *)MALLOC(sizeof(struct pa));
349: can->pa[0].length = w;
350: can->pa[0].pos = pa = (POINT *)MALLOC(w*sizeof(POINT));
351: for ( ix = 0; ix < w; ix++ ) {
352: #ifndef MAXSHORT
353: #define MAXSHORT ((short)0x7fff)
354: #endif
355: double t;
356:
357: XC(pa[ix]) = ix;
358: t = (h - 1)*(ymax - tab[ix])/dy;
359: if ( t > MAXSHORT )
360: YC(pa[ix]) = MAXSHORT;
361: else if ( t < -MAXSHORT )
362: YC(pa[ix]) = -MAXSHORT;
363: else
364: YC(pa[ix]) = t;
365: }
366: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>