[BACK]Return to calc.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / plot

Annotation of OpenXM_contrib2/asir2000/plot/calc.c, Revision 1.5

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.5     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/plot/calc.c,v 1.4 2001/08/22 09:19:21 noro Exp $
1.2       noro       49: */
1.1       noro       50: #include "ca.h"
1.5     ! noro       51: #include "parse.h"
1.1       noro       52: #include "ifplot.h"
                     53: #include <math.h>
                     54: #if PARI
                     55: #include "genpari.h"
                     56: #endif
                     57:
1.5     ! noro       58: void calc(double **tab,struct canvas *can,int nox)
1.1       noro       59: {
                     60:        double x,y,xmin,ymin,xstep,ystep;
                     61:        int ix,iy;
                     62:        Real r,rx,ry;
                     63:        Obj fr,g;
                     64:        int w,h;
                     65:        V vx,vy;
                     66:        Obj t,s;
                     67:
1.4       noro       68:        if ( !nox ) initmarker(can,"Evaluating...");
1.1       noro       69:        MKReal(1.0,r); mulr(CO,(Obj)can->formula,(Obj)r,&fr);
                     70:        vx = can->vx;
                     71:        vy = can->vy;
                     72:        w = can->width; h = can->height;
                     73:        xmin = can->xmin; xstep = (can->xmax-can->xmin)/w;
                     74:        ymin = can->ymin; ystep = (can->ymax-can->ymin)/h;
                     75:        MKReal(1.0,rx); MKReal(1.0,ry); /* dummy real */
                     76:        for( ix = 0, x = xmin; ix < w ; ix++, x += xstep ) {
                     77: #if 0
                     78:                MKReal(x,r); substp(CO,fr,vx,(P)r,&g);
1.4       noro       79:                if ( !nox ) marker(can,DIR_X,ix);
1.1       noro       80:                for( iy = 0, y = ymin; iy < h ; iy++, y += ystep )
                     81:                        tab[ix][iy] = usubstrp(g,y);
                     82: #endif
1.5     ! noro       83:                BDY(rx) = x; substr(CO,0,fr,vx,x?(Obj)rx:0,&t);
        !            84:                devalr(CO,t,&g);
1.4       noro       85:                if ( !nox ) marker(can,DIR_X,ix);
1.1       noro       86:                for( iy = 0, y = ymin; iy < h ; iy++, y += ystep ) {
                     87:                        BDY(ry) = y;
1.5     ! noro       88:                        substr(CO,0,g,vy,y?(Obj)ry:0,&t);
        !            89:                        devalr(CO,t,&s);
1.1       noro       90:                        tab[ix][iy] = ToReal(s);
                     91:                }
                     92:        }
                     93: }
                     94:
1.5     ! noro       95: double usubstrp(P p,double r)
1.1       noro       96: {
                     97:        double t;
                     98:        DCP dc;
                     99:        int d;
                    100:        double pwrreal0();
                    101:
                    102:        if ( !p )
                    103:                t = 0.0;
                    104:        else if ( NUM(p) )
                    105:                t = BDY((Real)p);
                    106:        else {
                    107:                dc = DC(p); t = BDY((Real)COEF(dc));
                    108:                for ( d = QTOS(DEG(dc)), dc = NEXT(dc); dc;
                    109:                        d = QTOS(DEG(dc)), dc = NEXT(dc) ) {
                    110:                        t = t*pwrreal0(r,(d-QTOS(DEG(dc))))+BDY((Real)COEF(dc));
                    111:                }
                    112:                if ( d )
                    113:                        t *= pwrreal0(r,d);
                    114:        }
                    115:        return t;
                    116: }
                    117:
