Logo

MSYMMEIG

Updated 2024-03-06 21:03:09.890000

Syntax

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

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}

Arguments

@A

A string representation of the real symmetric matrix.

Return Type

table

{"columns": [{"field": "colName", "headerName": "Name", "header": "name"}, {"field": "colDatatype", "headerName": "Type", "header": "type"}, {"field": "colDesc", "headerName": "Description", "header": "description", "minWidth": 1000}], "rows": [{"id": "9692226c-d595-4d56-9acd-431d9ca47784", "colName": "D", "colDatatype": "nvarchar(max)", "colDesc": "String representation of the D matrix"}, {"id": "d31de722-6b7a-47fb-946c-75ed474da36a", "colName": "V", "colDatatype": "nvarchar(max)", "colDesc": "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.

{"columns":[{"field":"D"}],"rows":[{"D":"21520.0548916024,0,0,0;0,32.9311831308751,0,0;0,0,0.999697351070802,0;0,0,0,0.014227915654616"}]}
{"columns":[{"field":"V"}],"rows":[{"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.

{"columns":[{"field":"0","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"2","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"3","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"0":"21520.0548916024","1":"0","2":"0","3":"0"},{"0":"0","1":"32.9311831308751","2":"0","3":"0"},{"0":"0","1":"0","2":"0.999697351070802","3":"0"},{"0":"0","1":"0","2":"0","3":"0.014227915654616"}]}
{"columns":[{"field":"0","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"2","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"3","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"0":"0.0107819695760205","1":"0.23936078568919","2":"0.779061889169004","3":"0.579355448969575"},{"0":"0.0467476325138823","1":"0.48628786622379","2":"0.419079336335536","3":"-0.765317763987238"},{"0":"0.210865645105216","1":"0.815711666652096","2":"-0.460882264857169","3":"0.278814803305254"},{"0":"0.976337076885217","1":"-0.202101465421276","2":"0.0708704497030608","3":"-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.

{"columns":[{"field":"D"}],"rows":[{"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"}]}
{"columns":[{"field":"V"}],"rows":[{"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.

{"columns":[{"field":"0","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"2","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"3","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"4","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"5","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"6","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"7","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"8","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"9","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"0":"68.8840809005962","1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0"},{"0":"0","1":"4.70343898711347","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0"},{"0":"0","1":"0","2":"3.05493270970174","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0"},{"0":"0","1":"0","2":"0","3":"2.25596564791433","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0"},{"0":"0","1":"0","2":"0","3":"0","4":"2.05832721909267","5":"0","6":"0","7":"0","8":"0","9":"0"},{"0":"0","1":"0","2":"0","3":"0","4":"0","5":"1.8746980286505","6":"0","7":"0","8":"0","9":"0"},{"0":"0","1":"0","2":"0","3":"0","4":"0","5":"0","6":"1.50962263584078","7":"0","8":"0","9":"0"},{"0":"0","1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"1.2473893767411","8":"0","9":"0"},{"0":"0","1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0.9090363473113","9":"0"},{"0":"0","1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0.577861097571784"}]}
{"columns":[{"field":"0","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"2","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"3","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"4","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"5","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"6","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"7","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"8","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"9","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"0":"0.33949667496189","1":"-0.22115504023097","2":"-0.132959451819543","3":"-0.127023275776141","4":"0.367057086381017","5":"0.546915128270288","6":"-0.480148578463973","7":"-0.209047040243989","8":"-0.131770167563048","9":"-0.276704835094071"},{"0":"0.328094277582681","1":"-0.0412291234505285","2":"0.454639829401461","3":"0.184168420281483","4":"0.0710987793662083","5":"-0.591224262198384","6":"-0.465068052013647","7":"0.142876832381989","8":"-0.187697466917105","9":"-0.153314503319383"},{"0":"0.305442082542215","1":"-0.0851355492402865","2":"0.385113872246908","3":"-0.463739279780351","4":"-0.549191466388174","5":"0.197515562528606","6":"0.0621852885032319","7":"-0.151481178704896","8":"-0.299056179286227","9":"0.28145770212895"},{"0":"0.354748485582571","1":"-0.0312049251394","2":"-0.290533981247046","3":"0.204180261846687","4":"0.329668900793036","5":"-0.227413242844925","6":"0.299583944778863","7":"-0.360106808914383","8":"-0.413509810301765","9":"0.44301693743465"},{"0":"0.336255988362768","1":"0.235012453656885","2":"0.366217362569137","3":"0.2562887317693","4":"0.0349492364657946","5":"0.14745608410612","6":"0.515581363489793","7":"-0.263384046200865","8":"0.153918060008263","9":"-0.500051170381636"},{"0":"0.267320184478105","1":"0.470719968266905","2":"-0.318378536266351","3":"0.336015778192934","4":"-0.464952884316525","5":"0.0465580314298593","6":"-0.376476788002286","7":"-0.227529497409104","8":"0.252515684200375","9":"0.130666312061937"},{"0":"0.259813694941897","1":"-0.431966762493451","2":"0.0400616323771255","3":"0.535703082260915","4":"-0.149305827078175","5":"0.318284583741964","6":"0.111028255790881","7":"0.520763425466868","8":"0.0568873296975572","9":"0.216710728561454"},{"0":"0.301285616787104","1":"0.581749397162736","2":"0.0779033622879969","3":"-0.280481323757994","4":"0.363113997542097","5":"0.139034651544863","6":"0.0385587930101211","7":"0.508669173996238","8":"0.0941153080222039","9":"0.256485280956755"},{"0":"0.353975677682947","1":"-0.096398360108633","2":"-0.547384367178401","3":"-0.280763193416735","4":"-0.254589874431408","5":"-0.254083077835926","6":"0.193203911573462","7":"0.317106831067601","8":"-0.126138487663318","9":"-0.451421458435258"},{"0":"0.299752679394178","1":"-0.360611103175706","2":"0.0108700579299369","3":"-0.264834529693876","4":"0.116441250719551","5":"-0.226944190812909","6":"0.0291681279413251","7":"-0.177835992513536","8":"0.75599047927214","9":"0.201970844185444"}]}

See Also

MUPDATE - perform elementwise operations on a matrix