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

edu.jas.vector.GenVector Maven / Gradle / Ivy

The newest version!
/*
 * $Id: GenVector.java 4125 2012-08-19 19:05:22Z kredel $
 */

package edu.jas.vector;


import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import edu.jas.kern.PrettyPrint;
import edu.jas.structure.ModulElem;
import edu.jas.structure.RingElem;


/**
 * GenVector implements generic vectors with RingElem entries. Vectors of n
 * columns over C.
 * @author Heinz Kredel
 */

public class GenVector> implements ModulElem, C> {


    private static final Logger logger = Logger.getLogger(GenVector.class);


    public final GenVectorModul modul;


    public final List val;


    /**
     * Constructor for zero GenVector.
     */
    public GenVector(GenVectorModul m) {
        this(m, m.getZERO().val);
    }


    /**
     * Constructor for GenVector.
     */
    public GenVector(GenVectorModul m, List v) {
        if (m == null || v == null) {
            throw new IllegalArgumentException("Empty m or v not allowed, m = " + m + ", v = " + v);
        }
        modul = m;
        val = v;
        logger.info(modul.cols + " vector constructed");
    }


    /**
     * Get the String representation as RingElem.
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuffer s = new StringBuffer();
        s.append("[ ");
        boolean first = true;
        for (C c : val) {
            if (first) {
                first = false;
            } else {
                s.append(", ");
            }
            s.append(c.toString());
        }
        s.append(" ]");
        if (!PrettyPrint.isTrue()) {
            s.append(" :: " + modul.toString());
            s.append("\n");
        }
        return s.toString();
    }


    /**
     * Get a scripting compatible string representation.
     * @return script compatible representation for this Element.
     * @see edu.jas.structure.Element#toScript()
     */
    //JAVA6only: @Override
    public String toScript() {
        // Python case
        StringBuffer s = new StringBuffer();
        s.append("( ");
        boolean first = true;
        for (C c : val) {
            if (first) {
                first = false;
            } else {
                s.append(", ");
            }
            s.append(c.toScript());
        }
        s.append(" )");
        return s.toString();
    }


    /**
     * Get a scripting compatible string representation of the factory.
     * @return script compatible representation for this ElemFactory.
     * @see edu.jas.structure.Element#toScriptFactory()
     */
    //JAVA6only: @Override
    public String toScriptFactory() {
        // Python case
        return factory().toScript();
    }


    /**
     * Get the corresponding element factory.
     * @return factory for this Element.
     * @see edu.jas.structure.Element#factory()
     */
    public GenVectorModul factory() {
        return modul;
    }


    /**
     * clone method.
     * @see java.lang.Object#clone()
     */
    @Override
    @SuppressWarnings("unchecked")
    public GenVector copy() {
        //return modul.copy(this);
        ArrayList av = new ArrayList(val);
        return new GenVector(modul, av);
    }


    /**
     * test if this is equal to a zero vector.
     */
    public boolean isZERO() {
        return (0 == this.compareTo(modul.getZERO()));
    }


    /**
     * equals method.
     */
    @Override
    public boolean equals(Object other) {
        if (!(other instanceof GenVector)) {
            return false;
        }
        GenVector ovec = (GenVector) other;
        if (!modul.equals(ovec.modul)) {
            return false;
        }
        if (!val.equals(ovec.val)) {
            return false;
        }
        return true;
    }


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


    /**
     * compareTo, lexicographical comparison.
     * @param b other
     * @return 1 if (this < b), 0 if (this == b) or -1 if (this > b).
     */
    //JAVA6only: @Override
    public int compareTo(GenVector b) {
        if (!modul.equals(b.modul)) {
            return -1;
        }
        List oval = b.val;
        int i = 0;
        for (C c : val) {
            int s = c.compareTo(oval.get(i++));
            if (s != 0) {
                return s;
            }
        }
        return 0;
    }


    /**
     * sign of vector.
     * @return 1 if (this < 0), 0 if (this == 0) or -1 if (this > 0).
     */
    public int signum() {
        return compareTo(modul.getZERO());
    }


    /**
     * Sum of vectors.
     * @param b other vector.
     * @return this+b
     */
    public GenVector sum(GenVector b) {
        List oval = b.val;
        ArrayList a = new ArrayList(modul.cols);
        int i = 0;
        for (C c : val) {
            C e = c.sum(oval.get(i++));
            a.add(e);
        }
        return new GenVector(modul, a);
    }


    /**
     * Difference of vectors.
     * @param b other vector.
     * @return this-b
     */
    public GenVector subtract(GenVector b) {
        List oval = b.val;
        ArrayList a = new ArrayList(modul.cols);
        int i = 0;
        for (C c : val) {
            C e = c.subtract(oval.get(i++));
            a.add(e);
        }
        return new GenVector(modul, a);
    }


