All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jscl.math.polynomial.GeoBucket Maven / Gradle / Ivy

The newest version!
package jscl.math.polynomial;

import java.util.Iterator;
import jscl.math.Generic;
import jscl.math.JSCLInteger;

final class GeoBucket extends Polynomial {
    final Polynomial factory;
    Polynomial content[];
    int size;
    boolean mutable=true;
    boolean canonicalized=true;

    GeoBucket(Polynomial factory) {
        super(factory.monomialFactory,factory.coefFactory);
        this.factory=factory;
    }

    GeoBucket(int size, Polynomial factory) {
        this(factory);
        init(size);
    }

    public int size() {
        return size;
    }

    void init(int size) {
        content=new Polynomial[size];
        this.size=size;
    }

    void resize(int size) {
        Polynomial content[]=new Polynomial[size];
        System.arraycopy(this.content,0,content,0,Math.min(this.size,size));
        this.content=content;
        this.size=size;
    }

    public Iterator iterator(boolean direction, Monomial current) {
        return new ContentIterator(direction,current);
    }

    class ContentIterator implements Iterator {
        final boolean direction;
        Term term;

        ContentIterator(boolean direction, Monomial current) {
            this.direction=direction;
            term=new Term(current,coefficient(JSCLInteger.valueOf(0)));
            seek();
        }

        void seek() {
            while(true) {
                int n=0;
                Term t=null;
                for(int i=0;i0) {
                        t=u;
                        n=i;
                    } else if(ordering.compare(t.monomial(),u.monomial())==0) {
                        t=behead(t,n,i);
                        n=i;
                    }
                }
                if(t==null || t.coef().signum()!=0) {
                    term=t;
                    return;
                }
            }
        }
        
        public boolean hasNext() {
            return term!=null;
        }
        
        public Object next() {
            Term t=term;
            seek();
            return t;
        }
        
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    Term behead(Term t, int n, int i) {
        Monomial m=t.monomial();
        Polynomial p=factory.valueof(m).multiply(t.coef());
        content[n]=content[n].subtract(p);
        content[i]=content[i].add(p);
        return new Term(m,content[i].coefficient(m));
    }

    void canonicalize() {
        Polynomial s=factory.valueof(JSCLInteger.valueOf(0));
        int sugar=0;
        for(int i=0;i3;n>>=2) i++;
        return i;
    }

    Polynomial polynomial() {
        if(canonicalized) return content[size-1];
        else throw new UnsupportedOperationException();
    }

    void set(Polynomial polynomial) {
        content[size-1]=polynomial;
    }

    public Polynomial subtract(Polynomial polynomial) {
        if(mutable) {
            Polynomial q=((GeoBucket)polynomial).polynomial();
            int n=log(q.size());
            if(n>=size) resize(n+1);
            Polynomial p=content[n];
            Polynomial s=(p==null?factory.valueof(JSCLInteger.valueOf(0)):p).subtract(q);
            content[n]=null;
            while(n=size) resize(n+1);
                p=content[n];
                if(p!=null) s=p.add(s);
                content[n]=null;
            }
            content[n]=s;
            canonicalized=false;
            normalized=false;
            return this;
        } else return copy().subtract(polynomial);
    }

    public Polynomial multiplyAndSubtract(Generic generic, Polynomial polynomial) {
        if(mutable) {
            Polynomial q=((GeoBucket)polynomial).polynomial();
            int n=log(q.size());
            if(n>=size) resize(n+1);
            Polynomial p=content[n];
            Polynomial s=(p==null?factory.valueof(JSCLInteger.valueOf(0)):p).multiplyAndSubtract(generic,q);
            content[n]=null;
            while(n=size) resize(n+1);
                p=content[n];
                if(p!=null) s=p.add(s);
                content[n]=null;
            }
            content[n]=s;
            canonicalized=false;
            normalized=false;
            return this;
        } else return copy().multiplyAndSubtract(generic,polynomial);
    }

    public Polynomial multiplyAndSubtract(Monomial monomial, Generic generic, Polynomial polynomial) {
        if(mutable) {
            Polynomial q=((GeoBucket)polynomial).polynomial();
            int n=log(q.size());
            if(n>=size) resize(n+1);
            Polynomial p=content[n];
            Polynomial s=(p==null?factory.valueof(JSCLInteger.valueOf(0)):p).multiplyAndSubtract(monomial,generic,q);
            content[n]=null;
            while(n=size) resize(n+1);
                p=content[n];
                if(p!=null) s=p.add(s);
                content[n]=null;
            }
            content[n]=s;
            canonicalized=false;
            normalized=false;
            return this;
        } else return copy().multiplyAndSubtract(monomial,generic,polynomial);
    }

    public Polynomial multiply(Generic generic) {
        if(mutable) {
            if(canonicalized) set(polynomial().multiply(generic));
            else for(int i=0;i");
            for(int i=0;i");
            return b.toString();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy