## Class ImmutableVector

• All Implemented Interfaces:
Vector, AbelianGroup<Vector>, BanachSpace<Vector,​Real>, HilbertSpace<Vector,​Real>, VectorSpace<Vector,​Real>, DeepCopyable

public class ImmutableVector
extends Object
implements Vector
This is a read-only view of a Vector instance. It keeps the reference to the instance and delegates all operations to the instance except for set(int, double) which will always result in an VectorAccessException. It may, however, still be modified indirectly via the reference of the original vector. To ensure complete immutability, an independent copy is needed. That is,
 ImmutableVector immutable = new ImmutableVector(v.deepCopy()); 
Note that the returned values of all operations (e.g., add) has the same types as the original ones.
• ### Constructor Summary

Constructors
Constructor Description
ImmutableVector​(Vector v)
Construct a read-only version of a vector.
• ### Method Summary

All Methods
Modifier and Type Method Description
Vector add​(double scalar)
Add a constant to all entries in this vector.
Vector add​(Vector that)
$$this + that$$
double angle​(Vector that)
Measure the angle, $$\theta$$, between this and that.
ImmutableVector deepCopy()
The implementation returns an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
Vector divide​(Vector that)
Divide this by that, entry-by-entry.
boolean equals​(Object obj)
double get​(int index)
Get the value at position i.
int hashCode()
double innerProduct​(Vector that)
Inner product in the Euclidean space is the dot product.
Vector minus​(double scalar)
Subtract a constant from all entries in this vector.
Vector minus​(Vector that)
$$this - that$$
Vector multiply​(Vector that)
Multiply this by that, entry-by-entry.
double norm()
Compute the length or magnitude or Euclidean norm of a vector, namely, $$\|v\|$$.
double norm​(double p)
Gets the $$L^p$$-norm $$\|v\|_p$$ of this vector.
Vector opposite()
Get the opposite of this vector.
Vector pow​(double scalar)
Take the exponentiation of all entries in this vector, entry-by-entry.
Vector scaled​(double scalar)
Scale this vector by a constant, entry-by-entry.
Vector scaled​(Real scalar)
Scale this vector by a constant, entry-by-entry.
void set​(int i, double value)
This method is overridden to always throw VectorAccessException.
int size()
Get the length of this vector.
double[] toArray()
Cast this vector into a 1D double[].
String toString()
Vector ZERO()
Get a 0-vector that has the same length as this vector.
• ### Methods inherited from class java.lang.Object

clone, finalize, getClass, notify, notifyAll, wait, wait, wait
• ### Constructor Detail

• #### ImmutableVector

public ImmutableVector​(Vector v)
Construct a read-only version of a vector. Note that, however, changing the original vector changes the "immutable" version as well.
Parameters:
v - a vector
• ### Method Detail

• #### set

public final void set​(int i,
double value)
This method is overridden to always throw VectorAccessException.
Specified by:
set in interface Vector
Parameters:
i - the index of the entry to change. The indices are counting from 1, NOT 0.
value - the value to change to
Throws:
VectorAccessException - always
• #### size

public int size()
Description copied from interface: Vector
Get the length of this vector.
Specified by:
size in interface Vector
Returns:
the vector length
• #### get

public double get​(int index)
Description copied from interface: Vector
Get the value at position i.
Specified by:
get in interface Vector
Parameters:
index - the position of a vector entry
Returns:
v[i]

public Vector add​(Vector that)
Description copied from interface: Vector
$$this + that$$
Specified by:
add in interface AbelianGroup<Vector>
Specified by:
add in interface Vector
Parameters:
that - a vector
Returns:
$$this + that$$
• #### minus

public Vector minus​(Vector that)
Description copied from interface: Vector
$$this - that$$
Specified by:
minus in interface AbelianGroup<Vector>
Specified by:
minus in interface Vector
Parameters:
that - a vector
Returns:
$$this - that$$
• #### multiply

public Vector multiply​(Vector that)
Description copied from interface: Vector
Multiply this by that, entry-by-entry.
Specified by:
multiply in interface Vector
Parameters:
that - a vector
Returns:
$$this \cdot that$$
• #### divide

