Solving a series of equations

93 Views Asked by At

I'm writing a piece of code to translate some data, and I keep banging my head against the wall with a one part of the transformation. I'm not as good at math as I ought to be :)

Say we have a collection of amounts of different types (type is denoted by capital letter):

$A = 100$

$B = 200$

And a collection of conversion from one type to another with a conversion factor:

$C_{A->B} = 0.2$

$C_{B->A} = 0.1$

The amounts are the results of a conversion with the above collection of conversions, and now I want to get back the original values.

I can do that by solving these two equations for A_before and B_before:

$$ A_{before} = \frac{A-(-B_{before}*C_{B->A})}{1-C_{A->B}} $$ $$ B_{before} = \frac{B-(A_{before}*C_{A->B})}{1-C_{B->A}} $$

Can I make an equation that gives me the original values (Abefore, Bbefore, ... Zbefore) with any number of amounts (A, B, ... Z) and conversions?

I have tried manually with three and four amounts, cyclic and non-cyclic conversions and solving for A_before and it works fine it seems, but I'm trying to implement this in a piece of software and I don't know how many amounts and conversions there will be.

Can anyone help point me in the right direction?

1

There are 1 best solutions below

7
On BEST ANSWER

Let

  • $x=A_\text{before};y=B_\text{before}$
  • $x'=A; y'=B$
  • $\lambda=C_{A\rightarrow B}; \mu=C_{B\rightarrow A}$

From equations given: $$\begin{align} \left. \begin{array}{1} x&=\dfrac{x'-(-\mu y)}{1-\lambda}\\ y&=\dfrac{y'-\lambda x}{1-\mu} \end{array} \right\}\\\\ \left. \begin{array}{1} (1-\lambda)x-\mu y&=x'\\ (1-\mu) y+\lambda x&=y' \end{array} \right\}\\\\ \left(\begin{matrix}1-\lambda&-\mu\\\lambda&1-\mu\end{matrix}\right) \left(\begin{matrix}x\\y\end{matrix}\right) &=\left(\begin{matrix}x'\\y'\end{matrix}\right)\\\\ \left(\begin{matrix}x\\y\end{matrix}\right) &=\left(\begin{matrix}1-\lambda&-\mu\\\lambda&1-\mu\end{matrix}\right)^{-1}\left(\begin{matrix}x'\\y'\end{matrix}\right)\\ &=\frac 1{1-\lambda-\mu+2\lambda\mu} \left(\begin{matrix}1-\mu&\mu\\-\lambda&1-\lambda\end{matrix}\right) \left(\begin{matrix}x'\\y'\end{matrix}\right)\\ \end{align}$$