# Function with lazy and non-lazy arguments

Consider the function

Z[entropy_, beta_] := Sum[E^(entropy[[i, 2]] - beta entropy[[i, 1]])
, {i, 1, Length[entropy]}]


where entropy is a list of pairs (e.g. E, S(E)). I will plot this as a function beta. Since the parameter "entropy" is fixed in the plot, I wound't need to expand the sum every time, which this function is doing. So, I could try (the difference is = instead of :=)

Z[entropy_, beta_] = Sum[E^(entropy[[i, 2]] - beta entropy[[i, 1]])
, {i, 1, Length[entropy]}]


However, in this situation, because "entropy" is not defined, it will always return 0 (sum of 0 terms).

Ideally, I would like to write Z[entropyA, beta] (where entropyA is a list) and it return the sum expanded (lazy on beta), and when it is called as Z[entropyA, 1.2], it returns the outcome of the calculation (i.e. it replaces beta by 1.2 on the sum).

Which brings me to my question: how can I define a function that is lazy on one parameter (e.g. beta), but not lazy on the other.

• You'll have to make up your mind; is the entropy argument a list or an integer, and does your function have two or three arguments? – J. M. will be back soon Jul 22 '15 at 8:25
• Thanks for pointing it out, fixed. The function was more complicated and I was trying to build a minimal example. – Jorge Leitao Jul 22 '15 at 8:32
• A compact definition: Z[entropy_List, beta_] := Total[E^(#2 - beta #1) & @@@ entropy]; this will not expand unless the first argument is manifestly a list. – J. M. will be back soon Jul 22 '15 at 8:37
• Will you need to do symbolic manipulations on this function (which makes sense for a partition function, e.g. for computing the average form the derivative)? Or do you only need to evaluate for numerical beta? – Szabolcs Jul 22 '15 at 10:33

z[entropy_][beta_] :=
Sum[E^(entropy[[i, 2]] - beta entropy[[i, 1]]), {i, 1,
Length[entropy]}];

f = z[{{1, 2}, {3, 4}}];

f /@ Range@5

(* {2 E, 1 + 1/E^2, 1/E^5 + 1/E, 1/E^8 + 1/E^2, 1/E^11 + 1/E^3} *)


Avoid using uppercase initials for your own symbols, BTW...

This still evaluates the sum, of course. You can achieve the desired goal with your Set definition:

f=Z[{{1, 2}, {3, 4}}, beta];


Then use replacement for differing beta:

f/.beta->1.2


What I think you are seeking:

z[entropy_List] :=
beta \[Function]
Evaluate[Sum[E^(entropy[[i, 2]] - beta entropy[[i, 1]]), {i, 1, Length[entropy]}]]


Now:

z[{{9, 1}, {3, 6}, {3, 4}, {4, 0}, {9, 5}}]

Function[beta$, E^(1 - 9 beta$) + E^(5 - 9 beta$) + E^(4 - 3 beta$) + E^(6 - 3 beta$) + E^(-4 beta$)]