Package dsa
Class Vector
- java.lang.Object
-
- dsa.Vector
-
public class Vector extends Object
An immutable data type to represent an n-dimensional vector.
-
-
Constructor Summary
Constructors Constructor Description Vector(double[] coords)
Constructs a vector given its components.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Vector
add(Vector other)
Returns the sum of this vector andother
.int
dimension()
Returns the dimension of this vector.Vector
direction()
Returns a unit vector in the direction of this vector.double
dot(Vector other)
Returns the dot product of this vector andother
.double
get(int i)
Returns the ith component of this vector.double
magnitude()
Returns the magnitude of this vector.static void
main(String[] args)
Unit tests the data type.Vector
scale(double alpha)
Returns a scaled (by factorother
) copy of this vector.Vector
subtract(Vector other)
Returns the difference of this vector andother
.String
toString()
Returns a string representation of this vector.
-
-
-
Method Detail
-
get
public double get(int i)
Returns the ith component of this vector.- Parameters:
i
- the component index.- Returns:
- the ith component of this vector.
-
add
public Vector add(Vector other)
Returns the sum of this vector andother
.- Parameters:
other
- the other vector.- Returns:
- the sum of this vector and
other
.
-
subtract
public Vector subtract(Vector other)
Returns the difference of this vector andother
.- Parameters:
other
- the other vector.- Returns:
- the difference of this vector and
other
.
-
dot
public double dot(Vector other)
Returns the dot product of this vector andother
.- Parameters:
other
- the other vector.- Returns:
- the dot product of this vector and
other
.
-
scale
public Vector scale(double alpha)
Returns a scaled (by factorother
) copy of this vector.- Parameters:
alpha
- the scale factor.- Returns:
- a scaled (by factor
other
) copy of this vector.
-
direction
public Vector direction()
Returns a unit vector in the direction of this vector.- Returns:
- a unit vector in the direction of this vector.
-
magnitude
public double magnitude()
Returns the magnitude of this vector.- Returns:
- the magnitude of this vector.
-
dimension
public int dimension()
Returns the dimension of this vector.- Returns:
- the dimension of this vector.
-
toString
public String toString()
Returns a string representation of this vector.
-
main
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-