0
$\begingroup$

I am creating a program which needs to do the following:

I have equations like the following and based on the input I get I should able to calculate the values of $x$, $y$ and $z$

$x = a + b$ $y = x + c$ $z = x + y$

When I get input in the order of $a$, $b$ and $c$ then I can evaluate the expression in the following order

find if $a$ belongs to any expression, if it is find any associated variables available in the input and in this $b$ is available in the input then calculate $x$. Check and see if $x$ is used in any other formula and in this case it is used to calculate $y$, so try to calculate the value of $y$. It requires $c$ use that and calculate $y$. Repeat the same for $y$ and calculate $z$. All works out.

Now if the input comes in different order, based on the formulas that I know before hand, I can sort the input and calculate the formulas properly.

Can any one think of better approach to this problem than the two options I suggested here?

** I can not normalize the equation since each value on left hand side need to be persisted for other purposes **

  • 0
    Do you need to fill in the left hand sides as quickly as possible as the input becomes available (over a network, say), or is all of the input available when you begin processing?2011-10-14

2 Answers 2

1

You can initialize all variables to zero before accepting user input. If a user does not enter the data of the variable the zero remains assigned (or assign zero if the user does not enter the value). By doing this, you need to evaluate the expressions without further checks.

You can forget about the order of evaluation if you express y and z as follows:

$y=a+b+c$

$z=a+b+a+b+c = 2(a+b)+c$

Edit

As per your comment, consider this approach:

You can build a table like the one below to help you identify the sequence of expression evaluation based on whether a given variable was entered or not.

The input variables are represented with columns as a, b, c, etc. Each column has the value 1 if the data is entered and zero otherwise.

The column named key is a unique representation of the unique input combination.

For each unique input combination, the columns exp1, exp2, ...etc. show the sequence where the calculation needs to be followed. So if user enters a, b, c:

1-Calculate the key to be 111

2-Locate the row in the table

3-Carry on the formulas in the sequence 1,2,3 as indicated by exp1, exp2, exp3

Table

  • 0
    This approach requires $2^v$ rows in your table, where v=number of variables.2011-10-18
0

Normalize all equations so they contain all variables on one side of the equal sign with a constant on the other side of the equal sign. Then use Gaussian elimination to zero all elements but the major diagonal (this is called row reduction). The major diagonal will then contain the values of the variables.

  • 0
    each value on left hand side need to be persisted for other purpose so I can not normalize the equation. Ty2011-10-14