4th International Conference on Principles and Practices ... - MADOC
4th International Conference on Principles and Practices ... - MADOC
4th International Conference on Principles and Practices ... - MADOC
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
of the f stream in order to compute the head element of the<br />
resulting stream.<br />
With the alternative map<br />
f ibs → (+ (0 : f ibs) (0 : (1 : f ibs)))<br />
the fixed point can be computed with both implementati<strong>on</strong>s.<br />
The two head elements which are accessed in the zip functi<strong>on</strong><br />
within class LinkedStream are defined.<br />
Stream fib<strong>on</strong>acci = fixedPoint(<br />
new StreamMap() {<br />
public Stream map(Stream f){<br />
return f.prepend(0).zip(new Add(),<br />
f.prepend(1).prepend(0));<br />
}<br />
}<br />
);<br />
If we print out the stream fib<strong>on</strong>acci we receive the following<br />
result:<br />
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...<br />
One advantage of fixed point definiti<strong>on</strong>s is that the recursively<br />
referenced data structure is not reevaluated (in c<strong>on</strong>trast<br />
to the definiti<strong>on</strong> with methods which are recursively<br />
called in the lazily evaluated part as shown in secti<strong>on</strong>s 2 <strong>and</strong><br />
3). This leads to more efficient programs than those where<br />
a stream is rec<strong>on</strong>structed by a method call [7].<br />
5. INFINITE POWER SERIES<br />
In order to apply the streams derived in this article we define<br />
infinite power series, i.e. power series where any number<br />
of terms can be asked. This is useful because the number of<br />
terms required is often not known in advance.<br />
Infinite power series use a stream to store their coefficients.<br />
It turns out, that many series operati<strong>on</strong>s have extremely<br />
simple implementati<strong>on</strong>s when using lazy evaluati<strong>on</strong><br />
<strong>and</strong> fixed point definiti<strong>on</strong>s.<br />
The interface of class PowerSeries is shown below. The<br />
rati<strong>on</strong>al coefficients are implemented in class Rati<strong>on</strong>al. With<br />
the methods diff <strong>and</strong> integrate a series can be differentiated<br />
<strong>and</strong> integrated. The integrati<strong>on</strong> c<strong>on</strong>stant can be provided<br />
as an opti<strong>on</strong>al parameter. The shift method allows<br />
to multiply a power series with the factor x (<strong>and</strong> to add a<br />
c<strong>on</strong>stant term).<br />
public interface PowerSeries {<br />
}<br />
public Rati<strong>on</strong>al coeff(int index);<br />
public PowerSeries add(PowerSeries value);<br />
public PowerSeries subtract(PowerSeries value);<br />
public PowerSeries negate();<br />
public PowerSeries multiply(Rati<strong>on</strong>al value);<br />
public PowerSeries multiply(PowerSeries value);<br />
public PowerSeries divide(PowerSeries value);<br />
public PowerSeries diff();<br />
public PowerSeries integrate();<br />
public PowerSeries integrate(Rati<strong>on</strong>al c);<br />
public PowerSeries shift(Rati<strong>on</strong>al c);<br />
In the implementing class the coefficients are stored in the<br />
field coefficients of type ArrayStream. As an<br />
example we show the method integrate which integrates a<br />
power series term by term.<br />
public PowerSeries integrate(final Rati<strong>on</strong>al c){<br />
return new PowerSeries(<br />
new ArrayStream(<br />
new ArrayStream.Coefficients(){<br />
public Rati<strong>on</strong>al get(int k){<br />
if(k==0) return c;<br />
else return coeff(k-1).divide(<br />
new Rati<strong>on</strong>al(k)<br />
);<br />
}<br />
}<br />
)<br />
);<br />
}<br />
Moreover, the power series class also defines a fixedpoint<br />
method which allows to define power series as fixed points<br />
of a power series map.<br />
interface PowerSeriesMap {<br />
PowerSeries map(PowerSeries series);<br />
}<br />
public PowerSeries fixedpoint(PowerSeriesMap map){<br />
PowerSeries s1 = new PowerSeries();<br />
PowerSeries s2 = map.map(s1);<br />
s1.coefficients = s2.coefficients;<br />
return s2;<br />
}<br />
The fixedpoint method works as l<strong>on</strong>g as all methods<br />
used in the power series map do not access the coefficients<br />
field in an eagerly evaluated part. For example, the multiply<br />
method which scales a power series which a rati<strong>on</strong>al number<br />
must not call the map functi<strong>on</strong> <strong>on</strong> the coefficients but rather<br />
has to define a new coefficient functi<strong>on</strong>.<br />
As an example we define the power series for exp(x). The<br />
equati<strong>on</strong><br />
exp(x) =<br />
Z<br />
exp(x) dx + exp(0)<br />
can directly be used to define the exp<strong>on</strong>ential series at x = 0<br />
using a fixed point:<br />
PowerSeries exp = PowerSeries.fixedpoint(<br />
new PowerSeriesMap(){<br />
public PowerSeries map(PowerSeries exp){<br />
return exp.integrate(Rati<strong>on</strong>al.ONE);<br />
}<br />
}<br />
);<br />
System.out.println(exp);<br />
=> 1 + x + 1/2 x^2 + 1/6 x^3 + 1/24 x^4 + O(x^5)<br />
As another example we show the definiti<strong>on</strong> of the power<br />
series for tan(x) which is the fixed point of the map<br />
tan(x) →<br />
Z<br />
1 + tan(x)<br />
2¡<br />
dx<br />
which leads to the following definiti<strong>on</strong> in Java:<br />
PowerSeries tan = fixedpoint(<br />
new PowerSeriesMap(){<br />
public PowerSeries map(PowerSeries tan){<br />
return tan.multiply(tan)<br />
.add(new PowerSeries(Rati<strong>on</strong>al.ONE))<br />
.integrate();<br />
}<br />
}<br />
);<br />
System.out.println(tan.toString(9));<br />
=> x + 1/3 x^3 + 2/15 x^5 + 17/315 x^7 + O(x^9)<br />
186