MathGroup Archive 2011

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

Search the Archive

Elementwise Matrix Subtraction

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121667] Elementwise Matrix Subtraction
  • From: velvetfish1 at hotmail.com
  • Date: Sat, 24 Sep 2011 22:34:57 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

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.




  • Prev by Date: Re: Compilation: Avoiding inlining
  • Next by Date: Re: problem opening mathematica
  • Previous by thread: Re: Manipulate for differences tables. Can I call as function?
  • Next by thread: Re: Elementwise Matrix Subtraction