Пример #1
0
def test_conv7():
    x = Symbol("x")
    y = Symbol("y")
    assert sin(x/3) == sin(sympy.Symbol("x") / 3)
    assert cos(x/3) == cos(sympy.Symbol("x") / 3)
    assert tan(x/3) == tan(sympy.Symbol("x") / 3)
    assert cot(x/3) == cot(sympy.Symbol("x") / 3)
    assert csc(x/3) == csc(sympy.Symbol("x") / 3)
    assert sec(x/3) == sec(sympy.Symbol("x") / 3)
    assert asin(x/3) == asin(sympy.Symbol("x") / 3)
    assert acos(x/3) == acos(sympy.Symbol("x") / 3)
    assert atan(x/3) == atan(sympy.Symbol("x") / 3)
    assert acot(x/3) == acot(sympy.Symbol("x") / 3)
    assert acsc(x/3) == acsc(sympy.Symbol("x") / 3)
    assert asec(x/3) == asec(sympy.Symbol("x") / 3)

    assert sin(x/3)._sympy_() == sympy.sin(sympy.Symbol("x") / 3)
    assert sin(x/3)._sympy_() != sympy.cos(sympy.Symbol("x") / 3)
    assert cos(x/3)._sympy_() == sympy.cos(sympy.Symbol("x") / 3)
    assert tan(x/3)._sympy_() == sympy.tan(sympy.Symbol("x") / 3)
    assert cot(x/3)._sympy_() == sympy.cot(sympy.Symbol("x") / 3)
    assert csc(x/3)._sympy_() == sympy.csc(sympy.Symbol("x") / 3)
    assert sec(x/3)._sympy_() == sympy.sec(sympy.Symbol("x") / 3)
    assert asin(x/3)._sympy_() == sympy.asin(sympy.Symbol("x") / 3)
    assert acos(x/3)._sympy_() == sympy.acos(sympy.Symbol("x") / 3)
    assert atan(x/3)._sympy_() == sympy.atan(sympy.Symbol("x") / 3)
    assert acot(x/3)._sympy_() == sympy.acot(sympy.Symbol("x") / 3)
    assert acsc(x/3)._sympy_() == sympy.acsc(sympy.Symbol("x") / 3)
    assert asec(x/3)._sympy_() == sympy.asec(sympy.Symbol("x") / 3)
Пример #2
0
def test_conv7():
    x = Symbol("x")
    y = Symbol("y")
    assert sin(x / 3) == sin(sympy.Symbol("x") / 3)
    assert cos(x / 3) == cos(sympy.Symbol("x") / 3)
    assert tan(x / 3) == tan(sympy.Symbol("x") / 3)
    assert cot(x / 3) == cot(sympy.Symbol("x") / 3)
    assert csc(x / 3) == csc(sympy.Symbol("x") / 3)
    assert sec(x / 3) == sec(sympy.Symbol("x") / 3)
    assert asin(x / 3) == asin(sympy.Symbol("x") / 3)
    assert acos(x / 3) == acos(sympy.Symbol("x") / 3)
    assert atan(x / 3) == atan(sympy.Symbol("x") / 3)
    assert acot(x / 3) == acot(sympy.Symbol("x") / 3)
    assert acsc(x / 3) == acsc(sympy.Symbol("x") / 3)
    assert asec(x / 3) == asec(sympy.Symbol("x") / 3)

    assert sin(x / 3)._sympy_() == sympy.sin(sympy.Symbol("x") / 3)
    assert sin(x / 3)._sympy_() != sympy.cos(sympy.Symbol("x") / 3)
    assert cos(x / 3)._sympy_() == sympy.cos(sympy.Symbol("x") / 3)
    assert tan(x / 3)._sympy_() == sympy.tan(sympy.Symbol("x") / 3)
    assert cot(x / 3)._sympy_() == sympy.cot(sympy.Symbol("x") / 3)
    assert csc(x / 3)._sympy_() == sympy.csc(sympy.Symbol("x") / 3)
    assert sec(x / 3)._sympy_() == sympy.sec(sympy.Symbol("x") / 3)
    assert asin(x / 3)._sympy_() == sympy.asin(sympy.Symbol("x") / 3)
    assert acos(x / 3)._sympy_() == sympy.acos(sympy.Symbol("x") / 3)
    assert atan(x / 3)._sympy_() == sympy.atan(sympy.Symbol("x") / 3)
    assert acot(x / 3)._sympy_() == sympy.acot(sympy.Symbol("x") / 3)
    assert acsc(x / 3)._sympy_() == sympy.acsc(sympy.Symbol("x") / 3)
    assert asec(x / 3)._sympy_() == sympy.asec(sympy.Symbol("x") / 3)
Пример #3
0
def quaternion_from_rpy(roll, pitch, yaw):
    """
    :type roll: Union[float, Symbol]
    :type pitch: Union[float, Symbol]
    :type yaw: Union[float, Symbol]
    :return: 4x1 Matrix
    :type: Matrix
    """
    roll_half = roll / 2.0
    pitch_half = pitch / 2.0
    yaw_half = yaw / 2.0

    c_roll = se.cos(roll_half)
    s_roll = se.sin(roll_half)
    c_pitch = se.cos(pitch_half)
    s_pitch = se.sin(pitch_half)
    c_yaw = se.cos(yaw_half)
    s_yaw = se.sin(yaw_half)

    cc = c_roll * c_yaw
    cs = c_roll * s_yaw
    sc = s_roll * c_yaw
    ss = s_roll * s_yaw

    x = c_pitch * sc - s_pitch * cs
    y = c_pitch * ss + s_pitch * cc
    z = c_pitch * cs - s_pitch * sc
    w = c_pitch * cc + s_pitch * ss

    return se.Matrix([x, y, z, w])
