Nice TWiki > Doc > CodeExamples > PowerSeriesCspExample (r1.5) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
This is an unusual use of the JCSP library. Normally the CSP processes are defined, created and then started. In this case, we spawn new processes as needed while the other processes are running.

See "Squinting at Power Series" and "Power Series, Power Serious".


/* 
   Please try the NoviceExamples before these examples
To compile:
   nicec --classpath "jcspclasses.jar;nice-csp.jar" --sourcepath .. -a power.jar power
To run:
   java -cp "jcspclasses.jar;power.jar" power.fun
*/

import nice.csp;
import nice.csp.util;
import jcsp.plugNplay.* (!);

let ArrayList<ProcessManager> m = new ArrayList();

void main(String[] args){  

   void stopProcesses(){
      for (each : m) each.stop;
      m.clear;   
   }

   let count = 5;
   
   println("");
   println("=== derivatives ===");    
   let d = derivative(ones());  
   print(d, count);  
   stopProcesses();
      
   println("");
   println("=== integrals ===");    
   let i = integral(ones());  
   print(i, count); 
   stopProcesses();
      
   println("");
   println("=== 1 / (1-x)**2 ==="); 
   let o = split(ones());            
   print(o[0]*o[1], count);    
}


PowerSeries ones(){ 
   void->void b(PowerSeries P){      
      void a(){
         while(true)
            P.write( RationalOne() );
      }    
      return a;    
   }
   return managedPS(b);
}


PowerSeries `*`(PowerSeries F, PowerSeries G){
   void->void b(PowerSeries P){
      void a(){
         let f = F.read;
         let g = G.read;
         let FF = split(F);
         let GG = split(G);    
         P.write(f * g);  
         let fG = f * GG[0];      
         let gF = g * FF[0];
         let xFG = xmul(FF[1] * GG[1]);      
           
         while(true)               
            P.write(fG.read + gF.read + xFG.read); 
      } 
      return a;    
   }
   return managedPS(b);
}


PowerSeries[] split(PowerSeries F){     
   let P0 = new PowerSeries(channel: 
      One2OneChannel.create(new InfiniteBuffer()));
   let P1 = new PowerSeries(channel: 
      One2OneChannel.create(new InfiniteBuffer()));        
   
   let pm = new ProcessManager( new Delta2(F.channel,P0.channel,P1.channel) );
   m.add(pm);
   pm.start;  
   return [P0,P1];
}


PowerSeries `*`(Rational c, PowerSeries F){
   void->void b(PowerSeries P){  
      void a(){ while(true) P.write(c*F.read); }
      return a;    
   }
   return managedPS(b); 
}


PowerSeries xmul(PowerSeries F){
   void->void b(PowerSeries P){     
      void a(){
         P.write(RationalZero());
         while(true) P.write(F.read);
      }    
      return a;    
   }
   return managedPS(b);    
}


void print(PowerSeries F, int n){
   void a(){
      var count = 0;
      println("Value\t\tThreads");      
      while(count<n){     
         println(F.read + "\t\t" + Thread.activeCount());
         count++;
         }
   } 
   seq(a).run;
}


PowerSeries derivative(PowerSeries F){
   void->void b(PowerSeries P){ 
      void a(){
         var f = F.read;
         var n = 1;
         while(true){
            f = F.read;      
            P.write( new Rational(num: n*f.num, den: f.den) );
            n++;
         }
      } 
      return a;    
   }
   return managedPS(b);
}


PowerSeries integral(PowerSeries F){
   void->void b(PowerSeries P){ 
      void a(){
         var f = F.read;
         var n = 1;
         while(true){
            f = F.read;      
            P.write( new Rational(num: f.num, den: f.den*n) );
            n++;
         }
      } 
      return a;    
   }
   return managedPS(b);
}


class PowerSeries {
   One2OneChannel channel = new One2OneChannel();

   Rational read() = cast(channel.read);   
   void write(Rational a) = channel.write(a);
}


PowerSeries managedPS(PowerSeries->(void->void) f){
   let P = new PowerSeries();
   let a = f(P);
   let pm = new ProcessManager( seq(a) );
   m.add(pm);
   pm.start;  
   return P;
}

/* Notes - language
Experimental use of the "Communicating Sequential Processes for Java TM (JCSP)" library.

See "Squinting at Power Series" 
   http://citeseer.nj.nec.com/mcilroy89squinting.html

and "Power Series, Power Serious"
   http://citeseer.nj.nec.com/mcilroy98power.html


   >java -cp jcspclasses.jar;power.jar power.fun

   === derivatives ===
   Value           Threads
   1               3
   2               3
   3               3
   4               3
   5               3

   === integrals ===
   Value           Threads
   1               3
   1/2             3
   1/3             3
   1/4             3
   1/5             3

   === 1 / (1-x)**2 ===
   Value           Threads
   1               7
   2               15
   3               19
   4               27
   5               27
*/

public class Rational {
   long num; // dividend
   long den; // divisor
   
   {
      let denominator = abs(den); 
      var numerator = num;
      if (den<0) numerator = -numerator;
      
      let gcd = numerator.gcd(denominator);
      if (gcd == 1){
         num = numerator;
         den = denominator;
      }
      else {
         num = numerator/gcd;
         den = denominator/gcd;
      }      
   }
      
   toString(){   
      if (den == 1) 
         return num.toString; 
      else 
         return num + "/" + den;     
   }                           
}

Rational `+`(Rational a, Rational b){
   if (a.den == b.den)
      return new Rational(num: a.num + b.num, den: a.den);
   else
      return new Rational(
         num: (a.num * b.den) + (b.num * a.den), 
         den: a.den * b.den
         );      
}   

Rational `*`(Rational a, Rational b) =
  new Rational(num: a.num * b.num, den: a.den * b.den); 
  
Rational RationalOne() = new Rational(num: 1, den: 1);
Rational RationalZero() = new Rational(num: 0, den: 1);

-- IsaacGouy - 25 Feb 2004

Topic PowerSeriesCspExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.8 | > | r1.7 | > | r1.6 | More }
Revision r1.5 - 27 Feb 2004 - 17:34 GMT - IsaacGouy
Parents: WebHome > CodeExamples
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.