    /**
     * Negative of this vector.
     * @return -this
     */
    public GenVector negate() {
        ArrayList a = new ArrayList(modul.cols);
        for (C c : val) {
            C e = c.negate();
            a.add(e);
        }
        return new GenVector(modul, a);
    }


    /**
     * Absolute value of this vector.
     * @return abs(this)
     */
    public GenVector abs() {
        if (signum() < 0) {
            return negate();
        }
        return this;
    }


    /**
     * Product of this vector with scalar.
     * @param s scalar.
     * @return this*s
     */
    public GenVector scalarMultiply(C s) {
        ArrayList a = new ArrayList(modul.cols);
        for (C c : val) {
            C e = c.multiply(s);
            a.add(e);
        }
        return new GenVector(modul, a);
    }


    /**
     * Left product of this vector with scalar.
     * @param s scalar.
     * @return s*this
     */
    public GenVector leftScalarMultiply(C s) {
        ArrayList a = new ArrayList(modul.cols);
        for (C c : val) {
            C e = s.multiply(c);
            a.add(e);
        }
        return new GenVector(modul, a);
    }


    /**
     * Linear combination of this vector with scalar multiple of other vector.
     * @param s scalar.
     * @param b other vector.
     * @param t scalar.
     * @return this*s+b*t
     */
    public GenVector linearCombination(C s, GenVector b, C t) {
        List oval = b.val;
        ArrayList a = new ArrayList(modul.cols);
        int i = 0;
        for (C c : val) {
            C c1 = c.multiply(s);
            C c2 = oval.get(i++).multiply(t);
            C e = c1.sum(c2);
            a.add(e);
        }
        return new GenVector(modul, a);
    }


    /**
     * Linear combination of this vector with scalar multiple of other vector.
     * @param b other vector.
     * @param t scalar.
     * @return this+b*t
     */
    public GenVector linearCombination(GenVector b, C t) {
        List oval = b.val;
        ArrayList a = new ArrayList(modul.cols);
        int i = 0;
        for (C c : val) {
            C c2 = oval.get(i++).multiply(t);
            C e = c.sum(c2);
            a.add(e);
        }
        return new GenVector(modul, a);
    }


    /**
     * Left linear combination of this vector with scalar multiple of other
     * vector.
     * @param b other vector.
     * @param t scalar.
     * @return this+t*b
     */
    public GenVector linearCombination(C t, GenVector b) {
        List oval = b.val;
        ArrayList a = new ArrayList(modul.cols);
        int i = 0;
        for (C c : val) {
            C c2 = t.multiply(oval.get(i++));
            C e = c.sum(c2);
            a.add(e);
        }
        return new GenVector(modul, a);
    }


    /**
     * left linear combination of this vector with scalar multiple of other
     * vector.
     * @param s scalar.
     * @param b other vector.
     * @param t scalar.
     * @return s*this+t*b
     */
    public GenVector leftLinearCombination(C s, C t, GenVector b) {
        List oval = b.val;
        ArrayList a = new ArrayList(modul.cols);
        int i = 0;
        for (C c : val) {
            C c1 = s.multiply(c);
            C c2 = t.multiply(oval.get(i++));
            C e = c1.sum(c2);
            a.add(e);
        }
        return new GenVector(modul, a);
    }


    /**
     * scalar / dot product of this vector with other vector.
     * @param b other vector.
     * @return this . b
     */
    public C scalarProduct(GenVector b) {
        C a = modul.coFac.getZERO();
        List oval = b.val;
        int i = 0;
        for (C c : val) {
            C c2 = c.multiply(oval.get(i++));
            a = a.sum(c2);
        }
        return a;
    }


    /**
     * scalar / dot product of this vector with list of other vectors.
     * @param B list of vectors.
     * @return this * b
     */
    public GenVector scalarProduct(List> B) {
        GenVector A = modul.getZERO();
        int i = 0;
        for (C c : val) {
            GenVector b = B.get(i++);
            GenVector a = b.leftScalarMultiply(c);
            A = A.sum(a);
        }
        return A;
    }


    /**
     * right scalar / dot product of this vector with list of other vectors.
     * @param B list of vectors.
     * @return b * this
     */
    public GenVector rightScalarProduct(List> B) {
        GenVector A = modul.getZERO();
        int i = 0;
        for (C c : val) {
            GenVector b = B.get(i++);
            GenVector a = b.scalarMultiply(c);
            A = A.sum(a);
        }
        return A;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy