Class BaseVector<V extends BaseVector<V>>

java.lang.Object
xal.tools.math.BaseVector<V>
All Implemented Interfaces:
Serializable, IArchive
Direct Known Subclasses:
EnergyVector, GenericVector, PhaseVector, R3, R4, R6, Rn

public abstract class BaseVector<V extends BaseVector<V>> extends Object implements IArchive, Serializable

Class Vector is the abstract base class for matrix objects supported in the XAL tools packages.

The current implementation uses an n×1 EJML matrix to represent the underlying vector. That is, the internal representation is a column vector.

Since:
Aug, 2002
Version:
Oct, 2013
Author:
Christopher K. Allen, Blaz Kranjc
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    attribute marker for data
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    BaseVector(double[] arrVals)
    Initializing constructor for bases class BaseVector.
    protected
    BaseVector(int intSize)
    Creates a new, uninitialized instance of a vector with the given size.
    protected
    BaseVector(int intSize, String strTokens)
    Parsing Constructor - creates an instance of the child class and initialize it according to a token string of element values.
    protected
    BaseVector(int intSize, DataAdaptor daSource)
    Initializing constructor for BaseVector.
    protected
    BaseVector(V vecParent)
    Copy constructor for Vector.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Assign this matrix to be the unity vector, the with all 1's.
    void
    Assign this vector to be the zero vector, specifically the vector containing all 0's.
    abstract V
    Base classes must override the clone operation in order to make deep copies of the current object.
    Perform a deep copy of this Vector object and return it.
    <U extends BaseVector<U>>
    void
    embedIn(U vecSup)
    Embeds this vector into the larger super-space represented by the given vector.
    boolean
    "Borrowed" implementation from AffineTransform, since it is based on double attribute values.
    double[]
    Returns a copy of the internal Java array containing the vector elements.
    double
    getElem(int iIndex)
    Get individual element of a vector at specified index
    double
    getElem(IIndex iIndex)
    Returns the vector component at the position indicated by the given index in the IIndex interface.
    int
    Get size of Vector (number of elements)
    protected org.ejml.data.DenseMatrix64F
    Return the internal matrix representation.
    int
    "Borrowed" implementation from AffineTransform, since it is based on double attribute values.
    double
    Vector inner product.
    boolean
    isEquivalentTo(V vecTest)
    Checks if the given vector is algebraically equivalent to this vector.
    <M extends SquareMatrix<M>>
    V
    leftMultiply(M mat)
    Vector left multiplication, or covariant operation of matrix on this vector (post-multiply vector by matrix).
    void
    load(DataAdaptor daptArchive)
    Restore the value of the this vector from the contents of a data archive.
    minus(V vecSub)
    Vector subtraction without destruction
    void
    minusEquals(V vecSub)
    Vector in-place subtraction.
    Element by element negation.
    void
    In place element-by-element negation of this vector.
    protected abstract V
    newInstance(double[] arrVecInt)
    Creates a new instance of this vector type with the given Java array as the internal representation.
    protected abstract V
    newInstance(int size)
    Creates a new, uninitialized instance of this vector type.
    protected V
    newInstance(org.ejml.data.DenseMatrix64F vecInit)
    Creates a new instance of this vector type initialized to the given implementation matrix.
    double
    Return the l1 norm of the vector.
    double
    Return the l2 norm of the vector.
    double
    Return the l∞ norm of the vector.
    plus(V vecAdd)
    Vector addition without destruction
    void
    plusEquals(V vecAdd)
    Vector in-place addition.
    void
    Print the vector contents to standard output.
    void
    Print the vector contents to an output stream, does not add new line.
    void
    Print the vector contents to an output stream, add new line character.
    <U extends BaseVector<U>>
    void
    projectOnto(U vecSub)
    Projects this vector onto the smaller subspace represented by the given vector.
    <M extends SquareMatrix<M>>
    V
    Vector right multiplication, or contra-variant operation of the matrix on this vector (pre-multiply vector by matrix).
    void
    save(DataAdaptor daptArchive)
    Save the value of this vector to disk.
    void
    setElem(int intIndex, double dblVal)
    Set individual element of a vector to given value
    void
    setElem(IIndex iIndex, double dblVal)
    Set individual element of a vector to given value.
    void
    setVector(double[] arrVector)
    Sets the entire vector to the values given in the Java primitive type double array.
    void
    setVector(String strValues)
    Parsing assignment - set the vector value according to a token string of element values.
    void
    setVector(V vecParent)
    Sets the entire vector to the values given to the value of the new vector.
    times(double s)
    Scalar multiplication
    void
    timesEquals(double s)
    In place scalar multiplication
    Convert the contents of the matrix to a string representation.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • BaseVector

      protected BaseVector(int intSize)
      Creates a new, uninitialized instance of a vector with the given size. The vector contains all zeros.
      Parameters:
      intSize - the vector size of this object
      Throws:
      UnsupportedOperationException - base class has not defined a public, zero-argument constructor
    • BaseVector

      protected BaseVector(V vecParent) throws UnsupportedOperationException
      Copy constructor for Vector. Creates a deep copy of the given object. The dimensions are set and the internal array is cloned.
      Parameters:
      vecParent - the vector to be cloned
      Throws:
      UnsupportedOperationException - base class has not defined a public, zero-argument constructor
      Since:
      Sep 25, 2013
    • BaseVector

      protected BaseVector(int intSize, String strTokens) throws NumberFormatException

      Parsing Constructor - creates an instance of the child class and initialize it according to a token string of element values.

      The token string argument is assumed to be one-dimensional and delimited by any of the characters " ,()[]{}" Repeated, contiguous delimiters are parsed together. This conditions allows a variety of parseable string representations. For example,

          { 1, 2, 3, 4 }

      and

          [1 2 3 4]

      would parse to the same homogeneous vector (1, 2, 3, 4 | 1).

      Parameters:
      intSize - the matrix size of this object
      strTokens - token vector of getSize() numeric values
      Throws:
      NumberFormatException - bad number format, unparseable
    • BaseVector

      protected BaseVector(double[] arrVals)

      Initializing constructor for bases class BaseVector. Sets the entire matrix to the values given in the Java primitive type double array. The argument itself remains unchanged.

      The dimensions of the new vector will be the length of the given Java double array.

      Parameters:
      arrVals - Java primitive array containing new vector values
      Since:
      Oct 4, 2013
    • BaseVector

      protected BaseVector(int intSize, DataAdaptor daSource)
      Initializing constructor for BaseVector. The vector values are taken from the data source provided.
      Parameters:
      intSize - size of this vector
      daSource - data source containing the initial values of this vector
      Since:
      Nov 5, 2013
  • Method Details

    • setVector

      public void setVector(double[] arrVector) throws IllegalArgumentException
      Sets the entire vector to the values given in the Java primitive type double array.

      NOTE!

      TODO This is not going to work for homogeneous coordinates!

      Parameters:
      arrVector - Java primitive array containing new vector values
      Throws:
      IllegalArgumentException - the argument must have the same dimensions as this matrix
      Since:
      Oct 4, 2013
    • setVector

      public void setVector(V vecParent)
      Sets the entire vector to the values given to the value of the new vector.
      Parameters:
      vecParent - Java primitive array containing new vector values
      Throws:
      ArrayIndexOutOfBoundsException - the argument must have the same dimensions as this matrix
      Since:
      Oct 4, 2013
    • setVector

      public void setVector(String strValues) throws NumberFormatException
      Parsing assignment - set the vector value according to a token string of element values. The token string argument is assumed to be one-dimensional and packed by column (aka FORTRAN).

      NOTE!

      TODO This is not going to work for homogeneous coordinates!

      Parameters:
      strValues - token vector of SIZE2 numeric values
      Throws:
      NumberFormatException - bad number format, unparseable
    • setElem

      public void setElem(int intIndex, double dblVal) throws ArrayIndexOutOfBoundsException
      Set individual element of a vector to given value
      Parameters:
      intIndex - index of element
      dblVal - new value of element
      Throws:
      ArrayIndexOutOfBoundsException - iIndex is larger than the vector
    • setElem

      public void setElem(IIndex iIndex, double dblVal) throws ArrayIndexOutOfBoundsException
      Set individual element of a vector to given value. The index is assumed to be an enumeration exposing the IIndex interface. That interface interface belongs to the BaseMatrix namespace. In this manner matrix indices can be used to set vector component values.
      Parameters:
      iIndex - index of element taken from the interface IIndex of class BaseMatrix
      dblVal - new value of element
      Throws:
      ArrayIndexOutOfBoundsException - iIndex is larger than the vector
    • getSize

      public int getSize()
      Get size of Vector (number of elements)
      Returns:
      vector length
    • getElem

      public double getElem(int iIndex) throws ArrayIndexOutOfBoundsException
      Get individual element of a vector at specified index
      Parameters:
      iIndex - data source providing index of element
      Returns:
      value of element at given index
      Throws:
      ArrayIndexOutOfBoundsException - iIndex is larger than vector size
    • getElem

      public double getElem(IIndex iIndex) throws ArrayIndexOutOfBoundsException

      Returns the vector component at the position indicated by the given index in the IIndex interface. That interface belongs to the BaseMatrix namespace. In this way matrix indices can be used to get vector component values.

      NOTES

      · It is expected that the object exposing the IIndex interface is an enumeration class restricting the number of possible index values.
      · Consequently we do not declare a thrown exception assuming that that enumeration class eliminates the possibility of an out of bounds error.

      Parameters:
      iIndex - source containing the vector index
      Returns:
      value of the matrix element at the given row and column
      Throws:
      ArrayIndexOutOfBoundsException - iIndex is larger than the vector
    • getArrayCopy

      public double[] getArrayCopy()
      Returns a copy of the internal Java array containing the vector elements. The array dimensions are given by the size of this matrix, available from getSize(). The returned array is a copy of this vector thus manipulation with not affect the parent object.
      Returns:
      copied array of vector values
      Since:
      Sep 25, 2013
    • clone

      public abstract V clone()
      Base classes must override the clone operation in order to make deep copies of the current object. This operation cannot be done without the exact type.
      Overrides:
      clone in class Object
      Since:
      Jul 3, 2014
      See Also:
    • toString

      public String toString()
      Convert the contents of the matrix to a string representation. The format is similar to that of Mathematica. Specifically,

      { a b c d }
      Overrides:
      toString in class Object
      Returns:
      string representation of the matrix
    • hashCode

      public int hashCode()
      "Borrowed" implementation from AffineTransform, since it is based on double attribute values. Must implement hashCode to be consistent with equals as specified by contract of hashCode in Object.
      Overrides:
      hashCode in class Object
      Returns:
      a hashCode for this object
    • equals

      public boolean equals(Object obj)
      "Borrowed" implementation from AffineTransform, since it is based on double attribute values. Must implement hashCode to be consistent with equals as specified by contract of hashCode in Object.
      Overrides:
      equals in class Object
      Returns:
      True if obj is equal to this
    • copyVector

      public V copyVector()
      Perform a deep copy of this Vector object and return it.
      Returns:
      a cloned copy of this vector
    • assignZero

      public void assignZero()
      Assign this vector to be the zero vector, specifically the vector containing all 0's.
      Since:
      Oct 3, 2013
    • assignUnity

      public void assignUnity()
      Assign this matrix to be the unity vector, the with all 1's.
      Since:
      Oct 3, 2013
    • projectOnto

      public <U extends BaseVector<U>> void projectOnto(U vecSub)

      Projects this vector onto the smaller subspace represented by the given vector. For example, say this vector v is an element of Rn and the given vector u is an element of Rm where mn. Then v decomposes as v = (v1 v2) ∈ Rm × Rn-m. That component v1 that lives in the subspace Rm is projected onto the given vector.

      To make it simple, the first m components of this vector are used to set all the values of the given vector, in respective order. If the give vector is larger than this vector an exception is thrown.

      Parameters:
      vecSub - The vector to receive the projection of this vector (determines size)
      Since:
      Oct 18, 2013
    • embedIn

      public <U extends BaseVector<U>> void embedIn(U vecSup)

      Embeds this vector into the larger super-space represented by the given vector. For example, say this vector v is an element of Rm and the given vector u is an element of Rn where mn. Then u decomposes as u = (u1 u2) ∈ Rm × Rn-m. This vector v is embedded as that component u1 that lives in the sub-space RmRm × Rn-m.

      To make it simple, the first m components of the given vector are set to the components of this vector, in respective order. If the give vector is smaller than this vector an exception is thrown.

      Parameters:
      vecSup - The vector to receive the embedding of this vector
      Since:
      Oct 18, 2013
    • isEquivalentTo

      public boolean isEquivalentTo(V vecTest)
      Checks if the given vector is algebraically equivalent to this vector. That is, it is equal in size and element values.
      Parameters:
      vecTest - vector under equivalency test
      Returns:
      true if the argument is equivalent to this vector, false if otherwise
      Since:
      Oct 1, 2013
    • negate

      public V negate()
      Element by element negation. A new object is returned and the current one is unmodified.
      Returns:
      antipodal vector of the current object
      Since:
      Oct 10, 2013
    • negateEquals

      public void negateEquals()
      In place element-by-element negation of this vector.
      Since:
      Oct 10, 2013
    • plusEquals

      public void plusEquals(V vecAdd)
      Vector in-place addition. Add the given vector to this vector which then takes on the summed value.
      Parameters:
      vecAdd - Vector to add to this vector (addend)
    • plus

      public V plus(V vecAdd)
      Vector addition without destruction
      Parameters:
      vecAdd - vector added to this one (addend)
      Returns:
      sum of this vector and given vector,
    • minusEquals

      public void minusEquals(V vecSub)
      Vector in-place subtraction. Subtracts the given vector from this vector which then takes the new value.
      Parameters:
      vecSub - Vector to subtract from this vector (subtrahend)
    • minus

      public V minus(V vecSub)
      Vector subtraction without destruction
      Parameters:
      vecSub - vector subtracted from this one (subtrahend)
      Returns:
      difference of this vector and the given vector,
    • times

      public V times(double s)
      Scalar multiplication
      Parameters:
      s - scalar value
      Returns:
      result of scalar multiplication
    • timesEquals

      public void timesEquals(double s)
      In place scalar multiplication
      Parameters:
      s - scalar
    • innerProd

      public double innerProd(V v)
      Vector inner product. Computes the inner product of of this vector with the given vector.
      Parameters:
      v - second vector
      Returns:
      inner product of this vector and argument
    • leftMultiply

      public <M extends SquareMatrix<M>> V leftMultiply(M mat)
      Vector left multiplication, or covariant operation of matrix on this vector (post-multiply vector by matrix).
      Parameters:
      mat - matrix operator
      Returns:
      result of vector-matrix product
    • rightMultiply

      public <M extends SquareMatrix<M>> V rightMultiply(M mat)
      Vector right multiplication, or contra-variant operation of the matrix on this vector (pre-multiply vector by matrix).
      Parameters:
      mat - matrix operator
      Returns:
      result of vector-matrix product
    • norm1

      public double norm1()
      Return the l1 norm of the vector.
      Returns:
      ||z||1 = Σi |zi|
    • norm2

      public double norm2()
      Return the l2 norm of the vector.
      Returns:
      ||z||2 = [ Σi zi2 ]1/2
    • normInf

      public double normInf()
      Return the l∞ norm of the vector.
      Returns:
      ||z|| = supi |zi|
    • save

      public void save(DataAdaptor daptArchive)
      Save the value of this vector to disk.
      Specified by:
      save in interface IArchive
      Parameters:
      daptArchive - interface to data sink
      See Also:
    • load

      public void load(DataAdaptor daptArchive) throws DataFormatException
      Restore the value of the this vector from the contents of a data archive.
      Specified by:
      load in interface IArchive
      Parameters:
      daptArchive - interface to data source
      Throws:
      DataFormatException - malformed data
      See Also:
    • print

      public void print()
      Print the vector contents to standard output.
    • print

      public void print(PrintWriter os)
      Print the vector contents to an output stream, does not add new line.
      Parameters:
      os - output stream object
    • println

      public void println(PrintWriter os)
      Print the vector contents to an output stream, add new line character.
      Parameters:
      os - output stream object
    • getVector

      protected org.ejml.data.DenseMatrix64F getVector()
      Return the internal matrix representation.
      Returns:
      the internal implementation matrix object
    • newInstance

      protected abstract V newInstance(int size)
      Creates a new, uninitialized instance of this vector type.
      Returns:
      uninitialized vector object of type V
      Since:
      Oct 1, 2013
    • newInstance

      protected abstract V newInstance(double[] arrVecInt)
      Creates a new instance of this vector type with the given Java array as the internal representation.
      Parameters:
      arrVecInt - new vector's guts
      Returns:
      new instance of this vector type with the internal representation
      Since:
      Jul 24, 2015 by Christopher K. Allen
    • newInstance

      protected V newInstance(org.ejml.data.DenseMatrix64F vecInit)
      Creates a new instance of this vector type initialized to the given implementation matrix.
      Parameters:
      vecInit - implementation vector containing initialization values
      Returns:
      initialized vector object of type V
      Since:
      Oct 1, 2013