▶ 书中第六章部分程序,加上自己补充的代码,包括快速傅里叶变换,多项式表示

● 快速傅里叶变换,需要递归

  1. package package01;
  2.  
  3. import edu.princeton.cs.algs4.StdOut;
  4. import edu.princeton.cs.algs4.StdRandom;
  5. import edu.princeton.cs.algs4.Complex;
  6.  
  7. public class class01
  8. {
  9. private static final Complex ZERO = new Complex(0, 0);
  10. private static final double EPSILON = 1e-8;
  11. private class01() { }
  12.  
  13. public static Complex[] fft(Complex[] x)// 正向傅里叶变换
  14. {
  15. int n = x.length;
  16. if (n == 1)
  17. return new Complex[] {x[0]};
  18. if (n % 2 != 0)
  19. throw new IllegalArgumentException("n != 2^k");
  20.  
  21. Complex[] temp = new Complex[n/2], result = new Complex[n];
  22. for (int k = 0; k < n/2; k++) // 偶数项和奇数分别递归
  23. temp[k] = x[2*k];
  24. Complex[] q = fft(temp);
  25. for (int k = 0; k < n/2; k++)
  26. temp[k] = x[2*k + 1];
  27. Complex[] r = fft(temp);
  28. for (int k = 0; k < n/2; k++) // 合并
  29. {
  30. double kth = -2 * k * Math.PI / n;
  31. Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
  32. result[k] = q[k].plus(wk.times(r[k]));
  33. result[k + n/2] = q[k].minus(wk.times(r[k]));
  34. }
  35. return result;
  36. }
  37.  
  38. public static Complex[] ifft(Complex[] x)// 傅里叶反变换,共轭、正变换、再共轭,最后除以项数
  39. {
  40. int n = x.length;
  41. Complex[] result = new Complex[n], temp;
  42. for (int i = 0; i < n; i++)
  43. result[i] = x[i].conjugate();
  44. result = fft(result);
  45. for (int i = 0; i < n; i++)
  46. result[i] = result[i].conjugate();
  47. for (int i = 0; i < n; i++)
  48. result[i] = result[i].scale(1.0 / n);
  49. return result;
  50. }
  51.  
  52. public static Complex[] cconvolve(Complex[] x, Complex[] y)// 环形卷积
  53. {
  54. if (x.length != y.length)
  55. throw new IllegalArgumentException("x.length != y.length");
  56. int n = x.length;
  57. Complex[] a = fft(x), b = fft(y), c = new Complex[n];
  58. for (int i = 0; i < n; i++)
  59. c[i] = a[i].times(b[i]);
  60. return ifft(c);
  61. }
  62.  
  63. public static Complex[] convolve(Complex[] x, Complex[] y)// 线性卷积,后一半垫 0
  64. {
  65. if (x.length != y.length)
  66. throw new IllegalArgumentException("x.length != y.length");
  67. Complex[] a = new Complex[2 * x.length], b = new Complex[2 * y.length];
  68. for (int i = 0; i < x.length; i++)
  69. a[i] = x[i];
  70. for (int i = x.length; i < 2*x.length; i++)
  71. a[i] = ZERO;
  72. for (int i = 0; i < y.length; i++)
  73. b[i] = y[i];
  74. for (int i = y.length; i < 2*y.length; i++)
  75. b[i] = ZERO;
  76. return cconvolve(a, b);
  77. }
  78.  
  79. private static void show(Complex[] x, String title)
  80. {
  81. StdOut.printf("%s\n-------------------\n",title);
  82. for (int i = 0; i < x.length; i++)
  83. StdOut.println(x[i]);
  84. StdOut.println();
  85. }
  86.  
  87. public static void main(String[] args)
  88. {
  89. int n = 16;
  90. Complex[] x = new Complex[n];
  91. for (int i = 0; i < n; i++)
  92. x[i] = new Complex(StdRandom.uniform(-1.0, 1.0), 0);
  93.  
  94. show(x, "x");
  95. Complex[] y = fft(x);
  96. show(y, "y = fft(x)");
  97. Complex[] z = ifft(y);
  98. show(z, "z = ifft(y)");
  99. Complex[] c = cconvolve(x, x);
  100. show(c, "c = cconvolve(x, x)");
  101. Complex[] d = convolve(x, x);
  102. show(d, "d = convolve(x, x)");
  103. }
  104. }

