Ravelin
Ops.h
1 class OPS
3 {
4  public:
5  static bool rel_equal(REAL x, REAL y);
6  static bool rel_equal(REAL x, REAL y, REAL tol);
7 
9  template <class V1, class V2>
10  static V2& axpy(REAL alpha, const V1& v1, V2& v2)
11  {
12  if (v1.size() != v2.size())
13  throw MissizeException();
14 
15  CBLAS::axpy(v1.size(), v1.data(), v1.inc(), v2.data(), v2.inc());
16  return v2;
17  }
18 
20  template <class M1, class M2>
21  static M2& transpose(const M1& m1, M2& m2)
22  {
23  // resize m2
24  m2.resize(m1.columns(), m1.rows());
25 
26  // setup necessary constants
27  const unsigned LD1 = m1.leading_dim();
28  const unsigned LD2 = m2.leading_dim();
29  const REAL* d1 = m1.data();
30  REAL* d2 = m2.data();
31 
32  // do the transposition
33  for (unsigned i=0, i1=0; i< m1.rows(); i++)
34  {
35  CBLAS::copy(m1.columns(), d1+i, LD1, d2+i1, 1);
36  i1 += LD2;
37  }
38 
39  return m2;
40  }
41 
43  template <class T, class U, class V>
44  static V& mult_transpose(const T& x, const U& y, V& z)
45  {
46  // verify that we can multiply these
47  if (x.columns() != y.columns())
48  throw MissizeException();
49 
50  // resize the result
51  z.resize(x.rows(), y.rows());
52 
53  // do the multiplication
54  CBLAS::gemm(CblasNoTrans, CblasTrans, x.rows(), y.rows(), x.columns(), 1.0, x.data(), x.leading_dim(), y.data(), y.leading_dim(), 0.0, z.data(), z.leading_dim);
55 
56  return z;
57  }
58 
60  template <class T, class U, class V>
61  static V& mult(const T& x, const U& y, V& z)
62  {
63  // verify that we can multiply these
64  if (x.columns() != y.rows())
65  throw MissizeException();
66 
67  // resize the result
68  z.resize(x.rows(), y.columns());
69 
70  // do the multiplication
71  if (y.columns() > 1)
72  // matrix
73  CBLAS::gemm(CblasNoTrans, CblasNoTrans, x.rows(), y.columns(), x.columns(), 1.0, x.data(), x.leading_dim(), y.data(), y.leading_dim(), 0.0, z.data(), z.leading_dim);
74  else
75  // vector
76  CBLAS::gemv(CblasColMajor, CblasNoTrans, x.rows(), x.columns(), 1.0, x.data(), x.leading_dim(), y.data(), y.inc(), 0.0, z.data(), z.inc());
77 
78  return z;
79  }
80 
82  template <class T, class U, class V>
83  static V& transpose_mult(const T& x, const U& y, V& z)
84  {
85  // verify that we can multiply these
86  if (x.rows() != y.rows())
87  throw MissizeException();
88 
89  // resize the result
90  z.resize(x.columns(), y.columns());
91 
92  // multiply
93  CBLAS::gemm(CblasTrans, CblasNoTrans, x.columns(), y.columns(), x.rows(), 1.0, x.data(), x.leading_dim(), y.data(), y.leading_dim(), 0.0, z.data(), z.leading_dim);
94 
95  return z;
96  }
97 
99  template <class T, class U, class V>
100  static V& transpose_mult_transpose(const T& x, const U& y, V& z)
101  {
102  // verify that we can multiply these
103  if (x.rows() != y.columns())
104  throw MissizeException();
105 
106  // resize the result
107  z.resize(x.columns(), y.rows());
108 
109  // do the multiplication
110  CBLAS::gemm(CblasTrans, CblasTrans, x.columns(), y.rows(), x.rows(), 1.0, x.data(), x.leading_dim(), y.data(), y.leading_dim(), 0.0, z.data(), z.leading_dim);
111 
112  return z;
113  }
114 
115  template <class U, class V, class M>
116  static M& outer_prod(const U& x, const V& y, M& z)
117  {
118  // make sure both are vectors
119  if (x.columns() != 1 || y.columns() != 1)
120  throw MissizeException();
121 
122  // get n and m
123  unsigned m = x.rows();
124  unsigned n = y.rows();
125 
126  // resize the matrix and set to zero
127  z.set_zero(m,n);
128 
129  // call BLAS outer product routine
130  if (m > 0 && n > 0)
131  CBLAS::ger(CblasColMajor, m, n, x.data(), x.inc(), y.data(), y.inc(), z.data(), z.leading_dim());
132 
133  return z;
134  }
135 
136 }; // end class
137 
static V & transpose_mult_transpose(const T &x, const U &y, V &z)
Multiples a matrix by a matrix or vector.
Definition: Ops.h:100
Class for general operations.
Definition: Ops.h:2
static V & transpose_mult(const T &x, const U &y, V &z)
Multiples the transpose of a matrix or vector by a matrix or vector.
Definition: Ops.h:83
static M2 & transpose(const M1 &m1, M2 &m2)
Does a transposition operation.
Definition: Ops.h:21
static V & mult(const T &x, const U &y, V &z)
Multiples a matrix by a matrix or vector.
Definition: Ops.h:61
static V & mult_transpose(const T &x, const U &y, V &z)
Multiples a matrix by the transpose of a matrix or vector.
Definition: Ops.h:44
static V2 & axpy(REAL alpha, const V1 &v1, V2 &v2)
Does a axpy operation.
Definition: Ops.h:10