|
1 | 1 | export merge_graphs; |
| 2 | +export split_lincomb!; |
2 | 3 |
|
3 | 4 | # Change a symbol name using prefix & postfix |
4 | 5 | function translate_keyname(key, prefix, postfix, skip_basic, input) |
@@ -115,3 +116,71 @@ function merge_graphs( |
115 | 116 |
|
116 | 117 | return Compgraph{T}(operations, parents, coeffs, outputs) |
117 | 118 | end |
| 119 | + |
| 120 | + |
| 121 | +""" |
| 122 | + (g,cref_modified,new_crefs)=split_lincomb!(g,node,ind2; |
| 123 | + newnode=node_new, |
| 124 | + cref_list=[]) |
| 125 | +
|
| 126 | +Takes the lincomb operation in node and splits it into two lincombs, by |
| 127 | +creating a new node newnode. The new node consists of the linear combination |
| 128 | +of the coefficients moving (node, ind2[1]),... (node,ind2[end]) and the |
| 129 | +old lincomb object has the old coefficients and an additional term pointing |
| 130 | +to newnode. The cref_list is updated to the new coefficient pointers. The |
| 131 | +new_crefs list contains all the new coefficient pointers. |
| 132 | +
|
| 133 | +
|
| 134 | +In this way, the graph is unchanged but one of the linear combinations is |
| 135 | +split up into two. |
| 136 | +
|
| 137 | +""" |
| 138 | +function split_lincomb!(g,node,ind2; |
| 139 | + newnode=Symbol("$(node)_new"), |
| 140 | + cref_list=[]) |
| 141 | + |
| 142 | + @show newnode |
| 143 | + # get ind1 = complement of ind2 |
| 144 | + nof_lincombs=size(g.coeffs[node],1); |
| 145 | + @show nof_lincombs |
| 146 | + ind1=map(i -> !(i in ind2), 1:nof_lincombs) |
| 147 | + |
| 148 | + org_parents=g.parents[node]; |
| 149 | + org_coeffs=g.coeffs[node]; |
| 150 | + |
| 151 | + @show org_coeffs |
| 152 | + |
| 153 | + # Move ind2 to a new lincomb |
| 154 | + add_lincomb!(g,newnode,org_coeffs[ind2],org_parents[ind2]) |
| 155 | + |
| 156 | + @show newnode |
| 157 | + # Store ind1 lincomb info |
| 158 | + new_parents1=[org_parents[ind1];newnode] |
| 159 | + new_coeffs1=[org_coeffs[ind1];1] |
| 160 | + |
| 161 | + # Update the node lincomb data to point ind2 + newnode |
| 162 | + empty!(g.coeffs[node]); |
| 163 | + push!(g.coeffs[node], new_coeffs1...) |
| 164 | + empty!(g.parents[node]); |
| 165 | + push!(g.parents[node], new_parents1...) |
| 166 | + |
| 167 | + |
| 168 | + # Update the cref_list |
| 169 | + |
| 170 | + new_crefs=[]; |
| 171 | + replace_list=Dict(); |
| 172 | + for (j,i)=enumerate(ind2); |
| 173 | + replace_list[(node,i)]=(newnode,j) |
| 174 | + push!(new_crefs,(newnode,j)); |
| 175 | + end |
| 176 | + |
| 177 | + |
| 178 | + for (cref_old,cref_new) in replace_list |
| 179 | + @show cref_old |
| 180 | + @show cref_new |
| 181 | + ii=findall( [cref_old] .== cref_list ) |
| 182 | + map(j -> cref_list[j]=cref_new, ii); |
| 183 | + end |
| 184 | + |
| 185 | + return (g,cref_list,new_crefs) |
| 186 | +end |
0 commit comments