DGtal 1.4.0
|
Aim: This class gathers several types and methods to make computation with integers. More...
#include <DGtal/arithmetic/IntegerComputer.h>
Public Types | |
typedef IntegerComputer< TInteger > | Self |
typedef NumberTraits< TInteger >::SignedVersion | Integer |
typedef NumberTraits< Integer >::ParamType | IntegerParamType |
typedef NumberTraits< TInteger >::UnsignedVersion | UnsignedInteger |
typedef NumberTraits< UnsignedInteger >::ParamType | UnsignedIntegerParamType |
typedef SpaceND< 2, Integer >::Point | Point2I |
typedef SpaceND< 2, Integer >::Vector | Vector2I |
typedef SpaceND< 3, Integer >::Point | Point3I |
typedef SpaceND< 3, Integer >::Vector | Vector3I |
Static Public Member Functions | |
static bool | isZero (IntegerParamType a) |
static bool | isNotZero (IntegerParamType a) |
static bool | isPositive (IntegerParamType a) |
static bool | isNegative (IntegerParamType a) |
static bool | isPositiveOrZero (IntegerParamType a) |
static bool | isNegativeOrZero (IntegerParamType a) |
static Integer | abs (IntegerParamType a) |
static Integer | max (IntegerParamType a, IntegerParamType b) |
static Integer | max (IntegerParamType a, IntegerParamType b, IntegerParamType c) |
static Integer | min (IntegerParamType a, IntegerParamType b) |
static Integer | min (IntegerParamType a, IntegerParamType b, IntegerParamType c) |
static Integer | staticGcd (IntegerParamType a, IntegerParamType b) |
Private Attributes | |
Integer | _m_a |
Used to store parameter a. | |
Integer | _m_b |
Used to store parameter b. | |
Integer | _m_a0 |
Used to for successive computation in gcd. | |
Integer | _m_a1 |
Used to for successive computation in gcd. | |
Integer | _m_q |
Used to represent a quotient. | |
Integer | _m_r |
Used to represent a remainder. | |
std::vector< Integer > | _m_bezout [4] |
Vector2I | _m_v |
Used to represent the Bezout vector. | |
Vector2I | _m_v0 |
Used to represent vectors. | |
Vector2I | _m_v1 |
Used to represent vectors. | |
Integer | _m_c0 |
Used to represent scalar products. | |
Integer | _m_c1 |
Used to represent scalar products. | |
Integer | _m_c2 |
Used to represent scalar products. | |
Aim: This class gathers several types and methods to make computation with integers.
Description of template class 'IntegerComputer'
This class is especially useful with using big integers (like GMP), since a substantial part of the execution time cames from the allocation/desallocation of integers. The idea is that the user instantiate once this object and computes gcd, bezout, continued fractions with it.
To be thread-safe, each thread must instantiate an IntegerComputer.
It is a model of boost::CopyConstructible, boost::DefaultConstructible, boost::Assignable. All its member data are mutable.
It is a backport of ImaGene.
TInteger | any model of integer (CInteger), like int , long int, int64_t , BigInteger (when GMP is installed). |
Definition at line 82 of file IntegerComputer.h.
typedef NumberTraits<TInteger>::SignedVersion DGtal::IntegerComputer< TInteger >::Integer |
Definition at line 87 of file IntegerComputer.h.
typedef NumberTraits<Integer>::ParamType DGtal::IntegerComputer< TInteger >::IntegerParamType |
Definition at line 88 of file IntegerComputer.h.
typedef SpaceND<2,Integer>::Point DGtal::IntegerComputer< TInteger >::Point2I |
Definition at line 93 of file IntegerComputer.h.
typedef SpaceND<3,Integer>::Point DGtal::IntegerComputer< TInteger >::Point3I |
Definition at line 95 of file IntegerComputer.h.
typedef IntegerComputer<TInteger> DGtal::IntegerComputer< TInteger >::Self |
Definition at line 86 of file IntegerComputer.h.
typedef NumberTraits<TInteger>::UnsignedVersion DGtal::IntegerComputer< TInteger >::UnsignedInteger |
Definition at line 90 of file IntegerComputer.h.
typedef NumberTraits<UnsignedInteger>::ParamType DGtal::IntegerComputer< TInteger >::UnsignedIntegerParamType |
Definition at line 91 of file IntegerComputer.h.
typedef SpaceND<2,Integer>::Vector DGtal::IntegerComputer< TInteger >::Vector2I |
Definition at line 94 of file IntegerComputer.h.
typedef SpaceND<3,Integer>::Vector DGtal::IntegerComputer< TInteger >::Vector3I |
Definition at line 96 of file IntegerComputer.h.
DGtal::IntegerComputer< TInteger >::~IntegerComputer | ( | ) |
Destructor.
DGtal::IntegerComputer< TInteger >::IntegerComputer | ( | ) |
Constructor. Each thread must have its own instance for all computations. Such object stores several local variables to limit the number of memory allocations.
Does nothing (member data are allocated, but their values are not used except during the execution of methods).
DGtal::IntegerComputer< TInteger >::IntegerComputer | ( | const Self & | other | ) |
Copy constructor.
Does nothing (member data are allocated, but their values are not used except during the execution of methods).
other | the object to clone. |
|
static |
a | any integer. |
Referenced by checkGenericPlane(), checkGenericPlane(), checkPlane(), checkPlane(), checkPlane(), checkPlaneGroupExtension(), checkWidth(), and testValidBezout().
DGtal::IntegerComputer< TInteger >::BOOST_CONCEPT_ASSERT | ( | (concepts::CInteger< Integer >) | ) |
DGtal::IntegerComputer< TInteger >::BOOST_CONCEPT_ASSERT | ( | (concepts::CIntegralNumber< UnsignedInteger >) | ) |
DGtal::IntegerComputer< TInteger >::BOOST_CONCEPT_ASSERT | ( | (concepts::CUnsignedNumber< UnsignedInteger >) | ) |
Integer DGtal::IntegerComputer< TInteger >::ceilDiv | ( | IntegerParamType | na, |
IntegerParamType | nb ) const |
Computes the ceil value of na/nb.
na | any integer. |
nb | any integer. |
Referenced by checkExtendWithManyPoints(), checkGenericPlane(), checkGenericPlane(), checkPlane(), checkPlane(), checkPlane(), checkPlaneGroupExtension(), checkWidth(), testCeilFloorDiv(), and unionComparisonTest().
Point2I DGtal::IntegerComputer< TInteger >::convergent | ( | const std::vector< Integer > & | quotients, |
unsigned int | k ) const |
Returns the fraction corresponding to the given quotients, more precisely its k-th principal convergent. When k >= quotients.size() - 1, it is the inverse of the function getCFrac.
quotients | the sequence of partial quotients. |
k | the desired partial convergent. |
Referenced by testCFrac().
Integer DGtal::IntegerComputer< TInteger >::crossProduct | ( | const Vector2I & | u, |
const Vector2I & | v ) const |
Computes and returns the cross product of u and v.
u | any vector in Z2. |
v | any vector in Z2. |
Referenced by testValidBezout().
Integer DGtal::IntegerComputer< TInteger >::dotProduct | ( | const Vector2I & | u, |
const Vector2I & | v ) const |
Computes and returns the dot product of u and v.
u | any vector in Z2. |
v | any vector in Z2. |
Referenced by DGtal::FrechetShortcut< TIterator, TInteger >::Tools::angleVectVect(), testCoefficientIntersection(), testValidBezout(), and unionComparisonTest().
Integer DGtal::IntegerComputer< TInteger >::dotProduct | ( | const Vector3I & | u, |
const Vector3I & | v ) const |
Computes and returns the dot product of u and v.
u | any vector in Z2. |
v | any vector in Z2. |
Vector2I DGtal::IntegerComputer< TInteger >::extendedEuclid | ( | IntegerParamType | a, |
IntegerParamType | b, | ||
IntegerParamType | c ) const |
Returns a solution of the Diophantine equation: a x + b y = c. The integer c should be a multiple of the gcd of a and b. Uses the extended Euclid algorithm to do it, whose complexity is bounded by max(log(a),log(b)).
The solution is chosen such that:
a | any non-null integer. |
b | any non-null integer. |
c | any integer multiple of gcd(|a|,|b|). |
Referenced by testExtendedEuclid().
Integer DGtal::IntegerComputer< TInteger >::floorDiv | ( | IntegerParamType | na, |
IntegerParamType | nb ) const |
Computes the floor value of na/nb.
na | any integer. |
nb | any integer. |
Referenced by testCeilFloorDiv(), testDSLSubsegment(), and unionComparisonTest().
Integer DGtal::IntegerComputer< TInteger >::gcd | ( | IntegerParamType | a, |
IntegerParamType | b ) const |
Returns the greatest common divisor of a and b (a and b may be either positive or negative).
a | any integer. |
b | any integer. |
Referenced by testCFrac(), testDSLSubsegment(), testExtendedEuclid(), testGCD(), testInitFraction(), testPatterns(), testReducedFraction(), testSubStandardDSLQ0(), testSubStandardDSLQ0(), and unionComparisonTest().
Integer DGtal::IntegerComputer< TInteger >::getCFrac | ( | OutputIterator | outIt, |
IntegerParamType | a, | ||
IntegerParamType | b ) const |
Computes and outputs the quotients of the simple continued fraction of a / b. (positive) and b (positive). For instance, 5/13=[0;2,1,1,2], which is exactly what is outputed with the OutputIterator outIt.
OutputIterator | a model of boost::OutputIterator |
outIt | an instance of output iterator that is used to write the successive quotients of the continued fraction of a/b. |
a | any positive integer. |
b | any positive integer. |
Integer DGtal::IntegerComputer< TInteger >::getCFrac | ( | std::vector< Integer > & | quotients, |
IntegerParamType | a, | ||
IntegerParamType | b ) const |
Computes and push_backs the simple continued fraction of a / b. (positive) and b (positive). For instance, 5/13=[0;2,1,1,2], which is exactly what is pushed at the back of quotients.
quotients | (modifies) adds to the back of the vector the quotients of the continued fraction of a/b. |
a | any positive integer. |
b | any positive integer. |
Referenced by testCFrac(), and testReducedFraction().
void DGtal::IntegerComputer< TInteger >::getCoefficientIntersection | ( | Integer & | fl, |
Integer & | ce, | ||
const Vector2I & | p, | ||
const Vector2I & | u, | ||
const Vector2I & | N, | ||
IntegerParamType | c ) const |
Computes the floor (fl) and the ceiling (ce) value of the real number k such that p + k u lies on the supporting line of the linear constraint N.p <= c.
Otherwise said: (u.N) fl <= c - p.N < (u.N) ce
fl | the greatest integer such that (u.N) fl <= c - p.N |
ce | the smallest integer such that c - p.N < (u.N) ce |
p | any vector in Z2 |
u | any vector in Z2 in the same quadrant as N. |
N | any vector in Z2 in the same quadrant as u. |
c | any integer. |
Referenced by testCoefficientIntersection().
void DGtal::IntegerComputer< TInteger >::getCrossProduct | ( | Integer & | cp, |
const Vector2I & | u, | ||
const Vector2I & | v ) const |
Computes the cross product of u and v.
cp | (returns) the cross product of u and v. |
u | any vector in Z2. |
v | any vector in Z2. |
void DGtal::IntegerComputer< TInteger >::getDotProduct | ( | Integer & | dp, |
const Vector2I & | u, | ||
const Vector2I & | v ) const |
Computes and returns the dot product of u and v.
dp | (returns) the dot product of u and v. |
u | any vector in Z2. |
v | any vector in Z2. |
void DGtal::IntegerComputer< TInteger >::getDotProduct | ( | Integer & | dp, |
const Vector3I & | u, | ||
const Vector3I & | v ) const |
Computes and returns the dot product of u and v.
dp | (returns) the dot product of u and v. |
u | any vector in Z3. |
v | any vector in Z3. |
void DGtal::IntegerComputer< TInteger >::getEuclideanDiv | ( | Integer & | q, |
Integer & | r, | ||
IntegerParamType | a, | ||
IntegerParamType | b ) const |
Computes the euclidean division of a/b, returning quotient and remainder. May be faster than computing separately quotient and remainder, depending on the integral type in use.
q | (returns) the quotient of a/b. |
r | (returns) the remainder of a/b. |
a | any integer. |
b | any integer. |
void DGtal::IntegerComputer< TInteger >::getFloorCeilDiv | ( | Integer & | fl, |
Integer & | ce, | ||
IntegerParamType | na, | ||
IntegerParamType | nb ) const |
Computes the floor and ceil value of na/nb.
fl | (returns) the floor value of na/nb. |
ce | (returns) the ceil value of na/nb. |
na | any integer. |
nb | any integer. |
Referenced by testCeilFloorDiv().
void DGtal::IntegerComputer< TInteger >::getGcd | ( | Integer & | g, |
IntegerParamType | a, | ||
IntegerParamType | b ) const |
Returns the greatest common divisor of a and b (a and b may be either positive or negative).
g | (returns) the gcd of a and b. |
a | any integer. |
b | any integer. |
Referenced by testGCD().
void DGtal::IntegerComputer< TInteger >::getValidBezout | ( | Vector2I & | v, |
const Point2I & | A, | ||
const Vector2I & | u, | ||
const Vector2I & | N, | ||
IntegerParamType | c, | ||
const Vector2I & | N2, | ||
IntegerParamType | c2, | ||
bool | compute_v = true ) const |
Compute the valid bezout vector v of u such that A+v satifies the constraints C2 and such that A+v+u doesn't satify the constraint C2.
(A+v).N2 <= c2, (A+v+u).N2 > c2.
The constraint (N,c) is used like this: if the Bezout is such that (A+v).N > c, then v <- -v. Thus v is oriented toward the constraint.
If ! compute_v, v is used as is. The constraint N.(A+v) <= c should be satisfied.
v | (modifies) a Bezout vector for u, with the constraints above. |
A | any point in Z2. |
u | any vector in Z2. |
N | any vector in Z2, defining the first constraint. |
c | the integer for the first constraint. |
N2 | any vector in Z2, defining the second constraint. |
c2 | the integer for the second constraint. |
compute_v | tells if v should be recomputed (true) or is already given (false), default to true. |
Referenced by testValidBezout().
|
static |
a | any integer. |
|
static |
a | any integer. |
|
static |
a | any integer. |
|
static |
a | any integer. |
|
static |
a | any integer. |
bool DGtal::IntegerComputer< TInteger >::isValid | ( | ) | const |
Checks the validity/consistency of the object.
|
static |
|
static |
a | any integer. |
b | any integer. |
|
static |
a | any integer. |
b | any integer. |
c | any integer. |
|
static |
a | any integer. |
b | any integer. |
|
static |
a | any integer. |
b | any integer. |
c | any integer. |
Self & DGtal::IntegerComputer< TInteger >::operator= | ( | const Self & | other | ) |
Assignment.
Does nothing (member data are allocated, but their values are not used except during the execution of methods).
other | the object to copy. |
void DGtal::IntegerComputer< TInteger >::reduce | ( | Vector2I & | p | ) | const |
void DGtal::IntegerComputer< TInteger >::reduce | ( | Vector3I & | p | ) | const |
Makes p irreducible.
p | any vector in Z3. |
void DGtal::IntegerComputer< TInteger >::selfDisplay | ( | std::ostream & | out | ) | const |
Writes/Displays the object on an output stream.
out | the output stream where the object is written. |
|
static |
Returns the greatest common divisor of a and b (a and b may be either positive or negative).
a | any integer. |
b | any integer. |
Referenced by exhaustiveTestLatticePolytope2D().
|
mutableprivate |
Used to store parameter a.
Definition at line 508 of file IntegerComputer.h.
|
mutableprivate |
Used to for successive computation in gcd.
Definition at line 512 of file IntegerComputer.h.
|
mutableprivate |
Used to for successive computation in gcd.
Definition at line 514 of file IntegerComputer.h.
|
mutableprivate |
Used to store parameter b.
Definition at line 510 of file IntegerComputer.h.
|
mutableprivate |
Used to represent the variables during an extended Euclid computation, [0] are remainders, [1] are quotients, [2] are successive a, [3] are successive b.
Definition at line 522 of file IntegerComputer.h.
|
mutableprivate |
Used to represent scalar products.
Definition at line 530 of file IntegerComputer.h.
|
mutableprivate |
Used to represent scalar products.
Definition at line 532 of file IntegerComputer.h.
|
mutableprivate |
Used to represent scalar products.
Definition at line 534 of file IntegerComputer.h.
|
mutableprivate |
Used to represent a quotient.
Definition at line 516 of file IntegerComputer.h.
|
mutableprivate |
Used to represent a remainder.
Definition at line 518 of file IntegerComputer.h.
|
mutableprivate |
Used to represent the Bezout vector.
Definition at line 524 of file IntegerComputer.h.
|
mutableprivate |
Used to represent vectors.
Definition at line 526 of file IntegerComputer.h.
|
mutableprivate |
Used to represent vectors.
Definition at line 528 of file IntegerComputer.h.