1 Introduction

Modular arithmetic is a fundamental tool in modern algebra systems. In conjunction with the Chinese remainder theorem it serves as the workhorse in several algorithms computing the gcd, resultant etc. Moreover, it can serve as a very efficient filter, since it is often possible to exclude that some value is zero by computing its modular correspondent with respect to one prime only.

模运算是现代代数系统的基本工具。与中国余数定理(Chinese remainder theorem)结合,是若干计算GCD和合矢量(resultant )算法的核心。另外,它能够作为一个十分高效的工具,原因在于它常常可能排除计算一个素数( since it is often possible to exclude that some value is zero by computing its modular correspondent with respect to one prime only.

2 Residue and Modularizable

First of all, this package introduces a type Residue. It represents Z/pZ for some prime p. The prime number p is stored in a static member variable. The class provides static member functions to change this value.

Changing the prime invalidates already existing objects of this type. However, already existing objects do not lose their value with respect to the old prime and can be reused after restoring the old prime. Since the type is based on double arithmetic the prime is restricted to values less than 226. The initial value of p is 67108859.

Moreover, the package introduces the concept Modularizable. An algebraic structure T is considered as Modularizable if there is a mapping from T into an algebraic structure that is based on the type Residue. For scalar types, e.g. Integers, this mapping is just the canonical homomorphism into Z/pZ represented by Residue. For compound types, e.g. Polynomials, the mapping is applied to the coefficients of the compound type. The mapping is provided by the class Modular_traits<T>. The class Modular_traits<T> is designed such that the concept Modularizable can be considered as optional, i.e., Modular_traits<T> provides a tag that can be used for dispatching.

首先,本包引入了Residue类型。它表示某素数p的乘法群(Z/pZ );素数(prime)p是保存在一个静态成员变量。一个类提供的静态函数用来改变这个值。数学上,同余(英语:congruence modulo,符号:≡)是数论中的一种等价关系。当两个整数以同一个正整数,若得相同余数,则二整数同余。同余是抽象代数中的同余关系的原型。在同余理论中,模 n 的互质同余类组成一个乘法,称为整数模 n 乘法群,也称为模 n 既约剩余类。在环理论中,一个抽象代数的分支,也称这个群为整数模 n 的环的单位群(单位是指乘法可逆元)。这个群是数论的基石,在密码学、整数分解和素性测试均有运用。例如,关于这个群的阶(即群的“大小”),我们可以确定如果 n 是质数当且仅当阶数为 n-1。)

改变素数的值使现存的这个类的对象非法。但现存的对象不会丢失该值,通过恢复旧的素数就可以重用。因该类型基于双精度算术运算,素数被限制在小于226,初始p的值是67108859。

另外,本包引入了“可余的”或可模的 Modularizable概念。如果一个代数结构T与一个基于Residue的结构存在一个映射(mapping ),则T被认为是Modularizable。对于标量,即整数,映射仅仅是Z/pZ(乘法群)中Residue表示的典型同态(canonical homomorphism);对于复合类型,即多项式,映射运用于复合类型的系数。映射由类 Modular_traits<T>提供。类 Modular_traits<T>的设计可以将概念Modularizable 作为可选的,即 Modular_traits<T> 提供了一个标签用于调度。

2.1 Example

In the following example modular arithmetic is used as a filter on order to avoid unnecessary gcd computations of polynomials. A gcd computation can be very costly due to coefficient growth within the Euclidean algorithm.

The general idea is that firstly the gcd is computed with respect to one prime only. If this modular gcd is constant we can (in most cases) conclude that the actual gcd is constant as well.

For this purpose the example introduces the function may_have_common_factor(). Note that there are two versions of this function, namely for the case that the coefficient type is Modularizable and that it is not. If the type is not Modularizable the filter is just not applied and the function returns true.

Further note that the implementation of class Residue requires a mantissa precision according to the IEEE Standard for Floating-Point Arithmetic (IEEE 754). However, on some processors the traditional FPU uses an extended precision. Hence, it is indispensable that the proper mantissa length is enforced before performing any arithmetic operations. Moreover, it is required that numbers are rounded to the next nearest value. This can be ensured using Protect_FPU_rounding with CGAL_FE_TONEAREST, which also enforces the required precision as a side effect.

下面的例子中,求余的运算用于过滤,避免多项式中不必要的GCD计算。GCD计算十分耗时,由于欧几里德算法(Euclidean algorithm)中的系数的增长。

