# Force ReplaceAll to output vector of same length

I would like to do a replace in an expression...

a = t;
b = 1;

a/.t->Range[1,5] (*returns: {1,2,3,4,5} *)
b/.t->Range[1,5] (*returns: 1           *)


but I would like to force the second output to return

{1, 1, 1, 1, 1}


Is there a way I can do this? My expression for "b" will be changing so I thought it would be nice if I could just force it to do this (like map but for expressions).

Thanks!

EDIT: Edit to clarify what I am trying to do.

I have an expression of "t" that changes as I make calculations. For example, let the expression be

express = t^2; (* OR t, OR 1, OR 3*t+t^2 *)


I then would like to evaluate this expression at several values of t so I do the following.

tSet = Range[1,5]
express/.t->tSet


where I will get a list if the expression contains "t". If the expression is constant

express = 1;
express/.t->tSet (* will output just 1, I would like it to be list of ones of length 5 to match tSet *)


I just get the scalar value, 1.

I don't want to create a function that does this unless I absolutely have to.

Hope that makes more sense...

• How comes t has length 5? You asked for same length as input.... – Dr. belisarius Feb 27 '15 at 3:35
• @belisarius Basically, I would like the expression to behave like a function without declaring a function. I would like "b/.t->Range[1,5]" to return "{1,1,1,1,1}" where I consider Range[1,5] to be an input vector of length 5. If that makes sense... – Jon Feb 27 '15 at 3:42
• You seem to have your syntax backward. Range[1,5] in your example is output, not input. We need a more complete example of what you are trying to accomplish. – Mr.Wizard Feb 27 '15 at 3:43
• express = t^2;t=Range@5;Print@expres; – Dr. belisarius Feb 27 '15 at 4:09

Based on your update I think this is the simplest way to achieve what I believe you want:

express = 1;
tSet = Range[1, 5];
Table[express, {t, tSet}]

{1, 1, 1, 1, 1}

express = t^2;
Table[express, {t, tSet}]

{1, 4, 9, 16, 25}


You lose the direct vector evaluation of Listable functions but this is both more general and a solution to your specific request.

Since you Accepted this (thanks) the result above must be what you want. Here is a way to do it using ReplaceAll as you started with, instead of Table.

When the right-hand-side of /. is a list of lists of rules the result is a list of substitutions each using one of those lists. For example:

0 /. {{_ -> 1}, {_ -> 2}, {_ -> 3}}

{1, 2, 3}


Therefore you could achieve what you want by converting t -> tSet into a list of lists of rules:

new = List /@ Thread[t -> tSet]

{{t -> 1}, {t -> 2}, {t -> 3}, {t -> 4}, {t -> 5}}


Application:

1 /. new
t^2 /. new

{1, 1, 1, 1, 1}

{1, 4, 9, 16, 25}


There are other approaches but I think these two are the easiest to apply and the closest to what you requested.

• If the OP accepts this one I'll upvote you as a recognition for your divination talent – Dr. belisarius Feb 27 '15 at 4:12
• @belisarius I have written more than once "+1 for divination" :^) – Mr.Wizard Feb 27 '15 at 4:13
• @belisarius Pay up. ;o) – Mr.Wizard Feb 27 '15 at 4:21
• OK,ok. But you're using dangerous fumes. This is the last warning – Dr. belisarius Feb 27 '15 at 4:23