Samuel Adeshina - 2015-04-24 21:12:42 - In reply to message 1 from Till Wehowski
Hi Tim, Thanks a lot for your question(s).
The logic behind this class' ability to solve equations is:
treating whatever equation that was supplied as a string, parsing it to verify if its actually a real equation and tries to identify the variable(s) and exponents used. After identifying the variable to solve for, it separates the parts of the equation with variables into an array and the ones without variables into another array called $with_variables and $without_variables respectively.
for instance, if you have the following equation:
3a + 2 = 1;
After parsing it you get two arrays looking like these:
[0=>'+3'] and [0=>'+2', 1=>'-1'].
It then adds up all the elements in each array together to get something like this:
$with_variables = (+2) + (-1) = +1;
$without_variables = +3
And then the final solution will be derived by dividing the $without_variables variable by the $with_variables variable as in
$final_solution = $without_variables / $with_variables
which gives us something like +3 / +1 = 3 in our previous example.
In order to return the final_solution with the appropriate sign ("+" or "-"), i had to extract the first index of the $final_solution variable then changing it to its opposite, using the if_else construct which you pointed out.
To answer your second question, the variable $final_solution needs to be treated as a string instead of as an integer (or any other number data type) in other to get the correct answer. For instance, using the example given above:
After initializing the $final_solution variable, we get +3 but the correct answer is suppose to be -3. That is why, the first index ('+' in this case) had to be extracted using the substr() function, then changing it to '-'.
I hope I was able to answer your questions or at least explain what was actually going on. You can always ask for further clarification.
Once again, thanks a lot for the question and the compliments.
With regards, Samuel