# Pattern match with "+" sign

Can you pattern match with a "+" sign?

Here is the recursion...(which does not work)

Pr2[i_ + j_, 0] := (λ/(λ + μ))^(i + j)
Pr2[i_ + j_, j_] := (((i + j) - j + 1)/j)*((1 - λ/(λ + μ))/(λ/(λ + μ)))*Pr2[i + j, j - 1];


The original recursion is:

Pr1[n_, 0] := (λ/(λ + μ))^n
Pr1[n_, j_] := ((n - j + 1)/j)*((1 - λ/(λ + μ))/(λ/(λ + μ)))*Pr2[n, j - 1];


which does work.

I just want to be able to substitute $$i+j$$ for $$n$$ in the recursion.

==============================

Here are some examples of function calls and output

$${\bf The\ function\ that\ works}$$:

$$\mbox{Pr1}[35, 10] = \frac{183579396 \lambda ^{25} \left(1-\frac{\lambda }{\lambda +\mu }\right)^{10}}{(\lambda +\mu )^{25}}$$

$$\mbox{Pr1}[4, 2] = \frac{6 \lambda ^2 \left(1-\frac{\lambda }{\lambda +\mu }\right)^2}{(\lambda +\mu )^2}$$

$$\bf{So...for\ the\ new\ function}$$...

$$\mbox{Pr2}[2 + 2, 2] = \mbox{Pr1}[4, 2]$$

$$\mbox{Pr2}[25 + 10, 10] = \mbox{Pr1}[35, 10]$$

$$\bf{I\ also\ tried}$$...

Pr2[Plus[i_, j_], 0] := (\[Lambda]/(\[Lambda] + \[Mu]))^Plus[i, j];
Pr2[Plus[i_, j_], i_] := (Plus[i, j] - i + 1)/
i (1 - \[Lambda]/(\[Lambda] + \[Mu]))/(\[Lambda]/(\[Lambda] + \
\[Mu])) Pr2[Plus[i, j], i - 1];


But the result is...

(3 (\[Lambda] + \[Mu]) (1 - \[Lambda]/(\[Lambda] + \[Mu])) Pr2[4,
1])/(2 \[Lambda])


which is not what I want.

• Yes, you can pattern match on Plus. But why do you want to? If i & j are numbers, then i + j will resolve to a number and won't match the Plus. If you pass a Plus expression with undefined symbols, that will match the Plus, but it also would have matched the n_, so I'm not sure what you gain. You could play around with various Hold* attributes, but without knowing what you're trying to achieve, it's hard to suggest anything. May 12, 2022 at 14:28
• What's a typical function call and expected output? May 12, 2022 at 15:26
• @MichaelE2 I gave some examples - thanks...
– PiE
May 12, 2022 at 18:32
• @lericr Because I can code this in .Net and don't understand why I can't do the same in Mathematica
– PiE
May 12, 2022 at 18:35

ClearAll[Pr2]


Give Pr2 the attribute HoldFirst

SetAttributes[Pr2, HoldFirst]

Pr2[i_ + j_, 0] := (λ/(λ + μ))^(i + j)
Pr2[i_ + j_, j_] := (((i + j) - j + 1)/j)*
((1 - λ/(λ + μ))/(λ/(λ + μ)))*Pr2[i + j, j - 1];

Pr2[2 + 2, 2]

(* (3 (λ + μ) (1 - λ/(λ + μ)) Pr2[2 + 2, 1])/(2 λ) *)


Note that the second definition requires the second argument to be the same as one of the summands; consequently, Pr2[2+2, 1] cannot evaluate.

Decouple the second argument from the summands

ClearAll[Pr2]

SetAttributes[Pr2, HoldFirst]

Pr2[i_ + j_, 0] := (λ/(λ + μ))^(i + j)
Pr2[i_ + j_, k_] := (((i + j) - k + 1)/k)*
((1 - λ/(λ + μ))/(λ/(λ + μ)))*Pr2[i + j, k - 1];

Pr2[2 + 2, 2]

(* (6 λ^2 (1 - λ/(λ + μ))^2)/(λ + μ)^2 *)

Pr2[25 + 10, 10]

(* (183579396 λ^25 (1 - λ/(λ + μ))^10)/(λ + μ)^25 *)