# Math

## Introduction

vtkMath provides methods to perform common math operations. These include
providing constants such as Pi; conversion from degrees to radians; vector
operations such as dot and cross products and vector norm; matrix determinant
for 2x2 and 3x3 matrices; univariate polynomial solvers; and for random
number generation (for backward compatibility only).

## Methods

### LUFactor3x3

LU Factorization of a 3x3 matrix.

Argument Type Required Description
`mat_3x3` Matrix3x3 Yes
`index_3` Vector3 Yes

### LUSolve3x3

LU back substitution for a 3x3 matrix.

Argument Type Required Description
`mat_3x3` Matrix3x3 Yes
`index_3` Vector3 Yes
`x_3` Vector3 Yes

### Pi

Get the number π.

Argument Type Required Description
`a` Vector3 Yes The first 3D vector.
`b` Vector3 Yes The second 3D vector.
`out` Vector3 Yes The output 3D vector.

### angleBetweenVectors

Angle between 3D vectors.

Argument Type Required Description
`v1` Vector3 Yes The first 3D vector.
`v2` Vector3 Yes The second 3D vector.

### areBoundsInitialized

Argument Type Required Description
`bounds` Bounds Yes The bounds to check.

### areEquals

Returns true if elements of both arrays are equals.

Argument Type Required Description
`a` Array. Yes An array of numbers (vector, point, matrix…)
`b` Array. Yes An array of numbers (vector, point, matrix…)
`eps` Number No The tolerance value.

### arrayMax

Get the maximum of the array.

Argument Type Required Description
`arr` Array. Yes The array.
`offset` Number Yes The offset.
`stride` Number Yes The stride.

### arrayMin

Get the minimum of the array.

Argument Type Required Description
`arr` Array. Yes The array.
`offset` Number Yes The offset.
`stride` Number Yes The stride.

### arrayRange

Argument Type Required Description
`arr` Array. Yes The array.
`offset` Number Yes The offset.
`stride` Number Yes The stride.

### beginCombination

Start iterating over “m choose n” objects.

Argument Type Required Description
`m` Number No The first numeric expression.
`n` Number No The second numeric expression.

### binomial

The number of combinations of n objects from a pool of m objects (m>n).

Argument Type Required Description
`m` Number Yes The first numeric expression.
`n` Number Yes The second numeric expression.

### boundsIsWithinOtherBounds

Check if first 3D bounds is within the second 3D bounds.

Argument Type Required Description
`bounds1_6` Bounds Yes The first bounds.
`bounds2_6` Bounds Yes The second bounds.
`delta_3` Vector3 Yes The error margin along each axis.

### ceil

Same as Math.ceil().

Argument Type Required Description
`param1` Number Yes A numeric expression.

### ceilLog2

Gives the exponent of the lowest power of two not less than x.

### clampAndNormalizeValue

Argument Type Required Description
`value` Number Yes
`range` Range Yes

### clampValue

Clamp some value against a range.

Argument Type Required Description
`value` Number Yes The value to clamp.
`minValue` Number Yes The minimum value.
`maxValue` Number Yes The maximum value.

### clampVector

Clamp some vector against a range.

Argument Type Required Description
`vector` Vector3 Yes The vector to clamp.
`minVector` Vector3 Yes The minimum vector.
`maxVector` Vector3 Yes The maximum vector.
`out` Vector3 Yes The output vector.

### columnsToMat3

Fill a 3x3 matrix with the given column vectors

Argument Type Required Description
`column0` Vector3 Yes
`column1` Vector3 Yes
`column2` Vector3 Yes
`mat` Matrix Yes

### columnsToMat4

Fill a 4x4 matrix with the given column vectors

Argument Type Required Description
`column0` Vector4 Yes
`column1` Vector4 Yes
`column2` Vector4 Yes
`column3` Vector4 Yes
`mat` Matrix Yes

### createArray

Argument Type Required Description
`size` Number Yes The size of the array.

### cross

Computes cross product of 3D vectors x and y.

Argument Type Required Description
`x` Vector3 Yes The first 3D vector.
`y` Vector3 Yes The second 3D vector.
`out` Vector3 Yes The output 3D vector.

Argument Type Required Description
`rad` Number Yes The value in radians.

### determinant2x2

Argument Type Required Description
`args` Array. Yes

### determinant3x3

Calculate the determinant of a 3x3 matrix.

Argument Type Required Description
`mat_3x3` Matrix3x3 Yes The input 3x3 matrix.

### diagonalize3x3

Argument Type Required Description
`a_3x3` Matrix3x3 Yes
`w_3` Vector3 Yes
`v_3x3` Matrix3x3 Yes

### distance2BetweenPoints

Compute distance squared between two points p1 and p2.

Argument Type Required Description
`x` Vector3 Yes The first 3D vector.
`y` Vector3 Yes The second 3D vector.

### dot

Argument Type Required Description
`x` Vector3 Yes
`y` Vector3 Yes

### dot2D

Argument Type Required Description
`x` Vector2 Yes
`y` Vector2 Yes

### estimateMatrixCondition

Argument Type Required Description
`A` Matrix Yes
`size` Number Yes

### extentIsWithinOtherExtent

Check if first 3D extent is within second 3D extent.

Argument Type Required Description
`extent1` Extent Yes The first extent.
`extent2` Extent Yes The second extent.

### factorial

Compute N factorial, N! = N*(N-1) * (N-2)…32*1.

### float2CssRGBA

Convert RGB or RGBA color array to CSS representation

Argument Type Required Description
`rgbArray` RGBColor or RGBAColor Yes The color array.

### floatRGB2HexCode

Argument Type Required Description
`rgbArray` RGBColor Yes
`prefix` string No

### floatToHex2

Argument Type Required Description
`value` Number Yes The value to convert.

### floor

Same as Math.floor().

Argument Type Required Description
`param1` Number Yes A numeric expression.

### gaussian

Generate pseudo-random numbers distributed according to the standard normal
distribution.

### gaussianAmplitude

Compute the amplitude of a Gaussian function with mean=0 and specified variance.

Argument Type Required Description
`mean` Number Yes The mean value.
`variance` Number Yes The variance value.
`position` Number Yes The position value.

### gaussianWeight

Compute the amplitude of an unnormalized Gaussian function with mean=0 and
specified variance.

Argument Type Required Description
`mean` Number Yes The mean value.
`variance` Number Yes The variance value.
`position` Number Yes The position value.

### getMajorAxisIndex

Argument Type Required Description
`vector` Array. Yes

### getScalarTypeFittingRange

Get the scalar type that is most likely to have enough precision to store a
given range of data once it has been scaled and shifted

### getSeed

Return the current seed used by the random number generator.

### getSparseOrthogonalMatrix

Return the closest orthogonal matrix of 1, -1 and 0
It works for both column major and row major matrices
This function iteratively associate a column with a row by choosing
the greatest absolute value from the remaining row and columns
For each association, a -1 or a 1 is set in the output, depending on
the sign of the value in the original matrix

Argument Type Required Description
`matrix` Array. Yes The matrix of size nxn
`n` Array. Yes The size of the square matrix, defaults to 3

### hex2float

Argument Type Required Description
`hexStr` String Yes
`outFloatArray` Array. No

### hsv2rgb

Argument Type Required Description
`hsv` HSVColor Yes An Array of the HSV color.
`rgb` RGBColor Yes An Array of the RGB color.

### identity

Set mat to the identity matrix.

Argument Type Required Description
`n` Number Yes The size of the matrix.
`mat` Array. Yes The output matrix.

### identity3x3

Set mat_3x3 to the identity matrix.

Argument Type Required Description
`mat_3x3` Matrix3x3 Yes The input 3x3 matrix.

### invert3x3

Invert a 3x3 matrix.

Argument Type Required Description
`in_3x3` Matrix3x3 Yes The input 3x3 matrix.
`outI_3x3` Matrix3x3 Yes The output 3x3 matrix.

### invertMatrix

Argument Type Required Description
`A` Matrix Yes The input matrix. It is modified during the inversion.
`AI` Matrix Yes The output inverse matrix. Can be the same as input matrix.
`size` Number No The square size of the matrix to invert : 4 for a 4x4
`index` Array. No
`column` Array. No

### isFinite

Determines whether the passed value is a finite number.

Argument Type Required Description
`value` Yes The value to check.

### isIdentity

Returns true if provided matrix is the identity matrix.

Argument Type Required Description
`mat` Array. Yes The 3x3 matrix to check
`eps` Number No The tolerance value.

### isIdentity3x3

Returns true if provided 3x3 matrix is the identity matrix.

Argument Type Required Description
`mat` Matrix3x3 Yes The 3x3 matrix to check
`eps` Number No The tolerance value.

### isInf

Determines whether the passed value is a infinite number.

Argument Type Required Description
`value` Number Yes The value to check.

### isNaN

Determines whether the passed value is a NaN.

Argument Type Required Description
`value` Number Yes The value to check.

### isNan

Determines whether the passed value is a NaN.

Argument Type Required Description
`value` Number Yes The value to check.

### isPowerOfTwo

Returns true if integer is a power of two.

Argument Type Required Description
`x` Number Yes A numeric expression.

### jacobi

Argument Type Required Description
`a_3x3` Matrix3x3 Yes
`w` Array. Yes
`v` Array. Yes

### jacobiN

Jacobi iteration for the solution of eigenvectors/eigenvalues. Input matrix a is modified (the upper triangle is filled with zeros)

Argument Type Required Description
`a` Matrix Yes real symetric nxn matrix
`n` Number Yes matrix size
`w` Array. Yes vector of size n to store eigenvalues (stored in decreasing order)
`v` Array. Yes matrix of size nxn to store eigenvectors (stored in decreasing order, normalized)

### lab2rgb

Argument Type Required Description
`lab` Vector3 Yes
`rgb` RGBColor Yes An Array of the RGB color.

### lab2xyz

Argument Type Required Description
`lab` Vector3 Yes
`xyz` Vector3 Yes

### linearSolve3x3

Solve mat_3x3y_3 = x_3 for y and place the result in y.

Argument Type Required Description
`mat_3x3` Matrix3x3 Yes
`x_3` Vector3 Yes
`y_3` Vector3 Yes

### luFactorLinearSystem

Argument Type Required Description
`A` Matrix Yes
`index` Array. Yes
`size` Number Yes

### luSolveLinearSystem

Argument Type Required Description
`A` Matrix Yes
`index` Array. Yes
`x` Array. Yes
`size` Number Yes

### matrix3x3ToQuaternion

Argument Type Required Description
`mat_3x3` Matrix3x3 Yes
`quat_4` Vector4 Yes

### max

Get the maximum of the two arguments provided. If either argument is NaN,
the first argument will always be returned.

Argument Type Required Description
`param1` Number Yes The first numeric expression.
`param2` Number Yes The second numeric expression.

### min

Get the minimum of the two arguments provided. If either argument is NaN,
the first argument will always be returned.

Argument Type Required Description
`param1` Number Yes The first numeric expression.
`param2` Number Yes The second numeric expression.

### multiply3x3_mat3

Argument Type Required Description
`a_3x3` Matrix3x3 Yes
`b_3x3` Matrix3x3 Yes
`out_3x3` Matrix3x3 Yes

### multiply3x3_vect3

Argument Type Required Description
`mat_3x3` Matrix3x3 Yes
`in_3` Vector3 Yes
`out_3` Vector3 Yes

### multiplyAccumulate

Argument Type Required Description
`a` Vector3 Yes
`b` Vector3 Yes
`scalar` Number Yes
`out` Vector3 Yes

### multiplyAccumulate2D

Argument Type Required Description
`a` Vector2 Yes
`b` Vector2 Yes
`scalar` Number Yes
`out` Vector2 Yes

### multiplyMatrix

Multiply two matrices.

Argument Type Required Description
`a` Matrix Yes
`b` Matrix Yes
`rowA` Number Yes
`colA` Number Yes
`rowB` Number Yes
`colB` Number Yes
`outRowAColB` Matrix Yes

### multiplyQuaternion

Argument Type Required Description
`quat_1` Vector4 Yes
`quat_2` Vector4 Yes
`quat_out` Vector4 Yes

### multiplyScalar

Argument Type Required Description
`vec` Vector3 Yes
`scalar` Number Yes

### multiplyScalar2D

Argument Type Required Description
`vec` Vector2 Yes
`scalar` Number Yes

### nearestPowerOfTwo

Compute the nearest power of two that is not less than x.

Argument Type Required Description
`xi` Number Yes A numeric expression.

### nextCombination

Given m, n, and a valid combination of n integers in the range [0,m[, this
function alters the integers into the next combination in a sequence of all
combinations of n items from a pool of m.

Argument Type Required Description
`m` Number Yes The first numeric expression.
`n` Number Yes The second numeric expression.
`r` Array. Yes

### norm

Argument Type Required Description
`x` Array. Yes
`n` Number Yes

### norm2D

Compute the norm of a 2-vector.

Argument Type Required Description
`x2D` Vector2 Yes x The 2D vector.

### normalize

Normalize in place. Returns norm.

Argument Type Required Description
`x` Vector3 Yes The vector to normlize.

### normalize2D

Normalize (in place) a 2-vector.

Argument Type Required Description
`x` Vector2 Yes The 2D vector.

### orthogonalize3x3

Argument Type Required Description
`a_3x3` Matrix3x3 Yes
`out_3x3` Matrix3x3 Yes

### outer

Outer product of two 3-vectors.

Argument Type Required Description
`x` Vector3 Yes The first 3D vector.
`y` Vector3 Yes The second 3D vector.
`out_3x3` Matrix3x3 Yes The output 3x3 matrix.

### outer2D

Outer product of two 2-vectors.

Argument Type Required Description
`x` Vector2 Yes The first 2D vector.
`y` Vector2 Yes The second 2D vector.
`out_2x2` Matrix Yes The output 2x2 matrix.

### perpendiculars

Given a unit vector v1, find two unit vectors v2 and v3 such that v1 cross v2 = v3

Argument Type Required Description
`x` Vector3 Yes The first vector.
`y` Vector3 Yes The second vector.
`z` Vector3 Yes The third vector.
`theta` Number Yes

### pointIsWithinBounds

Check if point is within the given 3D bounds.

Argument Type Required Description
`point_3` Vector3 Yes The coordinate of the point.
`bounds_6` Bounds Yes The bounds.
`delta_3` Vector3 Yes The error margin along each axis.

### projectVector

Argument Type Required Description
`a` Vector3 Yes
`b` Vector3 Yes
`projection` Vector3 Yes

### projectVector2D

Compute the projection of 2D vector `a` on 2D vector `b` and returns the
result in projection vector.

Argument Type Required Description
`a` Vector2 Yes The first 2D vector.
`b` Vector2 Yes The second 2D vector.
`projection` Vector2 Yes The projection 2D vector.

### quaternionToMatrix3x3

Argument Type Required Description
`quat_4` Vector4 Yes
`mat_3x3` Matrix3x3 Yes

Argument Type Required Description
`deg` Number Yes The value in degrees.

### random

Generate pseudo-random numbers distributed according to the uniform
distribution between min and max.

Argument Type Required Description
`minValue` Number Yes
`maxValue` Number Yes

### randomSeed

Initialize seed value.

Argument Type Required Description
`seed` Number Yes

### rgb2hsv

Argument Type Required Description
`rgb` RGBColor Yes An Array of the RGB color.
`hsv` HSVColor Yes An Array of the HSV color.

### rgb2lab

Argument Type Required Description
`rgb` RGBColor Yes
`lab` Vector3 Yes

### rgb2xyz

Argument Type Required Description
`rgb` RGBColor Yes An Array of the RGB color.
`xyz` Vector3 Yes

### round

Same as Math.round().

Argument Type Required Description
`param1` Number Yes The value to be rounded to the nearest integer.

### roundNumber

Argument Type Required Description
`num` Number Yes
`digits` Number No

### roundVector

Argument Type Required Description
`vector` Vector3 Yes
`out` Vector3 Yes

### roundVector

Argument Type Required Description
`vector` Vector3 Yes
`out` Vector3 No
`digits` Number No

### rowsToMat3

Fill a 3x3 matrix with the given row vectors

Argument Type Required Description
`row0` Vector3 Yes
`row1` Vector3 Yes
`row2` Vector3 Yes
`mat` Matrix Yes

### rowsToMat4

Fill a 4x4 matrix with the given row vectors

Argument Type Required Description
`row0` Vector4 Yes
`row1` Vector4 Yes
`row2` Vector4 Yes
`row3` Vector4 Yes
`mat` Matrix Yes

### signedAngleBetweenVectors

Signed angle between v1 and v2 with regards to plane defined by normal vN.
angle between v1 and v2 with regards to plane defined by normal vN.Signed
angle between v1 and v2 with regards to plane defined by normal
vN.t3(mat_3x3, in_3, out_3)

Argument Type Required Description
`v1` Vector3 Yes The first 3D vector.
`v2` Vector3 Yes The second 3D vector.
`vN` Vector3 Yes

### singularValueDecomposition3x3

Argument Type Required Description
`a_3x3` Matrix3x3 Yes
`u_3x3` Matrix3x3 Yes
`w_3` Vector3 Yes
`vT_3x3` Matrix3x3 Yes

### solve3PointCircle

In Euclidean space, there is a unique circle passing through any given three
non-collinear points P1, P2, and P3.

Using Cartesian coordinates to represent these points as spatial vectors, it
is possible to use the dot product and cross product to calculate the radius
and center of the circle. See:
http://en.wikipedia.org/wiki/Circumscribed_circle and more specifically the
section Barycentric coordinates from cross- and dot-products

Argument Type Required Description
`p1` Vector3 Yes The coordinate of the first point.
`p2` Vector3 Yes The coordinate of the second point.
`p3` Vector3 Yes The coordinate of the third point.
`center` Vector3 Yes The coordinate of the center point.

### solveHomogeneousLeastSquares

Solves for the least squares best fit matrix for the homogeneous equation
X’M’ = 0’. Uses the method described on pages 40-41 of Computer Vision by
Forsyth and Ponce, which is that the solution is the eigenvector associated
with the minimum eigenvalue of T(X)X, where T(X) is the transpose of X. The
inputs and output are transposed matrices. Dimensions: X’ is numberOfSamples
by xOrder, M’ dimension is xOrder by yOrder. M’ should be pre-allocated. All
matrices are row major. The resultant matrix M’ should be pre-multiplied to
X’ to get 0’, or transposed and then post multiplied to X to get 0

Argument Type Required Description
`numberOfSamples` Number Yes
`xt` Matrix Yes
`xOrder` Number Yes
`mt` Matrix Yes

### solveLeastSquares

Solves for the least squares best fit matrix for the equation X’M’ = Y’. Uses
pseudoinverse to get the ordinary least squares. The inputs and output are
transposed matrices. Dimensions: X’ is numberOfSamples by xOrder, Y’ is
numberOfSamples by yOrder, M’ dimension is xOrder by yOrder. M’ should be
pre-allocated. All matrices are row major. The resultant matrix M’ should be
pre-multiplied to X’ to get Y’, or transposed and then post multiplied to X
to get Y By default, this method checks for the homogeneous condition where
Y==0, and if so, invokes SolveHomogeneousLeastSquares. For better performance
when the system is known not to be homogeneous, invoke with
checkHomogeneous=0.

Argument Type Required Description
`numberOfSamples` Number Yes
`xt` Matrix Yes
`xOrder` Number Yes
`yt` Matrix Yes
`yOrder` Number Yes
`mt` Matrix Yes
`checkHomogeneous` Boolean No

### solveLinearSystem

Argument Type Required Description
`A` Matrix Yes
`x` Array. Yes
`size` Number Yes

### subtract

Subtraction of two 3-vectors.

Argument Type Required Description
`a` Vector3 Yes The first 3D vector.
`b` Vector3 Yes The second 3D vector.
`out` Vector3 Yes The output 3D vector.

### swapColumnsMatrix_nxn

Given two columns indices, swap the two columns of a nxn matrix

Argument Type Required Description
`matrix` Array. Yes The n by n matrix in wich we want to swap the vectors.
`n` Number Yes size of the matrix.
`column1` Number Yes index of first col to swap with the other.
`column2` Number Yes index of second col to swap with the other.

### swapRowsMatrix_nxn

Given two rows indices, swap the two rows of a nxn matrix

Argument Type Required Description
`matrix` Array. Yes The n by n matrix in wich we want to swap the vectors.
`n` Number Yes size of the matrix.
`row1` Number Yes index of first row to swap with the other.
`row2` Number Yes index of second row to swap with the other.

### transpose3x3

Transpose a 3x3 matrix.

Argument Type Required Description
`in_3x3` Matrix3x3 Yes The input 3x3 matrix.
`outT_3x3` Matrix3x3 Yes The output 3x3 matrix.

### uninitializeBounds

Returns bounds.

Argument Type Required Description
`bounds` Bounds Yes Output array that hold bounds, optionally empty.

### xyz2lab

Argument Type Required Description
`xyz` Vector3 Yes
`lab` Vector3 Yes

### xyz2rgb

Argument Type Required Description
`xyz` Vector3 Yes
`rgb` RGBColor Yes An Array of the RGB color.