Mathematica in 3×60 minutes - Part I

Gergely Gyebrószki | Dept. of Applied Mechanics, Budapest University of Technology and Economics

Mathematica Front-End

Text-editor
File extension: nb (notebook)
Notebook -> cells
Comment

Format -> Style
Alt + 1: Title
Alt + 4: Section
Alt + 7: Text
Alt + 9: Code (default)

Evaluation: ⇧+⌤ (or numpad ⌤)

2+3

7+a

2^4

3*2

␣ can be used instead of *

3 2

a 3

Input cell -> Output cell
Cells have hierarchy!

The front-end and kernel are different processes.
The kernel can be stopped/started: Evaluation -> Start/Stop kernel

Variables: Symbols

In Wolfram language type definition is not necessary
C++: double i = 5.5;
Wolfram language: i = 5.5

i=7

i=5.5

i

Note: defined symbols are not blue anymore!

Typesetting

Esc key has a special function
α = EscaEsc
-> = Esc->Esc
Superscript: Ctrl+6
Subscript: Ctrl+_
Square root: Ctrl+2
Fraction: Ctrl+⇧+6

(a^3)

Use Palettes to find out hotkeys.

(Subscript[x, j])

2 (10^(-3))

(Sqrt[3])

Everything in Mathematica is either a Symbol or a Function (or both)

3/17

Functions

Syntax: FunctionName[arg1, arg2, ...]
Function names are usually CamelCase

π

β

γ

F1: Help

N[π,200]

D[(x^2)+7x,x]

Most operators have an underlying built-in function, FullForm shows it!

FullForm[(x^2)+7x]

Assignment

Normal -> =
Delayed -> :=

a=2

b=a

c:=a

c

Note: no output!

Suppress output with ;

a=3;

b
c

Note: Symbol c evaluates its definition every time it is used.
Symbol b stored the value at the time of assignment.

Undefine symbol

Clear[a]

c

Watch out for protected symbols (defined by Mathematica)!

N=2

N[E]

Replace and Rule: substitute values for undefined symbols

A=(((d^2)π)/4)

Clear[d]

Rule[d,4]

ReplaceAll[A,Rule[d,4]]

A

A/.d->4

Short version of Rule: expression -> value
Short version of Replace: expression /. rules

A/.d->4

Note: does not change A

Lists: {...}

L={10,20,30,40}

Part: list[[...]] or Part[list, i]

i-th element (indexing starts from 1)

L[[2]]

L[[-1]]

Range of elements

L[[1;;3]]

Specific elements (1st and 4th)

L[[{1,4}]]

Lists are used in many situations, example: replace multiple symbols

A=e f

A/.{e->6,f->e}

Not elegant:

A/.{e->6,f->e}/.{e->6,f->e}

A//.{e->6,f->e}

User-defined functions

Arguments must be noted with _ (underscore)

a=2;

f1[x_]=2x+a

f2[x_]:=2x+a

f1[x]
f2[x]

a=7;

f1[2]
f2[2]

My personal opinion: define functions with more symbols delayed.

f3[x_,a_]:=2x+a

f3[y,d]

Symbols can have multiple meanings, ? lists quick info

f2[x_Integer]:=x/2

?f2

f2[4]

f2[4.]

Prefix (@) and Postfix (//) syntax

f[t]

Prefix: @

f@t

Postfix: //

t//f

Note: extremely useful in case of long expressions

{1,2,3,4,5,6,7,8,9,10}//Total

Apply a function (@@)

Example apply a function to a list (put list elements into the function as arguments)
{m, n} -> f[m, n] (not f[{m, n}])

f[{e,f}]

f[e,f]

Apply[Times,{1,2,3,4,5}]

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

Apply in fact replaces the Head of the expression

FullForm[{1,2,3,4,5}]

Head[{1,2,3,4,5}]

List[1,2,3,4,5] -> Times[1,2,3,4,5]

FullForm[(x^2)+3x]

Head[(x^2)+3x]

Apply[Times,(x^2)+3x]

Short: @@

Times@@{1,2,3,4,5}

One level below: @@@

Times@@@{{1,2},{1,2,3},{1,2,3,4}}

Map a function (/@)

Apply a function to every element of a list

Map[f,{1,2,3,4}]

f/@{1,2,3,4}

Pure function (#1, #2, &)

f4[x_]:=2x

2#&

f4[7.7]

2#&[7.7]

( ), structure, operator precedence

(#+1&)/@{1,2,3,4}