Guest User

Untitled

a guest
Oct 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. (defun CreatePolynomial ()
  2. "Creates a new empty polynomial."
  3. ())
  4.  
  5. (defun ReadPolynomial1 ()
  6. "Reads a polynomial from standard-in and returns it as a sorted list."
  7. ())
  8.  
  9. (defun ReadPolynomial2 (pl)
  10. "Reads a polynomial from the list pl and returns it as a sorted list."
  11. (if (null pl)
  12. (CreatePolynomial)
  13. (let ((a (first pl)) (b (second pl)))
  14. (AddPolynomial (list (list a b)) (ReadPolynomial2 (rest (rest pl)))))))
  15.  
  16. (defun WritePolynomial (p)
  17. "Readably outputs the polynomial p."
  18. (if (not (equal 0 (list-length p)))
  19. (progn
  20. (WritePolynomial (rest p))
  21. (if (not (equal 0 (list-length (rest p))))
  22. (if (< 0 (first (first p)))
  23. (format t " + ")
  24. (format t " - ")))
  25. (if (equal 0 (second (first p)))
  26. (format t "~a" (abs (first (first p))))
  27. (format t "~ax^~a" (abs (first (first p))) (second (first p)))))))
  28.  
  29. (defun AddPolynomial (p1 p2)
  30. "Computes and returns p1 + p2."
  31. (ReducePolynomial (concatenate 'list p1 p2)))
  32.  
  33. (defun SubPolynomial (p1 p2)
  34. (ReducePolynomial (SubPolynomialHelper p1 p2)))
  35.  
  36. (defun SubPolynomialHelper (p1 p2)
  37. "Computes and returns p1 - p2."
  38. (if (null p2)
  39. p1
  40. (SubPolynomial (concatenate 'list (list (list (- 0 (first (first p2))) (second (first p2)))) p1) (rest p2))))
  41.  
  42. (defun MultPolynomial (p1 p2)
  43. "Computes and returns p1 * p2."
  44. (ReducePolynomial (Cartesian p1 p2)))
  45.  
  46. (defun Cartesian (n m)
  47. ""
  48. (if (or (null n) (null m))
  49. ()
  50. (concatenate 'list (Distribute (first n) m) (Cartesian (rest n) m))))
  51.  
  52. (defun Distribute (n m)
  53. ""
  54. (if (null m)
  55. ()
  56. (concatenate 'list (list (MultiplyTerm n (first m))) (Distribute n (rest m)))))
  57.  
  58. (defun MultiplyTerm (n m)
  59. ""
  60. (list (* (first n) (first m)) (+ (second n) (second m))))
  61.  
  62. (defun EvalPolynomial (p a)
  63. "Evaluates the polynomial p at the point a."
  64. (if (null p)
  65. 0
  66. (+ (EvalPolynomial (rest p) a) (* (first (first p)) (expt a (second (first p)))))))
  67.  
  68. ;; Helper Functions
  69.  
  70. (defun ReducePolynomial (p)
  71. ""
  72. (ReduceZeroCoefficients (ReduceDuplicateExponents (sort p #'Compare))))
  73.  
  74. (defun ReduceDuplicateExponents (p)
  75. ""
  76. (if (null (rest p))
  77. p
  78. (if (equal (second (first p)) (second (second p)))
  79. (ReduceDuplicateExponents (concatenate 'list (list (list (+ (first (first p)) (first (second p))) (second (first p)))) (rest (rest p))))
  80. (concatenate 'list (list (first p)) (ReduceDuplicateExponents (rest p))))))
  81.  
  82. (defun ReduceZeroCoefficients (p)
  83. ""
  84. (if (null p)
  85. p
  86. (if (equal 0 (first (first p)))
  87. (ReduceZeroCoefficients (rest p))
  88. (concatenate 'list (list (first p)) (ReduceZeroCoefficients (rest p))))))
  89.  
  90. (defun Compare (t1 t2)
  91. ""
  92. (< (second t1) (second t2)))
  93.  
  94. ;; Testing
  95.  
  96. ;(print (ReadPolynomial2 '(99 0 17 200 3 150 8 200 0 0)))
  97. ;(write-line "")
  98.  
  99. (let (
  100. (polyA (ReadPolynomial2 '(1 2 3 1 5 0)))
  101. (polyB (ReadPolynomial2 '(2 2 5 1 2 0))))
  102.  
  103. (WritePolynomial polyA)
  104. (write-line "")
  105. (WritePolynomial polyB)
  106. (write-line "")
  107. ;(WritePolynomial (AddPolynomial polyA polyB))
  108. ;(write-line "")
  109. ;(WritePolynomial (SubPolynomial polyA polyB))
  110. ;(write-line "")
  111. (WritePolynomial (MultPolynomial polyA polyB))
  112. (write-line ""))
  113.  
  114. ;; Prototyping
Add Comment
Please, Sign In to add comment