2

For a given expression, such as:

exp=p[x]+p'[x]+p''[x]...Derivative[n][p][x]

I want to replace all the derivatives of p[x] to a constant 1. But the following rule:

rule=Derivative[_][p][x]:>1

works for the derivatives when n>=1, except `p[x]. That is:

p'[x]/.rule
(* output: 1 *)

p''[x]/.rule
(* output: 1 *)

p[x]/.rule
(* output: p[x] *)

I wonder what is the correct rule to replace all the Derivative[n][p][x] to 1, including p[x].

Thank you.

user64494
  • 26,149
  • 4
  • 27
  • 56
PureLine
  • 1,310
  • 7
  • 20
  • 1
    It's not elegant, but works: rule={Derivative[_][p][x] :> 1, p[x] :> 1} – Oscillon Apr 26 '19 at 15:56
  • @Oscillon there are almost 15 rules... so this solution will double the input work. Oh... we do need some solution to reduce our labour. – PureLine Apr 26 '19 at 15:59
  • 1
    Try rule = p[x] | Derivative[_][p][x] :> 1. – Somos Apr 26 '19 at 16:55
  • 1
    If you need it to be more general (i.e. you need to set every derivative to 1), then you can use a variation of @Somos's suggestion: rule = _[x] | Derivative[_][_][x] :> 1 or something there-abouts. – march Apr 26 '19 at 17:01
  • Duplicate of https://mathematica.stackexchange.com/q/194099 ? – Roman Apr 26 '19 at 17:15
  • @march En. It seems the method from Somo is viable. Only part of the function should be modified, not all the function. More precisely, I would have revised the arbitrary order derivatives of a particular function. – PureLine Apr 27 '19 at 02:37

1 Answers1

2

Given such an expression:

exp = Sum[Derivative[i][p][x], {i, 0, 7}]

A nice way would be to replace it by a pure function:

exp /. {p :> (1 &)}
(* 1 *)

which replaces all derivatives and the original function.

amzon-ex
  • 141
  • 6
  • 1
    I believe the desired output of replacing all the derivatives should be 8, not 1. Here's a cute way to use a pure function: exp /. p -> (Exp[# - x] &). Works only if the argument to the derivatives of p is x. – Michael E2 Aug 10 '22 at 14:15