usbong / UsbongKit

A framework for reading and displaying contents of Usbong Trees
www.usbong.ph
Apache License 2.0
2 stars 1 forks source link

Entering mathematical expressions as textField, etc component answer #28

Open masarapmabuhay opened 7 years ago

masarapmabuhay commented 7 years ago

Salut! I'm auditing MITx: CTL.SC1x Supply Chain Fundamentals right now, and I found an interesting feature that they're using in their edx platform.

A person can enter mathematical expressions as an answer.

UsbongKit can handle this too, in the sense that it checks the String if it is exactly equal to any of the possible correct answer/s given in the .utree beforehand.

I think it would be good if the app can check the mathematical expression against all the possible answers dynamically, instead of having the .utree developer preload all the possible answers in the .XML of the .utree.

So for example, z-x+106 and -x+z+106 are both acceptable answers, even if in the .utree, only one answer was given.

Qu'est-ce que tu penses?

Please never hesitate to share your thought on this.

Merci beaucoup toujours!

chrisamanse commented 7 years ago

This is interesting. The challenge is finding and implementing the appropriate algorithms to parse the expressions and how to evaluate their equality.

masarapmabuhay commented 7 years ago

Indeed! It may be possible to do a simple mathematical negation. What I mean is z-x+106 - (-x+z+106), which equates to 0.

Still, for something like these: (y-10)*(1/(x+49)) (y-10)/(x+49)

The app should be able to do PEMDAS before doing the mathematical negation.

UsbongKit (Android) can already store variables. The next step that was on my mind was to do simple addition, akin to the ALU of a CPU. From there, we can do subtraction, multiplication, division, and so on.

But I may be getting ahead of myself.

Pendant ce temps, merci beaucoup, vraiment!

chrisamanse commented 7 years ago

I was able to do this in C++ during my algorithms class: https://github.com/cia-backup/EquationParser

The code was not organized and should have used data structures. The best data structure I can think of right now is the use of binary trees. We parse the equation and place them in a tree:

operations

Taken from: https://github.com/raywenderlich/swift-algorithm-club/tree/master/Binary%20Tree

The challenge now is too evaluate if these trees are equivalent, and I don't think by simply evaluating these equations (by replacing x, and z with real numbers) would suffice since we only proved that those equations are equal for that certain values. For example, x^2, and x are equal for x = {0, 1}.

chrisamanse commented 7 years ago

Specifically, they are called Binary expression trees[1].

[1] https://en.wikipedia.org/wiki/Binary_expression_tree

masarapmabuhay commented 7 years ago

Salut! Oui, there are various ways to implement PEMDAS.

And indeed, putting real numbers into the equations will not suffice in this case. It will work for a subset of numbers, if, for example, you only want to see the general direction or trend of the graph of the equations given a specific scope or range. And it may actually work in some cases, but will not be exhaustive. To do so will consume an unlimited amount of resources, because the total of all possible numbers is infinite.

I think that doing equation_1 - equation_2 via PEMDAS is still the way to go. If the subtraction equates to 0, then the two equations are equal.

Please never hesitate to disagree.

Merci beaucoup!