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

edu.jas.poly.ResidueRing Maven / Gradle / Ivy

The newest version!
/*
 * $Id: ResidueRing.java 3366 2010-10-24 13:02:14Z kredel $
 */

package edu.jas.poly;

import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.io.Reader;

import org.apache.log4j.Logger;

import edu.jas.structure.ElemFactory;
import edu.jas.structure.RingElem;
import edu.jas.structure.RingFactory;


/**
 * Residue ring factory based on RingElem and RingFactory module.
 * Objects of this class are immutable.
 * @author Heinz Kredel
 */
public class ResidueRing > 
             implements RingFactory< Residue >  {

    private static final Logger logger = Logger.getLogger(ResidueRing.class);
    //private boolean debug = logger.isDebugEnabled();


    /** Ring element for reduction. 
     */
    protected final C modul;


    /** Ring factory. 
     */
    protected final RingFactory ring;


    /** Indicator if this ring is a field.
     */
    protected int isField = -1; // initially unknown


    /** The constructor creates a ResidueRing object 
     * from an ring factory and a modul. 
     * @param r ring factory.
     * @param m modul.
     */
    public ResidueRing(RingFactory r, C m) {
        ring = r;
        if ( m.isZERO() ) {
           throw new IllegalArgumentException("modul may not be null");
        }
        if ( m.isONE() ) {
           logger.warn("modul is one");
        }
        if ( m.signum() < 0 ) {
           m = m.negate();
        }
        modul = m; 
    }


    /**
     * Is this structure finite or infinite.
     * @return true if this structure is finite, else false.
     * @see edu.jas.structure.ElemFactory#isFinite()
     */
    public boolean isFinite() {
        throw new UnsupportedOperationException("not implemented");
        //return ring.isFinite();
    }


    /** Copy Residue element c.
     * @param c
     * @return a copy of c.
     */
    public Residue copy(Residue c) {
        return new Residue( c.ring, c.val );
    }


    /** Get the zero element.
     * @return 0 as Residue.
     */
    public Residue getZERO() {
        return new Residue( this, ring.getZERO() );
    }


    /**  Get the one element.
     * @return 1 as Residue.
     */
    public Residue getONE() {
        Residue one = new Residue( this, ring.getONE() );
        if ( one.isZERO() ) {
           logger.warn("one is zero, so all residues are 0");
        }
        return one;
    }


    /**  Get a list of the generating elements.
     * @return list of generators for the algebraic structure.
     * @see edu.jas.structure.ElemFactory#generators()
     */
    public List> generators() {
        List rgens = ring.generators();
        List> gens = new ArrayList >( rgens.size() );
        for ( C c: rgens ) {
             gens.add( new Residue(this,c) );
        }
        return gens;
    }

    
    /**
     * Query if this ring is commutative.
     * @return true if this ring is commutative, else false.
     */
    public boolean isCommutative() {
        return ring.isCommutative();
    }


    /**
     * Query if this ring is associative.
     * @return true if this ring is associative, else false.
     */
    public boolean isAssociative() {
        return ring.isAssociative();
    }


    /**
     * Query if this ring is a field.
     * @return false.
     */
    public boolean isField() {
        if ( isField > 0 ) { 
           return true;
        }
        if ( isField == 0 ) { 
           return false;
        }
        // ideal is prime ?
        return false;
    }


    /**
     * Characteristic of this ring.
     * @return characteristic of this ring.
     */
    public java.math.BigInteger characteristic() {
        return ring.characteristic();
    }


    /** Get a Residue element from a BigInteger value.
     * @param a BigInteger.
     * @return a Residue.
     */
    public Residue fromInteger(java.math.BigInteger a) {
        return new Residue( this, ring.fromInteger(a) );
    }


    /** Get a Residue element from a long value.
     * @param a long.
     * @return a Residue.
     */
    public Residue fromInteger(long a) {
        return new Residue( this, ring.fromInteger(a) );
    }
    

    /** Get the String representation as RingFactory.
     * @see java.lang.Object#toString()
     */
    @Override
     public String toString() {
        return "Residue[ " 
                + modul.toString() + " ]";
    }


    /** Get a scripting compatible string representation.
     * @return script compatible representation for this ElemFactory.
     * @see edu.jas.structure.ElemFactory#toScript()
     */
    //JAVA6only: @Override
    public String toScript() {
        // Python case
        return "ResidueRing(" + modul.toScript() + ")";
    }


    /** Comparison with any other object.
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    @SuppressWarnings("unchecked") // not jet working
    public boolean equals(Object b) {
        if ( ! ( b instanceof ResidueRing ) ) {
           return false;
        }
        ResidueRing a = null;
        try {
            a = (ResidueRing) b;
        } catch (ClassCastException e) {
        }
        if ( a == null ) {
            return false;
        }
        if ( ! ring.equals( a.ring ) ) {
            return false;
        }
        return modul.equals( a.modul );
    }


    /** Hash code for this residue ring.
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() { 
       int h;
       h = ring.hashCode();
       h = 37 * h + modul.hashCode();
       return h;
    }


    /** Residue random.
     * @param n such that 0 ≤ v ≤ (2n-1).
     * @return a random residue element.
     */
    public Residue random(int n) {
      C x = ring.random( n );
      // x = x.sum( ring.getONE() );
      return new Residue( this, x );
    }


    /** Residue random.
     * @param n such that 0 ≤ v ≤ (2n-1).
     * @param rnd is a source for random bits.
     * @return a random residue element.
     */
    public Residue random(int n, Random rnd) {
      C x = ring.random( n, rnd );
      // x = x.sum( ring.getONE() );
      return new Residue( this, x);
    }


    /** Parse Residue from String.
     * @param s String.
     * @return Residue from s.
     */
    public Residue parse(String s) {
        C x = ring.parse( s );
        return new Residue( this, x );
    }


    /** Parse Residue from Reader.
     * @param r Reader.
     * @return next Residue from r.
     */
    public Residue parse(Reader r) {
        C x = ring.parse( r );
        return new Residue( this, x );
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy