4
$\begingroup$

My goal is to add three equations: a==b, c==d, e==f. I could used MapThread and Replace as shown below, but is there a simpler method?

eqs={a==b,  c==d, e==f};
MapThread[Plus,eqs/.Equal->List]/.List->Equal   (* a+c+e==b+d+f *)
$\endgroup$

4 Answers 4

3
$\begingroup$
eqs = {a == b, c == d, e == f};

<< EqualThread.m

Total[eqs]
(*a+c+e==b+d+f*)

EqualThread is found at https://library.wolfram.com/infocenter/ID/4491/, and it has worked for every version of Mathematica since the 90's. If you put it in your init.m file, adding, subtracting, multiplying and dividing equations becomes effortless.

It also works for functions, such as

Log[eqs[[1]]]
(*Log[a] == Log[b]*)
$\endgroup$
1
  • $\begingroup$ Thank you for your answer; I have learned that Equal funtion have not Listable property for some reason. And as the use of TagSetDelayed, Unprotected and especilly, the Thread function in the little packge can handle this case also. $\endgroup$ Commented Jun 19 at 7:27
10
$\begingroup$

Option 1

eqs = {a == b, c == d, e == f};
Equal @@ Total[List @@@ eqs]

enter image description here

Option 2

Equal @@ Last@Accumulate[List @@@ eqs]

Option 3

Thread[Total[eqs],Equal]

Unfortunately, AddSides works on only pair of equations and not set of equations. Hence this works

 AddSides[a == b, c == d]

enter image description here

But not

AddSides[a == b, c == d, e == f]

But you can do

option 4

 Last@FoldList[AddSides,eqs]
$\endgroup$
3
  • 9
    $\begingroup$ for option 4, just Fold[AddSides, eqs] suffices. $\endgroup$ Commented Jun 17 at 15:28
  • 1
    $\begingroup$ @lericr I really was thinking of trying Fold but for some reason, I forgot. Good it works. $\endgroup$ Commented Jun 17 at 15:50
  • $\begingroup$ Thank you for your reply. I've learned that the functions for repetitive or nested computation can also be used cleverly in this context. $\endgroup$ Commented Jun 18 at 12:29
3
$\begingroup$

Try this:

eqs = {a == b, c == d, e == f};

Equal @@ Plus @@@ Transpose[List @@@ eqs]

(*  a + c + e == b + d + f *)

Have fun!

$\endgroup$
2
$\begingroup$
eqs = {a == b, c == d, e == f};

Total[eqs[[All, 1]]] == Total[eqs[[All, 2]]]

 (* a + c + e == b + d + f *)
```
$\endgroup$
1
  • 1
    $\begingroup$ (+1) Also ApplySides[Total, #[[All, 1]] == #[[All, 2]]] &[eqs]? $\endgroup$ Commented Jun 19 at 14:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.