MTRACE_q
Updated: 30 September 2013
Use MTRACE_q to calculate the trace of a de-normalized matrix. The trace of a matrix (A) is the sum of the diagonal elements. For example:
Syntax
SELECT [wctMath].[wct].[MTRACE_q](
<@Matrix_RangeQuery, nvarchar(max),>)
Arguments
@Matrix_RangeQuery
the SELECT statement, as text, used to determine the matrix to be used in this function. The SELECT statement specifies the column names from the table or view or can be used to enter the matrix values directly. Data returned from the @Matrix_RangeQuery select must be of the type float or of a type that implicitly converts to float.
Return Types
float
Remarks
· Use the MTRACE function for simpler queries.
· Use MTRACEN_q for a table in 3rd normal form.
· If the matrix is not square then an error will be returned.
· If the matrix diagonal contains NULL, then NULL will be returned.
· The function returns an error if the matrix diagonal contains a non-numeric value.
Examples
In this example, we calculate trace from a temporary table containing one matrix.
SET NOCOUNT ON
SELECT *
INTO #c
FROM (VALUES
(-78,-35,98,-65,49),
(48,-53,-1,-18,-12),
(35,-70,-77,44,73),
(37,-75,-13,-55,97),
(-56,36,84,-34,52)
)n(x1,x2,x3,x4,x5)
SELECT wct.MTRACE_q('SELECT * FROM #c') as TRACE
DROP TABLE #c
This produces the following result.
TRACE
----------------------
-211
Using the same data, we can calculate the trace directly from the derived table.
SELECT wct.MTRACE_q('SELECT *
FROM (VALUES
(-78,-35,98,-65,49),
(48,-53,-1,-18,-12),
(35,-70,-77,44,73),
(37,-75,-13,-55,97),
(-56,36,84,-34,52)
)n(x1,x2,x3,x4,x5)') as TRACE
This produces the following result.
TRACE
----------------------
-211
Let's put several matrices into a table and calculate trace for each.
SET NOCOUNT ON
CREATE TABLE #c(
Matrix int,
rn int,
x1 float,
x2 float,
x3 float,
PRIMARY KEY (Matrix, rn)
)
INSERT INTO #c VALUES (500,1,88,32,-19)
INSERT INTO #c VALUES (500,2,-14,-7,-87)
INSERT INTO #c VALUES (500,3,-99,-43,30)
INSERT INTO #c VALUES (501,1,-10,-69,25)
INSERT INTO #c VALUES (501,2,-94,83,-40)
INSERT INTO #c VALUES (501,3,0,-1,20)
INSERT INTO #c VALUES (502,1,-73,-55,1)
INSERT INTO #c VALUES (502,2,-84,-41,-92)
INSERT INTO #c VALUES (502,3,-70,-69,-70)
INSERT INTO #c VALUES (503,1,-41,-90,86)
INSERT INTO #c VALUES (503,2,-90,88,24)
INSERT INTO #c VALUES (503,3,52,60,-37)
SELECT Matrix
,wct.MTRACE_q('SELECT x1,x2,x3 FROM #c WHERE Matrix = ' + CAST(n.Matrix as varchar(max)) + ' ORDER BY rn') as Trace
FROM (SELECT DISTINCT Matrix FROM #c)n
DROP TABLE #c
This produces the following result.
Matrix Trace
----------- ----------------------
500 111
501 93
502 -184
503 10