I am attempting to create a basic derivative calculator and have been able to create the code to change the values in an equation to their derivative. But at the end, all I have are the individual derived values of each term and can not properly concatenate them because I do not know how to fit the operators into the appropriate spots since I am unable to get the program to identify whether a character in the string is a " " or "-" and where it belongs. Ex:
string = "9x^2 4x - 8"
Once this is passed through the main block of code, the results are:
18x 4
This is the correct derivation of the individual terms, but the operator is missing here, and I can not manually concatenate a " " since the user could have a " " or a "-" in the same spot. I have tried a variety of iterations through for-loops in combination with .split() .remove() .replace(). How could I preserve the location within the string and operand type? If necessary, the code block for calculating the derivative is below.
def derivative():
# Clean up input into usable data
string1 = string.replace("^", "")
string2 = string1.replace(" ", "")
string3 = string2.replace("-", "")
# Turn string in list, clean up list
eq = string3.split(" ")
while("" in eq):
eq.remove("")
# For loop to go through each item in list and turn it into its derivative
dydx = ""
for item in eq:
# Condition only allows a term with both an int, variable and a power
if item[-1].isdigit() and item[-1] != item:
# Derivation power rule
num = int(item[0:-2]) * int(item[-1])
var = item[-2] "^" str(int(item[-1]) - 1)
term = str(num) var
# If the power is 1, it is not necessary to show
if term[-1] == "1":
term = term[:-2]
dydx = (str(term) " ")
else:
dydx = (str(term) " ")
# Condition only allows a term with an int and variable
if item[-1].isalpha():
# Power rule, 1-1 = 0, anything to the 0 power is 1
if len(item) == 1:
term = item.replace(item[-1], "1")
dydx = (str(term) " ")
# 1 multiplied by the number next to it equals the number next to it
else:
term = item.replace(item[-1], "")
dydx = (str(term) " ")
# Only allows numbers through
if item[-1] == item and item.isdigit():
#The derivative of a constant (number) is zero
del item
print(dydx)
CodePudding user response:
I've figured out how to store the operands and use them later. I create as many empty variables as I need for plus and minus, ordering them 1,2,3 etc for after term 1, term 2, and then term 3. In a for-loop, I then enumerate the string so I can get the operand type relative to the term it is in front of. The conditions confirm which iteration the loop is on by using terms from the split string to identify the area around each operand in the equation, which the for loop then iterates through each character in that specific section of the string until it finds either a " " or a "-". The result is then incremented to it's respective empty string.
string = input("Enter equation: ")
countability = string.split()
plus1 = ""
minus1 = ""
plus2 = ""
minus2 = ""
plus3 = ""
minus3 = ""
for count, char in enumerate(string):
if count < (int(len(countability[0])) 3):
if char == " ":
plus1 = " "
elif char == "-":
minus1 = "-"
else:
pass
if count > (int(len(countability[0])) 3) and count < (int(len(countability[0])) (int(len(countability[2])) 6)):
if char == " ":
plus2 = " "
elif char == "-":
minus2 = "-"
else:
pass
if count > (int(len(countability[0])) int(len(countability[2])) 6) and count < (int(len(countability[0])) int(len(countability[2])) (int(len(countability[4]))) 9):
if char == " ":
plus3 = " "
elif char == "-":
minus3 = "-"
else:
pass