OCaml函数执行微分

时间:2018-10-10 09:36:30

标签: ocaml differentiation

我目前正在学习OCaml语言,并且遇到一个似乎无法解决问题的问题时,正在解决运动问题。这是问题:


“编写一个函数differentiate : expression * string -> expression,该函数接收一个代数方程和一个字符串作为参数,并返回该参数方程的微分形式。

例如,diff (Add [Mult [Int 3 ; Exp("x", 2)] ; Mult [Int 6 ; Variable "x"], "x")将产生结果:

Add [Mult [Int 6 ; Variable "x"] ; Int 6]


这是我编写的代码:

type expression =
  | Int of int
  | Variable of string
  | Exponent of string * int
  | Mult of expression list
  | Add of expression list


let rec differentiate : expression * string -> expression
= fun (exp, x) ->
  match exp with
  | Int a -> Int 0
  | Variable a -> if (a = x) then Int 1 else Variable a
  | Exponent (a, b) -> if (a = x) then
                      match b with
                      | 2 -> Mult [Int 2; Variable a]
                      | _ -> Mult [Int b; Exponent (a, b - 1)]
                    else Int 0
  | Mult [Int a; Int b] -> Const (a * b)
  | Mult (Int a::[Variable b]) -> Mult (Int a::[differentiate (Variable b, x)])
  | Mult (Int a::[Exponent (e1, e2)]) -> Mult (Int a::[differentiate (Exponent (e1, e2), 
x)])
  | Mult (Int a::[Mult (Int b :: l)]) -> Mult (Int (a * b) :: l)
  | Add l -> match l with
             | [] -> l
             | hd::tl -> Add ((differentiate (hd, x)) :: tl)
;;


我的算法基本上是执行严格的模式匹配。更具体地说,对于Mult,第一个元素始终是整数,因此我对第二个元素执行了模式匹配。对于Add,我的计划是编写函数,以便它在每个元素上执行函数differentiate。这是我想问的特定问题

  1. 实际上,此代码使我在模式匹配的Add l部分出现了错误。错误消息指出:Error: This expression has type (expression list) but an expression was expected of type (expression).据我所知,我确定Add lexpression类型,而不是expression list类型。为什么会产生此错误消息?

  2. 在此特定示例中,我不确定如何执行递归。我最初的想法是该函数应该只执行一次,否则结果将主要由Int 0Int 1组成。如果我错了,请纠正我。


任何反馈,我们将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

type exp =
  | Int  of int
  | Var  of string
  | Exp  of string * int
  | Add  of exp list
  | Mult of exp list

let rec diff x = function
  | Int _ -> Int 0
  | Var y -> Int (if x = y then 1 else 0)
  | Exp (y, n) -> if x = y then Mult [Int n; Exp (y, n - 1)] else Int 0
  | Add  exp_list -> Add (List.map (diff x) exp_list)
  | Mult exp_list ->
     let rec terms acc (left, right) =
       match right with
       | [] -> acc
       | hd :: tl -> terms ((hd, left @ tl) :: acc) (hd :: left, tl) in
     Add (List.map (fun (f, g) -> Mult [diff x f; Mult g]) (terms [] ([], exp_list)))

let rec norm = function
  | Exp (_, 0) -> Int 1
  | Exp (x, 1) -> Var x
  | Add  exp_list ->
     let int_list, rest = List.fold_left (fun (int_list, rest) exp ->
       match exp with
       | Int i -> i :: int_list, rest
       | Add (Int i :: exp_list) -> i :: int_list, exp_list @ rest
       | Add exp_list -> int_list, exp_list @ rest
       | _ -> int_list, exp :: rest) ([], []) (List.map norm exp_list) in
     let sum = List.fold_left (+) 0 int_list in
     begin match sum, rest with
       | _, []  -> Int sum
       | 0, [e] -> e
       | 0, _   -> Add rest
       | _      -> Add (Int sum :: rest) end
  | Mult exp_list ->
     let int_list, rest = List.fold_left (fun (int_list, rest) exp ->
       match exp with
       | Int i -> i :: int_list, rest
       | Mult (Int i :: exp_list) -> i :: int_list, exp_list @ rest
       | Mult exp_list -> int_list, exp_list @ rest
       | _ -> int_list, exp :: rest) ([], []) (List.map norm exp_list) in
     let prod = List.fold_left ( * ) 1 int_list in
     begin match prod, rest with
       | _, []  -> Int prod
       | 0, _   -> Int 0
       | 1, [e] -> e
       | 1, _   -> Mult rest
       | _      -> Mult (Int prod :: rest) end
  | (Int _ | Var _ | Exp _) as e -> e

这是解决此问题的一种可能的方法。想法是首先实现一种算法,通过递归表达式的结构来执行区分。而且我们应该保持简单,也就是说,我们不在乎语法树的简化,我们只在乎正确计算差异。然后,在第二步中,我们将树简化为“正常”形式(不确定是否对于相同的表达式总是产生相同的结果,但是由于问题并未指定我们应该执行哪些简化,因此我想免费选择)。

一些例子:

# norm (diff "x" (Add [Mult [Int 3 ; Exp("x", 2)] ; Mult [Int 6 ; Var "x"]]));;
- : exp = Add [Int 6; Mult [Int 6; Var "x"]]

# norm (diff "y" (Mult [Var "x"; Mult[Int 2; Exp ("y", 3)]]));;
- : exp = Mult [Int 6; Var "x"; Exp ("y", 2)]

# norm (diff "y" (Mult [Var "x"; Var "y"]));;
- : exp = Var "x"