1
$\begingroup$

I usually work a lot with equations and their linear combinations. So I thought that I may develop a function which take a given set of equations and compute a desired linear combinations of those equations. But I don't know what is the best way to do this. Here is my little effort

f[{c1_, Eq1_}, {c2_, Eq2_}] := c1*Eq1[[1]] + c2*Eq2[[1]] == c1*Eq1[[2]] + c2*Eq2[[2]]
Eq1 = a == b
Eq2 = c == d
f[{1, Eq1}, {1, Eq2}]

and the output is

a + c == b + d

here are my questions

1- Is this approach good?

2- How can I make the number of arguments of my function arbitrary?

3- I also noted that my function does not work when I give an identical equation like 1=1 to it. Is there any solutions for this?

$\endgroup$
2
  • $\begingroup$ What do you mean by making the arguments arbitrary?. Note that 1==1 simply evaluates to true. $\endgroup$
    – Feyre
    Dec 8, 2016 at 16:43
  • $\begingroup$ @Feyre: I mean to give any number of equations as you want! I knew that 1==1 evaluate to true, but I wanted to able to add 1 to both sides of equation by this function too. :) $\endgroup$ Dec 8, 2016 at 17:09

1 Answer 1

4
$\begingroup$

I would do it like this, this way you can put in any amount of equations:

f[c_, eq_] := Plus @@ (c.eq[[All, 1]]) == Plus @@ (c.eq[[All, 2]])

eq1 = a == b;
eq2 = c == b;
eq3 = d == a/b;
f[{2, 1, 3}, {eq1, eq2, eq3}]

2 a + 2 c + 3 d == (3 a)/b + 3 b

You can't store a an equation which automatically evaluates such as 1==2 or 1==1, as it will just store the False or True. To do this, you'd have to store the relevant equations as a list, which can be mixed with actual equations.

eq4 = {1, 1};
f[{2, 1, 3, 6}, {eq1, eq2, eq3, eq4}]

6 + 2 a + c + 3 d == 6 + (3 a)/b + 3 b

$\endgroup$
1
  • $\begingroup$ (+1) Thanks for the nice answer. :) $\endgroup$ Dec 8, 2016 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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