1
$\begingroup$

I work with linear combinations of graphs, $$c_1 G_1 + c_2 G_2 + \dotsc,$$ and I want to represent them in my Mathematica code. I represent graphs as adjacency matrices, e.g.

{{0,1},{1,0}}

The next step is to write down linear combinations of these matrices. However, I want to implement formal linear combinations of the kind

lin = 5 * AdjMtx[{{0,1},{1,0}}] + 3 * AdjMtx[{{1,0},{0,1}}]

with an unevaluated "function" or type AdjMtx. The reason I don't want to write

5 * {{0,1},{1,0}} + 3 * {{1,0},{0,1}}

is that Mathematica then treats the adjacency matrices as normal matrices which admit multiplication my scalars etc.

AdjMtx should allow other functions to access the contents of adjacency matrices as usual. For example, a function should be able to search through a linear combination of adjacency matrices and read out their respective matrix elements.

I guess that moving through a linear combination, e.g. lin from above, is just done using Part. Then the question is how to do implement something like

AdjMtx[...][[3,4]]
$\endgroup$
3
  • $\begingroup$ Can you give one or two simple examples of a function that does something meaningful to lin? $\endgroup$
    – halirutan
    Feb 23, 2018 at 11:57
  • $\begingroup$ It could be a function that replaces loops in the graph my certain numbers, just to name one example. Basically I want to access the adjacency matrices as if they were normal Mathematica lists, I want to distinguish them semantically from usual matrices. $\endgroup$
    – Deniz
    Feb 23, 2018 at 14:07
  • $\begingroup$ Might be useful to work with something other than Plus. Less clear is whether Times should also be replaced or in other ways inhibited e.g. by using a head for the matrices that is not List. $\endgroup$ Feb 24, 2018 at 17:49

2 Answers 2

1
$\begingroup$

Use a HoldAll instead of an unevaluated function, or define your AdjMtx function as HoldAll

In[127]:= AdjMtx[x___] := HoldAll[x];

In[128]:= lin = 5*AdjMtx[{{0, 1}, {1, 0}}] + 3*AdjMtx[{{1, 0}, {0, 1}}]

Out[128]= 5 HoldAll[{{0, 1}, {1, 0}}] + 3 HoldAll[{{1, 0}, {0, 1}}]

In[129]:= lin[[1]]

Out[129]= 5 HoldAll[{{0, 1}, {1, 0}}]

In[130]:= lin[[1, 2, 1, 1]]

Out[130]= {0, 1}
$\endgroup$
0
$\begingroup$

I am not sure whether I understand correclty what you are after, but I think that using UpValues should help you solve your problem, e.g. this definition:

Part[AdjMtx[data_], idcs___] ^:= Part[data, idcs]

will make it possible to access the matrix elements just as desired:

a = AdjMtx[{{1,0},{0,1}}];
a[[1,1]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.