Mathematicaの表式表現と特徴

Mathematicaの表式表現

t=a+b+c+d

FullForm[t]

s = a b c d

FullForm[s]

Attributes[Plus]

Attributes[Times]

Plus[{a,b},c]

Expand[(x+y)^3]

FullForm[(x+y)^3]

FullForm[Expand[(x+y)^3]]

FullForm[{a,b,c}]

f[a,b,c,d]

Append[%,e]

関数引数

不特定の数の引数表現が可能: 2または3つのblanks __ または___ (_+_ or _+_+_) を使う
x_ : an expression, whose reference name is x,
x__: more than one expressions whose reference name is x,
x___: zero or more expressions whose reference name is x.

ただし、可読性を失わないようにも心がける

f[a,b,c] /. f[x__] -> g[x,y]

f[x_] := x;
f[x_, y__] := f[x] + f[y];

f[1,2,3,4,5]

g[{a_}] := a^2;
g[{a_, b__}] := g[{a}] + g[{b}];

g[{a,b,c,d}]

Mathematica allows omittable arguments using x_:value.
    a[x_, y_:u, z_:v] := func[x,y,z]
shows that x is always necessary, y and z is omittable, which default values are u and v, respectively.

h[x_,y_:1] := x^y

h[3,4]

h[3]