Пример #4
0
def qubit_op_to_expr(qubit_op, angle_folds=0):
    qubit_op.compress()
    dict_op = qubit_op.terms

    expr = 0
    for key in dict_op:
        term = dict_op[key]

        for var in key:
            num, char = var

            if char == 'X':
                term *= se.cos(se.Symbol('phi' + str(num))) * se.sin(
                    se.Symbol('the' + str(num)))
                if angle_folds == 3:
                    term *= se.Symbol('W' + str(num))
            if char == 'Y':
                term *= se.sin(se.Symbol('phi' + str(num))) * se.sin(
                    se.Symbol('the' + str(num)))
                if angle_folds > 1:
                    term *= se.Symbol('Q' + str(num))
            if char == 'Z':
                term *= se.cos(se.Symbol('the' + str(num)))
                if angle_folds > 0:
                    term *= se.Symbol('Z' + str(num))
        expr += term
    return expr
Пример #5
0
def cos(expr):
    """Cosine"""
    if type(expr) == GC:
        return GC(
            se.cos(expr.expr),
            {s: -se.sin(expr.expr) * d
             for s, d in expr.gradients.items()})
    return se.cos(expr)
Пример #6
0
def test_sin():
    x = Symbol("x")
    e = sin(x)
    assert e == sin(x)
    assert e != cos(x)

    assert sin(x).diff(x) == cos(x)
    assert cos(x).diff(x) == -sin(x)

    e = sqrt(x).diff(x).diff(x)
    f = sin(e)
    g = f.diff(x).diff(x)
    assert isinstance(g, Add)
Пример #7
0
def test_sin():
    x = Symbol("x")
    e = sin(x)
    assert e == sin(x)
    assert e != cos(x)

    assert sin(x).diff(x) == cos(x)
    assert cos(x).diff(x) == -sin(x)

    e = sqrt(x).diff(x).diff(x)
    f = sin(e)
    g = f.diff(x).diff(x)
    assert isinstance(g, Add)
Пример #8
0
 def test_find_dependent_helpers(self):
     helpers = [
         (q, p),
         (r, sin(q)),
         (s, 3 * p + r),
         (u, Integer(42)),
     ]
     control = [
         (q, 1),
         (r, cos(q)),
         (s, 3 + cos(q)),
     ]
     dependent_helpers = find_dependent_helpers(helpers, p)
     # This check is overly restrictive due to depending on the order and exact form of the result:
     self.assertListEqual(dependent_helpers, control)
    def test_integralKernelMinus(self):
        n = 10
        m = 10
        listn = np.zeros(n, dtype=object)
        vals = np.zeros((n, m), dtype=object)
        for i in range(n):
            Plus0 = np.array(CFS_Action.integralKernelMinus(i + 1)).reshape(
                2, 2)
            print(Plus0)
            #Plus1 = np.array(CFS_Action.integralKernelMinus(i +1), dtype = object).reshape(2,2)
            listn[i] = si.lambdify(r[0], [
                sy.simplify(-2 * si.cos(r[0] / 2) * CFS_Action.prefactor(i) *
                            JacPol(si.Rational(1, 2), si.Rational(3, 2), i) +
                            Plus0[0, 0] + Plus0[1, 1])
            ],
                                   real=False)
            #print( sy.simplify(JacPol(1/2,3/2, i)),'=?=', sy.jacobi(i, 1/2,3/2, r[0]))
            #listn[i] =si.lambdify(r[0], [sy.simplify(JacPol(1/2,3/2, i)) - sy.jacobi(i, 1/2,3/2, r[0])])

        for i in range(n):
            for j in range(m):
                vals[i, j] = listn[i](j)

        print(vals)
        np.testing.assert_almost_equal(vals, np.zeros((n, m)), 10)
Пример #10
0
def reproject_axis_gen2(X, Y, Z, axis, cal):
    #(phase_cal, tilt_cal, curve_cal, gibPhase_cal, gibMag_cal, ogeePhase_cal, ogeeMag_cal) = cal

    B = atan2(Z, X)

    Ydeg = cal.tilt + (-1 if axis else 1) * math.pi / 6.
    tanA = tan(Ydeg)
    normXZ = sqrt(X * X + Z * Z)

    asinArg = tanA * Y / normXZ

    sinYdeg = sin(Ydeg)
    cosYdeg = cos(Ydeg)

    sinPart = sin(B - asin(asinArg) + cal.ogeephase) * cal.ogeemag

    normXYZ = sqrt(X * X + Y * Y + Z * Z)

    modAsinArg = Y / normXYZ / cosYdeg

    asinOut = asin(modAsinArg)

    mod, acc = calc_cal_series(asinOut)

    BcalCurved = sinPart + cal.curve
    asinArg2 = asinArg + mod * BcalCurved / (cosYdeg -
                                             acc * BcalCurved * sinYdeg)

    asinOut2 = asin(asinArg2)
    sinOut2 = sin(B - asinOut2 + cal.gibpha)

    return B - asinOut2 + sinOut2 * cal.gibmag - cal.phase - math.pi / 2.
Пример #11
0
    def integralKernelMinus(self, n):
        n = n - 1
        lala1 = sy.jacobi(n, si.Rational(1, 2), si.Rational(3, 2), r[0])
        lala2 = sy.jacobi(n, si.Rational(3, 2), si.Rational(1, 2), r[0])

        return self.prefactor(n) * (si.cos(r[0] / 2) * lala1 * ident +
                                    I * si.sin(r[0] / 2) * lala2 * sigma3)
    def test_integralKernelPlus(self):
        '''
        The idea in this test is to use the fact, that i know that if i 
        add the diagonal terms, the imaginary parts cancel each other out and
        the real terms add up. That's the idea for this and the following 3 tests. 
        Also this test is the most updated version. Here i do not use lambdify, 
        because of simplify, the terms really cancel to zero. Befor that i didn't 
        use simplify, so i had to use lambdify. I'm keeping the other tests the 
        way they are, so that i just can check how i did this. There were some 
        problems, i had to figure out and in future i maybe need to look it up.:) 
        '''
        n = 10
        m = 10
        listn = np.zeros(n, dtype=object)
        vals = np.zeros((n, m), dtype=object)
        for i in range(n):
            Plus0 = np.array(CFS_Action.integralKernelPlus(
                i + 1))  #, dtype = object).reshape(2,2)
            print(Plus0)
            listn[i] = sy.simplify(
                -2 * si.cos(r[0] / 2) * CFS_Action.prefactor(i) *
                JacPol(si.Rational(1, 2), si.Rational(3, 2), i) + Plus0[0, 0] +
                Plus0[1, 1])
        for i in range(n):
            vals[i] = listn[i]

        print(vals)
        np.testing.assert_array_equal(listn, np.zeros(n), 10)
Пример #13
0
def rotation_matrix_from_axis_angle(axis, angle):
    """
    Conversion of unit axis and angle to 4x4 rotation matrix according to:
    http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm
    :param axis: 3x1 Matrix
    :type axis: Matrix
    :type angle: Union[float, Symbol]
    :return: 4x4 Matrix
    :rtype: Matrix
    """
    ct = se.cos(angle)
    st = se.sin(angle)
    vt = 1 - ct
    m_vt_0 = vt * axis[0]
    m_vt_1 = vt * axis[1]
    m_vt_2 = vt * axis[2]
    m_st_0 = axis[0] * st
    m_st_1 = axis[1] * st
    m_st_2 = axis[2] * st
    m_vt_0_1 = m_vt_0 * axis[1]
    m_vt_0_2 = m_vt_0 * axis[2]
    m_vt_1_2 = m_vt_1 * axis[2]
    return se.Matrix(
        [[ct + m_vt_0 * axis[0], -m_st_2 + m_vt_0_1, m_st_1 + m_vt_0_2, 0],
         [m_st_2 + m_vt_0_1, ct + m_vt_1 * axis[1], -m_st_0 + m_vt_1_2, 0],
         [-m_st_1 + m_vt_0_2, m_st_0 + m_vt_1_2, ct + m_vt_2 * axis[2], 0],
         [0, 0, 0, 1]])
Пример #14
0
def axisangle2quat(axis_angle):
    qi, qj, qk = axis_angle
    mag = axisanglemagnitude(axis_angle)
    v = [qi / mag, qj / mag, qk / mag]

    sn = sin(mag / 2.0)
    return quatnormalize([cos(mag / 2.0), sn * v[0], sn * v[1], sn * v[2]])
Пример #15
0
def test_eval_double1():
    x = Symbol("x")
    y = Symbol("y")
    e = sin(x)**2 + cos(x)**2
    e = e.subs(x, 7)
    assert abs(e.n(real=True) - 1) < 1e-9
    assert abs(e.n() - 1) < 1e-9
Пример #16
0
def test_cse_gh174():
    x = se.symbols('x')
    funcs = [se.cos(x)**i for i in range(5)]
    f_lmb = se.Lambdify([x], funcs)
    f_cse = se.Lambdify([x], funcs, cse=True)
    a = np.array([1, 2, 3])
    assert np.allclose(f_lmb(a), f_cse(a))
Пример #17
0
def axisanglerotationmatrix(axis_angle):
    R = axisanglemagnitude(axis_angle)

    x = Piecewise((axis_angle[0] / R, R > 0), (1, True))
    y = Piecewise((axis_angle[1] / R, R > 0), (0, True))
    z = Piecewise((axis_angle[2] / R, R > 0), (0, True))

    csr = sp.cos(R)
    one_minus_csr = (1 - csr)
    snr = sp.sin(R)

    return sp.Matrix([[
        csr + x * x * (1 - csr), x * y * one_minus_csr - z * snr,
        x * z * one_minus_csr + y * snr
    ],
                      [
                          y * x * one_minus_csr + z * snr,
                          csr + y * y * one_minus_csr,
                          y * z * one_minus_csr - x * snr
                      ],
                      [
                          z * x * one_minus_csr - y * snr,
                          z * y * one_minus_csr + x * snr,
                          csr + z * z * one_minus_csr
                      ]])
