15 void swap_rows(std::vector<std::vector<double> > *A,
size_t row1,
size_t row2)
17 for (
size_t col = 0; col < (*A)[0].size(); col++){
18 std::swap((*A)[row1][col],(*A)[row2][col]);
23 for (
size_t col = 0; col < (*A)[0].size(); col++){
24 (*A)[row][col] -= multiple*(*A)[pivot_row][col];
27 void divide_row_by(std::vector<std::vector<double> > *A,
size_t row,
double value)
29 for (
size_t col = 0; col < (*A)[0].size(); col++){
30 (*A)[row][col] /= value;
39 for (
size_t row = col; row < (*A).size(); row++)
52 std::vector<std::vector<double> >
linsolve_Gauss_Jordan(std::vector<std::vector<double> >
const& A, std::vector<std::vector<double> >
const& B) {
53 std::vector<std::vector<double> > AB;
54 std::vector<std::vector<double> > X;
63 if (NrowA!=NrowB)
throw ValueError(
format(
"You have to provide matrices with the same number of rows: %d is not %d. ",NrowA,NrowB));
65 AB.resize(NrowA, std::vector<double>(NcolA+NcolB, 0));
66 X.resize(NrowA, std::vector<double>(NcolB, 0));
69 for (
size_t row = 0; row < NrowA; row++){
70 for (
size_t col = 0; col < NcolA; col++){
71 AB[row][col] = A[row][col];
73 for (
size_t col = NcolA; col < NcolA+NcolB; col++){
74 AB[row][col] = B[row][col-NcolA];
78 for (
size_t col = 0; col < NcolA; col++){
82 if (fabs(AB[pivot_row][col]) < 10*
DBL_EPSILON){
throw ValueError(
format(
"Zero occurred in row %d, the matrix is singular. ",pivot_row));}
89 pivot_element = AB[col][col];
96 for (
size_t row = col + 1; row < NrowA; row++)
102 for (
int col = NcolA - 1; col > 0; col--)
104 for (
int row = col - 1; row >=0; row--)
110 for (
size_t row = 0; row < NrowA; row++){
111 for (
size_t col = 0; col < NcolB; col++){
112 X[row][col] = AB[row][NcolA+col];
185 std::vector<std::vector<double> >
linsolve(std::vector<std::vector<double> >
const& A, std::vector<std::vector<double> >
const& B){
189 std::vector<double>
linsolve(std::vector<std::vector<double> >
const& A, std::vector<double>
const& b){
190 std::vector<std::vector<double> > B;
191 for (
size_t i = 0; i < b.size(); i++){
192 B.push_back(std::vector<double>(1,b[i]));
195 B[0].resize(B.size(),0.0);
196 for (
size_t i = 1; i < B.size(); i++){
204 std::size_t
num_rows (std::vector<std::vector<double> >
const& in){
return in.size(); }
205 std::size_t
num_cols (std::vector<std::vector<double> >
const& in){
216 std::size_t
max_cols (std::vector<std::vector<double> >
const& in){
217 std::size_t cols = 0;
219 for (std::size_t i = 0; i < in.size(); i++) {
221 if (cols<col) {cols = col;}
225 std::vector<double>
get_row(std::vector< std::vector<double> >
const& in,
size_t row) {
return in[row]; }
226 std::vector<double>
get_col(std::vector< std::vector<double> >
const& in,
size_t col) {
227 std::size_t sizeX = in.size();
228 if (sizeX<1)
throw ValueError(
format(
"You have to provide values, a vector length of %d is not valid. ",sizeX));
229 size_t sizeY = in[0].size();
230 if (sizeY<1)
throw ValueError(
format(
"You have to provide values, a vector length of %d is not valid. ",sizeY));
231 std::vector<double> out;
232 for (std::size_t i = 0; i < sizeX; i++) {
233 sizeY = in[i].size();
234 if (sizeY-1<col)
throw ValueError(
format(
"Your matrix does not have enough entries in row %d, last index %d is less than %d. ",i,sizeY-1,col));
235 out.push_back(in[i][col]);
239 bool is_squared(std::vector<std::vector<double> >
const& in){
241 if (cols!=
num_rows(in)) {
return false;}
243 for (std::size_t i = 0; i < in.size(); i++) {
244 if (cols!=in[i].size()) {
return false; }
249 std::vector<std::vector<double> >
make_squared(std::vector<std::vector<double> >
const& in){
252 std::size_t maxVal = 0;
253 std::vector<std::vector<double> > out;
254 std::vector<double> tmp;
256 if (cols>rows) {maxVal = cols; }
257 else {maxVal = rows; }
259 for (std::size_t i = 0; i < in.size(); i++) {
261 for (std::size_t j = 0; j < in[i].size(); j++) {
262 tmp.push_back(in[i][j]);
264 while (maxVal>tmp.size()) {
271 tmp.resize(maxVal,0.0);
272 while (maxVal>out.size()) {
278 double multiply( std::vector<double>
const& a, std::vector<double>
const& b){
282 std::vector<double>
multiply(std::vector<std::vector<double> >
const& A, std::vector<double>
const& b){
283 std::vector<std::vector<double> > B;
284 for (
size_t i = 0; i < b.size(); i++){
285 B.push_back(std::vector<double>(1,b[i]));
288 B[0].resize(B.size(),0.0);
289 for (
size_t i = 1; i < B.size(); i++){
295 std::vector<std::vector<double> >
multiply(std::vector<std::vector<double> >
const& A, std::vector<std::vector<double> >
const& B){
302 std::vector<std::vector<double> > outVec;
303 std::vector<double> tmpVec;
305 for (
size_t i = 0; i < rows; i++){
307 for (
size_t j = 0; j < cols; j++){
309 for (
size_t k = 0; k <
num_cols(A); k++){
310 tmp += A[i][k] * B[k][j];
312 tmpVec.push_back(tmp);
314 outVec.push_back(tmpVec);
319 double dot_product(std::vector<double>
const& a, std::vector<double>
const& b){
320 if (a.size()==b.size()){
321 return std::inner_product(a.begin(), a.end(), b.begin(), 0.0);
323 throw ValueError(
format(
"You have to provide vectors with the same length: %d is not equal to %d. ",a.size(),b.size()));
326 std::vector<double>
cross_product(std::vector<double>
const& a, std::vector<double>
const& b){
330 std::vector< std::vector<double> >
transpose(std::vector<std::vector<double> >
const& in){
331 size_t sizeX = in.size();
332 if (sizeX<1)
throw ValueError(
format(
"You have to provide values, a vector length of %d is not a valid. ",sizeX));
333 size_t sizeY = in[0].size();
334 size_t sizeYOld = sizeY;
335 if (sizeY<1)
throw ValueError(
format(
"You have to provide values, a vector length of %d is not a valid. ",sizeY));
336 std::vector< std::vector<double> > out(sizeY,std::vector<double>(sizeX));
337 for (
size_t i = 0; i < sizeX; ++i){
338 sizeY = in[i].size();
339 if (sizeY!=sizeYOld)
throw ValueError(
format(
"You have to provide a rectangular matrix: %d is not equal to %d. ",sizeY,sizeYOld));
340 for (
size_t j = 0; j < sizeY; ++j){
341 out[j][i] = in[i][j];
347 std::vector< std::vector<double> >
invert(std::vector<std::vector<double> >
const& in){
349 std::vector<std::vector<double> > identity;
352 identity.resize(dim, std::vector<double>(dim, 0));
353 for (
size_t row = 0; row < dim; row++){
354 identity[row][row] = 1.0;
360 std::stringstream out;
361 out <<
format(
"[ %7.3f ]",a);
370 return std::string(
"");
372 std::stringstream out;
375 for (
size_t j = 1; j < a.size(); j++) {
388 std::string
vec_to_string(std::vector<std::vector<double> >
const& A,
const char *fmt) {
389 std::stringstream out;
390 for (
size_t j = 0; j < A.size(); j++) {
std::vector< std::vector< double > > transpose(std::vector< std::vector< double > > const &in)
double multiply(std::vector< double > const &a, std::vector< double > const &b)
Define some basic math operations for vectors.
std::size_t num_cols(std::vector< std::vector< double > > const &in)
double dot_product(std::vector< double > const &a, std::vector< double > const &b)
size_t get_pivot_row(std::vector< std::vector< double > > *A, size_t col)
std::vector< std::vector< double > > linsolve_Gauss_Jordan(std::vector< std::vector< double > > const &A, std::vector< std::vector< double > > const &B)
std::vector< std::vector< double > > make_squared(std::vector< std::vector< double > > const &in)
std::size_t num_rows(std::vector< std::vector< double > > const &in)
Some shortcuts and regularly needed operations.
std::vector< double > cross_product(std::vector< double > const &a, std::vector< double > const &b)
void subtract_row_multiple(std::vector< std::vector< double > > *A, size_t row, double multiple, size_t pivot_row)
std::vector< std::vector< double > > linsolve(std::vector< std::vector< double > > const &A, std::vector< std::vector< double > > const &B)
bool is_squared(std::vector< std::vector< double > > const &in)
std::string vec_to_string(double const &a)
void divide_row_by(std::vector< std::vector< double > > *A, size_t row, double value)
void swap_rows(std::vector< std::vector< double > > *A, size_t row1, size_t row2)
std::vector< double > get_col(std::vector< std::vector< double > > const &in, size_t col)
std::vector< std::vector< double > > invert(std::vector< std::vector< double > > const &in)
std::vector< double > get_row(std::vector< std::vector< double > > const &in, size_t row)
std::size_t max_cols(std::vector< std::vector< double > > const &in)