Skip to content

Commit

Permalink
move linalg-dependent function into PCA unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
karlnapf committed Feb 3, 2018
1 parent b05b9d0 commit 289ca74
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 115 deletions.
50 changes: 20 additions & 30 deletions tests/unit/preprocessor/PCA_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2014 Parijat Mazumdar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Parijat Mazumdar, Heiko Strathmann
*/

#include <gtest/gtest.h>
#include <shogun/mathematics/Math.h>
Expand All @@ -37,10 +13,24 @@

#include <shogun/preprocessor/PCA.h>

#include "utils/Utils.h"

using namespace shogun;

/** Check eigenvector equality
* This expects that the input vectors are normalised
*
* @param a first vector
* @param b second vector
* @return 1.0 if the eigen vectors are pointing to the same director, -1.0
* if pointing to the opposite direction.
*/
template<class T>
inline T check_eigenvector_eq(const SGVector<T>& a, const SGVector<T>& b, float64_t epsilon = 10E-8)
{
T sign = linalg::dot(a, b);
EXPECT_NEAR(1.0, CMath::abs(sign), epsilon);
return (sign < 0.0) ? -1.0 : 1.0;
}

TEST(PCA, PCA_N_greater_D_EVD)
{
SGMatrix<float64_t> data(3,5);
Expand Down
37 changes: 4 additions & 33 deletions tests/unit/utils/Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,8 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2017 Giovanni De Toni
*
*/
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Giovanni De Toni, Heiko Strathmann
*/
#include "Utils.h"
#include <algorithm>
#include <cstdlib>
Expand Down
57 changes: 5 additions & 52 deletions tests/unit/utils/Utils.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2017 Giovanni De Toni
*
*/
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Giovanni De Toni, Heiko Strathmann
*/
#ifndef __UTILS_H__
#define __UTILS_H__

#include <shogun/lib/SGMatrix.h>
#include <shogun/lib/SGVector.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>

using namespace shogun;

Expand All @@ -56,21 +26,4 @@ void generate_toy_data_weather(
SGMatrix<float64_t>& data, SGVector<float64_t>& labels,
bool load_train_data = true);

/** Check eigenvector equality
* This expects that the input vectors are normalised
*
* @param gt eigenvector
* @param gt length of the eigenvector
* @param calc_ev calculated eigenvector
* @return 1.0 if the eigen vectors are pointing to the same director, -1.0
* pointing to the opposite direction.
*/
template<class T>
inline T check_eigenvector_eq(const SGVector<T>& a, const SGVector<T>& b, float64_t epsilon = 10E-8)
{
T sign = linalg::dot(a, b);
EXPECT_NEAR(1.0, CMath::abs(sign), epsilon);
return (sign < 0.0) ? -1.0 : 1.0;
}

#endif
#endif //__UTILS_H__

0 comments on commit 289ca74

Please sign in to comment.