Пример #18
0
def test_conv7b():
    x = sympy.Symbol("x")
    y = sympy.Symbol("y")
    assert sympify(sympy.sin(x / 3)) == sin(Symbol("x") / 3)
    assert sympify(sympy.sin(x / 3)) != cos(Symbol("x") / 3)
    assert sympify(sympy.cos(x / 3)) == cos(Symbol("x") / 3)
    assert sympify(sympy.tan(x / 3)) == tan(Symbol("x") / 3)
    assert sympify(sympy.cot(x / 3)) == cot(Symbol("x") / 3)
    assert sympify(sympy.csc(x / 3)) == csc(Symbol("x") / 3)
    assert sympify(sympy.sec(x / 3)) == sec(Symbol("x") / 3)
    assert sympify(sympy.asin(x / 3)) == asin(Symbol("x") / 3)
    assert sympify(sympy.acos(x / 3)) == acos(Symbol("x") / 3)
    assert sympify(sympy.atan(x / 3)) == atan(Symbol("x") / 3)
    assert sympify(sympy.acot(x / 3)) == acot(Symbol("x") / 3)
    assert sympify(sympy.acsc(x / 3)) == acsc(Symbol("x") / 3)
    assert sympify(sympy.asec(x / 3)) == asec(Symbol("x") / 3)
Пример #19
0
def axisanglerotationmatrix(axis_angle):
    R = axisanglemagnitude(axis_angle)

    x = axis_angle[0] / R
    y = axis_angle[1] / R
    z = axis_angle[2] / R

    csr = sp.cos(R)
    one_minus_csr = (1 - csr)
    snr = sp.sin(R)

    return sp.Matrix([[
        csr + x * x * (1 - csr), x * y * one_minus_csr - z * snr,
        x * z * one_minus_csr + y * snr
    ],
                      [
                          y * x * one_minus_csr + z * snr,
                          csr + y * y * one_minus_csr,
                          y * z * one_minus_csr - x * snr
                      ],
                      [
                          z * x * one_minus_csr - y * snr,
                          z * y * one_minus_csr + x * snr,
                          csr + z * z * one_minus_csr
                      ]])
Пример #20
0
def test_conv7b():
    x = sympy.Symbol("x")
    y = sympy.Symbol("y")
    assert sympify(sympy.sin(x/3)) == sin(Symbol("x") / 3)
    assert sympify(sympy.sin(x/3)) != cos(Symbol("x") / 3)
    assert sympify(sympy.cos(x/3)) == cos(Symbol("x") / 3)
    assert sympify(sympy.tan(x/3)) == tan(Symbol("x") / 3)
    assert sympify(sympy.cot(x/3)) == cot(Symbol("x") / 3)
    assert sympify(sympy.csc(x/3)) == csc(Symbol("x") / 3)
    assert sympify(sympy.sec(x/3)) == sec(Symbol("x") / 3)
    assert sympify(sympy.asin(x/3)) == asin(Symbol("x") / 3)
    assert sympify(sympy.acos(x/3)) == acos(Symbol("x") / 3)
    assert sympify(sympy.atan(x/3)) == atan(Symbol("x") / 3)
    assert sympify(sympy.acot(x/3)) == acot(Symbol("x") / 3)
    assert sympify(sympy.acsc(x/3)) == acsc(Symbol("x") / 3)
    assert sympify(sympy.asec(x/3)) == asec(Symbol("x") / 3)
Пример #21
0
def axisangle2quat(axis_angle):
    mag = axisanglemagnitude(axis_angle)

    x = axis_angle[0] / mag
    y = axis_angle[1] / mag
    z = axis_angle[2] / mag

    sn = sin(mag / 2.0)
    return quatnormalize([cos(mag / 2.0), sn * x, sn * y, sn * z])
Пример #22
0
def test_sin():
    x = Symbol("x")
    y = Symbol("y")
    e = sin(x)
    assert e.subs({x: y}) == sin(y)
    assert e.subs({x: y}) != sin(x)

    e = cos(x)
    assert e.subs({x: 0}) == 1
    assert e.subs(x, 0) == 1
Пример #23
0
def test_sin():
    x = Symbol("x")
    y = Symbol("y")
    e = sin(x)
    assert e.subs({x: y}) == sin(y)
    assert e.subs({x: y}) != sin(x)

    e = cos(x)
    assert e.subs({x: 0}) == 1
    assert e.subs(x, 0) == 1
Пример #24
0
def axisanglecompose(axis_angle, axis_angle2):
    a = axisanglemagnitude(axis_angle)
    ah = axisanglenormalize(axis_angle)

    b = axisanglemagnitude(axis_angle2)
    bh = axisanglenormalize(axis_angle2)

    sina = sin(a / 2)
    asina = [ah[0] * sina, ah[1] * sina, ah[2] * sina]

    sinb = sin(b / 2)
    bsinb = [bh[0] * sinb, bh[1] * sinb, bh[2] * sinb]

    c = 2 * acos(cos(a / 2)*cos(b / 2) - dot3d(asina, bsinb))
    d = axisanglenormalize(cos(a / 2) * sp.Matrix(bsinb) + cos(b / 2) * sp.Matrix(asina) + cross3d(asina, bsinb))
    return sp.Matrix([
            c * d[0],
            c * d[1],
            c * d[2]
        ])
