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.
| 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 | 1 |
| 1 | 0.5 | 0.25 | 0.125 | 0.0625 | 0.03125 |
| 1 | 0.333333333333333 | 0.111111111111111 | 0.0370370370370369 | 0.0123456790123456 | 0.00411522633744854 |
| 1 | 0.25 | 0.0625 | 0.015625 | 0.00390625 | 0.0009765625 |
| 1 | 0.2 | 0.04 | 0.008 | 0.0016 | 0.00032 |
See Also
EYE - Generate an m-by-n identity matrix.