Smart set (database?)

asked 2016-01-15 21:46:28 +0200

Rolandb gravatar image

Hi,

What I would like is to define a smart hierarchical set (or database) like:

 example_set={(0,0,0):[4,[7,11,0]], (0,0,1):[-5,[8,-3,6]], (0,1,0):[4,[7,11,-1]]}

The lefthand side is an hierarchy (a,b,c,d,e,...); the righthand side is some structure [, [,,,...]] consisting of nested lists. How can I quickly -via a smart design- determine for instance the sum for a higher level? For instance example_set.sum[(0,0)] would yield [4-5, [7,11,0] + [8,-3,6]] and example_set.sum[(0,)] yields [4-5+4, [7,11,0] + [8,-3,6] + [7,11,-1]].

The setting is that we have a few thousand hierarchical elements with unknown depth, but always the same structure on the righthand side. The structure could contain many nested lists with in total several hunderd values (flattened). I like to have (fast) basic operators for the lists within the structure such as addition and index.

Another example.

next_example={(0,0,0):[(5,6)], (0,0,1):[(8,9),(3,4)], (0,1,0):[(2,6)], (0,1,1):[(8,16)]} 
(wanted) print next_example.sum[(0, 0)]
[(5, 6), (8, 9), (3, 4)]

I considered to define a class and I looked at database. But that didn't help me much :(

To complicate matters, I would also like to connect hierarchical levels (one-direction). For instance in the first example assume (0,0,0) is connected to (0,1,1). How to store and display such a connection? For connections I looked for instance at igraph. But it isn't clear to me whether it would be helpful in combination with the above mentioned set design.

To summarize: it would be awesome if a routine 'smart_set(n, structure)' would define a n-level hierarchy whereby operations like .sum(input) and .sum.connected(input) and .show_graph() would work.

Suggestions how to move forward in Sage are much appreciated!

Roland

edit retag flag offensive close merge delete

Comments

What is addition of two lists for you?

vdelecroix gravatar imagevdelecroix ( 2016-01-15 23:53:42 +0200 )edit

For me [1,2] + [3,4] yields [1,2,3,4].

Rolandb gravatar imageRolandb ( 2016-01-16 22:55:50 +0200 )edit

Your examples behave different. The second example concatenates lists, the first one adds up the respective items of two lists.

ndomes gravatar imagendomes ( 2016-01-17 19:04:49 +0200 )edit

Yes, the examples behave different. In general if [A[B[C]]] and [X[Y[Z]]] are element of the structure, I want to define what A+X, B+Y and C+Z are. It can be concatenating a list or the addition of numbers. The structure therefor must be defined including the addition operators.

Rolandb gravatar imageRolandb ( 2016-01-18 22:37:00 +0200 )edit

An experiment with your examples: https://sagecell.sagemath.org/?q=yterpq

ndomes gravatar imagendomes ( 2016-01-19 13:41:06 +0200 )edit