下の math++.el は math.el を基にしたものですが, 実は複数行の編集に対応して
いない等がわかったので, mathematica.el を基に一からモードを書き直したもの
を
math++.el
として上げておきました。
Mathematica愉快すぎる。
2次元のMixture of Gaussiansのコードはこんな感じになるようです
(NormalDistribution[]は2次元には対応していない。)
(*
gaussian.ma
Sat Jun 20 00:12:25 2009 daichi<at>cslab.kecl.ntt.co.jp
*)
gauss[x_,y_,mu_,S_] :=
Block[
{v = {x,y}},
1 / (2 Pi Sqrt[Abs[Det[S]]])
Exp[- (v - mu) . Inverse[S] . (v - mu) / 2]
]
gauss::usage = "
gauss[x,y,mu,S] returns a Gaussian density of mean mu, covariance S
at the coordinate {x,y}.";
mgauss[x_,y_,Pi_,Mu_,S_] :=
If[ MatchQ[{}, Pi],
0,
Block[
{pi = First[Pi],
mu = First[Mu],
s = First[S]},
pi gauss[x, y, mu, s] +
mgauss[x, y, Rest[Pi], Rest[Mu], Rest[S]]
]
]
mgauss::usage = "
mgauss[x,y,Pi,Mu,S] returns a mixture of Gaussians of
gauss[{x,y},Mu[[1]],S[[1]]], gauss[{x,y},Mu[[2]],S[[2]]], ...
with mixing coefficients Pi.";
後ろのコードは, scheme で書くと
(define (mgauss x y Pi Mu S)
(if (null? Pi)
0
(let ((pi (car Pi))
(mu (car Mu))
(s (car S)))
(+ (* pi (gauss x y mu s))
(mgauss x y (cdr Pi) (cdr Mu) (cdr S))))))
で, まんまLispなのがよくわかる感じです。(car=First, cdr=Restらしい。)