edu.columbia.cs.coms4701.geometry
Class AbstractVector<T extends java.lang.Number>

java.lang.Object
  extended by edu.columbia.cs.coms4701.geometry.AbstractVector<T>
All Implemented Interfaces:
GeometricVector<T>, java.lang.Iterable<T>
Direct Known Subclasses:
DiscreteVector

public abstract class AbstractVector<T extends java.lang.Number>
extends java.lang.Object
implements GeometricVector<T>

Abstract base class for n-dimensional vectors in a module (i.e. commutative ring, e.g. doubles, integers, complex numbers)

This base class supplies most of the essential functionality; however, subclasses must implement the #add and #multiply methods, due to annoying "features" of the Number abstract class

Version:
0.1

User: aaron Date: Aug 25, 2006 Time: 9:42:36 AM

Author:
aaron@cs

Field Summary
protected  java.util.ArrayList<T> components
           
protected static int PRIME
           
 
Fields inherited from interface edu.columbia.cs.coms4701.geometry.GeometricVector
X_DIM, Y_DIM, Z_DIM
 
Constructor Summary
AbstractVector(int dimension)
          Constructs a new vector of the given dimension.
 
Method Summary
abstract  GeometricVector<T> add(GeometricVector<T> theVector)
          add returns a *new* vector, equal to the vector sum of this vector and the argument.
protected  void checkDimension(int theDimension)
          Used to check the legality of a dimension argument.
protected  void checkEqualOrSmallerDimensionVector(GeometricVector<T> theVector)
          Used to check the legality of a vector argument.
 boolean equals(java.lang.Object obj)
          Two vectors are considered equal if they have the same dimension and corresponding components in each dimension are equal.
 T getComponent(int theDimension)
          Returns the component in the specified dimension.
abstract  T[] getComponents()
          Returns the components as an array
 int getDimension()
          Return the dimension of this vector.
 double getLength()
          Returns the length (norm) of this vector as a double.
 int hashCode()
          Implementing classes should override hashCode() to synchronize with equals()
 java.util.Iterator<T> iterator()
          Returns an iterator over the components of the vector.
abstract  GeometricVector<T> multiply(T theScalar)
          multiply returns a *new* vector, equal to the scalar multiple of this vector and the argument.
 java.lang.String toString()
          Implementing classes should override toString() to produce a simple string representation, e.g.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

PRIME

protected static int PRIME

components

protected java.util.ArrayList<T extends java.lang.Number> components
Constructor Detail

AbstractVector

public AbstractVector(int dimension)
Constructs a new vector of the given dimension.

Subclasses *must* override this constructor to initialize the contents of the components (e.g. to zero).

Subclasses must also provide a variable-length argument constructor that initializes components to provided values

Parameters:
dimension -
Method Detail

add

public abstract GeometricVector<T> add(GeometricVector<T> theVector)
add returns a *new* vector, equal to the vector sum of this vector and the argument.

Performs a component-by-component addition

Specified by:
add in interface GeometricVector<T extends java.lang.Number>
Parameters:
theVector - the vector to add, which must be of equal or lesser dimension
Returns:

multiply

public abstract GeometricVector<T> multiply(T theScalar)
multiply returns a *new* vector, equal to the scalar multiple of this vector and the argument.

Performs a component-by-component scalar multiplication

Specified by:
multiply in interface GeometricVector<T extends java.lang.Number>
Parameters:
theScalar - the scalar to multiply by (may be < 1, or indeed < 0)

iterator

public java.util.Iterator<T> iterator()
Returns an iterator over the components of the vector.

This permits easy use of the for-each loop over vector components.

Specified by:
iterator in interface GeometricVector<T extends java.lang.Number>
Specified by:
iterator in interface java.lang.Iterable<T extends java.lang.Number>

equals

public boolean equals(java.lang.Object obj)
Two vectors are considered equal if they have the same dimension and corresponding components in each dimension are equal.

Specified by:
equals in interface GeometricVector<T extends java.lang.Number>
Overrides:
equals in class java.lang.Object
Parameters:
obj -
Returns:
whether the vectors are equal

hashCode

public int hashCode()
Description copied from interface: GeometricVector
Implementing classes should override hashCode() to synchronize with equals()

Specified by:
hashCode in interface GeometricVector<T extends java.lang.Number>
Overrides:
hashCode in class java.lang.Object

getLength

public double getLength()
Returns the length (norm) of this vector as a double.

Should use ordinary Euclidean distance unless otherwise specified.

Specified by:
getLength in interface GeometricVector<T extends java.lang.Number>

getDimension

public int getDimension()
Return the dimension of this vector. Note that the dimension cannot be changed (i.e. must be established at construction time).

Specified by:
getDimension in interface GeometricVector<T extends java.lang.Number>
Returns:
the dimension of the vector

getComponent

public T getComponent(int theDimension)
Returns the component in the specified dimension.

Specified by:
getComponent in interface GeometricVector<T extends java.lang.Number>
Parameters:
theDimension - specifies which component to retrieve

getComponents

public abstract T[] getComponents()
Returns the components as an array

Specified by:
getComponents in interface GeometricVector<T extends java.lang.Number>

checkDimension

protected void checkDimension(int theDimension)
Used to check the legality of a dimension argument. Throws an exception if the argument is outside of bounds (0,n-1)

Throws:
java.lang.IllegalArgumentException

checkEqualOrSmallerDimensionVector

protected void checkEqualOrSmallerDimensionVector(GeometricVector<T> theVector)
Used to check the legality of a vector argument. Throws an exception if the vector's dimension is outside of bounds (1,n)

Throws:
java.lang.IllegalArgumentException

toString

public java.lang.String toString()
Description copied from interface: GeometricVector
Implementing classes should override toString() to produce a simple string representation, e.g. (0,2,4)

Specified by:
toString in interface GeometricVector<T extends java.lang.Number>
Overrides:
toString in class java.lang.Object