Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:50

0001 #!/usr/bin/env python
0002 """
0003 symtran.py
0004 ============
0005 
0006 
0007 In [22]: T                                                                                                                                                                                                
0008 Out[22]: 
0009 Matrix([
0010 [ 1,  0,  0, 0],
0011 [ 0,  1,  0, 0],
0012 [ 0,  0,  1, 0],
0013 [tx, ty, tz, 1]])
0014 
0015 In [23]: R                                                                                                                                                                                                
0016 Out[23]: 
0017 Matrix([
0018 [sin(theta)*cos(phi), sin(phi)*sin(theta),  cos(theta), 0],
0019 [cos(phi)*cos(theta), sin(phi)*cos(theta), -sin(theta), 0],
0020 [          -sin(phi),            cos(phi),           0, 0],
0021 [                  0,                   0,           0, 1]])
0022 
0023 In [24]: T*R                                                                                                                                                                                              
0024 Out[24]: 
0025 Matrix([
0026 [                                          sin(theta)*cos(phi),                                           sin(phi)*sin(theta),                    cos(theta), 0],
0027 [                                          cos(phi)*cos(theta),                                           sin(phi)*cos(theta),                   -sin(theta), 0],
0028 [                                                    -sin(phi),                                                      cos(phi),                             0, 0],
0029 [tx*sin(theta)*cos(phi) + ty*cos(phi)*cos(theta) - tz*sin(phi), tx*sin(phi)*sin(theta) + ty*sin(phi)*cos(theta) + tz*cos(phi), tx*cos(theta) - ty*sin(theta), 1]])
0030 
0031 In [25]: R*T                                                                                                                                                                                              
0032 Out[25]: 
0033 Matrix([
0034 [sin(theta)*cos(phi), sin(phi)*sin(theta),  cos(theta), 0],
0035 [cos(phi)*cos(theta), sin(phi)*cos(theta), -sin(theta), 0],
0036 [          -sin(phi),            cos(phi),           0, 0],
0037 [                 tx,                  ty,          tz, 1]])
0038 
0039 """
0040 from collections import OrderedDict as odict 
0041 import numpy as np
0042 import sympy as sp
0043 
0044 np_ = lambda _:np.array(_).astype(np.float64)
0045 
0046 
0047 
0048 def pp(q, q_label="?", note=""):
0049 
0050     if type(q) is np.ndarray:
0051         q_type = "np"
0052     elif q.__class__.__name__  == 'MutableDenseMatrix':
0053         q_type = "sp.MutableDenseMatrix"
0054     else:
0055         q_type = "?"
0056     pass
0057     print("\n%s : %s : %s : %s \n" % (q_label, q_type, str(q.shape), note) )
0058 
0059     if q_type.startswith("sp"):
0060         sp.pprint(q)
0061     elif q_type.startswith("np"):
0062         print(q)
0063     else:
0064         print(q)
0065     pass
0066 
0067 
0068 
0069 radius, theta, phi = sp.symbols("radius theta phi")                                                                                                                                               
0070 tx, ty, tz = sp.symbols("tx ty tz")
0071 x, y, z, w = sp.symbols("x y z w")
0072 
0073 R = sp.Matrix([
0074        [ sp.sin(theta)*sp.cos(phi) , sp.sin(theta)*sp.sin(phi) , sp.cos(theta)  ,  0 ],
0075        [ sp.cos(theta)*sp.cos(phi) , sp.cos(theta)*sp.sin(phi) , -sp.sin(theta) ,  0 ],
0076        [ -sp.sin(phi)              , sp.cos(phi)               , 0              ,  0 ],
0077        [ 0                         , 0                         , 0              ,  1 ]
0078        ])
0079 
0080 
0081 T = sp.Matrix([ [1,0,0,0], [0,1,0,0], [0,0,1,0], [tx, ty, tz, 1] ])
0082 
0083 V = sp.Matrix([[x, y, z, w]] )        # row vector (same as with single bracket) 
0084 
0085 P = sp.Matrix([[x], [y], [z], [w] ])  # column vector
0086 
0087 
0088 pp(R,"R")
0089 pp(T,"T")
0090 pp(V,"V")
0091 pp(P,"P")
0092 pp(V*T,"V*T")
0093 pp(T.T*P, "T.T*P")
0094 pp(T*R, "T*R")  
0095 pp(R*T,"R*T", "simple tx ty yz 1 last row" )  
0096 
0097 
0098 
0099 t = 0.25
0100 p = 0.0
0101 loc = odict()
0102 loc["midlat"] = [(theta,t*np.pi),(phi,p*np.pi)] 
0103 
0104 pos = odict()
0105 pos["origin"] = [(x,0), (y,0), (z,0), (w,1)] 
0106 
0107 tlate = [(tx, 100), (ty, 200), (tz, 300) ]
0108 
0109 midlat = loc["midlat"]
0110 origin = pos["origin"]
0111 
0112 
0113 pp(R.subs(midlat), "R.subs(midlat)")
0114 pp(T.subs(tlate),  "T.subs(tlate)")
0115 pp(P.subs(origin), "P.subs(origin)")
0116 pp(V.subs(origin), "V.subs(origin)")
0117 
0118 pp(T.subs(tlate) * P.subs(origin), "T.subs(tlate) * P.subs(origin)", "no translation like this" )  
0119 pp(T.subs(tlate).T * P.subs(origin), "T.subs(tlate).T * P.subs(origin)", "have to transpose the T to get the translation" )  
0120 
0121 pp(V.subs(origin) * T.subs(tlate) , "V.subs(origin) * T.subs(tlate)", "when using row vector V have to mutltiply to the right with the un-transposed T to get translation " )
0122 
0123 
0124 nV = np_(V.subs(origin))
0125 nT = np_(T.subs(tlate))
0126 
0127 pp(nV, "nV = np_(V.subs(origin)) ")
0128 pp(nV.T, "nV.T ")
0129 
0130 pp(nT, "nT = np_(T.subs(tlate)) ")
0131 
0132 pp( np.dot(nV, nT), "np.dot(nV, nT)" )
0133 
0134 pp( np.dot(nT, nV.T), "np.dot(nT, nV.T)" )
0135 
0136