Herbert G. Mayer, PSU CS status 7/29/2013

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
1 CS 162 Introduction to Computer Science Chapter 7 Matrix Manipulation Herbert G. Mayer, PSU Status 9/21/2014.
1 CS 410 Mastery in Programming Chapter 8 Printing Binary Trees Herbert G. Mayer, PSU CS Status 5/22/2013.
1 CS 162 Introduction to Computer Science Chapter 9 Binary Trees Herbert G. Mayer, PSU Status 11/23/2014.
1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
Compiler Construction
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
1 CS 410 Mastery in Programming Chapter 7 Hints for: Symbolic Differentiation Herbert G. Mayer, PSU CS status 7/26/2011.
1 CS 162 Introduction to Computer Science Chapter 5 ASCII to Integer Conversion Herbert G. Mayer, PSU Status 11/9/2014.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
1 CS 163 Data Structures Chapter 9 Building, Printing Binary Trees Herbert G. Mayer, PSU Status 5/21/2015.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
1 CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees” Herbert G. Mayer, PSU CS Status 7/9/2014.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Computer Organization and Design Pointers, Arrays and Strings in C
User-Written Functions
CS 163 Data Structures Chapter 10 Symbolic Differentiation
Data types Data types Basic types
Introduction to Parsing (adapted from CS 164 at Berkeley)
© 2016 Pearson Education, Ltd. All rights reserved.
Revision Lecture
The Selection Structure
Lecture 22 Binary Search Trees Chapter 10 of textbook
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Week 7 Part 2 Kyle Dewey.
Computing with C# and the .NET Framework
C Basics.
Data Structures – Week #3
CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”
Proving Properties of Recursive Functions and Data Structures
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Compiler Construction
ECE 103 Engineering Programming Chapter 18 Iteration
Data Structures – Week #3
Programming Languages and Paradigms
Variables in C Topics Naming Variables Declaring Variables
CS 201 Computer Systems Programming Chapter 7 “Printing Binary Trees”
Compiler Construction
Presentation transcript:

Herbert G. Mayer, PSU CS status 7/29/2013 CS 410 Mastery in Programming Chapter 7 Hints for: Symbolic Differentiation Herbert G. Mayer, PSU CS status 7/29/2013

Syllabus Problem Assessment Rules of First Derivative Data Structures and Types Make Node Copy Tree Print Tree Build Tree Simplify Main References

Problem Assessment Goal of this assignment is to design, code, and execute symbolic differentiation of mathematical formulae or equations These formulae are simple, meaning: No partial differentiation Differentiation only w.r.t a single variable And that variable is pre-defined to be ‘x’ All numeric constants are integer, and of small value, i.e. single decimal digits; but leave “room” for adding up larger integer values Goal was not to focus on scanning, i.e. not on lexical analysis All variable names different from ‘x’ are constant w.r.t. differentiation toward ‘x If two derivatives are needed, then the output of the first can serve as input to the second differentiation So all formulae are simple, yet the mathematical problem remains general and highly interesting

Problem Assessment When applying the rules of differentiation, the result can be a formula with ample redundancies, e.g. +0, *1 etc. In such cases it is advised to simplify the result A careful programmer will actually submit the originally input formula f(x) to the same simplification algorithm Each input formula is terminated by a special symbol, the ‘$’ character For example: Input f(x) = 2*x+x*3+5+6*x$ Normalized input f(x) = ((((2*x)+(x*3))+5)+(6*x)) $ First derivative output f’(x) = (((((0*x)+(2*1))+((1*3)+(x*0)))+0)+((0*x)+(6*1))) Simplified output: 11

Rules of First Derivative Toward x Function references u and v below are functions of x, i.e. u(x) and v(x) the ‘ operator refers to the first derivative f(x) f’(x) f(x) = some variable != x f’(x) = 0 f(x) = integer constant f(x) = x f’(x) = 1 f(x) = u + v f’(x) = u’ + v’ f(x) = u - v f’(x) = u’ - v’ f(x) = u * v f’(x) = u’*v + u*v’ f(x) = u / v f’(x)= (u’*v -u*v’) / v2 f(x) = ln(u) = & u f’(x) = u’ / u f(x) = u ^ v f’(x) = u' * v * u ^ ( v - 1 ) + & u * v' * u ^ v Example: f(x) = x ^ x f’(x) = 1 * x * x^(x–1) + & x * 1 * x^x = x^x + & x * x^x = (& x + 1) * x^x

Data Structures and Types Element of symbolic-differentiation is a node Each mathematical function f(x) is represented internally as a tree, pointed to by “root” Root’s type is pointer to structure of node type Any node is either: A literal with a stored integer value - single decimal digit for now! A variable, which could either be the select variable ‘x’ or some other An operator, with the actual operator itself being stored, like ‘+’ ‘*’ ‘/’ ... Each node has all the following fields Class, specifying enumeration type { literal, variable, or operator } The single character Symbol, e.g. ‘x’ for the special variable The integer literal value LitVal to remember the integer value And node pointers Left and Right to the respective subtrees, if any It will be handy to store special nodes just once; resulting in a DAG

Data Structures and Types // each node has 1 of these class states: // a Literal, an Identifier (for variable), or an Operator. // Parenthesized expressions have been reduced typedef enum { Literal, Identifier, Operator } NodeClass; typedef struct NodeType * NodePtr; // forward announcement // now comes the actual node-type structure, // using the forward declared pointer type: NodePtr typedef struct NodeType { NodeClass Class; // 1 of the 3 classes. char Symbol; // store: Identifier, Operator int LitVal; // if Literal, this is its value NodePtr Left; // subtree NodePtr Right; // subtree } s_node_tp;

Make Node // malloc() new node from heap. All fields are passed in; // return the pointer to the new node to caller NodePtr Make( NodeClass Class, char Symbol, int value, NodePtr Left, NodePtr Right ) { // Make NodePtr Node = (NodePtr)malloc( sizeof( struct NodeType ) ); ASSERT( ... node’s space is really there ... ); Node->Class = Class; Node->Symbol = Symbol; Node->LitVal = value; Node->Left = Left; Node->Right = Right; return Node; } //end Make

Copy Tree // recursively copy tree pointed to by Root. // Pass pointer to the copy to its caller NodePtr Copy( NodePtr Root ) { // Copy if ( Root == NULL ) { return NULL; }else{ return Make( Root->Class, Root->Symbol, Root->LitVal, Copy( Root->Left ), Copy( Root->Right ) ); } //end if } //end Copy

