[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-gsl] gsl_matrix *p Pointer in Object
From: |
Thomas Schmitt |
Subject: |
Re: [Help-gsl] gsl_matrix *p Pointer in Object |
Date: |
Tue, 21 Jul 2009 22:00:24 +0200 |
Hi,
the gsl_matrix_view makes sense because i want to parse from a vector
that i read from a csv-file. Every instance of the class gets an
different vector. I attach the complete code.....and again thanks very
much for your help!!!!
/*
* GaussMatrix.h
*
* Created on: 17.07.2009
* Author: freak
*/
#ifndef GAUSSMATRIX_H_
#define GAUSSMATRIX_H_
#include <gsl/gsl_matrix.h>
#include <vector>
namespace std {
class GaussMatrix:public gsl_matrix {
private:
double * _array;
void ProcessArray ( const double * , size_t numDoubles ) ;
unsigned int _rows,_columns, k, l;
public:
gsl_matrix * p_matrix;
GaussMatrix(vector<double> init_vector, unsigned int rows, unsigned int
columns );
void print_matrix();
//virtual ~GaussMatrix();
};
}
#endif /* GAUSSMATRIX_H_ */
/*
* GaussMatrix.cpp
*
* Created on: 17.07.2009
* Author: freak
*/
#include "GaussMatrix.h"
#include <iostream>
#include <vector>
#include <string>
namespace std {
GaussMatrix::GaussMatrix(vector<double> init_vector, unsigned int rows,
unsigned int columns) {
cout << init_vector[0] << endl;
_rows = rows;_columns=columns;
ProcessArray ( &init_vector[0], init_vector.size() );
cout << init_vector[0] << endl;
_array = &init_vector[0];
gsl_matrix_view a = gsl_matrix_view_array( _array, _rows, _columns );
GaussMatrix::p_matrix = gsl_matrix_calloc(rows,columns);
GaussMatrix::p_matrix = &a.matrix;
cout << "pointer value:" << p_matrix << endl;
for (int i = 0; i < 2; i++) /* OUT OF RANGE ERROR */
for (int j = 0; j < 2; j++)
printf ("n(%d,%d) = %g\n", i, j,
gsl_matrix_get (p_matrix, i, j));
}
//GaussMatrix::~GaussMatrix() {
// TODO Auto-generated destructor stub
//}
void GaussMatrix::ProcessArray ( const double * , size_t numDoubles )
{};
void GaussMatrix::print_matrix(){
for (int i = 0; i < 2; i++) /* OUT OF RANGE ERROR */
for (int j = 0; j < 2; j++)
printf ("m(%d,%d) = %g\n", i, j,
gsl_matrix_get ( p_matrix, i, j));
}
}
and the main:
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_linalg.h>
#include "CalcPara.h"
#include <gsl/gsl_blas.h>
#include "CsvAutomation/CsvtoArray.h"
#include "GaussMatrix.h"
using namespace std;
int main() {
fstream infile("input.csv",ios::in);
vector<double> p_values;
CsvtoArray toarray;
/*
* This method just generates a vector from the read values
*/
toarray.csv_getArray_byName( infile,"P =", 1 , p_values);
GaussMatrix gaus(p_values, 2, 2);
gaus.print_matrix();
return 0;
}
Am Dienstag, den 21.07.2009, 21:43 +0200 schrieb Frank Reininghaus:
> Hi,
>
> On Tuesday 21 July 2009 21:12:25 Thomas Schmitt wrote:
> > you're right, the calloc doesn't make sense!! But the line
> > GaussMatrix::p_matrix = &a.matrix;
> > should give me a pointer to the view i create.....but if i try to call
> > the pointer later in a function of the class(it just prints out the
> > matrix)
> > i get different values than the ones i initialized.
>
> the calloc made more sense to me than this very strange thing you're doing
> with the gsl_matrix_view and the vector<double> in the class constructor. If
> you pass the same "init_vector" to the constructor of each class instance,
> all
> instances will share the same matrix data, so manipulations of one instance
> will also affect the others.
>
> But again, it's impossible to fully analyse your problem without the code.
>
> Cheers
> Frank