1

I'm sure this has been answered before, but imagine I have the following function

Om = Function[{eps, q, p},
   npJ = {{1, 0}, -{1, 0}, -{0, 1}, {0, 1}, -{1, 1}, {1, 1}};
   npP = {{0, 2}, -{0, 2}, {2, 0}, -{2, 0}, {1, -1}, {-1, 1}, {1, 
      2}, -{1, 2}, {2, 1}, -{2, 1}, {2, 2}, -{2, 2}};
   np = Join[npJ, npP];
   sJ = FullSimplify@
     Sum[If[MemberQ[npJ, i], 1/6, 
        0] E^(2 Pi I (q i[[1]] + p i[[2]])), {i, np}];
   sP = FullSimplify@
     Sum[If[MemberQ[npP, i], 1/12, 
        0] E^(2 Pi I (q i[[1]] + p i[[2]])), {i, np}];
   (1 - eps) sJ + eps sP
   ];

and I want to define another function that simply evaluates the expression obtained from the previous one, that is,

Om[eps, q, p]
Out[]=
        1/3 (1 - eps) (Cos[2 p \[Pi]] + Cos[2 \[Pi] q] + 
    Cos[2 \[Pi] (p + q)]) + 
 1/6 eps (Cos[4 p \[Pi]] + Cos[2 \[Pi] (p - q)] + Cos[4 \[Pi] q] + 
    Cos[4 \[Pi] (p + q)] + Cos[2 \[Pi] (2 p + q)] + 
    Cos[2 \[Pi] (p + 2 q)])

without having to run Om everytime and without needing to copy and paste this big expression everytime I want to use it. One straightforward way is to simply do

Oms[eps_, q_, p_] := 
 1/3 (1 - eps) (Cos[2 p \[Pi]] + Cos[2 \[Pi] q] + 
     Cos[2 \[Pi] (p + q)]) + 
  1/6 eps (Cos[4 p \[Pi]] + Cos[2 \[Pi] (p - q)] + Cos[4 \[Pi] q] + 
     Cos[4 \[Pi] (p + q)] + Cos[2 \[Pi] (2 p + q)] + 
     Cos[2 \[Pi] (p + 2 q)])

But I was wondering if I could avoid seeing the expression itself entirely, if that makes sense.

In other words, I want to generate an expression first and then use that expression to define a function, but without seeing the expression itself (perhaps store it somehow).

sam wolfe
  • 4,663
  • 7
  • 37
  • 1
    = can be used to define function, too. It's exactly for your task: Oms[eps_, q_, p_]=Om[eps, q, p]; Don't abuse :=. – xzczd Oct 07 '21 at 15:08
  • Related: https://mathematica.meta.stackexchange.com/q/2414/1871 – xzczd Oct 07 '21 at 15:34

0 Answers0