File: Vector.h

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (234 lines) | stat: -rw-r--r-- 7,685 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef VECTOR_H
#define VECTOR_H

#include "Matrix.h"

namespace ATC_matrix {

///////////////////////////////////////////////////////////////////////////////
// forward declarations ///////////////////////////////////////////////////////

//* Matrix-vector product
//template<typename T>
//void MultMv(const Matrix<T> &A, const Vector<T> &v, DenseVector<T> &c,
//            const bool At=0, T a=1, T b=0);

/******************************************************************************
* abstract class Vector
******************************************************************************/


template<typename T>
class Vector : public Matrix<T>
{
public:
  Vector() {}
  Vector(const Vector<T> &c); // do not implement!
  virtual ~Vector() {}

  std::string to_string() const;

  // pure virtual functions
  virtual T  operator()(INDEX i, INDEX j=0) const=0;
  virtual T& operator()(INDEX i, INDEX j=0)      =0;
  virtual T  operator[](INDEX i)            const=0;
  virtual T& operator[](INDEX i)                 =0;
  virtual INDEX nRows()                     const=0;
  virtual T* ptr()                      const=0;
  virtual void resize(INDEX nRows, INDEX nCols=1, bool copy=0)=0;
  virtual void  reset(INDEX nRows, INDEX nCols=1, bool zero=0)=0;
  virtual void copy(const T * ptr, INDEX nRows, INDEX nCols=1)=0;
  void write_restart(FILE *f)               const; // will be virtual


  // output to matlab
  using Matrix<T>::matlab;
  void matlab(std::ostream &o, const std::string &s="v") const;

  using Matrix<T>::operator=;
  INDEX nCols()                   const;
  bool in_range(INDEX i)          const;
  bool same_size(const Vector &m) const;
  static bool same_size(const Vector &a, const Vector &b);

