lisp - Common Lisp - 如何组合这两个简单的多项式函数?

标签 lisp common-lisp polynomial-math

我是 Lisp 新手,正在学习一些教程。下面是微分多项式的给定代码。我想简化它,所以 (d) 和 (simplify) 是一个功能/可以一步完成。我在想一些事情 (defun differentiate (poly x) (simplify (d (poly x)))) 但后来它认为 poly 是一个函数,它不起作用.

也许这不起作用,因为必须引用 (d) 的输入?即 (d '(+ x y) 'x)

抱歉,代码量很大,但我认为最好将其全部放入。相关功能在底部。

;;
;; Constructors for polynomials
;;

(defun make-constant (num)
  num)

(defun make-variable (sym)
  sym)

(defun make-negation (poly)
  (list '- poly))

(defun make-sum (poly1 poly2)
  (list '+ poly1 poly2))

(defun make-difference (poly1 poly2)
  (list '- poly1 poly2))

(defun make-product (poly1 poly2)
  (list '* poly1 poly2))

(defun make-power (poly num)
  (list '** poly num))

;;
;; Recognizers for polynomials
;;

(defun constant-p (poly)
  (numberp poly))

(defun variable-p (poly)
  (symbolp poly))

(defun negation-p (poly)
  (and (listp poly) (eq (first poly) '-) (null (rest (rest poly)))))

(defun sum-p (poly)
  (and (listp poly) (eq (first poly) '+)))

(defun difference-p (poly)
  (and (listp poly) (eq (first poly) '-) (not (null (rest (rest poly))))))

(defun product-p (poly)
  (and (listp poly) (eq (first poly) '*)))

(defun power-p (poly)
  (and (listp poly) (eq (first poly) '**)))

;;
;; Selectors for polynomials
;;

(defun constant-numeric (const)
  const)

(defun variable-symbol (var)
  var)

(defun negation-arg (neg)
  (second neg))

(defun sum-arg1 (sum)
  (second sum))

(defun sum-arg2 (sum)
  (third sum))

(defun difference-arg1 (diff)
  (second diff))

(defun difference-arg2 (diff)
  (third diff))

(defun product-arg1 (prod)
  (second prod))

(defun product-arg2 (prod)
  (third prod))

(defun power-base (pow)
  (second pow))

(defun power-exponent (pow)
  (third pow))

;;
;; Unevaluated derivative
;;

(defun make-derivative (poly x)
    (list 'd poly x))

(defun derivative-p (poly)
  (and (listp poly) (eq (first poly) 'd)))

;;
;; Differentiation function
;;

(defun d (poly x)
  (cond
   ((constant-p poly) 0)
   ((variable-p poly) 
    (if (equal poly x) 
    1 
      (make-derivative poly x)))
      ((negation-p poly)
    (make-negation (d (negation-arg poly) x)))
   ((sum-p poly) 
    (make-sum (d (sum-arg1 poly) x) 
          (d (sum-arg2 poly) x)))
      ((difference-p poly)
    (make-difference (d (difference-arg1 poly) x)
             (d (difference-arg2 poly) x)))
   ((product-p poly) 
    (make-sum (make-product (product-arg1 poly) 
                (d (product-arg2 poly) x))
          (make-product (product-arg2 poly) 
                (d (product-arg1 poly) x))))
   ((power-p poly)
    (make-product (make-product (power-exponent poly)
                (make-power (power-base poly) 
                        (1- (power-exponent poly))))
          (d (power-base poly) x)))))

;;
;; Simplification function
;;

(defun simplify (poly)
  "Simplify polynomial POLY."
  (cond
   ((constant-p poly) poly)
   ((variable-p poly) poly)
      ((negation-p poly) 
    (let ((arg (simplify (negation-arg poly))))
      (make-simplified-negation arg)))
   ((sum-p poly)
    (let ((arg1 (simplify (sum-arg1 poly)))
      (arg2 (simplify (sum-arg2 poly))))
      (make-simplified-sum arg1 arg2)))
   ((product-p poly)
    (let ((arg1 (simplify (product-arg1 poly)))
      (arg2 (simplify (product-arg2 poly))))
      (make-simplified-product arg1 arg2)))
      ((difference-p poly)
    (let ((arg1 (simplify (difference-arg1 poly)))
      (arg2 (simplify (difference-arg2 poly))))
      (make-simplified-difference arg1 arg2)))
   ((power-p poly)
    (let ((base (simplify (power-base poly)))
      (exponent (simplify (power-exponent poly))))
      (make-simplified-power base exponent)))
   ((derivative-p poly) poly)))

(defun make-simplified-negation (arg)
  "Given simplified polynomial ARG, construct a simplified negation of ARG."
  (cond
   ((and (constant-p arg) (zerop arg)) arg)
   ((negation-p arg)                   (negation-arg arg))
   (t                                  (make-negation arg))))

(defun make-simplified-sum (arg1 arg2)
  "Given simplified polynomials ARG1 and ARG2, construct a simplified sum of ARG1 and ARG2."
  (cond
   ((and (constant-p arg1) (zerop arg1)) arg2)
   ((and (constant-p arg2) (zerop arg2)) arg1)
      ((negation-p arg1)                   (make-simplified-difference 
                     arg2 (negation-arg arg1)))
      ((negation-p arg2)                   (make-simplified-difference 
                     arg1 (negation-arg arg2)))
   (t                                   (make-sum arg1 arg2))))

(defun make-simplified-difference (arg1 arg2)
  "Given simplified polynomials ARG1 and ARG2, construct a simplified difference of ARG1 and ARG2."
  (cond
   ((and (constant-p arg2) (zerop arg2)) arg1)
   ((and (constant-p arg1) (zerop arg1)) (make-simplified-negation arg2))
   ((negation-p arg2)                    (make-simplified-sum 
                      arg1 (negation-arg arg2)))
   (t                                    (make-difference arg1 arg2))))

(defun make-simplified-product (arg1 arg2)
  "Given simplified polynomials ARG1 and ARG2, construct a simplified product of ARG1 and ARG2."
  (cond
   ((and (constant-p arg1) (zerop arg1)) (make-constant 0))
   ((and (constant-p arg2) (zerop arg2)) (make-constant 0))
   ((and (constant-p arg1) (= arg1 1))   arg2)
   ((and (constant-p arg2) (= arg2 1))   arg1)
      ((and (constant-p arg1) (= arg1 -1))  (make-simplified-negation arg2))
      ((and (constant-p arg2) (= arg2 -1))  (make-simplified-negation arg1))
   (t                                    (make-product arg1 arg2))))

(defun make-simplified-power (base exponent)
  "Given simplified polynomials BASE and EXPONENT, construct a simplified power with base BASE and exponent EXPONENT."
  (cond
   ((and (constant-p exponent) (= exponent 1))   base)
   ((and (constant-p exponent) (zerop exponent)) (make-constant 1))
   (t                                            (make-power base exponent))))

最佳答案

poly 的输入“不需要被引用”。而不是

(d '(+ x y) 'x)

您可以编写以下任何内容:

(d (list '+ 'x 'y) 'x)

(d (make-sum 'x 'y) 'x)

(let ((ex 'x) (why 'y))
  (d (list '+ ex why) ex))

在您对问题的尝试中,您试图用 (poly x) 的结果调用 d ,它试图调用名为 的函数poly(实际上,符号 poly 的函数绑定(bind),或其他一些可能的东西,但这可能比我们现在需要的更深入)与变量的值x:

(defun differentiate (poly x)
  ;; call poly with the value of x to produce a value y, and
  ;; then call d with y to produce a value z, and then call
  ;; simplify with z.
  (simplify (d (poly x))))

这当然行不通,因为没有名为 poly 的函数,即使有,d 也需要两个参数,而不是一个。相反,您应该做的第一件事是使用 两个 参数调用 d,即变量 polyx 的值,然后用结果调用 simplify :

(defun differentiate (poly x)
  (simplify (d poly x)))

关于lisp - Common Lisp - 如何组合这两个简单的多项式函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20952999/

相关文章:

scheme - 以下是理解 lisp 上的 Bindigs 的好方法吗?

scope - 为什么这个函数每次都返回不同的值?

lisp - 函数名在 Lisp 中作为字符串?

lambda - 如何在 LISP 中定义 LAMBDA 函数?

scheme - 哪些 lisp 实现允许我在运行时修改代码?

macos - Hunchentoot 1.0 仅返回空响应

objective-c - 如何从 HEX 值计算 CRC-16?

javascript - 如何将等式中的多项式项移到一边?

c++ - 从根高效计算多项式系数

lisp - "UIOP"包在 SBCL 中不存在,有人可以帮我找到吗?