Print Tree void PrintTree( NodePtr Root ) { // PrintTree if ( Root != NULL ) { if ( Root->Class == Operator ) { printf( "(" ); } //end if PrintTree( Root->Left ); if ( Root->Class == Literal ) { printf( "%d", Root->LitVal ); // prints ints > 9 }else{ printf( "%c", Root->Symbol ); PrintTree( Root->Right ); printf( ")" ); } //end PrintTree

Build Tree for Expression() Expression : Term { plus_op Term } plus_op : ‘+’ | ‘-’ Term : Factor { mult_op Factor } mult_op : ‘*’ | ‘/’ Factor : Primary { ‘^’ Primary } Primary : IDENT | LITERAL | ‘(‘ Expression ‘)’ | ‘&’ Primary // for ln()

Build Tree Expression() // parse expression and build tree // using Term() and higher priority functions/ops // all returning pointers to nodes // in Expression() handle ‘+’ and ‘-’ operators NodePtr Expression() { // Expression char Op; // remember ‘+’ or ‘-’ NodePtr Left = Term(); // handle all higher prior. while ( NextChar == ‘+’ || ( NextChar == ‘-’ ) { Op = NextChar; // remember ‘+’ or ‘-’ GetNextChar(); // skip Op // note 0 below for LitVal is just a dummy Left = Make( Operator, Op, 0, Left, Term() ); } //end while return Left; } //end Expression

Build Tree Term() // multiply operators ‘*’ and ‘/’, later add ‘%’ NodePtr Term( ) { // Term char Op; // remember ‘*’ or ‘/’ NodePtr Left = Factor(); while ( NextChar == ‘*' || NextChar == ‘/' ) { Op = NextChar; // remember ‘*’ or ‘/’ GetNextChar(); // skip over Op // note 0 below for LitVal is just a dummy Left = Make( Operator, Op, 0, Left, Factor() ); } //end while return Left; } //end Term

Build Tree Factor() Left-Assoc. // exponentiation operator ‘^’ left-associatively NodePtr Factor() { // Factor NodePtr Left = Primary(); while ( NextChar == ‘^’ ) { GetNextChar(); // skip over ‘^’ Left = Make( Operator, ‘^’, 0, Left, Primary() ); } //end while return Left; } //end Factor // Think about left- versus right-associativity!!! // How would you change the code –-and grammar— // if indeed you make ‘^’ right associative?

Build Tree Factor () Right-Assoc. // exponentiation operator ‘^’ right-associative NodePtr Factor() { // Factor NodePtr Left = Primary(); if ( NextChar == ‘^’ ) { GetNextChar(); // skip over ‘^’ Left = Make( Operator, ‘^’, 0, Left, Factor() ); } //end if return Left; } //end Factor // now multiple ^ operators are handled right-to-left // in line with common precedence of exponentiation

Build Tree NodePtr primary( ) { // primary char Symbol = NextChar; // first_set = { ‘(‘, ‘&’, IDENT, LIT } NodePtr Temp; GetNextChar(); // skip over current Symbol if ( IsDigit( Symbol ) ) { // end node: don’t recurse return Make( Literal, Symbol, (int)(Symbol-'0’), NULL, NULL ); }else if ( IsLetter( Symbol ) ) { // also end node: don’t recurse return Make( Identifier, tolower( Symbol ), 0, NULL, NULL ); }else if ( ‘(‘ == Symbol ) { Temp = Expression(); Must_Be( ‘)’ ); return Temp; }else if ( Symbol == '&' ) { return Make( Operator, '&', 0, NULL, primary() ); }else{ printf( "Illegal character '%c'.\n", Symbol ); return NULL; } //end if // impossible to reach! Hence check Herb!! } //end primary

Derive, 1 NodePtr Derive( NodePtr Root ) { // Derive if ( NULL == Root ) { return NULL; }else{ switch ( Root->Class ) { case Literal: return Make( Literal, '0', 0, NULL, NULL ); case Identifier: if ( ( Root->Symbol == 'x' ) || ( Root->Symbol == 'X' ) ) { return Make( Literal, '1', 1, NULL, NULL ); } //end if case Operator: switch ( Root->Symbol ) { case '+': case '-': return Make( Operator, Root->Symbol, 0, Derive( Root->Left ), Derive( Root->Right ) ); . . .

Derive, 2 . . . case '*': return Make( Operator, '+', 0, Make( Operator, '*', 0, Derive( Root->Left ), Copy( Root->Right ) ), Make( Operator, '*', 0, Copy( Root->Left ), Derive( Root->Right ) ) ); case '/': return Make( Operator, '/', 0, Make( Operator, '-', 0, Derive( Root->Right ) ) ), Make( Operator, '*', 0, Copy( Root->Right ), Copy( Root->Right ) ) ); } //end switch

Derive, 3 Students write code for derive() of ‘^’

Derive, 3 case '^': return Make( Operator, '+', 0, Derive( Root->Left ), Copy( Root->Right ), Make( Operator, '^', 0, Copy( Root->Left ), Make( Operator, '-', 0, Copy( & OneNode ) ) ) ) ), Make( Operator, '&', 0, NULL, Copy( Root->Left ) ), Derive( Root->Right ) ), Copy( Root->Right ) ) ) ); case '&': if ( Root->Left != NULL ) { printf( "ln has only one operand.\n" ); } //end if // now do the work ...

