# How to construct multible rules for replacement at once?

After generating a list for which I used //N, I want to replace some values with the exact one.

What I had in mind is to generate something like:

{1, 2, 3, 4, -1, -2, -3, -4} /. {x_ /; x < 0 -> n, x > 0 -> p}


where the outcome is:{p, p, p, p, n, n, n, n}

My problem here is that my output prioritizes /; first and doesn't set d=5 and the i only gets distributed in the last omega (-> \[Omega][d]^(i)):

%/.Module[{d = 5},
Table[x_ /; Im[Chop[N[\[Omega][d]^(i)]]] - 10^(-5) <
Im[x] < Im[Chop[N[\[Omega][d]^(i)]]] + 10^(-5) -> \[Omega][
d]^(i), {i, 0, 4}]]


Output:

{x$$_ /; Im[Chop[N[\[Omega][d$$472152]^i]]] - 1/10^5 <
Im[x$$] < Im[Chop[N[\[Omega][d$$472152]^i]]] + 1/10^5 -> 1,...,...}


My noobish attempt was:

%/.Module[{d = 5}, ("x_/;") /@
Table[Im[Chop[N[\[Omega][d]^(i)]]] - 10^(-5) < x_ /;
Im[x] < Im[Chop[N[\[Omega][d]^(i)]]] + 10^(-5) -> \[Omega][
d]^(i), {i, 0, 4}]]


Output here: without %/.

{"x_ /;"[-(1/100000) < Im[x] < 0 -> 1],...,...}


with %/.

Syntax error:

....is neither a list of replacement rules nor a valid dispatch table, \ and so cannot be used for replacing.

Thanks for help beforehand!

• try Block[{d = 5}, Table[With[{i = i}, x_ /; Im[Chop[N[\[Omega][d]^(i)]]] - 10^(-5) < Im[x] < Im[Chop[N[\[Omega][d]^(i)]] + 10^(-5)] -> \[Omega][d]^(i)], {i, 0, 4}]]?
– kglr
Jul 24 '19 at 11:56
• your code as no problems, but still doesn't replaces my stuff. Maybe problems with my stuff.. I will tell if it's a problem from the code or my own a little later, THANKS Jul 24 '19 at 12:05
• ah, the promblem is that Im[Chop[N[\[Omega][d]^0]]] doens\t get evaluated: The Output is: {x$_ /; Im[Chop[N[\[Omega][d]^0]]] - 1/10^10 < Im[x$] < Im[Chop[N[\[Omega][d]^0]] + 1/10^10] -> 1,...,..} Jul 24 '19 at 12:06
• that means d wasn't 5, omega is had still d inside Jul 24 '19 at 12:12
• % /. Table[ With[{i = i, d = 5}, x_ /; Im[Chop[N[\[Omega][d]^(i)]]] - 10^(-3) < Im[x] < Im[Chop[N[\[Omega][d]^(i)]]] + 10^(-3) -> \[Omega][d]^(i)], {i, 0, 4}] WORKS! THANKS, didn't know that With[ ] existed Jul 24 '19 at 12:31

% /. Table[ With[{i = i, d = 5}, x_ /; Im[Chop[N[\[Omega][d]^(i)]]] - 10^(-3) <