Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jbytecode/diff.clj
Derivative of single variable functions and expression simplifier
https://github.com/jbytecode/diff.clj
Last synced: 22 days ago
JSON representation
Derivative of single variable functions and expression simplifier
- Host: GitHub
- URL: https://github.com/jbytecode/diff.clj
- Owner: jbytecode
- License: mit
- Created: 2021-08-23T12:13:39.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-29T20:07:19.000Z (about 3 years ago)
- Last Synced: 2024-10-15T16:57:15.561Z (2 months ago)
- Language: Clojure
- Size: 48.8 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![Clojars Project](https://img.shields.io/clojars/v/com.github.jbytecode/diffclj.svg)](https://clojars.org/com.github.jbytecode/diffclj)
# diff.clj
Derivative of single variable functions and expression simplifier# Installation
## Leiningen/Boot
```clojure
[com.github.jbytecode/diffclj "0.1.3"]
```## Clojure CLI/deps.edn
```clojure
com.github.jbytecode/diffclj {:mvn/version "0.1.3"}
```## Gradle
```gradle
implementation("com.github.jbytecode:diffclj:0.1.3")
```## Maven
```XML
com.github.jbytecode
diffclj
0.1.3```
# Examples
## Importing the library
In your ```ns``` definition add
```clojure
(ns [your-namespace]
(require [diffclj.core :refer :all]))
```to use the library or if you are using REPL, type
```clojure
(require '[diffclj.core :refer :all])
```and enter.
```clojure
diffclj.core=> (deriv '(* x x))
;; (+ (* 1 x) (* 1 x))diffclj.core=> (simplify (deriv '(* x x)))
;; (* 2 x)
``````clojure
diffclj.core=> (deriv '(* 2 x))
;; (+ (* 0 x) (* 1 2))diffclj.core=> (simplify (deriv '(* 2 x)))
;; 2
``````clojure
diffclj.core=> (def equation '(- 5 (log (* 2 x))))
;; #'diffclj.core/equationdiffclj.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/xdiffclj.core=> (eval (simplify (deriv equation)))
;; -1/10
``````clojure
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
```clojure
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/xdiffclj.core=> (eval (simplify (deriv (deriv '(log x)))))
;; -0.01
```## Logarithmic rule
```clojure
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
```clojure
(declare
deriv-acot ;; Inverse cotangent
deriv-atan ;; Inverse tangent
deriv-acos ;; Inverse cosine
deriv-asin ;; Inverse sine
deriv-csch ;; hyperbolic cosecant
deriv-sech ;; hyperbolic secant
deriv-coth ;; hyperbolic cotangent
deriv-tanh ;; hyperbolic tangent
deriv-cosh ;; hyperbolic cosine
deriv-sinh ;; hyperbolic sine
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
```clojure
diffclj.core=> (simplify '(+ 2 5))
;; 7diffclj.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)))
;; 2diffclj.core=> (simplify '(exp (* x 0)))
;; 1
```