Skip to main content
added 958 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

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.

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.

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.

Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

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.