Skip to content

Commit

Permalink
Use random vectors instead of predefined ones
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Jul 23, 2012
1 parent a120686 commit ed46678
Showing 1 changed file with 79 additions and 62 deletions.
141 changes: 79 additions & 62 deletions tests/unit/lib/SGVector_unittest.cc
Expand Up @@ -4,117 +4,134 @@

using namespace shogun;

static float64_t a_sample[] = {0,1,2,3,4,5,6,7,8,9};

TEST(SGVectorTest,ctor)
{
SGVector<float64_t> a(10);
EXPECT_EQ(a.vlen, 10);

a.zero();
for (int i=0; i < 10; ++i)
{
EXPECT_EQ(0, a[i]);
}

a.set_const(3.3);
for (int i=0; i < 10; ++i)
{
EXPECT_EQ(3.3, a[i]);
}

float64_t* a_clone = SGVector<float64_t>::clone_vector(a_sample, 10);
float64_t* a_clone = SGVector<float64_t>::clone_vector(a.vector, a.vlen);
for (int i=0; i < 10; ++i)
{
EXPECT_EQ(a_clone[i], a_sample[i]);
}
EXPECT_EQ(a_clone[i], a[i]);

SGVector<float64_t> b(a_clone, 10);
EXPECT_EQ(b.vlen, 10);
for (int i=0; i < 10; ++i)
{
EXPECT_EQ(b[i], a_sample[i]);
}
EXPECT_EQ(b[i], a[i]);

/* test copy ctor */
SGVector<float64_t> c(b);
EXPECT_EQ(c.vlen, b.vlen);
for (int i=0; i < c.vlen; ++i)
EXPECT_EQ(b[i], c[i]);

}

TEST(SGVectorTest,add)
{
float64_t* a_clone = SGVector<float64_t>::clone_vector(a_sample, 10);
SGVector<float64_t> a(a_clone, 10);
SGVector<float64_t> a(10);
SGVector<float64_t> b(10);
b.zero();
a.random(0.0, 1024.0);
b.random(0.0, 1024.0);
float64_t* b_clone = SGVector<float64_t>::clone_vector(b.vector, b.vlen);
SGVector<float64_t> c(b_clone, 10);

a.add(b);
for (int i=0; i < 10; ++i)
{
EXPECT_EQ(a[i], a_sample[i]);
}
c.add(a);
for (int i=0; i < c.vlen; ++i)
EXPECT_EQ(c[i], a[i]+b[i]);

SGVector<float64_t> c = a + a;
c = a + a;
EXPECT_EQ(c.vlen, 10);
for (int i=0; i < 10; ++i)
{
EXPECT_EQ(c[i], 2*a_sample[i]);
}
for (int i=0; i < c.vlen; ++i)
EXPECT_EQ(c[i], 2*a[i]);
}

TEST(SGVectorTest,dot)
{
float64_t* a_clone = SGVector<float64_t>::clone_vector(a_sample, 10);
SGVector<float64_t> a(a_clone, 10);
SGVector<float64_t> a(10);
a.random(0.0, 1024.0);
float64_t dot_val = 0.0;

EXPECT_EQ(285, a.dot(a.vector,a.vector, a.vlen));
for (int32_t i = 0; i < a.vlen; ++i)
dot_val += a[i]*a[i];

float64_t error = CMath::abs (dot_val - a.dot(a.vector,a.vector, a.vlen));
EXPECT_TRUE(error < 10E-10);
}

TEST(SGVectorTest,norm)
{
EXPECT_EQ(CMath::sqrt(285.0),SGVector<float64_t>::twonorm(a_sample, 10));
SGVector<float64_t> a(10);
a.random(-50.0, 1024.0);

EXPECT_EQ(45,SGVector<float64_t>::onenorm(a_sample, 10));
/* check l-2 norm */
float64_t l2_norm = CMath::sqrt(a.dot(a.vector,a.vector, a.vlen));
float64_t error = CMath::abs(l2_norm - SGVector<float64_t>::twonorm(a.vector, a.vlen));
EXPECT_TRUE(error < 10E-12);

float64_t l1_norm = 0.0;
for (int32_t i = 0; i < a.vlen; ++i)
l1_norm += CMath::abs(a[i]);
EXPECT_EQ(l1_norm, SGVector<float64_t>::onenorm(a.vector, a.vlen));

SGVector<float64_t> b(10);
b.set_const(1.0);
EXPECT_EQ(10.0,SGVector<float64_t>::qsq(b.vector, 10, 0.5));
EXPECT_EQ(10.0,SGVector<float64_t>::qsq(b.vector, b.vlen, 0.5));

EXPECT_EQ(100,SGVector<float64_t>::qnorm(b.vector, 10, 0.5));
EXPECT_EQ(100,SGVector<float64_t>::qnorm(b.vector, b.vlen, 0.5));
}

