00001
00002
00003 #include "BigMatrix.h"
00004
00005 DSS_NAMESPASE_BEGIN
00006
00007
00008 LargeVectorAttach::LargeVectorAttach()
00009 {
00010 data = NULL;
00011 n = 0;
00012 }
00013
00014 LargeVectorAttach::LargeVectorAttach(long N,double* data)
00015 {
00016 this->data = data;
00017 n = N;
00018 }
00019
00020 LargeVectorAttach::~LargeVectorAttach()
00021 {
00022 }
00023
00024 LargeVectorAttach LargeVectorAttach::GetPermutedVector(long* perm) const
00025 {
00026 LargeVector tmp(n);
00027 for (long i=0; i<n; i++)
00028 tmp[i] = data[perm[i]];
00029 return tmp;
00030 }
00031
00032 LargeVectorAttach LargeVectorAttach::GetPermuted_1Vector(long* perm) const
00033 {
00034 LargeVector tmp(n);
00035 for (long i=0; i<n; i++)
00036 tmp[perm[i]] = data[i];
00037 return tmp;
00038 }
00039
00040 void LargeVectorAttach::GetPermutedVector(LargeVectorAttach* outV, IntArrayList& perm,long bn) const
00041 {
00042 if (outV == NULL)
00043 outV = new LargeVector(perm.Count*bn);
00044 for (long i=outV->N()-1; i>=0; i--)
00045 (*outV)[i] = data[perm[i/bn]*bn+(i%bn)];
00046 }
00047
00048 void LargeVectorAttach::GetPermuted_1Vector(LargeVectorAttach* outV,IntArrayList& perm,long bn) const
00049 {
00050 if (outV == NULL)
00051 outV = new LargeVector(perm.Count*bn);
00052 for (long i=0; i<n; i++)
00053 (*outV)[perm[i/bn]*bn+(i%bn)] = data[i];
00054 }
00055
00056 void LargeVectorAttach::GetPermutedVector(LargeVectorAttach* outV, long* perm) const
00057 {
00058 if (outV == NULL)
00059 outV = new LargeVector(n);
00060 for (long i=outV->N()-1; i>=0; i--)
00061 (*outV)[i] = data[perm[i]];
00062 }
00063
00064 void LargeVectorAttach::GetPermuted_1Vector(LargeVectorAttach* outV,long* perm) const
00065 {
00066 if (outV == NULL)
00067 outV = new LargeVector(n);
00068 for (long i=0; i<n; i++)
00069 (*outV)[perm[i]] = data[i];
00070 }
00071
00072 void LargeVectorAttach::Init(long N)
00073 {
00074 if (data) delete [] data;
00075 data = new double[N];
00076 n = N;
00077 }
00078
00079 void LargeVectorAttach::Grow(long addN)
00080 {
00081 double* oldData = this->data;
00082 data = new double[n+addN];
00083 Array::Copy(oldData,data,n);
00084 if (oldData) delete oldData;
00085 n += addN;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 void LargeVectorAttach::Add(const LargeVectorAttach& A)
00098 {
00099 for (long i=n-1; i>=0; i-- )
00100 data[i] += A[i];
00101 }
00102
00103 void LargeVectorAttach::Add(const LargeVectorAttach& A,long start_index,long length)
00104 {
00105 for (long i=start_index; i<start_index+length; i++ )
00106 data[i] += A[i];
00107 }
00108
00109 void LargeVectorAttach::AddSmaler(const LargeVectorAttach &A)
00110 {
00111 for (long i=A.N()-1; i>=0; i-- )
00112 data[i] += A[i];
00113 }
00114
00115 void LargeVectorAttach::Mult(double &alfa)
00116 {
00117 for (long i=n-1; i>=0; i-- )
00118 data[i] *= alfa;
00119 }
00120
00121 void LargeVectorAttach::AddMult(double alfa,const LargeVectorAttach &B)
00122 {
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 double* dp1 = B.DataPtr();
00134 double* d = data;
00135
00136 int i,len4,len;
00137 len4 = n / 4;
00138 len = n % 4;
00139
00140 for ( i = 0; i < len4; i++ )
00141 {
00142 d[4*i] += alfa*dp1[4*i];
00143 d[4*i+1] += alfa*dp1[4*i+1];
00144 d[4*i+2] += alfa*dp1[4*i+2];
00145 d[4*i+3] += alfa*dp1[4*i+3];
00146 }
00147 dp1 += 4*len4;d +=4*len4;
00148
00149 for ( i = 0; i < len; i++ )
00150 d[i] += alfa*(*dp1++);
00151
00152 }
00153
00154 void LargeVectorAttach::LinComb(double alfa,LargeVectorAttach &A,double beta,LargeVectorAttach &B)
00155 {
00156 for (long i=n-1; i>=0; i-- )
00157 data[i] = alfa*A[i]+beta*B[i];
00158 }
00159
00160 void LargeVectorAttach::LinComb(double alfa,LargeVectorAttach &A,LargeVectorAttach &B)
00161 {
00162 for (long i=n-1; i>=0; i-- )
00163 data[i] = alfa*A[i]+B[i];
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 }
00192
00193 void LargeVectorAttach::LinComb(double alfa,LargeVectorAttach &A)
00194 {
00195 for (long i=n-1; i>=0; i-- )
00196 data[i] = alfa*A[i];
00197 }
00198
00199 void LargeVectorAttach::LinComb(LargeVectorAttach &A)
00200 {
00201 for (long i=n-1; i>=0; i-- )
00202 data[i] = A[i];
00203 }
00204
00205 void LargeVectorAttach::Initialize(const LargeVectorAttach &A)
00206 {
00207 Array::Copy(A.data,data,n);
00208 }
00209
00210 void LargeVectorAttach::Initialize(const LargeVectorAttach &A,long start_index,long length)
00211 {
00212 Array::Copy(A.data,start_index,data,start_index,length);
00213 }
00214
00215 void LargeVectorAttach::Initialize(long start_index,long length)
00216 {
00217 Array::Clear(data,start_index,length);
00218 }
00219
00220 void LargeVectorAttach::Initialize(double val)
00221 {
00222 for (long i=n-1; i>=0; i-- )
00223 data[i] = val;
00224 }
00225
00226 void LargeVectorAttach::Initialize()
00227 {
00228 Array::Clear(data,0,n);
00229 }
00230
00231 double LargeVectorAttach::Norm()
00232 {
00233 return sqrt(NormSqr());
00234 }
00235
00236 double LargeVectorAttach::NormSqr()
00237 {
00238 double tmp = 0;
00239 for (long i=n-1; i>=0; i-- )
00240 tmp += pow(data[i],2);
00241 return tmp;
00242 }
00243
00244 void LargeVectorAttach::DiagonalSolve(double* b,double* x)
00245 {
00246 for (long i=n-1; i>=0; i-- )
00247 x[i] = b[i] / data[i];
00248 }
00249
00250 double LargeVectorAttach::InnerProduct(double *dp1,double *dp2,int len)
00251 {
00252 int i,len4;
00253 double sum0, sum1, sum2, sum3;
00254
00255 sum0 = sum1 = sum2 = sum3 = 0.0;
00256
00257 len4 = len / 4;
00258 len = len % 4;
00259
00260 for ( i = 0; i < len4; i++ )
00261 {
00262 sum0 += dp1[4*i]*dp2[4*i];
00263 sum1 += dp1[4*i+1]*dp2[4*i+1];
00264 sum2 += dp1[4*i+2]*dp2[4*i+2];
00265 sum3 += dp1[4*i+3]*dp2[4*i+3];
00266 }
00267 sum0 += sum1 + sum2 + sum3;
00268 dp1 += 4*len4; dp2 += 4*len4;
00269
00270 for ( i = 0; i < len; i++ )
00271 sum0 += (*dp1++)*(*dp2++);
00272
00273 return sum0;
00274 }
00275
00276
00277 void LargeVectorAttach::AddMul(double* A,const double alfa,const double* B,int n)
00278 {
00279 for (long i=n-1; i>=0; i-- )
00280 *A++ += (alfa) * (*B++);
00281 }
00282
00283 void LargeVectorAttach::LoadBinary(FILE* stream)
00284 {
00285 if (data) delete [] data;
00286 data = NULL;
00287
00288 fread(&n,sizeof(n),1,stream);
00289 data = new double[n];
00290 fread(data,sizeof(double),n,stream);
00291 }
00292
00293 void LargeVectorAttach::Zero(double *dp,int len)
00294 {
00295 memset(dp,0,len*sizeof(double));
00296 }
00297
00298
00299
00300
00301
00302 LargeVector::LargeVector()
00303 {
00304 data = NULL;
00305 n = 0;
00306 }
00307
00308 LargeVector::LargeVector(long N)
00309 {
00310 data = new double[N];
00311 Zero(data,N);
00312 n = N;
00313 }
00314
00315 LargeVector::LargeVector(const LargeVectorAttach& B)
00316 {
00317 data = new double[B.N()];
00318 this->n = B.N();
00319 Array::Copy(B.DataPtr(),data,n);
00320 }
00321
00322 LargeVector::LargeVector(const LargeVector& B)
00323 {
00324 data = new double[B.N()];
00325 this->n = B.N();
00326 Array::Copy(B.DataPtr(),data,n);
00327 }
00328
00329 void LargeVector::Detach()
00330 {
00331 data = NULL;
00332 }
00333
00334 LargeVector::~LargeVector()
00335 {
00336 if (data) delete [] data;
00337 data = NULL;
00338 }
00339
00340
00341
00342
00343
00344 TraceableMatrix::TraceableMatrix()
00345 {
00346 eMT = &MT;
00347 }
00348
00349 void TraceableMatrix::Writeln(char* cmd)
00350 {
00351 if (eMT)
00352 eMT->Writeln(cmd);
00353 }
00354 void TraceableMatrix::Write(char* cmd)
00355 {
00356 if (eMT)
00357 eMT->Write(cmd);
00358 }
00359
00360 void TraceableMatrix::CS()
00361 {
00362
00363 time(&temporary_measure_start);
00364 clock_start = clock();
00365 }
00366
00367 char* TraceableMatrix::MC_()
00368 {
00369 clock_t end = clock();
00370 double duration = (double)(end - clock_start) / CLOCKS_PER_SEC;
00371 sprintf(m_string, "...%0.3f s", duration );
00372 return m_string;
00373 }
00374
00375 DSS_NAMESPASE_END