Skip to content

Commit

Permalink
Implement arbitrary large integers using the GMP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Boes committed Aug 9, 2015
1 parent 7492f5c commit cc91773
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib"
if("${isSystemDir}" STREQUAL "-1")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
endif("${isSystemDir}" STREQUAL "-1")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)

#########
# Flags #
Expand All @@ -30,6 +31,7 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG" )
find_package(Boost 1.58.0 REQUIRED COMPONENTS chrono system thread serialization)
find_package(Threads)
find_package(X11 REQUIRED)
find_package(GMP REQUIRED)

#######################
# Include Directories #
Expand Down
12 changes: 12 additions & 0 deletions cmake-modules/FindGMP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if (GMP_INCLUDE_DIR AND GMP_LIBRARIES)
set(GMP_FIND_QUIETLY TRUE)
endif (GMP_INCLUDE_DIR AND GMP_LIBRARIES)

find_path(GMP_INCLUDE_DIR NAMES gmp.h )
find_library(GMP_LIBRARIES NAMES gmp libgmp )
find_library(GMPXX_LIBRARIES NAMES gmpxx libgmpxx )

include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GMP DEFAULT_MSG GMP_INCLUDE_DIR GMP_LIBRARIES)

mark_as_advanced(GMP_INCLUDE_DIR GMP_LIBRARIES)
96 changes: 96 additions & 0 deletions include/chomp/Ring.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>

#include <gmpxx.h>

#include "Field.h"

namespace chomp {
Expand Down Expand Up @@ -134,6 +136,100 @@ inline std::ostream & operator << ( std::ostream & outstream, const Long & rhs )
return outstream;
}

class GMP_Integer {
private:
mpz_class x;
public:
GMP_Integer ( void ) : x ( 0 ) {}
GMP_Integer ( int64_t x ) : x(x) {}
GMP_Integer ( const mpz_class& x) : x(x) {}

bool invertible ( void ) const {
if ( x == 1 ) return true;
if ( x == -1 ) return true;
return false;
}

GMP_Integer balanced_value ( void ) const {
return x;
}

GMP_Integer operator - ( void ) const {
return mpz_class(-x);
}

GMP_Integer & operator += ( const GMP_Integer & rhs ) {
x += rhs . x;
return *this;
}

GMP_Integer & operator -= ( const GMP_Integer & rhs ) {
x -= rhs . x;
return *this;
}

GMP_Integer & operator *= ( const GMP_Integer & rhs ) {
x *= rhs . x;
return *this;
}

GMP_Integer operator + ( const GMP_Integer & rhs ) const {
return mpz_class(x + rhs . x);
}

GMP_Integer operator - ( const GMP_Integer & rhs ) const {
return mpz_class(x - rhs . x);
}

GMP_Integer operator * ( const GMP_Integer & rhs ) const {
return mpz_class(x * rhs . x);
}

GMP_Integer operator / ( const GMP_Integer & rhs ) const {
return mpz_class(x / rhs . x);
}

bool operator == ( const GMP_Integer & rhs ) const {
return x == rhs . x;
}

bool operator != ( const GMP_Integer & rhs ) const {
return x != rhs . x;
}

bool operator < ( const GMP_Integer & rhs ) const {
return x < rhs . x;
}

bool operator > ( const GMP_Integer & rhs ) const {
return x > rhs . x;
}

friend std::ostream & operator << ( std::ostream & outstream, const GMP_Integer & rhs );

friend class boost::serialization::access;
template < class Archive >
void save(Archive & ar, const unsigned int version) const
{
const std::string mpz_string = x.get_str();
ar & boost::serialization::make_nvp("mpz",mpz_string);
}
template<class Archive>
void load(Archive & ar, const unsigned int version)
{
std::string mpz_string;
ar & boost::serialization::make_nvp("mpz",mpz_string);
x.set_str(mpz_string,10);
}
BOOST_SERIALIZATION_SPLIT_MEMBER()

};

inline std::ostream & operator << ( std::ostream & outstream, const GMP_Integer & rhs ) {
outstream << rhs . x;
return outstream;
}

//typedef Long Ring;
#include "Field.h"

Expand Down
4 changes: 3 additions & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
set ( LIBS ${LIBS}
${X11_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${Boost_LIBRARIES} )
${Boost_LIBRARIES}
${GMP_LIBRARIES}
${GMPXX_LIBRARIES} )

set ( TARGETS chomp-cubical
chomp-matrix
Expand Down
2 changes: 1 addition & 1 deletion source/chomp-matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define RINGDEFINED
#include "chomp/Ring.h"
using namespace chomp;
typedef Long Ring;
typedef GMP_Integer Ring;

#include "chomp/MatrixComplex.h"
#include "chomp/Generators.h"
Expand Down

0 comments on commit cc91773

Please sign in to comment.