Question:
Derivation in java data Structure?
Confused
2011-01-15 13:48:39 UTC
Source file: Derivation.java
Input file: file name obtained from the command line
Output: file name obtained from the command line
The derivative of an expression that involves the variable x can be defined by a few rules:
• The derivative of a constant is 0.
• The derivative of x is 1.
• If A is an expression, let dA be the derivative of A. Then the derivative of –A is –dA.
• If A and B are expressions, let dA and dB be the derivatives of A and B respectively.
Then:
o The derivative of A+B is dA+dB.
o The derivative of A - B is dA - dB.
o The derivative of A * B is A * dB + B * dA.
o The derivative of A/B is (B*dA - A*dB) / (B*B).
For this problem, you should write a program that can compute the derivative of an expression.
All the information that you need is in the rules given above. Note that the resulting derivatives
can be more complicated than need be. For example, the derivative of 3x+1 will be computed
as (3 *1+0*x) +0. This is correct, even though it is kind of ugly, but after simplification it should
be only 3.
Input Format
The input file starts with a number M describing how many cases are to be examined.
Each of the next M lines then gives an expression with only one variable, x, to be derived. Note
that ^ means power. Also note that there is a space between characters except before or after
the power sign (i.e. ^). Assume that the input has no parentheses.
Output Format
The output should be M lines of simplified derivatives of the given cases
Sample Input: derive.in
5
3 x + 1
1 / x
3 x^3 / x
5 x^2 – 6 x + 7
x / 3
Sample Output: derive.out
3
-1 / x^2
6 x
10 x – 6
1 / 3
Three answers:
Light Cloud
2011-01-15 16:46:40 UTC
The cool thing about these kinds of definition is that they can almost be one-to-one mapped into code. These kinds of definitions are recursive, and so the code is most easily written recursively as well.



Since the mathematical function is:

The derivative of a constant is 0.

• The derivative of x is 1.

• If A is an expression, let dA be the derivative of A. Then the derivative of –A is –dA.

• If A and B are expressions, let dA and dB be the derivatives of A and B respectively.

Then:

o The derivative of A+B is dA+dB.

o The derivative of A - B is dA - dB.

o The derivative of A * B is A * dB + B * dA.

o The derivative of A/B is (B*dA - A*dB) / (B*B).



It makes sense that the corresponding code should be:

public static String getDerivative(String input) {

if ()

return "0";

if (input.equals("x"))

return "1";

if ()

return "-" + getDerivative( A );

if ()

return getDerivative(A) + "+" + getDerivative(B);

and so forth...

}
?
2016-10-28 12:54:10 UTC
Any primary series will artwork for storing the concepts. opt for an information structure reckoning on the way you ought to get proper of entry to the concepts. in case you purely want to connect 2 unrelated issues mutually, per chance to go back as a unmarried function effect, then an merchandise[] array is an straightforward option. go back new merchandise[] {rainfallData, ageAndFootballList}; If it truly is in a getRainfallAndFootball() technique, then the caller might want to apply: merchandise[] effect = getRainfallAndFootballList(); myRainfallData = effect[0]; myFootballList = effect[a million]; it truly is demanding to inform precisely what's necessary, because you do not say what you intend to do with the concepts.
John Pulsating A Lot
2011-01-15 13:52:36 UTC
so what exactly is your question?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...