2
$\begingroup$

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!

$\endgroup$
  • $\begingroup$ 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}]]? $\endgroup$ – kglr Jul 24 '19 at 11:56
  • 1
    $\begingroup$ 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 $\endgroup$ – BlackPhoton Jul 24 '19 at 12:05
  • $\begingroup$ 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,...,..} $\endgroup$ – BlackPhoton Jul 24 '19 at 12:06
  • $\begingroup$ that means d wasn't 5, omega is had still d inside $\endgroup$ – BlackPhoton Jul 24 '19 at 12:12
  • $\begingroup$ % /. 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 $\endgroup$ – BlackPhoton Jul 24 '19 at 12:31
2
$\begingroup$

Thanks kglr! Gave me the right tools to solve this!

The answer:

% /. 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}]
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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