1
$\begingroup$

I want to define a function in Mathematica:

$$h(\tau_x)=\tau_x$$

$\tau_x$ has no special meaning. To be honest, it's totally OK if I use the function $h(x)=x$ to replace $h(\tau_x)=\tau_x$. It doesn't influence the final value I want. I just want to make this function look better.

Here is what I tried

In[85]:= h[Subscript[\[Tau], x]_]:=Subscript[\[Tau], x]
In[88]:= h[4]
Out[88]= 1
In[89]:= h[x_]:=x
In[90]:= h[4]
Out[90]= 4
In[91]:= <<Notation`
In[92]:= Symbolize[Subscript[\[Tau], x]]
In[93]:= h[Subscript[\[Tau], x]_]:=Subscript[\[Tau], x]
In[94]:= h[4]
Out[94]= Subscript[\[Tau], x]

Here is the image:

enter image description here

$\endgroup$
5
  • 4
    $\begingroup$ I recommend you don't try this. $\endgroup$
    – lericr
    Jun 12, 2023 at 16:21
  • $\begingroup$ @lericr hhh, my sanity tells me so, too. But our foumulas does look better if such a thing is possible. And it would be more convenient if the variables in Mathematica corresponde to the original math form. $\endgroup$ Jun 12, 2023 at 16:26
  • $\begingroup$ First, that's a subjective opinion, and one that I disagree with. I find all of the extra semantic-free decorations to be very distracting, less visually pleasing, and not at all convenient. But subjective opinions are subjective opinions. $\endgroup$
    – lericr
    Jun 12, 2023 at 16:32
  • $\begingroup$ Second, Mathematica suffers a bit from its own success. People expect things from it that would be so infeasibe in other languages as to not even come under consideration. Remember, at the end of the day Wolfram Language is a programming language (or a computational language if you prefer). It needs some constraints to be able to parse and compute deterministically. Thus the way pattern matching works for arguments. It has its own sort of elegance, and it's just inappropriate to compare it to free form mathematical notation that can be explained on-the-fly (and needn't even be unambiguous). $\endgroup$
    – lericr
    Jun 12, 2023 at 16:36
  • $\begingroup$ @lericr Thanks! I got your idea and I won't waste more time on this. $\endgroup$ Jun 12, 2023 at 16:43

1 Answer 1

2
$\begingroup$

Even though this is – as already mentioned by @lericr in the comments and many times across this SE – generally not advised, I still want you to explain why your code does not work.

After symbolizing Subscript[τ, x] and defining h, take a look at the InputForm (!) of downvalues assigned to h:

Clear[h];
h[Subscript[τ, x] _] := Subscript[τ, x]
DownValues[h] // InputForm
(* {HoldPattern[h[τ⎵Subscript⎵x*_]] :> τ⎵Subscript⎵x} *)

Do you see the problem? Subscript[τ, x] _ got parsed as Subscript[τ, x]*_ – multiplication between Subscript[τ, x] and "something". That is why the following code returns a somehow "unexpected" result:

h[2 Subscript[τ, x]]
(* Subscript[τ, x] *)

Therefore, you need to force the parser to understand Subscript[τ, x]_ as Pattern[τ, Blank[]]. The cleanest way to do this is by using a colon:

Clear[h];
h[Subscript[τ, x] : _] := Subscript[τ, x]
h[4]
(* 4 *)
$\endgroup$
4
  • $\begingroup$ Thank you! But where is the semicolon you mentioned? There is only colon in h[Subscript[τ, x] : _] := Subscript[τ, x]. And there is error when I run it: Syntax::sntxf: "h[" cannot be followed by "Subscript[\[Tau],x]:_]". Is there any error? Sorry if I misunderstood you, I'm a big noob for Mathematica. $\endgroup$ Jun 13, 2023 at 11:32
  • $\begingroup$ Could you show your complete code if convenient? I think I can figure out where I made a mistake by checking your code. $\endgroup$ Jun 13, 2023 at 11:40
  • $\begingroup$ @XiangyuCui, ah, I meant colon instead of semicolon, sorry for the confusion. Note that you still have to first symbolize your variable, your code should look like this. $\endgroup$
    – Domen
    Jun 15, 2023 at 21:02
  • $\begingroup$ Sure, thank you! $\endgroup$ Jun 17, 2023 at 8:30

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.