@@ -26,189 +26,189 @@ namespace dftd4 {
2626
2727// Define a vector
2828template <class T > class TVector {
29- public:
30- int N; // Dimension of the vector
31- int ElementSize; // Size of each element in the vector
32- T *p; // The pointer to the vector
33-
34- TVector () {
35- N = 0 ;
36- p = nullptr ;
37- ElementSize = sizeof (T);
38- }
39- ~TVector () {
40- if (p != nullptr ) Delete ();
41- }
42- void NewVector (int VectorLength) {
43- if (VectorLength < 0 ) { std::exit (EXIT_FAILURE); }
44- if (p != nullptr && N == VectorLength) {
45- Init ();
46- } else {
47- Delete ();
48- if (VectorLength == 0 ) { return ; }
49- // get new memory
50- p = new T[VectorLength];
51- if (!p) { std::exit (EXIT_FAILURE); }
52- N = VectorLength;
53- Init ();
29+ public:
30+ int N; // Dimension of the vector
31+ int ElementSize; // Size of each element in the vector
32+ T *p; // The pointer to the vector
33+
34+ TVector () {
35+ N = 0 ;
36+ p = nullptr ;
37+ ElementSize = sizeof (T);
5438 }
55- }
56-
57- // alias for NewVector
58- void New (int VectorLength) { return NewVector (VectorLength); }
59- void NewVec (int VectorLength) { return NewVector (VectorLength); }
60-
61- void Delete () {
62- if (p != nullptr && N != 0 ) { delete[] p; }
63- p = nullptr ;
64- N = 0 ;
65- }
66- void CopyVec (const TVector &v) {
67- long int mem;
68- if (N != v.N ) {
69- Delete ();
70- New (v.N );
39+ ~TVector () {
40+ if (p != nullptr ) Delete ();
7141 }
72- if (v.N == 0 ) return ;
73- mem = (long int )N * ElementSize;
74- std::memcpy (p, v.p , mem);
75- }
76- void Init () {
77- if (p != nullptr ) {
78- long int mem = (long int )N * ElementSize;
79- std::memset (p, 0 , mem);
42+ void NewVector (int VectorLength) {
43+ if (VectorLength < 0 ) { std::exit (EXIT_FAILURE); }
44+ if (p != nullptr && N == VectorLength) {
45+ Init ();
46+ } else {
47+ Delete ();
48+ if (VectorLength == 0 ) { return ; }
49+ // get new memory
50+ p = new T[VectorLength];
51+ if (!p) { std::exit (EXIT_FAILURE); }
52+ N = VectorLength;
53+ Init ();
54+ }
8055 }
81- }
8256
83- void Print (char name[]) {
84- printf (" Vector printed: %s (%d)\n " , name, N);
85- for (int i = 0 ; i < N; i++) {
86- printf (" %+23.15e\n " , p[i]);
57+ // alias for NewVector
58+ void New (int VectorLength) { return NewVector (VectorLength); }
59+ void NewVec (int VectorLength) { return NewVector (VectorLength); }
60+
61+ void Delete () {
62+ if (p != nullptr && N != 0 ) { delete[] p; }
63+ p = nullptr ;
64+ N = 0 ;
65+ }
66+ void CopyVec (const TVector &v) {
67+ long int mem;
68+ if (N != v.N ) {
69+ Delete ();
70+ New (v.N );
71+ }
72+ if (v.N == 0 ) return ;
73+ mem = (long int )N * ElementSize;
74+ std::memcpy (p, v.p , mem);
75+ }
76+ void Init () {
77+ if (p != nullptr ) {
78+ long int mem = (long int )N * ElementSize;
79+ std::memset (p, 0 , mem);
80+ }
8781 }
88- printf (" \n " );
89- }
9082
91- inline T &operator ()(int i) { return p[i]; }
92- inline const T &operator ()(int i) const { return p[i]; }
93- inline T &operator [](int i) { return p[i]; }
94- inline const T &operator [](int i) const { return p[i]; }
83+ void Print (char name[]) {
84+ printf (" Vector printed: %s (%d)\n " , name, N);
85+ for (int i = 0 ; i < N; i++) {
86+ printf (" %+23.15e\n " , p[i]);
87+ }
88+ printf (" \n " );
89+ }
90+
91+ inline T &operator ()(int i) { return p[i]; }
92+ inline const T &operator ()(int i) const { return p[i]; }
93+ inline T &operator [](int i) { return p[i]; }
94+ inline const T &operator [](int i) const { return p[i]; }
9595};
9696
9797// Define a normal matrix
9898template <class T > class TMatrix {
99- public:
100- int rows, cols; // dimensions
101- int ElementSize; // Size of elements in matrix
102- T *p; // pointer to dynamic memory
103-
104- TMatrix () {
105- cols = 0 ;
106- rows = 0 ;
107- p = nullptr ;
108- ElementSize = sizeof (T);
109- }
110- ~TMatrix () {
111- if (p != nullptr ) Delete ();
112- }
113-
114- void NewMatrix (int r, int c) {
115- if (r < 0 || c < 0 ) std::exit (EXIT_FAILURE);
116- if (p != nullptr && r == rows && c == cols) {
117- Init ();
118- } else {
119- long int mem = (long int )r * (long int )c;
120- if (p != nullptr ) Delete (); // Eventually delete old matrix
121-
122- if (mem == 0 ) return ; // don't touch pointer if no memory is allocated
123-
124- p = new T[mem];
125- if (!p) std::exit (EXIT_FAILURE);
126- rows = r;
127- cols = c;
128- Init ();
99+ public:
100+ int rows, cols; // dimensions
101+ int ElementSize; // Size of elements in matrix
102+ T *p; // pointer to dynamic memory
103+
104+ TMatrix () {
105+ cols = 0 ;
106+ rows = 0 ;
107+ p = nullptr ;
108+ ElementSize = sizeof (T);
109+ }
110+ ~TMatrix () {
111+ if (p != nullptr ) Delete ();
112+ }
113+
114+ void NewMatrix (int r, int c) {
115+ if (r < 0 || c < 0 ) std::exit (EXIT_FAILURE);
116+ if (p != nullptr && r == rows && c == cols) {
117+ Init ();
118+ } else {
119+ long int mem = (long int )r * (long int )c;
120+ if (p != nullptr ) Delete (); // Eventually delete old matrix
121+
122+ if (mem == 0 ) return ; // don't touch pointer if no memory is allocated
123+
124+ p = new T[mem];
125+ if (!p) std::exit (EXIT_FAILURE);
126+ rows = r;
127+ cols = c;
128+ Init ();
129+ }
130+ return ;
129131 }
130- return ;
131- }
132-
133- void NewMatrix (const TMatrix &v) { NewMatrix (v.rows , v.cols ); }
134-
135- // alias for NewMatrix
136- void New (int r, int c) { return NewMatrix (r, c); }
137- void NewMat (int r, int c) { return NewMatrix (r, c); }
138-
139- void Delete () {
140- if (p != nullptr && rows * cols != 0 ) { delete[] p; }
141- rows = 0 ;
142- cols = 0 ;
143- p = nullptr ;
144- }
145-
146- void Init () {
147- long int mem;
148- if (p != nullptr ) {
149- mem = (long int )cols * (long int )rows * ElementSize;
150- std::memset (p, 0 , mem);
132+
133+ void NewMatrix (const TMatrix &v) { NewMatrix (v.rows , v.cols ); }
134+
135+ // alias for NewMatrix
136+ void New (int r, int c) { return NewMatrix (r, c); }
137+ void NewMat (int r, int c) { return NewMatrix (r, c); }
138+
139+ void Delete () {
140+ if (p != nullptr && rows * cols != 0 ) { delete[] p; }
141+ rows = 0 ;
142+ cols = 0 ;
143+ p = nullptr ;
151144 }
152- }
153-
154- void Transpose () {
155- T x;
156- int i, j;
157-
158- if (p != nullptr ) {
159- if (rows == cols) {
160- for (i = 0 ; i < rows; i++) {
161- for (j = 0 ; j < i; j++) {
162- x = p[i * cols + j];
163- p[i * cols + j] = p[j * cols + i];
164- p[j * cols + i] = x;
165- } // j
166- } // i
167- } // if NxN
168- else {
169- // for non-square matrix, we need an additional copy
170- TMatrix<T> temp;
171- temp.CopyMat (*this );
172- NewMatrix (cols, rows);
173- for (i = 0 ; i < rows; i++) {
174- for (j = 0 ; j < cols; j++) {
175- p[i * cols + j] = temp.p [j * cols + i];
176- } // j
177- } // i
145+
146+ void Init () {
147+ long int mem;
148+ if (p != nullptr ) {
149+ mem = (long int )cols * (long int )rows * ElementSize;
150+ std::memset (p, 0 , mem);
178151 }
179- } // if data is loaded
180- } // for NxN matrices transpose elements
152+ }
153+
154+ void Transpose () {
155+ T x;
156+ int i, j;
157+
158+ if (p != nullptr ) {
159+ if (rows == cols) {
160+ for (i = 0 ; i < rows; i++) {
161+ for (j = 0 ; j < i; j++) {
162+ x = p[i * cols + j];
163+ p[i * cols + j] = p[j * cols + i];
164+ p[j * cols + i] = x;
165+ } // j
166+ } // i
167+ } // if NxN
168+ else {
169+ // for non-square matrix, we need an additional copy
170+ TMatrix<T> temp;
171+ temp.CopyMat (*this );
172+ NewMatrix (cols, rows);
173+ for (i = 0 ; i < rows; i++) {
174+ for (j = 0 ; j < cols; j++) {
175+ p[i * cols + j] = temp.p [j * cols + i];
176+ } // j
177+ } // i
178+ }
179+ } // if data is loaded
180+ } // for NxN matrices transpose elements
181181
182- void CopyMat (const TMatrix &m) {
183- long int mem;
182+ void CopyMat (const TMatrix &m) {
183+ long int mem;
184184
185- if ((m.rows != rows) || (m.cols != cols)) {
186- Delete ();
187- New (m.rows , m.cols );
185+ if ((m.rows != rows) || (m.cols != cols)) {
186+ Delete ();
187+ New (m.rows , m.cols );
188+ }
189+ mem = (long int )rows * (long int )cols * ElementSize;
190+ if (mem == 0 ) return ;
191+ std::memcpy (p, m.p , mem);
188192 }
189- mem = (long int )rows * (long int )cols * ElementSize;
190- if (mem == 0 ) return ;
191- std::memcpy (p, m.p , mem);
192- }
193-
194- void Print (const char name[] = " unknown" ) {
195- printf (" Matrix printed: %s (%d, %d)\n " , name, rows, cols);
196- for (int i = 0 ; i < rows; i++) {
197- for (int j = 0 ; j < cols; j++) {
198- printf (" %+23.15e" , p[i * cols + j]);
199- if (j == cols - 1 ) {
200- printf (" \n " );
201- } else {
202- printf (" " );
193+
194+ void Print (const char name[] = " unknown" ) {
195+ printf (" Matrix printed: %s (%d, %d)\n " , name, rows, cols);
196+ for (int i = 0 ; i < rows; i++) {
197+ for (int j = 0 ; j < cols; j++) {
198+ printf (" %+23.15e" , p[i * cols + j]);
199+ if (j == cols - 1 ) {
200+ printf (" \n " );
201+ } else {
202+ printf (" " );
203+ }
203204 }
204205 }
206+ printf (" \n " );
205207 }
206- printf (" \n " );
207- }
208208
209- inline T &operator ()(int i, int j) { return p[i * cols + j]; }
210- inline const T &operator ()(int i, int j) const { return p[i * cols + j]; }
211- inline T *operator [](int i) { return p + i * cols; }
209+ inline T &operator ()(int i, int j) { return p[i * cols + j]; }
210+ inline const T &operator ()(int i, int j) const { return p[i * cols + j]; }
211+ inline T *operator [](int i) { return p + i * cols; }
212212};
213213
214214} // namespace dftd4
0 commit comments