Traversing expressions

The examples on this page use the Fermion type defined on the Home page.

ncmapreduce(f, (add, mul), x; scalarmap) maps f over every factor in x, combines the factors in each term with mul, and then combines the terms with add. Scalars are passed through scalarmap. For example, the highest number of factors in any term of a fermion expression is

julia> import NonCommutativeProducts: ncmapreducejulia> op = 1 + 2Fermion(1)'*Fermion(2) + Fermion(1)'*Fermion(2)'*Fermion(3)1I + 2*c†[1]*c[2] + c†[1]*c†[2]*c[3]julia> ncmapreduce(x -> 1, (max, +), op; scalarmap=zero)3

With the ordinary (+, *), one can substitute a matrix for each factor:

julia> using LinearAlgebrajulia> c = [0 1; 0 0];julia> ncmapreduce(x -> x.dagger ? c' : c, (+, *), Fermion(1)'*Fermion(1) + 2; scalarmap=x -> x*I)2×2 Matrix{Int64}: 2  0 0  3