Vivian
Vivian

Reputation: 217

Initializing constant matrix<double,0,80> (from Dlib) by a pointer

I use Dlib library in my work. http://dlib.net/ I can run this example.( http://dlib.net/optimization_ex.cpp.html)

In this example, there is a function.

typedef matrix<double,0,1> column_vector;
// This is a helper function used while optimizing the rosen() function.  
const column_vector rosen_derivative (const column_vector& m)
/*!
    ensures
        - returns the gradient vector for the rosen function
!*/
{
    const double x = m(0);
    const double y = m(1);

    // make us a column vector of length 2
    column_vector res(2);

    // now compute the gradient vector
    res(0) = -400*x*(y-x*x) - 2*(1-x); // derivative of rosen() with respect to x
    res(1) = 200*(y-x*x);              // derivative of rosen() with respect to y
    return res;
}

I am a beginner of C++. I have problem with typedef matrix<double,0,1> column_vector; I only mimic how to use it. I have a look at how to use `matrix' form the link http://dlib.net/matrix_ex.cpp.html.

The problem is I define typedef matrix<double,0,80> column_vector;. I have,

double * ptr=(double *) ptr(80*sizeof(double)). 

And I initialize ptr[i] (i=0...79) by some complex calculation. But I do not know how to use ptr to initialize column_vector. There are some errors. Errors means column_vector is constant but ptr is not.

For the ease to illustrate my problem, I said I define typedef matrix<double,0,80> column_vector;. Actually, it is a little more complex. The following is my code.

const lbfgs_para form_para_d(  float *  dcof,  float * dtheta)
{
    const lbfgs_para para_d; 
#pragma omp parallel for
    for(long col = 0;col < kernel_num;col++)
        for(long row = 0;row < kernel_num;row++)
        {
            para_d(row+col*kernel_num)=dcof[row*kernel_num+col] ; 
        }
#pragma omp parallel for
        for(int col = 0;col < kernel_num;col++)
        {
            para_d(col+kernel_num*kernel_num)=dtheta[col];
        }
        return para_d;    
}

The error occurs at para_d(row+col*kernel_num)=dcof[row*kernel_num+col] ; and para_d(col+kernel_num*kernel_num)=dtheta[col];. It is error c3892 you cannot assign to a variable that is const.

Upvotes: 0

Views: 929

Answers (1)

jee7s
jee7s

Reputation: 57

The format for Matrix in your library is Matrix. So, to make a column vector of length 80, you need to instantiate a Matrix. So, typedef that as a column_vector, then new it up with something like this:

typedef matrix<double, 80, 1> column_vector;
...
column_vector* myColumnVector = new column_vector;

Upvotes: -1

Related Questions