Problem Statement
Given a polynomial function of the variables x, y, and z, write a program to compute the mixed partial derivative with respect to a given list of variables.
The function will be expressed as a sum of terms. Each term will be a product of factors. Each factor will be either:
- * an integer coefficient between 2 and 9, inclusive
- * the variable x, y, or z, optionally raised to an integer power between 2 and 9, inclusive
7 x 3*y y*3 z^5 z^4*x^9 3*x*z^5*y
Examples of functions are:
7 x z + 6 y + y + 2*y 3 + y^3*2 + z^4*7*x^3
Given a
To compute a partial derivative:
- * The derivative of a sum of terms is equal to the sum of the derivatives of each term.
- * The derivative of a term with respect to a variable not contained in the term is zero.
- * The derivative of a term with respect to a variable contained in the term can be computed by multiplying the term by the power of that variable, and then decreasing the power of that variable by 1.
Examples of taking the partial derivatives of functions with respect to y:
3 -> 0 x^2 -> 0 y -> 1 4*y -> 4 2*y^5 -> 10*y^4 7*y*z^3 -> 7*z^3 x^2*y^3 + 9*y^4*z^5 -> 3*x^2*y^2 + 36*y^3*z^5
Your program should return the result as a
As with the input
If the result contains no terms, return the
Definition
- Class:
- Partial
- Method:
- derivative
- Parameters:
- String, String
- Returns:
- String
- Method signature:
- String derivative(String expr, String vars)
- (be sure your method is public)
Notes
- Pay careful attention to the instructions regarding formatting of the output string.
- The only spaces in the input and output are immediately before and after all '+' characters.
- If the vars String is empty, return the given function, subject to the output formatting rules specified in the problem.
Constraints
- expr will contain between 1 and 50 characters, inclusive.
- vars will contain between 0 and 5 characters, inclusive.
- vars will contain only the characters 'x', 'y', and 'z', in any order, possibly with multiple occurrences of the same character.
- No term in expr will contain more than one constant factor.
- No term in expr will contain the same variable more than once.
- expr will be formatted as described in the problem statement.
Examples
"7*x"
"x"
Returns: "7"
The derivative of 7x with respect to x is 7.
"x + z + 9*z^2 + y*z^3"
"z"
Returns: "3*y*z^2 + 18*z + 1"
The partial derivative of x with respect to z is 0, z with respect to z is 1, 9z2 with respect to z is 18z, and yz3 is 3yz2.
"x^2*y^2*z^2"
"xy"
Returns: "4*x*y*z^2"
The partial derivative of x2y2z2 with respect to x is 2xy2z2. The partial derivative of 2xy2z2 with respect to y is 4xyz2.
"y*8*z*x^3 + x^5 + y*2*x^4 + 9*x^4*z + x^5*5"
""
Returns: "6*x^5 + 2*x^4*y + 9*x^4*z + 8*x^3*y*z"
Variables and constant factors can appear in any order in the input. The vars string can be empty. Terms must be sorted in the correct order. Terms with equal powers of all variables must be added together.
"9*x^9*y^9*z^9 + x^2*y^2*z^2"
"xyzzy"
Returns: "419904*x^8*y^7*z^7 + 8*x"
"x + y + z"
"yy"
Returns: "0"
"9*x^9 + 9*x^9 + 9*x^9 + 9*x^9 + 9*x^9 + 9*x^9"
""
Returns: "54*x^9"
"9*y^9 + 9*y^9 + 9*y^9 + 9*y^9 + 9*y^9 + 9*y^9"
"yyyyy"
Returns: "816480*y^4"
"9*z^9 + 9*z^9 + 9*z^9 + 9*z^9 + 9*z^9 + 9*z^9"
"y"
Returns: "0"
"2*x^7*z^5 + x^7*y^5*z*7 + y^8*z^8*4 + x^7*3*z^3"
"xzyx"
Returns: "1470*x^5*y^4"
"y^7*x^4*6*z^3 + y*z^3*7*x^6 + x*y^8*z^2*7"
"yzzx"
Returns: "1008*x^3*y^6*z + 112*y^7 + 252*x^5*z"
"y*6*x^2*z^6 + x^6*y^3*z^8 + x^6*z^5*5*y^6 + x^7*7"
"x"
Returns: "30*x^5*y^6*z^5 + 6*x^5*y^3*z^8 + 12*x*y*z^6 + 49*x^6"
"x^8*z^7*y^9 + z^4*y^6*5*x^8 + x^8*z^6*y^7*3"
"xyzxz"
Returns: "21168*x^6*y^8*z^5 + 35280*x^6*y^6*z^4 + 20160*x^6*y^5*z^2"
"2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 2 + 3 + 4 + 5 + 6"
""
Returns: "64"
"x + y + z + x + y + z + x + y + z + x + y + z + x"
""
Returns: "5*x + 4*y + 4*z"
"x + y + z + x + y + z + x + y + z + x + y + z + x"
"x"
Returns: "5"
"x + y + z + x + y + z + x + y + z + x + y + z + x"
"z"
Returns: "4"
"x*z*y + 2*y*x*z + y*3*z*x + z*x*4*y + z*y*x*5"
"yzx"
Returns: "15"
"x + y + z"
""
Returns: "x + y + z"
"x^2*y + x*y^2"
""
Returns: "x^2*y + x*y^2"
"x^2"
"x"
Returns: "2*x"
"x*y*z"
""
Returns: "x*y*z"
"x"
""
Returns: "x"
"x^4 + y^3*z^3"
""
Returns: "y^3*z^3 + x^4"