Mathematica For Physicists - What is a Function?

1. Delayed Assignment

A. Mathematical Functions e.g. Sin[x]

Sin[π]

B. Procedural Functions e.g. Simplify[expr]

Simplify[(x^2)+2x+1]

f[x_]:=(x^2)

f[3]

Rigid rotor.

Z[T_]:=Sum[(2l+1)Exp[-l(l+1)/T],{l,0,100}];

Cv[T_]:=D[(x^2)D[Log[Z[x]],x],x]/.x->T;

Cv[1]//N

Count the number of operators of specified charge R. Write down a generating function g[t], then the coefficient of t^R in the expansion should be the number of operators of charge R.

NumberOperators[genfunc_,R_]:=Coefficient[Series[genfunc[t],{t,0,R}],t,R];

g[t_]:=1/(((1-t))^3);

NumberOperators[g,10]

NumberOperatorsMod[genfunc_,R_]:=Module[{series},series=Series[genfunc[t],{t,0,R}];Coefficient[series,t,R]];

NumberOperatorsMod[g,10]

series

Simplify[Abs[x],x<0]

comfunc[expr_]:=100Count[expr,_Abs,{0,Infinity}]+LeafCount[expr];

Simplify[Abs[x],x<0,ComplexityFunction->comfunc]

2. Upvalues and Downvalues

?comfunc

Function h[x] s.t. Exp[h[x]]=x^2 + 3.

Exp[h[x_]]:=(x^2)+3

Unprotect[Exp]

Exp[h[x_]]^:=(x^2)+3

Exp[h[2]]

?Exp

?h

Cyclic Group, Cyclic[n,m] where n is the order of the group, m is an integer element in the group

Cyclic[n_,m_]+Cyclic[n_,k_]^:=Cyclic[n,Mod[k+m,n]]

Cyclic[5,4]+Cyclic[5,2]

3. Pure Functions

Function[x,(x^2)][3]

hello//Function[x,(x^2)]

hello//((#)^2)&

((Sin[π ϵ])/(π ϵ))//Series[#,{ϵ,0,5}]&

4. Advanced Techniques

A. Multiple definitions

StepFunction[x_]:=0

StepFunction[x_]:=1/;x>0

Plot[StepFunction[x],{x,-1,1}]

B. Remember values that functions have found

fib[n_]:=fib[n-1]+fib[n-2]

fib[0]:=1

fib[1]:=1

fib[28]

fib[n_]:=fib[n]=fib[n-1]+fib[n-2]

fib[28]

fib[50]