Mathematica
https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Mathematica_Logo.svg/983px-Mathematica_Logo.svg.png
実行
shift-Enter
ヘルプ
?<Func>で関数の情報を見れる
code:nb
In1:= ?Plot
Plot[f,{x,Subscriptx, min,Subscriptx, max}] generates a plot of f as a function of x from Subscriptx, min to Subscriptx, max.
Plot[{Subscriptf, 1,Subscriptf, 2,...},{x,Subscriptx, min,Subscriptx, max}] plots several functions Subscriptf, i. >>
?<Func>*で<Func>から始まる関数名の一覧を表示
??<Func>でパラメータも確認できる
ユーザ定義関数は大文字から初めなくても良い
{}はリスト、範囲を定義
[]は関数に使う
==は方程式を定義するときに使う
変わった記号
https://reference.wolfram.com/language/guide/Syntax.html
関数型プログラミング
docs
これを一つずつ読み込みたい
# ref
Map関数などで使う
code:nb
Map[StringCount[我輩は猫である, Alternatives# &, {小学校1学年,小学校2学年}]
#の部分にMapの第二引数のリストが順次適用される
ref p.112
https://reference.wolfram.com/language/ref/Select.html 出てきた
Map
docs
/@は糖衣構文。より関数型っぽくかける
Select
docs
TSのfilter
&は無名関数
https://reference.wolfram.com/language/ref/Function.html
@は
https://reference.wolfram.com/language/ref/Prefix.html
@@
https://reference.wolfram.com/language/ref/Apply.html
反復関数
https://reference.wolfram.com/language/guide/FunctionalIteration.html.ja
Nest関数
再帰関数の代わりに使う?
https://www.isc.meiji.ac.jp/~mizutani/mathematica/program_hints.html
パターンマッチを使った関数定義
https://www.ueda.info.waseda.ac.jp/~hirose/mathematica/examples.html
https://www.ma.noda.tus.ac.jp/u/tg/mathematica/math5.pdf
https://reference.wolfram.com/language/guide/PatternMatchingFunctions.html.ja?source=footer
https://reference.wolfram.com/language/guide/Patterns.html.ja?source=footer
https://reference.wolfram.com/language/tutorial/PatternsAndTransformationRules.html.ja?source=footer
constraint
docs
Guardのこと
code:nb
facn_ /; n > 0 := n!
これをやってみると
code:nb
WPowera_, 0 := 1
WPower[a_, n_ /; EvenQn] := WPower[a*a, (Quotientn, 2)]
WPower[a_, n_ /; OddQn] := a*WPowera, (n - 1)
リストのheadとtail
docs
Haskellの(Head, Tail)は、(First, Rest)が対応
headはh_, tailはt__で表す
アンダーバーの話 ref
code:nb
(* 2進数表示のリストを10進数の値に変換 *)
B2d{0} := 0
B2d{1} := 1
B2d{h_, t__} := 2^(Length{t})*h + B2d{t}
(* ちゃんと書くなら *)
B2dm_:=2^(Lengthm-1)*Firstm+B2d[Restm]
clearする
code:nb
ClearAll"Global`*"
有限体パッケージ
#プログラミング言語