September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,29 @@
class IPS{
var [protected] w; // the coefficients of the infinite series
fcn init(w_or_a,b,c,etc){ // IPS(1,2,3) --> (1,2,3,0,0,...)
switch [arglist]{
case(Walker) { w=w_or_a.tweak(Void,0) }
else { w=vm.arglist.walker().tweak(Void,0) }
}
}
fcn __opAdd(ipf){ //IPS(1,2,3)+IPS(4,5)-->IPS(5,6,3,0,...), returns modified self
switch[arglist]{
case(1){ addConst(ipf) } // IPS + int/float
else { w=w.zipWith('+,ipf.w) } // IPS + IPS
}
self
}
fcn __opSub(ipf){ w=w.zipWith('-,ipf.w); self } // IPS - IPSHaskell
fcn __opMul(ipf){ } // stub
fcn __opDiv(x){ w.next().toFloat()/x } // *IPS/x, for integtate()
fcn __opNegate { w=w.tweak(Op("--")); self }
// integtate: b0 = 0 by convention, bn = an-1/n
fcn integrate{ w=w.zipWith('/,[1..]).push(0.0); self }
fcn diff { w=w.zipWith('*,[1..]); self }
fcn facts{ (1).walker(*).tweak(fcn(n){ (1).reduce(n,'*,1) }) } // 1!,2!...
fcn walk(n){ w.walk(n) }
fcn value(x,N=15){ ns:=[1..]; w.reduce(N,'wrap(s,an){ s + an*x.pow(ns.next()) }) }
fcn cons(k){ w.push(k); self } //--> k, a0, a1, a2, ...
// addConst(k) --> k + a0, a1, a2, ..., same as k + IPS
fcn addConst(k){ (w.next() + k) : w.push(_); self }
}

View file

@ -0,0 +1,2 @@
(IPS(1,2,3) + IPS(4,5)).walk(5).println();
(-IPS([1..]) + 11).walk(5).println();

View file

@ -0,0 +1,17 @@
fcn sine{ // sine Taylor series: 0 + x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - ...
IPS(Utils.Helpers.cycle(1.0, 0.0, -1.0, 0.0).zipWith('/,IPS.facts()))
.cons(0.0)
}
print("Sine Taylor series: "); dostuff(sine,"sin");
fcn cosine{ -sine().integrate() + 1.0 }
print("Cosine power series: "); dostuff(cosine,"cos");
fcn dostuff(ips,name){ // print series, evaluate at various points
f:='wrap(x,xnm){ v:=ips().value(x);
println("%s(%s) \U2192; %f \U394;=%f".fmt(name,xnm,v,x.Method(name)()-v));
};
ips().walk(15).println();
f(0.0,"0"); f((1.0).pi/4,"\Ubc;\U3c0;");
f((1.0).pi/2,"\Ubd;\U3c0;"); f((1.0).pi,"\U3c0;");
}