[CppAD] Own Data Type

Dominik Skanda dominik.skanda at biologie.uni-freiburg.de
Mon Feb 28 17:39:07 EST 2011


Hello thanks for help,

I don't get a compile or runtime error. The thing is that
the calculations are not performed correctly. For example
in the instance below instead of using -234.6665675 the
code only uses -234. I have not really a glue which methods
I have to implement in the MyType class, so that CppAD uses
it correctly and I can use the new type like the type
double for instance. The second thing I wonder, how the
program recognizes if I use a expression like

Y[0]=X[0]-234.123456789123456789123456789123456789;

does the compiler first convert it to a double. Does it
shrink the significant digits? I only have adapted the
example found in the documentation and modified it till it
compiles, but somehow I miss a important part!

regards

Dominik



On Mon, 28 Feb 2011 23:00:33 +0100
 Stefan Vigerske <stefan at math.hu-berlin.de> wrote:
> Hi,
> 
> CppAD work's for me with my own base type.
> What kind of error you get?
> 
> Stefan
> 
> Am 28.02.2011 18:23, schrieb Dominik Skanda:
> > Hello all,
> >
> > I try to implement my own numeric base type for use
> with cppAD, i.e.
> > with QD Library a numeric type with 63 digits
> > ( http://crd.lbl.gov/~dhbailey/mpdist/ ). For that I
> try to extend the
> > example (
> http://www.coin-or.org/CppAD/Doc/numerictype.cpp.xml ).
> > But somehow it's not working correctly. Has someone
> experience with a
> > similar case?
> >
> > Specially I want that following works:
> >
> > vector<  CppAD::AD<MyType>  >  X(1);
> > vector<  CppAD::AD<MyType>  >  Y(1);
> >
> >
> > CppAD::ADFun<MyType>  F;
> > 		
> > X[0]=6.2785676;
> >
> > CppAD::Independent(X);
> > 		
> >
> > Y[0]=X[0]-234.6665675;
> > 		
> > F.Dependent(X, Y);
> >
> >
> > I have no glue what happens with the numbers 6.2785676
> and 234.6665675
> > for instance.
> >
> > The definition of MyType looks by now:
> >
> >
> > 	class MyType {
> > 	private:
> > 		qd_real d;
> > 	public:
> > 		// constructor from void
> > 		MyType(void) : d(0.)
> > 		{ }
> > 		// constructor from an int
> > 		MyType(int d_) : d(d_)
> > 		{ }
> > 		
> > 		// constructor from an qd_real
> > 		MyType(qd_real d_) : d(d_)
> > 		{ }
> > 		
> > 		// constructor from an MyType
> > 		MyType(const MyType&d_) : d(d_.d)
> > 		{ }
> > 				
> > 		// assignment operator
> > 		
> > 		MyType&  operator = (const MyType&x)
> > 		{
> > 			if (this !=&x) {	
> > 			d = x.returnValue();
> > 			}	
> > 			return *this;
> > 		}
> > 		
> > 		MyType operator = (const double&x)
> > 		{
> > 			d = x; 	
> > 			return *this;
> > 		}
> > 		
> > 		MyType operator = (const qd_real&x)
> > 		{
> > 			d = x; 	
> > 			return *this;
> > 		}
> > 						
> > 		// member function that converts to double
> > 		double Double(void) const
> > 		{	return to_double(d); }
> > 		qd_real returnValue(void) const
> > 		{   return d;}
> > 		
> > 		// unary plus
> > 		MyType operator + (void) const
> > 		{	MyType x;
> > 			x.d =  d;
> > 			return x;
> > 		}
> > 		// unary plus
> > 		MyType operator - (void) const
> > 		{	MyType x;
> > 			x.d = - d;
> > 			return x;
> > 		}
> > 		// binary addition
> > 		MyType operator + (const MyType&x) const
> > 		{	MyType y;
> > 			y.d = d + x.d ;
> > 			return y;
> > 		}
> > 		// binary subtraction
> > 		MyType operator - (const MyType&x) const
> > 		{	MyType y;
> > 			y.d = d - x.d ;
> > 			return y;
> > 		}
> > 		MyType operator - (const double&x) const
> > 		{	MyType y;
> > 			y.d = d - x ;
> > 			return y;
> > 		}
> > 		
> > 		// binary multiplication
> > 		MyType operator * (const MyType&x) const
> > 		{	MyType y;
> > 			y.d = d * x.d ;
> > 			return y;
> > 		}
> > 		// binary division
> > 		MyType operator / (const MyType&x) const
> > 		{	MyType y;
> > 			y.d = d / x.d ;
> > 			return y;
> > 		}
> > 		// computed assignment addition
> > 		void operator += (const MyType&x)
> > 		{	d += x.d; }
> > 		
> > 		void operator += (const double&x)
> > 		{	d += x; }
> > 		// computed assignment subtraction
> > 		void operator -= (const MyType&x)
> > 		{	d -= x.d; }
> > 		
> > 		void operator -= (const double&x)
> > 		{	d -= x; }
> > 		
> > 		
> > 		
> > 		// computed assignment multiplication
> > 		void operator *= (const MyType&x)
> > 		{	d *= x.d; }
> > 		// computed assignment division
> > 		void operator /= (const MyType&x)
> > 		{	d /= x.d; }
> > 		
> > 		bool operator == (const MyType&x) const
> > 		{return d==x.d;}
> > 		
> > 		bool operator != (const MyType&x) const
> > 		{return d!=x.d;}
> > 		
> > 		
> > 		friend std::ostream&  operator<<(std::ostream&  out,
> const MyType&  f)
> > 			{
> > 				out<<  f.d;
> > 				
> > 				return out;
> > 			}		
> > 	};
> >
> >
> > namespace CppAD {
> > 	inline bool EqualOpSeq(
> > 		const MyType&x ,
> > 		const MyType&y )
> > 	{	
> > 		return x == y;
> > 	}
> >
> >
> >
> > 	inline bool IdenticalPar(const MyType&x)
> > 	{	return true; }
> > 	inline bool IdenticalZero(const MyType&x)
> > 	{	return (x.returnValue() == 0.0 ); }
> > 	inline bool IdenticalOne(const MyType&x)
> > 	{	return (x.returnValue() == 1.0 ); }
> > 	inline bool IdenticalEqualPar(const MyType&x,const
> MyType&y)
> > 	{	return (x==y); }
> >
> >
> >
> > 	inline MyType cos(const MyType&x)
> > 	{	return MyType(cos(x.returnValue())); }
> > 	
> > 	inline MyType cosh(const MyType&x)
> > 	{	return MyType(cosh(x.returnValue())); }
> > 	
> > 	inline MyType exp(const MyType&x)
> > 	{	return MyType(exp(x.returnValue())); }
> > 	
> > 	inline MyType log(const MyType&x)
> > 	{	return MyType(log(x.returnValue())); }
> > 	
> > 	inline MyType pow(const MyType&x, const MyType&y)
> > 	{	return MyType(pow(x.returnValue(),y.returnValue()));
> }
> > 	
> > 	inline MyType sin(const MyType&x)
> > 	{	return MyType(sin(x.returnValue())); }
> > 	
> > 	inline MyType sinh(const MyType&x)
> > 	{	return MyType(sinh(x.returnValue())); }
> > 	
> > 	inline MyType sqrt(const MyType&x)
> > 	{	return MyType(sqrt(x.returnValue())); }
> > 	
> > 	inline MyType atan(const MyType&x)
> > 	{	return MyType(atan(x.returnValue())); }
> > 	
> > 	inline MyType acos(const MyType&x)
> > 	{	return MyType(acos(x.returnValue())); }
> > 	
> > 	inline MyType asin(const MyType&x)
> > 	{	return MyType(asin(x.returnValue())); }
> >
> >
> > 	inline MyType CondExpOp(
> > 		enum CppAD::CompareOp      cop        ,
> > 		const MyType&left      ,
> > 		const MyType&right     ,
> > 		const MyType&trueCase  ,
> > 		const MyType&falseCase )
> > 	{	CppAD::ErrorHandler::Call(
> > 			true     , __LINE__ , __FILE__ ,
> > 			"MyType CondExpOp(...)",
> > 			"Error: cannot use CondExp with a MyType by now"
> > 		);
> > 		return MyType(0);
> > 	}
> >
> > 	inline bool GreaterThanZero(const MyType&x)
> > 	{	CppAD::ErrorHandler::Call(
> > 			true     , __LINE__ , __FILE__ ,
> > 			"GreaterThanZero(x)",
> > 			"Error: cannot use GreaterThanZero with MyType"
> > 		);
> > 		return false;
> > 	}
> > 	inline bool GreaterThanOrZero(const MyType&x)
> > 	{	CppAD::ErrorHandler::Call(
> > 			true     , __LINE__ , __FILE__ ,
> > 			"GreaterThanZero(x)",
> > 			"Error: cannot use GreaterThanZero with MyType"
> > 		);
> > 		return false;
> > 	}
> > 	inline bool LessThanZero(const MyType&x)
> > 	{	CppAD::ErrorHandler::Call(
> > 			true     , __LINE__ , __FILE__ ,
> > 			"LessThanZero(x)",
> > 			"Error: cannot use LessThanZero with MyType"
> > 		);
> > 		return false;
> > 	}
> > 	inline bool LessThanOrZero(const MyType&x)
> > 	{	CppAD::ErrorHandler::Call(
> > 			true     , __LINE__ , __FILE__ ,
> > 			"LessThanZero(x)",
> > 			"Error: cannot use LessThanZero with MyType"
> > 		);
> > 		return false;
> > 	}
> >
> > 	inline int Integer(const MyType&x)
> > 	{	return static_cast<int>( x.Double() ); }
> > }
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > Many thanks in advance
> >
> > Dominik
> >
> >
> > _______________________________________________
> > CppAD mailing list
> > CppAD at list.coin-or.org
> > http://list.coin-or.org/mailman/listinfo/cppad
> >
> 
> _______________________________________________
> CppAD mailing list
> CppAD at list.coin-or.org
> http://list.coin-or.org/mailman/listinfo/cppad



More information about the CppAD mailing list