# Testing the young tableau routines by determining the projection
# of the square of a four-component tensor onto the 2x4 tableau.

def test01():
    __cdbkernel__=create_scope()
    W_{a b c d}::WeylTensor.
    WWa:= W_{r1 r2 s1 s2} W_{r3 r4 s3 s4};
    WWb:= 16/5*@(WWa):
    # Note: the 16/5 normalisation was computed separately.
    # The direct route:
    young_project(WWa, shape=[2,2,2,2], indices=[0,2,1,3,4,6,5,7])
    indexsort(WWa)
    order(WWa, $_{r1},_{r2},_{r3},_{r4}$, anticommuting=True)
    order(WWa, $_{s1},_{s2},_{s3},_{s4}$, anticommuting=True)
    # Doing it by hand:
    sym(WWb, $_{r1},_{s1}$)
    sym(WWb, $_{r2},_{s2}$)
    sym(WWb, $_{r3},_{s3}$)
    sym(WWb, $_{r4},_{s4}$)
    asym(WWb, $_{r1},_{r2},_{r3},_{r4}$)
    asym(WWb, $_{s1},_{s2},_{s3},_{s4}$)
    indexsort(WWb)
    order(WWb, $_{r1},_{r2},_{r3},_{r4}$, anticommuting=True)
    order(WWb, $_{s1},_{s2},_{s3},_{s4}$, anticommuting=True)
    tst1:= @(WWa)-@(WWb);
    assert(tst1==0)
    print('Test 01 passed')

test01()

# Test 27: The example from the paper with Michael and Christian:
#

def test27():
    __cdbkernel__=create_scope()
    R_{m n p q}::RiemannTensor.
    {m,n,p,q,r,s,t,u}::Indices(vector).
    term1:= R_{m n p q} R_{m n p q};
    young_project_tensor(_)
    distribute(_)
    canonicalise(_)
    term2:= R_{m n p q} R_{m p n q};
    young_project_tensor(_)
    distribute(_)
    canonicalise(_)
    tst27:= 2*@(term2) - @(term1);
    assert(tst27==0)
    print('Test 27 passed')

test27()

# Test 27b: The same as above, but now using young_project_product.

def test27b():
    __cdbkernel__=create_scope()
    R_{m n p q}::RiemannTensor.
    {m,n,p,q,r,s,t,u}::Indices(vector).
    term1:= R_{m n p q} R_{m n p q};
    young_project_product(_)
    term2:= R_{m n p q} R_{m p n q};
    young_project_product(_)
    tst27b:= 2*@(term2) - @(term1);
    assert(tst27b==0)
    print('Test 27b passed')

test27b()

# Test 28: Simple Bianchi.

def test28():
    __cdbkernel__=create_scope()
    {m,n,p,q,r,s,t#}::Indices(vector).
    \nabla{#}::Derivative.
    R_{m n p q}::RiemannTensor.
    \nabla_{m}{R_{p q r s}}::SatisfiesBianchi.
    obj28:= \nabla_{m}{R_{p q r s}} + \nabla_{p}{R_{q m r s}} + \nabla_{q}{R_{m p r s}}:
    young_project_tensor(_, deep=False, depth=1)
    assert(obj28==0)
    print('Test 28 passed')

test28()

# Test 30: SatisfiesBianchi
#

# {r,m,n,p,q,s,k,j}::Indices(vector).
# dC_{r m n p q}::TableauSymmetry(shape={3,2}, indices={1,3,0,2,4}).
# C_{m n p q}::WeylTensor.
# \nabla{#}::Derivative.
# \nabla_{r}{ C_{m n p q} }::SatisfiesBianchi.
# 
# pl1:= dC_{r m n p q};
# young_project_tensor(_)
# pl2:= \nabla_{s}{\nabla_{r}{C_{m n p q}}};
# young_project_tensor(_, depth=1)
# 
# rl:=dC_{r m n p q} -> \nabla_{s}{\nabla_{r}{C_{m n p q}}};
# substitute(pl1, rl)
# tst:= @(pl1) - @(pl2);
# @collect_terms!(%);
# @assert(tst);
# 

def test29():
    __cdbkernel__=create_scope()
    R_{m n p q}::RiemannTensor.
    {m,n,p,q,r,s,t,u}::Indices(vector).
    term1:= R_{m n p q} R_{m n p q};
    young_project_tensor(_, modulo_monoterm=True)
    distribute(_)
    term2:= R_{m n p q} R_{m p n q};
    young_project_tensor(_, modulo_monoterm=True)
    distribute(_)
    tst:= 2*@(term2) - @(term1);
    canonicalise(_)
    assert(tst==0)
    print('Test 29 passed')

test29()

# The 8x8 tensor product in Young tableaux.
#
def test30():
    \tableau{#}::Tableau(dimension=3);
    \ftableau{#}::FilledTableau(dimension=3);
    ex:=\tableau{2}{1}\tableau{2}{1};
    lr_tensor(_)
    q=[int(tab_dimension(t.ex())) for t in ex.top().terms()]
    assert(q==[27,10,10,16,1])
    print("Test 30a passed")
    ex:=\ftableau{c,c}{c}\ftableau{a,a}{b};
    lr_tensor(_)
    q=[int(tab_dimension(t.ex())) for t in ex.top().terms()]
    assert(q==[27,10,10,8,8,1])
    print("Test 30b passed")

test30()
    
# def test30():
# \tableau{#}::Tableau(dimension=10);
# ex:=\tableau{2}{2}\tableau{2}{2} \tableau{2};
# converge(ex):
#     lr_tensor(ex)
#     distribute(ex)
# 
# q=[]
# p=[]
# for t in ex.top().terms():
#     p.append(t.ex())
#     q.append(int(tab_dimension(t.ex())))
# 
#     
# assert(q==[



    
