4
$\begingroup$

I have a function that I want to see applied, potentially within a derivative. The use of delayed vs. non-delayed rules seems irrelevant. See the following:

f$sub = {f[c_] :> a c};

f[c] //. f$sub;

q (f[c])^2 //. f$sub;

D[f[c], c] //. f$sub;

D[f[c] //. f$sub, c] //. f$sub;

(*The following is a rough reason of why I wanted rules *)
g$sub = {g[a] :> a + f[c] }

g2$sub = {g2[a] :> a + D[f[c], c] }

g[a] //. g$sub (* Don't want f sub'd, want to see it left with the function *)

g[a] //. Union[g$sub, f$sub] (* Want f$sub used to simplify things *)

f$alt$sub = {f[c_] :> 2 a c};

g2[a] //. Union[g2$sub, f$alt$sub ](* What does it look like with the alternative *)

Ideally, I would want to see the output as a c, q (a c)^2, a. The last doesn't work, presumably becomes FullForm@D[f[c],c] is Derivative[1][f][c] which doesn't match the pattern. Are there any easy tricks to get the substitution to respect derivatives? My first attempt of changing the rule to fsub = {f[c_] :> a c, Derivative[1][f][c_] :> D[f[c], c]} didn't seem to do anything (delayed or not).

$\endgroup$
9
  • $\begingroup$ May be this D[f[c] //. fsub, c] //. fsub.Raise it to power or anything else to see if its done or not. $\endgroup$ Commented Sep 19, 2013 at 18:47
  • $\begingroup$ That does it for this example. The problem is that in reality I am applying this substitution to pretty complicated expressions where I want to write out the expression properly and then do the substitution. (e.g. something like exp = D[f[c],c] + a c / (D[f[c],c,c]^2); exp //. fsub; That approach would force me to re-write the entire expression with the substitutions embedded throughout. $\endgroup$
    – jlperla
    Commented Sep 19, 2013 at 18:50
  • 1
    $\begingroup$ Instead of using Replace simply use function f[c_] := a c, you will get same result. $\endgroup$ Commented Sep 19, 2013 at 18:57
  • $\begingroup$ Related, possible duplicates: (7876), (6089), (27230) -- have you looked at these? $\endgroup$
    – Mr.Wizard
    Commented Sep 19, 2013 at 19:03
  • 1
    $\begingroup$ @Blackbird I went ahead and posted it. If you choose to post I will delete mine. $\endgroup$
    – Mr.Wizard
    Commented Sep 19, 2013 at 19:09

2 Answers 2

2
$\begingroup$

Here's another possibility:

Clear[f,g,a,c]

f$sub = {f :> Function[{c}, a c]};

f[c] //. f$sub

(* ==> a c *)

q (f[c])^2 //. f$sub

(* ==> a^2 c^2 q *)

D[f[c], c] //. f$sub

(* ==> a *)

D[f[c] //. f$sub, c] //. f$sub

(* ==> a *)

g$sub = {g :> Function[a, a + f[c]]};

g2$sub = {g2 :> Function[a, a + D[f[c], c]]};

g[a] //. g$sub (*Don't want f sub'd,want to see it left with the \
function*)

(* ==> a + f[c] *)

g[a] //. 
 Union[g$sub, f$sub] (*Want f$sub used to simplify things*)

(* ==> a + a c *)

The only difference to your substitution rules is that I substitute only the Head and replace it by a Function containing the desired form. This works with Derivative because in the FullForm of an expression such as f'[x] you find the f by itself, and the replacement rule won't match it if that rule involves f[c_] as a pattern.

What happens behind the scenes in your derivative example is that D[...] gets converted into Derivative, which can cause consternation if you make patterns that are intended to match the function in D[f[x],x] because f[x] never appears in that form there.

$\endgroup$
4
  • $\begingroup$ Very interesting. Assuming that I don't really need pattern matching, are there any tradeoffs or gotchas in defining with functions in the Function[...] way compared to the more canonical method? Extra credit if you can combine the parameter with my other outstanding question( mathematica.stackexchange.com/questions/32648/…). e.g. Function[{a, b[bar]}, a + 2 b[bar]] $\endgroup$
    – jlperla
    Commented Sep 19, 2013 at 21:34
  • $\begingroup$ I'm not sure I understand what you're asking. This post is about "applying rules", isn't it? When you say you "don't really need pattern matching", how is that related to the question? What do you mean by "more canonical"? $\endgroup$
    – Jens
    Commented Sep 19, 2013 at 21:39
  • $\begingroup$ I guess canonical is a little ambiguous. I guess what I mean is the f[x_]:= 2 x style definition as slightly more canonical than f := Function[x, 2 x]and allowing for pattern matching (e.g. f[x_Integer]:= 3 x can be added for a specialization). As I don't need any of that fancy pattern matching but am unfamiliar with using functions with formal parameters, I guess I am asking whether their are a bunch of other gotchas when using Function[{x}, ...] $\endgroup$
    – jlperla
    Commented Sep 19, 2013 at 21:57
  • $\begingroup$ I can't think of any general way of answering the "gotcha" question, but in the context of your question, there is no gotcha since my approach produces the output you want for each of the examples you gave, using rules as stated in the question title. $\endgroup$
    – Jens
    Commented Sep 20, 2013 at 1:43
3
$\begingroup$

Blackbird suggested defining a function f rather than using a replacement rule.
Indeed, this appears to work as requested:

fn = Block[{f}, f[c_] := a c; #] &;

f[c]       // fn
q (f[c])^2 // fn
D[f[c], c] // fn
a c

a^2 c^2 q

a
$\endgroup$
4
  • $\begingroup$ Thanks. Take a look at my updated use case to explain why I want to use rules (e.g. display without the substitution, use alternative substitutions, etc.) $\endgroup$
    – jlperla
    Commented Sep 19, 2013 at 19:43
  • $\begingroup$ @jlperla I need to leave soon. From a quick glance I think you could still use the method above, you just need a method to convert rules to definitions. Does that sound plausible? $\endgroup$
    – Mr.Wizard
    Commented Sep 19, 2013 at 19:48
  • $\begingroup$ Sure. I couldn't figure out how to do the things above without using rules, but if I can fulfill the stated requirements in any way, then I am happy. $\endgroup$
    – jlperla
    Commented Sep 19, 2013 at 20:48
  • $\begingroup$ Mr wizard, what do you think of the solution by @Jens? Is it more general than this approach $\endgroup$
    – jlperla
    Commented Sep 20, 2013 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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