0
$\begingroup$

I have a function that extracts a numerical value from two vectors, written as Spa[i_,j_]:=Flatten[LaU[i].LaD[j]][[1]]. (Note, I'm sure that this isn't the most efficient way to solve this problem but it works for my uses). In the next stage of my process, I'm having to calculate Spa functions when the argument needs something other than LaU, like something called QLaU[1], which is defined in a similar process (exact same dimensions etc).

Instead of manually creating a separate function like e.g SpaQ[i_,j_] :=Flatten[QLaU[1].LaD[2]][[1]], I'm hoping to use a Module function to simply combine the two together, and have MMA recognise the type of function. Is this possible within the scope of Module? I'm essentially trying to put a couple of functions within the single overall Spa function, to keep the final results looking similar.

$\endgroup$
3
  • 1
    $\begingroup$ Spa[lau_,i_,j_]:=Flatten[lau[i].LaD[j]][[1]]? $\endgroup$
    – Alan
    Jan 31, 2019 at 18:24
  • $\begingroup$ May I ask how that works? Spa with three arguments means I need to specify three bits of information, and I'm not sure how that would work for both LaU and QLaU? $\endgroup$
    – Brad
    Jan 31, 2019 at 18:26
  • 1
    $\begingroup$ Pass the symbol name LaU or QLaU as an argument to Spa. That is what Alan is implying in his comment. $\endgroup$ Jan 31, 2019 at 19:05

1 Answer 1

3
$\begingroup$

If you want to constrain Spa to only accept LaU and LaD, whichis good practice, you can define it like this:

Spa[la:(LaU | LaD), i_, j_] := Flatten[la[i].LaU[j]][[1]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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