【问题标题】:Add Two Polynoms (plus) with ArrayList<Term>使用 ArrayList<Term> 添加两个多项式(加号)
【发布时间】:2013-11-14 22:23:12
【问题描述】:

所以这就是我所写的。声明了三个类: 主要的 学期 多项式

所以在 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;
}

【问题讨论】:

    标签: arraylist add


    【解决方案1】:

    如果它们的指数相同,您只能添加两个 Term 实例。

    我将有两个类,MonomialPolynomial,具有公共接口 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();
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      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.
          // ...
        }
      }
      

      希望这能给你一个起点。

      【讨论】:

        猜你喜欢
        • 2015-12-16
        • 2014-09-15
        • 1970-01-01
        • 2013-05-03
        • 1970-01-01
        • 2015-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多