Nice TWiki > Doc > CodeExamples > PowerSeriesCspExample (r1.1) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* 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 int MAX = 10;
let ArrayList<CSProcess> processes = new ArrayList();

void main(String[] args){   
   let d = split( deriv( ones()) );
   print( add(d[0],d[1]) );

   par( processes.toArray ).run;
}


One2OneChannel ones(){ 
   let out = new One2OneChannel();

   void a(){
      while(true)
         out.write( new Rational(num: 1, den: 1) );
   } 

   processes.add( seq(a) );
   return out;
}


One2OneChannel deriv(One2OneChannel in){
   let out = new One2OneChannel();

   void a(){
      in.read;
      var n = 1;
      while(true){
         let Rational r = cast(in.read);      
         out.write( new Rational(num: n*r.num, den: r.den) );
         n++;
      }
   } 

   processes.add( seq(a) );
   return out;
}


One2OneChannel[] split(One2OneChannel in){ 
   let out1 = One2OneChannel.create( new InfiniteBuffer() );
   let out2 = One2OneChannel.create( new InfiniteBuffer() );

   processes.add( new Delta2(in,out1,out2) );
   return [out1,out2];
}


One2OneChannel add(One2OneChannel f, One2OneChannel g){
   let out = new One2OneChannel();

   void a(){
      var Rational rf;
      var Rational rg;
      while(true){
         rf = cast(f.read);
         rg = cast(g.read);      
         out.write( rf + rg );
      }
   } 

   processes.add( seq(a) );
   return out;
}


void print(One2OneChannel in){

   void a(){
      var count = 0;
      while(count<MAX){
         let Rational r = cast( in.read );
         println(r);
         count++;
      }
      System.exit(0);
   } 

   processes.add( seq(a) );
}

/* Notes - language
See "Squinting at Power Series" (1989)
http://citeseer.nj.nec.com/mcilroy89squinting.html

Experimental use of the "Communicating Sequential Processes for Java TM (JCSP)" library.

   >java -cp "jcspclasses.jar;power.jar" power.fun
   2/1
   4/1
   6/1
   8/1
   10/1
   12/1
   14/1
   16/1
   18/1
   20/1
*/

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 || gcd == denominator){
         num = numerator;
         den = denominator;
      }
      else {
         num = numerator/gcd;
         den = denominator/gcd;
      }      
   }
}

toString(Rational a) = a.num + "/" + a.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){
   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);
-- IsaacGouy - 25 Feb 2004

Topic PowerSeriesCspExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.8 | > | r1.7 | > | r1.6 | More }
Revision r1.1 - 25 Feb 2004 - 22:01 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.