1
$\begingroup$

I am working on a problem with a lot of (greek) variables and it makes sense to sub-script them. Some of them must appear in functions and I am having trouble using greek, subscripted variables appearing in functions. In fact I cannot use any subscripted variables.

f[Subscript[\[Beta], 1] _] := 0.1*Subscript[\[Beta], 1]
f[5.0]
f[5.]

While the similar code produces the correct result:

g[\[Beta]_] := 0.1*\[Beta]
g[5.0]
0.5

Note, I can't get things to look quite the same as in my Mathematica window here, so I include a screenshot:

enter image description here

How do I create a function with subscripted arguments?

$\endgroup$
8
  • 2
    $\begingroup$ Using subscripted variables often leads to problems. Instead, use indexed variables. E.g.: g[\[Beta][1] _] := 0.1*\[Beta][1] $\endgroup$ May 24, 2023 at 16:04
  • 2
    $\begingroup$ I have tried subscripts and never got them to work. I much prefer just to use a variable with a single bracket i.e. a[1] , a[2], a[3],... or b[1,1], b[1,2] ,b[1,3].... If I do want answers to look smart I do this at the end with replacements rules like poshForm={a[1]->a_1, a[2]-> a_2}. Sorry to suggest that you are making more effort than you need to. Perhaps someone has a good method; Let's see. $\endgroup$
    – Hugh
    May 24, 2023 at 16:04
  • 1
    $\begingroup$ Use Notation Package and Symbolize. $\endgroup$ May 24, 2023 at 16:06
  • 1
    $\begingroup$ Consider also Pitfalls, point #3. $\endgroup$
    – Michael E2
    May 24, 2023 at 16:20
  • 1
    $\begingroup$ @DanielHuber, your approach unfortunately does not work: Clear[g]; g[β[1]_] := 0.1*β[1]; g[1] gives g[1] ... $\endgroup$
    – Domen
    May 24, 2023 at 16:27

3 Answers 3

1
$\begingroup$

Using Notation Package and Symbolize:

<< Notation`
Symbolize[ParsedBoxWrapper[SubscriptBox["\[Beta]", "1"]]]
Subscript[\[Beta], 1] // Head
(*Symbol*)

Now, you can use this symbol to define your function:

f[Subscript[\[Beta], 1] : _] := 0.1*Subscript[\[Beta], 1]
f[5.0]
(*0.5*)
$\endgroup$
0
$\begingroup$

This is not directly possible. When you write g[β_] := 0.1*β, this can be written in a slightly longer form as g[β : _] := 0.1*β. The underscore represents a Blank pattern, and β before : denotes the name of the pattern. However, as you can read in the documentation for Pattern, the name can only be a pure symbol, such as β or β1, but not Subscript[β, 1]. You can read more about patterns in this tutorial.

If you really somehow visually prefer subscripted variables in function definition, you can use ReplaceAll to make replacements:

Clear[f]
f[β1_] := 0.1 * Subscript[β, 1] /. Subscript[β, 1] -> β1
f[2]
(* 0.2 *)

enter image description here

$\endgroup$
0
$\begingroup$

First level:

To address precisely your example, the expression

f[Subscript[\[Beta_], 1] ] := 0.1*Subscript[\[Beta], 1]

defines the function that only acts on any variable with the subscript 1. For this reason, this works:

f[Subscript[\[Alpha], 1]]

(*  0.1 Subscript[\[Alpha], 1]  *)

but this does not

f[0.5]

(*  f[0.5]  *)

This also does not:

f[Subscript[\[Alpha], 2]]

(* f[Subscript[\[Alpha], 2]]  *)

just because the function f we defined with the subscript 1, not with 2.

As a funny example of your definition which works, let us evaluate this:

f[Subscript[0.5, 1]]

(*  0.1 Subscript[0.5, 1]  *)

It works, as you see, whatever Subscript[0.5, 1] should mean.

The correct definition of your function should be:

g[x_] := 0.1*x

Then

g[Subscript[\[Alpha], 1]]

yields

 (*  0.1 Subscript[\[Alpha], 1]  *)

as it should be. If we first fix Alpha1:

Subscript[\[Alpha], 1] = 0.5;
g[Subscript[\[Alpha], 1]]

(*  0.05  *)

That's probably what you expect.

Second level:

However, it seems to me that you fall down into the so-called, XY problem. To go out of it, let me say that I realize that you need to use both Greek letters and subscripts in your work. So, what to do? Is it, what you really asked? If yes, then please read below, if not, skip it.

Here there are two questions:

  1. How to use Greek letters?
  2. What to do with subscripts?

The answer to the first question is straightforward, and, in fact, you somehow have already solved it since you already used Greek letters. You know probably, that there are three ways to enter such a letter?

The story with subscripts is not that easy. In principle, at present, subscripts are designed for presenting mathematical expressions, but not for using them in calculations. It is because the structures of the variable like a1 and the one like Subscript[a,1] are different. To check it just evaluate the two following expressions

TreeForm[a1]

TreeForm[Subscript[a, 1]]

The complex structure visible in the latter expression has the potential to disturb some Mma operations, such as Simplify, for example. But only a potential. That is, in some cases, it disturbs, in others, it does not, and you never know what will happen.

For this reason, one is generally discouraged to use subscripts in calculations.

What to do if you inevitably need it?

The simple idea is to use letters with numbers, during the calculations and only transform these numbers into the subscripts after the calculation has been finished.

Here is a most trivial example:

(a1^2 + b1^2)/c2^3 /. {a1 -> Subscript[a, 1], b1 -> Subscript[b, 1], 
  c2 -> Subscript[c, 2]}

yielding this:

enter image description here

Otherwise, you can do it like this:

(a1^2 + b1^2)/c2^3 /. {a1 -> Subscript[\[Alpha], 1], 
  b1 -> Subscript[\[Beta], 1], c2 -> Subscript[\[Gamma], 2]}

with the following effect:

enter image description here

Depending upon the expressions you meet in your work, one may write a bit more sophisticated rules. Moreover, you may make one universal rule once forever. You can apply the rule in question either to visualize an intermediate expression to better understand it, or to make the final expression look traditional. Otherwise, you may do it to prepare the expression to use in a report, article, or presentation.

Have fun!

$\endgroup$
2
  • $\begingroup$ I think your first example does not work (forgot to clear f?): Clear[f]; f[Subscript[β, 1] _] := 0.1*Subscript[β, 1]; f[Subscript[α, 1]] gives f[Subscript[α, 1]]. $\endgroup$
    – Domen
    May 24, 2023 at 16:42
  • 1
    $\begingroup$ @Domen Right. I corrected the definition. $\endgroup$ May 24, 2023 at 16:56

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