1.5     ! noro      118: void qcalc(char **tab,struct canvas *can)
1.1       noro      119: {
1.5     ! noro      120:        Q dx,dy,w,h,xstep,ystep,c,q1;
1.1       noro      121:        P g,g1,f1,f2,x,y,t,s;
                    122:        int ix,iy;
                    123:        int *a,*pa;
                    124:        VECT ss;
                    125:
                    126:
                    127:        subq(can->qxmax,can->qxmin,&dx); STOQ(can->width,w); divq(dx,w,&xstep);
                    128:        subq(can->qymax,can->qymin,&dy); STOQ(can->height,h); divq(dy,h,&ystep);
                    129:        MKV(can->vx,x); mulp(CO,(P)xstep,x,&t);
                    130:        addp(CO,(P)can->qxmin,t,&s); substp(CO,can->formula,can->vx,s,&f1);
                    131:        MKV(can->vy,y); mulp(CO,(P)ystep,y,&t);
                    132:        addp(CO,(P)can->qymin,t,&s); substp(CO,f1,can->vy,s,&f2);
                    133:        ptozp(f2,1,&c,&g);
                    134:        a = (int *)ALLOCA((MAX(can->width,can->height)+1)*sizeof(int));
                    135:        initmarker(can,"Horizontal scan...");
                    136:        for( ix = 0; ix < can->width; ix++ ) {
                    137:                marker(can,DIR_X,ix);
                    138:                STOQ(ix,q1); substp(CO,g,can->vx,(P)q1,&t); ptozp(t,1,&c,&g1);
                    139:                if ( !g1 )
                    140:                        for ( iy = 0; iy < can->height; iy++ )
                    141:                                tab[ix][iy] = 1;
                    142:                else if ( !NUM(g1) ) {
1.5     ! noro      143:                        sturmseq(CO,g1,&ss); seproot(ss,0,can->height,a);
1.1       noro      144:                        for ( iy = 0, pa = a; iy < can->height; iy++, pa++ )
                    145:                                if ( *pa < 0 || (*(pa+1) >= 0 && (*pa > *(pa+1))) )
                    146:                                        tab[ix][iy] = 1;
                    147:                }
                    148:        }
                    149:        initmarker(can,"Vertical scan...");
                    150:        for( iy = 0; iy < can->height; iy++ ) {
                    151:                marker(can,DIR_Y,iy);
                    152:                STOQ(iy,q1); substp(CO,g,can->vy,(P)q1,&t); ptozp(t,1,&c,&g1);
                    153:                if ( !g1 )
                    154:                        for ( ix = 0; ix < can->width; ix++ )
                    155:                                tab[ix][iy] = 1;
                    156:                else if ( !NUM(g1) ) {
1.5     ! noro      157:                        sturmseq(CO,g1,&ss); seproot(ss,0,can->width,a);
1.1       noro      158:                        for ( ix = 0, pa = a; ix < can->width; ix++, pa++ )
                    159:                                if ( *pa < 0 || (*(pa+1) >= 0 && (*pa > *(pa+1))) )
                    160:                                        tab[ix][iy] = 1;
                    161:                }
                    162:        }
                    163: }
                    164:
1.5     ! noro      165: void sturmseq(VL vl,P p,VECT *rp)
1.1       noro      166: {
                    167:        P g1,g2,q,r,s;
                    168:        P *t;
                    169:        V v;
                    170:        VECT ret;
                    171:        int i,j;
                    172:        Q a,b,c,d,h,l,m,x;
                    173:
                    174:        v = VR(p); t = (P *)ALLOCA((deg(v,p)+1)*sizeof(P));
                    175:        g1 = t[0] = p; diffp(vl,p,v,(P *)&a); ptozp((P)a,1,&c,&g2); t[1] = g2;
                    176:        for ( i = 1, h = ONE, x = ONE; ; ) {
                    177:                if ( NUM(g2) )
                    178:                        break;
                    179:                subq(DEG(DC(g1)),DEG(DC(g2)),&d);
                    180:                l = (Q)LC(g2);
                    181:                if ( SGN(l) < 0 ) {
                    182:                        chsgnq(l,&a); l = a;
                    183:                }
                    184:                addq(d,ONE,&a); pwrq(l,a,&b); mulp(vl,(P)b,g1,(P *)&a);
                    185:                divsrp(vl,(P)a,g2,&q,&r);
                    186:                if ( !r )
                    187:                        break;
                    188:                chsgnp(r,&s); r = s; i++;
                    189:                if ( NUM(r) ) {
                    190:                        t[i] = r; break;
                    191:                }
                    192:                pwrq(h,d,&m); g1 = g2;
                    193:                mulq(m,x,&a); divsp(vl,r,(P)a,&g2); t[i] = g2;
                    194:                x = (Q)LC(g1);
                    195:                if ( SGN(x) < 0 ) {
                    196:                        chsgnq(x,&a); x = a;
                    197:                }
                    198:                pwrq(x,d,&a); mulq(a,h,&b); divq(b,m,&h);
                    199:        }
                    200:        MKVECT(ret,i+1);
                    201:        for ( j = 0; j <= i; j++ )
                    202:                ret->body[j] = (pointer)t[j];
                    203:        *rp = ret;
                    204: }
                    205:
1.5     ! noro      206: void seproot(VECT s,int min,int max,int *ar)
1.1       noro      207: {
                    208:        P f;
                    209:        P *ss;
                    210:        Q q,t;
                    211:        int i,j,k;
                    212:
                    213:        ss = (P *)s->body; f = ss[0];
                    214:        for ( i = min; i <= max; i++ ) {
                    215:                STOQ(i,q); usubstqp(f,q,&t);
                    216:                if ( !t )
                    217:                        ar[i] = -1;
                    218:                else {
                    219:                        ar[i] = numch(s,q,t); break;
                    220:                }
                    221:        }
                    222:        if ( i > max )
                    223:                return;
                    224:        for ( j = max; j >= min; j-- ) {
                    225:                STOQ(j,q); usubstqp(f,q,&t);
                    226:                if ( !t )
                    227:                        ar[j] = -1;
                    228:                else {
                    229:                        if ( i != j )
                    230:                                ar[j] = numch(s,q,t);
                    231:                        break;
                    232:                }
                    233:        }
                    234:        if ( j <= i+1 )
                    235:                return;
                    236:        if ( ar[i] == ar[j] ) {
                    237:                for ( k = i+1; k < j; k++ )
                    238:                        ar[k] = ar[i];
                    239:                return;
                    240:        }
                    241:        k = (i+j)/2;
                    242:        seproot(s,i,k,ar);
                    243:        seproot(s,k,j,ar);
                    244: }
                    245:
