diprism / fggs

Factor Graph Grammars in Python
MIT License
13 stars 3 forks source link

Remove double loop in J #168

Open davidweichiang opened 1 year ago

davidweichiang commented 1 year ago

It can be changed into a single loop (thanks @darcey).

ccshan commented 1 year ago

Can I get more explanation?

davidweichiang commented 1 year ago

Currently J uses a double loop to compute $\prod_{i \in [n] \setminus {k}} weight(e_i)$ where $weight(e_i)$ is the sum-product of $e_i$'s label if nonterminal, or the weight of its factor if terminal. So it runs in O(|E|^2) time. But it can be reduced to O(|E|) time as follows:

davidweichiang commented 1 year ago

Unfortunately this isn't the easiest fix, because the outer loop is in J (and J_log) but the inner loop is in sum_product_edges.

ccshan commented 1 year ago

Thanks, this helps. So to compute $\prod_{i=1,...,k} weight(e_i)$ for example, do you mean a loop like this instead of what sum_product_edges currently does?

Is the size of these intermediate tensors (affected by the edge numbering) a concern then?

davidweichiang commented 1 year ago

Right. I guess the ordering does matter, which is something we haven't thought about at all yet. People who work on tensor networks have thought about this a lot.