Opportunities for Simplification # Original expression Simplified expression 1 x + 0 x 2 0 + x 3 x - 0 4 x - x 5 x * 0 6 0 * x 7 x * 1 8 1 * x 9 x / x 10 x / 1 11 x ^ 1 12 x ^ 0 13 1 ^ x 14 & 1

Simplify, 1 NodePtr Simplify( NodePtr Root ) { // Simplify int val = 0; // accumulate integer values from + - * etc. if ( !Root ) { return Root; }else{ switch ( Root->Class ) { case Literal: case Identifier: case Operator: Root->Left = Simplify( Root->Left ); Root->Right = Simplify( Root->Right ); switch ( Root->Symbol ) { case '+': if ( IsLit( '0', Root->Left ) ) { return Root->Right; }else if ( IsLit( '0', Root->Right ) ) { return Root->Left; }else if ( BothLit( Root->Left, Root->Right ) ) { val = Root->Left->LitVal + Root->Right->LitVal; return Make( Literal, (char)( val + '0' ), val, NULL, NULL ); return Root; // no other simplifiction for ‘+’ } //end if . . .

Simplify, 2 case '-': if ( IsLit( '0', Root->Right ) ) { return Root->Left; }else if ( BothLit( Root->Left, Root->Right ) ) { val = Root->Left->LitVal - Root->Right->LitVal; return Make( Literal, (char)( val + '0' ), val, NULL, NULL ); }else if ( IsEqual( Root->Left, Root->Right ) ) { return & NullNode; }else{ return Root; } //end if case '*': if ( IsLit( '1', Root->Left ) ) { return Root->Right; }else if ( IsLit( '1', Root->Right ) ) { }else if ( IsLit( '0', Root->Left ) || IsLit( '0', Root->Right ) ) { }//end if case '/': if ( IsLit( '1', Root->Right ) ) { }else if ( IsLit( '0', Root->Left ) ) { return & OneNode; case '^': if ( IsLit( '0', Root->Right ) ) { // x^0 = 1 }else if ( IsLit( '1', Root->Right ) ) { // x^1 = x }else if ( IsLit( '1', Root->Left ) ) { // 1^x = 1 . . .

Two Equal Trees Students write code for: bool IsEqual( NodePtr Left, NodePtr Right )

Two Equal Trees // return true only if both subtrees left and right are equal bool IsEqual( NodePtr Left, NodePtr Right ) { // IsEqual if ( ( !Left ) && ( !Right ) ) { return TRUE; }else if ( NULL == Left ) { // Right is known to be not NULL return FALSE; }else if ( NULL == Right ) { // Left is known to be NOT NULL }else if ( ( Left->Class == Literal ) && ( Right->Class == Literal ) ) { return ( Left->LitVal ) == ( Right->LitVal ); }else if ( ( Left->Class == Identifier ) && ( Right->Class == Identifier )){ return ( Left->Symbol ) == ( Right->Symbol ); }else{ // must be Operator; same? if ( ( Left->Symbol ) == ( Right->Symbol ) ) { // IsEqual yields true, only if both subtrees are equal return ( IsEqual( Left->Left, Right->Left ) && IsEqual( Left->Right, Right->Right ) ) || ( is_associative( Left->Symbol ) && IsEqual( Left->Left, Right->Right ) && IsEqual( Left->Right, Right->Left ) ); } //end if printf( "Impossible to reach in IsEqual.\n" ); } //end IsEqual

main() int main () { // main: Differentiation NodePtr root = NULL; Initialize(); root = Expression(); VERIFY( ( NextChar == '$' ), "$ expected, not found\n" ); SHOW( " original f(x) = ", root ); root = Simplify( root ); SHOW( " Simplified f(x) = ", root ); root = Derive( root ); SHOW( " derived f'(x) = ", root ); SHOW( " reduced f'(x) = ", root ); Or else: print_tree( simplify( derive( simplify( expression( root ))))); return 0; } //end main: Differentiation

References Differentiation rules, implementation code samples: http://www.codeproject.com/KB/recipes/Differentiati on.aspx More code samples in Lisp: http://mitpress.mit.edu/sicp/full- text/sicp/book/node39.html