//Returns a string representing the division of the mathematical values the operands represent. public static string DivideStrings(string operand1, string operand2) { if (operand1 == "0") { return("0"); } if (operand2 == "1") { return(operand1); } if (DifferentiationTokenizer.IsNumber(operand1) && DifferentiationTokenizer.IsNumber(operand2)) { return((int.Parse(operand1) / int.Parse(operand2)).ToString()); } return(ToPrefixFormat(operand1, operand2, '/')); }
//Returns a string representing the addition of the mathematical values the operands represent. //Also support operatorSymbol = '-'. Then it acts as subtraction on the values. public static string AddStrings(string operand1, string operand2, char operatorSymbol = '+') { if (operand1 == "0") { return((operatorSymbol == '-') ? InvertString(operand2) : operand2); } if (operand2 == "0") { return(operand1); } if (DifferentiationTokenizer.IsNumber(operand1) && DifferentiationTokenizer.IsNumber(operand2)) { return((int.Parse(operand1) + (operatorSymbol == '+' ? 1 : -1) * int.Parse(operand2)).ToString()); } return(ToPrefixFormat(operand1, operand2, operatorSymbol)); }
//Returns a string representing the power function applied to the mathematical values the operands represent. public static string PowerStrings(string operand1, string operand2) { if (operand2 == "0") { return("1"); } if (operand2 == "1") { return(operand1); } if (operand1 == "0") { return("0"); } if (DifferentiationTokenizer.IsNumber(operand1) && DifferentiationTokenizer.IsNumber(operand2)) { return(((int)Math.Round(Math.Pow(int.Parse(operand1), int.Parse(operand2)))).ToString()); } return(ToPrefixFormat(operand1, operand2, '^')); }
/* * Differentiates an operator expression. Uses rules for operands being general diffentiable functions. * +/-: Differentiation linear over +/- * *: Chain rule *./: https://www.wolframalpha.com/input/?i=differentiate+f%28x%29%2Fg%28x%29 * ^: https://www.wolframalpha.com/input/?i=differentiate+f%28x%29%5Eg%28x%29 (using expanded form) */ public static string DifferentiateOperator(string operatorSymbol, string arguments) { string[] operands = DifferentiationTokenizer.SplitArguments(arguments); string firstDerivative = Differentiate(operands[0]); string secondDerivative = Differentiate(operands[1]); switch (operatorSymbol[0]) { case '+': case '-': return(StringMath.AddStrings(firstDerivative, secondDerivative, operatorSymbol[0])); case '*': return(StringMath.AddStrings(StringMath.MultiplyStrings(firstDerivative, operands[1]), StringMath.MultiplyStrings(secondDerivative, operands[0]))); case '/': return(StringMath.DivideStrings( StringMath.AddStrings( StringMath.MultiplyStrings(firstDerivative, operands[1]), StringMath.MultiplyStrings(secondDerivative, operands[0]), '-'), StringMath.PowerStrings(operands[1], "2"))); case '^': return(StringMath.AddStrings( StringMath.MultiplyStrings( StringMath.MultiplyStrings(firstDerivative, operands[1]), StringMath.PowerStrings(operands[0], StringMath.AddStrings(operands[1], "1", '-'))), StringMath.MultiplyStrings( StringMath.MultiplyStrings(secondDerivative, StringMath.LogarithmString(operands[0])), StringMath.PowerStrings(operands[0], operands[1]) ) )); default: throw new FormatException(); } }