OverSim
yang.h
Go to the documentation of this file.
1 // classes for vectors and matrices
2 // From D. Yang: C++ and Object-Oriented Numeric Computing,
3 // Springer-Verlag, 2000
4 //
5 #ifndef _YANG_H_
6 #define _YANG_H_
7 
8 
9 #include <iostream>
10 
11 
12 class Mtx;
13 
14 class Vtr {
15  int lenth; // number of entries in the vector
16  double* ets; // entries of the vector
17 public:
18  Vtr(int, double*); // costruct a vector with given entries
19  Vtr(int, double = 0); // a vector with all entries same
20  Vtr(const Vtr&); // copy constructor
21  ~Vtr(){ delete[] ets; } // destructor is defined inline
22 
23  Vtr& operator=(const Vtr&); // copy assignment
24  Vtr& operator+=(const Vtr &); // v += v2
25  Vtr& operator-=(const Vtr &); // v -= v2
26  double& operator[](int i) const { return ets[i]; } // eg v[i] = 10;
27  double maxnorm() const; // maximum norm
28  double twonorm() const; // L-2 norm
29  int size() const { return lenth; } // return length of vector
30 
31  friend Vtr operator+(const Vtr&); // unary +, v = + v2
32  friend Vtr operator-(const Vtr&); // unary -, v = - v2
33  friend Vtr operator+(const Vtr&, const Vtr&); // binary +, v = v1+v2
34  friend Vtr operator-(const Vtr&, const Vtr&); // binary -, v = v1-v2
35  friend double dot(const Vtr&, const Vtr&); // dot product
36  friend Vtr operator*(const double, const Vtr&); // vec-scalar x
37  friend Vtr operator*(const Vtr&, const double); // vec-scalar x
38  friend Vtr operator*(const Vtr&, const Vtr&); // component-wise multiply
39  friend Vtr operator*(const Vtr&, const Mtx&); // vec-matrix x
40  friend Vtr operator/(const Vtr&, const double);
41  friend std::ostream& operator<<(std::ostream&, const Vtr&); // output operator
42 };
43 
44 inline Vtr operator*(const Vtr & v, const double scalar) {
45  return scalar*v;
46 }
47 
48 class Mtx {
49 private:
50  int nrows; // number of rows in the matrix
51  int ncols; // number of columns in matrix
52  double** ets; // entries of the matrix
53 public:
54  Mtx(int n, int m, double**); // construct an n by m matrix
55  Mtx(int n, int m, double d = 0); // all entries equal d
56  Mtx(const Mtx &); // copy constructor
57  ~Mtx(); // destructor
58 
59  Mtx& operator=(const Mtx&); // overload =
60  Mtx& operator+=(const Mtx&); // overload +=
61  Mtx& operator-=(const Mtx&); // overload -=
62  Vtr operator*(const Vtr&) const; // matrix vector multiply
63  double* operator[](int i) const { return ets[i]; } // subscripting, row i
64 
65  // Implement plus as a member fcn. minus could also be so implemented.
66  // But I choose to implement minus as a friend just to compare the difference
67  Mtx& operator+(); // unary +, eg, mat1 = + mat2
68  Mtx operator+(const Mtx&); // binary +, eg, m = m1 + m2
69  friend Mtx operator-(const Mtx&); // unary -, eg, m1 = -m2
70  friend Mtx operator-(const Mtx&,const Mtx&); // binary -, eg, m = m1 - m2
71 
72  // Added by Peter Seidler...
73  int rows() const {return nrows;}
74  int cols() const {return ncols;}
75  void getcol(int i, Vtr& vec) const;
76  void setcol(int i, const Vtr& vec);
77  void clear();
78  int transpose(Mtx& dest) const;
79  Mtx operator*(const Mtx&) const;
80  friend Vtr operator*(const Vtr&, const Mtx&); // vec-matrix x
81  const double& operator()(int i, int j) const {return ets[i][j];}
82  double& operator()(int i, int j) {return ets[i][j];}
83  friend std::ostream& operator<<(std::ostream&, const Mtx&);
84 
85  int QRdecomp(Mtx& Q, Mtx& R);
86  int QRdecomp_slow(Mtx& Q, Mtx& R);
87 };
88 
89 typedef Vtr Vec_DP;
90 typedef Mtx Mat_DP;
91 
92 #endif // _YANG_H_