def display_steps(expr, string_msg="", is_ipython=False):
    if string_msg != "":
        pp(string_msg)
    if is_ipython:
        display(expr)
    else:
        pp(expr)
Example #2
0
def translate():
    """
    Using col3 for the translation as opposed to glm/OpenGL approach 
    of row3 for the translation is a transpose of the translation matrix, 
    which means need to transpose the point for shape consistency
    and multiply in other order.
    """

    print("P1")
    pp(P1)
    print("T")
    pp(T)
    P1_T = P1 * T
    print("P1*T")
    pp(P1_T)
    print("P1*T*T")
    pp(P1 * T * T)
    P1_T_reverse_transpose_check = (T.T * P1.T).T
    print("(T.T*P1.T).T")
    pp(P1_T_reverse_transpose_check)
    assert P1_T_reverse_transpose_check == P1_T
Example #3
0
def rotateY():
    """
    This demonstrates that HepRotation::rotateY is multiplying to the rhs::

        Rxyz*Ry

    079 HepRotation & HepRotation::rotateY(double a){
     80   double c1 = std::cos(a);
     81   double s1 = std::sin(a);
     82   double x1 = rzx, y1 = rzy, z1 = rzz;
     83   rzx = c1*x1 - s1*rxx;
     84   rzy = c1*y1 - s1*rxy;
     85   rzz = c1*z1 - s1*rxz;
     86   rxx = s1*x1 + c1*rxx;
     87   rxy = s1*y1 + c1*rxy;
     88   rxz = s1*z1 + c1*rxz;
     89   return *this;
     90 }
    """
    x1, y1, z1 = sp.symbols("x1,y1,z1")
    v_rz = [(rzx, x1), (rzy, y1), (rzz, z1)]

    Ylhs = (Ry * R).subs(v_rz)
    Yrhs = (R * Ry).subs(v_rz)

    print(rotateY.__doc__)

    print("\nR")
    pp(R)

    print("\nv_rz")
    pp(v_rz)

    print("\nYrhs = (R*Ry).subs(v_rz) ")
    pp(Yrhs)
Example #4
0
def rotateX():
    """
    This demonstrates that HepRotation::rotateX is multiplying to the rhs::

        Rxyz*Rx

    66 HepRotation & HepRotation::rotateX(double a) {    // g4-cls Rotation
    67   double c1 = std::cos(a);
    68   double s1 = std::sin(a);
    69   double x1 = ryx, y1 = ryy, z1 = ryz;
    70   ryx = c1*x1 - s1*rzx;
    71   ryy = c1*y1 - s1*rzy;
    72   ryz = c1*z1 - s1*rzz;
    73   rzx = s1*x1 + c1*rzx;
    74   rzy = s1*y1 + c1*rzy;
    75   rzz = s1*z1 + c1*rzz;
    76   return *this;
    77 }
    """
    pass

    x1, y1, z1 = sp.symbols("x1,y1,z1")
    v_ry = [(ryx, x1), (ryy, y1), (ryz, z1)]

    Xlhs = (Rx * R).subs(v_ry)
    Xrhs = (R * Rx).subs(
        v_ry)  # HepRotation::rotateX is multiplying on the rhs

    print(rotateX.__doc__)

    print("\nR")
    pp(R)

    print("\nv_ry")
    pp(v_ry)

    print("\nXrhs = (R*Rx).subs(v_ry) ")
    pp(Xrhs)
Example #5
0
def rotateZ():
    """
    This demonstrates that HepRotation::rotateZ is multiplying to the rhs::

        Rxyz*Rz


    092 HepRotation & HepRotation::rotateZ(double a) {
     93   double c1 = std::cos(a);
     94   double s1 = std::sin(a);
     95   double x1 = rxx, y1 = rxy, z1 = rxz;
     96   rxx = c1*x1 - s1*ryx;
     97   rxy = c1*y1 - s1*ryy;
     98   rxz = c1*z1 - s1*ryz;
     99   ryx = s1*x1 + c1*ryx;
    100   ryy = s1*y1 + c1*ryy;
    101   ryz = s1*z1 + c1*ryz;
    102   return *this;
    103 }
    """

    x1, y1, z1 = sp.symbols("x1,y1,z1")
    v_rx = [(rxx, x1), (rxy, y1), (rxz, z1)]

    Zlhs = (Rz * R).subs(v_rx)
    Zrhs = (R * Rz).subs(v_rx)

    print(rotateZ.__doc__)

    print("\nR")
    pp(R)

    print("\nv_rx")
    pp(v_rx)

    print("\nZrhs = (R*Rz).subs(v_rx) ")
    pp(Zrhs)