Пример #25
0
def test_abs():
    x = Symbol("x")
    e = abs(x)
    assert e == abs(x)
    assert e != cos(x)

    assert abs(5) == 5
    assert abs(-5) == 5
    assert abs(Integer(5)/3) == Integer(5)/3
    assert abs(-Integer(5)/3) == Integer(5)/3
    assert abs(Integer(5)/3+x) != Integer(5)/3
    assert abs(Integer(5)/3+x) == abs(Integer(5)/3+x)
Пример #26
0
def test_abs():
    x = Symbol("x")
    e = abs(x)
    assert e == abs(x)
    assert e != cos(x)

    assert abs(5) == 5
    assert abs(-5) == 5
    assert abs(Integer(5) / 3) == Integer(5) / 3
    assert abs(-Integer(5) / 3) == Integer(5) / 3
    assert abs(Integer(5) / 3 + x) != Integer(5) / 3
    assert abs(Integer(5) / 3 + x) == abs(Integer(5) / 3 + x)
Пример #27
0
def rotation_matrix_from_rpy(roll, pitch, yaw):
    """
    Conversion of roll, pitch, yaw to 4x4 rotation matrix according to:
    https://github.com/orocos/orocos_kinematics_dynamics/blob/master/orocos_kdl/src/frames.cpp#L167
    :type roll: Union[float, Symbol]
    :type pitch: Union[float, Symbol]
    :type yaw: Union[float, Symbol]
    :return: 4x4 Matrix
    :rtype: Matrix
    """
    # TODO don't split this into 3 matrices

    rx = se.Matrix([[1, 0, 0, 0],
                    [0, se.cos(roll), -se.sin(roll), 0],
                    [0, se.sin(roll), se.cos(roll), 0],
                    [0, 0, 0, 1]])
    ry = se.Matrix([[se.cos(pitch), 0, se.sin(pitch), 0],
                    [0, 1, 0, 0],
                    [-se.sin(pitch), 0, se.cos(pitch), 0],
                    [0, 0, 0, 1]])
    rz = se.Matrix([[se.cos(yaw), -se.sin(yaw), 0, 0],
                    [se.sin(yaw), se.cos(yaw), 0, 0],
                    [0, 0, 1, 0],
                    [0, 0, 0, 1]])
    return (rz * ry * rx)
Пример #28
0
def _HSIN_(node, S1, S2):
    f = node.children[0].f_expression

    errList1 = [node.f_expression] + \
         [seng.Abs(Si*seng.cos(f)) for Si in S1]

    errList2 = [seng.Abs(Si.seng.cos(f)) for Si in S2]

    herr = seng.Abs(
        seng.sin(f + sum([seng.Abs(Si * pow(2, -53)) for Si in S1 + S2])) *
        sum([seng.Abs(Si * Sj) for Si in S1 + S2 for Sj in S1 + S2]))

    return _solve_(node, errList1, errList2, herr)
Пример #29
0
def quaternion_from_axis_angle(axis, angle):
    """
    :param axis: 3x1 Matrix
    :type axis: Matrix
    :type angle: Union[float, Symbol]
    :return: 4x1 Matrix
    :rtype: Matrix
    """
    half_angle = angle / 2
    return se.Matrix([axis[0] * se.sin(half_angle),
                      axis[1] * se.sin(half_angle),
                      axis[2] * se.sin(half_angle),
                      se.cos(half_angle)])
Пример #30
0
 def test_comparison(self):
     interval = (-3, 10)
     t = symengine.Symbol("t")
     spline = CubicHermiteSpline(n=2)
     spline.from_function(
         [symengine.sin(t), symengine.cos(t)],
         times_of_interest=interval,
         max_anchors=100,
     )
     times = np.linspace(*interval, 100)
     evaluation = spline.get_state(times)
     control = np.vstack((np.sin(times), np.cos(times))).T
     assert_allclose(evaluation, control, atol=0.01)
    def test_Lagrangian(self):
        """
        This Test only works for N  = 1, Rho=1.
        """
        n = 10

        LagVals = np.zeros(n)
        for i in range(n):
            KK = si.Rational(i, 1)
            CFS_Action.K_Liste[0] = KK
            Lag = CFS_Action.lagrangian_without_bound_constr()
            delta = 1 - KK**2 + (1 + KK**2) * sy.cos(r[0])
            Res = si.pi**(-8) * 8 * (1 + KK**2) * si.cos(r[0] / 2)**2 * delta
            LagVals[i] = sy.simplify(Res - Lag)
        np.testing.assert_array_equal(LagVals, np.zeros(n))
Пример #32
0
def rotation3_rpy(roll, pitch, yaw):
    """ Conversion of roll, pitch, yaw to 4x4 rotation matrix according to:
        https://github.com/orocos/orocos_kinematics_dynamics/blob/master/orocos_kdl/src/frames.cpp#L167
    """
    rx = sp.Matrix([[1, 0, 0, 0], [0, sp.cos(roll), -sp.sin(roll), 0],
                    [0, sp.sin(roll), sp.cos(roll), 0], [0, 0, 0, 1]])
    ry = sp.Matrix([[sp.cos(pitch), 0, sp.sin(pitch), 0], [0, 1, 0, 0],
                    [-sp.sin(pitch), 0, sp.cos(pitch), 0], [0, 0, 0, 1]])
    rz = sp.Matrix([[sp.cos(yaw), -sp.sin(yaw), 0, 0],
                    [sp.sin(yaw), sp.cos(yaw), 0, 0], [0, 0, 1, 0],
                    [0, 0, 0, 1]])
    return (rz * ry * rx)
