Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "SundanceADReal.hpp"
00044 #include "PlayaExceptions.hpp"
00045
00046
00047 using namespace Sundance;
00048 using namespace Teuchos;
00049
00050 ADReal ADReal::operator-() const
00051 {
00052 ADReal rtn = *this;
00053 rtn.value_ = -rtn.value_;
00054 rtn.gradient_ = -gradient_;
00055
00056 addFlops(1 + gradient_.dim());
00057 return rtn;
00058 }
00059
00060 ADReal ADReal::operator+(const ADReal& other) const
00061 {
00062 ADReal rtn(*this);
00063 rtn += other;
00064 return rtn;
00065 }
00066
00067 ADReal ADReal::operator-(const ADReal& other) const
00068 {
00069 ADReal rtn(*this);
00070 rtn -= other;
00071 return rtn;
00072 }
00073
00074 ADReal ADReal::operator*(const ADReal& other) const
00075 {
00076 ADReal rtn(*this);
00077 rtn *= other;
00078 return rtn;
00079 }
00080
00081 ADReal ADReal::operator/(const ADReal& other) const
00082 {
00083 ADReal rtn(*this);
00084 rtn /= other;
00085 return rtn;
00086 }
00087
00088 ADReal& ADReal::operator+=(const ADReal& other)
00089 {
00090 value_ += other.value_;
00091 gradient_ += other.gradient_;
00092
00093 addFlops(1 + gradient_.dim());
00094
00095 return *this;
00096 }
00097
00098 ADReal& ADReal::operator-=(const ADReal& other)
00099 {
00100 value_ -= other.value_;
00101 gradient_ -= other.gradient_;
00102
00103 addFlops(1 + gradient_.dim());
00104
00105 return *this;
00106 }
00107
00108 ADReal& ADReal::operator*=(const ADReal& other)
00109 {
00110 gradient_ = (other.value_*gradient_ + value_*other.gradient_);
00111 value_ *= other.value_;
00112
00113 addFlops(1 + 3*gradient_.dim());
00114
00115 return *this;
00116 }
00117
00118 ADReal& ADReal::operator/=(const ADReal& other)
00119 {
00120 TEUCHOS_TEST_FOR_EXCEPTION(other.value_ == 0.0, std::runtime_error,
00121 "ADReal::operator/=() division by zero");
00122
00123 gradient_ = (gradient_/other.value_
00124 - value_/(other.value_*other.value_) * other.gradient_);
00125 value_ /= other.value_;
00126
00127 addFlops(3 + 3*gradient_.dim());
00128
00129 return *this;
00130 }
00131
00132
00133
00134
00135 ADReal& ADReal::operator+=(const double& other)
00136 {
00137 value_ += other;
00138 addFlops(1);
00139 return *this;
00140 }
00141
00142 ADReal& ADReal::operator-=(const double& other)
00143 {
00144 value_ -= other;
00145
00146 addFlops(1);
00147 return *this;
00148 }
00149
00150 ADReal& ADReal::operator*=(const double& other)
00151 {
00152 gradient_ *= other;
00153 value_ *= other;
00154
00155 addFlops(1 + gradient_.dim());
00156
00157 return *this;
00158 }
00159
00160 ADReal& ADReal::operator/=(const double& other)
00161 {
00162 TEUCHOS_TEST_FOR_EXCEPTION(other == 0.0, std::runtime_error,
00163 "ADReal::operator/=() division by zero");
00164
00165 addFlops(2 + gradient_.dim());
00166 gradient_ *= 1.0/other;
00167 value_ /= other;
00168 return *this;
00169 }
00170
00171
00172 ADReal ADReal::operator+(const double& other) const
00173 {
00174 ADReal rtn(*this);
00175 rtn += other;
00176 return rtn;
00177 }
00178
00179 ADReal ADReal::operator-(const double& other) const
00180 {
00181 ADReal rtn(*this);
00182 rtn -= other;
00183 return rtn;
00184 }
00185
00186 ADReal ADReal::operator*(const double& other) const
00187 {
00188 ADReal rtn(*this);
00189 rtn *= other;
00190 return rtn;
00191 }
00192
00193 ADReal ADReal::operator/(const double& other) const
00194 {
00195 ADReal rtn(*this);
00196 rtn /= other;
00197 return rtn;
00198 }
00199
00200
00201 namespace Sundance
00202 {
00203 ADReal operator+(const double& scalar, const ADReal& a)
00204 {
00205 return a+scalar;
00206 }
00207
00208 ADReal operator-(const double& scalar, const ADReal& a)
00209 {
00210 return -a+scalar;
00211 }
00212
00213 ADReal operator*(const double& scalar, const ADReal& a)
00214 {
00215 return a*scalar;
00216 }
00217
00218 ADReal operator/(const double& scalar, const ADReal& a)
00219 {
00220 ADReal rtn(a);
00221 rtn.reciprocate();
00222 return rtn*scalar;
00223 }
00224 }
00225
00226 void ADReal::reciprocate()
00227 {
00228 TEUCHOS_TEST_FOR_EXCEPTION(value_==0.0, std::runtime_error,
00229 "div by 0 in ADReal::reciprocate()");
00230
00231 addFlops(1 + gradient_.dim());
00232
00233 gradient_ /= (value_*value_);
00234 gradient_ *= -1.0;
00235 value_ = 1.0/value_;
00236 }
00237
00238
00239
00240
00241
00242
00243
00244