# How can I define a function using ArrayFlatten without using SetDelayed?

I created a matrix F2 using SparseArray and ArrayFlatten as follows:

f0[x_, y_] = (3 - (Cos[x] + Cos[y])) PauliMatrix[0] + 5 PauliMatrix[3];
nf = 2;
F1[x_, y_] =
SparseArray[{Band[{1, 1}, {2 nf, 2 nf}] -> {f0[x, y]},
Band[{1, 3}, {2 nf, 2 nf}] -> {7 PauliMatrix[0]},
Band[{3, 1}, {2 nf, 2 nf}] -> {7 PauliMatrix[0]}}];
nt = 2;
IC = SparseArray[{{(2 nf - 1), 1} -> 1, {(2 nf), 2} ->
1, {(2 nf - 1), 3} -> 1, {(2 nf), 4} -> 1}, {2 nf, 4 nt}];
F2[x_, y_] = ArrayFlatten[{{F1[x, y], IC}}];


As an example, I put nf=nt = 2. but what I have is a very large matrix. The problem now is that F2[0, 0] will not substitute x = 0 and y = 0 in the matrix. It is working if I define F2 with SetDelayed, i.e., F2[x_, y_] := ..... But I do not want to use SetDelayed because I have heavy calculations to do with F2. So how can I solve this, please?

Use Normal to make the RHS of the definition of F2 a normal array. The formal arguments were being hidden in the SparseArray:

0[x_, y_] = (3 - (Cos[x] + Cos[y])) PauliMatrix[0] + 5 PauliMatrix[3];

nf = 2;

F1[x_, y_] =
SparseArray[{Band[{1, 1}, {2 nf, 2 nf}] -> {f0[x, y]},
Band[{1, 3}, {2 nf, 2 nf}] -> {7 PauliMatrix[0]},
Band[{3, 1}, {2 nf, 2 nf}] -> {7 PauliMatrix[0]}}];

nt = 2;

IC = SparseArray[{{(2 nf - 1), 1} -> 1, {(2 nf), 2} ->
1, {(2 nf - 1), 3} -> 1, {(2 nf), 4} -> 1}, {2 nf, 4 nt}];

F2[x_, y_] = Normal@ArrayFlatten[{{F1[x, y], IC}}];

F2[a, b]

(* {{8-Cos[a]-Cos[b],0,7,0,0,0,0,0,0,0,0,0},{0,-2-Cos[a]-Cos[b],0,7,0,\
0,0,0,0,0,0,0},{7,0,8-Cos[a]-Cos[b],0,1,0,1,0,0,0,0,0},{0,7,0,-2-Cos[\
a]-Cos[b],0,1,0,1,0,0,0,0}} *)

• Memoization should work here too, no? Seems this is a problem (long calculation that can’t be done over-and-over-again!) that is tailor made for the technique. Commented Sep 3, 2020 at 11:39