Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]]} *)
share|improve this question
    
what do you mean by "I want these symbols to be assumed real."? –  belisarius Apr 15 '13 at 12:25
    
@belisarius Re[d[1]] should return d_1 –  Yrogirg Apr 15 '13 at 12:38
add comment

3 Answers 3

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] = {}
share|improve this answer
    
It should be also complemented with Subscript /: Im[s_Subscript] := 0 –  Yrogirg Apr 15 '13 at 13:55
    
@Yrogirg done!:) –  Spawn1701D Apr 15 '13 at 14:59
add comment

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]}*)
share|improve this answer
add comment

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=..

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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