SundanceADReal.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
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 // operations with constant reals 
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 

Site Contact