Example #6
0
 (b) $\begin{pmatrix} 
3 & 1 & -4 \\
4 & 3 & 1 \\
1 & 4 & -3 
\end{pmatrix} 
and \begin{pmatrix} 
2 & 7 & -5 \\
-2 & 1 & 0 \\
6 & 3 & 4 
\end{pmatrix}$

###**Solution/Code/Answer**
"""

# (a) Solution
pp('(a)\n\t')
A = Matrix([[2, -1], [-7, 4]])
B = Matrix([[-3, 0], [7, -4]])

C = A + B
display(C)
pp('\n\t')

# (b) Solution
pp('(b)\n\t')
D = Matrix([[3, 1, -4], [4, 3, 1], [1, 4, -3]])
E = Matrix([[2, 7, -5], [-2, 1, 0], [6, 3, 4]])
F = D + E
display(F)
"""###**Problem 2.**
Subtract
Example #7
0
from sympy import pretty_print as pp
from sympy.abc import a, b, n

expr = (a * b)**n
pp(expr)

print("===========")
a, b, n = 2, 1, 5
expr = (a * b)**n
pp(expr)
        c[j][i] = ((((rho[j])**2)*((a[i])**2))+((rho[j])*gamma*(a[i])))*((1-((rho[j])**2)-(gamma**2)-(2*(rho[j])*gamma*(a[i])))**0.5)
    
plt.plot(rho,c[:,7],'r--')
plt.show()




#SYMPY

import sympy  
from sympy import expand, symbols
from sympy import pretty_print as pp, latex
s,d,c = sympy.symbols('s d c')
delete = ((s**2)+(2*s*d*c))
pp(delete.expand(), use_unicode=True)





-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# WC2 Odd or Even
a =50
phi = np.linspace(0, 2*np.pi, 1000)
WC2 = (np.sin(phi))*((a-(np.cos(phi)))**0.5)
plt.plot(phi,WC2,'r--')
plt.show()


Example #9
0
def translate_rotate():
    """
    P1
     x  y  z  1
    TR
     rxx  ryx  rzx  0 
                      
     rxy  ryy  rzy  0 
                      
     rxz  ryz  rzz  0 
                      
     tx   ty   tz   tw 
    P*TR
     rxx x + rxy y + rxz z + tx w  ryx x + ryy y + ryz z + ty w  rzx x + rzy y + rzz z + tz w  tw w
    P*TR.subs(v_rid)
     tx w + x  ty w + y  tz w + z  tw w

    """

    print("R")
    pp(R)
    print("T")
    pp(T)
    print(
        "T*R : row3 has translation and rotation mixed up : ie translation first and then rotation which depends  "
    )
    pp(T * R)
    print(
        "R*T : familiar row3 as translation : that means rotate then translate "
    )
    pp(R * T)

    print("RT")
    pp(RT)
    assert RT == R * T

    print("P1")
    pp(P1)
    print(
        "P*RT : notice that the translation just gets added to rotated coordinates : ie rotation first and then translation"
    )
    pp(P * RT)

    P_RT = P * RT
    print("P*RT.subs(v_rid) : setting rotation to identity ")
    pp(P_RT.subs(v_rid))
Example #10
0
def G4GDMLReadStructure():
    """

    289 void G4GDMLReadStructure::
    290 PhysvolRead(const xercesc::DOMElement* const physvolElement,
    291             G4AssemblyVolume* pAssembly)
    292 {
    ...
    372    G4Transform3D transform(GetRotationMatrix(rotation).inverse(),position);
    373    transform = transform*G4Scale3D(scale.x(),scale.y(),scale.z());


    132 G4RotationMatrix
    133 G4GDMLReadDefine::GetRotationMatrix(const G4ThreeVector& angles)
    134 {
    135    G4RotationMatrix rot;
    136 
    137    rot.rotateX(angles.x());
    138    rot.rotateY(angles.y());
    139    rot.rotateZ(angles.z());
    140    rot.rectify();  // Rectify matrix from possible roundoff errors
    141 
    142    return rot;
    143 }


    g4-cls Transform3D (icc)

    029 inline
     30 Transform3D::Transform3D(const CLHEP::HepRotation & m, const CLHEP::Hep3Vector & v) {
     31   xx_= m.xx(); xy_= m.xy(); xz_= m.xz();
     32   yx_= m.yx(); yy_= m.yy(); yz_= m.yz();
     33   zx_= m.zx(); zy_= m.zy(); zz_= m.zz();
     34   dx_= v.x();  dy_= v.y();  dz_= v.z();
     35 }


    NB the order  (rotateX, rotateY, rotateZ).inverse()


    In [18]: t[3218]        # use the instance index to give the instance transform : rot matrix and tlate look familiar
    Out[18]: 
    array([[   -0.1018,     0.2466,     0.9638,     0.    ],
           [   -0.9243,    -0.3817,     0.    ,     0.    ],
           [    0.3679,    -0.8908,     0.2668,     0.    ],
           [-7148.948 , 17311.74  , -5184.257 ,     1.    ]], dtype=float32)


    In [70]: pp((Rx*Ry*Rz).transpose().subs(v_rot)*T.subs(v_pos))
     -0.101820513179743   0.24656591428434    0.963762332221457    0
     -0.924290430171623  -0.381689927419044  8.32667268468867e-17  0
     0.367858374634817   -0.890796300632177   0.266762379264878    0
         -7148.9484          17311.741            -5184.2567       1


    In [52]: (P*IT*R1).subs(v_hit+v_pos+v_rot)    ### << MATCHES <<
    Out[52]: Matrix([[-112.670723956843, 165.921754136086, 109.638786999275, 1.0]])

    In [100]: (P*IT*Rx*Ry*Rz).subs(v_hit+v_pos+v_rot)
    Out[100]: Matrix([[-112.670723956843, 165.921754136086, 109.638786999275, 1]])



    In [101]: (P*Rx*Ry*Rz*IT).subs(v_hit+v_pos+v_rot)
    Out[101]: Matrix([[7036.28119031864, -17145.8318197405, -14140.1045440872, 1]])









    (gdb) p local_pos
    $7 = {dx = -112.67072395684227, dy = 165.92175413608675, dz = 109.63878699927591, static tolerance = 2.22045e-14}
    (gdb) p trans



    ## 4th row matches the GDML 

     71423       <physvol copynumber="11336" name="pLPMT_Hamamatsu_R128600x353fc90">
     71424         <volumeref ref="HamamatsuR12860lMaskVirtual0x3290b70"/>
     71425         <position name="pLPMT_Hamamatsu_R128600x353fc90_pos" unit="mm" x="-7148.9484" y="17311.741" z="-5184.2567"/>
     71426         <rotation name="pLPMT_Hamamatsu_R128600x353fc90_rot" unit="deg" x="-73.3288783033161" y="-21.5835981926051" z="-96.2863976680901"/>
     71427       </physvol>


             { rxx = -0.10182051317974285,   rxy = -0.92429043017162327,   rxz =  0.36785837463481702, 
               ryx =  0.24656591428433955,   ryy = -0.38168992741904467,   ryz = -0.89079630063217707, 
               rzx =  0.96376233222145669,   rzy =  0,                     rzz =  0.26676237926487772, 
               tx =  -0.0035142754759363015, ty =   0.012573876562782971,  tz  =  19434.000031086449}   

             THIS IS THE INVERSE TRANSFORM 


    From examples/UseGeant4/UseGeant.cc

    dbg_affine_trans   Transformation: 
    rx/x,y,z: -0.101821 -0.92429 0.367858
    ry/x,y,z: 0.246566 -0.38169 -0.890796
    rz/x,y,z: 0.963762 0 0.266762
    tr/x,y,z: -0.00351428 0.0125739 19434


    """
    R0 = Rx * Ry * Rz
    R0T = (Rx * Ry * Rz).T

    R1 = Rz * Ry * Rx
    R1T = (Rz * Ry * Rx).T

    print(G4GDMLReadStructure.__doc__)

    if 0:
        print("\nR0 = Rx*Ry*Rz \n")
        pp(R0)
        pp(R)
        pp(R0.subs(va))
    pass

    print(
        "\nR0T = (Rx*Ry*Rz).T   THIS MATCHES THE ROTATION PART OF THE ITRANSFORM \n"
    )
    pp(R0T)
    pp(R)
    pp(R0T.subs(v_rot))

    if 0:
        print("\nR1 = Rz*Ry*Rx\n")
        pp(R1)
        pp(R)
        pp(R1.subs(va))
    pass
