How to resolve the algorithm Formal power series step by step in the Racket programming language
How to resolve the algorithm Formal power series step by step in the Racket programming language
Table of Contents
Problem Statement
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Formal power series step by step in the Racket programming language
Source code in the racket programming language
#lang lazy
(require racket/match)
;; element-wise addition and subtraction
(define (<+> s1 s2) (map + s1 s2))
(define (<-> s1 s2) (map - s1 s2))
;; element-wise scaling
(define (scale a s) (map (λ (x) (* a x)) s))
;; series multiplication
(define (<*> fs gs)
(match-let ([(cons f ft) (! fs)]
[(cons g gt) (! gs)])
(cons (* f g) (<+> (scale f gt) (<*> ft gs)))))
;; series division
(define (</> fs gs)
(match-letrec ([(cons f ft) (! fs)]
[(cons g gt) (! gs)]
[qs (cons (/ f g) (scale (/ g) (<-> ft (<*> qs gt))))])
qs))
;; integration and differentiation
(define (int f) (map / f (enum 1)))
(define (diff f) (map * (cdr f) (enum 1)))
;; series of natural numbers greater then n
(define (enum n) (cons n (enum (+ 1 n ))))
(define <sin> (cons 0 (int <cos>)))
(define <cos> (cons 1 (scale -1 (int <sin>))))
-> (!! (take 10 <sin>))
'(0 1 0 -1/6 0 1/120 0 -1/5040 0 1/362880)
-> (!! (take 10 <cos>))
'(1 0 -1/2 0 1/24 0 -1/720 0 1/40320 0)
-> (!! (take 10 (diff <sin>)))
'(1 0 -1/2 0 1/24 0 -1/720 0 1/40320 0)
; sin(x)² + cos(x)² = 1
-> (!! (take 10 (<+> (<*> <cos> <cos>) (<*> <sin> <sin>))))
'(1 0 0 0 0 0 0 0 0 0)
; series of (tan x)
-> (!! (take 10 (</> <sin> <cos>)))
'(0 1 0 1/3 0 2/15 0 17/315 0 62/2835)
You may also check:How to resolve the algorithm Disarium numbers step by step in the Picat programming language
You may also check:How to resolve the algorithm A+B step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the HolyC programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the OCaml programming language
You may also check:How to resolve the algorithm Fermat numbers step by step in the Mathematica / Wolfram Language programming language