Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SGVector slicing #4662

Merged
merged 3 commits into from Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/shogun/lib/SGMatrix.cpp
Expand Up @@ -394,7 +394,7 @@ void SGMatrix<T>::create_diagonal_matrix(T* matrix, T* v,int32_t size)
}

template <class T>
SGMatrix<T> SGMatrix<T>::submatrix(index_t col_start, index_t col_end) const
SGMatrix<T> SGMatrix<T>::slice(index_t col_start, index_t col_end) const
{
assert_on_cpu();
return SGMatrix<T>(
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/lib/SGMatrix.h
Expand Up @@ -151,7 +151,7 @@ template<class T> class SGMatrix : public SGReferencedData
* @param col_end column index (excluded)
* @return the submatrix
*/
SGMatrix<T> submatrix(index_t col_start, index_t col_end) const;
SGMatrix<T> slice(index_t col_start, index_t col_end) const;

/** Map a column to a SGVector
* \warning The returned SGVector is non-owning!
Expand Down
9 changes: 9 additions & 0 deletions src/shogun/lib/SGVector.cpp
Expand Up @@ -309,6 +309,15 @@ void SGVector<T>::resize_vector(int32_t n)
vlen=n;
}

template <class T>
SGVector<T> SGVector<T>::slice(index_t l, index_t h) const
{
assert_on_cpu();
ASSERT(l >= 0 && h <= vlen);

return SGVector<T>(vector, h - l, l);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool! much simpler this way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you do reference counting for the returned vector?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reference counting

}

/** addition operator */
template<class T>
SGVector<T> SGVector<T>::operator+ (SGVector<T> x)
Expand Down
7 changes: 7 additions & 0 deletions src/shogun/lib/SGVector.h
Expand Up @@ -270,6 +270,13 @@ template<class T> class SGVector : public SGReferencedData
*/
void resize_vector(int32_t n);

/** Returns a view of the vector from l inclusive to h exclusive
*
* @param l slice start index (inclusive)
* @param h slice end index (exclusive)
*/
SGVector<T> slice(index_t l, index_t h) const;

/** Operator overload for vector read only access
*
* @param index dimension to access
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/multiclass/QDA.cpp
Expand Up @@ -144,7 +144,7 @@ CMulticlassLabels* CQDA::apply_multiclass(CFeatures* data)
rf->free_feature_vector(vec, i);
}

Map<MatrixXd> Em_M(m_M.submatrix(m_dim * k, m_dim * (k + 1)));
Map<MatrixXd> Em_M(m_M.slice(m_dim * k, m_dim * (k + 1)));
A = X*Em_M;

for (int i = 0; i < num_vecs; i++)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/lib/SGMatrix_unittest.cc
Expand Up @@ -636,7 +636,7 @@ TEST(SGMatrixTest, max_single)
EXPECT_GE(max, mat.matrix[i]);
}

TEST(SGMatrixTest, get_submatrix)
TEST(SGMatrixTest, get_slice)
{
const index_t n_rows = 6, n_cols = 8;
const index_t start_col = 2, end_col = 5;
Expand All @@ -646,7 +646,7 @@ TEST(SGMatrixTest, get_submatrix)
for (index_t i = 0; i < n_rows * n_cols; ++i)
mat[i] = CMath::randn_double();

auto sub = mat.submatrix(start_col, end_col);
auto sub = mat.slice(start_col, end_col);

EXPECT_EQ(sub.num_rows, mat.num_rows);
EXPECT_EQ(sub.num_cols, end_col - start_col);
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/lib/SGVector_unittest.cc
Expand Up @@ -468,3 +468,46 @@ TEST(SGVectorTest, as)
EXPECT_EQ((int32_t)data[i], vec_int[i]);
}
}

TEST(SGVectorTest, slice)
{
index_t vlen = 100;
index_t l = 20;
index_t h = 50;
index_t l2 = 10;
index_t h2 = 20;
index_t c1 = 0;
index_t c2 = 1;

SGVector<float64_t> vec(vlen);
vec.range_fill();

auto sliced_vec = vec.slice(l, h);
EXPECT_EQ(sliced_vec.size(), h - l);
for (index_t i = 0; i < h - l; i++)
{
EXPECT_EQ(vec[i + l], sliced_vec[i]);
}

auto sliced_vec2 = sliced_vec.slice(l2, h2);
EXPECT_EQ(sliced_vec2.size(), h2 - l2);
for (index_t i = 0; i < h2 - l2; i++)
{
EXPECT_EQ(sliced_vec[i + l2], sliced_vec2[i]);
}

sliced_vec.set_const(c1);
auto sliced_vec3 = sliced_vec2.slice(0, sliced_vec2.vlen);
EXPECT_EQ(sliced_vec2, sliced_vec3);
sliced_vec3.set_const(c2);

for (index_t i = 0; i < vlen; i++)
{
if (i < l || i >= h)
EXPECT_EQ(vec[i], i);
else if (i < l + l2 || i >= l + h2)
EXPECT_EQ(vec[i], sliced_vec[i - l]);
else
EXPECT_EQ(vec[i], sliced_vec2[i - l - l2]);
}
}