Maxima 高校生のための入門書

下の青い字のように入力すれば(%i 数字 の次に入力して Enterを押す),たちどころに計算してくれる。

注意は一つ,最後にセミコロンを入れる(C言語みたいです)。wxMaxima のほうは自動で入れてくれます。

xmaxima は HTML上 にそれを実現するもののようだ。下の青い字をダブルクリックして命令を覚えよう。

数Tから教科書に出てくる順で以下命令を書こう。

数T  数A  数U  数B  数V  数C

プログラミング


数T

まず,数の計算  演算記号は a+b,a-b,a*b,a/b,a^b(a の b 乗)
a/b/c と a^b^c は注意が必要。(a/b)/c と a/(b/c) (a^b)^c と a^(b^c) は意味が違う。
Maxima はどっちと解釈するか実験してみよう。

大きな数でも 100!;  分数計算も 1/2+1/3;  根号計算も sqrt(8)+sqrt(2);

変だと思ったら radcan(%);

radcan というのは Help を見ると radical(根号)canonical(標準)の略。指数・対数・根号を標準的な形に変形するということらしい。このさい数学の英語も覚えよう。 %ってのは前の結果のこと。
ちなみに % は Maxima では大切な記号で %i1 というのは inputu(入力)1番目,%o1 というのは output(出力)1番目という意味。

数学定数 ここで, 計算結果を小数表示したければ float(%);浮動小数点表示ってやつ。
ついでに,%pi は円周率です。そのうち出てくる %e はネイピア数,%i は虚数単位
float(%pi); もっと長く見たいって? fpprec:100;(floating point precedence かなあ?)としてから bfloat(%pi);(big float)
あっているとわかる人はすごい。
似ているけれども float(%phi); なんだかわかる? float(%phi^2-%phi-1); で,わかった?


整数論は,もっと教科書にもしっかり書いてほしいですね。
除法の商は quotient(100,7);  余りは mod(100,7); 両方一緒に見たければ divide(100,7);
答の[,]はリスト表示というもので行列などに使う形式。
素数かどうかを調べるのは primep(2007);
prime(素数)probability(可能性)false は偽,true が真。
Manualには100兆のオーダーを超えなければ true なら prime で,それを超えると probability の問題になる,とある。
(素)因数分解は factor(2007);
整数の素因数分解(integer factors)は ifactors(2007);
で,答の形式が少し違う。


次には,文字の計算

展開 expand((x+1)^100);  因数分解 factor(a^3+b^3+c^3-3*a*b*c); 除法は divide(x^3-1,x-1);

分数式の部分分数は partfrac(1/(x^3-1),x);


気をつけることは掛けるのアスタリスク,2x じゃなくて 2*x


方程式を解く solve(a*x^2+b*x+c=0,x);  solve([2*x-2*y+z=9,2*x-3*y+3*z=16,3*x+2*y-2*z=-2],[x,y,z]);

一次(線形)を意識すれば linearsolve という線形解 linsolve([2*x-2*y+z=9,2*x-3*y+3*z=16,3*x+2*y-2*z=-2],[x,y,z]); 答の形式が違うのに注意。

関数だってグラフが書けるぞ。
ちなみに,Options の Plot Windows には Embedded(埋め込み),Separate(分ける),Multiple(複数)があって,いくつかを比べるなら Multiple。
自分で関数を定義するのは f(x):= x の式

plot2d(x^2-4*x+5,[x,-1,5]); plot2d(x^2,[x,-2,2]); plot2d(2*x^2,[x,-2,2]); 
みんな同じグラフでおかしいって?よく見てみよう。西洋人と東洋人の発想の違いかなあ。
人目を引くには三次元だな。plot3d(sin(x)*cos(y),[x,-3,3],[y,-3,3]);
グラフの上でマウスをドラッグしてみよう。


三角関数  sin(%pi/3); cos(%pi/4);  tan(%pi/6);  三角方程式 solve(sin(x)=1/2,x);
何か注意が出ているでしょう,「逆三角関数を使って解いているから,失われている解もあるかもしれない」なんて,丁寧に注意してくれているわけだ。失われている解は何だか分かるよね?

