废话不多说,大名鼎鼎的Lenstra-Lenstra-Lovasz(LLL) 算法。实现参考论文:Factoring Polynomials with Rational Coefficients, 作者 A.K. Lenstra, H.W. Lenstra, Jr. and L. Lovasz.

/*    File        : LLL Lattice Reduction Matlab mex interface
* Description : do Lattice Reduction in the Real Field, Complex Lattice Redution is not currently supported
* reference paper 'Factoring Ploynominals with Rational Coefficients ' A.K Lenstra, H. W. Lenstra, and L.Lovasz
* Author : fangying
* Date : 2014-01-12
* Modified :
*/ #include "stdio.h"
#include "math.h"
#include "stdlib.h" /*
* Description : this function is used to give the round interger of the input double variable
* Input : a double variable
* Output : a round interger of the given variable
*/
int roundinteger(double var)
{
return (var > ) ? (int)(var + 0.5) : (int)(var - 0.5);
} /*
* Description : this function is used to perform inner product of two given vector or array
* Input : pointer of vector 'b1' and 'b2' with their length 'm'
* Output : pointer of the output result
*/ double innerproduct(double *b1, double *b2, int m)
{
double sum = ; while (m--)
{
sum += b1[m] * b2[m];
}
return sum;
} /*
* Description : this function performs Gram-Schmidt on input Matrix
* Input : pointer of the input Matrix(in array format) 'bin',pointer of the output Matrix 'bout',
* the projection between inner-vectors,and dimension 'N*M'('N' for cloumn,'M' for row)
* Output : the GSO(Gram-Schmidt Othogonal) Matrix pointer
*/ void GramSchmidt(double *bin, double *bout, double *u, double *B, int M, int N)
{
int i, j, k;
double uTemp, projection; /* copy bin to bout */
for (j = ; j < M*N; j++)
{
bout[j] = bin[j];
} /* Euclid Square of the first column */
B[] = innerproduct(bout, bout, M); for (i = ; i < N; i++)
{
for (k = ; k < i; k++)
{
projection = innerproduct(bout + k*M, bin + i*M, M);
uTemp = projection / B[k];
u[(i - )*(N - ) + k] = uTemp; for (j = ; j < M; j++)
{
bout[i*M + j] -= bout[k*M + j] * uTemp;
}
}
/* calculate the next B[i]*/
B[i] = innerproduct(bout + i*M, bout + i*M, M);
}
} /*
* Description : this function is used the do the reduction procedure
* Input : the column vector 'b',
* Output : update the ...
*/
void reduction(double *b, double *u, int k, int l, int M, int N)
{
int j, i, r;
double uTemp, uAbs; uTemp = u[(k - )*(N - ) + l];
uAbs = fabs(*u);
//uAbs = (uTemp >= 0) ? uTemp : (-uTemp);
//uAbs = (uTemp > 0) ? uTemp : (-uTemp); if (uAbs > 0.5)
{
r = roundinteger(uTemp); /* update u(k,k-1) <= u(k,k-1) - r */
u[(k - )*(N - ) + l] -= r; /* update b(k) <= b(k) -r*b(k-1) */
for (i = ; i < M; i++)
{
b[k*M + i] -= r*b[l*M + i];
} /* for u(k,j) with j<k-1, update u(k,j) <= u(k,j) - r*u(k-1,j) */
for (j = ; j <= l - ; j++)
{
u[(k - )*(N - ) + j] -= r*u[(l - )*(N - ) + j];
}
}
} /*
* Description : this function is used the do the check procedure
* Input : 'B' the input column Euclid Squre, 'b' the input Matrix element
'u' the projection , 'k' the current subscript and dimension 'M*N'
* Output : update the ...
*/
void check(double *B, double *b, double *u, int k, int l, int M, int N)
{
int i, j;
double uTemp, Btemp;
double tmp; uTemp = u[(k - )*(N - ) + k - ]; /* this is u(k,k-1) */ /* c(k-1) <= b(k)
* c(k) <= b(k-1)
* c(i) <= b(i) for i != k,k-1
*/
Btemp = B[k] + uTemp * uTemp * B[k - ]; // Btemp = c*(k-1) /* update u(k,k-1) <= u(k,k-1)B[k-1]/C[k-1] , this is v(k,k-1) */
u[(k - )*(N - ) + k - ] = uTemp*B[k - ] / Btemp; /* update B[k] <= C[k] */
B[k] = B[k - ] * B[k] / Btemp;
/* update B[k-1] <= C[k-1] */
B[k - ] = Btemp; /* exchange b[k] <=> b[k] */
for (j = ; j < M; j++)
{
tmp = b[k*M + j];
b[k*M + j] = b[(k - )*M + j];
b[(k - )*M + j] = tmp;
} /* for j<k-1 ,i.e j = [1 to k-2]
* u(k-1,j) <= u(k,j)
* u(k,j) <= u(k-1,j)
*/
for (j = ; j < k - ; j++)
{
tmp = u[(k - )*(N - ) + j]; // u(k-1,j)
u[(k - )*(N - ) + j] = u[(k - )*(N - ) + j];
u[(k - )*(N - ) + j] = tmp; // u(k,j)
} /* for j>k
*
* u(i,k) <= u(i,k-1) - u(i,k)*u(k,k-1)
* u(i,k-1) <= u(k,k-1) -uTemp*u(i,k)
*/ for (i = k + ; i < N; i++)
{
tmp = u[(i - )*(N - ) + k]; // u(i,k)
/* v(i,k) <= u(i,k-1) - u(i,k)*u(k,k-1) */
u[(i - )*(N - ) + k] = u[(i - )*(N - ) + (k - )] - u[(i - )*(N - ) + k] * uTemp;
/* v(i,k-1) <= u(i,k-1)*v() */ //更新v(i,k-1),看文献Page521页
u[(i - )*(N - ) + k - ] = u[(k - )*(N - ) + (k - )] * u[(i - )*(N - ) + (k - )] + tmp*( - uTemp*u[(k - )*(N - ) + (k - )]);
} } /*
* Description : LLL (Lattice Reduction Alogorithm
* Input : the input Matrix in array 'bin',the dimension of the Matrix 'N*M',the output 'HLR'
*/
void RLLL(double *bin, int M, int N, double *HLR)
{
int i, k, l; double *u;
double *B;
double *H; u = (double *)calloc(N*M, sizeof(double));
B = (double *)calloc(N, sizeof(double));
H = (double *)calloc(N*M, sizeof(double)); for (i = ; i < M*N; i++)
{
H[i] = bin[i];
} GramSchmidt(H, HLR, u, B, M, N); k = ; while ()
{
l = k - ;
reduction(H, u, k, l, M, N); /* iteration procedure */
if (B[k]<(0.75 - u[(k - )*(N - ) + k - ] * u[(k - )*(N - ) + k - ])*B[k - ])
{
check(B, H, u, k, l, M, N);
if (k>)
{
k--;
}
}
else
{
for (l = k - ; l >= ; l--)
{
reduction(H, u, k, l, M, N);
} if (k == N - ) break;
k++;
}
} for (i = ; i < M*N; i++)
{
HLR[i] = H[i];
} free(u);
free(H);
free(B); } int main()
{
int col = ;
int row = ;
int index = ; double magic[] = { , , , , , , , , };
double hlr[ * ] = { }; LLL(magic, col, row, hlr); return ;
}

