Toggle navigation
Egison
Documentations
Try It Out
Online Tools
Online Egison Interpreter
Online Egison Tutorial
Online Demonstrations
Poker Hands
Mahjong
Prime Numbers
Trees
Graph (Bellman-Ford Algorithm)
Randomized 3-SAT
Time-Series Data
Math
Blog
Community
Vector Analysis
Nov 2 2017
-- -- Tensor Arithmetics -- assertEqual "scalar + tensor" (1 + [| 1, 2, 3 |]) [| 2, 3, 4 |] assertEqual "tensor + scalar" ([| 1, 2, 3 |] + 1) [| 2, 3, 4 |] assertEqual "tensor + tensor (same index)" ([| 1, 2, 3 |]_i + [| 1, 2, 3 |]_i) [| 2, 4, 6 |]_i assertEqual "tensor + tensor (outer product)" ([| 10, 20, 30 |] + [| 1, 2, 3 |]) [| [| 11, 12, 13 |], [| 21, 22, 23 |], [| 31, 32, 33 |] |] assertEqual "tensor + 2D tensor" ([| 100, 200, 300 |]_i + [|[| 1, 2, 3 |], [| 10, 20, 30 |]|]_j_i) [| [| 101, 110 |], [| 202, 220 |], [| 303, 330 |] |]_i_j assertEqual "2D tensor + 1D tensor" ([|[| 11, 12 |], [| 21, 22 |], [| 31, 32 |]|]_i_j + [| 100, 200, 300 |]_i) [| [| 111, 112 |], [| 221, 222 |], [| 331, 332 |] |]_i_j -- -- Derivative -- assertEqual "partial derivative of f(x,y,z)" (∂/∂ (f x y z) x) (f|1 x y z) assertEqual "derivative of vector function" (∂/∂ [| (f x), (g x) |] x) [| (f|1 x), (g|1 x) |] assertEqual "gradient of f(x,y,z)" (∂/∂ (f x y z) [| x, y, z |]) [| (f|1 x y z), (f|2 x y z), (f|3 x y z) |] assertEqual "apply partial derivatives" ([| (∂/∂ $ x), (∂/∂ $ y) |] (f x y)) [| (f|1 x y), (f|2 x y) |] assertEqual "Jacobian matrix" ([| (∂/∂ $ x), (∂/∂ $ y) |] [| (f x y), (g x y) |]) [| [| (f|1 x y), (g|1 x y) |], [| (f|2 x y), (g|2 x y) |] |] -- -- Nabla -- def ∇ := ∂/∂ assertEqual "nabla f" (∇ (f x y) [| x, y |]) [| (f|1 x y), (f|2 x y) |] assertEqual "nabla vector" (∇ [| (f x y), (g x y) |] [| x, y |]) [| [| (f|1 x y), (f|2 x y) |], [| (g|1 x y), (g|2 x y) |] |] -- -- Contraction -- assertEqual "element-wise product" (contract ([|1, 2, 3|]~i * [|10, 20, 30|]_i)) [10, 40, 90] def trace %t := withSymbols [i] sum (contract t~i_i) assertEqual "trace of matrix" (trace [|[|10, 20, 30|], [|20, 40, 60|], [|30, 60, 90|]|]) 140 -- -- Divergence -- def div %t %x := trace (!(∇ t x)) assertEqual "divergence" (div [| (f x y z), (g x y z), (h x y z) |] [| x, y, z |]) (f|1 x y z + g|2 x y z + h|3 x y z) -- -- Taylor Expansion -- def multivariateTaylorExpansion $f %xs %ys := withSymbols [h] let hs := generateTensor (\[x] -> h_x) (tensorShape xs) in map2 (*) (map (\n -> 1 / fact n) nats0) (map (compose (\e -> V.substitute xs ys e) (\e -> V.substitute hs (withSymbols [i] xs_i - ys_i) e)) (iterate (compose (\e -> ∇ e xs) (\e -> V.* hs e)) f)) def taylorExpansion $f $x $a := multivariateTaylorExpansion f [|x|] [|a|] assertEqual "Taylor expansion of f(x)" (take 3 (taylorExpansion (f x) x 0)) [f 0, x * (f|1 0), x^2 * (f|1|1 0) / 2] assertEqual "Multivariate Taylor expansion" (take 3 (multivariateTaylorExpansion (f x y) [| x, y |] [| 0, 0 |])) [f 0 0, x * f|1 0 0 + y * f|2 0 0, (x^2 * f|1|1 0 0 + x * y * f|2|1 0 0 + y * x * f|1|2 0 0 + y^2 * f|2|2 0 0) / 2]
Links
Back to the Table of Contents
This website in other langauge:
English
,
日本語