# Defining functions with a rule [closed]

A have a list of coefficients, and each one of them depends on xp, which I haven't defined so far. For example, A[] reads:

A[]=-((I (a^2 m^2 + 2 (-1 + xp)^2 λ +
a m (I - I xp + 2 (-2 + Q^2) ω) + ω (Q^4 ω +
2 xp (-I + 2 ω - xp^3 ω + xp (I + ω)) +
Q^2 (I - 2 ω + 2 xp^2 ω -
xp (I + 4 ω)))))/(4 (-1 + xp)^2 (I + a m + Q^2 ω - xp (I + 2 ω))))


Later on, I define xp as a function of a and Q. So I would like to do something like this:

y[a_, Q_] := A[] /. xp -> xp[a, Q]


However, when I compute y[0,0], this is what I get:

-((I (a^2 m^2 + a m (I + 2 (-2 + Q^2) ω - I xp[0, 0]) +
2 λ (-1 + xp[0, 0])^2 + ω (Q^4 ω +
Q^2 (I - 2 ω - (I + 4 ω) xp[0, 0] +
2 ω xp[0, 0]^2) +
2 xp[0, 0] (-I +
2 ω + (I + ω) xp[0, 0] - ω xp[0,
0]^3))))/(4 (-1 + xp[0, 0])^2 (I + a m +
Q^2 ω - (I + 2 ω) xp[0, 0])))


The a's and Q's are not 0, except when they are arguments of the function xp[a,Q].

My question is: how can I define y[a_,Q_] such that the a's and Q's get replaced by whatever values are given to the function?

• Try using Set(=) instead of SetDelayed. y[a_, Q_] = A[] /. xp -> xp[a, Q]. – Anjan Kumar Jul 12 '17 at 2:59
• Now the output is simply y[0,0] – Thiago Jul 12 '17 at 3:01
• I can't figure what you are asking. Mathematica did exactly what you asked it to do. It replaced xp with xp[0,0] as you told it. So what is the issue? When later you call y[1,1] say, it will now replace xp with xp[1,1] and so on. – Nasser Jul 12 '17 at 3:07
• I want to define the function y[a,Q] in such a way that the a's and Q's appearing in the expression are replaced by, say, 1 and 2 respectively, when I call the function y[1,2]. – Thiago Jul 12 '17 at 3:10
• Then just do as Anjan suggested. Write y[a_, q_] = A0 /. xp -> xp[a, q] ? This will replace xp by xp[a,q] first, and the result, is the body of the function. It is important to know the difference between = and := in function definition. (But I still do not see what difference this will make in your case, as the final result will be the same). – Nasser Jul 12 '17 at 3:18

y[a2_, Q2_] := A[] /. {xp -> xp[a2, Q2],a->a2,Q->Q2}