通用的思路是,首先GCD计算只涉及一个素数。如果这个模GCD(modular gcd )是一个常数,我们可以推测大多数情况下实际的GCD也是一个常数。(不懂!!!!)

出于这个目的,例子中引入了一个函数may_have_common_factor()。这个函数有两个版本,分别用于系数类型是Modularizable和非Modularizable时。如果是后者,则过滤器则不会被使用,函数返回true.

另外,根据IEEE浮点运算标准(IEEE754)Residue类的实现要求尾数的精度。但在有些处理器中传统的FPU使用一个扩展的精度,所以必须在进行任何算法运算前将尾数精度进行适当设置。同是,要求将数舍入到最近的值,这个要求可通过带CGAL_FE_TONEAREST参数的Protect_FPU_rounding 来完成,它也同时将尾数精度进行了适当设置。

File Modular_arithmetic/modular_filter.cpp

 
#include <CGAL/basic.h>
 
#ifdef CGAL_USE_GMP
 
#include <CGAL/Gmpz.h>
#include <CGAL/Polynomial.h>
 
// Function in case Polynomial is Modularizable
template< typename Polynomial >
bool may_have_common_factor(
const Polynomial& p1, const Polynomial& p2, CGAL::Tag_true)
{
std::cout<< "The type is modularizable" << std::endl;
 
// Enforce IEEE double precision and rounding mode to nearest
// before useing modular arithmetic
CGAL::Protect_FPU_rounding<true> pfr(CGAL_FE_TONEAREST);
 
// Use Modular_traits to convert to polynomials with modular coefficients
typedef typename MT::Residue_type MPolynomial;
typedef typename MT::Modular_image Modular_image;
MPolynomial mp1 = Modular_image()(p1);
MPolynomial mp2 = Modular_image()(p2);
 
// check for unlucky primes, the polynomials should not lose a degree
if ( degree(p1) != mdegree(mp1)) return true;
if ( degree(p2) != mdegree(mp2)) return true;
 
// compute gcd for modular images
MPolynomial mg = CGAL::gcd(mp1,mp2);
 
// if the modular gcd is not trivial: return true
if ( mdegree(mg) > 0 ){
std::cout << "The gcd may be non trivial" << std::endl;
return true;
}else{
std::cout << "The gcd is trivial" << std::endl;
return false;
}
}
 
// This function returns true, since the filter is not applicable
template< typename Polynomial >
bool may_have_common_factor(
const Polynomial&, const Polynomial&, CGAL::Tag_false){
std::cout<< "The type is not modularizable" << std::endl;
return true;
}
 
template< typename Polynomial >
Polynomial modular_filtered_gcd(const Polynomial& p1, const Polynomial& p2){
typedef typename MT::Is_modularizable Is_modularizable;
 
// Try to avoid actual gcd computation
if (may_have_common_factor(p1,p2, Is_modularizable())){
// Compute gcd, since the filter indicates a common factor
return CGAL::gcd(p1,p2);
}else{
return construct(CGAL::gcd(content(p1),content(p2))); // return trivial gcd
}
}
 
int main(){
 
typedef CGAL::Gmpz NT;
typedef CGAL::Polynomial<NT> Poly;
 
Poly f1=construct(NT(2), NT(6), NT(4));
Poly f2=construct(NT(12), NT(4), NT(8));
Poly f3=construct(NT(3), NT(4));
 
std::cout << "f1 : " << f1 << std::endl;
std::cout << "f2 : " << f2 << std::endl;
 
std::cout << "compute modular filtered gcd(f1,f2): " << std::endl;
Poly g1 = modular_filtered_gcd(f1,f2);
std::cout << "gcd(f1,f2): " << g1 << std::endl;
 
std::cout << std::endl;
Poly p1 = f1*f3;
Poly p2 = f2*f3;
 
std::cout << "f3 : " << f3 << std::endl;
std::cout << "p1=f1*f3 : " << p1 << std::endl;
std::cout << "p2=f2*f3 : " << p2 << std::endl;
 
std::cout << "compute modular filtered gcd(p1,p2): " << std::endl;
Poly g2 = modular_filtered_gcd(p1,p2);
std::cout << "gcd(p1,p2): " << g2 << std::endl;
}
 
#else
 
int main (){
std::cout << " This examples needs GMP! " << std::endl;
}
 
#endif

3 Design and Implementation History

