# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.algebra.linear.vector.doubles.dense

## Class VectorMathOperation

• java.lang.Object
• com.numericalmethod.suanshu.algebra.linear.vector.doubles.dense.VectorMathOperation

• public class VectorMathOperation
extends Object
This is a generic implementation of the math operations of double based Vector. It assumes no knowledge of how a vector is implemented. A particular implementation of Vector can improve performance by taking advantage of having access to the internal members.
• ### Constructor Summary

Constructors
Constructor and Description
VectorMathOperation()
• ### Method Summary

All Methods
Modifier and Type Method and Description
static Vector abs(Vector v)
Computes the absolute values of a vector, element-by-element.
DenseVector add(Vector v1, double c)
Adds a constant to a vector, element-by-element.
DenseVector add(Vector v1, Vector v2)
double angle(Vector v1, Vector v2)
Computes the angle between two vectors.
static Vector cos(Vector v)
Computes the cosine of a vector, element-by-element.
DenseVector divide(Vector v1, Vector v2)
A vector is divided by another vector, element-by-element.
static Vector exp(Vector v)
Computes the exponential of a vector, element-by-element.
double innerProduct(Vector v1, Vector v2)
Computes the inner or dot product of two vectors.
static Vector log(Vector v)
Computes the log of a vector, element-by-element.
DenseVector minus(Vector v1, double c)
Subtracts a constant from a vector, element-by-element.
DenseVector minus(Vector v1, Vector v2)
A vector subtracts another vector, element-by-element.
DenseVector multiply(Vector v1, Vector v2)
Multiplies two vectors, element-by-element.
double norm(Vector v1)
Computes the norm of a vector.
double norm(Vector v1, double p)
Computes the norm of a vector.
DenseVector opposite(Vector v1)
Multiples a vector by -1, element-by-element.
DenseVector pow(Vector v1, double c)
Takes a power of a vector, element-by-element.
static Vector rbinom(int n, int nTrials, Vector p)
Generates n random binomial numbers.
static Vector rbinom(int n, int nTrials, Vector p, RandomLongGenerator uniform)
Generates n random binomial numbers.
static Vector rnorm(int n)
Generates n random standard Normals.
static Vector rnorm(int n, RandomStandardNormalGenerator z)
Generates n random standard Normals.
DenseVector scaled(Vector v1, double c)
Scales a vector, element-by-element.
DenseVector scaled(Vector v1, Real c)
Scales a vector, element-by-element.
static Vector sin(Vector v)
Computes the sine of a vector, element-by-element.
static Vector sqrt(Vector v)
Computes the square roots of a vector, element-by-element.
static Vector squared(Vector v)
Computes the squares of a vector, element-by-element.
static double sum(Vector v)
Computes the sum of all vector elements.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Constructor Detail

• #### VectorMathOperation

public VectorMathOperation()
• ### Method Detail

public DenseVector add(Vector v1,
Vector v2)
Parameters:
v1 - a vector
v2 - a vector
Returns:
$$v1 + v2$$
• #### minus

public DenseVector minus(Vector v1,
Vector v2)
A vector subtracts another vector, element-by-element.
Parameters:
v1 - a vector
v2 - a vector
Returns:
$$v1 - v2$$
• #### multiply

public DenseVector multiply(Vector v1,
Vector v2)
Multiplies two vectors, element-by-element.
Parameters:
v1 - a vector
v2 - a vector
Returns:
$$v1 * v2$$
• #### divide

public DenseVector divide(Vector v1,
Vector v2)
A vector is divided by another vector, element-by-element.
Parameters:
v1 - a vector
v2 - a vector
Returns:
$$v1 / v2$$
• #### innerProduct

public double innerProduct(Vector v1,
Vector v2)
Computes the inner or dot product of two vectors.
Parameters:
v1 - a vector
v2 - a vector
Returns:
$$<v1, v2>$$
• #### pow

public DenseVector pow(Vector v1,
double c)
Takes a power of a vector, element-by-element.
Parameters:
v1 - a vector
c - a constant
Returns:
$$v1 ^ c$$
• #### scaled

public DenseVector scaled(Vector v1,
double c)
Scales a vector, element-by-element.
Parameters:
v1 - a vector
c - a constant
Returns:
$$v1 * c$$

public DenseVector add(Vector v1,
double c)
Adds a constant to a vector, element-by-element.
Parameters:
v1 - a vector
c - a constant
Returns:
$$v1 + c$$
• #### minus

public DenseVector minus(Vector v1,
double c)
Subtracts a constant from a vector, element-by-element.
Parameters:
v1 - a vector
c - a constant
Returns:
$$v1 - c$$
• #### scaled

public DenseVector scaled(Vector v1,
Real c)
Scales a vector, element-by-element.
Parameters:
v1 - a vector
c - a constant
Returns:
$$v1 * c$$
• #### opposite

public DenseVector opposite(Vector v1)
Multiples a vector by -1, element-by-element.
Parameters:
v1 - a vector
Returns:
$$-v1$$
• #### angle

public double angle(Vector v1,
Vector v2)
Computes the angle between two vectors.
Parameters:
v1 - a vector
v2 - a vector
Returns:
the angle between v1 and v2
• #### norm

public double norm(Vector v1,
double p)
Computes the norm of a vector.
Parameters:
v1 - a vector
p - the norm degree
Returns:
the p-th norm of a vector
• #### norm

public double norm(Vector v1)
Computes the norm of a vector.
Parameters:
v1 - a vector
Returns:
$$|v1|$$
• #### sum

public static double sum(Vector v)
Computes the sum of all vector elements.
Parameters:
v - a vector
Returns:
the element sum
• #### squared

public static Vector squared(Vector v)
Computes the squares of a vector, element-by-element.
Parameters:
v - a vector
Returns:
the squares
• #### sqrt

public static Vector sqrt(Vector v)
Computes the square roots of a vector, element-by-element.
Parameters:
v - a vector
Returns:
the square roots
• #### abs

public static Vector abs(Vector v)
Computes the absolute values of a vector, element-by-element.
Parameters:
v - a vector
Returns:
the absolute values
• #### sin

public static Vector sin(Vector v)
Computes the sine of a vector, element-by-element.
Parameters:
v - a vector
Returns:
the sine
• #### cos

public static Vector cos(Vector v)
Computes the cosine of a vector, element-by-element.
Parameters:
v - a vector
Returns:
the cosine
• #### exp

public static Vector exp(Vector v)
Computes the exponential of a vector, element-by-element.
Parameters:
v - a vector
Returns:
the exponential
• #### log

public static Vector log(Vector v)
Computes the log of a vector, element-by-element.
Parameters:
v - a vector
Returns:
the log
• #### rnorm

public static Vector rnorm(int n,
RandomStandardNormalGenerator z)
Generates n random standard Normals.
Parameters:
n - a number or a count
z - a standard random Normal generator
Returns:
n random standard Normals
• #### rnorm

public static Vector rnorm(int n)
Generates n random standard Normals.
Parameters:
n - a number or a count
Returns:
n random standard Normals
• #### rbinom

public static Vector rbinom(int n,
int nTrials,
Vector p,
RandomLongGenerator uniform)
Generates n random binomial numbers.
Parameters:
n - a number or a count
nTrials - the number of trials, a natural number
p - the success probability in each trial, [0, 1]
uniform - a uniform random number generator
Returns:
n random binomial Normals
• #### rbinom

public static Vector rbinom(int n,
int nTrials,
Vector p)
Generates n random binomial numbers.
Parameters:
n - a number or a count
nTrials - the number of trials, a natural number
p - the success probability in each trial, [0, 1]
Returns:
n random binomial Normals