00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <iostream>
00024 #include <fstream>
00025 #include "bg.h"
00026
00027 namespace matvec{
00028
00029 unsigned BG::dimen=3;
00030 matvec::doubleMatrix BG::x;
00031
00032 BG::BG(void){
00033 if(dimen==0){
00034 std::cout <<"BG has not been initialized yet 1\n";
00035 exit (1);
00036 }
00037 else {
00038 f = 0.0;
00039 d.resize(dimen,1,0.0);
00040 D.resize(dimen,dimen,0.0);
00041 }
00042 }
00043
00044 BG::BG(unsigned i, double b){
00045 if(dimen==0){
00046 std::cout <<"BG has not been initialized yet 2\n";
00047 exit (1);
00048 }
00049 else {
00050 f = x[i][0]*b;
00051 d.resize(dimen,1,0.0);
00052 d[i][0] = b;
00053 D.resize(dimen,dimen,0.0);
00054 }
00055 }
00056
00057 BG::BG(double a){
00058 if(dimen==0){
00059 std::cout <<"BG has not been initialized yet 3\n";
00060 exit (1);
00061 }
00062 else {
00063 f = a;
00064 d.resize(dimen,1,0.0);
00065 D.resize(dimen,dimen,0.0);
00066 }
00067 }
00068
00069 BG::BG(const BG &a){
00070 if(dimen==0){
00071 std::cout <<"BG has not been initialized yet 4\n";
00072 exit (1);
00073 }
00074 else {
00075 f = a.f;
00076 d = a.d;
00077 D = a.D;
00078 }
00079 }
00080
00081
00082
00083 BG operator+(const BG &a, const BG &b){
00084 BG r;
00085 r.f = a.f + b.f;
00086 r.d = a.d + b.d;
00087 r.D = a.D + b.D;
00088 return r;
00089 }
00090
00091 BG operator-(const BG &a, const BG &b){
00092 BG r;
00093 r.f = a.f - b.f;
00094 r.d = a.d - b.d;
00095 r.D = a.D - b.D;
00096 return r;
00097 }
00098
00099 BG operator*(const BG &a, const BG &b){
00100 BG r;
00101 r.f = a.f * b.f;
00102 r.d = a.f*b.d + a.d*b.f;
00103 r.D = a.f*b.D + a.d*b.d.transpose() + b.d*a.d.transpose() + a.D*b.f;
00104 return r;
00105 }
00106
00107 BG operator/(double a, const BG &b){
00108 BG r;
00109 r.f = a/b.f;
00110 r.d = -a/(b.f*b.f) * b.d;
00111 r.D = -a*( 1.0/(b.f*b.f) * b.D - 2.0/(b.f*b.f*b.f)*b.d*b.d.transpose() );
00112 return r;
00113 }
00114
00115 BG operator-(const BG &a){
00116 BG r;
00117 r.f = -a.f;
00118 r.d = -a.d;
00119 r.D = -a.D;
00120 return r;
00121 }
00122
00123 BG operator/(const BG &a, const BG &b){
00124 BG r = a * (1.0/b);
00125 return r;
00126 }
00127
00128 BG sqrt(const BG &a){
00129 BG r;
00130 r.f = std::sqrt(a.f);
00131 r.d = 0.5/std::sqrt(a.f)*a.d;
00132 r.D = 0.5*( 1.0/std::sqrt(a.f)*a.D - 0.5/std::sqrt(a.f*a.f*a.f) * a.d*a.d.transpose() );
00133 return r;
00134 }
00135
00136 BG log(const BG &a){
00137 BG r;
00138 r.f = std::log(a.f);
00139 r.d = 1.0/a.f*a.d;
00140 r.D = 1.0/a.f*a.D - 1.0/(a.f*a.f) * a.d*a.d.transpose();
00141 return r;
00142 }
00143
00144 BG exp(const BG &a){
00145 BG r;
00146 r.f = std::exp(a.f);
00147 r.d = r.f*a.d;
00148 r.D = r.f*a.D + r.d*a.d.transpose();
00149 return r;
00150 }
00151
00152 BG operator+(double a, const BG &b){
00153 BG r;
00154 r.f = a + b.f;
00155 r.d = b.d;
00156 r.D = b.D;
00157 return r;
00158 }
00159
00160 BG operator-(double a, const BG &b){
00161 BG r;
00162 r.f = a - b.f;
00163 r.d = -b.d;
00164 r.D = -b.D;
00165 return r;
00166 }
00167
00168 BG operator*(double a, const BG &b){
00169 BG r;
00170 r.f = a*b.f;
00171 r.d = a*b.d;
00172 r.D = a*b.D;
00173 return r;
00174 }
00175
00176 BG operator+(const BG &a, double b){
00177 BG r = b + a;
00178 return r;
00179 }
00180
00181 BG operator-(const BG &a, double b){
00182 BG r;
00183 r.f = a.f - b;
00184 r.d = a.d;
00185 r.D = a.D;
00186 return r;
00187 }
00188
00189 BG operator*(const BG &a, double b){
00190 BG r = b * a;
00191 return r;
00192 }
00193
00194 BG operator/(const BG &a, double b){
00195 BG r = a * (1.0/b);
00196 return r;
00197 }
00198
00199 BG& operator+=(BG &a, const BG &b){
00200 a = a + b;
00201 return a;
00202 }
00203
00204 BG& operator-=(BG &a, const BG &b){
00205 a = a - b;
00206 return a;
00207 }
00208
00209 BG& operator*=(BG &a, const BG &b){
00210 a = a*b;
00211 return a;
00212 }
00213
00214 BG& operator/=(BG &a, const BG &b){
00215 a = a/b;
00216 return a;
00217 }
00218
00219 BG& operator+=(BG &a, double b){
00220 a = a + b;
00221 return a;
00222 }
00223
00224 BG& operator-=(BG &a, double b){
00225 a = a - b;
00226 return a;
00227 }
00228
00229 BG& operator*=(BG &a, double b){
00230 a = a * b;
00231 return a;
00232 }
00233
00234 BG& operator/=(BG &a, double b){
00235 a = a / b;
00236 return a;
00237 }
00238
00239 BG& BG::operator=(double b){
00240 f = b;
00241 d.resize(BG::dimen,1,0.0);
00242 D.resize(BG::dimen,BG::dimen,0.0);
00243 return (*this);
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 BG& BG::initialize(unsigned i, double b){
00261 f = b*x[i][0];
00262 d.resize(BG::dimen,1,0.0);
00263 d[i][0] = b;
00264 D.resize(BG::dimen,BG::dimen,0.0);
00265 return (*this);
00266 }
00267
00268
00269 void BG::display(void){
00270 std::cout <<"Function value is: " << f << std::endl << std::endl;
00271 std::cout <<"First Derivatives are: " << std::endl;
00272 std::cout << d << std::endl;
00273 std::cout <<"Second Derivatives are: " << std::endl;
00274 std::cout << D << std::endl;
00275 }
00276 void BG::NRupdate(void){
00277 x -= D.ginv0()*d;
00278 }
00279
00280
00281 }