Odd results

I have the following three member functions that modifies a 2D grid.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Clarification:
    - m_matrix is of type std::vector<long double>
*/
void Matrix_t::insert_row(size_t pos, size_t delta, ld fill){
    if(pos == Matrix_t::Negative || pos >= this->rows())
        pos = m_matrix.size();
    else pos *= m_columns;
    m_matrix.insert(
        m_matrix.begin()+pos, (delta)*m_columns, fill
    );
}
//These two append rows and columns
void Matrix_t::expand(size_t y, size_t x, ld filler){
    insert_row(Matrix_t::Negative, y, filler);
    insert_column(Matrix_t::Negative, x, filler);
}
void Matrix_t::expand_uniform(size_t xy, ld filler)
    {this->expand(xy, xy, filler);}


When I call the third one, it always adds one less row than specified. However, if I call the first two functions, they work as intended. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Matrix_t m(5,5,0.0); //Construct 5 x 5 matrix filled with 0.0
/*
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
*/
m.insert_row(300, 2, 1.5); //Append two rows
/*
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   1.5 1.5 1.5 1.5 1.5
   1.5 1.5 1.5 1.5 1.5
*/
m.expand(1, 0, 2.4); //Append one row
/*
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   0.0 0.0 0.0 0.0 0.0
   1.5 1.5 1.5 1.5 1.5
   1.5 1.5 1.5 1.5 1.5
   2.4 2.4 2.4 2.4 2.4
*/
m.expand_uniform(2, 9.7); //Append two rows and columns
/*
   0.0 0.0 0.0 0.0 0.0 9.7 9.7
   0.0 0.0 0.0 0.0 0.0 9.7 9.7
   0.0 0.0 0.0 0.0 0.0 9.7 9.7
   0.0 0.0 0.0 0.0 0.0 9.7 9.7
   0.0 0.0 0.0 0.0 0.0 9.7 9.7
   1.5 1.5 1.5 1.5 1.5 9.7 9.7
   1.5 1.5 1.5 1.5 1.5 9.7 9.7
   2.4 2.4 2.4 2.4 2.4 9.7 9.7
   9.7 9.7 9.7 9.7 9.7 9.7 9.7
^^^ Only one row added
*/


I've checked the parameters as they passed, but none of them change up to my call to insert on line 8. I don't really understand why this is happening. My temporary solution is to replace line 18 with:
{this->expand(xy+1, xy, filler);}
But I would like to learn why my code isn't working as intended and hopefully fix it.

Thanks.
I would want to see the implementation of
insert_column()
rows()
and whatever you are using to check the matrix content
Okay, here they are. As far as I know, the other functions are working correctly, but I'm not done testing my methods and their safeguards.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void Matrix_t::insert_column(size_t pos, size_t delta, ld fill){
    if(pos == Matrix_t::Negative || pos > m_columns)
        pos = m_columns;
    for(
        auto iter(m_matrix.begin() + pos);
        iter < m_matrix.end();
        iter += delta+m_columns
    ){
        size_t hold(std::distance(m_matrix.begin(), iter));
        m_matrix.insert(iter, delta, fill);
        iter = m_matrix.begin()+hold;
    }
    m_columns += delta;
}

size_t Matrix_t::rows() const
    {return m_matrix.size()/m_columns;}

ld Matrix_t::get(size_t y, size_t x) const
    {return m_matrix[y*m_columns + x];}

//m_columns is initially determined at construction; of the functions
//   that modify the size of the matrix, only insert_column() contains
//   instructions to change m_columns

//Code used to display the matrix where m is the matrix:
    for(size_t i(0); i < m.rows(); ++i){
        cout << "\t\t";
        for(size_t j(0); j < m.columns(); ++j)
            cout << std::setprecision(3) << std::fixed
                << m.get(i,j) << ' ';
        cout << '\n';
    }
Consider this
1
2
Matrix_t m(1,1,0);
m.insert_column(42,1,3.14); //append one column 
Now look at `insert_column'
`iter' would start at `m_matrix.end()' so you would do nothing, but increase m_columns.

Changing the condition to iter<=m_matrix.end() would work as long as the matrix did have at least one column.
Hah! It solved the issue! I should have known better than to assume insert_column() wouldn't affect the number of rows. I think I'm going to change how m_columns is changed, so it is dependent on the new matrix size. That line also obfuscated another problem when I was testing insert_column().

Was the issue because insert adds the elements one space before the position? And that's why the condition had to be <=?

Thanks for the help ne555.
Topic archived. No new replies allowed.