《算法》BEYOND 部分程序 part 2
▶ 书中第六章部分程序,加上自己补充的代码,包括快速傅里叶变换,多项式表示
● 快速傅里叶变换,需要递归
- package package01;
- import edu.princeton.cs.algs4.StdOut;
- import edu.princeton.cs.algs4.StdRandom;
- import edu.princeton.cs.algs4.Complex;
- public class class01
- {
- private static final Complex ZERO = new Complex(0, 0);
- private static final double EPSILON = 1e-8;
- private class01() { }
- public static Complex[] fft(Complex[] x)// 正向傅里叶变换
- {
- int n = x.length;
- if (n == 1)
- return new Complex[] {x[0]};
- if (n % 2 != 0)
- throw new IllegalArgumentException("n != 2^k");
- Complex[] temp = new Complex[n/2], result = new Complex[n];
- for (int k = 0; k < n/2; k++) // 偶数项和奇数分别递归
- temp[k] = x[2*k];
- Complex[] q = fft(temp);
- for (int k = 0; k < n/2; k++)
- temp[k] = x[2*k + 1];
- Complex[] r = fft(temp);
- for (int k = 0; k < n/2; k++) // 合并
- {
- double kth = -2 * k * Math.PI / n;
- Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
- result[k] = q[k].plus(wk.times(r[k]));
- result[k + n/2] = q[k].minus(wk.times(r[k]));
- }
- return result;
- }
- public static Complex[] ifft(Complex[] x)// 傅里叶反变换,共轭、正变换、再共轭,最后除以项数
- {
- int n = x.length;
- Complex[] result = new Complex[n], temp;
- for (int i = 0; i < n; i++)
- result[i] = x[i].conjugate();
- result = fft(result);
- for (int i = 0; i < n; i++)
- result[i] = result[i].conjugate();
- for (int i = 0; i < n; i++)
- result[i] = result[i].scale(1.0 / n);
- return result;
- }
- public static Complex[] cconvolve(Complex[] x, Complex[] y)// 环形卷积
- {
- if (x.length != y.length)
- throw new IllegalArgumentException("x.length != y.length");
- int n = x.length;
- Complex[] a = fft(x), b = fft(y), c = new Complex[n];
- for (int i = 0; i < n; i++)
- c[i] = a[i].times(b[i]);
- return ifft(c);
- }
- public static Complex[] convolve(Complex[] x, Complex[] y)// 线性卷积,后一半垫 0
- {
- if (x.length != y.length)
- throw new IllegalArgumentException("x.length != y.length");
- Complex[] a = new Complex[2 * x.length], b = new Complex[2 * y.length];
- for (int i = 0; i < x.length; i++)
- a[i] = x[i];
- for (int i = x.length; i < 2*x.length; i++)
- a[i] = ZERO;
- for (int i = 0; i < y.length; i++)
- b[i] = y[i];
- for (int i = y.length; i < 2*y.length; i++)
- b[i] = ZERO;
- return cconvolve(a, b);
- }
- private static void show(Complex[] x, String title)
- {
- StdOut.printf("%s\n-------------------\n",title);
- for (int i = 0; i < x.length; i++)
- StdOut.println(x[i]);
- StdOut.println();
- }
- public static void main(String[] args)
- {
- int n = 16;
- Complex[] x = new Complex[n];
- for (int i = 0; i < n; i++)
- x[i] = new Complex(StdRandom.uniform(-1.0, 1.0), 0);
- show(x, "x");
- Complex[] y = fft(x);
- show(y, "y = fft(x)");
- Complex[] z = ifft(y);
- show(z, "z = ifft(y)");
- Complex[] c = cconvolve(x, x);
- show(c, "c = cconvolve(x, x)");
- Complex[] d = convolve(x, x);
- show(d, "d = convolve(x, x)");
- }
- }
● 多项式表示
- package package01;
- import edu.princeton.cs.algs4.StdOut;
- public class class01
- {
- private int[] coef; // 系数列表
- private int degree; // 次数
- public class01(int a, int b) // 建立单项式 a*x^b
- {
- coef = new int[b + 1];
- coef[b] = a;
- reduce();
- }
- private void reduce() // 记录多项式的次数
- {
- degree = -1;
- for (int i = coef.length - 1; i >= 0; i--)
- {
- if (coef[i] != 0)
- {
- degree = i;
- return;
- }
- }
- }
- public int degree()
- {
- return degree;
- }
- public class01 plus(class01 that) // p(x) + q(x),把系数从低次项向高次项各加一遍
- {
- class01 poly = new class01(0, Math.max(degree, that.degree));
- for (int i = 0; i <= degree; i++)
- poly.coef[i] = coef[i];
- for (int i = 0; i <= that.degree; i++)
- poly.coef[i] += that.coef[i];
- poly.reduce();
- return poly;
- }
- public class01 minus(class01 that)
- {
- class01 poly = new class01(0, Math.max(degree, that.degree));
- for (int i = 0; i <= degree; i++)
- poly.coef[i] = coef[i];
- for (int i = 0; i <= that.degree; i++)
- poly.coef[i] -= that.coef[i];
- poly.reduce();
- return poly;
- }
- public class01 times(class01 that)
- {
- class01 poly = new class01(0, degree + that.degree);
- for (int i = 0; i <= degree; i++)
- {
- for (int j = 0; j <= that.degree; j++)
- poly.coef[i + j] += (coef[i] * that.coef[j]);
- }
- poly.reduce();
- return poly;
- }
- public class01 compose(class01 that)// p(q(x)),用了秦九韶
- {
- class01 poly = new class01(0, 0);
- for (int i = degree; i >= 0; i--)
- {
- class01 term = new class01(coef[i], 0);
- poly = term.plus(that.times(poly));
- }
- return poly;
- }
- public class01 differentiate() // p'(x)
- {
- if (degree == 0)
- return new class01(0, 0);
- class01 poly = new class01(0, degree - 1);
- poly.degree = degree - 1;
- for (int i = 0; i < degree; i++)
- poly.coef[i] = (i + 1) * coef[i + 1];
- return poly;
- }
- public int evaluate(int x) // p(x) /. {x->a}
- {
- int p = 0;
- for (int i = degree; i >= 0; i--)
- p = coef[i] + (x * p);
- return p;
- }
- public int compareTo(class01 that)
- {
- if (degree < that.degree)
- return -1;
- if (degree > that.degree)
- return +1;
- for (int i = degree; i >= 0; i--)
- {
- if (coef[i] < that.coef[i])
- return -1;
- if (coef[i] > that.coef[i])
- return +1;
- }
- return 0;
- }
- @Override
- public String toString()
- {
- if (degree == -1)
- return "0";
- if (degree == 0)
- return "" + coef[0];
- if (degree == 1)
- return coef[1] + "x + " + coef[0];
- String s = coef[degree] + "x^" + degree;
- for (int i = degree - 1; i >= 0; i--)
- {
- if (coef[i] == 0)
- continue;
- if (coef[i] > 0)
- s += " + " + (coef[i]);
- if (coef[i] < 0)
- s += " - " + (-coef[i]);
- s += (i == 1) ? "x" : ("x^" + i);
- }
- return s;
- }
- public static void main(String[] args)
- {
- class01 zero = new class01(0, 0);
- class01 p1 = new class01(4, 3);
- class01 p2 = new class01(3, 2);
- class01 p3 = new class01(1, 0);
- class01 p4 = new class01(2, 1);
- class01 p = p1.plus(p2).plus(p3).plus(p4); // 4x^3 + 3x^2 + 1
- class01 q1 = new class01(3, 2);
- class01 q2 = new class01(5, 0);
- class01 q = q1.plus(q2); // 3x^2 + 5
- StdOut.println("zero(x) = " + zero);
- StdOut.println("p(x) = " + p);
- StdOut.println("q(x) = " + q);
- StdOut.println("p(x) + q(x) = " + p.plus(q));
- StdOut.println("p(x) * q(x) = " + p.times(q));
- StdOut.println("p(q(x)) = " + p.compose(q));
- StdOut.println("p(x) - p(x) = " + p.minus(p));
- StdOut.println("0 - p(x) = " + zero.minus(p));
- StdOut.println("p(3) = " + p.evaluate(3));
- StdOut.println("p'(x) = " + p.differentiate());
- StdOut.println("p''(x) = " + p.differentiate().differentiate());
- }
- }
《算法》BEYOND 部分程序 part 2的更多相关文章
- 信号量和PV操作写出Bakery算法的同步程序
面包店烹制面包及蛋糕,由n个销售员卖出.当有顾客进店购买面包或蛋糕时,应先在取号机上取号,然后等待叫号,若有销售员空闲时便叫下一号,试用信号量和PV操作写出Bakery算法的同步程序. 设计要求 1) ...
- GMM算法的matlab程序
GMM算法的matlab程序 在“GMM算法的matlab程序(初步)”这篇文章中已经用matlab程序对iris数据库进行简单的实现,下面的程序最终的目的是求准确度. 作者:凯鲁嘎吉 - 博客园 h ...
- GMM算法的matlab程序(初步)
GMM算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648508.html文章中已经介绍了GMM算法,现在用matlab程序实现它. 作者:凯鲁嘎 ...
- KFCM算法的matlab程序(用FCM初始化聚类中心)
KFCM算法的matlab程序(用FCM初始化聚类中心) 在“聚类——KFCM”这篇文章中已经介绍了KFCM算法,现在用matlab程序对iris数据库进行实现,用FCM初始化聚类中心,并求其准确度与 ...
- KFCM算法的matlab程序
KFCM算法的matlab程序 在“聚类——KFCM”这篇文章中已经介绍了KFCM算法,现在用matlab程序对iris数据库进行简单的实现,并求其准确度. 作者:凯鲁嘎吉 - 博客园 http:// ...
- FCM算法的matlab程序2
FCM算法的matlab程序2 在“FCM算法的matlab程序”这篇文章中已经用matlab程序对iris数据库进行实现,并求解准确度.下面的程序是另一种方法,是最常用的方法:先初始化聚类中心,在进 ...
- FCM算法的matlab程序
FCM算法的matlab程序 在“FCM算法的matlab程序(初步)”这篇文章中已经用matlab程序对iris数据库进行简单的实现,下面的程序最终的目的是求准确度. 作者:凯鲁嘎吉 - 博客园 h ...
- K-means算法的matlab程序
K-means算法的matlab程序 在“K-means算法的matlab程序(初步)”这篇文章中已经用matlab程序对iris数据库进行简单的实现,下面的程序最终的目的是求准确度. 作者:凯鲁嘎吉 ...
- FCM算法的matlab程序(初步)
FCM算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648430.html文章中已经介绍了FCM算法,现在用matlab程序实现它. 作者:凯鲁嘎 ...
- K-means算法的matlab程序(初步)
K-means算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648369.html 文章中已经介绍了K-means算法,现在用matlab程序实现 ...
随机推荐
- 微信开发-业务域名、JS接口安全域名、网页授权域名
在微信公众平台上可配置这些域名. 1.业务域名:在微信浏览器中点击文本框,会弹出下面的提示,很不爽,通过配置业务域名可以将该提示去掉 2.JS接口安全域名:分享到朋友圈(js-sdk)时用上,此接口要 ...
- linux svn客户端通过 https访问windows VisualSVN Server Manager
1)需求: 已经在阿里云windwos系统 下面安装了VisualSVN Server Manager 做为svn服务器: 现在要在腾讯云源码安装新版本客户端 2)开始源码编译安装TortoiseSV ...
- 【spring】之事物配置,声明式事务管理和基于@Transactional注解的使用
http://blog.csdn.net/bao19901210/article/details/41724355
- VMWare VSphere6.0的实验笔记
在现有的一个vsphere6.0虚拟平台上环境下搭建一套VSphere环境平台. 任务1: 1.建立1个win2008主机,192.168.12.10.16Gram,40G硬盘1独立存储+150G硬盘 ...
- Java 线程转储 [转]
http://www.oschina.net/translate/java-thread-dump java线程转储 java的线程转储可以被定义为JVM中在某一个给定的时刻运行的所有线程的快照.一个 ...
- Hadoop是怎么分块Block的?
不多说,直接上干货! hadoop的分块有两部分. 第一部分就是数据的划分(即把File划分成Block),这个是物理上真真实实的进行了划分,数据文件上传到HDFS里的时候,需要划分成一块一块,每块的 ...
- vc++使用cookie登录网站
以http://www.idc816.com为例 效果图: 1)先登录获取cookie,再使用cookie获取用户信息 2)记录cookie,关闭程序后重新运行程序,直接获取用户信息 使用Fiddle ...
- docker安装testlink
testlink 镜像 https://hub.docker.com/r/bitnami/testlink ```#shell 下载镜像 docker pull bitnami/testlink
- html标签SEO规范
原文地址:http://blog.sina.com.cn/s/blog_6c3898dd0100whr7.html 1.<!--页面注解--> 2.<html> 3.<h ...
- PHP 多态理解
PHP 多态 多态性是指相同的操作或函数.过程可作用于多种类型的对象上并获得不同的结果.不同的对象,收到同一消息将可以产生不同的结果,这种现象称为多态性. 多态性允许每个对象以适合自身的方式去响应 ...