Logo

SQL Server MSYMMEIG Function

Updated 2024-03-06 21:03:09.890000

Description

Use the table-valued function MSYMMEIG to return the D and V matrices representing the eigenvalues and eigenvectors of a real symmetric matrix. The input into the function is a string representation of a real symmetric matrix where the columns are separated by commas and the rows are separated by semicolons. MYSMMEIG returns a single row of two columns containing the string representations of the D and V matrices such that:

\textbf{A}=\textbf{VDV}^\text{T}

Syntax

SELECT * FROM [westclintech].[wct].[MSYMMEIG](
   <@A, nvarchar(max),>)

Arguments

@A

A string representation of the real symmetric matrix.

Return Type

table

colNamecolDatatypecolDesc
Dnvarchar(max)String representation of the D matrix
Vnvarchar(max)String representation of the V matrix

Remarks

The string representation of @A must only contain numbers, commas (to separate the columns), and semi-colons to separate the rows.

Consecutive commas will generate an error.

Consecutive semi-colons will generate an error.

Non-numeric data between commas will generate an error.

Non-numeric data between semi-colons will generate an error.

To convert non-normalized data to a string format, use the MATRIX2STRING or the MATRIX2STRING_q function.

To convert normalized data to a string format, us the NMATRIX2STRING or the NMATRIX2STRING_q function.

To convert a string result to a table, us the table-valued function MATRIX.

Examples

Example #1

In this example we supply @A as a real symmetric matrix and return the D and V matrices.

