Liking cljdoc? Tell your friends :D

Clojars Project

diff.clj

Derivative of single variable functions and expression simplifier

Installation

Leiningen/Boot

[com.github.jbytecode/diffclj "0.1.0"]

Clojure CLI/deps.edn

com.github.jbytecode/diffclj {:mvn/version "0.1.0"}

Gradle

implementation("com.github.jbytecode:diffclj:0.1.0")

Maven

<dependency>
  <groupId>com.github.jbytecode</groupId>
  <artifactId>diffclj</artifactId>
  <version>0.1.0</version>
</dependency>

Examples

Importing the library

In your ns definition add

(ns [your-namespace]
  (require [diffclj.core :refer :all]))

to use the library or if you are using REPL, type

(require '[diffclj.core :refer :all])

and enter.

diffclj.core=> (deriv '(* x x))
;; (+ (* 1 x) (* 1 x))

diffclj.core=> (simplify (deriv '(* x x)))
;; (* 2 x)
diffclj.core=> (deriv '(* 2 x))
;; (+ (* 0 x) (* 1 2))

diffclj.core=> (simplify (deriv '(* 2 x)))
;; 2
diffclj.core=> (def equation '(- 5 (log (* 2 x))))
;; #'diffclj.core/equation

diffclj.core=> (deriv equation)
;; (- 0 (/ (+ (* 0 x) (* 1 2)) (* 2 x)))

diffclj.core=> (simplify (deriv equation))
;; (- 0 (/ 2 (* 2 x)))

diffclj.core=> (def x 10)
;; #'diffclj.core/x

diffclj.core=> (eval (simplify (deriv equation)))
;; -1/10
diffclj.core=> (deriv '(+ (* 3 (pow x 3)) (* 5 (pow x 2))))
;; (+ (+ (* 0 (pow x 3)) (* (* (pow x 3) (+ (* 0 (log x)) (* (/ 1 x) 3))) 3)) (+ (* 0 (pow x 2)) (* (* (pow x 2) (+ (* 0 (log x)) (* (/ 1 x) 2))) 5)))

diffclj.core=> (simplify (deriv '(+ (* 3 (pow x 3)) (* 5 (pow x 2)))))
;; (+ (* (* (pow x 3) (* (/ 1 x) 3)) 3) (* (* (pow x 2) (* (/ 1 x) 2)) 5))

Higher order derivatives

diffclj.core=> (deriv (deriv '(log x)))
;; (/ (- (* 0 x) (* 1 1)) (* x x))

diffclj.core=> (simplify (deriv (deriv '(log x))))
;; (/ -1 (pow x 2.0))

diffclj.core=> (def x 10)
;; #'diffclj.core/x

diffclj.core=> (eval (simplify (deriv (deriv '(log x)))))
;; -0.01

Logarithmic rule

diffclj.core=> (deriv '(pow x x))
;; (* (pow x x) (+ (* 1 (log x)) (* (/ 1 x) x)))

diffclj.core=> (simplify (deriv '(pow x x)))
;; (* (pow x x) (+ (log x) (* (/ 1 x) x)))

Defined functions

(declare deriv-cosec         ;; cosecant
         deriv-sec           ;; secant
         deriv-cot           ;; cotangent
         deriv-tan           ;; tangent
         deriv-cos           ;; cosine
         deriv-sin           ;; sine
         deriv-exp           ;; exponential
         deriv-plus          ;; +
         deriv-minus         ;; -
         deriv-product       ;; *
         deriv-divide        ;; /
         deriv-power         ;; ^
         deriv-log10         ;; Logarithm with base 10
         deriv-log2          ;; Logarithm with base 2
         deriv-log           ;; Natural logarithm
         deriv-sqrt          ;; Square root
         deriv-list        
         deriv)

Basic symbolic simplification

diffclj.core=> (simplify '(+ 2 5))
;; 7

diffclj.core=> (simplify '(* (+ x 5) (+ x 5)))
;; (pow (+ x 5) 2.0)

diffclj.core=> (simplify '(* 1 (sin (/ Math/PI 2))))
;; (sin (/ Math/PI 2))

diffclj.core=> (simplify '(/ 2 (/ x x)))
;; 2

diffclj.core=> (simplify '(exp (* x 0)))
;; 1

Can you improve this documentation? These fine people already did:
jbytecode & Mehmet Hakan Satman
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close