Пример #33
0
def rotation3_axis_angle(axis, angle):
    """ Conversion of unit axis and angle to 4x4 rotation matrix according to:
        http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm
    """
    ct = sp.cos(angle)
    st = sp.sin(angle)
    vt = 1 - ct
    m_vt_0 = vt * axis[0]
    m_vt_1 = vt * axis[1]
    m_vt_2 = vt * axis[2]
    m_st_0 = axis[0] * st
    m_st_1 = axis[1] * st
    m_st_2 = axis[2] * st
    m_vt_0_1 = m_vt_0 * axis[1]
    m_vt_0_2 = m_vt_0 * axis[2]
    m_vt_1_2 = m_vt_1 * axis[2]
    return sp.Matrix(
        [[ct + m_vt_0 * axis[0], -m_st_2 + m_vt_0_1, m_st_1 + m_vt_0_2, 0],
         [m_st_2 + m_vt_0_1, ct + m_vt_1 * axis[1], -m_st_0 + m_vt_1_2, 0],
         [-m_st_1 + m_vt_0_2, m_st_0 + m_vt_1_2, ct + m_vt_2 * axis[2], 0],
         [0, 0, 0, 1]])
Пример #34
0
def spherical_harmonics(lmax):
    """Symbolic real spherical harmonics up to order lmax.

    .. code-block:: python

        sh = spherical_harmonics(6)  # Inclusive, so computes up to l = 6
        sh[3][-3]                    # symbolic f-phi angular function

    Args:
        lmax (int): highest order angular momentum quantum number
    """
    phase = {m: (-1)**m for m in range(lmax + 1)}
    facts = {n: fac(n) for n in range(2 * lmax + 1)}
    sh = OrderedDict()
    for L in range(lmax + 1):
        sh[L] = OrderedDict()
        rac = ((2 * L + 1) / (4 * np.pi))**0.5
        der = (_x**2 - 1)**L
        den = 2**L * facts[L]
        for _ in range(L):
            der = der.diff(_x)
        for m in range(L + 1):
            pol = (1 - _x**2)**(m / 2)
            if m: der = der.diff(_x)
            leg = phase[m] / den * (pol * der).subs({_x: _z / _r})
            if not m:
                sh[L][m] = rac * leg
                continue
            N = 2**0.5 * phase[m]
            facs = facts[L - m] / facts[L + m]
            norm = facs**0.5
            phi = (m * _x).subs({_x: 'arctan2(_y, _x)'})
            fun = cos(phi)
            sh[L][m] = N * rac * norm * leg * fun
            fun = sin(phi)
            sh[L][-m] = N * rac * norm * leg * fun
    return sh
Пример #35
0
def spherical_harmonics(lmax):
    """Symbolic real spherical harmonics up to order lmax.

    .. code-block:: python

        sh = spherical_harmonics(6)  # Inclusive, so computes up to l = 6
        sh[3][-3]                    # symbolic f-phi angular function

    Args:
        lmax (int): highest order angular momentum quantum number
    """
    phase = {m: (-1) ** m for m in range(lmax + 1)}
    facts = {n: fac(n) for n in range(2 * lmax + 1)}
    sh = OrderedDict()
    for L in range(lmax + 1):
        sh[L] = OrderedDict()
        rac = ((2 * L + 1) / (4 * np.pi)) ** 0.5
        der = (_x ** 2 - 1) ** L
        den = 2 ** L * facts[L]
        for _ in range(L):
            der = der.diff(_x)
        for m in range(L + 1):
            pol = (1 - _x ** 2) ** (m/2)
            if m: der = der.diff(_x)
            leg = phase[m] / den * (pol * der).subs({_x: _z / _r})
            if not m:
                sh[L][m] = rac * leg
                continue
            N = 2 ** 0.5 * phase[m]
            facs = facts[L - m] / facts[L + m]
            norm = facs ** 0.5
            phi = (m * _x).subs({_x: 'arctan2(_y, _x)'})
            fun = cos(phi)
            sh[L][m] = N * rac * norm * leg * fun
            fun = sin(phi)
            sh[L][-m] = N * rac * norm * leg * fun
    return sh