The class Residue is based on the C-code of Sylvain Pion et. al. as it was presented in [2].

The remaining part of the package is the result of the integration process of the NumeriX library of Exacus [1] into CGAL.

Modular Arithmetic ( Arithmetic and Algebra) CGAL 4.13 -User Manual的更多相关文章

  1. Algebraic Foundations ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    理解: 本节主要介绍CGAL的代数结构和概念之间的互操作.与传统数论不同,CGAL的代数结构关注于实数轴的“可嵌入”特征.它没有将所有传统数的集合映射到自己的代数结构概念中,避免使用“数的类型”这一术 ...

  2. Polynomial ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Fundamentals A polynomial is either zero, or can be written as the sum of one or more non-zero ter ...

  3. Algebraic Kernel ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Introduction Real solving of polynomials is a fundamental problem with a wide application range. T ...

  4. Linear and Quadratic Programming Solver ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Which Programs can be Solved? This package lets you solve convex quadratic programs of the general ...

  5. Monotone and Sorted Matrix Search ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    monotone_matrix_search() and sorted_matrix_search() are techniques that deal with the problem of eff ...

  6. dD Geometry Kernel ( Geometry Kernels) CGAL 4.13 -User Manual

    1 Introduction This part of the reference manual covers the higher-dimensional kernel. The kernel co ...

  7. 2D and 3D Linear Geometry Kernel ( Geometry Kernels) CGAL 4.13 -User Manual

    1 Introduction CGAL, the Computational Geometry Algorithms Library, is written in C++ and consists o ...

  8. 2D Polygons( Poygon) CGAL 4.13 -User Manual

    1 Introduction A polygon is a closed chain of edges. Several algorithms are available for polygons. ...

  9. 2D Convex Hulls and Extreme Points( Convex Hull Algorithms) CGAL 4.13 -User Manual

    1 Introduction A subset S⊆R2 is convex if for any two points p and q in the set the line segment wit ...

随机推荐

  1. 使用GridFsTemplate在mongodb中存取文件

    spring-data-mongodb之gridfs   mongodb除了能够存储大量的数据外,还内置了一个非常好用的文件系统.基于mongodb集群的优势,GridFS当然也是分布式的,而且备份也 ...

  2. Android BroadcastReceiver 的简单实现

    参考:http://yangguangfu.iteye.com/blog/1063732 BroadcastReceiver的实现不难,其实就是三部曲:注册,接收,发送. 但有一点较疑惑的是:当我启动 ...

  3. centos7使用tinyproxy搭建简单http(s)服务器,无用户密码验证

    1  安装 yum install tinyproxy 2 查找配置文件地址 whereis tinyproxy.conf 3 编辑配置文件 vim tinyproxy.conf 把 allow 12 ...

  4. bootstrap的datetimepicker.js的结束时间大于开始时间,当前日期之前的代码

    感觉不错的代码,贴出来,以后接着用 <link href="__ROOT__static/css/bootstrap-datetimepicker.min.css " rel ...

  5. 225. Implement Stack using Queues + 232. Implement Queue using Stacks

    ▶ 栈和队列的相互表示.发现内置的队列和栈结构都十分高效,相互表示后性能损失都很小. ▶ 第 225 题,用队列实现栈 ● 自己的代码,3 ms,单队列实现,入栈 O(1),读取栈顶元素 O(n),出 ...

  6. firemonkey Grid自定义

    http://stackoverflow.com/questions/28893564/memory-leak-on-tstringgrids-ondrawcolumncell-event http: ...

  7. Git----时光穿梭机之管理修改04

    假如你已经阅读了https://www.cnblogs.com/cxq0017/p/9663452.html Git工作区和暂存区,并且已经掌握了暂存区的概念,下面我们要讨论的是,为什么Git比其他版 ...

  8. dom4j读取XML文件内容

    <?xml version="1.0" encoding="UTF-8"?> <RESULT> <VALUE> <NO ...

  9. The web application [/zzti] registered the JDBC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver

    先说下题目:因为是在我进行处理项目升级时同时遇到了上面的问题,一般都会遇到,所以就一并说了 不罗嗦了,正题: 在本地服务器中提供tomcat6,然后在myeclipse中配置severs tomcat ...

  10. Selenium+TestNG+Maven 搭建

    Java环境配置 Eclipse配置TestNG Eclipse配置Maven pom.xml文件相关配置,添加依赖selenium和TestNg的jar包 <!-- https://mvnre ...