# Assume function is real valued

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]]} *)

-
what do you mean by "I want these symbols to be assumed real."? – Dr. belisarius Apr 15 '13 at 12:25
@belisarius Re[d[1]] should return d_1 – Yrogirg Apr 15 '13 at 12:38

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] = {}

-
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

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]}*)

-

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:

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

-