1.5     ! noro      246: int numch(VECT s,Q n,Q a0)
1.1       noro      247: {
                    248:        int len,i,c;
                    249:        Q a;
                    250:        P *ss;
                    251:
                    252:        len = s->len; ss = (P *)s->body;
                    253:        for ( i = 1, c = 0; i < len; i++ ) {
                    254:                usubstqp(ss[i],n,&a);
                    255:                if ( a ) {
                    256:                        if ( (SGN(a)>0 && SGN(a0)<0) || (SGN(a)<0 && SGN(a0)>0) )
                    257:                                c++;
                    258:                        a0 = a;
                    259:                }
                    260:        }
                    261:        return c;
                    262: }
                    263:
1.5     ! noro      264: void usubstqp(P p,Q r,Q *v)
1.1       noro      265: {
                    266:        Q d,d1,a,b,t;
                    267:        DCP dc;
                    268:
                    269:        if ( !p )
                    270:                *v = 0;
                    271:        else if ( NUM(p) )
                    272:                *v = (Q)p;
                    273:        else {
                    274:                dc = DC(p); t = (Q)COEF(dc);
                    275:                for ( d = DEG(dc), dc = NEXT(dc); dc;
                    276:                        d = DEG(dc), dc = NEXT(dc) ) {
                    277:                        subq(d,DEG(dc),&d1); pwrq(r,d1,&a);
                    278:                        mulq(t,a,&b); addq(b,(Q)COEF(dc),&t);
                    279:                }
                    280:                if ( d ) {
                    281:                        pwrq(r,d,&a); mulq(t,a,&b); t = b;
                    282:                }
                    283:                *v = t;
                    284:        }
                    285: }
                    286:
1.5     ! noro      287: void plotcalc(struct canvas *can)
1.1       noro      288: {
                    289:        double x,xmin,xstep,ymax,ymin,dy;
                    290:        int ix;
                    291:        Real r;
                    292:        Obj fr;
                    293:        double usubstrp();
                    294:        int w,h;
                    295:        double *tab;
                    296:        POINT *pa;
                    297:        Real rx;
                    298:        Obj t,s;
                    299:
                    300:        MKReal(1.0,r); mulr(CO,(Obj)can->formula,(Obj)r,&fr);
                    301:        w = can->width; h = can->height;
                    302:        xmin = can->xmin; xstep = (can->xmax-can->xmin)/w;
                    303:        tab = (double *)ALLOCA(w*sizeof(double));
                    304:        MKReal(1,rx); /* dummy real number */
                    305:        for( ix = 0, x = xmin; ix < w ; ix++, x += xstep ) {
                    306:                /* full substitution */
                    307:                BDY(rx) = x;
1.5     ! noro      308:                substr(CO,0,fr,can->vx,x?(Obj)rx:0,&s);
        !           309:                devalr(CO,(Obj)s,&t);
1.1       noro      310:                if ( t && (OID(t)!=O_N || NID((Num)t)!=N_R) )
                    311:                        error("plotcalc : invalid evaluation");
                    312:                tab[ix] = ToReal((Num)t);
                    313: #if 0
                    314:                tab[ix] = usubstrp(fr,x);
                    315: #endif
                    316:        }
                    317:        if ( can->ymax == can->ymin ) {
                    318:                for ( ymax = ymin = tab[0], ix = 1; ix < w; ix++ ) {
                    319:                        if ( tab[ix] > ymax )
                    320:                                ymax = tab[ix];
                    321:                        if ( tab[ix] < ymin )
                    322:                                ymin = tab[ix];
                    323:                }
                    324:                can->ymax = ymax; can->ymin = ymin;
                    325:        } else {
                    326:                ymax = can->ymax; ymin = can->ymin;
                    327:        }
                    328:        dy = ymax-ymin;
                    329:        can->pa = (struct pa *)MALLOC(sizeof(struct pa));
                    330:        can->pa[0].length = w;
                    331:        can->pa[0].pos = pa = (POINT *)MALLOC(w*sizeof(POINT));
                    332:        for ( ix = 0; ix < w; ix++ ) {
                    333: #ifndef MAXSHORT
                    334: #define MAXSHORT ((short)0x7fff)
                    335: #endif
                    336:                double t;
                    337:
                    338:                XC(pa[ix]) = ix;
                    339:                t = (h - 1)*(ymax - tab[ix])/dy;
                    340:                if ( t > MAXSHORT )
                    341:                        YC(pa[ix]) = MAXSHORT;
                    342:                else if ( t < -MAXSHORT )
                    343:                        YC(pa[ix]) = -MAXSHORT;
                    344:                else
1.5     ! noro      345:                        YC(pa[ix]) = (long)t;
1.1       noro      346:        }
                    347: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>