式変形は少し注意が要る。sin(x)^2+cos(x)^2; だけじゃなくて,trigsimp(%);

trigonometric simplify(三角法による簡略化)ってことだろう。ここらへんの式変形ってのは目的によって変わるから,わずらわしいのは当然だろう。
trigreduce, ratsimp, radcan も違った変形があるかもしれない(may be)と Help にある。
trigonometric reduce (三角法による解法・換算・通分・約分)
rational simplify(有理数の簡略化)
実験してみよう。

数A

集合(set)についても色々ありますが,便利そうなものだけ
集合を表すのは a:{1,2,3,4,5};
約数の集合  b:divisors(100);
その集合の要素(element)かどうか elementp(3,a);elementp(3,b);
和集合     union(a,b);
共通集合   intersection(a,b);
分割数   integer_partitions(6);

次に場合の数で,まず,順列・組み合わせの関数が使えるようにする。 load(functs);
順列   permutation(10,6);
組合せ combination(10,6);
文字でもいけるのがすごいとこ。
combination(n,2);
二つ目の変数が文字だとうまくいきませんけど
combination(10,n);
binomial(10,n) ってなんだ?となるでしょう。二項係数つまり,組合せと同じ binomial(n,2);
こちらは,load(functs) はいらないです。
多項係数は multinomial_coeff(a,b,c);

実際に順列を見たいこともしばしばあります。
c:[g,a,u,s,s]; としておいて permutations(a); 

命題だって扱えます。
is(1=2);
is(not(1=2));
a:(1=2);b:(1=1);としてから,is(a and b);is(a or b);とやるとどうなるでしょう?



数U

恒等式これが Mathematica の方がすごいかな。
Mathematica では SolveAlways[等式,変数]っでのがあって係数をすぐに答えてくれる。
Maxima ではちょっと工夫して,例えば
f(x):=x^2-(a*(x-1)^2+b*(x-1)+c); とかしてから solve([f(0)=0,f(1)=0,f(2)=0,f(3)=0],[a,b,c]); とでもやるのかな。

図形と方程式
load( implicit_plot ); 陰関数表示です。
implicit_plot (x^2+y^2=1, [x,-2,2],[y,-2,2],[gnuplot_preamble,"set size ratio 1", "set zeroaxis"]);
最後の Gnuplot のオプションは縦横比率1の座標軸付という日本人好みのスタイル。

線形計画法(simplex 単体法)が便利そうです。
load("simplex"); としてから,
minimize_sx(2*x+y, [x+2*y<4, x-y<1,x>0,y>0]);
maximize_sx(2*x+y, [x+2*y<4, x-y<1,x>0,y>0]);

本当なら等号が必要ですが,入れなくても出ます。ちなみに,入れても出ましたが。

極限算 limit(1/x*(2-4/(x+2)), x, 0); 詳しくは数Vですよね。
微分  diff(a*x^3+b*x^2+c*x+d, x);
積分  integrate(a*x^2+b*x+c,x);
     定積分 integrate(a*x^2+b*x+c, x, 0, 1);
     factor(integrate((x-a)*(x-b), x, a, b));はやっぱり便利な公式です。
グラフはもちろん plot2d([x^3-3*x], [x,-2,2]);

数B

load(functs); をしてから
等差数列 arithmetic(a,d,n); 等比数列 geometric (a,r,n);

そして, sum(k, k, 1, n); 実際の計算をさせるには, sum(k, k, 1, n),simpsum;
等比数列の和も sum(r^k, k, 1, n), simpsum; 数列も数Vですよね。

sum(1/k/(k+1), k, 1, n), simpsum; なんかは load(simplify_sum);を読み込んでから simplify_sum(%);


漸化式も詳しくは数Vで。Maxima では Dynamics(動力学) がある。
load(dynamics);として
例えば展開 evolution(-1/2*x-1,5,10,[gnuplot_preamble, "set zeroaxis"]); とやると目で見る漸化式。5から始まって10回と言う意味。
こいつを2次元で見る命令まで用意されていて,階段 staircase(-1/2*x-1,5,10,[gnuplot_preamble, "set zeroaxis"]);
見事収束する様子が見えます。他のソフトでプログラミングしなくてよかった。

