INCOMPLETE. I'm still writing this, but since aeTIos bugged me to post this ASAP, here it is. Will update tonight or tomorrow depending on if I have time or not.So, I was lying in bed one day thinking random thoughts, and suddenly I have this idea in my head: CAS for the calc!
Actually, it wasn't that - it was CAS theory, which led to me thinking that this could be implemented as a calc app or program with Axe.
So - the theory. Basically, the core of this is simply arrays and strings. Numbers of course are in it, but only for non-variable calculations and integer calculations.
With that, let's get started!
Variable multiplicationSay, the user inputs this into the CAS:
AwesomeCAS v1.0
> (2x+5)(x+4)(5x+6)^5
How can we solve that?
First, we need to make our lives easier and apply existing laws/theorem to solving certain parts of the equation.
One thing comes to mind - Pascal's triangle!
A brief explanation of this theorem - Pascal's triangle is a very simple concept. You start with 1, and then 1, 1. You keep adding 1s to the side to make a triangle while adding the above numbers to place below inbetween them. Sound confusing? Here's an example:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
So you (kinda) get it. But why does this have anything to do with CASes?
Take a look at this equation:
(x+y)^3
No one really knows what that is, right?
Multiply it out, and you get:
x^3+3(x^2)y+3x(y^2)+y^3
The coeficients are 1, 3, 3, and 1. Hold on! Did we see that before??
1 2 1
1 3 3 1
We did indeed! Basically, you can the (n-1)th row's numbers for the coefficients!
(And you simply start at the highest degree of the first variable - x^3 - and add Ys to it at the same time decreasing the X power until you get y^3!)
BUT... no one wants to loop through this insanity to get the triangle. What if someone typed in (x+y)^9000?
That's where combinations come in
[more details here]
quick and dirty info: Enter "3 nCr 0" into your calc, going from 0 to 3. See the pattern?
So for this, we need to find such easy problems. Going back to the problem:
> (2x+5)(x+4)(5x+6)^5
We treat this as a STRING. I first verify to see if all the parenthesises are closed or not.
Then, I look for ^[number]. If the number is enclosed in (), I evaluate the () and then use that.
Using the above method, I find that the base equation is:
1x^5 + 5(x^4)y + 10(x^3)(y^2) + 10(x^2)(y^3) + 5x(y^4) + 1y^5
Now substitute x for 5x, and y for 6. You finally get:
3125 x^5+18750 x^4+45000 x^3+54000 x^2+32400 x+7776
But wait! How is this calculated in the first place??
Well, the above base equation is split into an array. Python style:
equArray = [ [1, ["x", 5]], equPlus, [5, ["x",4], ["y",1]], equPlus, [10,["x",3],["y",2]], equPlus, [10,["x",2],["y",3]], equPlus, [5,["x",1],["y",4]], equPlus, [1,["y",5]]]
equEquateX = ["x",[5, ["x", 1]]]
equEquateY = ["y", [6]]
...where equPlus is a special constant/type defined in program to signify a + sign, equArray is the array that contains our base equation, and equEquateX/Y contain the substitution we want to do.
So now let's plug the variables in!
(The below python code does not necessarily work, and is very crudely designed, but it does explain what I'm thinking. In the future, the code will be much more flexible and have more possibilities than this.)
for equPart in equArray: # This loops grabs every item of equArray into equPart
buffInt = 1 # Initialize a "blank" integer for us to use. This will serve as a standing point to create the final coefficient... for this part, anyway. :P
for varEquPart in equArray: # This loop grabs every item of the equPart into varEquPart
if type(varEquPart) == int:
buffInt = buffInt * varEquPart # Multiple the coefficient into the final one
else: # This should be an elemental array
for actVarEquPart in varEquPart: # You see why I don't exactly like turning this idea into code, eh? :P
if type(actVarEquPart) == str:
if actVarEquPart == equEquateX[1]:
# Substitute X in!
for varEquPartInSubstitution in equEquateX
if type(varEquPart) == int:
buffInt = buffInt * varEquPart # Multiple the coefficient into the final one
else: # This should be an elemental array
The above code is incomplete; my brain is hurting from writing this code. Any help possible is appreciated! Optimizing, prettifying, organizing, coding - you name it! Depending on my schedule, I may come back tonight or tomorrow to work on this code snipplet. I quickly outlined my ideas below with no concept code.Floating Point Math - AdditionWIP code posted below - this is in Python. Hopefully it's self explanatory (with assistance from the theory posted below!)
#!/usr/bin/env python
# Experimental floating point implementation
# Written by Albert H
# Variables
DEBUG = 1
n1 = raw_input("> Enter 1st floating point number: ")
n2 = raw_input("> Enter 2nd floating point number: ")
op = raw_input("> Enter operation (+, -, *, or /) to perform: ")
# Now for the REAL guts of this program!
# We use absolutely NO floating point operations here at all. NONE.
# If you enable the DEBUG var above, you get to see every part of the
# process. No single number used is a floating point number at all.
if op == '+':
# Easiest one of the bunch. Align the decimal point!
pointpos1 = n1.index(".")
pointpos2 = n2.index(".")
if pointpos1 < pointpos2:
# If pointpos1 has a lower index than pointpos2... that means
# we have n1 being 3.14 and n2 being 12.4.
for n in range(0, pointpos2-pointpos1):
n1 = " " + str(n1)
if DEBUG == 1:
print ">> DEBUG: n1 string is: "+str(n1)
print ">> DEBUG: n2 string is: "+str(n2)
# Number crunching time!
lenOfNum1 = len(n1)
lenOfNum2 = len(n2)
# TODO: Fix bug with entering same number/dec place == crash
# TODO: Fix 0.9+12.9 =12.18 due to lack of carryover correction
result = ""
tempResult = ""
for nIndex in range(0, max(lenOfNum1, lenOfNum2)):
if nIndex <= (lenOfNum1 - 1):
if nIndex <= (lenOfNum2 - 1):
if n1[nIndex] != ' ':
if n2[nIndex] != ' ':
# TODO: add checking for misalign of decimal points
if n1[nIndex] != '.':
if DEBUG == 1:
print ">> DEBUG: Both n1 and n2 digits are present! Adding integers: "+n1[nIndex]+" + "+n2[nIndex]
tempResult = int(n1[nIndex]) + int(n2[nIndex])
result = result + str(tempResult)
else:
if DEBUG == 1:
print ">> DEBUG: Decimal point detected! Appending decimal point to result."
result = result + "."
else:
if DEBUG == 1:
print ">> DEBUG: n2[nIndex] is a space, so adding n1's digit to result. Digit: "+str(n1[nIndex])
result = result + str(n1[nIndex])
else:
if n2[nIndex] != ' ':
if DEBUG == 1:
print ">> DEBUG: n1[nIndex] is a space, so adding n2's digit to result. Digit: "+str(n2[nIndex])
result = result + str(n2[nIndex])
else:
print "ERROR: Something went wrong in preprocessing!"
print "Guru meditation: n1[nIndex] and n2[nIndex] are both spaces! n1='"+n1+"', n2='"+n2+"', nIndex="+str(nIndex)
exit(1)
else:
if DEBUG == 1:
print ">> DEBUG: n2[nIndex] does not exist, so adding n1's digit to result. Digit: "+str(n1[nIndex])
result = result + str(n1[nIndex])
else:
if nIndex <= (lenOfNum2 - 1):
print ">> DEBUG: n1[nIndex] does not exist, so adding n2's digit to result. Digit: "+str(n2[nIndex])
result = result + str(n2[nIndex])
else:
print "ERROR: Something went wrong in preprocessing!"
print "Guru meditation: nIndex is out of range! n1='"+n1+"', n2='"+n2+"', nIndex="+str(nIndex)
print "> Result: "+result
Floating Point Math - MultiplicationThis method can also be applied to other operations as well.Z80 calcs don't have floating point math. It's all software. How can we do it without using TI's stuff?
Probably not the best way to do it, but...
HANDLE IT AS A STRING!
Take for instance: 0.51 X 0.003
Quickly outlining the method:
1) Find number of decimal places, figure it which one is longer or if they are both the same
2) Perform hand-done multiplication on computer... aka:
a) Cut string into numbers
b) Multiply based on place
c) Shift around as needed, like what you usually do when you switch places
d) Add things, AGAIN
e) Organize numbers into string
f) Figure out where to put decimal point, and put it there
g) Simplification as needed ("0.50000 => 0.5")
h) Done!
Variable ManipulationBasically, aiming for the famous TI-89 (or TI-Nspire CAS) command: solve(2x+6+x^1029=5, x)
Something like the first idea, but with a lot more complexity. Array element shifting to the extreme!
Again, treat variables like a string. Keep track of where they are in the equation, and perform operations to the equation array to isolate them and solve it.