Пример #36
0
def test_sage_conversions():
    try:
        import sage.all as sage
    except ImportError:
        return

    x, y = sage.SR.var('x y')
    x1, y1 = symbols('x, y')

    # Symbol
    assert x1._sage_() == x
    assert x1 == sympify(x)

    # Integer
    assert Integer(12)._sage_() == sage.Integer(12)
    assert Integer(12) == sympify(sage.Integer(12))

    # Rational
    assert (Integer(1) / 2)._sage_() == sage.Integer(1) / 2
    assert Integer(1) / 2 == sympify(sage.Integer(1) / 2)

    # Operators
    assert x1 + y == x1 + y1
    assert x1 * y == x1 * y1
    assert x1 ** y == x1 ** y1
    assert x1 - y == x1 - y1
    assert x1 / y == x1 / y1

    assert x + y1 == x + y
    assert x * y1 == x * y
    # Doesn't work in Sage 6.1.1ubuntu2
    # assert x ** y1 == x ** y
    assert x - y1 == x - y
    assert x / y1 == x / y

    # Conversions
    assert (x1 + y1)._sage_() == x + y
    assert (x1 * y1)._sage_() == x * y
    assert (x1 ** y1)._sage_() == x ** y
    assert (x1 - y1)._sage_() == x - y
    assert (x1 / y1)._sage_() == x / y

    assert x1 + y1 == sympify(x + y)
    assert x1 * y1 == sympify(x * y)
    assert x1 ** y1 == sympify(x ** y)
    assert x1 - y1 == sympify(x - y)
    assert x1 / y1 == sympify(x / y)

    # Functions
    assert sin(x1) == sin(x)
    assert sin(x1)._sage_() == sage.sin(x)
    assert sin(x1) == sympify(sage.sin(x))

    assert cos(x1) == cos(x)
    assert cos(x1)._sage_() == sage.cos(x)
    assert cos(x1) == sympify(sage.cos(x))

    assert function_symbol('f', x1, y1)._sage_() == sage.function('f', x, y)
    assert function_symbol('f', 2 * x1, x1 + y1).diff(x1)._sage_() == sage.function('f', 2 * x, x + y).diff(x)

    # For the following test, sage needs to be modified
    # assert sage.sin(x) == sage.sin(x1)

    # Constants
    assert pi._sage_() == sage.pi
    assert E._sage_() == sage.e
    assert I._sage_() == sage.I

    assert pi == sympify(sage.pi)
    assert E == sympify(sage.e)

    # SympyConverter does not support converting the following
    # assert I == sympify(sage.I)

    # Matrix
    assert DenseMatrix(1, 2, [x1, y1])._sage_() == sage.matrix([[x, y]])

    # SympyConverter does not support converting the following
    # assert DenseMatrix(1, 2, [x1, y1]) == sympify(sage.matrix([[x, y]]))

    # Sage Number
    a = sage.Mod(2, 7)
    b = PyNumber(a, sage_module)

    a = a + 8
    b = b + 8
    assert isinstance(b, PyNumber)
    assert b._sage_() == a

    a = a + x
    b = b + x
    assert isinstance(b, Add)
    assert b._sage_() == a

    # Sage Function
    e = x1 + wrap_sage_function(sage.log_gamma(x))
    assert str(e) == "x + log_gamma(x)"
    assert isinstance(e, Add)
    assert e + wrap_sage_function(sage.log_gamma(x)) == x1 + 2*wrap_sage_function(sage.log_gamma(x))

    f = e.subs({x1 : 10})
    assert f == 10 + log(362880)

    f = e.subs({x1 : 2})
    assert f == 2

    f = e.subs({x1 : 100});
    v = f.n(53, real=True);
    assert abs(float(v) - 459.13420537) < 1e-7

    f = e.diff(x1)
Пример #37
0
def test_sage_conversions():
    try:
        import sage.all as sage
    except ImportError:
        return

    x, y = sage.SR.var('x y')
    x1, y1 = symbols('x, y')

    # Symbol
    assert x1._sage_() == x
    assert x1 == sympify(x)

    # Integer
    assert Integer(12)._sage_() == sage.Integer(12)
    assert Integer(12) == sympify(sage.Integer(12))

    # Rational
    assert (Integer(1) / 2)._sage_() == sage.Integer(1) / 2
    assert Integer(1) / 2 == sympify(sage.Integer(1) / 2)

    # Operators
    assert x1 + y == x1 + y1
    assert x1 * y == x1 * y1
    assert x1**y == x1**y1
    assert x1 - y == x1 - y1
    assert x1 / y == x1 / y1

    assert x + y1 == x + y
    assert x * y1 == x * y
    # Doesn't work in Sage 6.1.1ubuntu2
    # assert x ** y1 == x ** y
    assert x - y1 == x - y
    assert x / y1 == x / y

    # Conversions
    assert (x1 + y1)._sage_() == x + y
    assert (x1 * y1)._sage_() == x * y
    assert (x1**y1)._sage_() == x**y
    assert (x1 - y1)._sage_() == x - y
    assert (x1 / y1)._sage_() == x / y

    assert x1 + y1 == sympify(x + y)
    assert x1 * y1 == sympify(x * y)
    assert x1**y1 == sympify(x**y)
    assert x1 - y1 == sympify(x - y)
    assert x1 / y1 == sympify(x / y)

    # Functions
    assert sin(x1) == sin(x)
    assert sin(x1)._sage_() == sage.sin(x)
    assert sin(x1) == sympify(sage.sin(x))

    assert cos(x1) == cos(x)
    assert cos(x1)._sage_() == sage.cos(x)
    assert cos(x1) == sympify(sage.cos(x))

    assert function_symbol('f', x1, y1)._sage_() == sage.function('f', x, y)
    assert function_symbol('f', 2 * x1,
                           x1 + y1).diff(x1)._sage_() == sage.function(
                               'f', 2 * x, x + y).diff(x)

    # For the following test, sage needs to be modified
    # assert sage.sin(x) == sage.sin(x1)

    # Constants
    assert pi._sage_() == sage.pi
    assert E._sage_() == sage.e
    assert I._sage_() == sage.I

    assert pi == sympify(sage.pi)
    assert E == sympify(sage.e)

    # SympyConverter does not support converting the following
    # assert I == sympify(sage.I)

    # Matrix
    assert DenseMatrix(1, 2, [x1, y1])._sage_() == sage.matrix([[x, y]])

    # SympyConverter does not support converting the following
    # assert DenseMatrix(1, 2, [x1, y1]) == sympify(sage.matrix([[x, y]]))

    # Sage Number
    a = sage.Mod(2, 7)
    b = PyNumber(a, sage_module)

    a = a + 8
    b = b + 8
    assert isinstance(b, PyNumber)
    assert b._sage_() == a

    a = a + x
    b = b + x
    assert isinstance(b, Add)
    assert b._sage_() == a

    # Sage Function
    e = x1 + wrap_sage_function(sage.log_gamma(x))
    assert str(e) == "x + log_gamma(x)"
    assert isinstance(e, Add)
    assert e + wrap_sage_function(
        sage.log_gamma(x)) == x1 + 2 * wrap_sage_function(sage.log_gamma(x))

    f = e.subs({x1: 10})
    assert f == 10 + log(362880)

    f = e.subs({x1: 2})
    assert f == 2

    f = e.subs({x1: 100})
    v = f.n(53, real=True)
    assert abs(float(v) - 459.13420537) < 1e-7

    f = e.diff(x1)
