Polynom

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Polynom

    Hier ein kleines Skript, wie man in Java mit einem Polynom 5ten Grades rechnnen kann:

    [code:1]public class Polynomial
    {

    Term[] x= new Term[5];
    Term[] x_new= new Term[5];

    // Constructor Polynomial which consumes terms
    public Polynomial (Term a, Term b, Term c, Term d, Term e)
    {
    x[0] = a;
    x[1] = b;
    x[2] = c;
    x[3] = d;
    x[4] = e;
    }

    // Method add, which consumes an other polynomial and returns an polynomial
    public Polynomial add(Polynomial poly)
    {
    for (int i = 0; i <x.length; i++)
    {
    if(this.x.getExponent() == poly.x[i].getExponent())
    {
    x_new[i] = new Term(this.x[i].getFactor() + poly.x[i].getFactor(), x[i].getExponent());
    }
    }

    Polynomial addPoly = new Polynomial(x_new[0], x_new[1], x_new[2], x_new[3], x_new[4]);
    return addPoly;
    }


    // Method sub, which consumes an other polynomial and returns an polynomial
    public Polynomial sub(Polynomial poly)
    {
    for (int i = 0; i <x.length; i++)
    {
    if(this.x[i].getExponent() == poly.x[i].getExponent())
    {
    x_new[i] = new Term(this.x[i].getFactor() - poly.x[i].getFactor(), x[i].getExponent());
    }
    }

    Polynomial subPoly = new Polynomial(x_new[0], x_new[1], x_new[2], x_new[3], x_new[4]);
    return subPoly;
    }

    // Method mul, which consumes an other polynomial and returns an polynomial
    public Polynomial mul(Polynomial poly)
    {
    for (int i = 0; i <x.length; i++)
    {
    for (int k = 0; k<x.length; k++)
    {
    x_new[i] = new Term(this.x[i].getFactor() * poly.x[k].getFactor(), this.x[i].getExponent() + poly.x[k].getExponent() );
    }
    }
    Polynomial mulPoly = new Polynomial(x_new[0], x_new[1], x_new[2], x_new[3], x_new[4]);

    return mulPoly;
    }

    // Method mul, which consumes a factor and returns an polynomial
    public Polynomial mul(int factor)
    {
    for (int i = 0; i <x.length; i++)
    {
    x_new[i] = new Term(this.x[i].getFactor() * factor, x[i].getExponent());
    }
    Polynomial mulPoly = new Polynomial(x_new[0], x_new[1], x_new[2], x_new[3], x_new[4]);

    return mulPoly;
    }

    // Method mul, which consumes a term and returns an polynomial
    public Polynomial mul(Term term)
    {
    for (int i = 0; i <x.length; i++)
    {
    x_new[i] = new Term(this.x[i].getFactor() * term.getFactor(), this.x[i].getExponent() + term.getExponent());
    }
    Polynomial mulPoly = new Polynomial(x_new[0], x_new[1], x_new[2], x_new[3], x_new[4]);

    return mulPoly;
    }


    // Method toString, which returns a String representation of the polynomial
    public String toString()
    {
    String string = "" ;
    for (int i = 0; i <x.length; i++)
    {
    if (this.x[i].getFactor() > 0 || this.x[i].getFactor() < 0 )
    {
    if (this.x[i].getExponent()== 0)
    string = string + "+" + this.x[i].getFactor();
    else string = string + "+" + this.x[i].getFactor() + "x" + "^" + this.x[i].getExponent();
    }
    }

    // Helps to replace "+-" through "-"
    String to_replace = "+-";
    String _with= "-";
    string = string.replace(to_replace, _with);

    return string;
    }

    }[/code:1]