Example #11
0
T2 = sp.Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [tx, ty, tz, 1]])

assert T == T2, (T, T2)

IT = sp.Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [-tx, -ty, -tz, 1]])
"""
     71423       <physvol copynumber="11336" name="pLPMT_Hamamatsu_R128600x353fc90">
     71424         <volumeref ref="HamamatsuR12860lMaskVirtual0x3290b70"/>
     71425         <position name="pLPMT_Hamamatsu_R128600x353fc90_pos" unit="mm" x="-7148.9484" y="17311.741" z="-5184.2567"/>
     71426         <rotation name="pLPMT_Hamamatsu_R128600x353fc90_rot" unit="deg" x="-73.3288783033161" y="-21.5835981926051" z="-96.2863976680901"/>
     71427       </physvol>
"""

if 0:
    print("\nRx")
    pp(Rx)
    print("\nRy")
    pp(Ry)
    print("\nRz")
    pp(Rz)


def rotateX():
    """
    This demonstrates that HepRotation::rotateX is multiplying to the rhs::

        Rxyz*Rx

    66 HepRotation & HepRotation::rotateX(double a) {    // g4-cls Rotation
    67   double c1 = std::cos(a);
    68   double s1 = std::sin(a);
Example #12
0
## for Palatino and other serif fonts use:
rc('font', **{'family': 'Computer Modern Roman'})
rc('text', usetex=True)

print("\n\n---------------- 1 ----------------\n\n")
# year_of_birth will be matrix A
year_of_birth = np.array([[1, 1920], [1, 1930], [1, 1940], [1, 1950],
                          [1, 1960], [1, 1970], [1, 1980], [1, 1990]])

# life_expectancy will be matrix b
life_expectancy = np.array([[54.1], [59.7], [62.9], [68.2], [69.7], [70.8],
                            [73.7], [75.4]])

# Pretty-Printing A matrix
print("Matrix A will be: \n")
pp(sp.Matrix(year_of_birth))

# Pretty-Printing b matrix
print("\n\nMatrix b will be: \n")
pp(sp.Matrix(life_expectancy))

# Normal Equations
equation_1 = (
    A**T * A
)**T  # I'm using (A^T * A)^T to Pretty-Print in the correct format i.e. A^T*A instead of A*A^T which is de default in sp.pprint()
equation_2 = (A**T * b)
At = year_of_birth.transpose()
AtA = np.dot(At, year_of_birth)
Atb = np.dot(At, life_expectancy)

# Pretty-Printing Normal Equations
# Base of power x
n_value = make_non_zero_num(-4, 4)
while n_value == 1 or n_value == -1:
    n_value = make_non_zero_num(-4, 4)


# Constant after x
c_value = make_non_zero_num(-3, 3)
while c_value == 1 or c_value == -1:
    c_value = make_non_zero_num(-3, 3)

# Vertical shift
d_value = random.randint(-10, 10)

# Base of the function of x
n_value = random.randint(2, 3)

c = c_value
d = d_value
n = n_value

x_value = random.randint(-c_value, 4 - c_value)

solution = (n_value**(x_value + c_value)) + d_value

equation = n**(x + c) + d
print("Solve the following exponential equation for x = {}.\n\n".format(x_value))
pp(equation)
print("\nSolution: {}".format(solution))