#!/usr/bin/python
#test_GA.py

"""
The reference D&L is "Geomertric Algebra for Physicists" by Doran and Lasenby
"""

import sys

try:
    import numpy
    disabled = False
except ImportError:
    #py.test will not execute any tests now
    disabled = True

if not disabled:
    sys.path.append('../')
    from sympy.galgebra.GA import set_main, MV, make_symbols, types, ZERO, ONE, HALF, S
    import sympy
    from sympy import collect, sympify


    set_main(sys.modules[__name__])

def F(x):
    """
    Conformal Mapping Function from 3D euclidian space to 5D conformal space
    where the images of all maps are null vectors.
    """
    Fx = HALF*((x*x)*n+2*x-nbar)
    #print 'F(x) =',Fx
    return(Fx)

def make_vector(a,n = 3):
    if type(a) == types.StringType:
        sym_str = ''
        for i in range(n):
            sym_str += a+str(i)+' '
        sym_lst = make_symbols(sym_str)
        sym_lst.append(ZERO)
        sym_lst.append(ZERO)
        a = MV(sym_lst,'vector')
    return(F(a))

def test_rmul():
    """
    Test for communitive scalar multiplication.  Leftover from when sympy and
    numpy were not working together and __mul__ and __rmul__ would not give the
    same answer.
    """
    MV.setup('x y z')
    make_symbols('a b c')
    assert 5*x == x*5
    assert HALF*x == x*HALF
    assert a*x == x*a

def test_contraction():
    """
    Test for inner product and left and right contraction
    """

    MV.setup('e_1 e_2 e_3','1 0 0, 0 1 0, 0 0 1',offset=1)

    assert ((e_1^e_3)|e_1) == -e_3
    assert ((e_1^e_3)>e_1) == -e_3
    assert (e_1|(e_1^e_3)) == e_3
    assert (e_1<(e_1^e_3)) == e_3
    assert ((e_1^e_3)<e_1) == 0
    assert (e_1>(e_1^e_3)) == 0

def test_substitution():

    MV.setup('e_x e_y e_z','1 0 0, 0 1 0, 0 0 1',offset=1)
    make_symbols('x y z')

    X = x*e_x+y*e_y+z*e_z
    Y = X.subs([(x,2),(y,3),(z,4)])
    assert Y == 2*e_x+3*e_y+4*e_z


def test_vector_extraction():
    """
    Show that conformal bivector encodes two points. See D&L Section 10.4.1
    """
    metric = ' 0 -1 #,'+ \
             '-1  0 #,'+ \
             ' #  # #,'

    MV.setup('P1 P2 a',metric)
    """
    P1 and P2 are null vectors and hence encode points in conformal space.
    Show that P1 and P2 can be extracted from the bivector B = P1^P2. a is a
    third vector in the conformal space with a.B not 0.
    """
    ZERO_MV = MV()
    B = P1^P2
    Bsq = B*B
    ap = a-(a^B)*B
    Ap = ap+ap*B
    Am = ap-ap*B