项目作者: oleks

项目描述 :
Research Apparatus for Automatic Differentiation
高级语言: Python
项目地址: git://github.com/oleks/mechanics.git
创建时间: 2017-03-24T17:34:01Z
项目社区:https://github.com/oleks/mechanics

开源协议:

下载


Reproduce my Development Environment, Reproduce my Results!

The primary entry-points for working with this apparatus are the respective
run-*.sh shell scripts. These employ Docker to run
the Haskell, or Idris apparatus,
respectively. This bears the benefits of (1) a minimal-effort install, and (2)
a reproducible development environment.

To get started, install Docker, and run the respective shell script to start up
a Docker image with the respective tools (GHC and/or Idris) installed.

Currently, the Haskell implementation is (much) outdated. To play
with the Idris implementation:

  1. $ ./run-idris.sh
  2. ~/mechanics/idris $ make && ./Main.bin

Grammar

  1. Expr := Number
  2. | VarName
  3. | Expr `+` Expr
  4. | Expr `-` Expr
  5. | Expr `*` Expr
  6. | Expr `/` Expr
  7. | `let` VarName `=` Expr `in` Expr
  8. | `if` Expr `then` Expr `else` Expr
  9. | `(` Expr `)`
  10. | `<` Expr `,` Expr `>`
  11. | FunName Args
  12. Args := `(` ArgList `)`
  13. ArgList :=
  14. | NonEmptyArgList
  15. NonEmptyArgList := Expr
  16. | Expr `,` NonEmptyArgList

Built-in Functions

The built-in functions are fst, snd, dup, and diff. diff takes a
variable name and an expression, and yields the expression differentiated with
respect to the given variable name. The other built-in functions work as
follows:

  1. > fst(<x,y>)
  2. Just x
  3. > snd(<x,y>)
  4. Just y
  5. > dup(x)
  6. Just < x, x >

Precedence and Associativity

  • let and if have the lowest precedence.
  • + and - are left-associative and have higher precedence than let and if.
  • * and / are left-associative and have higher precedence than + and -.

As usual, parentheses can be used to override precedence and associativity
rules.

Whitespace Rules

  • let and if are followed by at least one whitespace character.
  • in, then, and else are surrounded by at least one whitespace character.
  • All other tokens are separated by arbitrary whitespace.

Derivatives

lets

  1. Judgement: d/dx e1 ~> e2, where x \in VarName and e1, e2 \in Expr.
  2. Rules:
  3. d/dx e2[e1/y] ~> e3
  4. --------------------------- (if x /= y)
  5. d/dx let y = e1 in e2 ~> e3
  6. d/dy e2[e1/x] ~> e3
  7. --------------------------- (where y is fresh)
  8. d/dx let x = e1 in e2 ~> e3

ifs

ifs are differentiation according to the following principles:

  1. If the derivatives of the branches unify, then the derivative of a branch is
    the unified derivative.

  2. Otherwise, the derivative of a branch is a branching derivative, with the
    original condition.

Unification, for now, is trivial — it is mere equality.

Gotcha’s

The Idris Interpreter is Lazy

The Idris interpreter is lazy, while the Idris run-time is eager, unless
specified otherwise. This makes the interpreter useless for the shotgun testing
of run-time behaviour. Things that terminate in the interpreter, might not
terminate at run-time.