LoginSignup
6
3

More than 3 years have passed since last update.

MacでMaximaの環境構築を行った時の備忘録メモ

Posted at

過去記事(第1回 AI実装検定【A級】に合格したので色々まとめてみた)で投稿した様に、AI実装検定を受検しましたが、受験時に線形代数学の内容を忘れてしまっていることを痛感しました。機械学習の専門書も少し高度な内容の書籍となると、行列やベクトルで記載した数式が大量に登場します。今後を見据え、改めて線形代数学を再学習しようと思い立ちました。手計算も良いですが、数式処理ソフトの力を借りるのも悪くないと思い、Maximaをセットアップした時の内容を備忘録メモとしてまとめておくことにしました。

Maximaとは?

MITで元々開発されたMacsymaを基にした数式処理ソフトのOSSです。最近はPythonで行列やベクトルの演算が可能ですが、微分や積分が入ってくると現状お手上げ状態です。その点MaximaはPythonの様に汎用的に何でも出来る訳ではありませんが、数値計算、微分積分、ベクトル解析と言った解析的な用途には威力を発揮します。大学時代愛用していた時期が有りますが、いつの間にか使わなくなっていました。

インストール方法

最終的にMaximaのGUI環境wxMaximaを動作できる様にすることが目標です。インストールするアプリケーションは以下の通りです。

  • Maxima本体
  • wxMaxima(GUI環境)
  • gnuplot(グラフ描画用)

Maxima本体 + gnuplotのインストール

まずはMaxima本体とgnuplotをインストールします。この2つはCLIで利用することを前提に設計されています。インストールにはHomebrewを用います。

brew install maxima gnuplot

インストール後の確認
$ maxima
Maxima 5.43.2 http://maxima.sourceforge.net
using Lisp SBCL 2.0.0
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1)

$ gnuplot

    G N U P L O T
    Version 5.2 patchlevel 8    last modified 2019-12-01 

    Copyright (C) 1986-1993, 1998, 2004, 2007-2019
    Thomas Williams, Colin Kelley and many others

    gnuplot home:     http://www.gnuplot.info
    faq, bugs, etc:   type "help FAQ"
    immediate help:   type "help"  (plot window: hit 'h')

Terminal type is now 'qt'

gnuplot>

wxMaximaのインストール

MaximaのGUI版もインストールします。CLI版とまた違った使いやすさが魅力です。

インストーラーをダウンロード

ダウンロードページよりダウンロードします。パッケージマネージャーとしてHomebrewを利用している場合はMaxima 5.38.0のイメージでインストールすることをお薦めします。5.40.0以降はパッケージマネージャーMacPortsの利用を前提としています。Homebrewとの共存は推奨されていません。

インストーラーの起動

ダウンロードしたMaxima 5.38.0.dmgをダブルクリックし、マウントします。マウントが完了すると以下の様なウィンドウが起動します。

maxima

ウィンドウ内のwxMaxima.appMaxima.appを選択し、Applicationsディレクトリのショートカットにドラッグ&ドロップします。Applicationsディレクトリにコピーが格納されます。格納が完了したら、wxMaximaを起動します。セキュリティー警告が表示されますが、起動を許可します。因みにApplicationsに格納したMaximawxMaximaを動作させる為に必要です。

wxMaximaにMaxima本体を関連付ける

wxMaximaが起動したら、wxMaxima/設定を順にクリックし、設定ウィンドウを起動します。Maximaをクリックし、下図の様にMaxima本体のパスを設定します。

wxMaxima

CLI上でgnuplotを利用する時の設定の管理

設定ファイルを作り、そこに設定を記載します。ユーザー毎に設定する方式にしたいので、Homeディレクトリに/usr/local/Cellar/gnuplot/5.2.8/share/gnuplot/5.2/gnuplotrcをコピーします。この時、.gnuplotというファイル名でコピーします。

show loadpathコマンドで起動時に読み込む設定を記述するgnuplotrcの格納先が分かるので確認する。


$ gnuplot

    G N U P L O T
    Version 5.2 patchlevel 8    last modified 2019-12-01 

    Copyright (C) 1986-1993, 1998, 2004, 2007-2019
    Thomas Williams, Colin Kelley and many others

    gnuplot home:     http://www.gnuplot.info
    faq, bugs, etc:   type "help FAQ"
    immediate help:   type "help"  (plot window: hit 'h')

Terminal type is now 'qt'
gnuplot> show loadpath

    loadpath is empty
    gnuplotrc is read from /usr/local/Cellar/gnuplot/5.2.8/share/gnuplot/5.2

$ cp /usr/local/Cellar/gnuplot/5.2.8/share/gnuplot/5.2/gnuplotrc ~/.gnuplot

.gnuplotの中身

###
### Gnuplot version 5.0 intialization file
### This file is loaded by gnuplot at the start of each run.
### It is provided as a template, with all commands commented out.
### Uncomment and customize lines for local use.
### Any commands placed here will affect all users.
### To customize gnuplot's initial state for an individual user,
### place commands in a private file ~/.gnuplot instead.

###
### Language initialization
###
# set locale
# set encoding locale

###
### Default line colors and repeat cycle
###
# set linetype 1 lc rgb "dark-violet" lw 1
# set linetype 2 lc rgb "#009e73" lw 1
# set linetype 3 lc rgb "#56b4e9" lw 1
# set linetype 4 lc rgb "#e69f00" lw 1
# set linetype 5 lc rgb "#f0e442" lw 1
# set linetype 6 lc rgb "#0072b2" lw 1
# set linetype 7 lc rgb "#e51e10" lw 1
# set linetype 8 lc rgb "black"   lw 1
# set linetype cycle 8

###
### Initialize the default loadpath for shared gnuplot scripts and data.
### Please confirm that this path is correct before uncommented the line below.
###
# set loadpath "/usr/local/share/gnuplot/4.7/demo"

###
### Some commonly used functions that are not built in
###
# sinc(x) = sin(x)/x
# rgb(r,g,b) = sprintf("#%06x",256.*256.*255.*r+256.*255.*g+255.*b)
# hsv(h,s,v) = sprintf("#%06x",hsv2rgb(h,s,v))

###
### Other preferences
###
# set clip two

試しに線形代数の計算をやってみる

逆行列と固有値・固有ベクトルの計算をさせてみました。矢張り専用ソフトということも有り、計算結果が見やすいです。

線形代数

まとめ

Mac上でOSS数式処理ソフトのMaximaのセットアップを行いました。機械学習工学の更なる深い理解に向けて線形代数学を再勉強する為に利用していこうと思います。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3