"Fossies" - the Fresh Open Source Software Archive

Member "jas-2.6/examples/powerseries_multi.rb" (22 Feb 2015, 4787 Bytes) of package /linux/privat/jas-2.6.5988-src.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #
    2 # jruby examples for jas.
    3 # $Id: powerseries_multi.rb 5130 2015-02-22 10:56:21Z kredel $
    4 #
    5 
    6 require "examples/jas"
    7 
    8 # examples for power series
    9 
   10 # rational number examples
   11 #
   12 
   13 psr = MultiSeriesRing.new("Q(x,y,z)",truncate=4);
   14 puts "psr: " + str(psr);
   15 puts;
   16 
   17 one = psr.one();
   18 puts "one: " + str(one);
   19 #puts;
   20 
   21 zero = psr.zero();
   22 puts "zero: " + str(zero);
   23 puts;
   24 
   25 r1 = psr.random(4);
   26 puts "r1: " + str(r1);
   27 #puts;
   28 puts "r1: " + str(r1);
   29 #puts;
   30 puts "r1-r1: " + str(r1-r1);
   31 puts;
   32 
   33 r2 = psr.random(4);
   34 puts "r2: " + str(r2);
   35 #puts;
   36 puts "r2: " + str(r2);
   37 #puts;
   38 puts "r2-r2: " + str(r2-r2);
   39 puts;
   40 
   41 #sys.exit();
   42 
   43 r3 = r1 + r2;
   44 puts "r3: " + str(r3);
   45 puts;
   46 
   47 r4 = r1 * r2 + one;
   48 puts "r4: " + str(r4);
   49 puts;
   50 
   51 e = psr.exp(1);
   52 puts "e: " + str(e);
   53 puts;
   54 
   55 r5 = r1 * r2 + e;
   56 puts "r5: " + str(r5);
   57 puts;
   58 
   59 #puts "psr.gens: ", [ str(i) for i in psr.gens() ];
   60 one,x,y,z = psr.gens();
   61 puts "x: " + str(x);
   62 puts "y: " + str(y);
   63 puts "z: " + str(z);
   64 puts;
   65 
   66 r6 = one - y;
   67 puts "r6: " + str(r6);
   68 puts;
   69 
   70 r7 = one / r6;
   71 puts "r7: " + str(r7);
   72 puts;
   73 
   74 
   75 s = psr.sin(1);
   76 puts "s: " + str(s);
   77 puts;
   78 
   79 r8 = psr.gcd(y,s);
   80 puts "r8: " + str(r8);
   81 puts;
   82 
   83 
   84 ## s1 = s.evaluate( QQ(0) );
   85 ## puts "s1: " + str(s1);
   86 ## puts;
   87 
   88 r9 = psr.random(4);
   89 puts "r9: " + str(r9);
   90 puts;
   91 e = r9.evaluate( [QQ(1), QQ(0), QQ(1,2)] );
   92 puts "e:  " + str(e);
   93 puts;
   94 
   95 
   96 c = psr.cos(1);
   97 puts "c: " + str(c);
   98 puts;
   99 
  100 ## c1 = c.evaluate( QQ(0) );
  101 ## puts "c1: " + str(c1);
  102 ## puts;
  103 
  104 s2c2 = s*s + c*c; # sin^2 + cos^2 = 1
  105 puts "s2c2: " + str(s2c2);
  106 puts "s2c2 == 1: " + str(s2c2.isONE());
  107 puts;
  108 
  109 
  110 # conversion from polynomials
  111 
  112 pr = Ring.new("Q(x,y,z) L");
  113 puts "pr: " + str(pr);
  114 puts;
  115 
  116 one,xp,yp,zp = pr.gens();
  117 
  118 p1 = one;
  119 p2 = one - yp;
  120 
  121 ps1 = psr.fromPoly(p1);
  122 ps2 = psr.fromPoly(p2);
  123 
  124 # rational function as power series:
  125 ps3 = ps1 / ps2;
  126 
  127 puts "p1: "  + str(p1);
  128 puts "p2: "  + str(p2);
  129 puts "ps1: " + str(ps1);
  130 puts "ps2: " + str(ps2);
  131 puts "ps3: " + str(ps3);
  132 puts;
  133 
  134 
  135 p1 = one * 2 + yp**3 - yp**5;
  136 p2 = one - yp**2 + yp**4;
  137 
  138 ps1 = psr.fromPoly(p1);
  139 ps2 = psr.fromPoly(p2);
  140 
  141 
  142 # rational function as power series:
  143 ps3 = ps1 / ps2;
  144 
  145 ps4 = ps3.integrate( QQ(1), 1 );
  146 ps5 = ps3.differentiate(1);
  147 
  148 puts "p1: "  + str(p1);
  149 puts "p2: "  + str(p2);
  150 puts "ps1: " + str(ps1);
  151 puts "ps2: " + str(ps2);
  152 puts "ps3: " + str(ps3);
  153 puts "ps4: " + str(ps4);
  154 puts "ps5: " + str(ps5);
  155 puts;
  156 
  157 
  158 #exit();
  159 
  160 
  161 #
  162 # floating point examples
  163 #
  164 
  165 #dr = MultiSeriesRing.new(cofac=DD(),names=psr.ring.polyRing().getVars(),truncate=psr.ring.truncate());
  166 dr = MultiSeriesRing.new("",psr.ring.truncate(),nil,DD(),psr.ring.polyRing().getVars());
  167 puts "dr: " + str(dr);
  168 puts;
  169 
  170 de = dr.exp(1);
  171 puts "de: " + str(de);
  172 puts;
  173 
  174 
  175 # use lambdas:
  176 # type(a) == ExpVector
  177 f = lambda { |a| a.getVal(0) * a.getVal(1) * a.getVal(2) };
  178 
  179 ps = psr.create(f);
  180 puts "ps: " + str(ps);
  181 puts;
  182 
  183 # type(a) == ExpVector
  184 g = lambda { |a| a.totalDeg() };
  185 
  186 ps1 = psr.create(g);
  187 puts "ps1: " +str(ps1);
  188 puts;
  189 
  190 
  191 ps2 = ps * ps1;
  192 puts "ps2: " + str(ps2);
  193 puts;
  194 
  195 
  196 h = lambda { |a| psr.ring.coFac.fromInteger( 3 * a.totalDeg() ) };
  197 
  198 ps3 = psr.create(nil,h);
  199 puts "ps3: " + str(ps3);
  200 puts;
  201 
  202 ps4 = ps3 * ps1;
  203 puts "ps4: " + str(ps4);
  204 puts;
  205 
  206 #exit();
  207 
  208 # does not work, since get() is not known
  209 #def k(a)
  210 #    if a > 0
  211 #        return get(a-1).multiply( psr.ring.coFac.fromInteger( 2*a ) );
  212 #    else
  213 #        return psr.ring.coFac.fromInteger( 2*a );
  214 #    end
  215 #end
  216 #no#ps5 = psr.create(jfunc=k);
  217 #no#puts "ps5: ", ps5;
  218 #no#puts;
  219 
  220 
  221 class Mycoeff < MultiVarCoefficients
  222     def initialize(ring,ifunc=nil,jfunc=nil)
  223         super(ring);
  224         @coFac = ring.coFac;
  225         @ifunc = ifunc;
  226         @jfunc = jfunc;
  227     end
  228     def generate(i) # type(i) == ExpVector
  229         if i.signum() <= 0
  230             return @coFac.getONE();
  231         else
  232             dep = i.dependencyOnVariables();
  233             #if dep.length == 0 
  234             #   return @coFac.getZERO();
  235             #end
  236             i1 = i.subst( dep[0], i.getVal(dep[0])-1 ); # 0 <= i1 < i
  237             c = get( i1 ).negate();
  238             c = c.divide( @coFac.fromInteger(i.maxDeg()) ).sum( @coFac.fromInteger(i.totalDeg()-1) );
  239             #puts "c: " + str(c) + " i: " + str(i);
  240             return c;
  241         end
  242     end
  243 end
  244 
  245 
  246 ps6 = psr.create(nil,nil, Mycoeff.new(psr.ring) );
  247 puts "ps6: " + str(ps6);
  248 puts;
  249 
  250 ps7 = ps6*2 + s - ps6 - ps6;
  251 puts "ps7: " + str(ps7);
  252 puts "ps7 == s: " + str(ps7 == s);
  253 puts;
  254 
  255 #exit();
  256 
  257 class Cosmap 
  258     include MultiVarPowerSeriesMap # for interfaces use Module notations
  259     def initialize(cofac)
  260         @coFac = cofac;
  261     end
  262     def map(ps)
  263         return ps.negate().integrate( @coFac.getZERO(), 1 ).integrate( @coFac.getONE(), 1 );
  264     end
  265 end
  266 
  267 ps8 = psr.fixPoint( Cosmap.new( psr.ring.coFac ) );
  268 puts "ps8: " + str(ps8);
  269 puts;
  270 
  271 ps9 = ps8 - c;
  272 puts "ps9: " + str(ps9);
  273 puts "ps9 == 0: " + str(ps9.isZERO());
  274 puts;
  275 
  276 
  277 #exit();