In [1]:
import unittest

import sys

from sympy import symbols, sin, cos, Rational, expand, collect, simplify, Symbol
from printer import Format, Eprint, Get_Program
from ga import Ga, one, zero
from mv import Mv, Nga, com

def F(x):
    global n, nbar
    Fx =  ((x * x) * n + 2 * x - nbar) / 2
    return(Fx)

def make_vector(a, n=3, ga=None):
    if isinstance(a,str):
        v = zero
        for i in range(n):
            a_i = Symbol(a+str(i+1))
            v += a_i*ga.basis[i]
        v = ga.mv(v)
        return(F(v))
    else:
        return(F(a))

test_noneuclidian_distance_calculation


In [2]:
from sympy import solve,sqrt

g = '0 # #,# 0 #,# # 1'
necl = Ga('X Y e',g=g)
(X,Y,e) = necl.mv()

In [3]:
X


Out[3]:
\begin{equation*} \boldsymbol{X} \end{equation*}

In [4]:
Y


Out[4]:
\begin{equation*} \boldsymbol{Y} \end{equation*}

In [5]:
e


Out[5]:
\begin{equation*} \boldsymbol{e} \end{equation*}

In [6]:
(X^Y)*(X^Y)


Out[6]:
\begin{equation*} \left ( X\cdot Y\right ) ^{2} \end{equation*}

In [7]:
L = X^Y^e
L


Out[7]:
\begin{equation*} \boldsymbol{X}\wedge \boldsymbol{Y}\wedge \boldsymbol{e} \end{equation*}

In [8]:
B = (L*e).expand().blade_rep()
B


Out[8]:
\begin{equation*} \boldsymbol{X}\wedge \boldsymbol{Y} - \left ( Y\cdot e\right ) \boldsymbol{X}\wedge \boldsymbol{e} + \left ( X\cdot e\right ) \boldsymbol{Y}\wedge \boldsymbol{e} \end{equation*}

In [9]:
Bsq = B*B
Bsq


Out[9]:
\begin{equation*} \left ( X\cdot Y\right ) \left(\left ( X\cdot Y\right ) - 2 \left ( X\cdot e\right ) \left ( Y\cdot e\right ) \right) \end{equation*}

In [10]:
BsqScalar = Bsq.scalar()
BsqScalar


Out[10]:
(X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e)

In [11]:
(Bsq - BsqScalar).simplify() == 0


Out[11]:
True

In [12]:
BeBr = B*e*B.rev()
BeBr


Out[12]:
\begin{equation*} \left ( X\cdot Y\right ) \left(- \left ( X\cdot Y\right ) + 2 \left ( X\cdot e\right ) \left ( Y\cdot e\right ) \right) \boldsymbol{e} \end{equation*}

In [13]:
B*B


Out[13]:
\begin{equation*} \left ( X\cdot Y\right ) \left(\left ( X\cdot Y\right ) - 2 \left ( X\cdot e\right ) \left ( Y\cdot e\right ) \right) \end{equation*}

In [14]:
L*L


Out[14]:
\begin{equation*} \left ( X\cdot Y\right ) \left(\left ( X\cdot Y\right ) - 2 \left ( X\cdot e\right ) \left ( Y\cdot e\right ) \right) \end{equation*}

In [15]:
(s,c,Binv,M,S,C,alpha) = symbols('s c (1/B) M S C alpha')

In [16]:
XdotY = necl.g[0,1]
Xdote = necl.g[0,2]
Ydote = necl.g[1,2]

In [17]:
XdotY


Out[17]:
(X.Y)

In [18]:
Xdote


Out[18]:
(X.e)

In [19]:
Ydote


Out[19]:
(Y.e)

In [20]:
Bhat = Binv*B
Bhat


