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
| colName | colDatatype | colDesc |
|---|---|---|
| D | nvarchar(max) | String representation of the D matrix |
| V | nvarchar(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.
| 0 | 1 | 2 | 3 |
|---|---|---|---|
| 21520.0548916024 | 0 | 0 | 0 |
| 0 | 32.9311831308751 | 0 | 0 |
| 0 | 0 | 0.999697351070802 | 0 |
| 0 | 0 | 0 | 0.014227915654616 |
| 0 | 1 | 2 | 3 |
|---|---|---|---|
| 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 |
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.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| 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 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| 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.0465580314298593 | -0.376476788002286 | -0.227529497409104 | 0.252515684200375 | 0.130666312061937 |
| 0.259813694941897 | -0.431966762493451 | 0.0400616323771255 | 0.535703082260915 | -0.149305827078175 | 0.318284583741964 | 0.111028255790881 | 0.520763425466868 | 0.0568873296975572 | 0.216710728561454 |
| 0.301285616787104 | 0.581749397162736 | 0.0779033622879969 | -0.280481323757994 | 0.363113997542097 | 0.139034651544863 | 0.0385587930101211 | 0.508669173996238 | 0.0941153080222039 | 0.256485280956755 |
| 0.353975677682947 | -0.096398360108633 | -0.547384367178401 | -0.280763193416735 | -0.254589874431408 | -0.254083077835926 | 0.193203911573462 | 0.317106831067601 | -0.126138487663318 | -0.451421458435258 |
| 0.299752679394178 | -0.360611103175706 | 0.0108700579299369 | -0.264834529693876 | 0.116441250719551 | -0.226944190812909 | 0.0291681279413251 | -0.177835992513536 | 0.75599047927214 | 0.201970844185444 |