Skip to content
Snippets Groups Projects
Commit 4d0d14bd authored by Patrick Jaap's avatar Patrick Jaap
Browse files

hyperdual: use call-by-reference in operators

parent 1272eb69
Branches
No related tags found
1 merge request!68Improve hyperdual class
......@@ -58,63 +58,63 @@ public:
//basic manipulation
hyperdual operator+ () const;
hyperdual operator+ (const hyperdual rhs) const;
friend hyperdual operator+ (const double lhs, const hyperdual rhs);
hyperdual operator+ (const hyperdual& rhs) const;
friend hyperdual operator+ (const double lhs, const hyperdual& rhs);
hyperdual operator- () const;
hyperdual operator- (const hyperdual rhs) const;
friend hyperdual operator- (const double lhs, const hyperdual rhs);
hyperdual operator* (const hyperdual rhs)const;
friend hyperdual operator* (const double lhs, const hyperdual rhs);
friend hyperdual operator/ (const hyperdual lhs, const hyperdual rhs);
friend hyperdual operator/ (const double lhs, const hyperdual rhs);
friend hyperdual operator/ (const hyperdual lhs, const double rhs);
hyperdual& operator+= (hyperdual rhs);
hyperdual& operator-= (hyperdual rhs);
hyperdual& operator*= (hyperdual rhs);
hyperdual operator- (const hyperdual& rhs) const;
friend hyperdual operator- (const double lhs, const hyperdual& rhs);
hyperdual operator* (const hyperdual& rhs)const;
friend hyperdual operator* (const double lhs, const hyperdual& rhs);
friend hyperdual operator/ (const hyperdual& lhs, const hyperdual& rhs);
friend hyperdual operator/ (const double lhs, const hyperdual& rhs);
friend hyperdual operator/ (const hyperdual& lhs, const double rhs);
hyperdual& operator+= (const hyperdual& rhs);
hyperdual& operator-= (const hyperdual& rhs);
hyperdual& operator*= (const hyperdual& rhs);
hyperdual& operator*= (double rhs);
hyperdual& operator/= (hyperdual rhs);
hyperdual& operator/= (const hyperdual& rhs);
hyperdual& operator/= (double rhs);
//math.h functions
friend hyperdual pow (hyperdual x, double a);
friend hyperdual pow (hyperdual x, hyperdual a);
friend hyperdual exp(hyperdual x);
friend hyperdual log(hyperdual x);
friend hyperdual sin(hyperdual x);
friend hyperdual cos(hyperdual x);
friend hyperdual tan(hyperdual x);
friend hyperdual asin(hyperdual x);
friend hyperdual acos(hyperdual x);
friend hyperdual atan(hyperdual x);
friend hyperdual sqrt(hyperdual x);
friend hyperdual fabs(hyperdual x);
friend hyperdual abs(hyperdual x);
friend hyperdual max(hyperdual x1, hyperdual x2);
friend hyperdual max(hyperdual x1, double x2);
friend hyperdual max(double x1, hyperdual x2);
friend hyperdual min(hyperdual x1, hyperdual x2);
friend hyperdual min(hyperdual x1, double x2);
friend hyperdual min(double x1, hyperdual x2);
friend hyperdual pow (const hyperdual& x, double a);
friend hyperdual pow (const hyperdual& x, const hyperdual& a);
friend hyperdual exp(const hyperdual& x);
friend hyperdual log(const hyperdual& x);
friend hyperdual sin(const hyperdual& x);
friend hyperdual cos(const hyperdual& x);
friend hyperdual tan(const hyperdual& x);
friend hyperdual asin(const hyperdual& x);
friend hyperdual acos(const hyperdual& x);
friend hyperdual atan(const hyperdual& x);
friend hyperdual sqrt(const hyperdual& x);
friend hyperdual fabs(const hyperdual& x);
friend hyperdual abs(const hyperdual& x);
friend hyperdual max(const hyperdual& x1, const hyperdual& x2);
friend hyperdual max(const hyperdual& x1, double x2);
friend hyperdual max(double x1, const hyperdual& x2);
friend hyperdual min(const hyperdual& x1, const hyperdual& x2);
friend hyperdual min(const hyperdual& x1, double x2);
friend hyperdual min(double x1, const hyperdual& x2);
//comparisons
friend bool operator> (hyperdual lhs, hyperdual rhs);
friend bool operator> (double lhs, hyperdual rhs);
friend bool operator> (hyperdual lhs, double rhs);
friend bool operator>= (hyperdual lhs, hyperdual rhs);
friend bool operator>= (double lhs, hyperdual rhs);
friend bool operator>= (hyperdual lhs, double rhs);
friend bool operator< (hyperdual lhs, hyperdual rhs);
friend bool operator< (double lhs, hyperdual rhs);
friend bool operator< (hyperdual lhs, double rhs);
friend bool operator<= (hyperdual lhs, hyperdual rhs);
friend bool operator<= (double lhs, hyperdual rhs);
friend bool operator<= (hyperdual lhs, double rhs);
friend bool operator== (hyperdual lhs, hyperdual rhs);
friend bool operator== (double lhs, hyperdual rhs);
friend bool operator== (hyperdual lhs, double rhs);
friend bool operator!= (hyperdual lhs, hyperdual rhs);
friend bool operator!= (double lhs, hyperdual rhs);
friend bool operator!= (hyperdual lhs, double rhs);
friend bool operator> (const hyperdual& lhs, const hyperdual& rhs);
friend bool operator> (double lhs, const hyperdual& rhs);
friend bool operator> (const hyperdual& lhs, double rhs);
friend bool operator>= (const hyperdual& lhs, const hyperdual& rhs);
friend bool operator>= (double lhs, const hyperdual& rhs);
friend bool operator>= (const hyperdual& lhs, double rhs);
friend bool operator< (const hyperdual& lhs, const hyperdual& rhs);
friend bool operator< (double lhs, const hyperdual& rhs);
friend bool operator< (const hyperdual& lhs, double rhs);
friend bool operator<= (const hyperdual& lhs, const hyperdual& rhs);
friend bool operator<= (double lhs, const hyperdual& rhs);
friend bool operator<= (const hyperdual& lhs, double rhs);
friend bool operator== (const hyperdual& lhs, const hyperdual& rhs);
friend bool operator== (double lhs, const hyperdual& rhs);
friend bool operator== (const hyperdual& lhs, double rhs);
friend bool operator!= (const hyperdual& lhs, const hyperdual& rhs);
friend bool operator!= (double lhs, const hyperdual& rhs);
friend bool operator!= (const hyperdual& lhs, double rhs);
};
......@@ -192,7 +192,7 @@ hyperdual hyperdual::operator+ () const
{
return *this;
}
hyperdual hyperdual::operator+ (const hyperdual rhs) const
hyperdual hyperdual::operator+ (const hyperdual& rhs) const
{
hyperdual temp;
temp.f0 = f0 + rhs.f0;
......@@ -201,7 +201,7 @@ hyperdual hyperdual::operator+ (const hyperdual rhs) const
temp.f12 = f12 + rhs.f12;
return temp;
}
hyperdual operator+ (const double lhs, const hyperdual rhs)
hyperdual operator+ (const double lhs, const hyperdual& rhs)
{
hyperdual temp;
temp.f0 = lhs + rhs.f0;
......@@ -219,7 +219,7 @@ hyperdual hyperdual::operator- () const
temp.f12 = -f12;
return temp;
}
hyperdual hyperdual::operator- (const hyperdual rhs) const
hyperdual hyperdual::operator- (const hyperdual& rhs) const
{
hyperdual temp;
temp.f0 = f0 - rhs.f0;
......@@ -228,7 +228,7 @@ hyperdual hyperdual::operator- (const hyperdual rhs) const
temp.f12 = f12 - rhs.f12;
return temp;
}
hyperdual operator- (const double lhs, const hyperdual rhs)
hyperdual operator- (const double lhs, const hyperdual& rhs)
{
hyperdual temp;
temp.f0 = lhs - rhs.f0;
......@@ -237,7 +237,7 @@ hyperdual operator- (const double lhs, const hyperdual rhs)
temp.f12 = -rhs.f12;
return temp;
}
hyperdual hyperdual::operator* (const hyperdual rhs) const
hyperdual hyperdual::operator* (const hyperdual& rhs) const
{
hyperdual temp;
temp.f0 = f0*rhs.f0;
......@@ -246,7 +246,7 @@ hyperdual hyperdual::operator* (const hyperdual rhs) const
temp.f12 = f0*rhs.f12 + f1*rhs.f2 + f2*rhs.f1 + f12*rhs.f0;
return temp;
}
hyperdual operator* (const double lhs, const hyperdual rhs)
hyperdual operator* (const double lhs, const hyperdual& rhs)
{
hyperdual temp;
temp.f0 = lhs*rhs.f0;
......@@ -255,7 +255,7 @@ hyperdual operator* (const double lhs, const hyperdual rhs)
temp.f12 = lhs*rhs.f12;
return temp;
}
hyperdual operator/ (const hyperdual lhs, const hyperdual rhs)
hyperdual operator/ (const hyperdual& lhs, const hyperdual& rhs)
{
hyperdual inv;
double prod;
......@@ -267,13 +267,13 @@ hyperdual operator/ (const hyperdual lhs, const hyperdual rhs)
inv.f12 = 2.0*rhs.f1*rhs.f2/(prod*rhs.f0) - rhs.f12/prod;
return lhs*inv;
}
hyperdual operator/ (const double lhs, const hyperdual rhs)
hyperdual operator/ (const double lhs, const hyperdual& rhs)
{
hyperdual temp,inv;
temp = hyperdual(lhs)/rhs;
return temp;
}
hyperdual operator/ (const hyperdual lhs, const double rhs)
hyperdual operator/ (const hyperdual& lhs, const double rhs)
{
hyperdual temp;
double inv;
......@@ -284,7 +284,7 @@ hyperdual operator/ (const hyperdual lhs, const double rhs)
temp.f12 = inv*lhs.f12;
return temp;
}
hyperdual& hyperdual::operator+= (hyperdual rhs)
hyperdual& hyperdual::operator+= (const hyperdual& rhs)
{
f0 += rhs.f0;
f1 += rhs.f1;
......@@ -292,7 +292,7 @@ hyperdual& hyperdual::operator+= (hyperdual rhs)
f12 += rhs.f12;
return *this;
}
hyperdual& hyperdual::operator-= (hyperdual rhs)
hyperdual& hyperdual::operator-= (const hyperdual& rhs)
{
f0 -= rhs.f0;
f1 -= rhs.f1;
......@@ -300,7 +300,7 @@ hyperdual& hyperdual::operator-= (hyperdual rhs)
f12 -= rhs.f12;
return *this;
}
hyperdual& hyperdual::operator*= (hyperdual rhs)
hyperdual& hyperdual::operator*= (const hyperdual& rhs)
{
double tf0,tf1,tf2,tf12;
tf0=f0;
......@@ -321,7 +321,7 @@ hyperdual& hyperdual::operator*= (double rhs)
f12 *= rhs;
return *this;
}
hyperdual& hyperdual::operator/= (hyperdual rhs)
hyperdual& hyperdual::operator/= (const hyperdual& rhs)
{
*this = *this / rhs;
return *this;
......@@ -334,7 +334,7 @@ hyperdual& hyperdual::operator/= (double rhs)
f12 /= rhs;
return *this;
}
hyperdual pow (hyperdual x, double a)
hyperdual pow (const hyperdual& x, double a)
{
hyperdual temp;
double deriv,xval,tol;
......@@ -356,11 +356,11 @@ hyperdual pow (hyperdual x, double a)
return temp;
}
hyperdual pow (hyperdual x, hyperdual a)
hyperdual pow (const hyperdual& x, const hyperdual& a)
{
return exp(a*log(x));
}
hyperdual exp(hyperdual x)
hyperdual exp(const hyperdual& x)
{
hyperdual temp;
double deriv;
......@@ -371,7 +371,7 @@ hyperdual exp(hyperdual x)
temp.f12 = deriv*(x.f12 + x.f1*x.f2);
return temp;
}
hyperdual log(hyperdual x)
hyperdual log(const hyperdual& x)
{
hyperdual temp;
double deriv1,deriv2;
......@@ -383,7 +383,7 @@ hyperdual log(hyperdual x)
temp.f12 = x.f12/x.f0 - (deriv1*deriv2);
return temp;
}
hyperdual sin(hyperdual x)
hyperdual sin(const hyperdual& x)
{
hyperdual temp;
double funval,deriv;
......@@ -395,7 +395,7 @@ hyperdual sin(hyperdual x)
temp.f12 = deriv*x.f12 - funval*x.f1*x.f2;
return temp;
}
hyperdual cos(hyperdual x)
hyperdual cos(const hyperdual& x)
{
hyperdual temp;
double funval,deriv;
......@@ -407,7 +407,7 @@ hyperdual cos(hyperdual x)
temp.f12 = deriv*x.f12 - funval*x.f1*x.f2;
return temp;
}
hyperdual tan(hyperdual x)
hyperdual tan(const hyperdual& x)
{
hyperdual temp;
double funval,deriv;
......@@ -419,7 +419,7 @@ hyperdual tan(hyperdual x)
temp.f12 = deriv*x.f12 + x.f1*x.f2*(2*funval*deriv);
return temp;
}
hyperdual asin(hyperdual x)
hyperdual asin(const hyperdual& x)
{
hyperdual temp;
double funval,deriv1,deriv;
......@@ -432,7 +432,7 @@ hyperdual asin(hyperdual x)
temp.f12 = deriv*x.f12 + x.f1*x.f2*(x.f0*std::pow(deriv1,-1.5));
return temp;
}
hyperdual acos(hyperdual x)
hyperdual acos(const hyperdual& x)
{
hyperdual temp;
double funval,deriv1,deriv;
......@@ -445,7 +445,7 @@ hyperdual acos(hyperdual x)
temp.f12 = deriv*x.f12 + x.f1*x.f2*(-x.f0*std::pow(deriv1,-1.5));
return temp;
}
hyperdual atan(hyperdual x)
hyperdual atan(const hyperdual& x)
{
hyperdual temp;
double funval,deriv1,deriv;
......@@ -458,7 +458,7 @@ hyperdual atan(hyperdual x)
temp.f12 = deriv*x.f12 + x.f1*x.f2*(-2*x.f0/(deriv1*deriv1));
return temp;
}
hyperdual sqrt(hyperdual x)
hyperdual sqrt(const hyperdual& x)
{
hyperdual temp;
temp.f0 = std::sqrt(x.f0);
......@@ -467,7 +467,7 @@ hyperdual sqrt(hyperdual x)
temp.f12 = 0.5*(x.f12 - 2.*temp.f1*temp.f2)/temp.f0;
return temp;
}
hyperdual fabs(hyperdual x)
hyperdual fabs(const hyperdual& x)
{
hyperdual temp;
if (x < 0.0)
......@@ -477,11 +477,11 @@ hyperdual fabs(hyperdual x)
return temp;
}
hyperdual abs(hyperdual x)
hyperdual abs(const hyperdual& x)
{
return fabs(x);
}
hyperdual max(hyperdual x1, hyperdual x2)
hyperdual max(const hyperdual& x1, const hyperdual& x2)
{
hyperdual temp;
if (x1>x2)
......@@ -490,7 +490,7 @@ hyperdual max(hyperdual x1, hyperdual x2)
temp = x2;
return temp;
}
hyperdual max(hyperdual x1, double x2)
hyperdual max(const hyperdual& x1, double x2)
{
hyperdual temp;
if (x1>x2)
......@@ -499,7 +499,7 @@ hyperdual max(hyperdual x1, double x2)
temp = x2;
return temp;
}
hyperdual max(double x1, hyperdual x2)
hyperdual max(double x1, const hyperdual& x2)
{
hyperdual temp;
if (x1>x2)
......@@ -508,7 +508,7 @@ hyperdual max(double x1, hyperdual x2)
temp = x2;
return temp;
}
hyperdual min(hyperdual x1, hyperdual x2)
hyperdual min(const hyperdual& x1, const hyperdual& x2)
{
hyperdual temp;
if (x1<x2)
......@@ -517,7 +517,7 @@ hyperdual min(hyperdual x1, hyperdual x2)
temp = x2;
return temp;
}
hyperdual min(hyperdual x1, double x2)
hyperdual min(const hyperdual& x1, double x2)
{
hyperdual temp;
if (x1<x2)
......@@ -526,7 +526,7 @@ hyperdual min(hyperdual x1, double x2)
temp = x2;
return temp;
}
hyperdual min(double x1, hyperdual x2)
hyperdual min(double x1, const hyperdual& x2)
{
hyperdual temp;
if (x1<x2)
......@@ -536,75 +536,75 @@ hyperdual min(double x1, hyperdual x2)
return temp;
}
bool operator> (hyperdual lhs, hyperdual rhs)
bool operator> (const hyperdual& lhs, const hyperdual& rhs)
{
return (lhs.f0 > rhs.f0);
}
bool operator> (double lhs, hyperdual rhs)
bool operator> (double lhs, const hyperdual& rhs)
{
return (lhs > rhs.f0);
}
bool operator> (hyperdual lhs, double rhs)
bool operator> (const hyperdual& lhs, double rhs)
{
return (lhs.f0 > rhs);
}
bool operator>= (hyperdual lhs, hyperdual rhs)
bool operator>= (const hyperdual& lhs, const hyperdual& rhs)
{
return (lhs.f0 >= rhs.f0);
}
bool operator>= (double lhs, hyperdual rhs)
bool operator>= (double lhs, const hyperdual& rhs)
{
return (lhs >= rhs.f0);
}
bool operator>= (hyperdual lhs, double rhs)
bool operator>= (const hyperdual& lhs, double rhs)
{
return (lhs.f0 >= rhs);
}
bool operator< (hyperdual lhs, hyperdual rhs)
bool operator< (const hyperdual& lhs, const hyperdual& rhs)
{
return (lhs.f0 < rhs.f0);
}
bool operator< (double lhs, hyperdual rhs)
bool operator< (double lhs, const hyperdual& rhs)
{
return (lhs < rhs.f0);
}
bool operator< (hyperdual lhs, double rhs)
bool operator< (const hyperdual& lhs, double rhs)
{
return (lhs.f0 < rhs);
}
bool operator<= (hyperdual lhs, hyperdual rhs)
bool operator<= (const hyperdual& lhs, const hyperdual& rhs)
{
return (lhs.f0 <= rhs.f0);
}
bool operator<= (double lhs, hyperdual rhs)
bool operator<= (double lhs, const hyperdual& rhs)
{
return (lhs <= rhs.f0);
}
bool operator<= (hyperdual lhs, double rhs)
bool operator<= (const hyperdual& lhs, double rhs)
{
return (lhs.f0 <= rhs);
}
bool operator== (hyperdual lhs, hyperdual rhs)
bool operator== (const hyperdual& lhs, const hyperdual& rhs)
{
return (lhs.f0 == rhs.f0);
}
bool operator== (double lhs, hyperdual rhs)
bool operator== (double lhs, const hyperdual& rhs)
{
return (lhs == rhs.f0);
}
bool operator== (hyperdual lhs, double rhs)
bool operator== (const hyperdual& lhs, double rhs)
{
return (lhs.f0 == rhs);
}
bool operator!= (hyperdual lhs, hyperdual rhs)
bool operator!= (const hyperdual& lhs, const hyperdual& rhs)
{
return (lhs.f0 != rhs.f0);
}
bool operator!= (double lhs, hyperdual rhs)
bool operator!= (double lhs, const hyperdual& rhs)
{
return (lhs != rhs.f0);
}
bool operator!= (hyperdual lhs, double rhs)
bool operator!= (const hyperdual& lhs, double rhs)
{
return (lhs.f0 != rhs);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment