Logo

SQL Server VANDERMONDE Function

Updated 2024-03-07 15:44:17.283000

Description

Use the scalar function VANDERMONDE to return the Vandermonde matrix. The returned matrix will contain N + 1 columns with the columns representing the geometric progress of the input vector x.

\textbf{V}=\begin{bmatrix}1&x_0&x_0^2&x_0^3&\dots&x_0^n\\1&x_1&x_1^2&x_1^3&\dots&x_1^n\\1&x_2&x_2^2&x_2^3&\dots&x_2^n\\1&x_3&x_3^2&x_3^3&\dots&x_3^n\\\vdots&\vdots&\vdots&\vdots&\ddots&\vdots\\1&x_m&x_m^2&x_m^3&\dots&x_m^n\end{bmatrix}

Syntax

SELECT [westclintech].[wct].[VANDERMONDE] (
   <@X, nvarchar(max),>
  ,<@N, int,>)

Arguments

@X

The input vector.

@N

The upper bound of the Vandermonde matrix. @N is an expression of type int or of a type that can be implicitly converted to int.

Return Type

nvarchar(max)

Remarks

N must be greater than zero.

Examples

Example #1

VANDERMONDE takes the string representation of the X vector and returns a string value.

SELECT wct.VANDERMONDE('1;2;3;4;5', 3);

This produces the following result.

column 1
1,1,1,1;1,2,4,8;1,3,9,27;1,4,16,64;1,5,25,125

Example #2

In this example we use the MATRIX2STRING_q function to create the X vector, the TRANSPOSE function to put that vector into the proper format for the VANDERMONDE function, and the MATRIX function to return the string result as a table in 3rd normal form.

DECLARE @X as nvarchar(max) = wct.MATRIX2STRING_q('SELECT 1,1/2e+0,1/3e+0,1/4e+0,
          1/5e+0');
SELECT [0],
       [1],
       [2],
       [3],
       [4],
       [5]
FROM wct.MATRIX(wct.VANDERMONDE(wct.TRANSPOSE(@X), 5))
    PIVOT
    (
        MAX(ItemValue)
        FOR ColNum in ([0], [1], [2], [3], [4], [5])
    ) d
ORDER BY RowNum;

This produces the following result.

012345
111111
10.50.250.1250.06250.03125
10.3333333333333330.1111111111111110.03703703703703690.01234567901234560.00411522633744854
10.250.06250.0156250.003906250.0009765625
10.20.040.0080.00160.00032

See Also

EYE - Generate an m-by-n identity matrix.

ONES - create an m-by-n matrix of ones

ZERO - Generate an m-by-n matrix of zeroes.