0
$\begingroup$

I would like to make a substitution like the following

(-3 + n)!/.(-x_Integer + n)!:>a

where I expect a to be the output. Unfortunately, Mathematica 10 just returns

(-3 + n)!

I even tried

(-3 + n)!/.Factorial[Plus[-x_Integer,n]]:>a

And the result is still

(-3 + n)!

What am I doing wrong? How to make this substitution work?

$\endgroup$
  • 3
    $\begingroup$ What does MatchQ[-3, -x_Integer] tell you? $\endgroup$ – Marius Ladegård Meyer Oct 19 '16 at 15:11
  • 1
    $\begingroup$ It works if you leave out the minus, i.e. (-3 + n)!/.Factorial[Plus[x_Integer,n]]:>a because Mathematica recognizes the negative integer as integer too. If you only want negative values to match, you could do x_?(IntegerQ[#]&&Negative[#]&) $\endgroup$ – AndreasP Oct 19 '16 at 15:11
  • $\begingroup$ Thanks, good to know! Guess I'll just do /. (x_Integer + n)! /; x < 0:>a. $\endgroup$ – Kagaratsch Oct 19 '16 at 15:18
7
$\begingroup$

When having problems with a replacement rule, use FullForm to understand the patterns involved

(-3+n)!//FullForm

(*  Factorial[Plus[-3,n]]  *)

(-x_Integer+n)!//FullForm

(*  Factorial[Plus[n,Times[-1,Pattern[x,Blank[Integer]]]]]  *)

You can use

{(-3+n)!,(3+n)!}/.(_Integer+n)!:>a

(*  {a,a}  *)

Or, if the rule is only to apply for negative integers then use

{(-3+n)!,(3+n)!}/.(_Integer?Negative+n)!:>a

(*  {a,(3+n)!}  *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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