Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Elementwise Matrix Subtraction

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121680] Re: Elementwise Matrix Subtraction
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Sun, 25 Sep 2011 05:42:59 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201109250234.WAA26217@smc.vnet.net>
  • Reply-to: murray at math.umass.edu

Once you take MatrixForm of a nested list representing a matrix, you no 
longer have that "matrix" and so cannot perform matrix operations upon 
it. Essentially, MatrixForm is something you want to use only to display 
a list of lists as a matrix.

The following gives the same result for km1 as for km2:


   examplegraph =
     Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3,
       3 \[UndirectedEdge] 4, 4 \[UndirectedEdge] 5,
       4 \[UndirectedEdge] 6, 1 \[UndirectedEdge] 5,
       2 \[UndirectedEdge] 5}, {VertexLabels -> "Name"}, {ImagePadding ->
          10}];
   km1 = KirchhoffMatrix[examplegraph]; km1 // MatrixForm
   am = AdjacencyMatrix[examplegraph]; am // MatrixForm
   im = DiagonalMatrix[{2, 3, 2, 3, 3, 1}]; im // MatrixForm
   km2 = im - am; km2 // MatrixForm


On 9/24/11 10:34 PM, velvetfish1 at hotmail.com wrote:
> I am trying to do a simple element-wise matrix subtraction to obtain
> the Laplacian Matrix of a Graph, by doing an element wise subtraction
> of the adjacency matrix, using AdjacenyMatrix[] from the incidence
> matrix derived from input using DiagonalMatrix[] that I am using as a
> simple check to understand more complex matrices have been properly
> specified.  This should give the same result as KirchhoffMatrix[].
> However, I get a behavior I don't quite understand.
>
> Rather than seeing the evaluated result of the element-wise
> subtraction instead I see, when visualizing in //MatrixForm the
> difference of the two matrices each shown in MatrixForm  but
> symbolically specified as the difference two separate matrices, rather
> than as a single matrix with the subtraction evaluated.  I've tried
> doing the subtraction either in a map or only by using the
> Subtraction[] function.   It seems that in either case the kernel is
> not evaluating the subtraction, but I can not figure out why.  There
> is no HoldOn[] being specified as far as I know, but am unsure how to
> tell, not having specified one.
>
> The code is as follows:
>
> examplegraph =
>   Graph[{1 \[UndirectedEdge] 2,  2 \[UndirectedEdge] 3,
>     3 \[UndirectedEdge] 4, 4 \[UndirectedEdge] 5,
>     4 \[UndirectedEdge] 6, 1 \[UndirectedEdge] 5,
>     2 \[UndirectedEdge] 5}, {VertexLabels ->  "Name"}, {ImagePadding ->
>      10}]
>
> km1 = KirchhoffMatrix[examplegraph] // MatrixForm
>
> am = AdjacencyMatrix[examplegraph] // MatrixForm
>
> im = DiagonalMatrix[{2, 3, 2, 3, 3, 1}] // MatrixForm
>
> km2 = MatrixForm /@ {im - am}
>
> km3 = Subtract[dm, am]
>
> km2 // MatrixForm
>
> km3 // MatrixForm
>
>
> Thus the output looks like:
>
> \!\(\*
> TagBox[
> RowBox[{
> TagBox[
> RowBox[{"(", "", GridBox[{
> {"2", "0", "0", "0", "0", "0"},
> {"0", "3", "0", "0", "0", "0"},
> {"0", "0", "2", "0", "0", "0"},
> {"0", "0", "0", "3", "0", "0"},
> {"0", "0", "0", "0", "3", "0"},
> {"0", "0", "0", "0", "0", "1"}
> },
> GridBoxAlignment->{
>          "Columns" ->  {{Center}}, "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$,
> MatrixForm[BoxForm`e$]]], "-",
> TagBox[
> RowBox[{"(", "", GridBox[{
> {"0", "1", "0", "0", "1", "0"},
> {"1", "0", "1", "0", "1", "0"},
> {"0", "1", "0", "1", "0", "0"},
> {"0", "0", "1", "0", "1", "1"},
> {"1", "1", "0", "1", "0", "0"},
> {"0", "0", "0", "1", "0", "0"}
> },
> GridBoxAlignment->{
>          "Columns" ->  {{Center}}, "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$,
> MatrixForm[
>        SparseArray[
>         Automatic, {6, 6}, 0, {
>          1, {{0, 2, 5, 7, 10, 13, 14}, {{2}, {5}, {1}, {3}, {5}, {2}, {
>            4}, {3}, {5}, {6}, {4}, {1}, {2}, {4}}}, {1, 1, 1, 1, 1, 1,
>           1, 1, 1, 1, 1, 1, 1, 1}}]]]]}],
> Function[BoxForm`e$,
> MatrixForm[BoxForm`e$]]]\)
>
> Rather than:
>
> \!\(\*
> TagBox[
> RowBox[{"(", "", GridBox[{
> {"2",
> RowBox[{"-", "1"}], "0", "0",
> RowBox[{"-", "1"}], "0"},
> {
> RowBox[{"-", "1"}], "3",
> RowBox[{"-", "1"}], "0",
> RowBox[{"-", "1"}], "0"},
> {"0",
> RowBox[{"-", "1"}], "2",
> RowBox[{"-", "1"}], "0", "0"},
> {"0", "0",
> RowBox[{"-", "1"}], "3",
> RowBox[{"-", "1"}],
> RowBox[{"-", "1"}]},
> {
> RowBox[{"-", "1"}],
> RowBox[{"-", "1"}], "0",
> RowBox[{"-", "1"}], "3", "0"},
> {"0", "0", "0",
> RowBox[{"-", "1"}], "0", "1"}
> },
> GridBoxAlignment->{
>        "Columns" ->  {{Center}}, "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$,
> MatrixForm[
>      SparseArray[
>       Automatic, {6, 6}, 0, {
>        1, {{0, 3, 7, 10, 14, 18, 20}, {{1}, {2}, {5}, {2}, {1}, {3}, {
>          5}, {3}, {2}, {4}, {4}, {3}, {5}, {6}, {5}, {4}, {1}, {2}, {
>          6}, {4}}}, {2, -1, -1, 3, -1, -1, -1, 2, -1, -1,
>         3, -1, -1, -1, 3, -1, -1, -1, 1, -1}}]]]]\)
>
> As I would expect.
>
> Does anyone have an idea why the evaluation does not take place, even
> when it is explicitly evaluated?  Could this have something to do with
> "automatic" display or evaluation when a Sparce Array is being created
> implicitly?
>
> Thanks in advance for any help, someone might kindly provide.
>

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305




  • Prev by Date: Re: A fast way to compare two vectors
  • Next by Date: Re: A fast way to compare two vectors
  • Previous by thread: Elementwise Matrix Subtraction
  • Next by thread: Nonhomogeneous Wave Equation with NDSolve