Out[20]:
\begin{equation*} (1/B) \boldsymbol{X}\wedge \boldsymbol{Y} - (1/B) \left ( Y\cdot e\right ) \boldsymbol{X}\wedge \boldsymbol{e} + (1/B) \left ( X\cdot e\right ) \boldsymbol{Y}\wedge \boldsymbol{e} \end{equation*}

In [21]:
R = c+s*Bhat # Rotor R = exp(alpha*Bhat/2)
R


Out[21]:
\begin{equation*} c + (1/B) s \boldsymbol{X}\wedge \boldsymbol{Y} - (1/B) \left ( Y\cdot e\right ) s \boldsymbol{X}\wedge \boldsymbol{e} + (1/B) \left ( X\cdot e\right ) s \boldsymbol{Y}\wedge \boldsymbol{e} \end{equation*}

In [22]:
Z = R*X*R.rev()
Z


Out[22]:
\begin{equation*} \left ( (1/B)^{2} \left ( X\cdot Y\right ) ^{2} s^{2} - 2 (1/B)^{2} \left ( X\cdot Y\right ) \left ( X\cdot e\right ) \left ( Y\cdot e\right ) s^{2} + 2 (1/B) \left ( X\cdot Y\right ) c s - 2 (1/B) \left ( X\cdot e\right ) \left ( Y\cdot e\right ) c s + c^{2}\right ) \boldsymbol{X} + 2 (1/B) \left ( X\cdot e\right ) ^{2} c s \boldsymbol{Y} + 2 (1/B) \left ( X\cdot Y\right ) \left ( X\cdot e\right ) s \left(- (1/B) \left ( X\cdot Y\right ) s + 2 (1/B) \left ( X\cdot e\right ) \left ( Y\cdot e\right ) s - c\right) \boldsymbol{e} \end{equation*}

In [23]:
Z.obj


Out[23]:
2*(1/B)*(X.Y)*(X.e)*s*(-(1/B)*(X.Y)*s + 2*(1/B)*(X.e)*(Y.e)*s - c)*e + 2*(1/B)*(X.e)**2*c*s*Y + ((1/B)**2*(X.Y)**2*s**2 - 2*(1/B)**2*(X.Y)*(X.e)*(Y.e)*s**2 + 2*(1/B)*(X.Y)*c*s - 2*(1/B)*(X.e)*(Y.e)*c*s + c**2)*X

In [24]:
Z.obj = expand(Z.obj)
Z.obj


Out[24]:
-2*(1/B)**2*(X.Y)**2*(X.e)*s**2*e + (1/B)**2*(X.Y)**2*s**2*X + 4*(1/B)**2*(X.Y)*(X.e)**2*(Y.e)*s**2*e - 2*(1/B)**2*(X.Y)*(X.e)*(Y.e)*s**2*X - 2*(1/B)*(X.Y)*(X.e)*c*s*e + 2*(1/B)*(X.Y)*c*s*X + 2*(1/B)*(X.e)**2*c*s*Y - 2*(1/B)*(X.e)*(Y.e)*c*s*X + c**2*X

In [25]:
Z.obj = Z.obj.collect([Binv,s,c,XdotY])
Z.obj


Out[25]:
(1/B)**2*(-2*(X.Y)**2*(X.e)*s**2*e + (X.Y)**2*s**2*X + 4*(X.Y)*(X.e)**2*(Y.e)*s**2*e - 2*(X.Y)*(X.e)*(Y.e)*s**2*X) + (1/B)*(-2*(X.Y)*(X.e)*c*s*e + 2*(X.Y)*c*s*X + 2*(X.e)**2*c*s*Y - 2*(X.e)*(Y.e)*c*s*X) + c**2*X

In [26]:
Z


