CS 410 / 510
HM
MASTERY IN PROGRAMMING
HW 4
PSU
(7/19/2013)
Due Date:
Subject:
Include:
Wednesday 7/31/2013
Symbolic Differentiation, plus design decision
Suitable, concise commenting counts > 10% of total points
General Homework Rules: Implement your homework in a high-level language for which a compiler is available to all CS students at PSU. Write well-structured, readable, source programs.
Use helpful comments and apply them liberally, but express only ideas that are not immediately visible from the source code. E.g. express solution ideas, list sources of algorithms, note caveats, explain goals. Only individual work counts. Submit via email by the due date before the start of class, using subject line: CS 410 HW4 –the 4 alluding to homework 4, but changing over time to different numbers; CS 510 students use subject line: CS 510 HW4 . Use capital letters in the subject line, and blanks around “410” or “510” . Include in your email all source files, output files, and if applicable input files; preferred for our generally simple assignments is a single source file, plus a single output file. For full credit, send only ASCII files, no executable files, no object code, no a.out file, not processed files; i.e. do not Windows ZIP files, do not use Unix tar files, no shar utilities, nor makefiles; only ASCII files.
Summary: Implement program diff that conducts differentiation of a function f(x) with respect to variable ‘x’. Input to diff is a restricted arithmetic formula f(x), such a 2*x+x*(8-x*5)$, terminated by ‘$’ . The first output is input formula f(x), but with parenthesis explicitly showing precedence. The second output is the first derivative f’(x) of the input formula. The third output is a simplified first derivative f’(x)
Detail: Design, implement, document, and test a program that conducts differentiation of a function f(x) with respect to variable ‘x’ . The common arithmetic operators such as + for addition and - / * ^ & are allowed, also single-digit decimal number and other single-character identifiers. Only variable ‘x’ in complex arithmetic formulae is special; ‘x’ is the variable toward which derivation will happen. Parentheses () are allowed to overwrite implicit precedences. The precedence of operators in absence of parenthesis is like in regular arithmetic, with & for the natural log having highest precedence, ^ for exponentiation binding more strongly than * or / , and * and / binding more strongly than + and .
Input to diff is a restricted arithmetic formula f(x) , expressed as a sequence of ASCII characters, such a 2*x+x*(8-x*5)$.
The input is terminated by a ‘$’ character. The first output is input formula f(x) itself, but with parenthesis explicitly showing precedence. The second output is a simplified form if possible; the third output is the derivative f’(x) of the input formula. The fourth output is a simplified first derivative f’(x), with all redundant operations eliminated.
First Derivatives: The first derivative of any variable or any literal yields 0, except for the special variable ‘x’. See the list below for first derivatives of composite arithmetic formulae that are functions of u and v, both of which can be functions of x. These derivation rules are applied recursively to the whole function f(x) = f1( u, v ) , with u = f2(x) , and v = f3(x) :
Tabel 1: First Derivatives: derive( x ) = 1 derive( a ) = 0 derive( number ) = 0
-- any single character variable ‘a’ except ‘x’ or ‘X’
-- with ‘number” being a single-digit decimal literal derive( u + v ) = u' + v' -- where u = f2(x) and v = f3(x) derive( u - v ) = u' - v' derive( u * v ) = u' * v + u * v' derive( u / v ) = ( u' * v - u * v' ) / ( v * v)
1
HW 4
CS 410 / 510
HM
MASTERY IN PROGRAMMING
HW 4
PSU derive( u ^ v ) = u' * v * u ^ ( v - 1 ) + & u * v' * u ^ v derive( & u ) = u' / u -- the & operator stands for ln, the natural log
Table 2: Simplifications:
#
1
2
3
Possible First derivative
0 + u(x) u(x) + 0 u(x) – 0
Simplified u(x) u(x) u(x)
4
5
6 u(x) – u(x)
0 * u(x) u(x) * 0
0
0
0
7
8
9
10
1 * u(x) u(x) * 1 u(x) / u(x) u(x) / 1 u(x) u(x)
1 u(x)
11
12
13 u(x) ^ 1 u(x) ^ 0
& 1 u(x)
1
0
Examples: the samples below repeat the input formula in normalized form, and show two outputs each per input formula. The second output is the reduced (AKA simplified) derivative:
Enter f(x), ended with $: x$ original f(x) = x reduced f(x) = x derived f'(x) = 1 reduced f'(x) = 1
Enter f(x), ended with $ : 2*x+y$ original f(x) = ((2*x)+y) reduced f(x) = ((2*x)+y) derived f'(x) = (((0*x)+(2*1))+0) reduced f'(x) = 2
Enter f(x), ended with $: x*(2*x)+5*b-4*x$ original f(x) = (((x*(2*x))+(5*b))-(4*x)) reduced f(x) = (((x*(2*x))+(5*b))-(4*x)) derived f'(x) = ((((1*(2*x))+(x*((0*x)+(2*1))))+((0*b)+(5*0)))-((0*x)+(4*1))) reduced f'(x) = (((2*x)+(x*2))-4)
Enter f(x), ended with $ :(3+2*x)/(x*3+9*x)-x*(7+b-7*x)$ original f(x) = (((3+(2*x))/((x*3)+(9*x)))-(x*((7+b)-(7*x)))) reduced f(x) = (((3+(2*x))/((x*3)+(9*x)))-(x*((7+b)-(7*x)))) derived f'(x) = (((((0+((0*x)+(2*1)))*((x*3)+(9*x)))-
((3+(2*x))*(((1*3)+(x*0))+((0*x)+(9*1)))))/(((x*3)+(9*x))*((x*3)+(9*x))))-
((1*((7+b)-(7*x)))+(x*((0+0)-((0*x)+(7*1)))))) reduced f'(x) = ((((2*((x*3)+(9*x)))-
((3+(2*x))*12))/(((x*3)+(9*x))*((x*3)+(9*x))))-(((7+b)-(7*x))+(x*-7)))
Enter f(x), ended with $: 2*x+x*3+5+6*x$ original f(x) = ((((2*x)+(x*3))+5)+(6*x)) reduced ... derived f'(x) = (((((0*x)+(2*1))+((1*3)+(x*0)))+0)+((0*x)+(6*1))) reduced f'(x) = 11
Discussion: Your primary data structure is root, a pointer to a tree of expression nodes. Some of the nodes denote integer literals of value 0 or 1. Would it be correct to have all subtrees that
2
HW 4
CS 410 / 510
HM
MASTERY IN PROGRAMMING
HW 4
PSU point to one-nodes or zero-nodes point to the same physical node? In that case the structure would no longer be a tree proper but a graph. Write a brief discussion of pros- and cons. This is worth 25 of the total 150 points.
Hints: One of the modules you will design is the function simplify(), more formally:
Node_ptr_tp simplify( Node_ptr_tp root ) which will eliminate redundant expressions found in the tree pointed to by root, and return a pointer to a possibly simpler tree, simpler than the originals one. It is a good idea to use simplify() already on the original tree, in addition to the derived tree.
Special extra credit: Including a simple constant expression evaluation (cee) module will yield up to 20 points of extra credit. Such a cee module computes, i.e. changes, simple constant expressions of the kind 2*(3+4) into 2*7 and then into 14 . But this step is not required for full points.
Testing: It is important that for each of the arithmetic operators for which you compute the derivate, you include a simple test plus at least one non-trivial test that uses a combination with other operators. Moreover, all sample input strings shown above in this homework specification needs to be tested.
Table 3: Distribution of points
Number
1
Credit Possible
Points
50
2
3
Correctly parsing all inputs, generating correct output of first derivative
Provided adequate number of correct test cases, all cases in assignment and more
Correctly simplify output of first derivative, by eliminating idem-potent cases
10
20
4
5
Provided sufficient number of error cases and catch each error with suitable message
Readable, sound SW engineering methods used for function design
10
10
7
8
Discussion
Proper commenting, only and all ideas that are not evident from reading the source
Total
10
15
125
What to turn in and how: Submit homework via email to the grader dkitt2@pdx.edu
; copy herb@cs.pdx.edu
. Keep the original email, in case there is a need to re-submit later. All tests listed above are to be handed in, for each showing the input with parentheses to make precedences explicit, a simplified version (reduced tree) of the input, the first derivative, and the simplified derivative (reduced tree) with redundant expressions eliminated.
3
HW 4