2
$\begingroup$

I am trying to set a function within a function. What I do is to first create a function with two arguments. Then I would like to set a new function with the same two arguments, in which the previous function is inside the new one. However, when I do this, it seems that it does not read it properly.

I would appreciate it if sb could tell me what I am doing wrong.

See code below.

    Subscript[p, 1][Subscript[q, 1] _, Subscript[q, 2] _] := 
 Subscript[α, 1] + γ Subscript[q, 2] + 
  Subscript[q, 1] Subscript[β, 1]
Subscript[Π, 1][Subscript[q, 1] _, 
  Subscript[q, 
   2] _] := (Subscript[p, 1][Subscript[q, 1], Subscript[q, 2]] - 
    Subscript[w, 1])*Subscript[q, 1]
Subscript[Π, 1][Subscript[q, 1] _, Subscript[q, 2] _]

And see the picture for help enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks for your answer. It is meant to define a function Pi_A with arguments q_1 and q_2, which is also defined as a previously defined function with the same arguments. $\endgroup$ Commented Mar 16, 2022 at 10:36
  • 1
    $\begingroup$ Take a look at this attempt. Function arguments don't need to be subscripted. When calling a function, you provide arguments, not patterns with an underscore. $\endgroup$
    – Syed
    Commented Mar 16, 2022 at 10:44
  • 1
    $\begingroup$ @Syed, Many thanks, that solved my problem. $\endgroup$ Commented Mar 16, 2022 at 11:08

1 Answer 1

3
$\begingroup$

You must use Symbolize of the built-in Notation` package to use Subscripts as symbols.

Note: I symbolized both p and q below but you only need to symbolize q for the below to work.

Only symbols can be used as parameter names for function definitions. However, your subscripted expressions are not symbols.

Head @ Subscript[q, 1]
Subscript

With the Notation package and its helpful palette you can Symbolize these expressions.

Needs["Notation`"]
Symbolize[NotationTemplateTag[Subscript[p, _]]]
Symbolize[NotationTemplateTag[Subscript[q, _]]]

enter image description here

Subscripted p and q are now parsed as symbols.

Head @ Subscript[q, 1]
Symbol

One final step is we must use the Optional syntax when using these Symbolized symbols as parameter names.

Subscript[p, 1][Subscript[q, 1] : _, Subscript[q, 2] : _] := 
 Subscript[α, 1] + γ Subscript[q, 2] + Subscript[q, 1] Subscript[β, 1]

Notebook view below; easier to read.

enter image description here

You also have the ability to assign values to your symbolized subscripted symbols.

Subscript[q, 1] = 5;
Subscript[\[CapitalPi], 1][Subscript[q, 1], Subscript[q, 2]]

enter image description here

Symbolized symbols can be removed with RemoveSymbolize; also in the palette.

RemoveSymbolize[NotationTemplateTag[Subscript[p, _]] ]
RemoveSymbolize[NotationTemplateTag[Subscript[q, _]] ]

enter image description here

Hope this helps.

$\endgroup$

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