Out[26]:
\begin{equation*} \left ( (1/B)^{2} \left ( X\cdot Y\right ) ^{2} s^{2} - 2 (1/B)^{2} \left ( X\cdot Y\right ) \left ( X\cdot e\right ) \left ( Y\cdot e\right ) s^{2} + 2 (1/B) \left ( X\cdot Y\right ) c s - 2 (1/B) \left ( X\cdot e\right ) \left ( Y\cdot e\right ) c s + c^{2}\right ) \boldsymbol{X} + 2 (1/B) \left ( X\cdot e\right ) ^{2} c s \boldsymbol{Y} + 2 (1/B) \left ( X\cdot Y\right ) \left ( X\cdot e\right ) s \left(- (1/B) \left ( X\cdot Y\right ) s + 2 (1/B) \left ( X\cdot e\right ) \left ( Y\cdot e\right ) s - c\right) \boldsymbol{e} \end{equation*}

In [27]:
W = Z|Y
W


Out[27]:
\begin{equation*} \left ( X\cdot Y\right ) \left((1/B)^{2} \left ( X\cdot Y\right ) ^{2} s^{2} - 4 (1/B)^{2} \left ( X\cdot Y\right ) \left ( X\cdot e\right ) \left ( Y\cdot e\right ) s^{2} + 4 (1/B)^{2} \left ( X\cdot e\right ) ^{2} \left ( Y\cdot e\right ) ^{2} s^{2} + 2 (1/B) \left ( X\cdot Y\right ) c s - 4 (1/B) \left ( X\cdot e\right ) \left ( Y\cdot e\right ) c s + c^{2}\right) \end{equation*}

In [28]:
W.scalar()


Out[28]:
(1/B)**2*(X.Y)**3*s**2 - 4*(1/B)**2*(X.Y)**2*(X.e)*(Y.e)*s**2 + 4*(1/B)**2*(X.Y)*(X.e)**2*(Y.e)**2*s**2 + 2*(1/B)*(X.Y)**2*c*s - 4*(1/B)*(X.Y)*(X.e)*(Y.e)*c*s + (X.Y)*c**2

In [29]:
W = W.scalar()
W = expand(W)
W = simplify(W)
W = W.collect([s*Binv])
W


Out[29]:
(X.Y)*((1/B)**2*s**2*((X.Y)**2 - 4*(X.Y)*(X.e)*(Y.e) + 4*(X.e)**2*(Y.e)**2) + (1/B)*s*(2*(X.Y)*c - 4*(X.e)*(Y.e)*c) + c**2)

In [30]:
Bsq = Bsq.scalar()
M = 1/Bsq
M


Out[30]:
1/((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e))

In [31]:
W = W.subs(Binv**2,M)
W


Out[31]:
(X.Y)*((1/B)*s*(2*(X.Y)*c - 4*(X.e)*(Y.e)*c) + c**2 + s**2*((X.Y)**2 - 4*(X.Y)*(X.e)*(Y.e) + 4*(X.e)**2*(Y.e)**2)/((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e)))

In [32]:
W = simplify(W)
W


Out[32]:
2*(1/B)*(X.Y)**2*c*s - 4*(1/B)*(X.Y)*(X.e)*(Y.e)*c*s + (X.Y)*c**2 + (X.Y)*s**2 - 2*(X.e)*(Y.e)*s**2

In [33]:
Bmag = sqrt(XdotY**2-2*XdotY*Xdote*Ydote)
Bmag


Out[33]:
sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e))

In [34]:
W = W.collect([Binv*c*s,XdotY])
W


Out[34]:
(1/B)*c*s*(2*(X.Y)**2 - 4*(X.Y)*(X.e)*(Y.e)) + (X.Y)*(c**2 + s**2) - 2*(X.e)*(Y.e)*s**2

In [35]:
W = W.subs(2*XdotY**2-4*XdotY*Xdote*Ydote,2/(Binv**2))
W = W.subs(2*c*s,S)
W = W.subs(c**2,(C+1)/2)
W = W.subs(s**2,(C-1)/2)
W = simplify(W)
W = W.subs(1/Binv,Bmag)
W = expand(W)
W


Out[35]:
(1/B)*(X.Y)*C*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e)) - (1/B)*(X.e)*(Y.e)*C*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e)) + (1/B)*(X.e)*(Y.e)*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e)) + S*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e))

In [36]:
# FIXME assert str(W.simplify()) == '(X.Y)*C - (X.e)*(Y.e)*C + (X.e)*(Y.e) + S*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e))'
W.simplify()


Out[36]:
sqrt((X.Y)*((X.Y) - 2*(X.e)*(Y.e)))*((1/B)*(X.Y)*C - (1/B)*(X.e)*(Y.e)*C + (1/B)*(X.e)*(Y.e) + S)

In [37]:
Wd = collect(W,[C,S],exact=True,evaluate=False)

Wd_1 = Wd[one]
Wd_C = Wd[C]
Wd_S = Wd[S]

In [38]:
Wd


Out[38]:
{S: sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e)),
 C: (1/B)*(X.Y)*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e)) - (1/B)*(X.e)*(Y.e)*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e)),
 1: (1/B)*(X.e)*(Y.e)*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e))}

In [39]:
# FIXME assert str(Wd_1) == '(X.e)*(Y.e)'
Wd_1


Out[39]:
(1/B)*(X.e)*(Y.e)*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e))

In [40]:
Wd_1.simplify()


Out[40]:
(1/B)*(X.e)*(Y.e)*sqrt((X.Y)*((X.Y) - 2*(X.e)*(Y.e)))

In [41]:
# FIXME assert str(Wd_C) == '(X.Y) - (X.e)*(Y.e)'
Wd_C


Out[41]:
(1/B)*(X.Y)*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e)) - (1/B)*(X.e)*(Y.e)*sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e))

In [42]:
Wd_C.simplify()


Out[42]:
(1/B)*sqrt((X.Y)*((X.Y) - 2*(X.e)*(Y.e)))*((X.Y) - (X.e)*(Y.e))

In [43]:
Wd_S


Out[43]:
sqrt((X.Y)**2 - 2*(X.Y)*(X.e)*(Y.e))

test_derivatives_in_spherical_coordinates


In [44]:
X = (r, th, phi) = symbols('r theta phi')
s3d = Ga('e_r e_theta e_phi', g=[1, r ** 2, r ** 2 * sin(th) ** 2], coords=X, norm=True)
(er, eth, ephi) = s3d.mv()
grad = s3d.grad

f = s3d.mv('f', 'scalar', f=True)
A = s3d.mv('A', 'vector', f=True)
B = s3d.mv('B', 'bivector', f=True)

In [45]:
r


Out[45]:
r

In [46]:
th


Out[46]:
theta

In [47]:
phi


Out[47]:
phi

In [48]:
er


Out[48]:
\begin{equation*} \boldsymbol{e}_{r} \end{equation*}

In [49]:
eth


Out[49]:
\begin{equation*} \boldsymbol{e}_{\theta } \end{equation*}

In [50]:
ephi


Out[50]:
\begin{equation*} \boldsymbol{e}_{\phi } \end{equation*}

In [51]:
grad


Out[51]:
\begin{equation*} e_r D{r} + e_theta 1/r*D{theta} + e_phi 1/(r*sin(theta))*D{phi} \end{equation*}

In [52]:
f


Out[52]:
\begin{equation*} f = f {\left (r,\theta ,\phi \right )} \end{equation*}

In [53]:
A


Out[53]:
\begin{equation*} A = A^{r} {\left (r,\theta ,\phi \right )} \boldsymbol{e}_{r} + A^{\theta } {\left (r,\theta ,\phi \right )} \boldsymbol{e}_{\theta } + A^{\phi } {\left (r,\theta ,\phi \right )} \boldsymbol{e}_{\phi } \end{equation*}

In [54]:
B


