2
$\begingroup$

I am looking for replacement rules that will accomplish the following:

  • If the variable name of the function contains "p", append "aa" to the function name
  • If the variable name of the function does not contain "p", append "bb" to the function name

Here are some examples

myfun1[x1] ==> myfun1bb[x1]
myfun2[x1p] ==> myfun2aa[x1p]
myfun3[x4p] ==> myfun4aa[x4p]

Any help is greatly appreciated!

$\endgroup$
4
  • $\begingroup$ May I ask why? :P Mixing up the name of a variable/function and the value of a variable/function is not usually considered good practice. $\endgroup$ Aug 4, 2017 at 22:27
  • $\begingroup$ Will you only consider symbols as arguments, or can they be numbers, or expressions, or... $\endgroup$ Aug 4, 2017 at 22:28
  • $\begingroup$ @MariusLadegårdMeyer Thanks for your comments, I will only use symbols as arguments. The reason why I am doing this is that I have a long expression (>1000 terms) which I need to integrate over (it is a multi dimensional integration). It happens that the individual parts can be integrated analytically, but the integral depends in on the symbolic variable (it makes a difference on whether we are integrating over x1 or x1p). Mathematica can sovle these integrals but it is rather slow. $\endgroup$
    – ftiaronsem
    Aug 6, 2017 at 14:37
  • $\begingroup$ I have therefore saved the analytically integrated parts in the variables myfun1bb and myfun2aa. So after the replacement the result is the correctly integrated function. But maybe I overlooked a simpler way of achieving this... $\endgroup$
    – ftiaronsem
    Aug 6, 2017 at 14:41

4 Answers 4

1
$\begingroup$

Convert to strings and then back to a symbol.

rule = fun_[arg_] :> Module[
    {
     funString = ToString[fun],
     argString = ToString[arg]
     },
    If[
     StringMatchQ[argString, ___ ~~ "p" ~~ ___],
       funString = funString <> "aa",
       funString = funString <> "bb"
     ];
    ToExpression[funString <> "[" <> argString <> "]"]
    ];

Now test it on the two cases.

myfun[x1] /. rule
(* myfunbb[x1] *)

myfun[x1p] /. rule
(* myfunaa[x1p] *)
$\endgroup$
1
  • $\begingroup$ This worked perfectly for what I was trying to do! Thanks so much $\endgroup$
    – ftiaronsem
    Aug 7, 2017 at 12:55
1
$\begingroup$

I answer this with the caveat that I share Marius's puzzlement at why you have to do this. Nevertheless:

Operate[Function[x, Symbol[ToString[#] <>
                           If[StringFreeQ[ToString[x], "p"], "bb", "aa"]][x]] &,
        myfun1[x1]]
   myfun1bb[x1]

Operate[Function[x, Symbol[ToString[#] <>
                           If[StringFreeQ[ToString[x], "p"], "bb", "aa"]][x]] &,
        myfun3[x4p]]
   myfun3aa[x4p]
$\endgroup$
1
$\begingroup$
f1 = # /. h_[x__] :> If[And @@ StringFreeQ[ToString /@ {x}, "p"], 
  Symbol[SymbolName[h] <> "bb"], Symbol[SymbolName[h] <> "aa"]][ x] &;


f1 /@ {myfun1[x1], myfun2[x1p], myfun3[x1, x2, x3p], myfun4[x1, x2, xyz[pqrs]]}

{myfun1bb[x1], myfun2aa[x1p], myfun3aa[x1, x2, x3p], myfun4aa[x1, x2, xyz[pqrs]]}

Or, using Replace

f2 = Replace[#, h_[x__] :> If[And @@ StringFreeQ[ToString /@ {x}, "p"], 
       Symbol[SymbolName[h] <> "bb"], Symbol[SymbolName[h] <> "aa"]][x]] &;
$\endgroup$
1
$\begingroup$

Now that we have some clarification in the comments, I would propose a simple wrapper-type definition:

myfun1[arg_] := If[StringFreeQ[SymbolName[arg], "p"], myfun1bb[arg], myfun1aa[arg]]

Do you need it to be more complicated than this?

$\endgroup$
2
  • $\begingroup$ Thanks for providing this answer, unfortunately it doesn't quite fit in my case since I can't redefine the function myfun1. I do need it later on in the calculations (together with the integrated function). Basically I get this very long sum of terms containing myfuns and need to integrate it. Afterwards I need access to the original sum of terms and to the integrated sum of terms. $\endgroup$
    – ftiaronsem
    Aug 7, 2017 at 3:35
  • $\begingroup$ Hmm, but thinking of it, I could make a copy of the long expression, replace all the occurances of myfun with myfuntemp and then use your approach. So it would work. I will evaluate the different answers more closely tomorrow. Thanks. $\endgroup$
    – ftiaronsem
    Aug 7, 2017 at 3:36

Your Answer

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

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