解くだけなら load(solve_rec); として,線形漸化式(recurrence equation)の解答を出してくれます。
例えば solve_rec(a[n+1]-2*a[n]-1,a[n],a[1]=2);
フィボナッチ数列も  solve_rec(a[n+2]=a[n+1]+a[n],a[n],a[1]=1,a[2]=1);


ベクトルはリスト表示で計算できる。
和・差・実数倍 k*[a,b]+l*[c,d];

積は注意。[a,b]*[c,d];は違う計算。
内積(inner product)は load ("eigen");(固有値 eige nvalue かな)としてから 
inprod([a,b],[c,d]);




プログラミング

プログラムと言っても,まずやりたいことは,「繰り返し」と「判断」でしょう。

電卓としてコンピュータを使ってもいいが,「繰り返し」こそコンピュータが人間以上に得意なことでしょう。

for a: 初期値 thru 最終値 step 階級値 do 命令

for a:1 thru 10 step 2 do display(a);  thru は through だろう,でも調べてみると省略してこうも書くのだな。

そして,「判断」が if 条件 then 命令 です。

for a:1 thru 10 do for b:a thru 10 do for c:1 thru 10 do if a^2+b^2=c^2 then display(a,b,c);

セミコロン(;)が入力されるまでは複数行にわたってもいいようです。
でも,入力がめんどくさいですよね。
Edit Previous input (Alt+P)で前の命令がすぐ出ます。
まあ,これで覚えて wxMaxima ではプログラム入力機能(Multiline input)を使いましょう。

複数行入力で注意すべきことは,
for a:1 thru 10 do
for b:a thru 10 do
for c:1 thru 10 do
if a^2+b^2=c^2 then display(a,b,c);
じゃあ,駄目なんですよね。do の後か for の前に Space がないとエラーになります。
考えれば当たり前だけど,こんなことも分かるまで結構時間食ってしまいますよね。

作ったプログラムはバッチファイルとして,テキスト保存し(拡張子.mac)File−Batch file で読み込むことができます。
あるいは File−save しておくと拡張子.wxm というテキストファイルで保存されます。





Maxima is a computer program for doing mathematics calculations, symbolic manipulations, numerical computations and graphics. Procedures can be programmed and then run by Maxima to do complex tasks. Much of the syntax for other languages such as Maple was copied from Maxima.

Project and documentation links

The Help menu in Xmaxima gives you access to the following documents:

Getting started

To do basic operations, a line is typed, followed by a semicolon, and then entered. This can be done in the window above. Alternately you may edit the blue portions in this buffer, and click on them, to see the result evaluated above and/or inserted in this window, depending on what was specified in the html source for this file. For example clicking below

You may double click the above formula, and the integral will be substituted into the Maxima evaluation in the other window. There are examples which you may also look at 3d plotting If you wish to have your plots appear in a separate window, go to the preferences button under file, and select separate. You may also go to the netmath page to see some more capabilities.

Here are some examples from basic calculus. To have Maxima evaluate the derivative of the function below, click on this line.

Maxima can calculate indefinite integrals.

Maxima can perform calculations to arbitrary precision. The following example computes Pi to one hundred decimal places.

Linear Algebra

For example, matrices can be entered and manipulated. Click these two lines.

Then the procedure can be called. Fib[8]; gives 21

Maxima can solve ordinary differential equations analytically and numerically. Click the following line for an example of an analytic solution.

Defining a Function

The standard form is

Local variables:

The block construct lets us introduce local variables, and also lets us have a sequence of statements:

block([v1:val1,v2:val2,v3,v4:val4],stmt1,stmt2,... stmtn)
the value is the value of the last statement. During the execution the variables v1,v2,... will have the values indicated. If no value is given for v3 then it will just evaluate to itself:

Thus if we set v3 globally to be 7,

An example in a function: Using for:

a for loop always returns 'done as its value. To get the value you want add the w.

The function describe(s) returns documentation on all functions whose names include the string s. For example, if the string is "log",
describe("log");all prints this output inserted by FC2 system