Пример #38
0
def test_args():
    x = Symbol("x")
    e = cos(x)
    raises(TypeError, lambda: e.subs(x, 0, 3))
Пример #39
0
def test_eval_double2():
    x = Symbol("x")
    y = Symbol("y")
    e = sin(x)**2 + cos(x)**2
    raises(RuntimeError, lambda: (abs(eval_double(e) - 1) < 1e-9))
Пример #40
0
def test_eval_double1():
    x = Symbol("x")
    y = Symbol("y")
    e = sin(x)**2 + cos(x)**2
    e = e.subs(x, 7)
    assert abs(eval_double(e) - 1) < 1e-9
Пример #41
0
def test_sage_conversions():
    try:
        import sage.all as sage
    except ImportError:
        return

    x, y = sage.SR.var('x y')
    x1, y1 = symbols('x, y')

    # Symbol
    assert x1._sage_() == x
    assert x1 == sympify(x)

    # Integer
    assert Integer(12)._sage_() == sage.Integer(12)
    assert Integer(12) == sympify(sage.Integer(12))

    # Rational
    assert (Integer(1) / 2)._sage_() == sage.Integer(1) / 2
    assert Integer(1) / 2 == sympify(sage.Integer(1) / 2)

    # Operators
    assert x1 + y == x1 + y1
    assert x1 * y == x1 * y1
    assert x1 ** y == x1 ** y1
    assert x1 - y == x1 - y1
    assert x1 / y == x1 / y1

    assert x + y1 == x + y
    assert x * y1 == x * y
    # Doesn't work in Sage 6.1.1ubuntu2
    # assert x ** y1 == x ** y
    assert x - y1 == x - y
    assert x / y1 == x / y

    # Conversions
    assert (x1 + y1)._sage_() == x + y
    assert (x1 * y1)._sage_() == x * y
    assert (x1 ** y1)._sage_() == x ** y
    assert (x1 - y1)._sage_() == x - y
    assert (x1 / y1)._sage_() == x / y

    assert x1 + y1 == sympify(x + y)
    assert x1 * y1 == sympify(x * y)
    assert x1 ** y1 == sympify(x ** y)
    assert x1 - y1 == sympify(x - y)
    assert x1 / y1 == sympify(x / y)

    # Functions
    assert sin(x1) == sin(x)
    assert sin(x1)._sage_() == sage.sin(x)
    assert sin(x1) == sympify(sage.sin(x))

    assert cos(x1) == cos(x)
    assert cos(x1)._sage_() == sage.cos(x)
    assert cos(x1) == sympify(sage.cos(x))

    assert function_symbol('f', x1, y1)._sage_() == sage.function('f', x, y)
    assert function_symbol('f', 2 * x1, x1 + y1).diff(x1)._sage_() == sage.function('f', 2 * x, x + y).diff(x)

    # For the following test, sage needs to be modified
    # assert sage.sin(x) == sage.sin(x1)

    # Constants
    assert pi._sage_() == sage.pi
    assert E._sage_() == sage.e
    assert I._sage_() == sage.I

    assert pi == sympify(sage.pi)
    assert E == sympify(sage.e)

    # SympyConverter does not support converting the following
    # assert I == sympify(sage.I)

    # Matrix
    assert DenseMatrix(1, 2, [x1, y1])._sage_() == sage.matrix([[x, y]])
Пример #42
0
def test_xreplace():
    x = Symbol("x")
    y = Symbol("y")
    f = sin(cos(x))
    assert f.xreplace({x: y}) == sin(cos(y))
Пример #43
0
def test_conv7():
    x = Symbol("x")
    y = Symbol("y")
    assert sin(x/3)._sympy_() == sympy.sin(sympy.Symbol("x") / 3)
    assert sin(x/3)._sympy_() != sympy.cos(sympy.Symbol("x") / 3)
    assert cos(x/3)._sympy_() == sympy.cos(sympy.Symbol("x") / 3)
Пример #44
0
def test_conv7b():
    x = sympy.Symbol("x")
    y = sympy.Symbol("y")
    assert sympify(sympy.sin(x/3)) == sin(Symbol("x") / 3)
    assert sympify(sympy.sin(x/3)) != cos(Symbol("x") / 3)
    assert sympify(sympy.cos(x/3)) == cos(Symbol("x") / 3)