1
$\begingroup$

Suppose I start with an expression like y[t] + x[t]: is there a function that lets me remove the [t] elements, so I obtain the expression y + x?

$\endgroup$
2
  • 1
    $\begingroup$ What you asking is rather strange. What a you trying to accomplish? Also, are you aware that the Mathematica evaluator sees your expression as Plus[x[t], y[t]]; that there is another level of square brackets that the notebook editor doesn't normally show? $\endgroup$
    – m_goldberg
    May 10, 2019 at 0:28
  • $\begingroup$ Imagine this: I have an expression where x[t] and y[t] are functions that I evaluate for different values of t. I want to perform other manipulations of the expression, such as taking derivatives with respect to x or y, that no longer require keeping the argument [t]. $\endgroup$
    – dtcitron
    May 10, 2019 at 1:39

3 Answers 3

2
$\begingroup$

try

exp= y[t] + x[t] ;
exp /.Level[#->Head@#&/@Level[exp,1],1]    

x+y

$\endgroup$
2
  • $\begingroup$ Can you tell me more about the syntax you're using here? $\endgroup$
    – dtcitron
    May 10, 2019 at 1:40
  • $\begingroup$ this replaces x[t] with Head[x[t]] etc... $\endgroup$
    – ZaMoC
    May 10, 2019 at 1:49
5
$\begingroup$

For your example at least, the following works.

Replace[y[t] + x[t], u_[_] -> u, 2]

x + y

Update

As I suspected, for the real problem underlying the question, the above method is not going to work. I would recommend using ReplaceAll and give a separate rule for each specific head involved. Like so.

Sin[y[t] + x[t]]/(1 + x[t]^2 + y[t]^2)^(1/2) /. {x[t] -> x, y[t] -> y}
Sin[x + y]/Sqrt[1 + x^2 + y^2]

As you see from the above example, this approach will handle quite complex expression involving x[t] and y[t] quite robustly.

$\endgroup$
3
  • 1
    $\begingroup$ +1. I like Head /@ (y[t] + x[t]) for weird usage points... $\endgroup$
    – ciao
    May 10, 2019 at 0:45
  • $\begingroup$ This worked nicely - can you explain more the syntax with u_[_] here? $\endgroup$
    – dtcitron
    May 10, 2019 at 1:31
  • $\begingroup$ @dtcitron. u_[_] is a pattern that matches any expression with a single argument. When a match is made, Mathematica will bind u (temporarily) to the expression's head. The rule in which u_[_] appears tells Replace that, when a match occurs, to replace the matched expression with what u is bound to, namely the head. $\endgroup$
    – m_goldberg
    May 10, 2019 at 3:15
2
$\begingroup$

Something like

f = (ToString /* StringDelete[Shortest["["~~___~~"]"]]);
f[y[t] + x[t] - z[u]]
(* x + y - z *)
$\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.