I am currently trying to write a program that calculates the derivative of the equation inputed as a string. I want to be able to split the problems into small pieces so that I can solve the larger equation recursively if necessary. Here is an example:

local function solve(stringInput, x[, value, n]) -- x stands for the letter variable and value is if you want the function to solve for a certain number in that derivative and n is how many times you want it to repeat the process for the first, second, and so on derivative
	local lastSymbolIndex = 1
	for v in string.gmatch(stringInput,) -- I don't know how to look for just + or - and not only that, but also how to save the index of the last one found into the variable above

This is so that I can split the problem into bits which I can solve recursively. For example a basic string input may look like 3x^2+5x^3-4x^(11) Note: my program assumes that the exponent is one in length unless parentheses are included. The real issue comes when there are chain rule or product/quotient rule problems that have the symbols -/+ in them. How do I count those out of the search? My only option is to create an algorithm for this kind of thing. The algorithm may encounter something like sqrt(2-sec(5x^2+1)). Now, I may be able to see the +/- symbols in the parentheses, but the program cannot and will count those in its search. Would I have to search through the string before hand for special rules and removing them from the string? Any help would be appreciated. Thanks! Note: I don't need help with the math. The string manipulation is my only issue. Edit: For example, sqrt(2-sec(5x^2+1))+cos(x+1) would be turned into two variables sqrt(2-sec(5x^2+1)) and cos(x+1) and an array would hold the symbols in the order that they came in. For instancelocal symbols = {}; table.insert(symbols, symbol) where symbol is the symbol that was between them.

5 comments

0

kingdom5 suggested RPN(reverse polish notation) and I am looking into that now.

0

Not very good with algebra though and not for string manipulation. Back to square one. Please help if you have any suggestions.

0

Someone else recommends stacks.

0

looking into that now because they said doing this in regex would be hard

0

Wait, you're attempting to parse a string into multiple parts if it's either + or - ?