实现多项式的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. ...
随机推荐
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- SAS文档:简单的随机点名器
本次实验,我们设计了一个简单的随机点名系统,下面我来介绍一下它的SRS文档. 1.功能需求: 1.1 模块1 在此模块中,我们设置了RandomName类,创建一个随机点名器,里面加入了所在课程的名单 ...
- FreeBSD_11-系统管理——{Part_6 - SYSCTL}
sysctl 常见参数 Name Type Changeable 示例 & 示意 kern.ostype string no FreeBSD kern.osrelease string no ...
- 学习python之练习(一)
#1.计算1000以内3与5的倍数的总和 import math nums = 0 for i in range(0,1000): if i%3 == 0 or i%5 == 0 : if i != ...
- vim基础使用
vim的常用模式有分为命令模式,插入模式,可视模式,正常模式.本教程中,只需要用到正常模式和插入模式.二者间的切换即可以帮助你完成本指南的学习. 进入方法: vim xxx.xml 正常模式 正常模式 ...
- 在CentOS安装cobbler自动化部署软件
#!/bin/bash##cobbler server addressip=192.168.119.133#DHCP server net and address fanweinet=192.168. ...
- maven工程引用外部jar包
maven工程经常回遇到引用外部jar包,需要先安装在jar包,然后再在工程中pom.xml文件中添加依赖. 示例: 命令行中运行: mvn install:install-file -Dfile=E ...
- 2014 New Year’s First Blog
新年开篇博客,依旧是流水账. 读到一篇强文,<关于两个世界体系的对话>.common practice 往往是针对某种语言或者特定技术为背景,然而,很多时候,common practice ...
- JSON.parse解析单引号名值对报错
今天整理代码的时候发现JSON.pare解析时会报了一个错误,而且很难找原因,调试了几分钟没有成功,猜测可能是单双引号引起的错误.修改了单双引号后程序正常运行了,现在记录下这个bug. 关于JSON. ...
- [转]Java中的回车换行符/n /r /t
'\r'是回车,'\n'是换行,前者使光标到行首,后者使光标下移一格.通常用的Enter是两个加起来.下面转一篇文章. 回车和换行 今天,我总算搞清楚"回车"(carriage r ...