mots quotidiens.
Daichi Mochihashi (持橋大地) daichi <at> ism.ac.jp by hns, version 2.10-pl1.

先月 2009年06月 来月
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

2009年06月20日() [n年日記]

#1 -

下の 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らしい。)

1 days displayed.
タイトル一覧
カテゴリ分類
 なかのひと
Powered by hns-2.10-pl1, HyperNikkiSystem Project