public Vector divide​(Vector that)
Description copied from interface: Vector
Divide this by that, entry-by-entry.
Specified by:
divide in interface Vector
Parameters:
that - a vector
Returns:
$$this / that$$

public Vector add​(double scalar)
Description copied from interface: Vector
Add a constant to all entries in this vector.
Specified by:
add in interface Vector
Parameters:
scalar - a constant
Returns:
$$v + c$$
• #### minus

public Vector minus​(double scalar)
Description copied from interface: Vector
Subtract a constant from all entries in this vector.
Specified by:
minus in interface Vector
Parameters:
scalar - a constant
Returns:
$$v - c$$
• #### innerProduct

public double innerProduct​(Vector that)
Description copied from interface: Vector
Inner product in the Euclidean space is the dot product.
Specified by:
innerProduct in interface HilbertSpace<Vector,​Real>
Specified by:
innerProduct in interface Vector
Parameters:
that - a vector
Returns:
$$this \cdot that$$
Wikipedia: Dot product
• #### pow

public Vector pow​(double scalar)
Description copied from interface: Vector
Take the exponentiation of all entries in this vector, entry-by-entry.
Specified by:
pow in interface Vector
Parameters:
scalar - a constant
Returns:
$$v ^ c$$
• #### scaled

public Vector scaled​(double scalar)
Description copied from interface: Vector
Scale this vector by a constant, entry-by-entry. Here is a way to get a unit version of the vector:
 vector.scaled(1. / vector.norm()) 
Specified by:
scaled in interface Vector
Parameters:
scalar - a constant
Returns:
$$c \times this$$
• #### scaled

public Vector scaled​(Real scalar)
Description copied from interface: Vector
Scale this vector by a constant, entry-by-entry. Here is a way to get a unit version of the vector:
 vector.scaled(1. / vector.norm()) 
Specified by:
scaled in interface Vector
Specified by:
scaled in interface VectorSpace<Vector,​Real>
Parameters:
scalar - a constant
Returns:
$$c \times this$$
Wikipedia: Scalar multiplication
• #### norm

public double norm()
Description copied from interface: Vector
Compute the length or magnitude or Euclidean norm of a vector, namely, $$\|v\|$$.
Specified by:
norm in interface BanachSpace<Vector,​Real>
Specified by:
norm in interface Vector
Returns:
the Euclidean norm
Wikipedia: Norm (mathematics)
• #### angle

public double angle​(Vector that)
Description copied from interface: Vector
Measure the angle, $$\theta$$, between this and that. That is, $this \cdot that = \|this\| \times \|that\| \times \cos \theta$
Specified by:
angle in interface HilbertSpace<Vector,​Real>
Specified by:
angle in interface Vector
Parameters:
that - a vector
Returns:
the angle, $$\theta$$, between this and that
• #### opposite

public Vector opposite()
Description copied from interface: Vector
Get the opposite of this vector.
Specified by:
opposite in interface AbelianGroup<Vector>
Specified by:
opposite in interface Vector
Returns:
-v
• #### ZERO

public Vector ZERO()
Description copied from interface: Vector
Get a 0-vector that has the same length as this vector.
Specified by:
ZERO in interface AbelianGroup<Vector>
Specified by:
ZERO in interface Vector
Returns:
the 0-vector
• #### toArray

public double[] toArray()
Description copied from interface: Vector
Cast this vector into a 1D double[].
Specified by:
toArray in interface Vector
Returns:
a copy of all vector entries as a double[]
• #### deepCopy

public ImmutableVector deepCopy()
Description copied from interface: DeepCopyable
The implementation returns an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
Specified by:
deepCopy in interface DeepCopyable
Specified by:
deepCopy in interface Vector
Returns:
an independent (deep) copy of the instance
• #### toString

public String toString()
Overrides:
toString in class Object
• #### equals

public boolean equals​(Object obj)
Overrides:
equals in class Object
• #### hashCode

public int hashCode()
Overrides:
hashCode in class Object