Class UnivariatePolynomial<E>

    • Field Detail

      • ring

        public final Ring<E> ring
        The coefficient ring
    • Method Detail

      • parse

        public static <E> UnivariatePolynomial<E> parse​(String string,
                                                        Ring<E> ring,
                                                        String var)
        Parse string into polynomial
        Parameters:
        string - string expression
        ring - the ring
        var - variable string
      • create

        public static <E> UnivariatePolynomial<E> create​(Ring<E> ring,
                                                         E... data)
        Creates new univariate polynomial over specified ring with the specified coefficients. Note: the array data will not be copied.
        Parameters:
        ring - the ring
        data - the coefficients
        Returns:
        new univariate polynomial over specified ring with specified coefficients
      • createUnsafe

        public static <E> UnivariatePolynomial<E> createUnsafe​(Ring<E> ring,
                                                               E[] data)
        skips ring.setToValueOf(data)
      • create

        public static UnivariatePolynomial<BigInteger> create​(Ring<BigInteger> ring,
                                                              long... data)
        Creates univariate polynomial over specified ring (with integer elements) with the specified coefficients
        Parameters:
        ring - the ring
        data - the coefficients
        Returns:
        new univariate polynomial over specified ring with specified coefficients
      • create

        public static UnivariatePolynomial<BigInteger> create​(long... data)
        Creates new univariate Z[x] polynomial
        Parameters:
        data - the data
        Returns:
        new univariate Z[x] polynomial with specified coefficients
      • constant

        public static <E> UnivariatePolynomial<E> constant​(Ring<E> ring,
                                                           E constant)
        Creates constant polynomial over specified ring
        Parameters:
        ring - the ring
        constant - the value
        Returns:
        constant polynomial over specified ring
      • zero

        public static <E> UnivariatePolynomial<E> zero​(Ring<E> ring)
        Creates zero polynomial over specified ring
        Parameters:
        ring - the ring
        Returns:
        zero polynomial over specified ring
      • one

        public static <E> UnivariatePolynomial<E> one​(Ring<E> ring)
        Creates unit polynomial over specified ring
        Parameters:
        ring - the ring
        Returns:
        unit polynomial over specified ring
      • asPolyZSymmetric

        public static UnivariatePolynomial<BigInteger> asPolyZSymmetric​(UnivariatePolynomial<BigInteger> poly)
        Converts Zp[x] polynomial to Z[x] polynomial formed from the coefficients of this represented in symmetric modular form (-modulus/2 <= cfx <= modulus/2).
        Parameters:
        poly - Zp polynomial
        Returns:
        Z[x] version of the poly with coefficients represented in symmetric modular form (-modulus/2 <= cfx <= modulus/2).
        Throws:
        IllegalArgumentException - is poly.ring is not a IntegersZp
      • degree

        public int degree()
        Description copied from interface: IPolynomial
        Returns the degree of this polynomial
        Specified by:
        degree in interface IPolynomial<E>
        Returns:
        the degree
      • get

        public E get​(int i)
        Returns i-th coefficient of this poly
      • set

        public UnivariatePolynomial<E> set​(int i,
                                           E el)
        Sets i-th coefficient of this poly with specified value
      • firstNonZeroCoefficientPosition

        public int firstNonZeroCoefficientPosition()
        Description copied from interface: IUnivariatePolynomial
        Returns position of the first non-zero coefficient, that is common monomial exponent (e.g. 2 for x^2 + x^3 + ...). In the case of zero polynomial, -1 returned
        Specified by:
        firstNonZeroCoefficientPosition in interface IUnivariatePolynomial<E>
        Returns:
        position of the first non-zero coefficient or -1 if this is zero
      • setRing

        public UnivariatePolynomial<E> setRing​(Ring<E> newRing)
        Returns a copy of this with elements reduced to a new coefficient ring
        Parameters:
        newRing - the new ring
        Returns:
        a copy of this with elements reduced to a new coefficient ring
      • lc

        public E lc()
        Returns the leading coefficient
        Returns:
        leading coefficient
      • cc

        public E cc()
        Returns the constant coefficient
        Returns:
        constant coefficient
      • getRange

        public UnivariatePolynomial<E> getRange​(int from,
                                                int to)
        Description copied from interface: IUnivariatePolynomial
        Creates polynomial formed from the coefficients of this starting from from (inclusive) to to (exclusive)
        Specified by:
        getRange in interface IUnivariatePolynomial<E>
        Parameters:
        from - the initial index of the range to be copied, inclusive
        to - the final index of the range to be copied, exclusive.
        Returns:
        polynomial formed from the range of coefficients of this
      • sameCoefficientRingWith

        public boolean sameCoefficientRingWith​(UnivariatePolynomial<E> oth)
        Description copied from interface: IPolynomial
        Returns whether oth and this have the same coefficient ring
        Specified by:
        sameCoefficientRingWith in interface IPolynomial<E>
        Parameters:
        oth - other polynomial
        Returns:
        whether this and oth are over the same coefficient ring
      • createFromArray

        public UnivariatePolynomial<E> createFromArray​(E[] data)
        Creates new poly with the specified coefficients (over the same ring)
        Parameters:
        data - the data
        Returns:
        polynomial
      • createLinear

        public UnivariatePolynomial<E> createLinear​(E cc,
                                                    E lc)
        Creates linear polynomial of form cc + x * lc (over the same ring)
        Parameters:
        cc - the constant coefficient
        lc - the leading coefficient
        Returns:
        cc + x * lc
      • createMonomial

        public UnivariatePolynomial<E> createMonomial​(E coefficient,
                                                      int degree)
        Creates monomial coefficient * x^degree (over the same ring)
        Parameters:
        coefficient - monomial coefficient
        degree - monomial degree
        Returns:
        coefficient * x^degree
      • createConstant

        public UnivariatePolynomial<E> createConstant​(E val)
        Creates constant polynomial with specified value (over the same ring)
        Parameters:
        val - the value
        Returns:
        constant polynomial with specified value
      • isZeroAt

        public boolean isZeroAt​(int i)
        Description copied from interface: IUnivariatePolynomial
        Returns whether i-th coefficient of this is zero
        Specified by:
        isZeroAt in interface IUnivariatePolynomial<E>
        Parameters:
        i - the position
        Returns:
        whether i-th coefficient of this is zero
      • isZero

        public boolean isZero()
        Description copied from interface: IPolynomial
        Returns true if this is zero
        Specified by:
        isZero in interface IPolynomial<E>
        Returns:
        whether this is zero
      • isOne

        public boolean isOne()
        Description copied from interface: IPolynomial
        Returns true if this is one
        Specified by:
        isOne in interface IPolynomial<E>
        Returns:
        whether this is one
      • isMonic

        public boolean isMonic()
        Description copied from interface: IPolynomial
        Returns true if this polynomial is monic
        Specified by:
        isMonic in interface IPolynomial<E>
        Returns:
        whether this is monic
      • isUnitCC

        public boolean isUnitCC()
        Description copied from interface: IPolynomial
        Returns true if constant term is equal to one
        Specified by:
        isUnitCC in interface IPolynomial<E>
        Returns:
        whether constant term is 1
      • isConstant

        public boolean isConstant()
        Description copied from interface: IPolynomial
        Returns true if this polynomial has only constant term
        Specified by:
        isConstant in interface IPolynomial<E>
        Returns:
        whether this is constant
      • isMonomial

        public boolean isMonomial()
        Description copied from interface: IPolynomial
        Returns true if this polynomial has only one monomial term
        Specified by:
        isMonomial in interface IPolynomial<E>
        Returns:
        whether this has only one monomial term
      • signumOfLC

        public int signumOfLC()
        Description copied from interface: IPolynomial
        Gives signum of the leading coefficient
        Specified by:
        signumOfLC in interface IPolynomial<E>
        Returns:
        signum of the leading coefficient
      • isOverField

        public boolean isOverField()
        Description copied from interface: IPolynomial
        Returns whether the coefficient ring of this polynomial is a field
        Specified by:
        isOverField in interface IPolynomial<E>
        Returns:
        whether the coefficient ring of this polynomial is a field
      • isOverFiniteField

        public boolean isOverFiniteField()
        Description copied from interface: IPolynomial
        Returns whether the coefficient ring of this polynomial is a finite field
        Specified by:
        isOverFiniteField in interface IPolynomial<E>
        Returns:
        whether the coefficient ring of this polynomial is a finite field
      • isOverZ

        public boolean isOverZ()
        Description copied from interface: IPolynomial
        Returns whether the coefficient ring of this polynomial is Z
        Specified by:
        isOverZ in interface IPolynomial<E>
        Returns:
        whether the coefficient ring of this polynomial is Z
      • coefficientRingCardinality

        public BigInteger coefficientRingCardinality()
        Description copied from interface: IPolynomial
        Returns cardinality of the coefficient ring of this poly
        Specified by:
        coefficientRingCardinality in interface IPolynomial<E>
        Returns:
        cardinality of the coefficient ring
      • coefficientRingCharacteristic

        public BigInteger coefficientRingCharacteristic()
        Description copied from interface: IPolynomial
        Returns characteristic of the coefficient ring of this poly
        Specified by:
        coefficientRingCharacteristic in interface IPolynomial<E>
        Returns:
        characteristic of the coefficient ring
      • isOverPerfectPower

        public boolean isOverPerfectPower()
        Description copied from interface: IPolynomial
        Returns whether the coefficientRingCardinality() is a perfect power
        Specified by:
        isOverPerfectPower in interface IPolynomial<E>
        Returns:
        whether the coefficientRingCardinality() is a perfect power
      • coefficientRingPerfectPowerBase

        public BigInteger coefficientRingPerfectPowerBase()
        Description copied from interface: IPolynomial
        Returns base so that coefficientRingCardinality() == base^exponent or null if cardinality is not finite
        Specified by:
        coefficientRingPerfectPowerBase in interface IPolynomial<E>
        Returns:
        base so that coefficientRingCardinality() == base^exponent or null if cardinality is not finite
      • coefficientRingPerfectPowerExponent

        public BigInteger coefficientRingPerfectPowerExponent()
        Description copied from interface: IPolynomial
        Returns exponent so that coefficientRingCardinality() == base^exponent or null if cardinality is not finite
        Specified by:
        coefficientRingPerfectPowerExponent in interface IPolynomial<E>
        Returns:
        exponent so that coefficientRingCardinality() == base^exponent or null if cardinality is not finite
      • norm2Double

        public static double norm2Double​(UnivariatePolynomial<BigInteger> poly)
        Returns L2 norm of the poly, i.e. a square root of a sum of coefficient squares.
      • maxAbsCoefficient

        public E maxAbsCoefficient()
        Returns max abs coefficient of the poly
      • normMax

        public E normMax()
      • shiftLeft

        public UnivariatePolynomial<E> shiftLeft​(int offset)
        Description copied from interface: IUnivariatePolynomial
        Returns the quotient this / x^offset, it is polynomial with coefficient list formed by shifting coefficients of this to the left by offset.
        Specified by:
        shiftLeft in interface IUnivariatePolynomial<E>
        Parameters:
        offset - shift amount
        Returns:
        the quotient this / x^offset
      • truncate

        public UnivariatePolynomial<E> truncate​(int newDegree)
        Description copied from interface: IUnivariatePolynomial
        Returns the remainder this rem x^(newDegree + 1), it is polynomial formed by coefficients of this from zero to newDegree (both inclusive)
        Specified by:
        truncate in interface IUnivariatePolynomial<E>
        Parameters:
        newDegree - new degree
        Returns:
        remainder this rem x^(newDegree + 1)
      • content

        public E content()
        Returns the content of the poly
        Returns:
        polynomial content
      • primitivePart

        public UnivariatePolynomial<E> primitivePart()
        Description copied from interface: IPolynomial
        Reduces poly to its primitive part (primitive part will always have positive l.c.)
        Specified by:
        primitivePart in interface IPolynomial<E>
        Returns:
        primitive part (poly will be modified)
      • primitivePartSameSign

        public UnivariatePolynomial<E> primitivePartSameSign()
        Description copied from interface: IPolynomial
        Reduces poly to its primitive part, so that primitive part will have the same signum as the initial poly
        Specified by:
        primitivePartSameSign in interface IPolynomial<E>
        Returns:
        primitive part (poly will be modified)
      • evaluate

        public E evaluate​(long point)
        Evaluates this poly at a given point (via Horner method).
        Parameters:
        point - point
        Returns:
        value at point
      • evaluate

        public E evaluate​(E point)
        Evaluates this poly at a given point (via Horner method).
        Parameters:
        point - point
        Returns:
        value at point
      • shift

        public UnivariatePolynomial<E> shift​(E value)
        Shifts variable x -> x + value and returns the result (new instance)
        Parameters:
        value - shift amount
        Returns:
        a copy of this with x -> x + value
      • add

        public UnivariatePolynomial<E> add​(E val)
        Add constant to this.
        Parameters:
        val - some number
        Returns:
        this + val
      • subtract

        public UnivariatePolynomial<E> subtract​(E val)
        Subtract constant from this.
        Parameters:
        val - some number
        Returns:
        this + val
      • addMonomial

        public UnivariatePolynomial<E> addMonomial​(E coefficient,
                                                   int exponent)
        Adds coefficient*x^exponent to this
        Parameters:
        coefficient - monomial coefficient
        exponent - monomial exponent
        Returns:
        this + coefficient*x^exponent
      • addMul

        public UnivariatePolynomial<E> addMul​(UnivariatePolynomial<E> oth,
                                              E factor)
        Adds oth * factor to this
        Parameters:
        oth - the polynomial
        factor - the factor
        Returns:
        this + oth * factor modulo modulus
      • subtract

        public UnivariatePolynomial<E> subtract​(UnivariatePolynomial<E> oth,
                                                E factor,
                                                int exponent)
        Subtracts factor * x^exponent * oth from this
        Parameters:
        oth - the polynomial
        factor - the factor
        exponent - the exponent
        Returns:
        this - factor * x^exponent * oth
      • multiply

        public UnivariatePolynomial<E> multiply​(E factor)
        Multiplies this by the factor
        Parameters:
        factor - the factor
        Returns:
        this multiplied by the factor
      • divideByLC

        public UnivariatePolynomial<E> divideByLC​(UnivariatePolynomial<E> other)
        Description copied from interface: IPolynomial
        Divides this polynomial by the leading coefficient of other or returns null (causing loss of internal data) if some of the elements can't be exactly divided by the other.lc(). NOTE: if null is returned, the content of this is destroyed.
        Specified by:
        divideByLC in interface IPolynomial<E>
        Parameters:
        other - the polynomial
        Returns:
        this divided by the other.lc() or null if exact division is not possible
      • divideOrNull

        public UnivariatePolynomial<E> divideOrNull​(E factor)
        Divides this polynomial by a factor or returns null (causing loss of internal data) if some of the elements can't be exactly divided by the factor. NOTE: if null is returned, the content of this is destroyed.
        Parameters:
        factor - the factor
        Returns:
        this divided by the factor or null
      • divideExact

        public UnivariatePolynomial<E> divideExact​(E factor)
        Divides this polynomial by a factor or throws exception if exact division is not possible
        Parameters:
        factor - the factor
        Returns:
        this divided by the factor
        Throws:
        ArithmeticException - if exact division is not possible
      • monic

        public UnivariatePolynomial<E> monic()
        Description copied from interface: IPolynomial
        Sets this to its monic part (that is this divided by its leading coefficient), or returns null (causing loss of internal data) if some of the elements can't be exactly divided by the lc(). NOTE: if null is returned, the content of this is destroyed.
        Specified by:
        monic in interface IPolynomial<E>
        Returns:
        monic this or null
      • monic

        public UnivariatePolynomial<E> monic​(E factor)
        Sets this to its monic part multiplied by the factor.
        Parameters:
        factor - the factor
        Returns:
        this
      • monicWithLC

        public UnivariatePolynomial<E> monicWithLC​(UnivariatePolynomial<E> other)
        Description copied from interface: IPolynomial
        Sets this to its monic part multiplied by the leading coefficient of other;
        Specified by:
        monicWithLC in interface IPolynomial<E>
        Parameters:
        other - other polynomial
        Returns:
        monic part multiplied by the leading coefficient of other or null if exact division by the reduced leading coefficient is not possible
      • stream

        public Stream<E> stream()
        Returns a sequential Stream with coefficients of this as its source.
        Returns:
        a sequential Stream over the coefficients in this polynomial
      • mapCoefficients

        public <T> UnivariatePolynomial<T> mapCoefficients​(Ring<T> ring,
                                                           Function<E,​T> mapper)
        Applies transformation function to this and returns the result. This method is equivalent of stream().map(mapper).collect(new PolynomialCollector<>(ring)).
        Type Parameters:
        T - result elements type
        Parameters:
        ring - ring of the new polynomial
        mapper - function that maps coefficients of this to coefficients of the result
        Returns:
        a new polynomial with the coefficients obtained from this by applying mapper
      • mapCoefficients

        public UnivariatePolynomialZp64 mapCoefficients​(IntegersZp64 ring,
                                                        ToLongFunction<E> mapper)
        Applies transformation function to this and returns the result. This method is equivalent of stream().map(mapper).collect(new PolynomialCollector<>(ring)).
        Parameters:
        ring - ring of the new polynomial
        mapper - function that maps coefficients of this to coefficients of the result
        Returns:
        a new polynomial with the coefficients obtained from this by applying mapper
      • getDataReferenceUnsafe

        public E[] getDataReferenceUnsafe()
        internal API
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object