Tag Info

New answers tagged

4

The General Case Alas, Cases does not permit multiple replacement rules. But Replace does, although we must take care to 1) create an output list, 2) omit non-matching elements and 3) operate only upon the first level: lis = 6 + 1/z^4 + 4/z^2 + 4 z^2 + z^4; Replace[List @@ lis, {x_. z^e_. :> {x, e}, x_ :> {x, 0}, _ :> Sequence[]}, {1}] (* ...


5

Another possibility is to use ReplaceAll on the rhs of RuleDelayed in the second argument of Cases: rules = Alternatives[x_. Power[z, e_.] :> {x, e}, Times[x_, Power[z, e_.]] :> {x, e}, x_. :> {x, 0}]; (* your prefered second argument for Cases *) Cases[lis, pat : rules[[All, 1]] :> (pat /. List @@ rules)] (* {{6, 0}, {1, ...


7

Just define a new function that could take different inputs, either through overloading or using If, Which, or Switch. Clear[f] f[x_. Power[z, e_.]] := {x, e} f[Times[x_, Power[z, e_.]]] := {x, e} f[x_] := {x, 0} Cases[lis, y : Alternatives[x_., x_. Power[z, e_.], Times[x_, Power[z, e_.]]] :> f[y]] (* {{6, 0}, {1, -4}, {4, -2}, {4, 2}, {1, 4}} *) ...


5

One possibility is to use Replace with Sow/Reap: Scan[ Replace[#, { x_. Power[z, e_.] :> Sow[{x, e}], Times[x_, Power[z, e_.]] :> Sow[{x, e}], x_ :> Sow[{x, 0}]}] &, lis] // Reap // #[[-1, -1]] & {{6, 0}, {1, -4}, {4, -2}, {4, 2}, {1, 4}} However, the wildcard x_ could be tricky if you needed to operate deeper ...



Top 50 recent answers are included