Skip to content

Commit

Permalink
change reflection to perturbation
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Aug 22, 2019
1 parent fb43152 commit e6be612
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 66 deletions.
2 changes: 1 addition & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ target_sources(ginkgo
base/composition.cpp
base/executor.cpp
base/mtx_io.cpp
base/reflection.cpp
base/perturbation.cpp
base/version.cpp
factorization/par_ilu.cpp
log/convergence.cpp
Expand Down
12 changes: 6 additions & 6 deletions core/base/reflection.cpp → core/base/perturbation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <ginkgo/core/base/reflection.hpp>
#include <ginkgo/core/base/perturbation.hpp>


#include <ginkgo/core/matrix/dense.hpp>
Expand All @@ -40,7 +40,7 @@ namespace gko {


template <typename ValueType>
void Reflection<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
void Perturbation<ValueType>::apply_impl(const LinOp *b, LinOp *x) const
{
// x = (I + scaler * basis * projector) * b
// temp = projector * b : projector->apply(b, temp)
Expand All @@ -58,8 +58,8 @@ void Reflection<ValueType>::apply_impl(const LinOp *b, LinOp *x) const


template <typename ValueType>
void Reflection<ValueType>::apply_impl(const LinOp *alpha, const LinOp *b,
const LinOp *beta, LinOp *x) const
void Perturbation<ValueType>::apply_impl(const LinOp *alpha, const LinOp *b,
const LinOp *beta, LinOp *x) const
{
// x = alpha * (I + scaler * basis * projector) b + beta * x
// = beta * x + alpha * b + alpha * scaler * basis * projector * b
Expand All @@ -84,8 +84,8 @@ void Reflection<ValueType>::apply_impl(const LinOp *alpha, const LinOp *b,
}


#define GKO_DECLARE_REFLECTION(_type) class Reflection<_type>
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_REFLECTION);
#define GKO_DECLARE_PERTURBATION(_type) class Perturbation<_type>
GKO_INSTANTIATE_FOR_EACH_VALUE_TYPE(GKO_DECLARE_PERTURBATION);


} // namespace gko
2 changes: 1 addition & 1 deletion core/test/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ ginkgo_create_test(iterator_factory)
ginkgo_create_test(lin_op)
ginkgo_create_test(matrix_data)
ginkgo_create_test(mtx_io)
ginkgo_create_test(perturbation)
ginkgo_create_test(polymorphic_object)
ginkgo_create_test(range)
ginkgo_create_test(range_accessors)
ginkgo_create_test(reflection)
ginkgo_create_test(types)
ginkgo_create_test(utils)
ginkgo_create_test(version)
22 changes: 11 additions & 11 deletions core/test/base/reflection.cpp → core/test/base/perturbation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <ginkgo/core/base/reflection.hpp>
#include <ginkgo/core/base/perturbation.hpp>


#include <memory>
Expand Down Expand Up @@ -86,9 +86,9 @@ struct TransposableDummyOperator
};


class Reflection : public ::testing::Test {
class Perturbation : public ::testing::Test {
protected:
Reflection()
Perturbation()
: exec{gko::ReferenceExecutor::create()},
basis{std::make_shared<DummyOperator>(exec, gko::dim<2>{2, 1})},
projector{std::make_shared<DummyOperator>(exec, gko::dim<2>{1, 2})},
Expand All @@ -105,33 +105,33 @@ class Reflection : public ::testing::Test {
};


TEST_F(Reflection, CanBeEmpty)
TEST_F(Perturbation, CanBeEmpty)
{
auto cmp = gko::Reflection<>::create(exec);
auto cmp = gko::Perturbation<>::create(exec);

ASSERT_EQ(cmp->get_size(), gko::dim<2>(0, 0));
}


TEST_F(Reflection, CanCreateFromTwoOperators)
TEST_F(Perturbation, CanCreateFromTwoOperators)
{
auto cmp = gko::Reflection<>::create(scaler, basis, projector);
auto cmp = gko::Perturbation<>::create(scaler, basis, projector);

ASSERT_EQ(cmp->get_size(), gko::dim<2>(2, 2));
ASSERT_EQ(cmp->get_basis(), basis);
ASSERT_EQ(cmp->get_projector(), projector);
}


TEST_F(Reflection, CannotCreateFromOneNonTransposableOperator)
TEST_F(Perturbation, CannotCreateFromOneNonTransposableOperator)
{
ASSERT_THROW(gko::Reflection<>::create(scaler, basis), gko::NotSupported);
ASSERT_THROW(gko::Perturbation<>::create(scaler, basis), gko::NotSupported);
}


TEST_F(Reflection, CanCreateFromOneTranposableOperator)
TEST_F(Perturbation, CanCreateFromOneTranposableOperator)
{
auto cmp = gko::Reflection<>::create(scaler, trans_basis);
auto cmp = gko::Perturbation<>::create(scaler, trans_basis);

ASSERT_EQ(cmp->get_size(), gko::dim<2>(3, 3));
ASSERT_EQ(cmp->get_basis(), trans_basis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#ifndef GKO_CORE_BASE_REFLECTION_HPP_
#define GKO_CORE_BASE_REFLECTION_HPP_
#ifndef GKO_CORE_BASE_PERTURBATION_HPP_
#define GKO_CORE_BASE_PERTURBATION_HPP_


#include <memory>
Expand All @@ -44,11 +44,11 @@ namespace gko {


/**
* The Reflection class can be used to construct a LinOp to represent the
* The Perturbation class can be used to construct a LinOp to represent the
* `(identity + scaler * basis * projector)` This operator adds a movement along
* a direction construted by `basis` and `projector` on the LinOp. `projector`
* gives the coefficient of `basis` to decide the direction.
* For example, Householder matrix can be represented in Reflection.
* For example, Householder matrix can be represented in Perturbation.
* Householder matrix = (I - 2 u u*), u is the housholder factor
* scaler = -2, basis = u, and projector = u*
*
Expand All @@ -57,16 +57,16 @@ namespace gko {
* @ingroup LinOp
*/
template <typename ValueType = default_precision>
class Reflection : public EnableLinOp<Reflection<ValueType>>,
public EnableCreateMethod<Reflection<ValueType>> {
friend class EnablePolymorphicObject<Reflection, LinOp>;
friend class EnableCreateMethod<Reflection>;
class Perturbation : public EnableLinOp<Perturbation<ValueType>>,
public EnableCreateMethod<Perturbation<ValueType>> {
friend class EnablePolymorphicObject<Perturbation, LinOp>;
friend class EnableCreateMethod<Perturbation>;

public:
using value_type = ValueType;

/**
* Returns the basis of the reflection.
* Returns the basis of the perturbation.
*
* @return the basis
*/
Expand All @@ -76,7 +76,7 @@ class Reflection : public EnableLinOp<Reflection<ValueType>>,
}

/**
* Returns the projector of the reflection.
* Returns the projector of the perturbation.
*
* @return the projector
*/
Expand All @@ -86,7 +86,7 @@ class Reflection : public EnableLinOp<Reflection<ValueType>>,
}

/**
* Returns the scaler of the reflection.
* Returns the scaler of the perturbation.
*
* @return the scaler
*/
Expand All @@ -97,25 +97,25 @@ class Reflection : public EnableLinOp<Reflection<ValueType>>,

protected:
/**
* Creates an empty operator reflection (0x0 operator).
* Creates an empty operator perturbation (0x0 operator).
*
* @param exec Executor associated to the reflection
* @param exec Executor associated to the perturbation
*/
explicit Reflection(std::shared_ptr<const Executor> exec)
: EnableLinOp<Reflection>(std::move(exec))
explicit Perturbation(std::shared_ptr<const Executor> exec)
: EnableLinOp<Perturbation>(std::move(exec))
{}

/**
* Creates a reflection with scaler and basis by setting projector to the
* conjugate transpose of basis. Basis must be transposable. Reflection will
* throw GKO_NOT_SUPPORT if basis is not transposable.
* Creates a perturbation with scaler and basis by setting projector to the
* conjugate transpose of basis. Basis must be transposable. Perturbation
* will throw GKO_NOT_SUPPORT if basis is not transposable.
*
* @param scaler scaling of the movement
* @param basis the direction basis
*/
explicit Reflection(std::shared_ptr<const LinOp> scaler,
std::shared_ptr<const LinOp> basis)
: Reflection(
explicit Perturbation(std::shared_ptr<const LinOp> scaler,
std::shared_ptr<const LinOp> basis)
: Perturbation(
std::move(scaler),
// basis can not be std::move(basis). Otherwise, Program deletes
// basis before applying conjugate transpose
Expand All @@ -124,22 +124,22 @@ class Reflection : public EnableLinOp<Reflection<ValueType>>,
{}

/**
* Creates a reflection of scaler, basis and projector.
* Creates a perturbation of scaler, basis and projector.
*
* @param scaler scaling of the movement
* @param basis the direction basis
* @param projector decides the coefficient of basis
*/
explicit Reflection(std::shared_ptr<const LinOp> scaler,
std::shared_ptr<const LinOp> basis,
std::shared_ptr<const LinOp> projector)
: EnableLinOp<Reflection>(basis->get_executor(),
gko::dim<2>{basis->get_size()[0]}),
explicit Perturbation(std::shared_ptr<const LinOp> scaler,
std::shared_ptr<const LinOp> basis,
std::shared_ptr<const LinOp> projector)
: EnableLinOp<Perturbation>(basis->get_executor(),
gko::dim<2>{basis->get_size()[0]}),
scaler_{std::move(scaler)},
basis_{std::move(basis)},
projector_{std::move(projector)}
{
this->validate_reflection();
this->validate_perturbation();
}

void apply_impl(const LinOp *b, LinOp *x) const override;
Expand All @@ -148,12 +148,12 @@ class Reflection : public EnableLinOp<Reflection<ValueType>>,
LinOp *x) const override;

/**
* validate_reflection check the dimension of scaler, basis, projector.
* validate_perturbation check the dimension of scaler, basis, projector.
* scaler must be 1 by 1.
* The dimension of basis should be same as the dimension of conjugate
* transpose of projector.
*/
void validate_reflection()
void validate_perturbation()
{
GKO_ASSERT_CONFORMANT(basis_, projector_);
GKO_ASSERT_CONFORMANT(projector_, basis_);
Expand All @@ -170,4 +170,4 @@ class Reflection : public EnableLinOp<Reflection<ValueType>>,
} // namespace gko


#endif // GKO_CORE_BASE_REFLECTION_HPP_
#endif // GKO_CORE_BASE_PERTURBATION_HPP_
2 changes: 1 addition & 1 deletion include/ginkgo/ginkgo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ginkgo/core/base/matrix_data.hpp>
#include <ginkgo/core/base/mtx_io.hpp>
#include <ginkgo/core/base/name_demangling.hpp>
#include <ginkgo/core/base/perturbation.hpp>
#include <ginkgo/core/base/polymorphic_object.hpp>
#include <ginkgo/core/base/range.hpp>
#include <ginkgo/core/base/range_accessors.hpp>
#include <ginkgo/core/base/reflection.hpp>
#include <ginkgo/core/base/std_extensions.hpp>
#include <ginkgo/core/base/types.hpp>
#include <ginkgo/core/base/utils.hpp>
Expand Down
2 changes: 1 addition & 1 deletion reference/test/base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ginkgo_create_test(combination)
ginkgo_create_test(composition)
ginkgo_create_test(reflection)
ginkgo_create_test(perturbation)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <ginkgo/core/base/reflection.hpp>
#include <ginkgo/core/base/perturbation.hpp>


#include <memory>
Expand All @@ -48,11 +48,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace {


class Reflection : public ::testing::Test {
class Perturbation : public ::testing::Test {
protected:
using mtx = gko::matrix::Dense<>;

Reflection()
Perturbation()
: exec{gko::ReferenceExecutor::create()},
basis{gko::initialize<mtx>({2.0, 1.0}, exec)},
projector{gko::initialize<mtx>({{3.0, 2.0}}, exec)},
Expand All @@ -66,13 +66,13 @@ class Reflection : public ::testing::Test {
};


TEST_F(Reflection, AppliesToVector)
TEST_F(Perturbation, AppliesToVector)
{
/*
cmp = I + 2 * [ 2 ] * [ 3 2 ]
[ 1 ]
*/
auto cmp = gko::Reflection<>::create(scaler, basis, projector);
auto cmp = gko::Perturbation<>::create(scaler, basis, projector);
auto x = gko::initialize<mtx>({1.0, 2.0}, exec);
auto res = clone(x);

Expand All @@ -82,13 +82,13 @@ TEST_F(Reflection, AppliesToVector)
}


TEST_F(Reflection, AppliesLinearCombinationToVector)
TEST_F(Perturbation, AppliesLinearCombinationToVector)
{
/*
cmp = I + 2 * [ 2 ] * [ 3 2 ]
[ 1 ]
*/
auto cmp = gko::Reflection<>::create(scaler, basis, projector);
auto cmp = gko::Perturbation<>::create(scaler, basis, projector);
auto alpha = gko::initialize<mtx>({3.0}, exec);
auto beta = gko::initialize<mtx>({-1.0}, exec);
auto x = gko::initialize<mtx>({1.0, 2.0}, exec);
Expand All @@ -100,13 +100,13 @@ TEST_F(Reflection, AppliesLinearCombinationToVector)
}


TEST_F(Reflection, ConstructionByBasisAppliesToVector)
TEST_F(Perturbation, ConstructionByBasisAppliesToVector)
{
/*
cmp = I + 2 * [ 2 ] * [ 2 1 ]
[ 1 ]
*/
auto cmp = gko::Reflection<>::create(scaler, basis);
auto cmp = gko::Perturbation<>::create(scaler, basis);
auto x = gko::initialize<mtx>({1.0, 2.0}, exec);
auto res = clone(x);

Expand All @@ -116,13 +116,13 @@ TEST_F(Reflection, ConstructionByBasisAppliesToVector)
}


TEST_F(Reflection, ConstructionByBasisAppliesLinearCombinationToVector)
TEST_F(Perturbation, ConstructionByBasisAppliesLinearCombinationToVector)
{
/*
cmp = I + 2 * [ 2 ] * [ 2 1 ]
[ 1 ]
*/
auto cmp = gko::Reflection<>::create(scaler, basis);
auto cmp = gko::Perturbation<>::create(scaler, basis);
auto alpha = gko::initialize<mtx>({3.0}, exec);
auto beta = gko::initialize<mtx>({-1.0}, exec);
auto x = gko::initialize<mtx>({1.0, 2.0}, exec);
Expand Down
6 changes: 3 additions & 3 deletions test_install/test_install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ int main(int, char **)
&testVar, 1u, 1u, 1u);
}

// core/base/reflection.hpp
// core/base/perturbation.hpp
{
using type1 = int;
static_assert(
std::is_same<gko::Reflection<type1>::value_type, type1>::value,
"Reflection.hpp not included properly");
std::is_same<gko::Perturbation<type1>::value_type, type1>::value,
"Perturbation.hpp not included properly");
}

// core/base/std_extensions.hpp
Expand Down

0 comments on commit e6be612

Please sign in to comment.