home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   sci.math.symbolic      Symbolic algebra discussion      10,432 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 9,480 of 10,432   
   Rosario19 to All   
   composition of math expressions in C Lan   
   15 Jun 17 12:05:40   
   
   XPost: comp.lang.c   
   From: Ros@invalid.invalid   
      
   /*   
   Do you like this   
   function that would find sub expression for composition of math   
   expressions?   
      
   for example:   
        Input                output   
   [3*(x+4)^2+3*(x+4)^3] [3*y_^2+3*y_^3] [(x+4)]   
   [3*(x+4)  +3*(x+4)+2] [y_+y_+2] [3*(x+4)]   
   [3*(x+4)*(x+4)+3] [3*y_*y_+3] [(x+4)]   
   [3*(x+4)+(x+4)^3] [3*y_+y_^3] [(x+4)]   
   [x^3+4^2+x^3+4+7] [y_+4^2+y_+4+7] [x^3]   
   [x^3+4^2+x^3+4^2] [y_+y_] [x^3+4^2]   
   [x^3+(4)^2+x^3+(4)^3] [y_+(4)^2+y_+(4)^3] [x^3]   
   [x^3+4444^2+x^3+4444^3] [y_+4444^2+y_+4444^3] [x^3]   
   [3*(x+4*x^3)^2+3*(x+4*x^3)^3] [3*y_^2+3*y_^3] [(x+4*x^3)]   
   [3*(x^2+4*x)  +3*(x^2+4*x)+2] [y_+y_+2] [3*(x^2+4*x)]   
   [3*(x+4*x)+(x+4*x)^3] [3*y_+y_^3] [(x+4*x)]   
   [x^3+x^9+4^2+x^3+x^9+4+7] [y_+4^2+y_+4+7] [x^3+x^9]   
   [x^3+4^2+x^3+4^2] [y_+y_] [x^3+4^2]   
   [x^3+(4)^2+x^3+(4)^3] [y_+(4)^2+y_+(4)^3] [x^3]   
   [x^3+4444^2+x^3+4444^3] [y_+4444^2+y_+4444^3] [x^3]   
   [sin(x)^2+9+sin(x)^2] [y_+9+y_] [sin(x)^2]   
   [3*sin(x)^2+9+3*sin(x)^2+9+1] [y_+y_+1] [3*sin(x)^2+9]   
      
   where it would find the max lenght decomposition of the string in   
   input where   
   appear x with such decompositio different from x itself   
   ["x^2+x" can not have the valid for me, decomposition "y_^2+y_", "x"]   
      
   can you suggest some math expression where this little function fail?   
   can you show some C language error [or some logic error]?   
      
   it is only one little try, possible the way of see the problem is   
   wrong...   
      
   thank you   
   */   
      
      
   #define F      for   
   #define R      return   
   #define G(a,b) if(a)goto b   
      
   int a[256];   
   void init(void)   
   {int  i;   
    F(i='0';i<='9';++i)a[i]=1;   
    F(i='A';i<='Z';++i)a[i|32]=2,a[i]=3;   
    a['_']=4;   
    a['?']=5;   
   // a['<']=a['>']=a['=']=6; //l'espressione non e' una uguaglianza ma   
   l'espressione di una funzione   
    a['&']=a['|']=7;   
    a['+']=a['-']=8;   
    a['*']=a['/']=a['%']=a[':']=9;   
    a['^']=10;   
    a['~']=11;a['!']=12;   
    a[')']=13; // 13+3=16=a['(']   
    a[']']=14; // 14+3=17=a['[']   
    a['}']=15; // 15+3=18=a['{']   
    a['(']=16;   
    a['[']=17;   
    a['{']=18;   
   }   
      
   int isOpr(int x){R a[x]<13&&a[x]>4;}   
   int isAlphaNum(int x){R a[x]<5;}   
      
   /*   
      
    Input:   
          # ww == una stringa espressione matematica   
            con i seguenti operatori matematici e parentesi   
            /+-(){}[]:%* [^ inteso come elevamento a potenza]sin() cos()   
   tan() e   
            tutti gli operatori matematici con la loro precedenza nelle   
   espressioni matematiche   
      
          # xx == una stringa che contiene il valore di una variabile   
   dell'espressione   
      
    output:   
          # return -1 se il processo individua un errore oppure non c'e   
   altra sostituzione da fare   
            che g==x per esempio come in xx="x" ed "sin(x)+cos(x)+x^3"   
      
          # return +1 il processo individua la massima sottostringa di ww   
   che contiene la varabile in xx   
            che e' espressione matematica, posta in g   
            un altra espressione matematica posta in f   
            tali che: in f non compare la variabile in xx   
                      in f     compare la sottostringa[variabile] "y_"   
                      che sostituendo ad y_, g in f si ottiene ww   
   ww=f(g(x))   
      
   Assegna puntatori ad ogni x   
   scorre a destra e a sinistra per vedere il massimo che si puo'   
   prendere   
        fa considerazioni sulla massima stringa ottenuta e sull'ultimo   
   operatore trovato   
   leva tale stringa dalla string ww   
   se la stringa differenza non ha x allora la decomposizione e' valida   
   altrimenti fail   
   */   
   int doComp(char**f, char**g, char*ww,char*xx)   
   {static char   
   Ff[10000],Gg[10000],*pt[1024],pl[1024],pr[1024],w[10000],led;   
    int i,j,k,pwlen,h,gstart,gend,c,d,lastOpr,kSup,ld;   
    char *p,*we,*pp,*g1,*p1,*p2;   
      
    if(led==0){led=1;init();}   
    if(f==0||g==0||w==0) R -1;   
      
    // toglie gli spazi e tutti i caratteri non riconosciuti dalla   
   stringa di partenza   
    F(j=i=0;ww[i]&&i<10000;++i)   
        if(a[ww[i]])w[j++]=ww[i];  // non e' ammesso altro tipo di   
   carattere   
    if(j>=10000)R-1;   
    w[j]=0;   
      
    // Trova le stringhe variablile e mette un puntatore all'inizio   
    F(k=i=0; w[i];++i)   
        {F(j=0;xx[j]&&w[i+j];++j)   
               if(xx[j]!=w[i+j])break;   
         if(xx[j]==0)   
               {if(k>=1024)R-1;   
                pt[k++]=w+i;   
               }   
        }   
      
    g1=Gg+5000; pwlen=k; we=w+i;   
    F(i=0;i=4990)R-1;   
      
    p=pt[0];   
     // max in the left   
    F(k=-1;p+k>=w&&k>-4990;--k)   
      {c=p[k];   
       F(i=1;i=w;++i)   
               if(pl[i]&&c!=pt[i][k])pl[i]=0;   
       F(i=1,d=0;i=4990)R-1;   
    g1[k]=0;   
      
    gend=-gstart+k-1;   
    if(k==1&&gstart==0){*f=0;*g=0;R -1;}   
    //F(h=0;h4 && c<13 && c>lastOpr)lastOpr=c;   
          }   
    else if(isOpr(pt[0][k-1])) // assegna il valore di lastOpr   
       F(ld=1,i=0;i4 && c<13 && c>lastOpr)lastOpr=c;   
          }   
    // assegna i puntatori ad ogni elemento distinto della stringa g   
    pt[0]=pp; c=a[pp[0]];   
    F(k=i=1;i12&&c<19))   
             {if(k>=1024)R-1;c=a[pp[i]];pt[k++]=pp+i;}   
    kSup=k;   
    // 3*(x+4)^  =>   x+4   
    // controlla l'eventuale ultimo operatore con l'altro operatore   
      
    if(lastOpr)   
       {if(ld&&k>1)      {pt[k-1][0]=0;gend-=1;--kSup;k-=2;}   
        else if(k<=1)R-1;   
        else             {                            k-=1;}   
        p1=pt[0]; p2=pt[k];     // p1..p2 la string pp   
        c=a[pt[k][0]];   
        if(c==13||c==14||c==15) // c'e' una parentesi chiusa   
           {   
            F(--k;k>=0;--k)          // cerca una parentesi aperta dello   
   stesso tipo   
                if(c+3==a[pt[k][0]])break;   
            if(k<0)R-1;   
            G(k==0,a3);   
            // cerca l'altro operando   
            if(!isOpr(pt[k][0]))   
               F(--k;k>=0;--k)   
                   if(isOpr(pt[k][0]))break;   
            if(k<0)R-1;   
      
   [continued in next message]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca