Mathematica für "Fortgeschrittene"

Zuweisungen und verzögerte Zuweisungen

Sofortige Zuweisung (=)

a = 5

bsofort = 1 + Sqrt[a]

a = 7

bsofort

?bsofort

Verzögerte Zuweisung (:=)

bverz := 1 + Sqrt[a]

bverz

a = 5

bverz

?? bverz

Alles ist Liste

Interne Darstellung von Objekten, FullForm, TreeForm

Clear[a]

t1 = a^2 + b^2 + c^2 + 2  d Sqrt[e]

FullForm[t1]

TreeForm[t1]

t2 = {a, b, c, d, e}

FullForm[t2]

Head[t1]

Head[t2]

t1[[0]]

t1[[1]]

t1[[2]]

Ersetzen des Heads - Apply (@@)

t1

FullForm[t1]

Apply[List, t1]

List @@ t1

Times @@ t1

FullForm[%]

?? Times

Times[]

Times[c, b, a]

FullForm[%]

Ausgabeformat

t3 = Series[Cos[x], {x, 0, 5}]

FullForm[t3]

5!

n!

FullForm[%]

Binomial[n, k]

Format[MyBinomial[n_, k_]] := MatrixForm[{{n}, {k}}]

MyBinomial[m, l]

FullForm[%]

Map, Pure Functions, ...

li = {1, 2, 3, 4, 5, 6, a, b, c}

g[x_] = 1 + Sqrt[x]

Map[g, li]

Map[1 + Sqrt[#] &, li]

li1 = Table[a[i], {i, 4}]

li2 = Table[b[i], {i, 4}]

Clear[f, g]

Inner[f, li1, li2, g]

Inner[Times, li1, li2, Plus]

Inner[Plus, li1, li2, Times]

Inner[List, li1, li2, List]

TableForm[%]

f[x_] = x (1 - x)

NestList[g, 1/2, 5]

NestList[f, 1/2, 5]

NestList[f, 0.5, 100]

Pattern Matching

Patterns in Regeln

Log[27  n] /. Log[a_ b_] -> Log[a] + Log[b]

LogRules = {Log[a_ b_] -> Log[a] + Log[b]}

Log[27 n] /. LogRules

Log[27] /. LogRules

Log[27  Sqrt[n]] /. LogRules

AppendTo[LogRules, Log[a_^n_] -> n Log[a]]

Log[27  Sqrt[n]] /. LogRules

% /. LogRules

Log[27  Sqrt[n]] //. LogRules

Typüberprüfung in Patterns

li = {1, Pi, 5, 7, 3}

li1 = Map[gamma, li]

li1 /. gamma[n_] -> Factorial[n - 1]

li1 /. gamma[n_Integer] -> Factorial[n - 1]

LineareFunktion[u_Plus] := Map[LineareFunktion, u]

LineareFunktion[5  a + 7  b + 8  c]

Bedingte Patterns

MyBinomial[n_Integer  /; n > 0,  k_Integer  /; k > 0] := 
      n/k MyBinomial[n - 1, k - 1]

MyBinomial[5, 3]

MyBinomial[n_Integer /;  n ≥ 0,  0] = 1

MyBinomial[5, 3]

MyBinomial[3, 5]

MyBinomial[0, k_Integer /; k > 0] = 0

MyBinomial[5, 3]

MyBinomial[3, 5]

MyBinomial[n, k]

?? MyBinomial

Ein paar nützliche "Test"-Funktionen

liste = {-5, 1, 2, Pi, E, 2/3, 4.5, I}

TesteFunktion[u_] := 
      Map[{#, u[#], Head[#]} &, liste] // TableForm

TesteFunktion[NumberQ]

TesteFunktion[IntegerQ]

TesteFunktion[EvenQ]

TesteFunktion[OddQ]

TesteFunktion[PrimeQ]

TesteFunktion[Element[#, Reals] &]

TesteFunktion[Element[#, Complexes] &]

LineareFunktion[n_ x_  /; Element[n, Reals]] := 
      n LineareFunktion[x]

LineareFunktion[a + Pi b + 7  c + d e]

Variable Anzahl von Argumenten

Mittelwert[u__] := Apply[Plus, {u}]/Length[{u}]

Mittelwert[a, b, c]

Programmierung

Speichern von Zwischenergebnissen

Fibo[n_Integer /; n > 1] = 
      Fibo[n - 1] + Fibo[n - 2]
Fibo[0] = 0
Fibo[1] = 1

Map[Fibo, Range[10]]

Fibo[25]

Clear[Fibo]

Fibo[n_Integer /; n > 1] := Fibo[n] = 
        Fibo[n - 1] + Fibo[n - 2]
Fibo[0] = 0
Fibo[1] = 1

Fibo[25]

Lokale Variablen

t = 17

Module[{t}, 
t = 8; 
Print[
        t]]

t

Standardabweichung[x_List] := 
      Module[{n, μ, σ}, 
n = 
          Length[x]; 
μ = 
          Plus  @@  x/n; 
σ = 
          Sqrt[Plus @@ 
                Map[(# - μ)^2  &, x]/(n - 
                  1)]; 
{μ, σ}
]

Standardabweichung[{a, b}]

Table[Random[], {100}]

Standardabweichung[%]

Do, For, While, If,

Do

Do[Print["Hallo"], {5}]

Do[Print[i], {i, 5}]

Do[Print[i], {i, 1, 5, 1/2}]

For

For[i = 1, i ≤ 5, 
      i = i + 1/2, 
Print[i]]

If

Do[If[Random[] < 0.5, Print["Kleiner 0.5"],  
        Print["Nicht kleiner 0.5"]], {5}]

\(If[Random[] < 0.5, 
\((\ 
        Print["Kleiner"]; 
Print["als"]; 
Print["0.5"])\), 
\((Print["Größer"]; 
Print["als"]; 
Print["0.5"])\)]\)

While

xalt = 2; xneu = 1

While[
      Abs[xalt - xneu] > 
        10^(-20), 
(xalt = 
          xneu; 
xneu = 
          1/2 (xalt + 2/xalt))]

xneu

N[%]

xneu - Sqrt[2] // N

"Benutzerdefinierte Datentypen"

Format[Intervall[a_, b_]] := 
      StringJoin["[", ToString[a], ", ", 
        ToString[b], "]"]

Intervall[1, 5]

Intervall[1, 5] + Intervall[2, 6]

Intervall[a_, b_] + Intervall[c_, d_] := 
      Intervall[a + c, b + d]

?? Intervall

Intervall[a_, b_] + Intervall[c_, d_] ^:= 
      Intervall[a + c, b + d]

?? Intervall

Intervall[1, 5] + Intervall[2, 6]

Intervall[a_, b_] Intervall[c_, d_] ^:= 
      Intervall[a c, b d]  /; 
        c ≥ 0 && a ≥ 0

Intervall[1, 5]*Intervall[2, 6]

Intervall[a_, b_] Intervall[c_, d_] ^:= 
      Intervall[c b, a d ] /;  
        a ≥ 0 && d ≤ 0

Intervall[1, 5]*Intervall[-6, -2]

Intervall[-6, -2]*Intervall[1, 5]

?? Times

?? Orderless

?? Intervall

Intervall /: Intervall[a_, b_]*n_Integer := 
      Intervall[n a, n b] /; n > 0

?? Intervall

Intervall[1, 3]*7