 protected:
  void _set_equal(const Matrix<T> &r);
  //* don't allow this
  Vector& operator=(const Vector &r);
};

///////////////////////////////////////////////////////////////////////////////
//* performs a matrix-vector multiply with default naive implementation
template<typename T>
void MultMv(const Matrix<T> &A, const Vector<T> &v, DenseVector<T> &c,
            const bool At, T /* a */, T b)
{
  const INDEX sA[2] = {A.nRows(), A.nCols()};  // m is sA[At] k is sA[!At]
  const INDEX M=sA[At], K=sA[!At];
  GCK(A, v, v.size()!=K, "MultAb<T>: matrix-vector multiply");
  if (c.size() != M)
  {
    c.resize(M);             // set size of C
    c.zero();                // do not add result to C
  }
  else c *= b;
  for (INDEX p=0; p<M; p++)
    for (INDEX r=0; r<K; r++)
       c[p] += A(p*!At+r*At, p*At+r*!At) * v[r];
}
///////////////////////////////////////////////////////////////////////////////
//* Operator for Matrix-vector product
template<typename T>
DenseVector<T> operator*(const Matrix<T> &A, const Vector<T> &b)
{
  DenseVector<T> c;
  MultMv(A, b, c, 0, 1.0, 0.0);
  return c;
}
///////////////////////////////////////////////////////////////////////////////
//* Operator for Vector-matrix product
template<typename T>
DenseVector<T> operator*(const Vector<T> &a, const Matrix<T> &B)
{
  DenseVector<T> c;
  MultMv(B, a, c, 1, 1.0, 0.0);
  return c;
}
///////////////////////////////////////////////////////////////////////////////
//* Multiply a vector by a scalar
template<typename T>
DenseVector<T> operator*(const Vector<T> &v, const T s)
{
  DenseVector<T> r(v);
  r*=s;
  return r;
}
///////////////////////////////////////////////////////////////////////////////
//* Multiply a vector by a scalar - communitive
template<typename T>
DenseVector<T> operator*(const T s, const Vector<T> &v)
{
  DenseVector<T> r(v);
  r*=s;
  return r;
}
///////////////////////////////////////////////////////////////////////////////
//* inverse scaling operator - must always create memory
template<typename T>
DenseVector<T> operator/(const Vector<T> &v, const T s)
{
  DenseVector<T> r(v);
  r*=(1.0/s); // for integer types this may be worthless
  return r;
}
///////////////////////////////////////////////////////////////////////////////
//* Operator for Vector-Vector sum
template<typename T>
DenseVector<T> operator+(const Vector<T> &a, const Vector<T> &b)
{
  DenseVector<T> c(a);
  c+=b;
  return c;
}
///////////////////////////////////////////////////////////////////////////////
//* Operator for Vector-Vector subtraction
template<typename T>
DenseVector<T> operator-(const Vector<T> &a, const Vector<T> &b)
{
  DenseVector<T> c(a);
  c-=b;
  return c;
}

///////////////////////////////////////////////////////////////////////////////
// Template definitions ///////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//* output operator
template<typename T>
std::string Vector<T>::to_string() const
{
  std::string s;
  int sz = this->size();
  for (INDEX i = 0; i < sz; i++)
    s += std::string(i?"\t":"") + ATC_Utility::to_string((*this)[i],myPrecision);
  return s;
}
///////////////////////////////////////////////////////////////////////////////
//* Writes a matlab script defining the vector to the stream
template<typename T>
void Vector<T>::matlab(std::ostream &o, const std::string &s) const
{
  o << s <<"=zeros(" << this->size() << ",1);\n";
  int sz = this->size();
  for (INDEX i = 0; i < sz; i++)
    o << s << "("<<i+1<<") = " << (*this)[i] << ";\n";
}
///////////////////////////////////////////////////////////////////////////////
//* writes the vector data to a file
template <typename T>
void Vector<T>::write_restart(FILE *f)                                    const
{
  INDEX size = this->size();
  fwrite(&size, sizeof(INDEX),1,f);
  if (size) fwrite(this->ptr(), sizeof(T), this->size(), f);
}
///////////////////////////////////////////////////////////////////////////////
//* returns the number of columns; always 1
template<typename T>
inline INDEX Vector<T>::nCols()                                           const
{
  return 1;
}
///////////////////////////////////////////////////////////////////////////////
//* returns true if INDEX i is within the range of the vector
template<typename T>
bool Vector<T>::in_range(INDEX i)                                         const
{
  return i<this->size();
}
///////////////////////////////////////////////////////////////////////////////
//* returns true if m has the same number of elements this vector
template<typename T>
bool Vector<T>::same_size(const Vector &m)                                const
{
  return this->size() == m.size();
}
///////////////////////////////////////////////////////////////////////////////
//* returns true if a and b have the same number of elements
template<typename T>
inline bool Vector<T>::same_size(const Vector &a, const Vector &b)
{
  return a.same_size(b);
}
//----------------------------------------------------------------------------
// general matrix assignment (for densely packed matrices)
//----------------------------------------------------------------------------
template<typename T>
void Vector<T>::_set_equal(const Matrix<T> &r)
{
  this->resize(r.nRows(), r.nCols());
  const Matrix<T> *pr = &r;
#ifdef OBSOLETE
  if (const SparseMatrix<T> *ps = dynamic_cast<const SparseMatrix<T>*>(pr))//sparse_cast(pr))
    copy_sparse_to_matrix(ps, *this);

  else if (dynamic_cast<const DiagonalMatrix<T>*>(pr))//diag_cast(pr))  // r is Diagonal?
  {
    this->zero();
    for (INDEX i=0; i<r.size(); i++) (*this)(i,i) = r[i];
  }
  else memcpy(this->ptr(), r.ptr(), r.size()*sizeof(T));
#else
  const Vector<T>         *pv = dynamic_cast<const Vector<T>*>         (pr);
  if (pv)  this->copy(pv->ptr(),pv->nRows());
  else
  {
    std::cout <<"Error in general vector assignment\n";
    exit(1);
  }
#endif
}
} // end namespace


#endif