I'm trying to create a matrix in which the elements are functions of two variables, but I can't figure out how to do it. Is it possible? The only way I could figure out is to define the matrix in terms of two variables, then replace those variables with values later on. Here's my code:
row1 = {};
row2 = {};
For[ii = 1, ii <= Length[modes], ii++,
{l, m} = modes[[ii]];
G1lm = sglm[-2, l, m, \[Theta]] + (-1)^l sglm[-2, l, -m, \[Theta]];
G2lm = sglm[-2, l, m, \[Theta]] - (-1)^l sglm[-2, l, -m, \[Theta]];
AppendTo[row1, G1lm Cos[m \[Phi]]];
AppendTo[row1, G1lm Sin[m \[Phi]]];
AppendTo[row2, G2lm Sin[m \[Phi]]];
AppendTo[row2, -G2lm Cos[m \[Phi]]];
];
proj = {row1, row2};
Theta and Phi are my variables. Later on, when I want proj for a specific Theta and Phi I use replace:
pN = proj /. {\[Theta] -> a, \[Phi] -> b};
where a and b are some numbers. The problem with this solution is that if I use Theta and Phi for something else later on, say I set Theta = 0.5, then proj will permanently have Theta = 0.5, regardless of the replace I use. This happened to me: I was calling proj in a loop, but I kept getting the same results. After ripping my hair out for a few hours, I realized I had Theta set to a specific value in a block of code further up in my notebook. The simple solution is to simply not use Theta and Phi anywhere else in the notebook, but that doesn't seem very stable.
The root problem here is I'm using global variables to do something that should really be local to proj, but I don't know how to fix that. The ideal would be if I could somehow make proj a function of Theta and Phi so that later on I can simply do pN = proj[a,b]
. I know how to make the Gnlm's functions of theta, but then I don't know how to make row1, row2, and by extension proj, functions of Theta (and Phi). Any help would be greatly appreciated.
modes
andsglm
? Otherwise I can only post a general answer on how to create a list of functions. Or rather, as it seems that is what you need, a function in two variables that creates a list. $\endgroup$ – einbandi Mar 6 '13 at 17:11modes
is a 2x5 array of numbers --- specifically{{2,2},{2,1},{3,3},{4,4},{5,5}}
--- and thesglm
are the theta-dependent parts of the spin-weighted spherical harmonics. Honestly, the exact functional form of the sglm doesn't matter --- you could just use Sin[l theta] and Cos[l theta] if you wish. The point is each element of the matrix is a different function of the same two variables, theta and phi. $\endgroup$ – ccap83 Mar 6 '13 at 17:20