Created
July 30, 2019 01:10
-
-
Save alacret/5a9bae94ed76242db34b41b15c989f84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def split_terms(equation): | |
| split_terms = [] | |
| chr_buffer = [] | |
| for chr in equation: | |
| if chr in ["+", "-"]: | |
| if len(chr_buffer) > 0: | |
| split_terms.append(''.join(chr_buffer)) | |
| chr_buffer = [chr] | |
| else: | |
| chr_buffer.append(chr) | |
| if len(chr_buffer) > 0: | |
| split_terms.append(''.join(chr_buffer)) | |
| return split_terms | |
| def _coeficient(comp): | |
| #print(comp) | |
| if comp == '': | |
| return 1 | |
| if comp == '-': | |
| return -1 | |
| if comp == '+': | |
| return 1 | |
| return int(comp) | |
| def differentiate(equation, point): | |
| terms = split_terms(equation) | |
| #print(equation) | |
| #print(terms) | |
| #print(point) | |
| new_terms = [] | |
| for term in terms: | |
| comp = term.split('x') | |
| # constant value | |
| if len(comp) == 1: | |
| continue | |
| coeficient = _coeficient(comp[0]) | |
| caret_pos = comp[1].find('^') | |
| # 1st degree | |
| if caret_pos == -1: | |
| new_terms.append(coeficient) | |
| continue | |
| # 2nd or greater degree | |
| caret_parts = comp[1].split('^') | |
| exponent = int(caret_parts[1]) | |
| new_terms.append(exponent * coeficient * (point ** (exponent - 1))) | |
| #print(new_terms) | |
| return sum(new_terms) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment