Re: lower diagonal matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg80445] Re: lower diagonal matrix
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 22 Aug 2007 04:54:00 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <fae9s7$eai$1@smc.vnet.net>
Sean Bonness wrote:
> Hello...
>
> I am in need of some help in trying to convert data I got from
> another program into mathematica so that I can eventually diagonalize
> the matrix. What I have from the other program are values for a lower
> diagonal matrix, which corresponds to a full symmetric matrix and
> whereby the program tries to save on writing alot of data by just
> writing the lower diagonal matrix. The values I have from another
> program are in a five column by 56 row matrix. The first element
> (1,1) in this matrix corresponds to the (1,1) element in a lower
> diagonal matrix, the second element (1,2) corresponds to (2,1), the
> third (1,3) corresponds to (2,2), the fourth (1,4) corresponds to
> (3,1), the fifth (1,5) corresponds to (3,2), the sixth (2,1)
> corresponds to (3,3) and so on. What I have done so far is read in
> the data into mathematica, but when I do it reads it in as a one
> dimensional list. What I would like to do is to break up the data
> into a human readable form of a lower diagonal matrix and then
> construct a full symmetric (23x23) matrix of this data so as to
> eventually diagonalize and solve it. The lower diagonal elements are
> the same as the upper diagonal elements, so I understand that I would
> need to specify the elements to be a[i,j]=a[j,i] somewhere in
> defining the symmetric matrix. Any help in trying to help me write/
> construct the full symmetric matrix from the data I have would be
> much appreciated.
The following function, called "uncompress", should do what you are
looking for. My understanding is that the structure of the imported
matrix is irrelevant, so we use a flat list (i.e. a vector or one
dimensional list) to work on. The position of the element to be
extracted is one plus the triangular number of the value of the index
and the length is the value of the index. Simultaneously, we pad the
extracted sub-list by the required number of zeros. Finally, we add the
conjugate and remove once the diagonal to get a symmetric matrix.
In[1]:= uncompress[lst_List, size_Integer] :=
Module[{data = Flatten[lst], pos, mat},
pos[m_] := m (m + 1)/2 + 1;
mat = Table[
PadRight[data[[pos[m] ;; pos[m] + m]], size], {m, 0, size - 1}];
mat + ConjugateTranspose[mat] - IdentityMatrix[size] mat
]
In[2]:= data = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14,
15}};
In[3]:= m = uncompress[data, 5]
Out[3]= {{1, 2, 4, 7, 11}, {2, 3, 5, 8, 12}, {4, 5, 6, 9, 13}, {7, 8,
9, 10, 14}, {11, 12, 13, 14, 15}}
In[4]:= HermitianMatrixQ[m]
Out[4]= True
In[5]:= m // TableForm
Out[5]//TableForm= \!\(\*
TagBox[GridBox[{
{"1", "2", "4", "7", "11"},
{"2", "3", "5", "8", "12"},
{"4", "5", "6", "9", "13"},
{"7", "8", "9", "10", "14"},
{"11", "12", "13", "14", "15"}
},
GridBoxAlignment->{
"Columns" -> {{Left}}, "ColumnsIndexed" -> {},
"Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.7]},
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]},
Offset[0.2]}, "RowsIndexed" -> {}}],
Function[BoxForm`e$,
TableForm[BoxForm`e$]]]\)
In[6]:= data = RandomReal[{0, 1}, {56, 5}];
In[7]:= m = uncompress[data, 23]
Out[7]= {{0.710717, 0.131138, 0.221324, 0.226394, 0.49073, 0.0149741,
0.692508, 0.953457, 0.233841, 0.783827, 0.588755, 0.937376,
0.659113, 0.928914, 0.775069, 0.656546, 0.167131, 0.534274,
0.0323822, 0.431196, 0.562121, 0.970309, 0.477049}, {0.131138,
0.527486, 0.125354, 0.218365, 0.715535, 0.574485, 0.461012,
0.067413, 0.00367435, 0.557646, 0.240758, 0.141335, 0.446906,
0.716553, 0.822357, 0.415322, 0.206348, 0.154103, 0.201439,
0.763963, 0.384584, 0.878335, 0.429067}, {0.221324, 0.125354,
0.812676, 0.547281, 0.328656, 0.997729, 0.849957, 0.142776,
0.0605619, 0.180052, 0.929873, 0.419411, 0.364597, 0.442295,
0.586875, 0.724169, 0.985189, 0.0696359, 0.0289478, 0.708608,
0.40603, 0.734883, 0.0419927}, {0.226394, 0.218365, 0.547281,
0.0492387, 0.223998, 0.354611, 0.16016, 0.368427, 0.66816, 0.212199,
0.913719, 0.201456, 0.382018, 0.521089, 0.224076, 0.415497,
0.253636, 0.053183, 0.788375, 0.166598, 0.719781, 0.0429214,
0.360938}, {0.49073, 0.715535, 0.328656, 0.223998, 0.908519,
0.925605, 0.281155, 0.494699, 0.265922, 0.0812629, 0.410171,
0.209307, 0.328203, 0.234251, 0.182982, 0.804365, 0.988496,
0.388223, 0.692343, 0.11226, 0.978131, 0.55466,
0.920261}, {0.0149741, 0.574485, 0.997729, 0.354611, 0.925605,
0.609964, 0.240646, 0.487819, 0.88161, 0.654632, 0.414674, 0.3851,
0.835877, 0.42225, 0.452797, 0.745494, 0.288863, 0.263505, 0.306902,
0.415946, 0.904017, 0.256359, 0.0602231}, {0.692508, 0.461012,
0.849957, 0.16016, 0.281155, 0.240646, 0.110068, 0.663708, 0.826099,
0.763479, 0.275342, 0.231679, 0.383075, 0.178933, 0.0194876,
0.402809, 0.285574, 0.63012, 0.064015, 0.544356, 0.386216, 0.274245,
0.945935}, {0.953457, 0.067413, 0.142776, 0.368427, 0.494699,
0.487819, 0.663708, 0.299842, 0.020306, 0.951123, 0.650772,
0.156161, 0.550351, 0.985315, 0.846965, 0.207978, 0.0599321,
0.992182, 0.616287, 0.85736, 0.933834, 0.060046,
0.45068}, {0.233841, 0.00367435, 0.0605619, 0.66816, 0.265922,
0.88161, 0.826099, 0.020306, 0.522174, 0.353829, 0.624172, 0.420401,
0.243934, 0.460364, 0.0803673, 0.3786, 0.215833, 0.406435,
0.0848938, 0.383003, 0.39354, 0.276166, 0.617732}, {0.783827,
0.557646, 0.180052, 0.212199, 0.0812629, 0.654632, 0.763479,
0.951123, 0.353829, 0.0308089, 0.90062, 0.47999, 0.176914, 0.449639,
0.575474, 0.348583, 0.197707, 0.321705, 0.713142, 0.922015,
0.520696, 0.429628, 0.222807}, {0.588755, 0.240758, 0.929873,
0.913719, 0.410171, 0.414674, 0.275342, 0.650772, 0.624172, 0.90062,
0.311941, 0.617701, 0.662665, 0.338894, 0.385367, 0.101996,
0.963393, 0.305829, 0.0930515, 0.0336846, 0.433405, 0.221707,
0.876067}, {0.937376, 0.141335, 0.419411, 0.201456, 0.209307,
0.3851, 0.231679, 0.156161, 0.420401, 0.47999, 0.617701, 0.252413,
0.0594674, 0.940329, 0.449275, 0.0198093, 0.0225507, 0.869569,
0.384659, 0.0734106, 0.501737, 0.915843, 0.377367}, {0.659113,
0.446906, 0.364597, 0.382018, 0.328203, 0.835877, 0.383075,
0.550351, 0.243934, 0.176914, 0.662665, 0.0594674, 0.313136,
0.56292, 0.571232, 0.558606, 0.913633, 0.753722, 0.265655, 0.873647,
0.88088, 0.155241, 0.970118}, {0.928914, 0.716553, 0.442295,
0.521089, 0.234251, 0.42225, 0.178933, 0.985315, 0.460364, 0.449639,
0.338894, 0.940329, 0.56292, 0.46182, 0.19523, 0.864812, 0.788098,
0.581585, 0.169576, 0.618853, 0.849762, 0.716902,
0.209603}, {0.775069, 0.822357, 0.586875, 0.224076, 0.182982,
0.452797, 0.0194876, 0.846965, 0.0803673, 0.575474, 0.385367,
0.449275, 0.571232, 0.19523, 0.956367, 0.0847374, 0.175862,
0.0584801, 0.621011, 0.197968, 0.39124, 0.884344,
0.351673}, {0.656546, 0.415322, 0.724169, 0.415497, 0.804365,
0.745494, 0.402809, 0.207978, 0.3786, 0.348583, 0.101996, 0.0198093,
0.558606, 0.864812, 0.0847374, 0.112901, 0.678126, 0.18461,
0.215131, 0.636855, 0.0545228, 0.798171, 0.619516}, {0.167131,
0.206348, 0.985189, 0.253636, 0.988496, 0.288863, 0.285574,
0.0599321, 0.215833, 0.197707, 0.963393, 0.0225507, 0.913633,
0.788098, 0.175862, 0.678126, 0.0281418, 0.48584, 0.592625,
0.762454, 0.419555, 0.807593, 0.0888013}, {0.534274, 0.154103,
0.0696359, 0.053183, 0.388223, 0.263505, 0.63012, 0.992182,
0.406435, 0.321705, 0.305829, 0.869569, 0.753722, 0.581585,
0.0584801, 0.18461, 0.48584, 0.234736, 0.195808, 0.285563, 0.812998,
0.355011, 0.198806}, {0.0323822, 0.201439, 0.0289478, 0.788375,
0.692343, 0.306902, 0.064015, 0.616287, 0.0848938, 0.713142,
0.0930515, 0.384659, 0.265655, 0.169576, 0.621011, 0.215131,
0.592625, 0.195808, 0.553445, 0.749319, 0.272007, 0.877568,
0.640487}, {0.431196, 0.763963, 0.708608, 0.166598, 0.11226,
0.415946, 0.544356, 0.85736, 0.383003, 0.922015, 0.0336846,
0.0734106, 0.873647, 0.618853, 0.197968, 0.636855, 0.762454,
0.285563, 0.749319, 0.437411, 0.997179, 0.236881,
0.459469}, {0.562121, 0.384584, 0.40603, 0.719781, 0.978131,
0.904017, 0.386216, 0.933834, 0.39354, 0.520696, 0.433405, 0.501737,
0.88088, 0.849762, 0.39124, 0.0545228, 0.419555, 0.812998,
0.272007, 0.997179, 0.991409, 0.0799093, 0.451535}, {0.970309,
0.878335, 0.734883, 0.0429214, 0.55466, 0.256359, 0.274245,
0.060046, 0.276166, 0.429628, 0.221707, 0.915843, 0.155241,
0.716902, 0.884344, 0.798171, 0.807593, 0.355011, 0.877568,
0.236881, 0.0799093, 0.598423, 0.924888}, {0.477049, 0.429067,
0.0419927, 0.360938, 0.920261, 0.0602231, 0.945935, 0.45068,
0.617732, 0.222807, 0.876067, 0.377367, 0.970118, 0.209603,
0.351673, 0.619516, 0.0888013, 0.198806, 0.640487, 0.459469,
0.451535, 0.924888, 0.0490088}}
In[8]:= HermitianMatrixQ[m]
Out[8]= True
In[9]:= $Version
Out[9]= "6.0 for Microsoft Windows (32-bit) (June 19, 2007)"
Regards,
--
Jean-Marc