Out[54]:
\begin{equation*} B = B^{r\theta } {\left (r,\theta ,\phi \right )} \boldsymbol{e}_{r}\wedge \boldsymbol{e}_{\theta } + B^{r\phi } {\left (r,\theta ,\phi \right )} \boldsymbol{e}_{r}\wedge \boldsymbol{e}_{\phi } + B^{\phi \phi } {\left (r,\theta ,\phi \right )} \boldsymbol{e}_{\theta }\wedge \boldsymbol{e}_{\phi } \end{equation*}

In [55]:
grad*f


Out[55]:
\begin{equation*} \frac{\partial}{\partial r} f {\left (r,\theta ,\phi \right )} \boldsymbol{e}_{r} + \frac{1}{r} \frac{\partial}{\partial \theta } f {\left (r,\theta ,\phi \right )} \boldsymbol{e}_{\theta } + \frac{\frac{\partial}{\partial \phi } f {\left (r,\theta ,\phi \right )}}{r \sin{\left (\theta \right )}} \boldsymbol{e}_{\phi } \end{equation*}

In [56]:
(grad|A).simplify()


Out[56]:
\begin{equation*} \frac{1}{r} \left(r \frac{\partial}{\partial r} A^{r} {\left (r,\theta ,\phi \right )} + 2 A^{r} {\left (r,\theta ,\phi \right )} + \frac{A^{\theta } {\left (r,\theta ,\phi \right )}}{\tan{\left (\theta \right )}} + \frac{\partial}{\partial \theta } A^{\theta } {\left (r,\theta ,\phi \right )} + \frac{1}{\sin{\left (\theta \right )}} \frac{\partial}{\partial \phi } A^{\phi } {\left (r,\theta ,\phi \right )}\right) \end{equation*}

In [57]:
-s3d.I()*(grad^A)


Out[57]:
\begin{equation*} \frac{1}{r} \left(\frac{A^{\phi } {\left (r,\theta ,\phi \right )}}{\tan{\left (\theta \right )}} + \frac{\partial}{\partial \theta } A^{\phi } {\left (r,\theta ,\phi \right )} - \frac{1}{\sin{\left (\theta \right )}} \frac{\partial}{\partial \phi } A^{\theta } {\left (r,\theta ,\phi \right )}\right) \boldsymbol{e}_{r} + \frac{1}{r} \left(- r \frac{\partial}{\partial r} A^{\phi } {\left (r,\theta ,\phi \right )} - A^{\phi } {\left (r,\theta ,\phi \right )} + \frac{1}{\sin{\left (\theta \right )}} \frac{\partial}{\partial \phi } A^{r} {\left (r,\theta ,\phi \right )}\right) \boldsymbol{e}_{\theta } + \frac{1}{r} \left(r \frac{\partial}{\partial r} A^{\theta } {\left (r,\theta ,\phi \right )} + A^{\theta } {\left (r,\theta ,\phi \right )} - \frac{\partial}{\partial \theta } A^{r} {\left (r,\theta ,\phi \right )}\right) \boldsymbol{e}_{\phi } \end{equation*}

In [58]:
B|(eth^ephi)


Out[58]:
\begin{equation*} - B^{\phi \phi } {\left (r,\theta ,\phi \right )} \end{equation*}

In [59]:
btp = B|(eth^ephi)

In [60]:
str(btp)


Out[60]:
'-B__thetaphi'

In [62]:
from printer import print_latex
print_latex(btp)


- B^{\phi \phi } {\left (r,\theta ,\phi  \right )}

In [63]:
import printer
def latex(expr, **settings):
    return GaLatexPrinter(settings).doprint(expr)


def print_latex(expr, **settings):
    """Prints LaTeX representation of the given expression."""
    print latex(expr, **settings)

In [81]:
gp = printer.GaLatexPrinter({})

In [82]:
gp.doprint(btp)


Out[82]:
'- B^{\\phi \\phi } {\\left (r,\\theta ,\\phi  \\right )}'

In [83]:
printer.GaLatexPrinter.split_super_sub(str(btp))


Out[83]:
('*', ['-B'], [['thetaphi']], [[]])

In [84]:
gp._print_Mv(btp)


Out[84]:
'- B^{\\phi \\phi } {\\left (r,\\theta ,\\phi  \\right )}'

In [72]:
btp.Mv_latex_str()


Out[72]:
'- B^{\\phi \\phi } {\\left (r,\\theta ,\\phi  \\right )}'

In [80]:
btp.components()


Out[80]:
[-B__thetaphi]

In [87]:
str(btp.components()[0])


Out[87]:
'-B__thetaphi'

In [90]:
btp.Ga.blades_lst


Out[90]:
[e_r, e_theta, e_phi, e_r^e_theta, e_r^e_phi, e_theta^e_phi, e_r^e_theta^e_phi]

In [91]:
btp.Ga.blades_to_grades_dict


Out[91]:
{e_r^e_theta^e_phi: 3,
 e_r^e_phi: 2,
 e_r: 1,
 e_r^e_theta: 2,
 e_theta: 1,
 e_theta^e_phi: 2,
 e_phi: 1}

In [93]:
printer.ostr(btp)


Out[93]:
'-B__thetaphi'

In [108]:
btp.Mv_str()


Out[108]:
'-B__thetaphi'

In [111]:
str(btp.base_rep())


Out[111]:
'-B__thetaphi'

In [112]:
btp.blade_coefs()


Out[112]:
[-B__thetaphi(r, theta, phi), 0, 0, 0, 0, 0, 0, 0]

In [113]:
btp.blade_coefs()[0]


Out[113]:
-B__thetaphi(r, theta, phi)

In [114]:
gp.latex(btp.blade_coefs()[0])


Out[114]:
'- B^{\\phi \\phi } {\\left (r,\\theta ,\\phi  \\right )}'

In [120]:
str(btp.blade_rep())


Out[120]:
'-B__thetaphi'

In [125]:
btp.coords


Out[125]:
[r, theta, phi]

In [126]:
btp.dual_mode_lst


Out[126]:
['+I', 'I+', '+Iinv', 'Iinv+', '-I', 'I-', '-Iinv', 'Iinv-']

In [136]:
btp.grades


Out[136]:
[0]

In [143]:
(mode, name_lst, supers_lst, subs_lst) = printer.GaLatexPrinter.split_super_sub(str(btp))

In [144]:
mode


Out[144]:
'*'

In [145]:
name_lst


Out[145]:
['-B']

In [146]:
supers_lst


Out[146]:
[['thetaphi']]

In [153]:
GaLatexPrinter = printer.GaLatexPrinter
def translate(s):
    tmp = s

    parse_dict = {}
    i_sub = 1

    for glyph in GaLatexPrinter.special_alphabet:
        if glyph in tmp:
            parse_sym = '????' + str(i_sub)
            parse_dict[parse_sym] = '\\' + glyph + ' '
            tmp = tmp.replace(glyph, parse_sym)
            print tmp

    for parse_sym in parse_dict:
        tmp = tmp.replace(parse_sym, parse_dict[parse_sym])

    for glyph in GaLatexPrinter.greek_translated:
        if glyph in tmp:
            tmp = tmp.replace(glyph, GaLatexPrinter.greek_translated[glyph])

    return tmp

In [154]:
translate('thetaphi')


????1phi
????1????1
Out[154]:
'\\phi \\phi '

In [157]:
def translate_corrected(s):
    tmp = s

    parse_dict = {}
    i_sub = 1

    for glyph in printer.GaLatexPrinter.special_alphabet:
        if glyph in tmp:
            parse_sym = '????' + str(i_sub)
            i_sub += 1
            parse_dict[parse_sym] = '\\' + glyph + ' '
            tmp = tmp.replace(glyph, parse_sym)
            print tmp

    for parse_sym in parse_dict:
        tmp = tmp.replace(parse_sym, parse_dict[parse_sym])

    for glyph in GaLatexPrinter.greek_translated:
        if glyph in tmp:
            tmp = tmp.replace(glyph, GaLatexPrinter.greek_translated[glyph])

    return tmp

In [158]:
translate_corrected('thetaphi')


????1phi
????1????2
Out[158]:
'\\theta \\phi '

In [159]:
grad


Out[159]:
\begin{equation*} e_r D{r} + e_theta 1/r*D{theta} + e_phi 1/(r*sin(theta))*D{phi} \end{equation*}

In [160]:
str(grad)


Out[160]:
'e_r*D{r} + e_theta*1/r*D{theta} + e_phi*1/(r*sin(theta))*D{phi}'

In [161]:
printer.GaLatexPrinter.split_super_sub(str(grad))


Out[161]:
('*',
 ['e', 'D{r} + e', '1/r', 'D{theta} + e', '1/(r', 'sin(theta))', 'D{phi}'],
 [[], [], [], [], [], [], []],
 [['r'], ['theta'], [], ['phi'], [], [], []])

In [166]:
translate_corrected('e_theta*1/r*D{theta}')


e_????1*1/r*D{????1}
Out[166]:
'e_\\theta *1/r*D{\\theta }'

In [167]:
translate('e_theta*1/r*D{theta}')


e_????1*1/r*D{????1}
Out[167]:
'e_\\theta *1/r*D{\\theta }'

In [194]:
print translate(str(grad))


e_r*D{r} + e_????1*1/r*D{????1} + e_phi*1/(r*sin(????1))*D{phi}
e_r*D{r} + e_????1*1/r*D{????1} + e_????1*1/(r*sin(????1))*D{????1}
e_r*D{r} + e_\phi *1/r*D{\phi } + e_\phi *1/(r*sin(\phi ))*D{\phi }
$$ e_r*D{r} + e_\phi *1/r*D{\phi } + e_\phi *1/(r*sin(\phi ))*D{\phi } $$

In [201]:
cpns = (gr, gt, gph) = grad.components()
cpns


Out[201]:
(e_r*D{r}, e_theta*1/r*D{theta}, e_phi*1/(r*sin(theta))*D{phi})

In [206]:
gp._print_Dop(gt)


Out[206]:
'e_theta 1/r*D{theta}'

In [207]:
gt.Dop_latex_str()


Out[207]:
'e_theta 1/r*D{theta}'

In [211]:
gt.terms


Out[211]:
[(e_theta/r, D{theta})]

In [217]:
def Dop_latex_str(self):
    if len(self.terms) == 0:
        return ' 0 '

    self.consolidate_coefs()

    mv_terms = self.Dop_mv_expand(modes=simplify)
    s = ''

    for (sdop, base) in mv_terms:
        str_sdop = str(sdop)
        if base == S(1):
            s += str_sdop
        else:
            if str_sdop == '1':
                s += str(base)
            if str_sdop == '-1':
                s += '-' + str(base)
                if str_sdop[1:] != '1':
                    s += ' ' + str_sdop[1:]
            else:
                if len(sdop.terms) > 1:
                    if self.cmpflg:
                        s += r'\left ( ' + str_sdop + r'\right ) ' + str(base)
                    else:
                        s += str(base) + ' ' + r'\left ( ' + str_sdop + r'\right ) '
                else:
                    if str_sdop[0] == '-' and not isinstance(sdop.terms[0][0], Add):
                        if self.cmpflg:
                            s += str_sdop + str(base)
                        else:
                            s += '-' + str(base) + ' ' + str_sdop[1:]
                    else:
                        if self.cmpflg:
                            s += str_sdop + ' ' + str(base)
                        else:
                            s += str(base) + ' ' + str_sdop
        s += ' + '

    s = s.replace('+ -','-')
    # Sdop.str_mode = False
    return s[:-3]

In [218]:
Dop_latex_str(gt)


Out[218]:
'e_theta 1/r*D{theta}'

In [220]:
terms = gt.Dop_mv_expand(modes=simplify)
terms


Out[220]:
[(1/r*D{theta}, e_theta)]

In [231]:
def Dop_latex_str_corrected(self):
    if len(self.terms) == 0:
        return ' 0 '

    self.consolidate_coefs()

    mv_terms = self.Dop_mv_expand(modes=simplify)
    s = ''

    for (sdop, base) in mv_terms:
        str_base = printer.latex(base)
        str_sdop = printer.latex(sdop)
        
        print str_base
        print str_sdop
        
        if base == S(1):
            s += str_sdop
        else:
            if str_sdop == '1':
                s += str_base
            if str_sdop == '-1':
                s += '-' + str_base
                if str_sdop[1:] != '1':
                    s += ' ' + str_sdop[1:]
            else:
                if len(sdop.terms) > 1:
                    if self.cmpflg:
                        s += r'\left ( ' + str_sdop + r'\right ) ' + str_base
                    else:
                        s += str_base + ' ' + r'\left ( ' + str_sdop + r'\right ) '
                else:
                    if str_sdop[0] == '-' and not isinstance(sdop.terms[0][0], Add):
                        if self.cmpflg:
                            s += str_sdop + str_base
                        else:
                            s += '-' + str_base + ' ' + str_sdop[1:]
                    else:
                        if self.cmpflg:
                            s += str_sdop + ' ' + str_base
                        else:
                            s += str_base + ' ' + str_sdop
        s += ' + '

    s = s.replace('+ -','-')
    # Sdop.str_mode = False
    return s[:-3]

In [232]:
print Dop_latex_str_corrected(gt)


\boldsymbol{e}_{\theta }
1/r D{theta}
\boldsymbol{e}_{\theta } 1/r D{theta}
$$ \boldsymbol{e}_{\theta } 1/r D{theta} $$

In [233]:
print translate_corrected(str(grad))


e_r*D{r} + e_????1*1/r*D{????1} + e_phi*1/(r*sin(????1))*D{phi}
e_r*D{r} + e_????1*1/r*D{????1} + e_????2*1/(r*sin(????1))*D{????2}
e_r*D{r} + e_\theta *1/r*D{\theta } + e_\phi *1/(r*sin(\theta ))*D{\phi }
$$ e_r*D{r} + e_\theta *1/r*D{\theta } + e_\phi *1/(r*sin(\theta ))*D{\phi } $$

In [229]:
(gt0, gt1) = terms[0]
gt0


Out[229]:
1/r D{theta}

In [234]:
print translate_corrected(str(gt0))


1/r*D{????1}
1/r*D{\theta }

In [236]:
gp._print_Sdop(gt0)


Out[236]:
'1/r D{theta}'

$ \boldsymbol{e}_{r} D{r} + \boldsymbol{e}_{\theta } 1/r D{theta} + \boldsymbol{e}_{\phi } 1/(r*sin(theta)) D{phi} $

$ \boldsymbol{e}_{r} D{r} + \boldsymbol{e}_{\theta } 1/r D{\theta} + \boldsymbol{e}_{\phi } 1/(r*sin(\theta)) D{\phi} $

$ \boldsymbol{e}_{r} \frac{\partial}{\partial r} + \boldsymbol{e}_{\theta } \frac{1}{r} \frac{\partial}{\partial theta} + \boldsymbol{e}_{\phi } \frac{1}{r \sin{\left (\theta \right )}} \frac{\partial}{\partial phi} $

$ \boldsymbol{e}_{r} \frac{\partial}{\partial r} + \boldsymbol{e}_{\theta } \frac{1}{r} \frac{\partial}{\partial \theta } + \boldsymbol{e}_{\phi } \frac{1}{r \sin{\left (\theta \right )}} \frac{\partial}{\partial \phi } $

$ - B^{\theta \phi } {\left (r,\theta ,\phi \right )} $


In [ ]: