0
$\begingroup$

I want to create a function returning subscripted symbols and I want these symbols to be assumed real. How do I do that?

ClearAll[Evaluate[Context[]<>"*"]]

d[n_]:=If[OddQ[n],Subscript[d,n],0]

Map[Re[d[#]]&,Range[1,5]]

(* {Re[Subscript[d, 1]],0,Re[Subscript[d, 3]],0,Re[Subscript[d, 5]]} *)
$\endgroup$
2
  • $\begingroup$ what do you mean by "I want these symbols to be assumed real."? $\endgroup$ Apr 15, 2013 at 12:25
  • $\begingroup$ @belisarius Re[d[1]] should return d_1 $\endgroup$
    – Yrogirg
    Apr 15, 2013 at 12:38

3 Answers 3

1
$\begingroup$

You can create the following set of upvalues for Subscript:

Subscript/:(Re|Im)[s_Subscript]:=s

if you want to clear the new rules run the command

UpValues[Subscript] = {}
$\endgroup$
2
  • $\begingroup$ It should be also complemented with Subscript /: Im[s_Subscript] := 0 $\endgroup$
    – Yrogirg
    Apr 15, 2013 at 13:55
  • $\begingroup$ @Yrogirg done!:) $\endgroup$
    – Spawn1701D
    Apr 15, 2013 at 14:59
0
$\begingroup$

Although @Spawn1701D 's solution seems to be working for simple cases, I couldn't make Mathematica to take Re of more bulky expressions. However there is an easy workaround via ComplexExpand :

ClearAll[Evaluate[Context[] <> "*"]]

d[n_] := If[OddQ[n], Subscript[d, n], 0]

Map[ComplexExpand[Re[d[#]]] &, Range[1, 5]]

(*{Subscript[d, 1], 0, Subscript[d, 3], 0, Subscript[d, 5]}*)
$\endgroup$
0
$\begingroup$

The following function createVars creates a specified number of subscripted variables, adds the appropriate assumptions to $Assumtpions and returns the variable list:

createVars[name_String, i_Integer] := With[{
   vars = Array[Subscript[ToExpression@name, #] &, i]},
  If[$Assumptions === True,
$Assumptions = {vars \[Element] Reals},
    AppendTo[$Assumptions, vars \[Element] Reals]];
  vars]

To make use of the assumptions, just apply Simplify:

enter image description here

If you want to remove the assumptions at some point, use $Assumptions=..

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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