Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的多项式_Java_Polynomial Math - Fatal编程技术网

Java中的多项式

Java中的多项式,java,polynomial-math,Java,Polynomial Math,我的add方法可以工作,但是当我创建一个新的SparsePolynomial对象(在add方法的底部)时,newsparePolynomy的值在我调试它时会发生变化,我无法确定额外的信息来自何处。有人能帮我吗 这是我的代码副本: import java.util.ArrayList; public class SparsePolynomial { private ArrayList<Polynomial> polynomialarraylist = new ArrayLi

我的add方法可以工作,但是当我创建一个新的
SparsePolynomial
对象(在add方法的底部)时,
newsparePolynomy
的值在我调试它时会发生变化,我无法确定额外的信息来自何处。有人能帮我吗

这是我的代码副本:

import java.util.ArrayList;

public class SparsePolynomial {

    private ArrayList<Polynomial> polynomialarraylist = new ArrayList<Polynomial>();

    /**
     * Constructor to get values of an arraylist of integers
     * @param arraylist that contains the  integer values used for the polynomials
     */
    public SparsePolynomial(ArrayList<Integer> arrayList)
    {
        //MODIFIDED: polynomialarraylist
        //EFFECT: constructs the arraylist of polynomials based off the arraylist of integers
        insertIntoPolynomialArray(arrayList);
    }

    /**
     * Converts the elements of the integer array into polynomials
     * @param arrayList that contains the polynomials contents
     */
    private void insertIntoPolynomialArray(ArrayList<Integer> arrayList)
    {
        //MODIFIED: polynomialarray
        //EFFECT: inputs the values of the arrayList into the polynomial array based on the position of the digits
        for(int i = 0; i < arrayList.size(); i++)
        {
            Polynomial polynomial = new Polynomial(arrayList.get(i), arrayList.get(i+1));
            polynomialarraylist.add(polynomial);
            System.out.println("coef" + arrayList.get(i));
            System.out.println("degree" + arrayList.get(i+1));
            i++;
        }
    }


    /**
     * 
     */
    @Override
    public String toString() 
    {
        String result = "";
        sort();
        if (getDegree(0) ==  0) 
            return "" + getCoefficient(0);
        if (getDegree(0) ==  1) 
            return getCoefficient(0) + "x + " + getCoefficient(0);
        result =  getCoefficient(0) + "x^" + getDegree(0);

        for (int j = 1; j < polynomialarraylist.size(); j++) 
        {
            if(j > polynomialarraylist.size())
            {
                break;
            }
            if      
            (getCoefficient(j) == 0) continue;
            else if 
            (getCoefficient(j)  > 0) result = result+ " + " + ( getCoefficient(j));
            else if 
            (getCoefficient(j)  < 0) result = result+ " - " + (-getCoefficient(j));
            if(getDegree(j) == 1) result = result + "x";
            else if (getDegree(j) >  1) result = result + "x^" + getDegree(j);
        }
        return result;


    }

    /**
     * Sorts array 
     * @param array to sort
     */
    private void sort()
    {
        ArrayList<Polynomial> temp = polynomialarraylist;
        ArrayList<Polynomial> temp2 = new ArrayList<Polynomial>();
        int polydegreemain = polynomialarraylist.get(0).degree();
        temp2.add(polynomialarraylist.get(0));
        for(int i = 1; i < polynomialarraylist.size(); i++)
        {
            if(i > polynomialarraylist.size())
            {
                break;
            }
            int polydegreesecondary = polynomialarraylist.get(i).degree();

            if(polydegreemain < polydegreesecondary)
            {
                temp.set(i-1, polynomialarraylist.get(i));
                temp.set(i, temp2.get(0));
            }

        }

        polynomialarraylist = temp;
    }
    /**
     * Makes object hashable
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime
                * result
                + ((polynomialarraylist == null) ? 0 : polynomialarraylist
                        .hashCode());
        return result;
    }

    /**
     * Checks for equality of two objects
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SparsePolynomial other = (SparsePolynomial) obj;
        if (polynomialarraylist == null) {
            if (other.polynomialarraylist != null)
                return false;
        } else if (!polynomialarraylist.equals(other.polynomialarraylist))
            return false;
        return true;
    }

    public boolean equals(SparsePolynomial Sparse)
    {
        if(this == Sparse)
        {
        return true;
        }
        else
        {
        return false;
        }
    }

    public SparsePolynomial add(SparsePolynomial other)
    {

        ArrayList<Polynomial> thisPolynomial = createPolynomial();
        SparsePolynomial newSparsePolynomial;
        ArrayList<Polynomial> otherPolynomial = other.createPolynomial();
        Polynomial oldsum = new Polynomial();
        Polynomial newsum = new Polynomial();
        for(int i = 0; i < thisPolynomial.size();i++)
        {
            if(thisPolynomial.size() == 1)
            {
                newsum = thisPolynomial.get(i);
                oldsum = newsum;
                break;
            }
            if(i == 0)
            {
            newsum = thisPolynomial.get(i).add(thisPolynomial.get(i+1));
            oldsum = newsum;
            i++;
            }
            else
            { 
                newsum = oldsum.add(thisPolynomial.get(i));
                oldsum = newsum;
            }
        }
        for(int i = 0; i < otherPolynomial.size(); i++)
        {       
            newsum = oldsum.add(otherPolynomial.get(i));
            oldsum = newsum;
        }

        ArrayList<Integer> ints = new ArrayList<Integer>();

        for(int i = 0; i < oldsum.degree()+1; i++)
        {
            ints.add(oldsum.coefficient(i));
            ints.add(i);
        }
        newSparsePolynomial = new SparsePolynomial(ints);

        return newSparsePolynomial;
    }

    public SparsePolynomial subtract(SparsePolynomial other)
    {
        ArrayList<Polynomial> thisPolynomial = createPolynomial();

        ArrayList<Polynomial> otherPolynomial = other.createPolynomial();

        Polynomial olddifference = new Polynomial();
        Polynomial newdifference = new Polynomial();
        for(int i = 0; i < thisPolynomial.size()+1;i++)
        {
            if(i == 0)
            {
                newdifference = thisPolynomial.get(i).subtract(thisPolynomial.get(i+1));
                olddifference = newdifference;
                i++;
            }
            else
            {
                newdifference = olddifference.subtract(thisPolynomial.get(i));
                olddifference = newdifference;
            }
        }
        for(int i = 0; i < otherPolynomial.size(); i++)
        {

            newdifference = olddifference.add(otherPolynomial.get(i));
            olddifference = newdifference;

        }

        ArrayList<Polynomial> polyarray = createArrayListOfPolynomialsFromPolynomials(olddifference);

        ArrayList<Integer> ints = new ArrayList<Integer>();

        for(int i = 0; i < polyarray.size(); i++)
        {
        ints.add(polyarray.get(i).coefficient(polyarray.get(i).degree()));
        ints.add(polyarray.get(i).degree());
        }

        SparsePolynomial newSparsePolynomial = new SparsePolynomial(ints);

        return newSparsePolynomial;
    }


    private int getDegree(int index)
    {
        int degree;

        degree = polynomialarraylist.get(index).degree();

        return degree;
    }

    private int getCoefficient(int index)
    {
        int coefficient;

        coefficient = polynomialarraylist.get(index).coefficient(polynomialarraylist.get(index).degree());

        return coefficient;
    }

    private ArrayList<Polynomial> createPolynomial()
    {
        Polynomial polynomial = null;
        ArrayList<Polynomial> polynomialArray = new ArrayList<Polynomial>();
        for(int i = 0; i < polynomialarraylist.size(); i++)
        {
            polynomial = new Polynomial(getCoefficient(i), getDegree(i));
            polynomialArray.add(polynomial);        
        }
        return polynomialArray;
    }
Polynomial class

public class Polynomial {
    // Overview: ...
    private int[] terms;
    private int degree;

    // Constructors
    public Polynomial() {
        // Effects: Initializes this to be the zero polynomial
        terms = new int[1];
        degree = 0;
    }

    public Polynomial(int constant, int power) {
        // Effects: if n < 0 throws IllegalArgumentException else
        //  initializes this to be the polynomial c*x^n
        if(power < 0){
            throw new IllegalArgumentException("Polynomial(int, int) constructor");
        }

        if(constant == 0) {
            terms = new int[1];
            degree = 0;
            return;
        }

        terms = new int[power+1];

        for(int i=0; i<power; i++) {
            terms[i] = 0;
        }

        terms[power] = constant;
        degree = power;
    }

    private Polynomial(int power) {
        terms = new int[power+1];
        degree = power;
    }

    // Methods
    public int degree() {
        // Effects: Returns the degree of this, i.e., the largest exponent
        //  with a non-zero coefficient.  Returns 0 is this is the zero polynomial
        return degree;
    }

    public int coefficient(int degree) {
        // Effects: Returns the coefficient of the term of this whose exponent is degree
        if(degree < 0 || degree > this.degree) {
            return 0;
        }
        else {
            return terms[degree];
        }
    }

    public Polynomial subtract(Polynomial other) throws NullPointerException {
        // Effects: if other is null throws a NullPointerException else
        //  returns the Polynomial this - other
        return add(other.minus());
    }

    public Polynomial minus() {
        // Effects: Returns the polynomial - this
        Polynomial result = new Polynomial(degree);
        for(int i=0; i<=degree; i++) {
            result.terms[i] = -this.terms[i];
        }
        return result;
    }

    public Polynomial add(Polynomial other) throws NullPointerException {
        // Effects: If other is null throws NullPointerException else
        //  returns the Polynomial this + other
        Polynomial larger, smaller;
        if (degree > other.degree){
            larger = this;
            smaller = other;
        }
        else {
            larger = other;
            smaller = this;
        }

        int newDegree = larger.degree;

        if (degree == other.degree) {
            for(int k = degree; k > 0 ; k--) {
                if (this.terms[k] + other.terms[k] != 0) {
                    break;
                }
                else {
                    newDegree --;
                }
            }
        }


        Polynomial newPoly = new Polynomial(newDegree);
        int i;
        for (i=0; i <= smaller.degree && i <= newDegree; i++){
            newPoly.terms[i] = smaller.terms[i] + larger.terms[i];
        }

        for(int j=i; j <= newDegree; j++) {
            newPoly.terms[j] = larger.terms[j];
        }

        return newPoly;
    }

    public Polynomial multiply(Polynomial other) throws NullPointerException {
        // Effects: If other is null throws NullPointerException else
        //  returns the Polynomial this * other
        if ((other.degree == 0 && other.terms[0] == 0) ||
                (this.degree==0 && this.terms[0] == 0)) {
                    return new Polynomial();
        }

        Polynomial newPoly = new Polynomial(degree + other.degree);

        newPoly.terms[degree + other.degree] = 0;

        for(int i=0; i<=degree; i++) {
            for (int j=0; j<= other.degree; j++) {
                newPoly.terms[i+j] = newPoly.terms[i+j] + this.terms[i] * other.terms[j];
            }
        }
        return newPoly;
    }
import java.util.ArrayList;
公共类稀疏多项式{
私有ArrayList多项式ArrayList=新ArrayList();
/**
*构造函数来获取整数数组列表的值
*@param arraylist,其中包含用于多项式的整数值
*/
公共SPARSEPLYNOMIAL(ArrayList ArrayList)
{
//修改:多项式数组列表
//效果:基于整数数组列表构造多项式数组列表
插入多项式阵列(arrayList);
}
/**
*将整数数组的元素转换为多项式
*@param arrayList,其中包含多项式内容
*/
私有void插入多项式阵列(ArrayList ArrayList)
{
//修改:多项式阵列
//效果:根据数字的位置将arrayList的值输入到多项式数组中
对于(int i=0;ipolynomialarraylist.size())
{
打破
}
如果
(系数(j)==0)继续;
否则如果
(getCoefficient(j)>0)结果=结果+“+”+(getCoefficient(j));
否则如果
(getCoefficient(j)<0)结果=结果+“-”+(-getCoefficient(j));
如果(getDegree(j)==1)结果=结果+“x”;
如果(getDegree(j)>1)结果=结果+“x^”+getDegree(j);
}
返回结果;
}
/**
*排序数组
*@param数组进行排序
*/
私有空排序()
{
ArrayList temp=多项式ArrayList;
ArrayList temp2=新的ArrayList();
int polydegreemain=polynomialarraylist.get(0.degree();
temp2.add(polynomialarraylist.get(0));
对于(int i=1;ipolynomialarraylist.size())
{
打破
}
int polyDegreesSecondary=polynomialarraylist.get(i).degree();
if(多度主次)
{
温度集(i-1,多项式arraylist.get(i));
温度设置(i,温度2.get(0));
}
}
多项式arraylist=温度;
}
/**
*使对象可散列
*/
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
结果=素数
*结果
+((polynomialarraylist==null)?0:polynomialarraylist
.hashCode());
返回结果;
}
/**
*检查两个对象是否相等
*/
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
SparsePolynomial other=(SparsePolynomial)obj;
if(polynomialarraylist==null){
if(other.polynomialarraylist!=null)
返回false;
}else如果(!polynomialarraylist.equals(other.polynomialarraylist))
返回false;
返回true;
}
公共布尔等于(稀疏多项式稀疏)
{
if(this==Sparse)
{
返回true;
}
其他的
{
返回false;
}
}
公共SPARSEPLYNOMIAL add(SPARSEPLYNOMIAL其他)
{
ArrayList ThisPolynomy=CreatePolynomy();
稀疏多项式;
ArrayList OtherPolynomy=other.createPolynomy();
多项式oldsum=新多项式();
多项式newsum=新多项式();
对于(int i=0;ifor(int i = 0; i < arrayList.size(); i++)
{
    Polynomial polynomial = new Polynomial(arrayList.get(i), arrayList.get(i+1));
    polynomialarraylist.add(polynomial);
    System.out.println("coef" + arrayList.get(i));
    System.out.println("degree" + arrayList.get(i+1));
    i++;
}