00001
00002
00003 #ifndef _MATRIX_H__
00004 #define _MATRIX_H__
00005
00006 #include "DenseMatrix.h"
00007 #include "IntArrayList.h"
00008 #include "SparseMatrixF.h"
00009
00010 DSS_NAMESPASE_BEGIN
00011
00012
00013
00014 class Array2
00015 {
00016 public:
00017
00018 inline static double ScalarProduct(double* a, double* b,long n)
00019 {
00020 double tmp = 0;
00021 for (long i=0; i<n; i++)
00022 tmp += a[i]*b[i];
00023 return tmp;
00024 }
00025
00026 inline static void Add(const double* a, double* c,long n)
00027 {
00028 for (long i=0; i<n; i++)
00029 c[i] += a[i];
00030 }
00031
00032 inline static void Add(const double* a, const double* b,double* c,long n)
00033 {
00034 for (long i=0; i<n; i++)
00035 c[i] = a[i]+b[i];
00036 }
00037
00038 inline static void Add(const double* a,const double coeff_a, const double* b,const double coeff_b,double* c,long n)
00039 {
00040 for (long i=0; i<n; i++)
00041 c[i] = coeff_a*a[i]+coeff_b*b[i];
00042 }
00043
00044 inline static void Add(const double* a,const double coeff_a, double* c,long n)
00045 {
00046 for (long i=0; i<n; i++)
00047 c[i] += coeff_a*a[i];
00048 }
00049
00050 inline static void Mul(double* a,const double coeff_a,long n)
00051 {
00052 for (long i=0; i<n; i++)
00053 a[i] *= coeff_a;
00054 }
00055 };
00056
00057
00058 class LargeVectorAttach
00059 {
00060 protected:
00061 long n;
00062 double* data;
00063
00064 public:
00065 long N() const {return n;}
00066 inline double* DataPtr() const { return data; }
00067
00068 LargeVectorAttach();
00069 LargeVectorAttach(long N,double* data);
00070 ~LargeVectorAttach();
00071
00072 LargeVectorAttach GetPermutedVector(long* perm) const;
00073 LargeVectorAttach GetPermuted_1Vector(long* perm) const;
00074 void GetPermutedVector(LargeVectorAttach* outV, IntArrayList& perm,long bn) const;
00075 void GetPermuted_1Vector(LargeVectorAttach* outV,IntArrayList& perm,long bn) const;
00076 void GetPermutedVector(LargeVectorAttach* outV, long* perm) const;
00077
00078 void GetPermuted_1Vector(LargeVectorAttach* outV,long* perm) const;
00079 void Init(long N);
00080 void Grow(long addN);
00081
00082 inline double operator[](long i) const
00083 {
00084 return data[i];
00085 }
00086
00087 inline double& operator[](long i)
00088 {
00089 return data[i];
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void Add(const LargeVectorAttach& A);
00102 void Add(const LargeVectorAttach& A,long start_index,long length);
00103 void AddSmaler(const LargeVectorAttach &A);
00104 void Mult(double &alfa);
00105 void AddMult(double alfa,const LargeVectorAttach &B);
00106 void LinComb(double alfa,LargeVectorAttach &A,double beta,LargeVectorAttach &B);
00107 void LinComb(double alfa,LargeVectorAttach &A,LargeVectorAttach &B);
00108 void LinComb(double alfa,LargeVectorAttach &A);
00109 void LinComb(LargeVectorAttach &A);
00110 void Initialize(const LargeVectorAttach &A);
00111 void Initialize(const LargeVectorAttach &A,long start_index,long length);
00112 void Initialize(long start_index,long length);
00113 void Initialize(double val);
00114 void Initialize();
00115
00116 double Norm();
00117 double NormSqr();
00118
00119 void DiagonalSolve(double* b,double* x);
00120
00121 static double InnerProduct(double *dp1,double *dp2,int len);
00122 static void Zero(double *dp1,int len);
00123
00124
00125 static void AddMul(double* A,const double alfa,const double* B,int n);
00126
00127 void LoadBinary(FILE* stream);
00128 };
00129
00130
00131 class LargeVector : public LargeVectorAttach
00132 {
00133 public:
00134 LargeVector();
00135 LargeVector(long N);
00136 LargeVector(const LargeVectorAttach& B);
00137 LargeVector(const LargeVector& B);
00138
00139 void Detach();
00140 ~LargeVector();
00141 };
00142
00143
00144
00145
00146 struct IMatrix
00147 {
00148 virtual long N() const = 0;
00149 virtual long Nonzeros() const = 0;
00150 virtual ~IMatrix(){};
00151 };
00152
00153
00154
00155 struct ILargeMatrix : public IMatrix
00156 {
00157
00158 virtual double& ElementAt(int i, int j) PURE;
00159
00160 virtual void WriteStatistics(long no_init_blocks,long no_nonzeros) PURE;
00161 virtual long No_Multiplications() PURE;
00162
00163 virtual void SolveLV(const LargeVector& x, LargeVector& y) PURE;
00164 virtual void Solve(double* b, double* x) PURE;
00165 virtual void MultiplyByVector(const LargeVectorAttach& x, LargeVectorAttach& y) PURE;
00166 virtual void Factorize() PURE;
00167 virtual void LoadZeros() PURE;
00168 virtual void LoadMatrixNumbers(SparseMatrixF& sm) PURE;
00169 };
00170
00171
00172
00173 class TraceableMatrix
00174 {
00175 private:
00176 char m_string[128];
00177
00178 public:
00179 MathTracer MT;
00180 MathTracer* eMT;
00181
00182 TraceableMatrix();
00183 virtual ~TraceableMatrix(){};
00184
00185 void Writeln(char* cmd);
00186 void Write(char* cmd);
00187
00188 void CS();
00189
00190 char* MC_();
00191
00192 protected:
00193 time_t temporary_measure_start;
00194 clock_t clock_start;
00195 };
00196
00197
00198 struct IConectMatrix : public IMatrix
00199 {
00200 virtual IntArrayList* GetIndexesAboveDiagonalInColumn(long j) = 0;
00201 virtual IntArrayList* DetachIndexesAboveDiagonalInColumn(long j) = 0;
00202 };
00203
00204
00205 DSS_NAMESPASE_END
00206
00207 #endif //_MATRIX_H__