0

所以这就是我所写的。声明了三个类: Main Term Polynom

所以在 Polynom 我尝试调用“加号”方法:

我试过这个:

    public Polynom add(Polynom pol){

    Polynom res = new Polynom("",0);
    Term tox = new Term(0,0);

    for(Term p : Polynom){


            for(Term other : pol.Polynom ){
                if(p.getDeg()==other.getDeg()){
                    tox.setCoef(p.getCoef()+ other.getCoef());
                    tox.setDeg(p.getDeg());
        }
                res.addTerm(tox);
            }


    }
    return res;
}

我面临一个问题:我引用了一个对象 this 并且我正在进入另一个对象,这也是一个 this 。所以当我尝试添加两个多项式时,我得到了错误的结果。

有没有添加两个多项式的好方法?

private ArrayList<Term> Polynom = new ArrayList<Term>();
private String name;
private double number;

术语是:

public class Term {
private int deg;
private double coef;
}
4

2 回答 2

0

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();
    }
}
于 2013-11-14T22:28:41.747 回答
0

您只创建一个新项,而不是为生成的多项式中的每个次数创建一个。如果你解决这个问题,事情会变得更好,比如

Polynom res = new Polynom("",0);

for(Term p : Polynom){
  Term tox = new Term(0,0); // Here's the change

  for(Term other : pol.Polynom ){
    if(p.getDeg()==other.getDeg()){
      tox.setCoef(p.getCoef()+ other.getCoef());
      tox.setDeg(p.getDeg());
    }
    res.addTerm(tox);
  }
}

不幸的是,这只会添加其他多项式中与原始多项式中的项具有相同次数的项。您的结果可能会缺少条款。您可以通过按照以下框架进行操作来获得所有这些:

List<Term> sorted1;
List<Term> sorted2; // Sort both lists of Terms in ascending order of degree

while( sorted1.size() > 0 || sorted2.size() > 0 ) {
  if( sorted1.size() == 0 ) {
    // Make a new term from the last element of sorted2
    sorted2.remove( sorted2.size() - 1);
  }
  else if( sorted2.size() == 0 ) {
    // Make a new term from the last element of sorted1
    sorted1.remove( sorted1.size() - 1);
  else {
    // Figure out whether the terms have the same degree or not - if they do,
    // add them together into one term. Otherwise, choose the one with the larger
    // degree.
    // ...
  }
}

希望这能给你一个开始的地方。

于 2013-11-14T23:00:24.233 回答