● 多项式表示

  1. package package01;
  2.  
  3. import edu.princeton.cs.algs4.StdOut;
  4.  
  5. public class class01
  6. {
  7. private int[] coef; // 系数列表
  8. private int degree; // 次数
  9.  
  10. public class01(int a, int b) // 建立单项式 a*x^b
  11. {
  12. coef = new int[b + 1];
  13. coef[b] = a;
  14. reduce();
  15. }
  16.  
  17. private void reduce() // 记录多项式的次数
  18. {
  19. degree = -1;
  20. for (int i = coef.length - 1; i >= 0; i--)
  21. {
  22. if (coef[i] != 0)
  23. {
  24. degree = i;
  25. return;
  26. }
  27. }
  28. }
  29.  
  30. public int degree()
  31. {
  32. return degree;
  33. }
  34.  
  35. public class01 plus(class01 that) // p(x) + q(x),把系数从低次项向高次项各加一遍
  36. {
  37. class01 poly = new class01(0, Math.max(degree, that.degree));
  38. for (int i = 0; i <= degree; i++)
  39. poly.coef[i] = coef[i];
  40. for (int i = 0; i <= that.degree; i++)
  41. poly.coef[i] += that.coef[i];
  42. poly.reduce();
  43. return poly;
  44. }
  45.  
  46. public class01 minus(class01 that)
  47. {
  48. class01 poly = new class01(0, Math.max(degree, that.degree));
  49. for (int i = 0; i <= degree; i++)
  50. poly.coef[i] = coef[i];
  51. for (int i = 0; i <= that.degree; i++)
  52. poly.coef[i] -= that.coef[i];
  53. poly.reduce();
  54. return poly;
  55. }
  56.  
  57. public class01 times(class01 that)
  58. {
  59. class01 poly = new class01(0, degree + that.degree);
  60. for (int i = 0; i <= degree; i++)
  61. {
  62. for (int j = 0; j <= that.degree; j++)
  63. poly.coef[i + j] += (coef[i] * that.coef[j]);
  64. }
  65. poly.reduce();
  66. return poly;
  67. }
  68.  
  69. public class01 compose(class01 that)// p(q(x)),用了秦九韶
  70. {
  71. class01 poly = new class01(0, 0);
  72. for (int i = degree; i >= 0; i--)
  73. {
  74. class01 term = new class01(coef[i], 0);
  75. poly = term.plus(that.times(poly));
  76. }
  77. return poly;
  78. }
  79.  
  80. public class01 differentiate() // p'(x)
  81. {
  82. if (degree == 0)
  83. return new class01(0, 0);
  84. class01 poly = new class01(0, degree - 1);
  85. poly.degree = degree - 1;
  86. for (int i = 0; i < degree; i++)
  87. poly.coef[i] = (i + 1) * coef[i + 1];
  88. return poly;
  89. }
  90.  
  91. public int evaluate(int x) // p(x) /. {x->a}
  92. {
  93. int p = 0;
  94. for (int i = degree; i >= 0; i--)
  95. p = coef[i] + (x * p);
  96. return p;
  97. }
  98.  
  99. public int compareTo(class01 that)
  100. {
  101. if (degree < that.degree)
  102. return -1;
  103. if (degree > that.degree)
  104. return +1;
  105. for (int i = degree; i >= 0; i--)
  106. {
  107. if (coef[i] < that.coef[i])
  108. return -1;
  109. if (coef[i] > that.coef[i])
  110. return +1;
  111. }
  112. return 0;
  113. }
  114.  
  115. @Override
  116. public String toString()
  117. {
  118. if (degree == -1)
  119. return "0";
  120. if (degree == 0)
  121. return "" + coef[0];
  122. if (degree == 1)
  123. return coef[1] + "x + " + coef[0];
  124. String s = coef[degree] + "x^" + degree;
  125. for (int i = degree - 1; i >= 0; i--)
  126. {
  127. if (coef[i] == 0)
  128. continue;
  129. if (coef[i] > 0)
  130. s += " + " + (coef[i]);
  131. if (coef[i] < 0)
  132. s += " - " + (-coef[i]);
  133. s += (i == 1) ? "x" : ("x^" + i);
  134. }
  135. return s;
  136. }
  137.  
  138. public static void main(String[] args)
  139. {
  140. class01 zero = new class01(0, 0);
  141. class01 p1 = new class01(4, 3);
  142. class01 p2 = new class01(3, 2);
  143. class01 p3 = new class01(1, 0);
  144. class01 p4 = new class01(2, 1);
  145. class01 p = p1.plus(p2).plus(p3).plus(p4); // 4x^3 + 3x^2 + 1
  146.  
  147. class01 q1 = new class01(3, 2);
  148. class01 q2 = new class01(5, 0);
  149. class01 q = q1.plus(q2); // 3x^2 + 5
  150.  
  151. StdOut.println("zero(x) = " + zero);
  152. StdOut.println("p(x) = " + p);
  153. StdOut.println("q(x) = " + q);
  154. StdOut.println("p(x) + q(x) = " + p.plus(q));
  155. StdOut.println("p(x) * q(x) = " + p.times(q));
  156. StdOut.println("p(q(x)) = " + p.compose(q));
  157. StdOut.println("p(x) - p(x) = " + p.minus(p));
  158. StdOut.println("0 - p(x) = " + zero.minus(p));
  159. StdOut.println("p(3) = " + p.evaluate(3));
  160. StdOut.println("p'(x) = " + p.differentiate());
  161. StdOut.println("p''(x) = " + p.differentiate().differentiate());
  162. }
  163. }

