3
$\begingroup$

I don't have much experience in Mathematica and think that my question is pretty straightforward. I have a couple of functions

f1[x1,x2,...],...,fN[x1,x2,...].

For the sake of clarity in the final expression (which is very long), I would now like to replace the expressions of the functions that include the variable names, simply by their names, i.e. drop their dependent variables: #/. fi[x1,x2,...] -> fi. Is there a simple way to achieve this without having to introduce this same replacement rule for every function f1,...,fN?

Any help is appreciated, thanks!

$\endgroup$
3
  • 3
    $\begingroup$ Does Head achieve your goal? $\endgroup$
    – rnotlnglgq
    Commented Mar 7, 2023 at 17:51
  • $\begingroup$ Do your functions have definitions? If they are just "dummy" symbols, then it would be pretty easy to build a replacement rule or a new function to do the replacement. But if f1, f2, etc, already have definitions, then you'll need to suppress evaluation first. Like, if f1 is already defined so that f1[1,2,3] evaluates to 17 (or whatever), then you'll need to suppress evaluation so that f1 is still explicit and can be manipulated. $\endgroup$
    – lericr
    Commented Mar 7, 2023 at 19:05
  • $\begingroup$ If you could simplify your actual expressions enough to give us a more concrete example of what your expected result is (given whatever definitions you may have for your functions), then it would be easier to provide help. $\endgroup$
    – lericr
    Commented Mar 7, 2023 at 19:20

3 Answers 3

4
$\begingroup$

As an example:

funcs = {f1[x1, x2], f2[x1, x2, x3], f3[x1], f4[x1, x2, x3, x4]}

rule = a_[__] /; 
    StringTake[ToString@a, 1] == "f" && 
     Head@ToExpression@StringTake[ToString@a, {2, -1}] == Integer :> 
   a;

funcs /. rule

{f1, f2, f3, f4}

$\endgroup$
3
$\begingroup$

This is an example of why it is easier to use indexed variables.

To display indexed variables as subscripts

(Format[#[n_]] := Subscript[#, n]) & /@ {f, x};

expr = Total[f[#] @@ Array[x, 4] & /@ Range[5]]

enter image description here

To abbreviate the functions' display

expr /. f[n_][arg__] :> f[n]

enter image description here

$\endgroup$
3
$\begingroup$

Let's start by assuming your functions are just dummy symbols (i.e. they have no DownValues or other definitions assigned to them).

You could define a helper symbol for display purposes--I'll call it Pretty. (You could define Format directly on your symbols, but it sounds like you might only want to do this occasionally, "on demand").

Format[Pretty[f_][___]] := Style[f, Bold, Purple]

Now, let's assume that this is your expression:

f4[f1[1], f2[2, 3]] + f3[4, 5, 6]

You could now replace any desired heads with pretty versions:

f4[f1[1], f2[2, 3]] + f3[4, 5, 6] /. {f1 -> Pretty[f1], f2 -> Pretty[f2], f3 -> Pretty[f3]}

If your symbols aren't just dummy symbols, then it becomes more difficult. Let's apply some definitions:

f1[a_] := 2^a;
f2[a_, b_] := a^b;
f3[a_, b_, c_] := Mod[a*b, c];
f4[a_, b_] := a - b

If we just try the above again...

f4[f1[1], f2[2, 3]] + f3[4, 5, 6] /. {f1 -> Pretty[f1], f2 -> Pretty[f2], f3 -> Pretty[f3]}

...we'll get -4.

But we can play some tricks:

Activate[
  Inactivate[
    f4[f1[1], f2[2, 3]] + f3[4, 5, 6]] /. 
      {f1 -> Pretty[f1], f2 -> Pretty[f2], f3 -> Pretty[f3]}]

to get f1-f2+f3 (in bold and purple!).

You need to be careful, because although this looks like just f1-f2+f3, that's just the display form because of how I defined Format for Pretty expressions. The Pretty heads are still there, so you must be careful when doing any subsequent processing on this expression. For example, you might need to perform another replacement that un-applies the Pretty heads. One advantage of using Format directly on f1 et al is that the underlying expressions are still there--they haven't been mutated and so subsequent processing could proceed without needing to deal with Pretty.

$\endgroup$

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.