### Create polynomials

Posted:

**Sun Aug 25, 2019 3:45 pm**Suppose we have the polynomial y (x) = 3*x^2 - 7*x + 5 and we want to calculate the values of y for x ranging from 0 to 10 (with step 1).

We can define a function that represents the polynomial:
And then to get the searched values:
Since the polynomials have a well-defined structure, we can write a function that takes the coefficients of a polynomial and returns a function that represents the polynomial:

For example, the polynomial:
is represented by the function:
Our function must therefore construct a new lambda function that represents the polynomial (we work on the lambda function as if it were a list).
In this way we can define a new "poly" function that represents our polynomial:
Evaluating the polynomial for x = 0 we obtain the constant term:
And to get the values:
Do you known a better/elegant method to create lambda functions for polynomials?

We can define a function that represents the polynomial:

Code: Select all

```
(define (poly x)
(+ 5 (mul 7 x) (mul 3 (pow x 2))))
(poly 0)
;-> 5
```

Code: Select all

```
(for (x 0 10) (println x { } (poly x)))
;-> 0 5
;-> 1 15
;-> 2 31
;-> 3 53
;-> 4 81
;-> 5 115
;-> 6 155
;-> 7 201
;-> 8 253
;-> 9 311
;-> 10 375
```

For example, the polynomial:

Code: Select all

` y(x) = 4*x^3 + 5*x^2 + 7*x + 10 `

Code: Select all

` (lambda (x) (add 10 (mul x 7) (mul (pow x 2) 5) (mul (pow x 3) 4)))`

Code: Select all

```
(define (make-poly coeff)
(local (fun body)
(reverse coeff)
(setq fun '(lambda (x) x)) ;funzione lambda base
(setq body '()) ;corpo della funzione
(push 'add body -1)
(push (first coeff) body -1) ;termine noto
(push (list 'mul 'x (coeff 1)) body -1) ;termine lineare
(for (i 2 (- (length coeff) 1))
(push (list 'mul (list 'pow 'x i) (coeff i)) body -1)
)
(setq (last fun) body) ;modifica corpo della funzione
fun
)
)
```

Code: Select all

```
(setq poly (make-poly '(4 5 7 10)))
;-> (lambda (x) (add 10 (mul x 7) (mul (pow x 2) 5) (mul (pow x 3) 4)))
```

Code: Select all

```
(poly 0)
;-> 10
```

Code: Select all

```
(for (x 0 10) (println x { } (poly x)))
;-> 0 10
;-> 1 26
;-> 2 76
;-> 3 184
;-> 4 374
;-> 5 670
;-> 6 1096
;-> 7 1676
;-> 8 2434
;-> 9 3394
;-> 10 4580
```