Stimulator

機械学習とか好きな技術話とかエンジニア的な話とかを書く

MacにJuliaしてSublimeTextする

OS X Yosemite 10.10.3にプログラミング言語"Julia"を導入して、Sublime Text 2の環境を整える話。

- はじめに -

Juliaは科学数値計算向けに開発されている言語です。
MatlabやR、Octave、numpy、scipyのような記述でかつ高速に処理を行えます。
http://julialang.orgにもあるように他言語に比べてとにかく早いです。めっちゃ早いです。

最近では、電脳戦の将棋AI等にも使われ、少しずつ認知度が上がってます。
私は機械学習を専攻していますが、Pythonで設計してJuliaで実験するのが日常になっています。

Juliaの構文以外で好きなところして

  • 早い > 嬉しいです
  • Juliaの中身がJuliaで書かれている > いざという時なんとかなります
  • Python連携 > Pythonのコードやライブラリを呼び出したり、Pythonからの移行が非常に楽です。

この辺が気に入って書いています。
後以下の記事とかを暇潰しに読んでいたところハマりました。


- Juliaのインストール -

参考:https://github.com/staticfloat/homebrew-julia/

Macにhomebrewをインストールしておいて

$ brew update
$ brew install gfortran
$ brew tap staticfloat/julia
$ brew install julia

起動した
f:id:vaaaaaanquish:20150519061826p:plain

- エラーと解決策 -

- brew update

brewのアップデートでエラーが出て

error: Your local changes to the following files would be overwritten by merge:
    Library/Formula/boost.rb
Please, commit your changes or stash them before you can merge.
error: Your local changes to the following files would be overwritten by merge:
    Library/Formula/boost-python.rb
    Library/Formula/gflags.rb
    Library/Formula/glog.rb
    Library/Formula/lmdb.rb
    Library/Formula/protobuf.rb
    Library/Formula/snappy.rb
    Library/Formula/szip.rb
Please, commit your changes or stash them before you can merge.
Aborting
Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master

この前機械学習ライブラリのCaffeを入れた時に書き換えたのでgit mergeできないと怒られた。
強制的に上書きして解決した。

$ cd $(brew --prefix)
$ git fetch origin
$ git reset --hard origin/master

これで行けるでしょって思ったら

error: Your local changes to the following files would be overwritten by merge:
    opencv.rb
Please, commit your changes or stash them before you can merge.
Aborting
Error: Failed to update tap: homebrew/science

scienceの中身も書き換わっていたので、/usr/local/Library/Taps/のscienceのバックアップを取ってからtap

$ cd /usr/local/Library/Taps/homebrew
$ mv homebrew-science/ homebrew-science-bak/
$ brew tap homebrew/science

brew update上手くいきました。

- install gfortran

あとgfortranのインストール時にもエラーが出た

Error: No available formula for gfortran 
GNU Fortran is now provided as part of GCC, and can be installed with:
  brew install gcc

gccの中に入ってるからgccインストールすれば良いとの事。gccは以前入れていたのでスルーした。
入っていなければエラー通り

$ brew install gcc

多分大丈夫。

- Sublime Textでbulid -

Command+bでビルドしたいので、[Tools]>[Bulid System]>[New Build System](日本語だと[ツール]>[ビルドシステム]から[ビルドシステムの追加])を選択。よしなに設定を書いた。

{
    "cmd": ["julia", "$file"],
    "selector": "source.julia"
}

 

- Packageのインストール -

Package Installerが導入されているなら、いつものCommand+Shift+PでInstall Packageを選択し

を入れると吉っぽい。

(他にもIJuliaのPackageもあったけどST3にしか対応してないので入れてない)

- テスト -

1linerのHello World(Fizzbuzz)
GitHub - vaaaaanquish/Julia_1liner_helloworld_fizzbuzz: show title

println([(x==0) ? "Hello, World!" : (x%3==0) && (x%5==0) ? "FizzBuzz" : (x%3==0) ? "Fizz" : (x%5==0) ? "Buzz" : x for x in 0:20])

 

- まとめ -

問題なく動いてハッピー。
Juliaにコード規約とかそれ関連のSTPackageとかできないかなと思う日々である。