实现多项式的JAVA类
1 package practice;
2 //http://introcs.cs.princeton.edu/java/92symbolic/Polynomial.java.html
3 /*************************************************************************
4 * Compilation: javac Polynomial.java
5 * Execution: java Polynomial
6 *
7 * Polynomials with integer coefficients.
8 *
9 * % java Polynomial
10 * zero(x) = 0
11 * p(x) = 4x^3 + 3x^2 + 2x + 1
12 * q(x) = 3x^2 + 5
13 * p(x) + q(x) = 4x^3 + 6x^2 + 2x + 6
14 * p(x) * q(x) = 12x^5 + 9x^4 + 26x^3 + 18x^2 + 10x + 5
15 * p(q(x)) = 108x^6 + 567x^4 + 996x^2 + 586
16 * 0 - p(x) = -4x^3 - 3x^2 - 2x - 1
17 * p(3) = 142
18 * p'(x) = 12x^2 + 6x + 2
19 * p''(x) = 24x + 6
20 *
21 *************************************************************************/
22 public class Polynomial {
23 private int[] coef; //coefficients 系数
24 private int deg;//degree of polynomial (0 for the zero polynomial)
25
26 //a*x^b
27 public Polynomial(int a, int b){
28 coef = new int[b+1];
29 coef[b] = a;
30 deg = degree();
31 }
32
33 // return the degree of this polynomial (0 for the zero polynomial)
34 public int degree(){
35 int d = 0;
36 for(int i = 0; i<coef.length; i++)
37 if(coef[i]!=0)
38 d = i;
39
40 return d;
41 }
42
43 //return c = a+b
44 public Polynomial plus(Polynomial b){
45 Polynomial a = this;
46 Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
47 for(int i = 0; i <= a.deg; i++)
48 c.coef[i] += a.coef[i];
49 for(int i = 0; i <= b.deg; i++)
50 c.coef[i] += b.coef[i];
51 c.deg = c.degree();
52 return c;
53 }
54
55 // return (a - b)
56 public Polynomial minus(Polynomial b) {
57 Polynomial a = this;
58 Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
59 for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
60 for (int i = 0; i <= b.deg; i++) c.coef[i] -= b.coef[i];
61 c.deg = c.degree();
62 return c;
63 }
64
65 // return (a * b)
66 public Polynomial times(Polynomial b) {
67 Polynomial a = this;
68 Polynomial c = new Polynomial(0, a.deg + b.deg);
69 for (int i = 0; i <= a.deg; i++)
70 for (int j = 0; j <= b.deg; j++)
71 c.coef[i+j] += (a.coef[i] * b.coef[j]);
72 c.deg = c.degree();
73 return c;
74 }
75
76 // return a(b(x)) - compute using Horner's method
77 public Polynomial compose(Polynomial b) {
78 Polynomial a = this;
79 Polynomial c = new Polynomial(0, 0);
80 for (int i = a.deg; i >= 0; i--) {
81 Polynomial term = new Polynomial(a.coef[i], 0);
82 c = term.plus(b.times(c));
83 }
84 return c;
85 }
86
87
88 // do a and b represent the same polynomial?
89 public boolean eq(Polynomial b) {
90 Polynomial a = this;
91 if (a.deg != b.deg) return false;
92 for (int i = a.deg; i >= 0; i--)
93 if (a.coef[i] != b.coef[i]) return false;
94 return true;
95 }
96
97
98 // use Horner's method to compute and return the polynomial evaluated at x
99 public int evaluate(int x) {
int p = 0;
for (int i = deg; i >= 0; i--)
p = coef[i] + (x * p);
return p;
}
// differentiate this polynomial and return it
public Polynomial differentiate() {
if (deg == 0) return new Polynomial(0, 0);
Polynomial deriv = new Polynomial(0, deg - 1);
deriv.deg = deg - 1;
for (int i = 0; i < deg; i++)
deriv.coef[i] = (i + 1) * coef[i + 1];
return deriv;
}
public String toString(){
if(deg == 0)
return "" + coef[0];
if(deg == 1)
return coef[1] + "x + " + coef[0];
String s = coef[deg] + "x^" + deg;
for(int i = deg-1; i >= 0; i--){
if(coef[i] == 0)
continue;
else if(coef[i] > 0)
s = s + " + " + ( coef[i]);
else if(coef[i] < 0)
s = s + " - " + (-coef[i]);
if(i== 1)
s = s + "x";
else if(i > 1)
s = s +"x^" + i;
}
return s;
}
public static void main(String[] args) {
Polynomial zero = new Polynomial(0, 0);
Polynomial p1 = new Polynomial(4, 3);
Polynomial p2 = new Polynomial(3, 2);
Polynomial p3 = new Polynomial(1, 0);
Polynomial p4 = new Polynomial(2, 1);
Polynomial p = p1.plus(p2).plus(p3).plus(p4); // 4x^3 + 3x^2 + 1
Polynomial q1 = new Polynomial(3, 2);
Polynomial q2 = new Polynomial(5, 0);
Polynomial q = q1.plus(q2); // 3x^2 + 5
Polynomial r = p.plus(q);
// Polynomial s = p.times(q);
// Polynomial t = p.compose(q);
System.out.println("zero(x) = " + zero);
System.out.println("p(x) = " + p);
System.out.println("q(x) = " + q);
System.out.println("p(x) + q(x) = " + r);
// System.out.println("p(x) * q(x) = " + s);
// System.out.println("p(q(x)) = " + t);
// System.out.println("0 - p(x) = " + zero.minus(p));
// System.out.println("p(3) = " + p.evaluate(3));
// System.out.println("p'(x) = " + p.differentiate());
// System.out.println("p''(x) = " + p.differentiate().differentiate());
}
}
实现多项式的JAVA类的更多相关文章
- 如何用Java类配置Spring MVC(不通过web.xml和XML方式)
DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servl ...
- jvm系列(一):java类的加载机制
java类的加载机制 1.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装 ...
- java类与实例
最近在看设计模式,感觉自己对java的三大特性的理解不够清晰,搞不清楚抽象类.接口.泛型的用处和优缺点.设计模式学了一半,想着还是停下来脑补一下java的基础,就从java对象开始吧. 一.java对 ...
- oracle调用JAVA类的方法
导入jar包 在oracle中导入需要的jar包,我们把编辑好的java类打成jar包,直接在oarcle里面写简单的调用就可以了, 1.操作系统需要拥有支持loadjava命令的jdk. 2.加 ...
- Java 类的实例变量初始化的过程 静态块、非静态块、构造函数的加载顺序
先看一道Java面试题: public class Baset { private String baseName = "base"; // 构造方法 public Baset() ...
- hibernate中java类的成员变量类型如何映射到SQL中的数据类型变化
hibernate映射文件??.hbm.xml配置映射元素详解--Hibernate映射类型 在从Hibernate的java的成员类型映射到SQL中的数据类型,其内映射方式它满足,SQL可以自己调制 ...
- kettle系列-[KettleUtil]kettle插件,类似kettle的自定义java类控件
该kettle插件功能类似kettle现有的定义java类插件,自定java类插件主要是支持在kettle中直接编写java代码实现自定特殊功能,而本控件主要是将自定义代码转移到jar包,就是说自定义 ...
- Myeclipse中导入项目后java类中汉字注释出现乱码问题(已解决)
今天重装系统,安装了新的Myeclipse后,导入之前的项目后,,出现了乱码问题.乱码问题主要是java类中的注释,而jsp页面中汉字却完好如初: 右键项目,查看项目的编码格式,UTF-8,把java ...
- Java类初始化
Java类初始化 成员变量的初始化和构造器 如果类的成员变量在定义时没有进行显示的初始化赋值,Java会给每个成员变量一个默认值 对于 char.short.byte.int.long.float. ...
随机推荐
- update select
update中加入select 最常用的update语法是:UPDATE <table_name>SET <column_name1> = <value>, SET ...
- SSE:服务器发送事件,使用长链接进行通讯
概述 传统的网页都是浏览器向服务器“查询”数据,但是很多场合,最有效的方式是服务器向浏览器“发送”数据.比如,每当收到新的电子邮件,服务器就向浏览器发送一个“通知”,这要比浏览器按时向服务器查询(po ...
- 如何在命令行里运行python脚本
python是一款应用非常广泛的脚本程序语言,谷歌公司的网页就是用python编写.python在生物信息.统计.网页制作.计算等多个领域都体现出了强大的功能.python和其他脚本语言如java.R ...
- 关于把A表中的数据复制到B表中(整理)
如果A,B两个表中没有重复数据且表结构一样可以直接 insert into B select * from A 如果结构不一样可以 insert into B(字段列表),select 字段列表 fr ...
- Eclipse-修改工程名
Eclipse-修改工程名 来自:http://southking.iteye.com/blog/1821754 直接修改工程可能会产生一些莫名其妙的问题,需遵循以下四步: 1. 右键工程:Ref ...
- MySQL MHA配置
MySQL环境: master:192.168.202.129:3306 slave:192.168.202.129:3307,192.168.202.129:3307,192.168.202.130 ...
- UIPageControl页控制器
一.基本知识 #import "ViewController.h"@interface ViewController ()<UIScrollViewDelegate>{ ...
- MySQL中distinct和group by性能比较[转]
MySQL中distinct和group by性能比较[转] 之前看了网上的一些测试,感觉不是很准确,今天亲自测试了一番.得出了结论(仅在个人计算机上测试,可能不全面,仅供参考) 测试过程: 准备一张 ...
- don't forget the bigger picture
Imagine a circle that contains all of human knowledge: By the time you finish elementary school, you ...
- 不制作证书是否能加密SQLSERVER与客户端之间传输的数据?
不制作证书是否能加密SQLSERVER与客户端之间传输的数据? 在做实验之前请先下载network monitor抓包工具 微软官网下载:http://www.microsoft.com/en-us/ ...