Overloading arithmetic on graphs or digraphs

2 ビュー (過去 30 日間)
Roy Goodman
Roy Goodman 2017 年 6 月 5 日
コメント済み: Roy Goodman 2017 年 6 月 5 日
I am solving some problems in "quantum graphs." Without getting into the detail, I have a directed graph, built as a MATLAB digraph object. On each edge, I have defined a coordinate x, given as a discrete sequence of x values, and a function y(x) which is an array of the same size.
To do any computations on this object, I have a routine that reorders all the y-values into a column vector. I do all my computations on this column vector. Then I have another routine to convert the column vector of y-values into a graph with the same same structure as my original graph.
For reasons of problem abstraction, I would like to be able to add and subtract these y-values in place, and to multiply them by scalars, without first converting to column vectors and then converting back. I'm using MATLAB's digraph objects to build these quantum graphs. Can I somehow overload plus, times, minus, etc to work on digraphs? I'm a bit confused, since it's not a class I've defined myself.
  1 件のコメント
John D'Errico
John D'Errico 2017 年 6 月 5 日
You can do virtually anything you want, IF you know how to do it. My gut tells me this is not worth the time you would invest in doing the job. But it is your time after all, so it costs me nothing.



Christine Tobler
Christine Tobler 2017 年 6 月 5 日
I'm afraid methods for MATLAB's digraph class can't be overloaded. There is still a way to get what you want, but it may be more work than it's worth:
You could write a new class, quantumGraph for example, that would contain a digraph class as a property. You would then overload the operators for this class (see how here).
However, you would also have to overload all methods of digraph that you are currently using for quantumGraph, to pass the data along to the digraph property inside the quantumGraph class.
This is a lot of work! The easier variant would be to just write functions quantumGraphSum, quantumGraphTimes, and so on, which would, for example, take a digraph and a scalar, and return a new digraph with modified properties.
  1 件のコメント
Roy Goodman
Roy Goodman 2017 年 6 月 5 日
That's what I was afraid of. I had started on the easier variant already, and it's going to make some ugly code!


その他の回答 (1 件)

Steven Lord
Steven Lord 2017 年 6 月 5 日
You can't add additional methods to digraph, nor can you subclass it. But there may still be a way for you to do something close to what you want. You can add custom data to the Nodes and Edges table in a digraph and operate on that custom data. See the "Add Custom Attributes" and later sections on that documentation page. For example, using the digraph from that page with Weight and Power variables in the Edges table:
function G = togglePower(G, rows)
% Assume G is a graph or digraph whose Edges table has a variable Power
% Also assume rows is a vector of row numbers to toggle
for k = 1:length(rows)
if strcmp(G.Edges{rows(k), 'Power'}, 'on')
G.Edges{rows(k), 'Power'} = {'off'};
G.Edges{rows(k), 'Power'} = {'on'};
Call this as:
G = togglePower(G, [2 4]);
The resulting digraph should have Power 'on' for edges 1, 3, and 4.
  1 件のコメント
Roy Goodman
Roy Goodman 2017 年 6 月 5 日
Thanks. The fields x and y I mentioned are implemented as Custom Attributes.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by