--Create a real, symmetric matrix
DECLARE @A as varchar(max) = '5,15,55,225;15,55,225,979;55,225,979,4425;225,979,4425,20515';
--Create variables to store the eigenvalues and the eignevectors
DECLARE @D as varchar(max), @V as varchar(max);
--Put the eigenvalues into the D matrix; the eigenvectors into the V matrix
SELECT @D = D, @V = V FROM wct.MSYMMEIG(@A);
--Return the eigenvalues
SELECT @D as D;
--Return the eigenvectors
SELECT @V as V;
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@D) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum';
--DECLARE @cols as nvarchar(max);
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@D) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''));
--SET @cols = REPLACE(@cols,'][','],[');
--DECLARE @D_pivot as varchar(max) = REPLACE(REPLACE(@M,'@cols',@cols),'@D','''' + @D + '''');
--EXECUTE(@D_pivot);
--DECLARE @V_pivot as varchar(max) = REPLACE(REPLACE(@M,'@cols',@cols),'@D','''' + @V + '''');
--EXECUTE(@V_pivot);

This produces the following result.

D
21520.0548916024,0,0,0;0,32.9311831308751,0,0;0,0,0.999697351070802,0;0,0,0,0.014227915654616
V
0.0107819695760205,0.23936078568919,0.779061889169004,0.579355448969575;0.0467476325138823,0.48628786622379,0.419079336335536,-0.765317763987238;0.210865645105216,0.815711666652096,-0.460882264857169,0.278814803305254;0.976337076885217,-0.202101465421276,0.0708704497030608,-0.0299714753171836

Here are the results formatted as a matrix.

0123
21520.0548916024000
032.931183130875100
000.9996973510708020
0000.014227915654616
0123
0.01078196957602050.239360785689190.7790618891690040.579355448969575
0.04674763251388230.486287866223790.419079336335536-0.765317763987238
0.2108656451052160.815711666652096-0.4608822648571690.278814803305254
0.976337076885217-0.2021014654212760.0708704497030608-0.0299714753171836

Example #2

The cross product of any real matrix is a real symmetric matrix. In this example we generate an m-by-n random matrix, calculate the cross product and return the eigenvalues and eigenvectors.

--Create variables to store the eigenvalues and the eignevectors
DECLARE @D as varchar(max), @V as varchar(max);
--Put the eigenvalues into the D matrix; the eigenvectors into the V matrix
SELECT @D = D, @V = V FROM wct.MSYMMEIG(wct.CROSSPROD(wct.MRAND(25,10),NULL));
--Return the eigenvalues
SELECT @D as D;
--Return the eigenvectors
SELECT @V as V;
--Only run this SQL to automatically PIVOT the results into the
--traditional row/column matrix presentation
--DECLARE @M as nvarchar(max) = N'SELECT @cols FROM wct.Matrix(@D) d PIVOT(MAX(ItemValue) FOR ColNum in (@cols))p ORDER BY RowNum';
--DECLARE @cols as nvarchar(max);
 
--SET @cols = (SELECT '[' + cast(ColNum as varchar(max)) + ']' FROM wct.MATRIX(@D) WHERE RowNum = 0 ORDER BY colnum FOR XML PATH(''));
--SET @cols = REPLACE(@cols,'][','],[');
--DECLARE @D_pivot as varchar(max) = REPLACE(REPLACE(@M,'@cols',@cols),'@D','''' + @D + '''');
--EXECUTE(@D_pivot);
--DECLARE @V_pivot as varchar(max) = REPLACE(REPLACE(@M,'@cols',@cols),'@D','''' + @V + '''');
--EXECUTE(@V_pivot);

This produces the following result. Your results will be different.

D
68.8840809005962,0,0,0,0,0,0,0,0,0;0,4.70343898711347,0,0,0,0,0,0,0,0;0,0,3.05493270970174,0,0,0,0,0,0,0;0,0,0,2.25596564791433,0,0,0,0,0,0;0,0,0,0,2.05832721909267,0,0,0,0,0;0,0,0,0,0,1.8746980286505,0,0,0,0;0,0,0,0,0,0,1.50962263584078,0,0,0;0,0,0,0,0,0,0,1.2473893767411,0,0;0,0,0,0,0,0,0,0,0.9090363473113,0;0,0,0,0,0,0,0,0,0,0.577861097571784
V
0.33949667496189,-0.22115504023097,-0.132959451819543,-0.127023275776141,0.367057086381017,0.546915128270288,-0.480148578463973,-0.209047040243989,-0.131770167563048,-0.276704835094071;0.328094277582681,-0.0412291234505285,0.454639829401461,0.184168420281483,0.0710987793662083,-0.591224262198384,-0.465068052013647,0.142876832381989,-0.187697466917105,-0.153314503319383;0.305442082542215,-0.0851355492402865,0.385113872246908,-0.463739279780351,-0.549191466388174,0.197515562528606,0.0621852885032319,-0.151481178704896,-0.299056179286227,0.28145770212895;0.354748485582571,-0.0312049251394,-0.290533981247046,0.204180261846687,0.329668900793036,-0.227413242844925,0.299583944778863,-0.360106808914383,-0.413509810301765,0.44301693743465;0.336255988362768,0.235012453656885,0.366217362569137,0.2562887317693,0.0349492364657946,0.14745608410612,0.515581363489793,-0.263384046200865,0.153918060008263,-0.500051170381636;0.267320184478105,0.470719968266905,-0.318378536266351,0.336015778192934,-0.464952884316525,0.0465580314

Here are the results formatted as a matrix.

0123456789
68.8840809005962000000000
04.7034389871134700000000
003.054932709701740000000
0002.25596564791433000000
00002.0583272190926700000
000001.87469802865050000
0000001.50962263584078000
00000001.247389376741100
000000000.90903634731130
0000000000.577861097571784
0123456789
0.33949667496189-0.22115504023097-0.132959451819543-0.1270232757761410.3670570863810170.546915128270288-0.480148578463973-0.209047040243989-0.131770167563048-0.276704835094071
0.328094277582681-0.04122912345052850.4546398294014610.1841684202814830.0710987793662083-0.591224262198384-0.4650680520136470.142876832381989-0.187697466917105-0.153314503319383
0.305442082542215-0.08513554924028650.385113872246908-0.463739279780351-0.5491914663881740.1975155625286060.0621852885032319-0.151481178704896-0.2990561792862270.28145770212895
0.354748485582571-0.0312049251394-0.2905339812470460.2041802618466870.329668900793036-0.2274132428449250.299583944778863-0.360106808914383-0.4135098103017650.44301693743465
0.3362559883627680.2350124536568850.3662173625691370.25628873176930.03494923646579460.147456084106120.515581363489793-0.2633840462008650.153918060008263-0.500051170381636
0.2673201844781050.470719968266905-0.3183785362663510.336015778192934-0.4649528843165250.0465580314298593-0.376476788002286-0.2275294974091040.2525156842003750.130666312061937
0.259813694941897-0.4319667624934510.04006163237712550.535703082260915-0.1493058270781750.3182845837419640.1110282557908810.5207634254668680.05688732969755720.216710728561454
0.3012856167871040.5817493971627360.0779033622879969-0.2804813237579940.3631139975420970.1390346515448630.03855879301012110.5086691739962380.09411530802220390.256485280956755
0.353975677682947-0.096398360108633-0.547384367178401-0.280763193416735-0.254589874431408-0.2540830778359260.1932039115734620.317106831067601-0.126138487663318-0.451421458435258
0.299752679394178-0.3606111031757060.0108700579299369-0.2648345296938760.116441250719551-0.2269441908129090.0291681279413251-0.1778359925135360.755990479272140.201970844185444

See Also

MUPDATE - perform elementwise operations on a matrix