Upgrade to Pro — share decks privately, control downloads, hide ads and more …

新機能で知るEgison

Avatar for kotapiku kotapiku
December 04, 2018

 新機能で知るEgison

約1年の間に追加された主な機能についての紹介です.

Avatar for kotapiku

kotapiku

December 04, 2018

Other Decks in Programming

Transcript

  1. .ΦϓγϣϯͰग़ྗΛ੾Γସ͑Δ % egison -M asciimath > [| [| 1 0

    0 |] [| 0 1 0 |] [| 0 0 1 |] |] #asciimath|((1, 0, 0), (0, 1, 0), (0, 0, 1))|# % egison -M latex > [| [| 1 0 0 |] [| 0 1 0 |] [| 0 0 1 |] |]
 #latex|\begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end{pmatrix}|# % egison -M mathematica > [| [| 1 0 0 |] [| 0 1 0 |] [| 0 0 1 |] |] #mathematica|{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}|#
  2. 'VODUJPO4ZNCPM > (define $f (function [x y])) > f f

    > (d/d f x) f|x > (d/d (d/d f x) y) f|x|y > (define $x (* r (cos θ))) > (define $y (* r (sin θ))) > (d/d f r) (+ (* f|x (cos θ)) (* f|y (sin θ))) G Y Z Λఆٛ ඍ෼͕៉ྷʹ͔͚·͢ ߹੒ؔ਺ͷඍ෼΋Ͱ͖·͢
  3. ඍ෼͸Ͳ͏࣮ݱ͞Ε͍ͯΔ͔ (define $∂/∂ (lambda [$f *$x] (match f math-expr {

    . . . [<func _ $argnames $args _> (sum (map2 (lambda[$s $r] (* (user-refs f {s}) (∂/∂ r x))) argnames args))] . . . }))) > (define $f (function [x y])) > (define $x (* r (cos θ))) > (define $y (* r (sin θ))) > (d/d f r) (+ (* f|x (cos θ)) (* f|y (sin θ))) BSHOBNFT\lYzlZz^ BSHT\ S DPTВ  S TJOВ ^
  4. 'VODUJPO4ZNCPM > (define $f (function [x y])) > f f

    > (d/d f x) f|x > (d/d (d/d f x) y) f|x|y > (define $x (* r (cos θ))) > (define $y (* r (sin θ))) > (d/d f r) (+ (* f|x (cos θ)) (* f|y (sin θ))) ධՁ஋Ͱ͸ͳ໊͘લΛग़ྗ͢Δ
  5. (define $g11 (function [x y])) (define $g22 (function [x y]))

    (define $g33 (function [x y])) (define $g_i_j [| [| g11 0 0 |] [| 0 g22 0 |] [| 0 0 g33 |] |]_i_j) (d/d g_i_j x) ͜ͷ··Ͱ͸ʜ w ֤ཁૉʹ͍ͭͯॻ͘ͷ͕໘౗ w H͕H@@ͳͷ͔Hddͳͷ͔෼͔Βͳ͍
  6. ͜͏ॻ͚ΔΑ͏ʹ͠·ͨ͠ > (define $g__ (generate-tensor ɹ(match-lambda [integer integer] ɹɹ{[[$n ,n]

    (function [x y z])] [[_ _] 0]}) {3 3})) > g_i_j [| [| g_1_1 0 0 |] [| 0 g_2_2 0 |] [| 0 0 g_3_3 |] |]_i_j > (d/d g_i_j x) [| [| g_1_1|x 0 0 |] [| 0 g_2_2|x 0 |] [| 0 0 g_3_3|x |] |]_i_j ςϯιϧͷ໊લ ఴࣈͷ্Լ ໊લΛࣗಈͰ͚ͭͯ͘ΕΔ ඍ෼΋ͪΌΜͱදࣔ͞ΕΔ
  7. %'4QBUUFSO > (take 10 (match-all nats (set integer) [(dfs <cons

    $m <cons $n <cons $l _>>>) [m n l]])) {[1 1 1] [1 1 2] [1 1 3] [1 1 4] [1 1 5] [1 1 6] [1 1 7] [1 1 8] [1 1 9] [1 1 10]} > (take 10 (match-all nats (set integer) [<cons $m (dfs <cons $n <cons $l _>>)> [m n l]])) {[1 1 1] [2 1 1] [3 1 1] [4 1 1] [5 1 1] [6 1 1] [7 1 1] [8 1 1] [9 1 1] [10 1 1]} > (take 10 (match-all nats (set integer) [(dfs <cons $m (bfs <cons $n <cons $l _>>)>) [m n l]])) {[1 1 1] [1 1 2] [1 2 1] [1 1 3] [1 2 2] [1 3 1] [1 1 4] [1 2 3] [1 3 2] [1 4 1]} > (take 10 (match-all nats (set integer) [(dfs <cons $m <cons $n (bfs <cons $l _>)>>) [m n l]])) {[1 1 1] [1 1 2] [1 1 3] [1 1 4] [1 1 5] [1 1 6] [1 1 7] [1 1 8] [1 1 9] [1 1 10]}
  8. → MState{[<cons $m <cons $n _>> nats (multiset integer)]} env

    {} → MState{[$m 1 integer] [<cons $n _> {2 3 4 ...} (multiset integer)]} env {} MState{[$m 2 integer] [<cons $n _> {1 3 4 ...} (multiset integer)]} env {} … → MState{[$m 1 something] [<cons $n _> {2 3 4 ...} (multiset integer)]} env {} → MState{[<cons $n _> {2 3 4 ...} (multiset integer)]} env {[m 1]} → MState{[$n 2 integer] [_ {3 4 ...} (multiset integer)]} env {[m 1]} MState{[$n 3 integer] [_ {2 4 ...} (multiset integer)]} env {[m 1]} … → MState{[$n 2 something] [_ {3 4 ...} (multiset integer)]} env {[m 1]} → MState{[_ {3 4 ...} (multiset integer)]} env {[m 1] [n 2]} → MState{} env {[m 1] [n 2]} ύλʔϯϚονͷ༷ࢠ লུͯ͠
 Ұͭ໨ͷ.4UBUFͷΈ
 ਐΊ͍ͯ·͢ (match-all nats (multiset integer) [<cons $m <cons $n _>> {m n}])
  9. 0SEFSFE031BUUFSO > (match-all (between 1 9) (multiset integer) [<cons (&

    (|* ,1 ,2 ,3) $x) <cons (& (|* ,4 ,5 ,6) $y) <cons (& (|* ,7 ,8 ,9) $z) _>>> {x y z}]) {{1 4 7} {1 4 8} {1 4 9} {1 5 7} {1 5 8} {1 5 9} {1 6 7} {1 6 8} {1 6 9} {2 4 7} {2 4 8} {2 4 9} {2 5 7} {2 5 8} {2 5 9} {2 6 7} {2 6 8} {2 6 9} {3 4 7} {3 4 8} {3 4 9} {3 5 7} {3 5 8} {3 5 9} {3 6 7} {3 6 8} {3 6 9}}
  10. #FTUTIVGqFͷղ౴ྫ (define $shuffle-string (lambda [$s] (pack (shuffle (unpack s))))) (define

    $shuffle (lambda [$lst] (match [lst (randomize lst)] [(list eq) (multiset eq)] {[[(loop $i [1 $n] <cons $x_i ...> <nil>) (loop $i [1 n] (dfs <cons (& (|* !,x_i ,x_i) $y_i) ...>) <nil>)] (map 1#y_%1 (between 1 n))]}))) (shuffle-string “tree”) ; “eetr” γϟοϑϧલͷจࣈ\YYYʜYO^ γϟοϑϧޙͷจࣈ\ZZZʜZO^
  11. (define $list (lambda [$a] (matcher {[<nil> [] {[{} {[]}] [_

    {}]}] [<cons $ $> [a (list a)] {[{$x @$xs} {[x xs]}] [_ {}]}] [<snoc $ $> [a (list a)] {[{@$xs $x} {[x xs]}] [_ {}]}] [<join $ $> [(list a) (list a)] {[$tgt (match-all tgt (list a) [(loop $i [1 $n] <cons $xa_i ...> $rs) [(foldr (lambda [%i %r] {xa_i @r}) {} (between 1 n)) rs]])]}] [<nioj $ $> [(list a) (list a)] {[$tgt (match-all tgt (list a) [(loop $i [1 $n] <snoc $xa_i ...> $rs) [(foldr (lambda [%i %r] {@r xa_i}) {} (between 1 n)) rs]])]}] [,$val [] {[$tgt (if (eq? val tgt) {[]} {})]}] [$ [something] {[$tgt {tgt}]}] }))) Before
  12. list(a) = matcher | <nil> as () -> | []

    -> {()} | _ -> {} | <cons $ $> as (a, list(a)) -> | [$x, @$xs] -> {(x, xs)} | _ -> {} | <snoc $ $> as (a, list(a)) -> | [@$xs, $x] -> {(x, xs)} | _ -> {} | <join $ $> as (list(a), list(a)) -> | $tgt -> matchAll tgt as list(a) | loop($i, [1, $n], <cons $xa_i ...>, $rs) -> (foldr((%i %r -> [xa_i, @r]), [], 1..n), rs) | <nioj $ $> as (list(a), list(a)) -> | $tgt -> matchAll tgt as list(a) | loop($i, [1, $n], <snoc $xa_i ...>, $rs) -> (foldr((%i %r -> [@r, xa_i]), [], 1..n), rs) | $val as () -> | $tgt -> if val == tgt then {()} else {} | $ as (something) -> | $tgt -> {tgt} After
  13. (define $twin-primes (match-all primes (list integer) [<join _ <cons $p

    <cons ,(+ p 2) _>>> [p (+ p 2)]])) (take 10 twin-primes) twinPrimes = matchAll primes as list(integer) | _ <++> p <:> (p + 2) <:> _ -> [p, p + 2] take(10, twinPrimes) Before After