Advertisement
Guest User

Untitled

a guest
May 7th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn evaluate [expression, applyMap] ((.evaluate expression) applyMap))
  2. (defn toString [expression] (.toStr expression))
  3. (defn diff [expression, diffVarName] ((.diff expression) diffVarName))
  4.  
  5. (definterface BaseInterface
  6.   (evaluate [])
  7.   (toStr [])
  8.   (diff []))
  9.  
  10. (declare ZERO)
  11. (declare ONE)
  12. (declare TWO)
  13.  
  14. (deftype Const [value]
  15.   BaseInterface
  16.   (evaluate [this] (fn [applyMap] value))
  17.   (toStr [this] (format "%.1f" (double value)))
  18.   (diff [this] (fn [diffVarName] ZERO)))
  19.  
  20. (defn Constant [value] (Const. value))
  21.  
  22. (def ZERO (Constant 0))
  23. (def ONE (Constant 1))
  24. (def TWO (Constant 2))
  25.  
  26. (deftype Var [varName]
  27.   BaseInterface
  28.   (evaluate [this] (fn [applyMap] (applyMap varName)))
  29.   (toStr [this] (str varName))
  30.   (diff [this] #(if (= % varName) ONE ZERO)))
  31.  
  32. (defn Variable [varName] (Var. varName))
  33.  
  34. (deftype newOp [evalFun, opName, args, diffFun]
  35.   BaseInterface
  36.   (evaluate [this] #(apply evalFun (map (fn [x] (evaluate x %)) args)))
  37.   (toStr [this] (str "(" opName " " (clojure.string/join " " (map toString args)) ")"))
  38.   (diff [this] #(apply diffFun (concat args (map (fn [x] (diff x %)) args)))))
  39.  
  40. (defn Add [& args] (newOp. +, "+", args,
  41.                            (fn [a b da db] (Add da db))))
  42.  
  43. (defn Subtract [& args] (newOp. -, "-", args,
  44.                                 (fn [a b da db] (Subtract da db))))
  45.  
  46. (defn Multiply [& args] (newOp. *, "*", args,
  47.                                 (fn [a b da db] (Add
  48.                                                   (Multiply da b)
  49.                                                   (Multiply a db)))))
  50.  
  51. (defn Divide [& args] (newOp. #(/ %1 (double %2)), "/", args,
  52.                               (fn [a b da db] (Divide
  53.                                                 (Subtract
  54.                                                   (Multiply da b)
  55.                                                   (Multiply a db))
  56.                                                 (Multiply b b)))))
  57.  
  58. (defn Negate [& arg] (newOp. -, "negate", arg,
  59.                              (fn [a da] (Negate da))))
  60.  
  61. (declare Sinh)
  62. (declare Cosh)
  63.  
  64. (defn Sinh [& arg] (newOp. #(Math/sinh %), "sinh", arg,
  65.                            (fn [a da] (Multiply da (Cosh a)))))
  66.  
  67. (defn Cosh [& arg] (newOp. #(Math/cosh %), "cosh", arg,
  68.                            (fn [a da] (Multiply da (Sinh a)))))
  69.  
  70. (def operations {'+      Add,
  71.                  '-      Subtract,
  72.                  '*      Multiply,
  73.                  '/      Divide,
  74.                  'negate Negate,
  75.                  'sinh   Sinh,
  76.                  'cosh   Cosh
  77.                  })
  78.  
  79. (defn parse [expression]
  80.   (cond
  81.     (number? expression) (Constant expression)
  82.     (symbol? expression) (Variable (str expression))
  83.     (list? expression) (apply (operations (first expression)) (map parse (rest expression)))
  84.     ))
  85.  
  86. (defn parseObject [s] (parse (read-string s)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement