I'm building a C++ library that includes a Partitions class. I'm trying to implement conjugation (explained below) in place, and I cannot get it to work.
My class members are:
size_t _size;
size_t _length;
std::vector<int> _parts;
As an example, the integer partition [5,4,4,1]
has
_size = 14 // 5 + 4 + 4 + 1
_length = 4 // 4 nonzero parts
_parts[0] = 5
_parts[1] = 4
_parts[2] = 4
_parts[3] = 1
_parts[i] = junk // i>3
If the partition is [m_1,m_2,...,m_k]
, then the conjugate is [n_1,n_2,...,n_l]
where
l = m_1 // length and the first part are switched
n_i = sum{ m_j | m_j > i}
For example, the conjugate of [5,4,4,1]
is [4,3,3,3,1]
. Another way to see this is to draw the partition as rows of unit squares where the number of squares in the i
th row is m_i
. Reading the heights of the columns then gives the conjugate. For the same example, the picture is
1| x
4| x x x x
4| x x x x
5| x x x x x
__________
4 3 3 3 1
The math translated into the programming syntax as m_i = _parts[i-1]
and k = _length
. Here's a broken implementation for conjugation:
void
Partition::conjugate() {
size_t k = _length;
_length = _parts[0];
int newPart;
for (int i=(int)_length; i>0; --i) {
newPart = 0;
for (int j=0; j<k; ++j) {
if (_parts[j] >= i) newPart++;
else break;
}
_parts[i-1] = newPart;
}
}
This works much of the time, but occasionally it overwrites part of the partition that is still needed. I'm looking for a clever way to do the conjugation in place, i.e. without creating a new instance of Partition
.
One other way to think of conjugation is to realize that the conjugate is the following sequence
k...k (k-1)...(k-1) ... 1...1
x m_k x(m_(k-1)-m_k) x(m_1 - m_2)
Using this idea, I have the following implementation that gives the correct answer:
void
Partition::conjugate() {
if (_length == _size) {
this->first();
return;
} else if (_length == 1) {
this->last();
return;
}
std::vector<int> diffs;
diffs.push_back(_parts[_length-1]);
for (size_t i=_length-1; i>0; --i)
diffs.push_back(_parts[i-1]-_parts[i]);
size_t pos = 0;
for (int i=0; i<_length; ++i) {
for (int j = diffs[i]; j>0; --j)
_parts[pos++] = (int)_length - i;
}
_length = pos;
}
However, it uses another std vector, which I am trying to avoid.
In line with Evgeny Kluev's answer (accepted below), here's the final code that works (see his answer for details):
void
Partition::conjugate() {
if (_length == _size) {
this->first();
return;
} else if (_length == 1) {
this->last();
return;
}
int last = _parts[_length-1];
for (int i=1; i<_length; ++i)
_parts[_size-i] = _parts[i-1] - _parts[i];
size_t pos = 0;
for (int i=0; i<last; ++i)
_parts[pos++] = (int)_length;
for (int i=1; i<_length; ++i) {
for (int j = _parts[_size-_length+i]; j>0; --j)
_parts[pos++] = (int)_length - i;
}
_length = pos;
}