《算法》BEYOND 部分程序 part 2的更多相关文章

  1. 信号量和PV操作写出Bakery算法的同步程序

    面包店烹制面包及蛋糕,由n个销售员卖出.当有顾客进店购买面包或蛋糕时,应先在取号机上取号,然后等待叫号,若有销售员空闲时便叫下一号,试用信号量和PV操作写出Bakery算法的同步程序. 设计要求 1) ...

  2. GMM算法的matlab程序

    GMM算法的matlab程序 在“GMM算法的matlab程序(初步)”这篇文章中已经用matlab程序对iris数据库进行简单的实现,下面的程序最终的目的是求准确度. 作者:凯鲁嘎吉 - 博客园 h ...

  3. GMM算法的matlab程序(初步)

    GMM算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648508.html文章中已经介绍了GMM算法,现在用matlab程序实现它. 作者:凯鲁嘎 ...

  4. KFCM算法的matlab程序(用FCM初始化聚类中心)

    KFCM算法的matlab程序(用FCM初始化聚类中心) 在“聚类——KFCM”这篇文章中已经介绍了KFCM算法,现在用matlab程序对iris数据库进行实现,用FCM初始化聚类中心,并求其准确度与 ...

  5. KFCM算法的matlab程序

    KFCM算法的matlab程序 在“聚类——KFCM”这篇文章中已经介绍了KFCM算法,现在用matlab程序对iris数据库进行简单的实现,并求其准确度. 作者:凯鲁嘎吉 - 博客园 http:// ...

  6. FCM算法的matlab程序2

    FCM算法的matlab程序2 在“FCM算法的matlab程序”这篇文章中已经用matlab程序对iris数据库进行实现,并求解准确度.下面的程序是另一种方法,是最常用的方法:先初始化聚类中心,在进 ...

  7. FCM算法的matlab程序

    FCM算法的matlab程序 在“FCM算法的matlab程序(初步)”这篇文章中已经用matlab程序对iris数据库进行简单的实现,下面的程序最终的目的是求准确度. 作者:凯鲁嘎吉 - 博客园 h ...

  8. K-means算法的matlab程序

    K-means算法的matlab程序 在“K-means算法的matlab程序(初步)”这篇文章中已经用matlab程序对iris数据库进行简单的实现,下面的程序最终的目的是求准确度. 作者:凯鲁嘎吉 ...

  9. FCM算法的matlab程序(初步)

    FCM算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648430.html文章中已经介绍了FCM算法,现在用matlab程序实现它. 作者:凯鲁嘎 ...

  10. K-means算法的matlab程序(初步)

    K-means算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648369.html 文章中已经介绍了K-means算法,现在用matlab程序实现 ...

随机推荐

  1. 微信开发-业务域名、JS接口安全域名、网页授权域名

    在微信公众平台上可配置这些域名. 1.业务域名:在微信浏览器中点击文本框,会弹出下面的提示,很不爽,通过配置业务域名可以将该提示去掉 2.JS接口安全域名:分享到朋友圈(js-sdk)时用上,此接口要 ...

  2. linux svn客户端通过 https访问windows VisualSVN Server Manager

    1)需求: 已经在阿里云windwos系统 下面安装了VisualSVN Server Manager 做为svn服务器: 现在要在腾讯云源码安装新版本客户端 2)开始源码编译安装TortoiseSV ...

  3. 【spring】之事物配置,声明式事务管理和基于@Transactional注解的使用

    http://blog.csdn.net/bao19901210/article/details/41724355

  4. VMWare VSphere6.0的实验笔记

    在现有的一个vsphere6.0虚拟平台上环境下搭建一套VSphere环境平台. 任务1: 1.建立1个win2008主机,192.168.12.10.16Gram,40G硬盘1独立存储+150G硬盘 ...

  5. Java 线程转储 [转]

    http://www.oschina.net/translate/java-thread-dump java线程转储 java的线程转储可以被定义为JVM中在某一个给定的时刻运行的所有线程的快照.一个 ...

  6. Hadoop是怎么分块Block的?

    不多说,直接上干货! hadoop的分块有两部分. 第一部分就是数据的划分(即把File划分成Block),这个是物理上真真实实的进行了划分,数据文件上传到HDFS里的时候,需要划分成一块一块,每块的 ...

  7. vc++使用cookie登录网站

    以http://www.idc816.com为例 效果图: 1)先登录获取cookie,再使用cookie获取用户信息 2)记录cookie,关闭程序后重新运行程序,直接获取用户信息 使用Fiddle ...

  8. docker安装testlink

    testlink 镜像 https://hub.docker.com/r/bitnami/testlink ```#shell 下载镜像 docker pull bitnami/testlink

  9. html标签SEO规范

    原文地址:http://blog.sina.com.cn/s/blog_6c3898dd0100whr7.html 1.<!--页面注解--> 2.<html> 3.<h ...

  10. PHP 多态理解

    PHP 多态   多态性是指相同的操作或函数.过程可作用于多种类型的对象上并获得不同的结果.不同的对象,收到同一消息将可以产生不同的结果,这种现象称为多态性. 多态性允许每个对象以适合自身的方式去响应 ...