Skip to content

Instantly share code, notes, and snippets.

@vito
Created December 1, 2010 04:41
Show Gist options
  • Save vito/722958 to your computer and use it in GitHub Desktop.
Save vito/722958 to your computer and use it in GitHub Desktop.
derivatives in atomo
define: *variable* as: @no-variable
(b: Block) derive :=
with: *variable* as: b arguments head name do: {
Block: b contents (map: @derive) arguments: b arguments
}
`(~x ^ ~y) derive :=
condition: {
-- constant exponent
y constant? ->
`((~y * (~x ^ (~y - 1))) * ~(x derive))
-- constant base
x constant? ->
`((~x ^ ~y) * ~(y derive))
otherwise ->
`(e ^ (~y * ~x ln)) derive
}
`(~x sqrt) derive := `(~x ^ 1/2) derive
`(~x ln) derive := `(1 / ~x)
`(~x log: ~a) derive :=
`(1 / (~x * ~a ln))
`(~x / ~y) derive :=
`((~(x derive) * ~y - ~x * ~(y derive)) / (~y ^ 2))
`(~x * ~y) derive :=
`(~x * ~(y derive) + ~(x derive) * ~y)
`(~x + ~y) derive := `(~(x derive) + ~(y derive))
`(~x - ~y) derive := `(~(x derive) - ~(y derive))
`(~x sin) derive := `(~x cos * ~(x derive))
`(~x cos) derive := `(-1 * (~x sin) * ~(x derive))
`(~x tan) derive := `((~x sec) ^ 2)
(e: Expression) derive :=
condition: {
e type == @primitive -> 0
e respect-to? -> 1
otherwise -> `(~e unknown)
}
load: "util"
load: "derive"
load: "simplify"
{ x | 3 * (x ^ 3/2) } derive simplify print
{ x | (2 * x) cos } derive simplify print
{ x | (5 * x) * x sin } derive simplify print
{ x | (3 * (x ^ 3/2)) + (2 * x) cos - ((5 * x) * x sin) } derive simplify print
(b: Block) simplify :=
{ es = b contents map:
{ e |
old = e
new = old simplify
while: { old /= new } do: {
old = new
new = old simplify
}
new
}
Block: es arguments: b arguments
} call
`(e ln) simplify := `1
`(~x ^ 1/2) simplify :=
`(~x sqrt)
`(e ^ (~y ln * ~z)) simplify :=
`(~y ^ ~z)
`(e ^ (~y * ~z ln)) simplify :=
`(~z ^ ~y)
`(0 * ~_) simplify := `0
`(~_ * 0) simplify := `0
`(1 * ~y) simplify := y
`(~y * 1) simplify := y
`(0 + ~y) simplify := y
`(~y + 0) simplify := y
`(0 - ~y) simplify := y
`(~y - 0) simplify := y
`(~(x: Primitive) * (~(y: Primitive) * ~z)) simplify :=
`(~(evaluate: x * evaluate: y) * ~z)
`((~x * ~(y: Primitive)) * ~(z: Primitive)) simplify :=
`(~(evaluate: y * evaluate: z) * ~x)
`((~(x: Primitive) * ~y) * ~(z: Primitive)) simplify :=
`(~(evaluate: x * evaluate: z) * ~y)
`(~(x: Primitive) * (~(y: Primitive) * ~z)) simplify :=
`(~(evaluate: x * evaluate: y) * ~z)
`(~(x: Primitive) * (~y * ~(z: Primitive))) simplify :=
`(~(evaluate: x * evaluate: z) * ~y)
`(~(x: Primitive) * ~(y: Primitive)) simplify :=
`(~(evaluate: x * evaluate: y))
`(~x * (1 / ~z)) simplify :=
if: (x == z)
then: { `1 }
else: { `(~(x simplify) * (1 / ~(z simplify))) }
`(~(x: Primitive) / ~(y: Primitive)) simplify :=
`(~(evaluate: x / evaluate: y))
`(~(x: Primitive) + ~(y: Primitive)) simplify :=
`(~(evaluate: x + evaluate: y))
`(~(x: Primitive) - ~(y: Primitive)) simplify :=
`(~(evaluate: x - evaluate: y))
(e: Expression) simplify :=
e type match: {
@(dispatch: _) ->
`Dispatch new: e particle to: e targets (map: @simplify)
_ -> e
}
(e: Expression) respect-to? :=
e type == @(dispatch: @single) && e name == *variable*
(e: Expression) constant? :=
e type == @primitive || e == `e
@vito
Copy link
Author

vito commented Dec 2, 2010

more simplification:

{ x | 9/2 * x sqrt }
{ x | -2 * (2 * x) sin }
{ x | ((5 * x) * x cos) + (5 * x sin) }
{ x | ((9/2 * x sqrt) + (-2 * (2 * x) sin)) - (((5 * x) * x cos) + (5 * x sin)) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment