A power series is an infinite sum of the form






a

0


+

a

1


⋅
x
+

a

2


⋅

x

2


+

a

3


⋅

x

3


+
⋯


{\displaystyle a_{0}+a_{1}\cdot x+a_{2}\cdot x^{2}+a_{3}\cdot x^{3}+\cdots }



The ai are called the coefficients of the series. Such sums can be added, multiplied etc., where the new coefficients of the powers of x are calculated according to the usual rules.

If one is not interested in evaluating such a series for particular values of x, or in other words, if convergence doesn't play a role, then such a collection of coefficients is called formal power series. It can be treated like a new kind of number.

Task: Implement formal power series as a numeric type. Operations should at least include addition, multiplication, division and additionally non-numeric operations like differentiation and integration (with an integration constant of zero). Take care that your implementation deals with the potentially infinite number of coefficients.

As an example, define the power series of sine and cosine in terms of each other using integration, as in





sin
⁡
x
=

∫

0


x


cos
⁡
t

d
t


{\displaystyle \sin x=\int _{0}^{x}\cos t\,dt}







cos
⁡
x
=
1
−

∫

0


x


sin
⁡
t

d
t


{\displaystyle \cos x=1-\int _{0}^{x}\sin t\,dt}



Goals: Demonstrate how the language handles new numeric types and delayed (or lazy) evaluation.

component FormalPowerSeries(Number);
  type PowerSeries;
       PowerSeries(Size = integer) -> PowerSeries;
 
       + PowerSeries -> PowerSeries;
       - PowerSeries -> PowerSeries;  
 
       PowerSeries + PowerSeries -> PowerSeries; 
       PowerSeries - PowerSeries -> PowerSeries;
       PowerSeries * PowerSeries -> PowerSeries; 
 
       Integral(PowerSeries)     -> PowerSeries;
       Differential(PowerSeries) -> PowerSeries;
 
       Zero -> PowerSeries;
       One  -> PowerSeries;
 
       Array(PowerSeries) -> array(Number);
 begin
       PowerSeries(Size) = PowerSeries:[T = array(Number, Size); Size];
 
       + A = A;
 
       - A = [ C = PowerSeries(A.Size); 
		       [ i = 1 .. A.Size; C.T[i] := - A.T[i] ];
		     C];
 
       A + B = [ if A.Size > B.Size then return(B + A);
		         C = PowerSeries(B.Size);
		         [ i = 1 .. A.Size; C.T[i] := A.T[i] + B.T[i] ];
		         [ i = (A.Size +1) .. B.Size;  C.T[i] := B.T[i] ];
		       C];
 
       A - B = A + (- B );
 
       A * B = [ C = PowerSeries(A.Size + B.Size - 1);
 	         [ i = 1 .. A.Size; 
		     [j = 1.. B.Size; 
		         C.T[i + j - 1] := C.T[i + j - 1] + A.T[i] * B.T[j] ] ];
	          C];
 
      Integral(A) = [ if A.Size == 0 then return (A);
 		      C = PowerSeries(A.Size + 1);
		      [ i = 1 .. A.Size; C.T[i +1] := A.T[i] / Number( i )];
		      C.T[1]:= Number(0); 
		      C ];
 
      Differential(A) = [ if A.Size == 1 then return (A);
		          C = PowerSeries(A.Size - 1);
		          [ i = 1 .. C.Size; C.T[i] := A.T[i + 1] * Number( i )];
		          C ];
 
      Zero = [ C = PowerSeries (1); C.T[1]:= Number(0);  C];	
      One =  [ C = PowerSeries (1); C.T[1]:= Number(1);  C];	
 
      Array(PowerSeries) -> array(Number);
      Array(TS) = TS.T;
 
end component FormalPowerSeries;
 