Lattice Reduction (LLL) 算法C代码实现的更多相关文章

  1. 关于Lattice Planner规划算法的若干问答

    Apollo问答 | 关于Lattice Planner规划算法的若干问答   上周,我们在Apollo开发者交流群内做了关于Lattice Planner的分享.这里,我们将社群分享里开发者提出的问 ...

  2. Python实现各种排序算法的代码示例总结

    Python实现各种排序算法的代码示例总结 作者:Donald Knuth 字体:[增加 减小] 类型:转载 时间:2015-12-11我要评论 这篇文章主要介绍了Python实现各种排序算法的代码示 ...

  3. 10个经典的C语言面试基础算法及代码

    10个经典的C语言面试基础算法及代码作者:码农网 – 小峰 原文地址:http://www.codeceo.com/article/10-c-interview-algorithm.html 算法是一 ...

  4. 经典面试题(二)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯

    1.正整数序列Q中的每个元素都至少能被正整数a和b中的一个整除,现给定a和b,需要计算出Q中的前几项, 例如,当a=3,b=5,N=6时,序列为3,5,6,9,10,12 (1).设计一个函数void ...

  5. php四种排序算法实现代码

    分享php排序的四种算法与代码. 冒泡:function bubble_sort($arr){ $num = count($arr); for($i=0;$i<$num;$i++){ for($ ...

  6. 排序算法Java代码实现(一)—— 选择排序

    以下几篇随笔都是记录的我实现八大排序的代码,主要是贴出代码吧,讲解什么的都没有,主要是为了方便我自己复习,哈哈,如果看不明白,也不要说我坑哦! 本片分为两部分代码: 常用方法封装 排序算法里需要频繁使 ...

  7. 【转】Algorithms -离散概率值(discrete)和重置、洗牌(shuffle)算法及代码

    离散概率值(discrete) 和 重置\洗牌(shuffle) 算法 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/1 ...

  8. k-近邻算法python代码实现(非常全)

    1.k近邻算法是学习机器学习算法最为经典和简单的算法,它是机器学习算法入门最好的算法之一,可以非常好并且快速地理解机器学习的算法的框架与应用.它是一种经典简单的分类算法,当然也可以用来解决回归问题.2 ...

  9. Adaboost算法及其代码实现

    . . Adaboost算法及其代码实现 算法概述 AdaBoost(adaptive boosting),即自适应提升算法. Boosting 是一类算法的总称,这类算法的特点是通过训练若干弱分类器 ...

随机推荐

  1. MySQL同步常见问题解答(自己的小心得)

    前几天刚刚注册了博客园,我想写一些技巧性的教程,今天给大家分享一个MySQL同步常见问题解答. Q:如果主服务器正在运行并且不想停止主服务器,怎样配置一个从服务器? A:有多种方法.如果你在某时间点做 ...

  2. MySQL 索引

    MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是 ...

  3. 搭建SpringMVC+Spring+Hibernate平台

    一. 开发环境 1. 点击此查看并下载需要的 Eclipse IDE for Java EE Developers 开发工具,推荐选用32位   2. 点击此查看并下载需要的 MySQL Server ...

  4. 无法解析指定对象的 TargetProperty (UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)“的异常解决

    最近在写动画的时候做一个倒计时的效果,就是数字从大到小的一个动画,但是当我设置要new PropertyPath("XXXXXXX")的时候却报了标题的异常,各种报错.百度了好久也 ...

  5. 对C#泛型实例化对像

    public class A { } public class B<T> { public static T Get() { //在这一块如何实例化T这个对象呢?如果用default(T) ...

  6. react自学笔记总结不间断更新

    React React 是由Facfbook维护的一套框架,并且引用到instagram React只是我们熟悉MVC框中的V层,只是视图层面的一个框架,只有俩个半api(createClass,cr ...

  7. grep 命令过滤配置文件中的注释和空行

    grep 用法 Usage: grep [OPTION]... PATTERN [FILE]... Search for PATTERN in each FILE or standard input. ...

  8. [LeetCode] Heaters 加热器

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  9. 数据集偏斜 - class skew problem - 以SVM松弛变量为例

    原文 接下来要说的东西其实不是松弛变量本身,但由于是为了使用松弛变量才引入的,因此放在这里也算合适,那就是惩罚因子C.回头看一眼引入了松弛变量以后的优化问题: 注意其中C的位置,也可以回想一下C所起的 ...

  10. [转]Excel导入异常Cannot get a text value from a numeric cell解决

    原文地址:http://blog.csdn.net/ysughw/article/details/9288307 POI操作Excel时偶尔会出现Cannot get a text value fro ...