n = var('n')
l = var('l')
r = var('r')
s = var('s')
g1 = var('g1')
g2 = var('g2')
restriction_l = (l <= n/2)
restriction_g1 = (g1 >= n/2, g1 >= n - l)
restriction_r = ( 2*r <=l)
restrictions = (g1 >= n/2, g1 >= n - l, 2*r <= l)
Phase1 = l
Phase2 = n + s - g1
Phase3_1 = g2
Phase3_p = r + 3 * g1 - n - s - 2 * r
Phase3_2 = Phase3_p + n - g2
Phase3_3 = Phase3_p + l + g2 - n
Phase3_4 = Phase3_p + l + s - g1
Phase3_5 = n/2 + 2 * r
show('--------------------------------------------------------------------')
show('Complexity of Phases are (log2): ')
show('Phase 1: ', Phase1)
show('Phase 2: ', Phase2)
show('Phase 3 term 1: ', Phase3_1.simplify_full())
show('Phase 3 term 2: ', Phase3_2.simplify_full())
show('Phase 3 term 3: ', Phase3_3.simplify_full())
show('Phase 3 term 4: ', Phase3_4.simplify_full())
show('Phase 3 term 5: ', Phase3_5.simplify_full())
show('--------------------------------------------------------------------')
g2s = solve([Phase3_1 == Phase3_2], g2)[0]
show('--------------------------------------------------------------------')
show('Balance the first two terms in Phase 3 by setting: ')
show(g2s)
g2s = g2s.rhs()
Phase3_1 = Phase3_1(g2 = g2s)
Phase3_2 = Phase3_2(g2 = g2s)
Phase3_3 = Phase3_3(g2 = g2s)
show('--------------------------------------------------------------------')
show('Complexity of Phases are (log2): ')
show('Phase 1: ', Phase1)
show('Phase 2: ', Phase2)
show('Phase 3 term 1: ', Phase3_1.simplify_full())
show('Phase 3 term 2: ', Phase3_2.simplify_full())
show('Phase 3 term 3: ', Phase3_3.simplify_full())
show('Phase 3 term 4: ', Phase3_4.simplify_full())
show('Phase 3 term 5: ', Phase3_5.simplify_full())
ss = solve([Phase2 == Phase3_1], s)[0]
show('--------------------------------------------------------------------')
show('Balance Phase 2 and Phase 3 by setting:')
show(ss)
ss = ss.rhs()
Phase2 = Phase2(s = ss)
Phase3_1 = Phase3_1(s = ss)
Phase3_2 = Phase3_2(s = ss)
Phase3_3 = Phase3_3(s = ss)
Phase3_4 = Phase3_4(s = ss)
show('--------------------------------------------------------------------')
show('Complexity of Phases are (log2): ')
show('Phase 1: ', Phase1)
show('Phase 2: ', Phase2)
show('Phase 3 term 1: ', Phase3_1.simplify_full())
show('Phase 3 term 2: ', Phase3_2.simplify_full())
show('Phase 3 term 3: ', Phase3_3.simplify_full())
show('Phase 3 term 4: ', Phase3_4.simplify_full())
show('Phase 3 term 5: ', Phase3_5.simplify_full())
show('--------------------------------------------------------------------')
show('Optimize the complexity by setting: ')
show(g1 == n - l)
Phase2 = Phase2(g1 = n - l)
Phase3_1 = Phase3_1(g1 = n - l)
Phase3_2 = Phase3_2(g1 = n - l)
Phase3_3 = Phase3_3(g1 = n - l)
Phase3_4 = Phase3_4(g1 = n - l)
show('--------------------------------------------------------------------')
show('Complexity of Phases are (log2): ')
show('Phase 1: ', Phase1)
show('Phase 2: ', Phase2)
show('Phase 3 term 1: ', Phase3_1.simplify_full())
show('Phase 3 term 2: ', Phase3_2.simplify_full())
show('Phase 3 term 3: ', Phase3_3.simplify_full())
show('Phase 3 term 4: ', Phase3_4.simplify_full())
show('Phase 3 term 5: ', Phase3_5.simplify_full())
rs = solve([Phase2 == Phase3_5], r)[0]
show('--------------------------------------------------------------------')
show('Balance Phase 2 and Phase 3 term 5 by setting:')
show(rs)
rs = rs.rhs()
show('This balance only valid under the restriction: ')
show(restriction_r)
show('Which implies: ')
bound_l = solve([rs <= l/2, l < n], l)[-1]
show(bound_l[0])
show('--------------------------------------------------------------------')
Phase1_case1 = Phase1(r = rs)
Phase2_case1 = Phase2(r = rs)
Phase3_1_case1 = Phase3_1(r = rs)
Phase3_2_case1 = Phase3_2(r = rs)
Phase3_3_case1 = Phase3_3(r = rs)
Phase3_4_case1 = Phase3_4(r = rs)
Phase3_5_case1 = Phase3_5(r = rs)
show('--------------------------------------------------------------------')
show('For the case')
show(bound_l[0])
show('Complexity of Phases are (log2): ')
show('Phase 1: ', Phase1_case1)
show('Phase 2: ', Phase2_case1)
show('Phase 3 term 1: ', Phase3_1_case1.simplify_full())
show('Phase 3 term 2: ', Phase3_2_case1.simplify_full())
show('Phase 3 term 3: ', Phase3_3_case1.simplify_full())
show('Phase 3 term 4: ', Phase3_4_case1.simplify_full())
show('Phase 3 term 5: ', Phase3_5_case1.simplify_full())
ls = n / 2
show('--------------------------------------------------------------------')
show('The optimal complexity is (log2):')
show('Phase1: ', Phase1_case1(l = ls).simplify_full())
show('Phase2: ', Phase2_case1(l = ls).simplify_full())
show('Phase 3, term 1:', Phase3_1_case1(l = ls).simplify_full())
show('Phase 3, term 2:', Phase3_2_case1(l = ls).simplify_full())
show('Phase 3, term 3:', Phase3_3_case1(l = ls).simplify_full())
show('Phase 3, term 4:', Phase3_4_case1(l = ls).simplify_full())
show('Phase 3, term 5:', Phase3_5_case1(l = ls).simplify_full())
show('Obtained for ')
show(ls)
show('--------------------------------------------------------------------')
bound_l_low = (bound_l[0].rhs() <= bound_l[0].lhs())
rs = (r == l/2)
Phase1_case2 = Phase1(r = l / 2)
Phase2_case2 = Phase2(r = l / 2)
Phase3_1_case2 = Phase3_1(r = l / 2)
Phase3_2_case2 = Phase3_2(r = l / 2)
Phase3_3_case2 = Phase3_3(r = l / 2)
Phase3_4_case2 = Phase3_4(r = l / 2)
Phase3_5_case2 = Phase3_5(r = l / 2)
show('--------------------------------------------------------------------')
show('For the case')
show(bound_l_low)
show('Set: ', rs)
show('Complexity of Phases are (log2): ')
show('Phase 1: ', Phase1_case2)
show('Phase 2: ', Phase2_case2)
show('Phase 3 term 1: ', Phase3_1_case2.simplify_full())
show('Phase 3 term 2: ', Phase3_2_case2.simplify_full())
show('Phase 3 term 3: ', Phase3_3_case2.simplify_full())
show('Phase 3 term 4: ', Phase3_4_case2.simplify_full())
show('Phase 3 term 5: ', Phase3_5_case2.simplify_full())
ls = bound_l_low.rhs()
show('--------------------------------------------------------------------')
show('The optimal complexity is (log2):')
show('Phase1: ', Phase1_case1(l = ls).simplify_full())
show('Phase2: ', Phase2_case1(l = ls).simplify_full())
show('Phase 3, term 1:', Phase3_1_case1(l = ls).simplify_full())
show('Phase 3, term 2:', Phase3_2_case1(l = ls).simplify_full())
show('Phase 3, term 3:', Phase3_3_case1(l = ls).simplify_full())
show('Phase 3, term 4:', Phase3_4_case1(l = ls).simplify_full())
show('Phase 3, term 5:', Phase3_5_case1(l = ls).simplify_full())
show('Obtained for ')
show(ls)
show('--------------------------------------------------------------------')
--------------------------------------------------------------------
Complexity of Phases are (log2):
Phase 1: l Phase 2: −g1+n+s Phase 3 term 1: g2 Phase 3 term 2: 3g1−g2−r−s Phase 3 term 3: 3g1+g2+l−2n−r−s Phase 3 term 4: 2g1+l−n−r Phase 3 term 5: 21n+2r --------------------------------------------------------------------
--------------------------------------------------------------------
Balance the first two terms in Phase 3 by setting:
g2=23g1−21r−21s --------------------------------------------------------------------
Complexity of Phases are (log2):
Phase 1: l Phase 2: −g1+n+s Phase 3 term 1: 23g1−21r−21s Phase 3 term 2: 23g1−21r−21s Phase 3 term 3: 29g1+l−2n−23r−23s Phase 3 term 4: 2g1+l−n−r Phase 3 term 5: 21n+2r --------------------------------------------------------------------
Balance Phase 2 and Phase 3 by setting:
s=35g1−32n−31r --------------------------------------------------------------------
Complexity of Phases are (log2):
Phase 1: l Phase 2: 32g1+31n−31r Phase 3 term 1: 32g1+31n−31r Phase 3 term 2: 32g1+31n−31r Phase 3 term 3: 2g1+l−n−r Phase 3 term 4: 2g1+l−n−r Phase 3 term 5: 21n+2r --------------------------------------------------------------------
Optimize the complexity by setting:
g1=−l+n --------------------------------------------------------------------
Complexity of Phases are (log2):
Phase 1: l Phase 2: −32l+n−31r Phase 3 term 1: −32l+n−31r Phase 3 term 2: −32l+n−31r Phase 3 term 3: −l+n−r Phase 3 term 4: −l+n−r Phase 3 term 5: 21n+2r --------------------------------------------------------------------
Balance Phase 2 and Phase 3 term 5 by setting:
r=−72l+143n This balance only valid under the restriction:
2r≤l 113n<l --------------------------------------------------------------------
--------------------------------------------------------------------
113n<l Complexity of Phases are (log2):
Phase 1: l Phase 2: −74l+1413n Phase 3 term 1: −74l+1413n Phase 3 term 2: −74l+1413n Phase 3 term 3: −75l+1411n Phase 3 term 4: −75l+1411n Phase 3 term 5: −74l+1413n --------------------------------------------------------------------
The optimal complexity is (log2):
Phase1: 21n Phase2: 149n Phase 3, term 1: 149n Phase 3, term 2: 149n Phase 3, term 3: 73n Phase 3, term 4: 73n Phase 3, term 5: 149n 21n --------------------------------------------------------------------
--------------------------------------------------------------------
l≤113n Set: r=21l Complexity of Phases are (log2):
Phase 1: l Phase 2: −65l+n Phase 3 term 1: −65l+n Phase 3 term 2: −65l+n Phase 3 term 3: −23l+n Phase 3 term 4: −23l+n Phase 3 term 5: l+21n --------------------------------------------------------------------
The optimal complexity is (log2):
Phase1: 113n Phase2: 2217n Phase 3, term 1: 2217n Phase 3, term 2: 2217n Phase 3, term 3: 2213n Phase 3, term 4: 2213n Phase 3, term 5: 2217n 113n --------------------------------------------------------------------