public final class Rn extends Object
This includes a set of methods related to linear transformations of Rn. Because of the need to have matrices stored in contiguous memory (rather than Java-style as an array of pointers), matrices are also of type double[]. Since all the matrices that occur here are square ones, the actual dimensions can be calculated easily from the over-all length. For example, an array of length 16 corresponds to a 4x4 matrix. Currently the limit on the size of these square matrices is 10x10. Methods where there is a lower limit annotate this restriction.
Conventions:
public static double[] add(double[] dst, double[]src1, double[] src2)
. The operation is then given by dst = src1 + src2
. Care has
been taken to allow re-use of the same variable, for example, in the
invocation Rn.add(a, a, a)
which adds a vector a to
itself and stores the result back into itself. Rn.add(d, c, Rn.add(null, a, b))
in
the process.null
value for the destination vector or
matrix. The method itself in this case then allocates a new one, fills it
accordingly, and returns it. Unless otherwise noted, this is the behavior of
the static methods in this class.
Methods try as much as possible to react reasonably to differing argument types. In general, the methods check the lengths of the arguments, determine a minimum valid length on which the desired operation can be carried out, and does so. This allows some flexibility in terms of lengths of input vectors which the following paragraph attempts to justify on practical grounds.
For example. the method
matrixTimesVector(double[], double[], double[])
is happiest when the
dimension of the square matrix matches the length of the input vectors.
However, it is often the case that users have dehomogenized coordinates for
vectors, that is, one dimension less than the dimension of the matrix. For
example, points in Euclidean 3-space are naturally stored as 3-vectors
although the natural setting for isometries is 4x4 matrices. In this case the
method assumes an implicit final coordinate of 1.0 and carries out the
multiplication accordingly. At the same time, the method also operates
correctly on 4-vectors.
Some commonly used methods come in two varieties: one acts on single vectors
( double[]
) and another acts on array of vectors (
double[][]
). Compare
matrixTimesVector(double[], double[], double[])
and
matrixTimesVector(double[][], double[], double[][])
.
Pn
Modifier and Type | Field and Description |
---|---|
static double |
TOLERANCE
The default tolerance used for checking numerical equality = 10E-8.
|
Modifier and Type | Method and Description |
---|---|
static double[] |
abs(double[] dst,
double[] src) |
static double[] |
add(double[] dst,
double[] src1,
double[] src2)
Add the vector src1 to the vector src2 and put the result
in dst.
|
static double[] |
adjoint(double[] dst,
double[] src)
Calculate the adjoint of the square matrix src and put the result
into dst.
|
static double[] |
average(double[] dst,
double[][] vlist)
Calculate the average of the elements in the vector list vlist.
|
static double[] |
barycentricTriangleInterp(double[] dst,
double[][] corners,
double[] weights)
Calculate the euclidean coordinates of the point whose barycentric
coordinates with respect to the triangle corners are
coords.
|
static double[] |
bezierCombination(double[] dst,
double t,
double[] v0,
double[] t0,
double[] t1,
double[] v1) |
static double[] |
bilinearInterpolation(double[] ds,
double u,
double v,
double[] vb,
double[] vt,
double[] cb,
double[] ct) |
static double[][] |
calculateBounds(double[][] bounds,
double[][] vlist)
Given a list of vectors, calculate the minimum and maximum values in each
coordinate.
|
static double |
cofactor(double[] m,
int row,
int column)
Calculate the (i,j)th cofactor of the matrix m The
dimension of the matrix m can be no greater than 5.
|
static double[][] |
completeBasis(double[][] dst,
double[][] partial)
The array partial contains m n-vectors which are assumed to
be linearly independent.
|
static double[] |
conjugateByMatrix(double[] dst,
double[] m,
double[] c)
Form the conjugate of the matrix m by the matrix c:
|
static double[] |
convertArray2DToArray1D(double[] target,
double[][] src)
inlines the given 2-dim array.
|
static double[] |
convertArray3DToArray1D(double[][][] V) |
static double[] |
convertArray3DToArray1D(double[][][] V,
int usample,
int vsample) |
static double[][] |
convertArray3DToArray2D(double[][][] V) |
static float[] |
convertDoubleToFloatArray(double[] ds) |
static double[] |
copy(double[] dst,
double[] src)
Make a copy of src in dst.
|
static double[] |
crossProduct(double[] dst,
double[] u,
double[] v)
Calculate the cross product of the two vectors u and v.
|
static double |
determinant(double[] m)
Calculate the determinate of the square matrix m.
|
static double |
determinant(double[][] m) |
static double[] |
diagonalMatrix(double[] dst,
double[] entries) |
static boolean |
equals(double[] u,
double[] v)
Calculate whether the two arrays contain exactly the same values.
|
static boolean |
equals(double[] u,
double[] v,
double tol)
Calculate whether the two arrays contain the same values.
|
static double |
euclideanAngle(double[] u,
double[] v)
Calculate the euclidean angle between the vectors u and v.
|
static double |
euclideanDistance(double[] u,
double[] v)
Calculates and returns the euclidean distance between the two points
u and v.
|
static double |
euclideanDistanceSquared(double[] u,
double[] v)
calculates and returns the square of the euclidean distance between the
two points u and v.
|
static double |
euclideanNorm(double[] vec)
Calculates and returns the euclidean norm of the input vector.
|
static double |
euclideanNormSquared(double[] vec)
Calculates and returns the square of the euclidean norm of the input
vector.
|
static double[] |
extractSubmatrix(double[] subm,
double[] src,
int l,
int r,
int t,
int b)
Extract a rectangular submatrix of the input matrix.
|
static double[] |
identityMatrix(int dim)
Create and return an identity matrix of dimension dim.
|
static double |
innerProduct(double[] u,
double[] v)
Calculate and return the euclidean inner product of the two vectors.
|
static double |
innerProduct(double[] u,
double[] v,
int n)
Calculate and return at most n terms of the inner product of the two
vectors.
|
static double[] |
inverse(double[] minvIn,
double[] m)
Use Gaussian pivoting to calculate the inverse matrix of the input
matrix.
|
static boolean |
isIdentityMatrix(double[] mat,
double tol)
Calculates whether the input matrix in within tol of the identity
matrix.
|
static boolean |
isNan(double[] ds) |
static boolean |
isSpecialMatrix(double[] mat,
double tol)
Calculates whether the input matrix is within tol of being a
"special" matrix.
|
static boolean |
isZero(double[] iline) |
static boolean |
isZero(double[] iline,
double tol) |
static double[] |
linearCombination(double[] dst,
double a,
double[] aVec,
double b,
double[] bVec)
Calculate the linear combination dst = a*aVec + b*bVec.
|
static double |
manhattanNorm(double[] vec)
Calculate the "manhattan norm" of the input vector.
|
static double |
manhattanNormDistance(double[] u,
double[] v)
Calculate the "manhattan norm" distance between the two input vectors.
|
static double[][] |
matrixTimesVector(double[][] dst,
double[] m,
double[][] src)
A vectorized version of
matrixTimesVector(double[], double[], double[]) . |
static double[] |
matrixTimesVector(double[] dst,
double[] m,
double[] src)
Multiply the input vector src by the matrix m and put the
result into dst.
|
static String |
matrixToJavaString(double[] v)
Like
matrixToString(double[]) , but formatted for direct
insertion as java source code |
static String |
matrixToString(double[] m)
Print the square matrix m into a string using default format from
String.format(java.lang.String, java.lang.Object[]) and return
the string. |
static String |
matrixToString(double[] m,
String formatString)
Print the square matrix m into a string using formatString
and return the string.
|
static double[] |
max(double[] dst,
double[] src1,
double[] src2)
Calculate the maximum of two vectors, coordinate-wise.
|
static double |
maxNorm(double[] vec)
Calculate the "max norm" of a vector.
|
static double |
maxNormDistance(double[] u,
double[] v)
Calculate the distance between two points based on the
"max norm" . |
static double[] |
min(double[] dst,
double[] src1,
double[] src2)
Calculate the minimum of two vectors, coordinate-wise.
|
static int |
mysqrt(int sq)
Returns the square root of the
sq parameter. |
static double[] |
negate(double[] dst,
double[] src)
dst[i] = -src[i]
|
static double[][] |
normalize(double[][] dst,
double[][] src) |
static double[] |
normalize(double[] dst,
double[] src)
Normalize src using the
euclidean norm . |
static double[] |
permutationMatrix(double[] dst,
int[] perm)
Calculate a permutation matrix which sends e(i) to e(perm[i])
|
static double[] |
planeParallelToPassingThrough(double[] plane,
double[] ds,
double[] ds2) |
static double[] |
polarDecompose(double[] q,
double[] s,
double[] m) |
static double[] |
projectOnto(double[] dst,
double[] src,
double[] fixed)
Project orthogonally src onto fixed.
|
static double[] |
projectOntoComplement(double[] dst,
double[] src,
double[] fixed)
Project src onto the orthogonal complement of fixed.
|
static double[] |
setDiagonalMatrix(double[] dst,
double[] diag)
Construct and return a diagonal matrix with the entries given by the
vector diag.
|
static double[] |
setEuclideanNorm(double[] dst,
double length,
double[] src)
Scale the vector to have the given length.
|
static double[] |
setIdentityMatrix(double[] mat)
Set the matrix to be the identity matrix.
|
static float[] |
setIdentityMatrix(float[] mat) |
static double[] |
setToLength(double[] p1,
double[] p12,
double rad) |
static double[] |
setToValue(double[] dst,
double val)
Set the destination to vector to have constant value val.
|
static double[] |
setToValue(double[] dst,
double x,
double y)
Initialize the 2-vector dst.
|
static double[] |
setToValue(double[] dst,
double x,
double y,
double z)
Initialize the 3-vector dst.
|
static double[] |
setToValue(double[] dst,
double x,
double y,
double z,
double w)
Initialize the 4-vector dst.
|
static double[] |
submatrix(double[] subm,
double[] m,
int row,
int column)
Extract the submatrix gotten by deleting the given row and column.
|
static double[][] |
subtract(double[][] dst,
double[][] src1,
double[][] src2) |
static double[] |
subtract(double[] dst,
double[] src1,
double[] src2)
dst[i] = src1[i] - src2[i]
|
static void |
swap(double[] u,
double[] v)
Swap the contents of the two vectors.
|
static double[][] |
times(double[][] dst,
double factor,
double[][] src)
A vectorized version of
times(double[], double, double[]) ; |
static double[] |
times(double[] dst,
double[] src1,
double[] src2)
Multiply the square matrices according to the equation dst = src1 *
src2 .
|
static double[] |
times(double[] dst,
double factor,
double[] src)
Multiply the vector
|
static float[] |
times(float[] dst,
float[] src1,
float[] src2) |
static String |
toString(double[] v)
Print the array v into a string using default format from
String.format(java.lang.String, java.lang.Object[]) and return
the string. |
static String |
toString(double[][] v)
A vectorized version of
toString(double[]) . |
static String |
toString(double[][][] v) |
static String |
toString(double[][] v,
int n) |
static String |
toString(double[] v,
String formatString)
Print the array v into a string using formatString and
return the string.
|
static String |
toString(float[] v) |
static double |
trace(double[] m)
Calculate the trace of the given square matrix.
|
static double[] |
transpose(double[] dst,
double[] src)
Transpose the given square matrix src into dst.
|
static float[] |
transposeD2F(float[] dst,
double[] src)
Transpose the given 4x4 matrix src into dst .
|
static double[] |
transposeF2D(double[] dst,
float[] src)
Transpose the given 4x4 matrix src into dst .
|
public static final double TOLERANCE
public static double[] abs(double[] dst, double[] src)
ds
- ds2
- public static double[] add(double[] dst, double[] src1, double[] src2)
dst
- src1
- src2
- public static double[] adjoint(double[] dst, double[] src)
dst
- may be nullsrc
- may be = dstpublic static double[] average(double[] dst, double[][] vlist)
dst
- vlist
- public static double[] barycentricTriangleInterp(double[] dst, double[][] corners, double[] weights)
dst
- double[n] (may be null)corners
- double[3][n]weights
- double[3]public static double[][] calculateBounds(double[][] bounds, double[][] vlist)
bounds
- double[2][n]vlist
- double[][n]public static double cofactor(double[] m, int row, int column)
m
- double[n][n]row
- column
- public static double[] conjugateByMatrix(double[] dst, double[] m, double[] c)
dst
- double[n][n]m
- double[n][n]c
- double[n][n]public static double[] convertArray2DToArray1D(double[] target, double[][] src)
public static double[] convertArray3DToArray1D(double[][][] V)
public static double[] convertArray3DToArray1D(double[][][] V, int usample, int vsample)
V
- public static double[][] convertArray3DToArray2D(double[][][] V)
V
- public static float[] convertDoubleToFloatArray(double[] ds)
public static double[] copy(double[] dst, double[] src)
dst
- src
- public static double[] crossProduct(double[] dst, double[] u, double[] v)
dst
- double[3]u
- double[3]v
- double[3]public static double determinant(double[] m)
m
- public static double determinant(double[][] m)
public static boolean equals(double[] u, double[] v)
"manhattan norm"
of
the difference of the two vectors does not exceed the default
tolerance
.u
- double[n]v
- double[n]equals()
public static boolean equals(double[] u, double[] v, double tol)
"manhattan norm"
of the
difference of the two vectors does not exceed the provided tol.u
- double[n]v
- double[n]tol
- public static double euclideanAngle(double[] u, double[] v)
u
- v
- public static double euclideanDistance(double[] u, double[] v)
u
- v
- public static double euclideanDistanceSquared(double[] u, double[] v)
u
- double[n]v
- double[n]public static double euclideanNorm(double[] vec)
vec
- double[n]innerProduct()
public static double euclideanNormSquared(double[] vec)
vec
- double[n]public static double[] extractSubmatrix(double[] subm, double[] src, int l, int r, int t, int b)
subm
- double[b-t][r-l]src
- double[n][n]l
- begin with the lth columnr
- and include the rth columnt
- begin with the t-th rowb
- and include the b-th rowpublic static double[] identityMatrix(int dim)
dim
- public static double[] diagonalMatrix(double[] dst, double[] entries)
public static double innerProduct(double[] u, double[] v)
u
- v
- public static double innerProduct(double[] u, double[] v, int n)
u
- v
- public static double[] inverse(double[] minvIn, double[] m)
minv
- double[n][n]m
- double[n][n]public static boolean isIdentityMatrix(double[] mat, double tol)
mat
- tol
- public static boolean isSpecialMatrix(double[] mat, double tol)
mat
- tol
- public static double[] linearCombination(double[] dst, double a, double[] aVec, double b, double[] bVec)
dst
- double[n]a
- aVec
- double[n]b
- bVec
- double[n]public static double manhattanNorm(double[] vec)
vec
- public static double manhattanNormDistance(double[] u, double[] v)
u
- v
- public static double[] matrixTimesVector(double[] dst, double[] m, double[] src)
dst
- double[n] (may be null)m
- double[n][n]src
- double[n]public static double[][] matrixTimesVector(double[][] dst, double[] m, double[][] src)
matrixTimesVector(double[], double[], double[])
.dst
- double[m][n] (may be null)m
- double[n][n]src
- double[m][n]public static String matrixToJavaString(double[] v)
matrixToString(double[])
, but formatted for direct
insertion as java source codev
- public static String matrixToString(double[] m)
String.format(java.lang.String, java.lang.Object[])
and return
the string.m
- public static String matrixToString(double[] m, String formatString)
m
- formatString
- String.format(java.lang.String, java.lang.Object[])
public static double[] max(double[] dst, double[] src1, double[] src2)
dst
- src1
- src2
- public static double maxNorm(double[] vec)
vec
- public static double maxNormDistance(double[] u, double[] v)
"max norm"
.u
- v
- public static double[] min(double[] dst, double[] src1, double[] src2)
dst
- src1
- src2
- public static double[] negate(double[] dst, double[] src)
dst
- src
- public static double[] normalize(double[] dst, double[] src)
euclidean norm
.dst
- src
- public static double[][] normalize(double[][] dst, double[][] src)
ds
- public static double[] planeParallelToPassingThrough(double[] plane, double[] ds, double[] ds2)
plane
- ds
- ds2
- public static double[] polarDecompose(double[] q, double[] s, double[] m)
q
- s
- m
- public static double[] projectOnto(double[] dst, double[] src, double[] fixed)
dst
- src
- fixed
- public static double[] projectOntoComplement(double[] dst, double[] src, double[] fixed)
dst
- src
- fixed
- public static double[] setDiagonalMatrix(double[] dst, double[] diag)
dst
- diag
- public static double[] setEuclideanNorm(double[] dst, double length, double[] src)
dst
- length
- src
- public static double[] setIdentityMatrix(double[] mat)
mat
- public static float[] setIdentityMatrix(float[] mat)
public static double[] setToValue(double[] dst, double val)
dst
- val
- public static double[] setToValue(double[] dst, double x, double y)
dst
- x
- y
- public static double[] setToValue(double[] dst, double x, double y, double z)
dst
- x
- y
- z
- public static double[] setToValue(double[] dst, double x, double y, double z, double w)
dst
- x
- y
- z
- w
- public static int mysqrt(int sq)
sq
parameter. Will return
very fast for square roots of 0 to 10.sq
- public static double[] submatrix(double[] subm, double[] m, int row, int column)
subm
- double[n-1 * n-1]m
- double[n*n]row
- column
- public static double[] subtract(double[] dst, double[] src1, double[] src2)
dst
- src1
- src2
- public static double[][] subtract(double[][] dst, double[][] src1, double[][] src2)
public static void swap(double[] u, double[] v)
u
- v
- public static double[] times(double[] dst, double factor, double[] src)
dst
- factor
- src
- public static double[] times(double[] dst, double[] src1, double[] src2)
dst
- double[n*n] may be nullsrc1
- double[n*n]src2
- double[n*n]public static double[][] times(double[][] dst, double factor, double[][] src)
times(double[], double, double[])
;dst
- factor
- src
- public static String toString(double[] v)
String.format(java.lang.String, java.lang.Object[])
and return
the string.v
- public static String toString(double[] v, String formatString)
v
- String.format(java.lang.String, java.lang.Object[])
public static String toString(double[][] v)
toString(double[])
.v
- public static String toString(double[][] v, int n)
public static String toString(double[][][] v)
public static String toString(float[] v)
public static double trace(double[] m)
m
- public static double[] transpose(double[] dst, double[] src)
dst
- src
- public static float[] transposeD2F(float[] dst, double[] src)
double
to float
. This is useful
for hooking up to GL libraries, for example.dst
- src
- public static double[] transposeF2D(double[] dst, float[] src)
float
to double
.dst
- src
- public static double[] bilinearInterpolation(double[] ds, double u, double v, double[] vb, double[] vt, double[] cb, double[] ct)
public static double[] bezierCombination(double[] dst, double t, double[] v0, double[] t0, double[] t1, double[] v1)
public static boolean isNan(double[] ds)
public static double[] setToLength(double[] p1, double[] p12, double rad)
public static double[][] completeBasis(double[][] dst, double[][] partial)
dst
- double[n][n]partial
- double[m][n]public static double[] permutationMatrix(double[] dst, int[] perm)
dst
- perm
- public static boolean isZero(double[] iline)
public static boolean isZero(double[] iline, double tol)
public static float[] times(float[] dst, float[] src1, float[] src2)