废话不多说,大名鼎鼎的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. QTimer的用法

    1.singleShot的用法 代码: QTextEdit *testEdit = new QTextEdit("hello world"); testEdit->setMa ...

  2. 萌新笔记——linux下(ubuntu)反删除(误删恢复)与回收站制作

    刚刚有个小伙伴不小心删了他写了好几的天代码,为他心疼之余帮他找回了文件. 想到我之前也常常误删一些文件,就干脆分享一下我的反删除方法,并说说我做的回收站(好low的,求大神指点) 首先是反删除软件ex ...

  3. [No00009E]几种常见的命名规则

    变量命名规则 必须遵循的命名规则 1.    变量名首字母必须为字母(a-z A-Z),下划线(_),或者美元符号($)开始php编程中所有变量必须以$开始. 2.    变量名只能是字母(a-z A ...

  4. pcl计算样点法向并显示

    利用最小二乘法估计样点表面法向,并显示 #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include < ...

  5. Linux下Source Insight的安装和汉化

    原创文章,转载请注明出处. 工欲善其事,必先利其器.Source Insight绝对是阅读C和C++代码的利器,另外,Source Insight的体量很小,安装便捷,显示直观,比vim+cscope ...

  6. 如何在 Java 中正确使用 wait, notify 和 notifyAll(转)

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...

  7. flex自适应高度内容高度超出容器高度自动出现滚动条的问题

    在容器中设置 flex-grow:2; overflow-y:auto;overflow-x:hidden;容器高度自适应. 内容高度不固定,无法出现滚动条,然后在容器中添加height:0,出现滚动 ...

  8. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  9. JavaScript模板引擎artTemplate.js——template.helper()方法

    上一篇文章我们已经讲到了helper()方法,但是上面的例子只是一个参数的写法,如果是多个参数,写法就另有区别了. <div id="user_info"></d ...

  10. Nfs+Drdb+Heartbeat 数据存储高可用服务架构方案

    一.方案的应用场景 适用于2千万-3千万PV架构的网站,Nfs数据存储高可用服务方案 备注:互联网排名前30左右公司常用的架构 二.生产环境方案部署原理图 三.生产环境服务器硬件配置: 生产环境中采用 ...