[llvm-commits] CVS: llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv2/student3.cpp student3.h

Chris Lattner lattner at cs.uiuc.edu
Mon Oct 4 13:01:28 PDT 2004



Changes in directory llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv2:

student3.cpp added (r1.1)
student3.h added (r1.1)
---
Log message:

Initial checkin of all of the source


---
Diffs of the changes:  (+307 -0)

Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv2/student3.cpp
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv2/student3.cpp:1.1
*** /dev/null	Mon Oct  4 15:01:23 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv2/student3.cpp	Mon Oct  4 15:01:13 2004
***************
*** 0 ****
--- 1,192 ----
+ // Written for undergraduate C++ course project at Dept of
+ // Computer Science, Rutgers University.  
+ #include <stream.h>
+ #include <string.h>
+ #include "student3.h"
+ // evaluation of a constant is the value of the constant.
+ double const_expr::eval(double numb)
+ {
+   return(value);
+ }
+ // evaluation of a variable is the input value.
+ double var_expr::eval(double numb)
+ {
+   return(numb);
+ }
+ // evaluation of a sum expression is the sum of the values of its
+ // subexpressions.
+ double sum_expr::eval(double numb)
+ {
+   return(first->eval(numb) + second->eval(numb));
+ }
+ // evaluation of a product expression is the product of the values of 
+ // its subexpressions.
+ double prod_expr::eval(double numb)
+ {
+   return(first->eval(numb) * second->eval(numb));
+ }
+ // evaluation of a quotient expression of the quotient
+ // of the values of its subexpressions.
+ double quotient_expr::eval(double numb)
+ {
+   return(first->eval(numb) / second->eval(numb));
+ }
+ // An expression is printed recursively such that its left subexpression
+ // is printed, then the operator, then its right subexpression.
+ void bin_op_expr::print_me()
+ {
+   cout << "(";
+   first->print_me();
+   cout << " " << op_name << " "; 
+   second->print_me();
+   cout << ")";
+ }
+ // The derivative of a constant is 0.
+ expr *const_expr::deriv(strng var)
+ {
+   return(new const_expr(0));
+ }
+ // The derivative of a variable is 1 if the derivative is in
+ // respect to the variable, and 0 otherwise.
+ expr *var_expr::deriv(strng var)
+ {
+   if (0/*strcmp(var, name)*/)
+     return(new const_expr(0));
+   else 
+     return(new const_expr(1));
+ }
+ // The derivative of a sum expression is equal to the sum of the 
+ // derivatives of the subexpressions.
+ expr *sum_expr::deriv(strng var)
+ {
+   expr *dvfirst, *dvsecond;
+   dvfirst = first->deriv(var);
+   dvsecond = second->deriv(var);
+   
+   if ((dvfirst->isconst()) && (dvfirst->eval(0) == 0))
+     return(dvsecond);
+   else if ((dvsecond->isconst()) && (dvsecond->eval(0) == 0))
+     return(dvfirst);
+   else
+     return(new sum_expr(dvfirst, dvsecond));
+ }
+ // The derivative of a product expression is equal the the sum
+ // of the products of the first subexpression and the derivative of the
+ // second subexpression, and the second subexpression and the 
+ // derivative of the first.
+ expr *prod_expr::deriv(strng var)
+ {
+   expr *dvfirst, *dvsecond;
+   dvfirst = first->deriv(var);
+   dvsecond = second->deriv(var);
+   
+   if ((dvfirst->isconst()) && (dvfirst->eval(0) == 0))
+     {
+       if ((dvsecond->isconst()) && (dvsecond->eval(0) == 0))
+ 	return(dvfirst);
+       else if ((dvsecond->isconst()) && (dvsecond->eval(0) == 1))
+ 	return(first);
+       else
+ 	return(new prod_expr(first, dvsecond));
+     }
+   else if ((dvfirst->isconst()) && (dvfirst->eval(0) ==1))
+     {
+       if ((dvsecond->isconst()) && (dvsecond->eval(0) == 0))
+ 	return(second);
+       else if ((dvsecond->isconst()) && (dvsecond->eval(0) == 1))
+ 	return(new sum_expr(first, second));
+       else
+ 	return(new sum_expr(new prod_expr(first, dvsecond), second));
+     }
+   else
+     {
+       if ((dvsecond->isconst()) && (dvsecond->eval(0) == 0))
+ 	return(new prod_expr(dvfirst, second));
+       else if ((dvsecond->isconst()) && (dvsecond->eval(0) == 1))
+ 	return(new sum_expr(first, new prod_expr(dvfirst, second)));
+       else
+ 	return(new sum_expr(new prod_expr(first, dvsecond),
+ 			    new prod_expr(dvfirst, second)));
+     }
+ }
+ // The derivative of a quotient expression if equal to:
+ // (second subexpresssion * derivative of the first) - (the derivative
+ // of the second * the first subexpression) all over the
+ // second subexpression squared.
+ expr *quotient_expr::deriv(strng var)
+ {
+   expr *dvfirst, *dvsecond;
+   dvfirst = first->deriv(var);
+   dvsecond = second->deriv(var);
+   
+   if ((dvfirst->isconst()) && (dvfirst->eval(0) == 0))
+     {
+       if ((dvsecond->isconst()) && (dvsecond->eval(0) == 0))
+ 	return(dvfirst);
+       else if ((dvsecond->isconst()) && (dvsecond->eval(0) == 1))
+ 	if (first->eval(0) == 1) 
+ 	  return(new quotient_expr(new const_expr(-1),
+ 				   new prod_expr(second, second)));
+ 	else
+ 	  return(new quotient_expr(new prod_expr(new const_expr(-1), first),
+ 				   new prod_expr(second, second)));
+       else
+ 	if (first->eval(0) == 1)
+ 	  return(new quotient_expr(new prod_expr(new const_expr(-1), dvsecond),
+ 				   new prod_expr(second, second)));
+         else
+ 	  return(new quotient_expr(new prod_expr(new const_expr(-1),
+ 				       	 new prod_expr(dvsecond, first)),
+ 				   new prod_expr(second, second)));
+     }
+   else if ((dvfirst->isconst()) && (dvfirst->eval(0) == 1))
+     {
+       if ((dvsecond->isconst()) && (dvsecond->eval(0) == 0))
+ 	return(new quotient_expr(dvfirst, second));
+       else if ((dvsecond->isconst()) && (dvsecond->eval(0) == 1))
+ 	return(new quotient_expr(new sum_expr(second,
+ 			           new prod_expr(new const_expr(-1), first)),
+ 				 new prod_expr(second, second)));
+       else
+ 	return(new quotient_expr(new sum_expr(second,
+             new prod_expr(new const_expr(-1),new prod_expr(dvsecond, first))),
+ 				 new prod_expr(second, second)));
+     }
+   else
+     {
+       if ((dvsecond->isconst()) && (dvsecond->eval(0) == 0))
+ 	return(new quotient_expr(dvfirst, second));
+       else if ((dvsecond->isconst()) && (dvsecond->eval(0) == 1))
+ 	return(new quotient_expr(new sum_expr(new prod_expr(second, dvfirst),
+ 	          		      new prod_expr(new const_expr(-1),first)),
+ 				 new prod_expr(second, second)));
+       else
+ 	return(new quotient_expr(new sum_expr(new prod_expr(second, dvfirst),
+             new prod_expr(new const_expr(-1), new prod_expr(dvsecond, first))),
+ 				 new prod_expr(second, second)));
+     }
+ }
+ 
+ main() {
+  const_expr c(8);
+  var_expr x("x");
+  prod_expr simple(new const_expr(123.45), new var_expr("y"));
+ 
+  cout << "c is ";
+  c.print_me();
+  cout << "\n      and its value at 3 is: " << c.eval(3);
+  cout << "\n      and its derivative with respect to x is: ";
+  c.deriv("x")->print_me();
+  cout << "\nx is ";
+  x.print_me();
+  cout << "\n      and its value at 3 is: " << x.eval(3);
+  cout << "\n      and its derivative with respect to x is: ";
+  x.deriv("x")->print_me();
+  cout << "\nsimple is ";
+  simple.print_me();
+  cout << "\n     and its value at 3 is: " << simple.eval(3);
+  cout << "\n     and its derivative with respect to y is: ";
+  simple.deriv("y")->print_me();
+  cout << "\n     and its derivative with respect to x is: ";
+  simple.deriv("x")->print_me();
+  }


Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv2/student3.h
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv2/student3.h:1.1
*** /dev/null	Mon Oct  4 15:01:28 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/deriv2/student3.h	Mon Oct  4 15:01:13 2004
***************
*** 0 ****
--- 1,115 ----
+ // Written for undergraduate C++ course project at Dept of
+ // Computer Science, Rutgers University.
+ 
+ //extern "C" {
+ // char *strcpy(char *nam, char *str);
+ // }
+ 
+ typedef char * strng;
+ 
+ /* class expr is the top of the class heirarchy for expressions. Any
+    expression object will be an instance of some derived class which has
+    class expr as its base class (or as the base class of its base class...)
+ */
+ class expr {
+  public:
+   expr(){}
+   ~expr(){}
+   virtual void print_me()     = 0;
+ 
+      /* This is the function that an expression object uses to print itself.
+       The version given here should never be run - the print function should
+       be redefined in the derived class */
+ 
+   virtual expr *deriv(char *var)  = 0;
+      /* This is the function that an expression object uses to compute its
+       derivative with respect to "var". The version given here should never
+       be run - deriv() should be redefined in the derived class */
+ 
+   virtual double eval(double at)  = 0;
+    /* This is the function that an expression object uses to compute its
+       value when all variables in it are replaced by the value of "at"
+       */
+ 
+   virtual int isconst() = 0;
+    /* This is the function that can be used to determine if an expression
+       is a constant. */
+ };
+      
+      
+ /* an expression which is a numerical constant */
+ class const_expr : public expr {
+   double value;
+  public:
+   const_expr(double v) {value = v;}
+   ~const_expr(){}
+   void print_me() {cout << " " << value << " ";}
+   double eval(double at);
+   expr *deriv(strng var);
+   int isconst()
+     {
+       return(1); // if the expression is a const this funtion will return true.
+     }
+  };
+ 
+ /* an expression which is a single variable */
+ class var_expr : public expr {
+ private:
+   char name[30];
+  public:
+   var_expr(strng str) {strcpy(name,str);} /* Copy the initialization string
+                        into the name buffer. To read the documentation for
+                        strcpy() and other C string manipulation functions,
+                        type the Unix shell command "man string" */
+   ~var_expr(){}
+   void print_me()  {cout << " " << name << " ";}
+   double eval(double at);
+   expr *deriv(strng var);
+   int isconst()
+   {
+     return(0); // if the expression is a variable this functions returns false.
+   }
+ };
+ 
+ /* expression resulting from applying a binary operator to two expressions */
+ class bin_op_expr : public expr {
+  public:
+   bin_op_expr(expr *e1, expr *e2) {first = e1; second = e2;}
+   ~bin_op_expr(){}
+   void print_me();
+   int isconst()
+   {
+     return(0); // if the expression is a bin_op this function return false.
+   }
+  protected:
+   expr *first;
+   expr *second;
+   char op_name;
+  };
+ 
+ /* an expression which is the sum of two expressions */
+ class sum_expr : public bin_op_expr {
+  public:
+   sum_expr(expr *e1, expr *e2) : bin_op_expr(e1,e2) {op_name = '+';}
+   ~sum_expr(){}
+   double eval(double at);
+   expr *deriv(strng var);
+  };
+ 
+ /* an expression which is the product of two expressions */
+ class prod_expr : public bin_op_expr {
+  public:
+   prod_expr(expr *e1, expr *e2) : bin_op_expr(e1,e2) {op_name = '*';}
+   ~prod_expr(){}
+   double eval(double at);
+   expr *deriv(strng var);
+  };
+ 
+ /* an expression which is the quotient of two expressions */
+ class quotient_expr : public bin_op_expr {
+  public:
+   quotient_expr(expr *e1, expr *e2) : bin_op_expr(e1,e2) {op_name = '/';}
+   ~quotient_expr(){}
+   double eval(double at);
+   expr *deriv(strng var);
+  };






More information about the llvm-commits mailing list