Term
如果它们的指数相同,则只能添加两个实例。
我将有两个类,Monomial
并且Polynomial
,具有一个公共接口Term
。我会add
在界面中有方法。
它被称为复合模式。
像这样的东西:
单项:
package poly;
import java.util.Iterator;
/**
* IMonomial
* @author Michael
* @since 5/27/11
*/
public interface IMonomial {
double TOLERANCE = 1.0E-4;
boolean hasChildren();
int getNumTerms();
double getCoeff();
int getExpon();
Iterator<IMonomial> iterator();
IMonomial add(IMonomial other);
IMonomial sub(IMonomial other);
IMonomial mul(IMonomial other);
IMonomial div(IMonomial other);
IMonomial integrate();
IMonomial differentiate();
double evaluate(double value);
}
单项式:
package poly;
import java.util.Iterator;
public class Monomial implements IMonomial {
private final double coeff;
private final int expon;
public Monomial() {
this(0.0, 0);
}
public Monomial(double coeff) {
this(coeff, 0);
}
public Monomial(double coeff, int expon) {
this.coeff = coeff;
this.expon = expon;
}
public boolean hasChildren() {
return false;
}
public int getNumTerms() {
return 1;
}
public double getCoeff() {
return this.coeff;
}
public int getExpon() {
return expon;
}
public Iterator<IMonomial> iterator() {
return new Iterator<IMonomial>() {
private boolean hasNext = true;
public boolean hasNext() {
return hasNext;
}
public IMonomial next() {
hasNext = false;
return new Monomial(getCoeff(), getExpon());
}
public void remove() {
throw new UnsupportedOperationException("remove() is not implemented");
}
};
}
public IMonomial add(IMonomial other) {
if (this.expon != other.getExpon())
throw new IllegalArgumentException("Exponents must match in order to add");
return new Monomial((this.getCoeff() + other.getCoeff()), this.expon);
}
public IMonomial sub(IMonomial other) {
if (this.expon != other.getExpon())
throw new IllegalArgumentException("Exponents must match in order to subtract");
return new Monomial((this.getCoeff() - other.getCoeff()), this.expon);
}
public IMonomial mul(IMonomial other) {
return new Monomial((this.getCoeff() * other.getCoeff()), (this.expon + other.getExpon()));
}
public IMonomial div(IMonomial other) {
return new Monomial((this.getCoeff() / other.getCoeff()), (this.expon - other.getExpon()));
}
public IMonomial integrate() {
if (Math.abs(this.getCoeff()) < IMonomial.TOLERANCE) return new Monomial(1.0);
else return new Monomial(this.getCoeff() / (this.getExpon() + 1.0), (this.getExpon() + 1));
}
public IMonomial differentiate() {
return ((this.getExpon() == 0) ? new Monomial() : new Monomial(this.getCoeff() * this.getExpon(), (this.getExpon() - 1)));
}
public double evaluate(double value) {
return this.coeff * Math.pow(value, this.expon);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Monomial monomial = (Monomial) o;
if (Double.compare(monomial.getCoeff(), getCoeff()) != 0) {
return false;
}
if (expon != monomial.expon) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result;
long temp;
temp = getCoeff() != +0.0d ? Double.doubleToLongBits(getCoeff()) : 0L;
result = (int) (temp ^ (temp >>> 32));
result = 31 * result + expon;
return result;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
if (getCoeff() > 0.0) sb.append('+');
sb.append(getCoeff());
if (expon != 0) {
if (Math.abs(getCoeff()) > IMonomial.TOLERANCE) sb.append('x');
if (expon != 1) sb.append('^').append(expon);
}
return sb.toString();
}
}
多项式:
package poly;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Polynomial
* @author Michael
* @since 5/27/11
*/
public class Polynomial implements IMonomial {
private Map<Integer, IMonomial> terms;
public Polynomial() {
init();
}
private void init() {
this.terms = new HashMap<Integer, IMonomial>();
}
public Polynomial(IMonomial other) {
init();
if (other.hasChildren()) {
Polynomial p = (Polynomial) other;
for (IMonomial term : p.terms.values()) {
this.terms.put(term.getExpon(), term);
}
} else {
this.terms.put(other.getExpon(), other);
}
}
public boolean hasChildren() {
return (this.terms.size() > 0);
}
public int getNumTerms() {
return this.terms.size();
}
public double getCoeff() {
return ((this.terms.size() == 0) ? 0.0 : this.terms.get(Collections.max(this.terms.keySet())).getCoeff());
}
public int getExpon() {
return ((this.terms.size() == 0) ? 0 : this.terms.get(Collections.max(this.terms.keySet())).getExpon());
}
public Iterator<IMonomial> iterator() {
return this.terms.values().iterator();
}
public IMonomial add(IMonomial other) {
if (other.hasChildren()) {
Iterator<IMonomial> iterator = this.terms.values().iterator();
while (iterator.hasNext()) {
IMonomial next = iterator.next();
this.add(next);
}
} else {
IMonomial term = this.terms.get(other.getExpon());
if (term != null) {
this.terms.put(other.getExpon(), term.add(other));
} else {
this.terms.put(other.getExpon(), other);
}
}
return new Polynomial(this);
}
public IMonomial sub(IMonomial other) {
if (other.hasChildren()) {
Iterator<IMonomial> iterator = this.terms.values().iterator();
while (iterator.hasNext()) {
IMonomial next = iterator.next();
this.sub(next);
}
} else {
IMonomial term = this.terms.get(other.getExpon());
if (term != null) {
this.terms.put(other.getExpon(), term.sub(other));
} else {
this.terms.put(other.getExpon(), new Monomial(-other.getCoeff(), other.getExpon()));
}
}
return new Polynomial(this);
}
public IMonomial mul(IMonomial other) {
Polynomial value = new Polynomial();
for (IMonomial term : terms.values()) {
value.add(term.mul(other));
}
return value;
}
public IMonomial div(IMonomial other) {
Polynomial value = new Polynomial();
for (IMonomial term : terms.values()) {
value.add(term.div(other));
}
return value;
}
public IMonomial integrate() {
Polynomial value = new Polynomial();
for (IMonomial term : terms.values()) {
value.add(term.integrate());
}
return value;
}
public IMonomial differentiate() {
Polynomial value = new Polynomial();
for (IMonomial term : terms.values()) {
value.add(term.differentiate());
}
return value;
}
public double evaluate(double value) {
double sum = 0.0;
for (IMonomial term : this.terms.values()) {
sum += term.evaluate(value);
}
return sum;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Polynomial that = (Polynomial) o;
if (terms != null ? !terms.equals(that.terms) : that.terms != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
return terms != null ? terms.hashCode() : 0;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
for (IMonomial term : this.terms.values()) {
sb.append(term);
}
return sb.toString();
}
}