Scheme Polynomial Divition

having trouble with it

Page 1 of 1

2 Replies - 2384 Views - Last Post: 20 April 2010 - 05:42 PM

#1 Jotedem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-April 10

Scheme Polynomial Divition

Posted 18 April 2010 - 11:18 PM

Hello everyone, this is my first post in this forum . I apologize if my english is not good but im from Chile and im doing my best to learn it (Spanish is my First Language). I would like to know if someone could help me a little here with Scheme. Im using it for the AI course in my University and we got as assignment to create a basic calculator using prefix notation . the functions we need to create are +,-,*,/,% , addition of NxM matrix and polynomial divition . I`ve created the basic functions (+,-,*,/ and %), im trying to do the polynomial divition now and the Matrix Addition later.
For the polynomial divition im trying to use the input as a list that specifies the number with the variable (for example x^3+2x^2-8x+1 would be the list (1 2 -8 1) ) and another imput with the next polynom that will be dividing this one (for example x-5 as (0 0 1 -5) ).
¿Would it be better to treat them booth as vectors and try to operate them in that way? i was trying to do that, but to be honest im having a hard time with the start of the problem. The code i have for the functions i`ve already coded is this :

(define cuadrado  ;  ^2  function
  (lambda (x)
    (* x x)))

(define porciento  ; % function
  (lambda (x y)
    (*(/ y x) 100)))

( define  
   (potencia base exponente)   ;  x^y  function
  (if ( zero? exponente )
     1
     ( * base ( potencia base (- exponente 1)))))

(define factorial      ;  ! function
  (lambda (n)
  (if (= n 0) 1
  (* n (factorial (- n 1)))))) 

(define (suma lista)   ;  +
   (if (null? lista)
       0
       (+ (car lista)
          (suma (cdr lista)))))

(define (resta lista)   ; -
   (if (null? lista)
       0
       (- (car lista)
          (suma (cdr lista)))))


(define (multiplicacion lista)   ;  *
   (if (null? lista)
       1
       (* (car lista)
          (multiplicacion (cdr lista)))))

(define (division lista)       ;  /
   (if (null? lista)
       1
       (/ (car lista)
          (multiplicacion (cdr lista)))))




Here is pretty hard to buy books since they are very expensive in conparison to Usa or Europe prices. Any kind of online course that would help me to understand Scheme in a better way would be apreciatted. At this moment im using the MIT book and currently reading http://www.shido.inf.../idx_scm_e.html (since i just found out about it on this page).

Once again, Im sorry for the rusty english im doing my best to learn about it :D

Thanks to everyone that decide to take some time and read this post,

Francisco (Y)

Is This A Good Question/Topic? 0
  • +

Replies To: Scheme Polynomial Divition

#2 s243a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: Scheme Polynomial Divition

Posted 19 April 2010 - 01:19 AM

Is it possible that in your assignment you were suppose to do all those as polynomial operations. I ask this because it would be easier to do polynomial division if you had some of the other functions in place.

For instance polynomial subtraction

(a1,a0)-(b0)=(a1,a0-b0)

This kind of subtraction is important in synthetic division

Polynomial multiplications:

(a1,a0)*(b1,0)=(a1b1,a0b1,0)
b1*(1,0)=(b1,0)

This kind of multiplication is important in synthetic division

Now if you have synthetic division you can define a modula function for polynomials where:

(a1,a0)^n(bn,...,b0)%(a1,a0) = (bn,...,b0)
Was This Post Helpful? 0
  • +
  • -

#3 s243a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 137
  • Joined: 17-March 10

Re: Scheme Polynomial Divition

Posted 20 April 2010 - 05:42 PM

I've never programed in scheme or read a tutorial on it but I think your program should look something like this:

(define syndiv (x y)
   (let ((m (mondiv (pcar x) (pcar y))))
      (let (( r (poly- x (poly* m y))))
         (if (LT (order r) (order y))  
          '(r y)
          (poly+ m (syndiv r y)
         )
       )
     )
)



Where:
syndiv is the synthetic division function described above
mondiv stands for monomial division (this should return a polynomial with exactly one term)
poly- stands for polynomial subtraction
poly* stands for polynomial multiplication
order returns the order of the polynomial (note that length would work, order=length-1. )

You will have to program yourself (syndiv, modiv poly0 poly* and order

edit
changed
(let ((m (mondiv (car x) (car y))))
to
(let ((m (mondiv (pcar x) (pcar y))))

Since car will not return a monomial. Alternatively one may prefer to write
(let ((m (mondiv x y)))
and write mondiv to treat x and y as monomials even if they aren't since all the pcar will do is set all elements of the list except for the head to zero. pcar will also need to be written.

This post has been edited by s243a: 21 April 2010 - 08:38 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1