TEST(SGVectorTest,misc)
{
EXPECT_EQ(0.0, SGVector<float64_t>::min(a_sample,10));
EXPECT_EQ(9.0, SGVector<float64_t>::max(a_sample,10));
EXPECT_EQ(45.0, SGVector<float64_t>::sum(a_sample,10));

float64_t* a_clone = SGVector<float64_t>::clone_vector(a_sample, 10);
SGVector<float64_t> a(a_clone, 10);
SGVector<float64_t> c(10);
SGVector<float64_t>::vector_multiply(c.vector, a.vector, a.vector, 10);
for (int32_t i = 0; i < a.vlen; ++i)
{
EXPECT_EQ(c[i],a_sample[i]*a_sample[i]);
}

SGVector<float64_t>::add(c.vector, 1.5, a.vector, 1.3, a.vector, 10);
SGVector<float64_t> a(10);
a.random(-1024.0, 1024.0);

/* test, min, max, sum */
float64_t min = 1025, max = -1025, sum = 0.0, sum_abs = 0.0;
for (int32_t i = 0; i < a.vlen; ++i)
{
EXPECT_EQ(c[i],1.5*a_sample[i]+1.3*a_sample[i]);
sum += a[i];
sum_abs += CMath::abs(a[i]);
if (a[i] > max)
max = a[i];
if (a[i] < min)
min = a[i];
}

EXPECT_EQ(min, SGVector<float64_t>::min(a.vector,a.vlen));
EXPECT_EQ(max, SGVector<float64_t>::max(a.vector,a.vlen));
EXPECT_EQ(sum, SGVector<float64_t>::sum(a.vector,a.vlen));
EXPECT_EQ(sum_abs, SGVector<float64_t>::sum_abs(a.vector, a.vlen));

SGVector<float64_t>::scale_vector(-1.0,a.vector,a.vlen);
EXPECT_EQ(45.0, SGVector<float64_t>::sum_abs(a.vector,a.vlen));
/* test ::vector_multiply(...) */
SGVector<float64_t> c(10);
SGVector<float64_t>::vector_multiply(c.vector, a.vector, a.vector, a.vlen);
for (int32_t i = 0; i < c.vlen; ++i)
EXPECT_EQ(c[i], a[i]*a[i]);

SGVector<float64_t>::scale_vector(-1.0,a.vector,a.vlen);
SGVector<float64_t>::add_scalar(1.1, a.vector, a.vlen);
/* test ::add(...) */
SGVector<float64_t>::add(c.vector, 1.5, a.vector, 1.3, a.vector, a.vlen);
for (int32_t i = 0; i < a.vlen; ++i)
{
EXPECT_EQ(a[i],a_sample[i]+1.1);
}
EXPECT_EQ(c[i],1.5*a[i]+1.3*a[i]);

float64_t* b_clone = SGVector<float64_t>::clone_vector(a_sample, 10);
SGVector<float64_t> b(b_clone,10);
SGVector<float64_t>::vec1_plus_scalar_times_vec2(b.vector, 1.3, b.vector, 10);
for (int32_t i = 0; i < a.vlen; ++i)
{
EXPECT_EQ(b[i],a_sample[i]+1.3*a_sample[i]);
}
/* tests ::add_scalar */
SGVector<float64_t>::scale_vector(-1.0,a.vector, a.vlen);
float64_t* a_clone = SGVector<float64_t>::clone_vector(a.vector, a.vlen);
SGVector<float64_t> b(a_clone, 10);
SGVector<float64_t>::add_scalar(1.1, b.vector, b.vlen);
for (int32_t i = 0; i < b.vlen; ++i)
EXPECT_EQ(b[i],a[i]+1.1);

float64_t* b_clone = SGVector<float64_t>::clone_vector(b.vector, b.vlen);
SGVector<float64_t> d(b_clone, b.vlen);
SGVector<float64_t>::vec1_plus_scalar_times_vec2(d.vector, 1.3, d.vector, b.vlen);
for (int32_t i = 0; i < d.vlen; ++i)
EXPECT_EQ(d[i],b[i]+1.3*b[i]);
}

0 comments on commit ed46678

Please sign in to comment.