2
$\begingroup$

I'm doing some heavy symbolic calculations and for preliminary simple tests I need to replace all coefficients except some choosings, e.g.:

A = {{0, 0}, {{2, 0}, {1, 1}, {0, 2}}, {{4, 0}, {3, 1}, {2, 2}, {1, 3}, {0, 4}}}

a = {{0, 0} -> e1, {1, 1} -> e2, {3, 1} -> e3} (* e.g from CoefficientRules *)

I want that something like A /. a gives {e1, {e2}, {e3}}.

These two similar question didn't help me:

$\endgroup$

3 Answers 3

4
$\begingroup$

I agree with Domen's answer. But if your coefficients were numeric and you were worried about the edge cases, you could do something like this:

Replace[A, Append[a, _ -> Nothing], {-2}]
$\endgroup$
6
$\begingroup$

I don't think it's worth complicating with some general except pattern. Since you know all your elements are in the form of {a_, b_}, just use this pattern to remove any numeric leftovers:

A /. a /. {_?NumberQ, _?NumberQ} -> Nothing
(* {e1, {e2}, {e3}} *)
$\endgroup$
2
$\begingroup$

may be (for the example you show)

A={{0,0},{{2,0},{1,1},{0,2}},{{4,0},{3,1},{2,2},{1,3},{0,4}}}
a={{0,0}->e1,{1,1}->e2,{3,1}->e3} 

And now

Map[DeleteCases[#,_List]&,A/.a]

enter image description here

I am sure there are many other ways to do this. (AT least 9 more ways)

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.