0
$\begingroup$

Suppose I have a matrix

mat[a_,b_,c_,w0_,y0_][omega_,ak_]:={
{I TMM[a,b,c][omega,ak]w0, -I TMP[a,b,c][omega,ak]},
{TMP[a,b,c]y0, I TMM[a,b,c]w0 y0}}

Now the problem is that I have two sets of definitions for the functions TMM and TMP, which I will use the letter g and m to distinguish:

gTMM[a_,b_,c_][omega_,ak_]:=a*b*c
gTMP[a_,b_,c_][omega_,ak_]:=a*b*c*omega*ak
mTMM[a_,b_,c_][omega_,ak_]:=a*b*c^2
mTMP[a_,b_,c_][omega_,ak_]:=a*b*c^2*omega*ak

The above are just examples. In the actual codes they are a lot more complicated. So far my approach is to define two matrices gmat and mmat to take into account the two sets of TMM and TMP. Both matrices have exactly the same structure, the only difference is that one uses mTMM/mTMP and the other one uses gTMM/gTMP. This works, but as I extend my codes, it is becoming more and more troublesome (especially when the size of the matrix is something like 10x10 but not 2x2). So I try to use a single mat to represent the two cases, and define another variable type and do some something like:

(* definitions of gTMM, gTMP, mTMM, mTMP which are not shown here *)

mat[type_:"M", a_,b_,c_,w0_,y0_][omega_,ak_]:=Module[{TMM,TMP},
If[type=="G", TMM=gTMM;TMP=gTMP, TMM=mTMM;TMP=mTMP];{
{I TMM[a,b,c][omega,ak]w0, -I TMP[a,b,c][omega,ak]},
{TMP[a,b,c]y0, I TMM[a,b,c]w0 y0}}

But it is not working. How to make it work so that I can switch between the two definitions of TMM and TMP inside one single mat?

$\endgroup$
3
$\begingroup$

You can use "type" everywhere:

mat[type_, a_, b_, c_, w0_, y0_][omega_, ak_] := 
{{I TMM[type, a, b, c][omega, ak] w0, -I TMP[type, a, b, c][omega, ak]}, 
{TMP[type, a, b, c][w0,y0], I TMM[type, a, b, c] [w0,y0]}}
TMM[1, a_, b_, c_][omega_, ak_] := a b c
TMP[1, a_, b_, c_][omega_, ak_] := a b c omega ak
TMM[2, a_, b_, c_][omega_, ak_] := a b c^2
TMP[2, a_, b_, c_][omega_, ak_] := a b c^2 omega ak
$\endgroup$
2
$\begingroup$

In the very last line of code you have TMP[a,b,c] while it should be TMP[a, b, c][omega, ak]; the same for TMM[a, b, c]. Then

mat["G", 1, 1, 2, 1, 1][1, 1]

{{2 I, -2 I}, {2, 2 I}}

mat["M", 1, 1, 2, 1, 1][1, 1]

{{4 I, -4 I}, {4, 4 I}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.