rsvs3D  0.0.0
Codes for the c++ implementation of the 3D RSVS
matrixtools.cpp
1 #include "matrixtools.hpp"
2 
3 #include <Eigen>
4 #include <fstream>
5 #include <iomanip>
6 #include <iostream>
7 #include <vector>
8 
9 #include "vectorarray.hpp"
10 #include "warning.hpp"
11 // #include "matrixtools.hpp"
12 
13 using namespace std;
14 using namespace Eigen;
15 
16 void ArrayVec2MatrixXd(const ArrayVec<double> &arrayIn, MatrixXd &matOut)
17 {
18  int nR = 0, nC = 0;
19  arrayIn.size(nR, nC);
20  matOut.setZero(nR, nC);
21 
22  for (int i = 0; i < nR; ++i)
23  {
24  for (int j = 0; j < nC; ++j)
25  {
26  matOut(i, j) = arrayIn[i][j];
27  }
28  }
29 }
30 
31 void VecBy3DimArray(const MatrixXd &vec, const MatrixXd &arr3dim, MatrixXd &retArray)
32 {
33  /*
34  3 Dimensional array is expected as
35  xc
36  yc zc x0 x1 xn y0
37  y1 yn z0 z1 zn x0 x1 xn y0 y1 yn z0 z1 zn x0 x1 xn y0 y1 yn
38  z0 z1 zn x0 [ ] [ ] [ ] x1 [ ] [ ] [ ] xn [
39  ] [ ] [ ] y0 [ ]
40  [ ] [ ] y1 [ ] [
41  ] [ ] yn [ ] [ ]
42  [ ] z0 [ ] [ ] [ ]
43  z1 [ ] [ ] [ ] zn
44  [ ] [ ] [ ] vector
45  expected as a row vector [xc yc zc]
46 
47  will sum the three Hessians according to the values in the vector
48  */
49 
50  int nRow, nCol, nVec, nColFin;
51  int ii, jj, kk;
52  nRow = arr3dim.rows();
53  nCol = arr3dim.cols();
54  nVec = vec.cols();
55  nColFin = nCol / nVec;
56 
57 #ifdef SAFE_ALGO
58  // size checks
59  if ((nVec * nColFin) != nCol)
60  {
61  std::cerr << std::endl << "nVec (" << nVec << ") ; nColFin (" << nColFin << ") ; nCol (" << nCol << ")";
62  RSVS3D_ERROR_ARGUMENT("Sizes do not match in 3D array collapse");
63  }
64 #endif
65 
66  retArray.setZero(nRow, nColFin);
67  // needs to add checks for matching sizes
68  for (ii = 0; ii < nRow; ii++)
69  {
70  for (jj = 0; jj < nColFin; jj++)
71  {
72  for (kk = 0; kk < nVec; kk++)
73  {
74  retArray(ii, jj) += arr3dim(ii, jj + kk * nColFin) * vec(0, kk);
75  }
76  }
77  }
78 }
79 
80 void Deriv1stChainScalar(const MatrixXd &dSdc, const MatrixXd &dcdd, MatrixXd &dSdd)
81 {
82  dSdd = dSdc * dcdd;
83 }
84 
85 void Deriv2ndChainScalar(const MatrixXd &dSdc, const MatrixXd &dcdd, const MatrixXd &HSc, const MatrixXd &Hcd,
86  MatrixXd &HSd)
87 {
88  VecBy3DimArray(dSdc, Hcd, HSd);
89 #ifdef SAFE_ALGO
90  bool errFlag = false;
91  errFlag |= dcdd.rows() != HSc.cols();
92  errFlag |= dcdd.cols() != HSd.cols();
93  errFlag |= HSd.rows() != HSd.cols();
94  if (errFlag)
95  {
96  std::cerr << std::endl << "dSdc: [" << dSdc.rows() << ", " << dSdc.cols() << "]" << std::endl;
97  PrintMatrix(dSdc);
98  std::cerr << std::endl << "dcdd: [" << dcdd.rows() << ", " << dcdd.cols() << "]" << std::endl;
99  PrintMatrix(dcdd);
100  std::cerr << std::endl << "HSc: [" << HSc.rows() << ", " << HSc.cols() << "]" << std::endl;
101  PrintMatrix(HSc);
102  std::cerr << std::endl << "HSd: [" << HSd.rows() << ", " << HSd.cols() << "]" << std::endl;
103  PrintMatrix(HSd);
104 
105  std::cerr << std::endl << "dSdc: [" << dSdc.rows() << ", " << dSdc.cols() << "]";
106  std::cerr << std::endl << "dcdd: [" << dcdd.rows() << ", " << dcdd.cols() << "]";
107  std::cerr << std::endl << "HSc: [" << HSc.rows() << ", " << HSc.cols() << "]";
108  std::cerr << std::endl << "HSd: [" << HSd.rows() << ", " << HSd.cols() << "]" << std::endl;
109 
110  RSVS3D_ERROR_LOGIC("Matrix sizes do not match");
111  }
112 #endif
113 
114  HSd = HSd + (dcdd.transpose() * HSc * dcdd);
115 }
116 
117 void PrintMatrix(const MatrixXd &mat)
118 {
119  int ii, jj, ni, nj;
120 
121  ni = mat.rows();
122  nj = mat.cols();
123  for (ii = 0; ii < ni; ++ii)
124  {
125  for (jj = 0; jj < nj; ++jj)
126  {
127  cout << mat(ii, jj) << " ";
128  }
129  cout << endl;
130  }
131 }
132 void PrintMatrixFile(const MatrixXd &mat, const char *name)
133 {
134  ofstream myfile;
135  myfile.open(name, ios::app);
136  PrintMatrixFile(mat, myfile);
137  myfile.close();
138 }
139 
140 void PrintMatrixFile(const MatrixXd &mat, std::ostream &myfile)
141 {
142  int ii, jj, ni, nj;
143 
144  ni = mat.rows();
145  nj = mat.cols();
146 
147  for (ii = 0; ii < ni; ++ii)
148  {
149  for (jj = 0; jj < nj; ++jj)
150  {
151  myfile << mat(ii, jj) << " ";
152  }
153  myfile << endl;
154  }
155  myfile << endl;
156 }
157 
158 void PrintMatrix(const VectorXd &mat)
159 {
160  int ii, ni;
161 
162  ni = mat.size();
163  for (ii = 0; ii < ni; ++ii)
164  {
165  cout << mat[ii] << " ";
166  cout << endl;
167  }
168 }
169 void PrintMatrix(const RowVectorXd &mat)
170 {
171  int ii, ni;
172 
173  ni = mat.size();
174  for (ii = 0; ii < ni; ++ii)
175  {
176  cout << mat[ii] << " ";
177  }
178  cout << endl;
179 }
180 
181 double StreamStatistics(const VectorXd &&vec, ostream &out, const string &&sep)
182 {
183  /*
184  Uses a rvalue reference to allow operations to be passed
185  directly in the statistics
186  */
187  double norm = vec.norm();
188  out << norm << sep;
189  if (vec.size() > 0)
190  {
191  out << std::setw(out.precision() + 7) << vec.mean() << sep;
192  out << std::setw(out.precision() + 7) << sqrt((vec.array() - vec.mean()).square().sum() / (vec.size() - 1))
193  << sep;
194  out << std::setw(out.precision() + 7) << vec.maxCoeff() << sep;
195  out << std::setw(out.precision() + 7) << vec.minCoeff() << sep;
196  }
197  out << endl;
198  return (norm);
199 }
200 
201 void StreamOutVector(const VectorXd &&vec, std::ostream &out, const string &&sep)
202 {
203  /*
204  Uses a rvalue reference to allow operations to be passed
205  directly in the statistics
206  */
207  int i, n;
208  n = vec.size();
209  for (i = 0; i < n; ++i)
210  {
211  out << vec[i] << sep;
212  }
213  out << endl;
214 }
Tools to support conversion, display and derivatives of Eigen matrices.
Provides a 2D std::vector based container.
Provides the error and warning system used by the RSVS3D project.
#define RSVS3D_ERROR_LOGIC(M)
Throw a logic_error.
Definition: warning.hpp:139
#define RSVS3D_ERROR_ARGUMENT(M)
Throw a invalid_argument.
Definition: warning.hpp:148