Beispiel #1
0
    def cross(self, vect):
        """
        Represents the cross product between this operator and a given
        vector - equal to the curl of the vector field.

        Parameters
        ==========

        vect : Vector
            The vector whose curl is to be calculated.

        Examples
        ========

        >>> from sympy.vector import CoordSysCartesian
        >>> C = CoordSysCartesian('C')
        >>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
        >>> C.delop ^ v
        (-C.x*C.y + C.x*C.z)*C.i + (C.x*C.y - C.y*C.z)*C.j + (-C.x*C.z + C.y*C.z)*C.k
        >>> C.delop.cross(C.i)
        0

        """

        vectx = express(vect.dot(self._i), self.system)
        vecty = express(vect.dot(self._j), self.system)
        vectz = express(vect.dot(self._k), self.system)
        outvec = Vector.zero
        outvec += (diff(vectz, self._y) - diff(vecty, self._z)) * self._i
        outvec += (diff(vectx, self._z) - diff(vectz, self._x)) * self._j
        outvec += (diff(vecty, self._x) - diff(vectx, self._y)) * self._k

        return outvec
Beispiel #2
0
    def cross(self, vect):
        """
        Represents the cross product between this operator and a given
        vector - equal to the curl of the vector field.

        Parameters
        ==========

        vect : Vector
            The vector whose curl is to be calculated.

        Examples
        ========

        >>> from sympy.vector import CoordSysCartesian
        >>> C = CoordSysCartesian('C')
        >>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
        >>> C.delop ^ v
        (-C.x*C.y + C.x*C.z)*C.i + (C.x*C.y - C.y*C.z)*C.j + (-C.x*C.z + C.y*C.z)*C.k
        >>> C.delop.cross(C.i)
        0

        """

        vectx = express(vect.dot(self._i), self.system)
        vecty = express(vect.dot(self._j), self.system)
        vectz = express(vect.dot(self._k), self.system)
        outvec = Vector.zero
        outvec += (diff(vectz, self._y) - diff(vecty, self._z)) * self._i
        outvec += (diff(vectx, self._z) - diff(vectz, self._x)) * self._j
        outvec += (diff(vecty, self._x) - diff(vectx, self._y)) * self._k

        return outvec
Beispiel #3
0
def curl(vect, coord_sys=None, doit=True):
    """
    Returns the curl of a vector field computed wrt the base scalars
    of the given coordinate system.

    Parameters
    ==========

    vect : Vector
        The vector operand

    coord_sys : CoordSys3D
        The coordinate system to calculate the gradient in.
        Deprecated since version 1.1

    doit : bool
        If True, the result is returned after calling .doit() on
        each component. Else, the returned expression contains
        Derivative instances

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, curl
    >>> R = CoordSys3D('R')
    >>> v1 = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> curl(v1)
    0
    >>> v2 = R.x*R.y*R.z*R.i
    >>> curl(v2)
    R.x*R.y*R.j + (-R.x*R.z)*R.k

    """

    coord_sys = _get_coord_sys_from_expr(vect, coord_sys)

    if coord_sys is None:
        return Vector.zero
    else:
        i, j, k = coord_sys.base_vectors()
        x, y, z = coord_sys.base_scalars()
        h1, h2, h3 = coord_sys.lame_coefficients()
        from sympy.vector.functions import express
        vectx = express(vect.dot(i), coord_sys, variables=True)
        vecty = express(vect.dot(j), coord_sys, variables=True)
        vectz = express(vect.dot(k), coord_sys, variables=True)
        outvec = Vector.zero
        outvec += (Derivative(vectz * h3, y) -
                   Derivative(vecty * h2, z)) * i / (h2 * h3)
        outvec += (Derivative(vectx * h1, z) -
                   Derivative(vectz * h3, x)) * j / (h1 * h3)
        outvec += (Derivative(vecty * h2, x) -
                   Derivative(vectx * h1, y)) * k / (h2 * h1)

        if doit:
            return outvec.doit()
        return outvec
Beispiel #4
0
def curl(vect, coord_sys=None, doit=True):
    """
    Returns the curl of a vector field computed wrt the base scalars
    of the given coordinate system.

    Parameters
    ==========

    vect : Vector
        The vector operand

    coord_sys : CoordSysCartesian
        The coordinate system to calculate the gradient in.
        Deprecated since version 1.1

    doit : bool
        If True, the result is returned after calling .doit() on
        each component. Else, the returned expression contains
        Derivative instances

    Examples
    ========

    >>> from sympy.vector import CoordSysCartesian, curl
    >>> R = CoordSysCartesian('R')
    >>> v1 = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> curl(v1)
    0
    >>> v2 = R.x*R.y*R.z*R.i
    >>> curl(v2)
    R.x*R.y*R.j + (-R.x*R.z)*R.k

    """

    coord_sys = _get_coord_sys_from_expr(vect, coord_sys)
    if coord_sys is None:
        return Vector.zero
    else:
        from sympy.vector.functions import express
        vectx = express(vect.dot(coord_sys._i), coord_sys, variables=True)
        vecty = express(vect.dot(coord_sys._j), coord_sys, variables=True)
        vectz = express(vect.dot(coord_sys._k), coord_sys, variables=True)
        outvec = Vector.zero
        outvec += (Derivative(vectz * coord_sys._h3, coord_sys._y) -
                   Derivative(vecty * coord_sys._h2, coord_sys._z)
                   ) * coord_sys._i / (coord_sys._h2 * coord_sys._h3)
        outvec += (Derivative(vectx * coord_sys._h1, coord_sys._z) -
                   Derivative(vectz * coord_sys._h3, coord_sys._x)
                   ) * coord_sys._j / (coord_sys._h1 * coord_sys._h3)
        outvec += (Derivative(vecty * coord_sys._h2, coord_sys._x) -
                   Derivative(vectx * coord_sys._h1, coord_sys._y)
                   ) * coord_sys._k / (coord_sys._h2 * coord_sys._h1)

        if doit:
            return outvec.doit()
        return outvec
Beispiel #5
0
def curl(vect, coord_sys=None, doit=True):
    """
    Returns the curl of a vector field computed wrt the base scalars
    of the given coordinate system.

    Parameters
    ==========

    vect : Vector
        The vector operand

    coord_sys : CoordSys3D
        The coordinate system to calculate the gradient in.
        Deprecated since version 1.1

    doit : bool
        If True, the result is returned after calling .doit() on
        each component. Else, the returned expression contains
        Derivative instances

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, curl
    >>> R = CoordSys3D('R')
    >>> v1 = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> curl(v1)
    0
    >>> v2 = R.x*R.y*R.z*R.i
    >>> curl(v2)
    R.x*R.y*R.j + (-R.x*R.z)*R.k

    """

    coord_sys = _get_coord_sys_from_expr(vect, coord_sys)
    if coord_sys is None:
        return Vector.zero
    else:
        from sympy.vector.functions import express
        vectx = express(vect.dot(coord_sys._i), coord_sys, variables=True)
        vecty = express(vect.dot(coord_sys._j), coord_sys, variables=True)
        vectz = express(vect.dot(coord_sys._k), coord_sys, variables=True)
        outvec = Vector.zero
        outvec += (Derivative(vectz * coord_sys._h3, coord_sys._y) -
                   Derivative(vecty * coord_sys._h2, coord_sys._z)) * coord_sys._i / (coord_sys._h2 * coord_sys._h3)
        outvec += (Derivative(vectx * coord_sys._h1, coord_sys._z) -
                   Derivative(vectz * coord_sys._h3, coord_sys._x)) * coord_sys._j / (coord_sys._h1 * coord_sys._h3)
        outvec += (Derivative(vecty * coord_sys._h2, coord_sys._x) -
                   Derivative(vectx * coord_sys._h1, coord_sys._y)) * coord_sys._k / (coord_sys._h2 * coord_sys._h1)

        if doit:
            return outvec.doit()
        return outvec
def test_coordinate_vars():
    """
    Tests the coordinate variables functionality with respect to
    reorientation of coordinate systems.
    """
    A = CoordSysCartesian('A')
    assert BaseScalar('Ax', 0, A, ' ', ' ') == A.x
    assert BaseScalar('Ay', 1, A, ' ', ' ') == A.y
    assert BaseScalar('Az', 2, A, ' ', ' ') == A.z
    assert BaseScalar('Ax', 0, A, ' ', ' ').__hash__() == A.x.__hash__()
    assert isinstance(A.x, BaseScalar) and \
           isinstance(A.y, BaseScalar) and \
           isinstance(A.z, BaseScalar)
    assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z}
    assert A.x.system == A
    B = A.orient_new_axis('B', q, A.k)
    assert B.scalar_map(A) == {B.z: A.z, B.y: -A.x*sin(q) + A.y*cos(q),
                                 B.x: A.x*cos(q) + A.y*sin(q)}
    assert A.scalar_map(B) == {A.x: B.x*cos(q) - B.y*sin(q),
                                 A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z}
    assert express(B.x, A, variables=True) == A.x*cos(q) + A.y*sin(q)
    assert express(B.y, A, variables=True) == -A.x*sin(q) + A.y*cos(q)
    assert express(B.z, A, variables=True) == A.z
    assert express(B.x*B.y*B.z, A, variables=True) == \
           A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q))
    assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \
           (B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \
           B.y*cos(q))*A.j + B.z*A.k
    assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \
                            variables=True)) == \
           A.x*A.i + A.y*A.j + A.z*A.k
    assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \
           (A.x*cos(q) + A.y*sin(q))*B.i + \
           (-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k
    assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \
                            variables=True)) == \
           B.x*B.i + B.y*B.j + B.z*B.k
    N = B.orient_new_axis('N', -q, B.k)
    assert N.scalar_map(A) == \
           {N.x: A.x, N.z: A.z, N.y: A.y}
    C = A.orient_new_axis('C', q, A.i + A.j + A.k)
    mapping = A.scalar_map(C)
    assert mapping[A.x] == 2*C.x*cos(q)/3 + C.x/3 - \
           2*C.y*sin(q + pi/6)/3 + C.y/3 - 2*C.z*cos(q + pi/3)/3 + C.z/3
    assert mapping[A.y] == -2*C.x*cos(q + pi/3)/3 + \
           C.x/3 + 2*C.y*cos(q)/3 + C.y/3 - 2*C.z*sin(q + pi/6)/3 + C.z/3
    assert mapping[A.z] == -2*C.x*sin(q + pi/6)/3 + C.x/3 - \
           2*C.y*cos(q + pi/3)/3 + C.y/3 + 2*C.z*cos(q)/3 + C.z/3
    D = A.locate_new('D', a*A.i + b*A.j + c*A.k)
    assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b}
    E = A.orient_new_axis('E', a, A.k, a*A.i + b*A.j + c*A.k)
    assert A.scalar_map(E) == {A.z: E.z + c,
                               A.x: E.x*cos(a) - E.y*sin(a) + a,
                               A.y: E.x*sin(a) + E.y*cos(a) + b}
    assert E.scalar_map(A) == {E.x: (A.x - a)*cos(a) + (A.y - b)*sin(a),
                               E.y: (-A.x + a)*sin(a) + (A.y - b)*cos(a),
                               E.z: A.z - c}
    F = A.locate_new('F', Vector.zero)
    assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
Beispiel #7
0
    def __call__(self, scalar_field):
        """
        Represents the gradient of the given scalar field.

        Parameters
        ==========

        scalar_field : SymPy expression
            The scalar field to calculate the gradient of.

        Examples
        ========

        >>> from sympy.vector import CoordSysCartesian
        >>> C = CoordSysCartesian('C')
        >>> C.delop(C.x*C.y*C.z)
        C.y*C.z*C.i + C.x*C.z*C.j + C.x*C.y*C.k

        """

        scalar_field = express(scalar_field, self.system, variables=True)
        vx = diff(scalar_field, self._x)
        vy = diff(scalar_field, self._y)
        vz = diff(scalar_field, self._z)

        return vx * self._i + vy * self._j + vz * self._k
Beispiel #8
0
    def __call__(self, scalar_field):
        """
        Represents the gradient of the given scalar field.

        Parameters
        ==========

        scalar_field : SymPy expression
            The scalar field to calculate the gradient of.

        Examples
        ========

        >>> from sympy.vector import CoordSysCartesian
        >>> C = CoordSysCartesian('C')
        >>> C.delop(C.x*C.y*C.z)
        C.y*C.z*C.i + C.x*C.z*C.j + C.x*C.y*C.k

        """

        scalar_field = express(scalar_field, self.system,
                               variables = True)
        vx = diff(scalar_field, self._x)
        vy = diff(scalar_field, self._y)
        vz = diff(scalar_field, self._z)

        return vx*self._i + vy*self._j + vz*self._k
Beispiel #9
0
    def express_coordinates(self, coordinate_system):
        """
        Returns the Cartesian/rectangular coordinates of this point
        wrt the origin of the given CoordSysCartesian instance.

        Parameters
        ==========

        coordinate_system : CoordSysCartesian
            The coordinate system to express the coordinates of this
            Point in.

        Examples
        ========

        >>> from sympy.vector import Point, CoordSysCartesian
        >>> N = CoordSysCartesian('N')
        >>> p1 = N.origin.locate_new('p1', 10 * N.i)
        >>> p2 = p1.locate_new('p2', 5 * N.j)
        >>> p2.express_coordinates(N)
        (10, 5, 0)

        """

        #Determine the position vector
        pos_vect = self.position_wrt(coordinate_system.origin)
        #Express it in the given coordinate system
        pos_vect = trigsimp(
            express(pos_vect, coordinate_system, variables=True))
        coords = []
        for vect in coordinate_system.base_vectors():
            coords.append(pos_vect.dot(vect))
        return tuple(coords)
Beispiel #10
0
    def express_coordinates(self, coordinate_system):
        """
        Returns the Cartesian/rectangular coordinates of this point
        wrt the origin of the given CoordSysCartesian instance.

        Parameters
        ==========

        coordinate_system : CoordSysCartesian
            The coordinate system to express the coordinates of this
            Point in.

        Examples
        ========

        >>> from sympy.vector import Point, CoordSysCartesian
        >>> N = CoordSysCartesian('N')
        >>> p1 = N.origin.locate_new('p1', 10 * N.i)
        >>> p2 = p1.locate_new('p2', 5 * N.j)
        >>> p2.express_coordinates(N)
        (10, 5, 0)

        """

        #Determine the position vector
        pos_vect = self.position_wrt(coordinate_system.origin)
        #Express it in the given coordinate system
        pos_vect = trigsimp(express(pos_vect, coordinate_system,
                                    variables = True))
        coords = []
        for vect in coordinate_system.base_vectors():
            coords.append(pos_vect.dot(vect))
        return tuple(coords)
def test_vector():
    """
    Tests the effects of orientation of coordinate systems on
    basic vector operations.
    """
    N = CoordSysCartesian("N")
    A = N.orient_new_axis("A", q1, N.k)
    B = A.orient_new_axis("B", q2, A.i)
    C = B.orient_new_axis("C", q3, B.j)

    # Test to_matrix
    v1 = a * N.i + b * N.j + c * N.k
    assert v1.to_matrix(A) == Matrix([[a * cos(q1) + b * sin(q1)], [-a * sin(q1) + b * cos(q1)], [c]])

    # Test dot
    assert N.i.dot(A.i) == cos(q1)
    assert N.i.dot(A.j) == -sin(q1)
    assert N.i.dot(A.k) == 0
    assert N.j.dot(A.i) == sin(q1)
    assert N.j.dot(A.j) == cos(q1)
    assert N.j.dot(A.k) == 0
    assert N.k.dot(A.i) == 0
    assert N.k.dot(A.j) == 0
    assert N.k.dot(A.k) == 1

    assert N.i.dot(A.i + A.j) == -sin(q1) + cos(q1) == (A.i + A.j).dot(N.i)

    assert A.i.dot(C.i) == cos(q3)
    assert A.i.dot(C.j) == 0
    assert A.i.dot(C.k) == sin(q3)
    assert A.j.dot(C.i) == sin(q2) * sin(q3)
    assert A.j.dot(C.j) == cos(q2)
    assert A.j.dot(C.k) == -sin(q2) * cos(q3)
    assert A.k.dot(C.i) == -cos(q2) * sin(q3)
    assert A.k.dot(C.j) == sin(q2)
    assert A.k.dot(C.k) == cos(q2) * cos(q3)

    # Test cross
    assert N.i.cross(A.i) == sin(q1) * A.k
    assert N.i.cross(A.j) == cos(q1) * A.k
    assert N.i.cross(A.k) == -sin(q1) * A.i - cos(q1) * A.j
    assert N.j.cross(A.i) == -cos(q1) * A.k
    assert N.j.cross(A.j) == sin(q1) * A.k
    assert N.j.cross(A.k) == cos(q1) * A.i - sin(q1) * A.j
    assert N.k.cross(A.i) == A.j
    assert N.k.cross(A.j) == -A.i
    assert N.k.cross(A.k) == Vector.zero

    assert N.i.cross(A.i) == sin(q1) * A.k
    assert N.i.cross(A.j) == cos(q1) * A.k
    assert N.i.cross(A.i + A.j) == sin(q1) * A.k + cos(q1) * A.k
    assert (A.i + A.j).cross(N.i) == (-sin(q1) - cos(q1)) * N.k

    assert A.i.cross(C.i) == sin(q3) * C.j
    assert A.i.cross(C.j) == -sin(q3) * C.i + cos(q3) * C.k
    assert A.i.cross(C.k) == -cos(q3) * C.j
    assert C.i.cross(A.i) == (-sin(q3) * cos(q2)) * A.j + (-sin(q2) * sin(q3)) * A.k
    assert C.j.cross(A.i) == (sin(q2)) * A.j + (-cos(q2)) * A.k
    assert express(C.k.cross(A.i), C).trigsimp() == cos(q3) * C.j
Beispiel #12
0
 def directional_derivative(field):
     field = express(field, other.system, variables=True)
     out = self.dot(other._i) * df(field, other._x)
     out += self.dot(other._j) * df(field, other._y)
     out += self.dot(other._k) * df(field, other._z)
     if out == 0 and isinstance(field, Vector):
         out = Vector.zero
     return out
Beispiel #13
0
 def directional_derivative(field):
     field = express(field, other.system, variables = True)
     out = self.dot(other._i) * df(field, other._x)
     out += self.dot(other._j) * df(field, other._y)
     out += self.dot(other._k) * df(field, other._z)
     if out == 0 and isinstance(field, Vector):
         out = Vector.zero
     return out
Beispiel #14
0
    def cross(self, vect, doit=False):
        """
        Represents the cross product between this operator and a given
        vector - equal to the curl of the vector field.

        Parameters
        ==========

        vect : Vector
            The vector whose curl is to be calculated.

        doit : bool
            If True, the result is returned after calling .doit() on
            each component. Else, the returned expression contains
            Derivative instances

        Examples
        ========

        >>> from sympy.vector import CoordSysCartesian
        >>> C = CoordSysCartesian('C')
        >>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
        >>> C.delop.cross(v, doit = True)
        (-C.x*C.y + C.x*C.z)*C.i + (C.x*C.y - C.y*C.z)*C.j +
            (-C.x*C.z + C.y*C.z)*C.k
        >>> (C.delop ^ C.i).doit()
        0

        """

        vectx = express(vect.dot(self._i), self.system, variables=True)
        vecty = express(vect.dot(self._j), self.system, variables=True)
        vectz = express(vect.dot(self._k), self.system, variables=True)
        outvec = Vector.zero
        outvec += (Derivative(vectz * self._h3, self._y) - Derivative(
            vecty * self._h2, self._z)) * self._i / (self._h2 * self._h3)
        outvec += (Derivative(vectx * self._h1, self._z) - Derivative(
            vectz * self._h3, self._x)) * self._j / (self._h1 * self._h3)
        outvec += (Derivative(vecty * self._h2, self._x) - Derivative(
            vectx * self._h1, self._y)) * self._k / (self._h2 * self._h1)

        if doit:
            return outvec.doit()
        return outvec
Beispiel #15
0
    def cross(self, vect, doit=False):
        """
        Represents the cross product between this operator and a given
        vector - equal to the curl of the vector field.

        Parameters
        ==========

        vect : Vector
            The vector whose curl is to be calculated.

        doit : bool
            If True, the result is returned after calling .doit() on
            each component. Else, the returned expression contains
            Derivative instances

        Examples
        ========

        >>> from sympy.vector import CoordSysCartesian
        >>> C = CoordSysCartesian('C')
        >>> v = C.x*C.y*C.z * (C.i + C.j + C.k)
        >>> C.delop.cross(v, doit = True)
        (-C.x*C.y + C.x*C.z)*C.i + (C.x*C.y - C.y*C.z)*C.j +
            (-C.x*C.z + C.y*C.z)*C.k
        >>> (C.delop ^ C.i).doit()
        0

        """

        vectx = express(vect.dot(self._i), self.system, variables=True)
        vecty = express(vect.dot(self._j), self.system, variables=True)
        vectz = express(vect.dot(self._k), self.system, variables=True)
        outvec = Vector.zero
        outvec += (Derivative(vectz, self._y) -
                   Derivative(vecty, self._z)) * self._i
        outvec += (Derivative(vectx, self._z) -
                   Derivative(vectz, self._x)) * self._j
        outvec += (Derivative(vecty, self._x) -
                   Derivative(vectx, self._y)) * self._k

        if doit:
            return outvec.doit()
        return outvec
Beispiel #16
0
def _diff_conditional(expr, base_scalar, coeff_1, coeff_2):
    """
    First re-expresses expr in the system that base_scalar belongs to.
    If base_scalar appears in the re-expressed form, differentiates
    it wrt base_scalar.
    Else, returns S(0)
    """
    from sympy.vector.functions import express
    new_expr = express(expr, base_scalar.system, variables=True)
    if base_scalar in new_expr.atoms(BaseScalar):
        return Derivative(coeff_1 * coeff_2 * new_expr, base_scalar)
    return S(0)
Beispiel #17
0
def _diff_conditional(expr, base_scalar):
    """
    First re-expresses expr in the system that base_scalar belongs to.
    If base_scalar appears in the re-expressed form, differentiates
    it wrt base_scalar.
    Else, returns S(0)
    """

    new_expr = express(expr, base_scalar.system, variables=True)
    if base_scalar in new_expr.atoms():
        return Derivative(new_expr, base_scalar)
    return S(0)
Beispiel #18
0
def _diff_conditional(expr, base_scalar, coeff_1, coeff_2):
    """
    First re-expresses expr in the system that base_scalar belongs to.
    If base_scalar appears in the re-expressed form, differentiates
    it wrt base_scalar.
    Else, returns S(0)
    """
    from sympy.vector.functions import express
    new_expr = express(expr, base_scalar.system, variables=True)
    if base_scalar in new_expr.atoms(BaseScalar):
        return Derivative(coeff_1 * coeff_2 * new_expr, base_scalar)
    return S(0)
Beispiel #19
0
def _diff_conditional(expr, base_scalar):
    """
    First re-expresses expr in the system that base_scalar belongs to.
    If base_scalar appears in the re-expressed form, differentiates
    it wrt base_scalar.
    Else, returns S(0)
    """

    new_expr = express(expr, base_scalar.system, variables = True)
    if base_scalar in new_expr.atoms(BaseScalar):
        return Derivative(new_expr, base_scalar)
    return S(0)
Beispiel #20
0
def gradient(scalar_field, coord_sys=None, doit=True):
    """
    Returns the vector gradient of a scalar field computed wrt the
    base scalars of the given coordinate system.

    Parameters
    ==========

    scalar_field : SymPy Expr
        The scalar field to compute the gradient of

    coord_sys : CoordSys3D
        The coordinate system to calculate the gradient in
        Deprecated since version 1.1

    doit : bool
        If True, the result is returned after calling .doit() on
        each component. Else, the returned expression contains
        Derivative instances

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, gradient
    >>> R = CoordSys3D('R')
    >>> s1 = R.x*R.y*R.z
    >>> gradient(s1)
    R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> s2 = 5*R.x**2*R.z
    >>> gradient(s2)
    10*R.x*R.z*R.i + 5*R.x**2*R.k

    """
    coord_sys = _get_coord_sys_from_expr(scalar_field, coord_sys)

    if coord_sys is None:
        return Vector.zero
    else:
        from sympy.vector.functions import express
        h1, h2, h3 = coord_sys.lame_coefficients()
        i, j, k = coord_sys.base_vectors()
        x, y, z = coord_sys.base_scalars()
        scalar_field = express(scalar_field, coord_sys, variables=True)
        vx = Derivative(scalar_field, x) / h1
        vy = Derivative(scalar_field, y) / h2
        vz = Derivative(scalar_field, z) / h3

        if doit:
            return (vx * i + vy * j + vz * k).doit()
        return vx * i + vy * j + vz * k
Beispiel #21
0
def test_coordinate_vars():
    """
    Tests the coordinate variables functionality with respect to
    reorientation of coordinate systems.
    """
    assert BaseScalar('Ax', 0, A) == A.x
    assert BaseScalar('Ay', 1, A) == A.y
    assert BaseScalar('Az', 2, A) == A.z
    assert BaseScalar('Ax', 0, A).__hash__() == A.x.__hash__()
    assert isinstance(A.x, BaseScalar) and \
           isinstance(A.y, BaseScalar) and \
           isinstance(A.z, BaseScalar)
    assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z}
    assert A.x.system == A
    B = A.orient_new('B', 'Axis', [q, A.k])
    assert B.scalar_map(A) == {B.z: A.z, B.y: -A.x*sin(q) + A.y*cos(q),
                                 B.x: A.x*cos(q) + A.y*sin(q)}
    assert A.scalar_map(B) == {A.x: B.x*cos(q) - B.y*sin(q),
                                 A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z}
    assert express(B.x, A, variables=True) == A.x*cos(q) + A.y*sin(q)
    assert express(B.y, A, variables=True) == -A.x*sin(q) + A.y*cos(q)
    assert express(B.z, A, variables=True) == A.z
    assert express(B.x*B.y*B.z, A, variables=True) == \
           A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q))
    assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \
           (B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \
           B.y*cos(q))*A.j + B.z*A.k
    assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \
                            variables=True)) == \
           A.x*A.i + A.y*A.j + A.z*A.k
    assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \
           (A.x*cos(q) + A.y*sin(q))*B.i + \
           (-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k
    assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \
                            variables=True)) == \
           B.x*B.i + B.y*B.j + B.z*B.k
    N = B.orient_new('N', 'Axis', [-q, B.k])
    assert N.scalar_map(A) == \
           {N.x: A.x, N.z: A.z, N.y: A.y}
    C = A.orient_new('C', 'Axis', [q, A.i + A.j + A.k])
    mapping = A.scalar_map(C)
    assert mapping[A.x] == 2*C.x*cos(q)/3 + C.x/3 - \
           2*C.y*sin(q + pi/6)/3 + C.y/3 - 2*C.z*cos(q + pi/3)/3 + C.z/3
    assert mapping[A.y] == -2*C.x*cos(q + pi/3)/3 + \
           C.x/3 + 2*C.y*cos(q)/3 + C.y/3 - 2*C.z*sin(q + pi/6)/3 + C.z/3
    assert mapping[A.z] == -2*C.x*sin(q + pi/6)/3 + C.x/3 - \
           2*C.y*cos(q + pi/3)/3 + C.y/3 + 2*C.z*cos(q)/3 + C.z/3
Beispiel #22
0
 def directional_derivative(field):
     from sympy.vector.operators import _get_coord_sys_from_expr
     coord_sys = _get_coord_sys_from_expr(field)
     if coord_sys is not None:
         field = express(field, coord_sys, variables=True)
         out = self.dot(coord_sys._i) * df(field, coord_sys._x)
         out += self.dot(coord_sys._j) * df(field, coord_sys._y)
         out += self.dot(coord_sys._k) * df(field, coord_sys._z)
         if out == 0 and isinstance(field, Vector):
             out = Vector.zero
         return out
     elif isinstance(field, Vector) :
         return Vector.zero
     else:
         return S(0)
Beispiel #23
0
 def directional_derivative(field):
     from sympy.vector.operators import _get_coord_sys_from_expr
     coord_sys = _get_coord_sys_from_expr(field)
     if coord_sys is not None:
         field = express(field, coord_sys, variables=True)
         out = self.dot(coord_sys._i) * df(field, coord_sys._x)
         out += self.dot(coord_sys._j) * df(field, coord_sys._y)
         out += self.dot(coord_sys._k) * df(field, coord_sys._z)
         if out == 0 and isinstance(field, Vector):
             out = Vector.zero
         return out
     elif isinstance(field, Vector):
         return Vector.zero
     else:
         return S(0)
Beispiel #24
0
def gradient(scalar_field, coord_sys=None, doit=True):
    """
    Returns the vector gradient of a scalar field computed wrt the
    base scalars of the given coordinate system.

    Parameters
    ==========

    scalar_field : SymPy Expr
        The scalar field to compute the gradient of

    coord_sys : CoordSys3D
        The coordinate system to calculate the gradient in
        Deprecated since version 1.1

    doit : bool
        If True, the result is returned after calling .doit() on
        each component. Else, the returned expression contains
        Derivative instances

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, gradient
    >>> R = CoordSys3D('R')
    >>> s1 = R.x*R.y*R.z
    >>> gradient(s1)
    R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> s2 = 5*R.x**2*R.z
    >>> gradient(s2)
    10*R.x*R.z*R.i + 5*R.x**2*R.k

    """
    coord_sys = _get_coord_sys_from_expr(scalar_field, coord_sys)

    if coord_sys is None:
        return Vector.zero
    else:
        from sympy.vector.functions import express
        scalar_field = express(scalar_field, coord_sys,
                                   variables=True)
        vx = Derivative(scalar_field, coord_sys._x) / coord_sys._h1
        vy = Derivative(scalar_field, coord_sys._y) / coord_sys._h2
        vz = Derivative(scalar_field, coord_sys._z) / coord_sys._h3

        if doit:
            return (vx * coord_sys._i + vy * coord_sys._j + vz * coord_sys._k).doit()
        return vx * coord_sys._i + vy * coord_sys._j + vz * coord_sys._k
Beispiel #25
0
def _orient_axis(amounts, rot_order, parent):
    """
    Helper method for orientation using Axis method.
    """
    if not rot_order == '':
        raise TypeError('Axis orientation takes no' + 'rotation order')
    if not (isinstance(amounts, (list, tuple)) and (len(amounts) == 2)):
        raise TypeError('Amounts should be of length 2')
    theta = amounts[0]
    axis = amounts[1]
    axis = express(axis, parent).normalize()
    axis = axis.to_matrix(parent)
    parent_orient = ((eye(3) - axis * axis.T) * cos(theta) +
                     Matrix([[0, -axis[2], axis[1]], [axis[2], 0, -axis[0]],
                             [-axis[1], axis[0], 0]]) * sin(theta) +
                     axis * axis.T)
    return parent_orient
Beispiel #26
0
def _orient_axis(amounts, rot_order, parent):
    """
    Helper method for orientation using Axis method.
    """
    if not rot_order == "":
        raise TypeError("Axis orientation takes no" + "rotation order")
    if not (isinstance(amounts, (list, tuple)) and (len(amounts) == 2)):
        raise TypeError("Amounts should be of length 2")
    theta = amounts[0]
    axis = amounts[1]
    axis = express(axis, parent).normalize()
    axis = axis.to_matrix(parent)
    parent_orient = (
        (eye(3) - axis * axis.T) * cos(theta)
        + Matrix([[0, -axis[2], axis[1]], [axis[2], 0, -axis[0]], [-axis[1], axis[0], 0]]) * sin(theta)
        + axis * axis.T
    )
    return parent_orient
Beispiel #27
0
    def gradient(self, scalar_field, doit=False):
        """
        Returns the gradient of the given scalar field, as a
        Vector instance.

        Parameters
        ==========

        scalar_field : SymPy expression
            The scalar field to calculate the gradient of.

        doit : bool
            If True, the result is returned after calling .doit() on
            each component. Else, the returned expression contains
            Derivative instances

        Examples
        ========

        >>> from sympy.vector import CoordSysCartesian
        >>> C = CoordSysCartesian('C')
        >>> C.delop.gradient(9)
        (Derivative(9, C.x))*C.i + (Derivative(9, C.y))*C.j +
            (Derivative(9, C.z))*C.k
        >>> C.delop(C.x*C.y*C.z).doit()
        C.y*C.z*C.i + C.x*C.z*C.j + C.x*C.y*C.k

        """

        scalar_field = express(scalar_field, self.system,
                               variables=True)
        vx = Derivative(scalar_field, self._x)
        vy = Derivative(scalar_field, self._y)
        vz = Derivative(scalar_field, self._z)

        if doit:
            return (vx * self._i + vy * self._j + vz * self._k).doit()
        return vx * self._i + vy * self._j + vz * self._k
def test_coordinate_vars():
    """
    Tests the coordinate variables functionality with respect to
    reorientation of coordinate systems.
    """
    A = CoordSysCartesian("A")
    assert BaseScalar("Ax", 0, A, " ", " ") == A.x
    assert BaseScalar("Ay", 1, A, " ", " ") == A.y
    assert BaseScalar("Az", 2, A, " ", " ") == A.z
    assert BaseScalar("Ax", 0, A, " ", " ").__hash__() == A.x.__hash__()
    assert isinstance(A.x, BaseScalar) and isinstance(A.y, BaseScalar) and isinstance(A.z, BaseScalar)
    assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z}
    assert A.x.system == A
    B = A.orient_new_axis("B", q, A.k)
    assert B.scalar_map(A) == {B.z: A.z, B.y: -A.x * sin(q) + A.y * cos(q), B.x: A.x * cos(q) + A.y * sin(q)}
    assert A.scalar_map(B) == {A.x: B.x * cos(q) - B.y * sin(q), A.y: B.x * sin(q) + B.y * cos(q), A.z: B.z}
    assert express(B.x, A, variables=True) == A.x * cos(q) + A.y * sin(q)
    assert express(B.y, A, variables=True) == -A.x * sin(q) + A.y * cos(q)
    assert express(B.z, A, variables=True) == A.z
    assert express(B.x * B.y * B.z, A, variables=True) == A.z * (-A.x * sin(q) + A.y * cos(q)) * (
        A.x * cos(q) + A.y * sin(q)
    )
    assert (
        express(B.x * B.i + B.y * B.j + B.z * B.k, A)
        == (B.x * cos(q) - B.y * sin(q)) * A.i + (B.x * sin(q) + B.y * cos(q)) * A.j + B.z * A.k
    )
    assert simplify(express(B.x * B.i + B.y * B.j + B.z * B.k, A, variables=True)) == A.x * A.i + A.y * A.j + A.z * A.k
    assert (
        express(A.x * A.i + A.y * A.j + A.z * A.k, B)
        == (A.x * cos(q) + A.y * sin(q)) * B.i + (-A.x * sin(q) + A.y * cos(q)) * B.j + A.z * B.k
    )
    assert simplify(express(A.x * A.i + A.y * A.j + A.z * A.k, B, variables=True)) == B.x * B.i + B.y * B.j + B.z * B.k
    N = B.orient_new_axis("N", -q, B.k)
    assert N.scalar_map(A) == {N.x: A.x, N.z: A.z, N.y: A.y}
    C = A.orient_new_axis("C", q, A.i + A.j + A.k)
    mapping = A.scalar_map(C)
    assert (
        mapping[A.x]
        == 2 * C.x * cos(q) / 3
        + C.x / 3
        - 2 * C.y * sin(q + pi / 6) / 3
        + C.y / 3
        - 2 * C.z * cos(q + pi / 3) / 3
        + C.z / 3
    )
    assert (
        mapping[A.y]
        == -2 * C.x * cos(q + pi / 3) / 3
        + C.x / 3
        + 2 * C.y * cos(q) / 3
        + C.y / 3
        - 2 * C.z * sin(q + pi / 6) / 3
        + C.z / 3
    )
    assert (
        mapping[A.z]
        == -2 * C.x * sin(q + pi / 6) / 3
        + C.x / 3
        - 2 * C.y * cos(q + pi / 3) / 3
        + C.y / 3
        + 2 * C.z * cos(q) / 3
        + C.z / 3
    )
    D = A.locate_new("D", a * A.i + b * A.j + c * A.k)
    assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b}
    E = A.orient_new_axis("E", a, A.k, a * A.i + b * A.j + c * A.k)
    assert A.scalar_map(E) == {A.z: E.z + c, A.x: E.x * cos(a) - E.y * sin(a) + a, A.y: E.x * sin(a) + E.y * cos(a) + b}
    assert E.scalar_map(A) == {
        E.x: (A.x - a) * cos(a) + (A.y - b) * sin(a),
        E.y: (-A.x + a) * sin(a) + (A.y - b) * cos(a),
        E.z: A.z - c,
    }
    F = A.locate_new("F", Vector.zero)
    assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
Beispiel #29
0
def test_vector_with_orientation():
    """
    Tests the effects of orientation of coordinate systems on
    basic vector operations.
    """
    N = CoordSys3D("N")
    A = N.orient_new_axis("A", q1, N.k)
    B = A.orient_new_axis("B", q2, A.i)
    C = B.orient_new_axis("C", q3, B.j)

    # Test to_matrix
    v1 = a * N.i + b * N.j + c * N.k
    assert v1.to_matrix(A) == Matrix([[a * cos(q1) + b * sin(q1)],
                                      [-a * sin(q1) + b * cos(q1)], [c]])

    # Test dot
    assert N.i.dot(A.i) == cos(q1)
    assert N.i.dot(A.j) == -sin(q1)
    assert N.i.dot(A.k) == 0
    assert N.j.dot(A.i) == sin(q1)
    assert N.j.dot(A.j) == cos(q1)
    assert N.j.dot(A.k) == 0
    assert N.k.dot(A.i) == 0
    assert N.k.dot(A.j) == 0
    assert N.k.dot(A.k) == 1

    assert N.i.dot(A.i + A.j) == -sin(q1) + cos(q1) == (A.i + A.j).dot(N.i)

    assert A.i.dot(C.i) == cos(q3)
    assert A.i.dot(C.j) == 0
    assert A.i.dot(C.k) == sin(q3)
    assert A.j.dot(C.i) == sin(q2) * sin(q3)
    assert A.j.dot(C.j) == cos(q2)
    assert A.j.dot(C.k) == -sin(q2) * cos(q3)
    assert A.k.dot(C.i) == -cos(q2) * sin(q3)
    assert A.k.dot(C.j) == sin(q2)
    assert A.k.dot(C.k) == cos(q2) * cos(q3)

    # Test cross
    assert N.i.cross(A.i) == sin(q1) * A.k
    assert N.i.cross(A.j) == cos(q1) * A.k
    assert N.i.cross(A.k) == -sin(q1) * A.i - cos(q1) * A.j
    assert N.j.cross(A.i) == -cos(q1) * A.k
    assert N.j.cross(A.j) == sin(q1) * A.k
    assert N.j.cross(A.k) == cos(q1) * A.i - sin(q1) * A.j
    assert N.k.cross(A.i) == A.j
    assert N.k.cross(A.j) == -A.i
    assert N.k.cross(A.k) == Vector.zero

    assert N.i.cross(A.i) == sin(q1) * A.k
    assert N.i.cross(A.j) == cos(q1) * A.k
    assert N.i.cross(A.i + A.j) == sin(q1) * A.k + cos(q1) * A.k
    assert (A.i + A.j).cross(N.i) == (-sin(q1) - cos(q1)) * N.k

    assert A.i.cross(C.i) == sin(q3) * C.j
    assert A.i.cross(C.j) == -sin(q3) * C.i + cos(q3) * C.k
    assert A.i.cross(C.k) == -cos(q3) * C.j
    assert C.i.cross(
        A.i) == (-sin(q3) * cos(q2)) * A.j + (-sin(q2) * sin(q3)) * A.k
    assert C.j.cross(A.i) == (sin(q2)) * A.j + (-cos(q2)) * A.k
    assert express(C.k.cross(A.i), C).trigsimp() == cos(q3) * C.j
Beispiel #30
0
def test_express():
    assert express(Vector.zero, N) == Vector.zero
    assert express(S.Zero, N) is S.Zero
    assert express(A.i, C) == cos(q3) * C.i + sin(q3) * C.k
    assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \
        sin(q2)*cos(q3)*C.k
    assert express(A.k, C) == -sin(q3)*cos(q2)*C.i + sin(q2)*C.j + \
        cos(q2)*cos(q3)*C.k
    assert express(A.i, N) == cos(q1) * N.i + sin(q1) * N.j
    assert express(A.j, N) == -sin(q1) * N.i + cos(q1) * N.j
    assert express(A.k, N) == N.k
    assert express(A.i, A) == A.i
    assert express(A.j, A) == A.j
    assert express(A.k, A) == A.k
    assert express(A.i, B) == B.i
    assert express(A.j, B) == cos(q2) * B.j - sin(q2) * B.k
    assert express(A.k, B) == sin(q2) * B.j + cos(q2) * B.k
    assert express(A.i, C) == cos(q3) * C.i + sin(q3) * C.k
    assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \
        sin(q2)*cos(q3)*C.k
    assert express(A.k, C) == -sin(q3)*cos(q2)*C.i + sin(q2)*C.j + \
        cos(q2)*cos(q3)*C.k
    # Check to make sure UnitVectors get converted properly
    assert express(N.i, N) == N.i
    assert express(N.j, N) == N.j
    assert express(N.k, N) == N.k
    assert express(N.i, A) == (cos(q1) * A.i - sin(q1) * A.j)
    assert express(N.j, A) == (sin(q1) * A.i + cos(q1) * A.j)
    assert express(N.k, A) == A.k
    assert express(N.i, B) == (cos(q1) * B.i - sin(q1) * cos(q2) * B.j +
                               sin(q1) * sin(q2) * B.k)
    assert express(N.j, B) == (sin(q1) * B.i + cos(q1) * cos(q2) * B.j -
                               sin(q2) * cos(q1) * B.k)
    assert express(N.k, B) == (sin(q2) * B.j + cos(q2) * B.k)
    assert express(
        N.i, C) == ((cos(q1) * cos(q3) - sin(q1) * sin(q2) * sin(q3)) * C.i -
                    sin(q1) * cos(q2) * C.j +
                    (sin(q3) * cos(q1) + sin(q1) * sin(q2) * cos(q3)) * C.k)
    assert express(
        N.j, C) == ((sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1)) * C.i +
                    cos(q1) * cos(q2) * C.j +
                    (sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)) * C.k)
    assert express(N.k, C) == (-sin(q3) * cos(q2) * C.i + sin(q2) * C.j +
                               cos(q2) * cos(q3) * C.k)

    assert express(A.i, N) == (cos(q1) * N.i + sin(q1) * N.j)
    assert express(A.j, N) == (-sin(q1) * N.i + cos(q1) * N.j)
    assert express(A.k, N) == N.k
    assert express(A.i, A) == A.i
    assert express(A.j, A) == A.j
    assert express(A.k, A) == A.k
    assert express(A.i, B) == B.i
    assert express(A.j, B) == (cos(q2) * B.j - sin(q2) * B.k)
    assert express(A.k, B) == (sin(q2) * B.j + cos(q2) * B.k)
    assert express(A.i, C) == (cos(q3) * C.i + sin(q3) * C.k)
    assert express(A.j, C) == (sin(q2) * sin(q3) * C.i + cos(q2) * C.j -
                               sin(q2) * cos(q3) * C.k)
    assert express(A.k, C) == (-sin(q3) * cos(q2) * C.i + sin(q2) * C.j +
                               cos(q2) * cos(q3) * C.k)

    assert express(B.i, N) == (cos(q1) * N.i + sin(q1) * N.j)
    assert express(B.j, N) == (-sin(q1) * cos(q2) * N.i +
                               cos(q1) * cos(q2) * N.j + sin(q2) * N.k)
    assert express(B.k, N) == (sin(q1) * sin(q2) * N.i -
                               sin(q2) * cos(q1) * N.j + cos(q2) * N.k)
    assert express(B.i, A) == A.i
    assert express(B.j, A) == (cos(q2) * A.j + sin(q2) * A.k)
    assert express(B.k, A) == (-sin(q2) * A.j + cos(q2) * A.k)
    assert express(B.i, B) == B.i
    assert express(B.j, B) == B.j
    assert express(B.k, B) == B.k
    assert express(B.i, C) == (cos(q3) * C.i + sin(q3) * C.k)
    assert express(B.j, C) == C.j
    assert express(B.k, C) == (-sin(q3) * C.i + cos(q3) * C.k)

    assert express(
        C.i, N) == ((cos(q1) * cos(q3) - sin(q1) * sin(q2) * sin(q3)) * N.i +
                    (sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1)) * N.j -
                    sin(q3) * cos(q2) * N.k)
    assert express(C.j, N) == (-sin(q1) * cos(q2) * N.i +
                               cos(q1) * cos(q2) * N.j + sin(q2) * N.k)
    assert express(
        C.k, N) == ((sin(q3) * cos(q1) + sin(q1) * sin(q2) * cos(q3)) * N.i +
                    (sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)) * N.j +
                    cos(q2) * cos(q3) * N.k)
    assert express(C.i, A) == (cos(q3) * A.i + sin(q2) * sin(q3) * A.j -
                               sin(q3) * cos(q2) * A.k)
    assert express(C.j, A) == (cos(q2) * A.j + sin(q2) * A.k)
    assert express(C.k, A) == (sin(q3) * A.i - sin(q2) * cos(q3) * A.j +
                               cos(q2) * cos(q3) * A.k)
    assert express(C.i, B) == (cos(q3) * B.i - sin(q3) * B.k)
    assert express(C.j, B) == B.j
    assert express(C.k, B) == (sin(q3) * B.i + cos(q3) * B.k)
    assert express(C.i, C) == C.i
    assert express(C.j, C) == C.j
    assert express(C.k, C) == C.k == (C.k)

    #  Check to make sure Vectors get converted back to UnitVectors
    assert N.i == express((cos(q1) * A.i - sin(q1) * A.j), N).simplify()
    assert N.j == express((sin(q1) * A.i + cos(q1) * A.j), N).simplify()
    assert N.i == express(
        (cos(q1) * B.i - sin(q1) * cos(q2) * B.j + sin(q1) * sin(q2) * B.k),
        N).simplify()
    assert N.j == express(
        (sin(q1) * B.i + cos(q1) * cos(q2) * B.j - sin(q2) * cos(q1) * B.k),
        N).simplify()
    assert N.k == express((sin(q2) * B.j + cos(q2) * B.k), N).simplify()

    assert A.i == express((cos(q1) * N.i + sin(q1) * N.j), A).simplify()
    assert A.j == express((-sin(q1) * N.i + cos(q1) * N.j), A).simplify()

    assert A.j == express((cos(q2) * B.j - sin(q2) * B.k), A).simplify()
    assert A.k == express((sin(q2) * B.j + cos(q2) * B.k), A).simplify()

    assert A.i == express((cos(q3) * C.i + sin(q3) * C.k), A).simplify()
    assert A.j == express(
        (sin(q2) * sin(q3) * C.i + cos(q2) * C.j - sin(q2) * cos(q3) * C.k),
        A).simplify()

    assert A.k == express(
        (-sin(q3) * cos(q2) * C.i + sin(q2) * C.j + cos(q2) * cos(q3) * C.k),
        A).simplify()
    assert B.i == express((cos(q1) * N.i + sin(q1) * N.j), B).simplify()
    assert B.j == express(
        (-sin(q1) * cos(q2) * N.i + cos(q1) * cos(q2) * N.j + sin(q2) * N.k),
        B).simplify()

    assert B.k == express(
        (sin(q1) * sin(q2) * N.i - sin(q2) * cos(q1) * N.j + cos(q2) * N.k),
        B).simplify()

    assert B.j == express((cos(q2) * A.j + sin(q2) * A.k), B).simplify()
    assert B.k == express((-sin(q2) * A.j + cos(q2) * A.k), B).simplify()
    assert B.i == express((cos(q3) * C.i + sin(q3) * C.k), B).simplify()
    assert B.k == express((-sin(q3) * C.i + cos(q3) * C.k), B).simplify()
    assert C.i == express(
        (cos(q3) * A.i + sin(q2) * sin(q3) * A.j - sin(q3) * cos(q2) * A.k),
        C).simplify()
    assert C.j == express((cos(q2) * A.j + sin(q2) * A.k), C).simplify()
    assert C.k == express(
        (sin(q3) * A.i - sin(q2) * cos(q3) * A.j + cos(q2) * cos(q3) * A.k),
        C).simplify()
    assert C.i == express((cos(q3) * B.i - sin(q3) * B.k), C).simplify()
    assert C.k == express((sin(q3) * B.i + cos(q3) * B.k), C).simplify()
Beispiel #31
0
def test_express():
    assert express(Vector.zero, N) == Vector.zero
    assert express(S(0), N) == S(0)
    assert express(A.i, C) == cos(q3)*C.i + sin(q3)*C.k
    assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \
        sin(q2)*cos(q3)*C.k
    assert express(A.k, C) == -sin(q3)*cos(q2)*C.i + sin(q2)*C.j + \
        cos(q2)*cos(q3)*C.k
    assert express(A.i, N) == cos(q1)*N.i + sin(q1)*N.j
    assert express(A.j, N) == -sin(q1)*N.i + cos(q1)*N.j
    assert express(A.k, N) == N.k
    assert express(A.i, A) == A.i
    assert express(A.j, A) == A.j
    assert express(A.k, A) == A.k
    assert express(A.i, B) == B.i
    assert express(A.j, B) == cos(q2)*B.j - sin(q2)*B.k
    assert express(A.k, B) == sin(q2)*B.j + cos(q2)*B.k
    assert express(A.i, C) == cos(q3)*C.i + sin(q3)*C.k
    assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \
        sin(q2)*cos(q3)*C.k
    assert express(A.k, C) == -sin(q3)*cos(q2)*C.i + sin(q2)*C.j + \
        cos(q2)*cos(q3)*C.k
    # Check to make sure UnitVectors get converted properly
    assert express(N.i, N) == N.i
    assert express(N.j, N) == N.j
    assert express(N.k, N) == N.k
    assert express(N.i, A) == (cos(q1)*A.i - sin(q1)*A.j)
    assert express(N.j, A) == (sin(q1)*A.i + cos(q1)*A.j)
    assert express(N.k, A) == A.k
    assert express(N.i, B) == (cos(q1)*B.i - sin(q1)*cos(q2)*B.j +
            sin(q1)*sin(q2)*B.k)
    assert express(N.j, B) == (sin(q1)*B.i + cos(q1)*cos(q2)*B.j -
            sin(q2)*cos(q1)*B.k)
    assert express(N.k, B) == (sin(q2)*B.j + cos(q2)*B.k)
    assert express(N.i, C) == (
        (cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*C.i -
        sin(q1)*cos(q2)*C.j +
        (sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*C.k)
    assert express(N.j, C) == (
        (sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*C.i +
        cos(q1)*cos(q2)*C.j +
        (sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*C.k)
    assert express(N.k, C) == (-sin(q3)*cos(q2)*C.i + sin(q2)*C.j +
            cos(q2)*cos(q3)*C.k)

    assert express(A.i, N) == (cos(q1)*N.i + sin(q1)*N.j)
    assert express(A.j, N) == (-sin(q1)*N.i + cos(q1)*N.j)
    assert express(A.k, N) == N.k
    assert express(A.i, A) == A.i
    assert express(A.j, A) == A.j
    assert express(A.k, A) == A.k
    assert express(A.i, B) == B.i
    assert express(A.j, B) == (cos(q2)*B.j - sin(q2)*B.k)
    assert express(A.k, B) == (sin(q2)*B.j + cos(q2)*B.k)
    assert express(A.i, C) == (cos(q3)*C.i + sin(q3)*C.k)
    assert express(A.j, C) == (sin(q2)*sin(q3)*C.i + cos(q2)*C.j -
            sin(q2)*cos(q3)*C.k)
    assert express(A.k, C) == (-sin(q3)*cos(q2)*C.i + sin(q2)*C.j +
            cos(q2)*cos(q3)*C.k)

    assert express(B.i, N) == (cos(q1)*N.i + sin(q1)*N.j)
    assert express(B.j, N) == (-sin(q1)*cos(q2)*N.i +
            cos(q1)*cos(q2)*N.j + sin(q2)*N.k)
    assert express(B.k, N) == (sin(q1)*sin(q2)*N.i -
            sin(q2)*cos(q1)*N.j + cos(q2)*N.k)
    assert express(B.i, A) == A.i
    assert express(B.j, A) == (cos(q2)*A.j + sin(q2)*A.k)
    assert express(B.k, A) == (-sin(q2)*A.j + cos(q2)*A.k)
    assert express(B.i, B) == B.i
    assert express(B.j, B) == B.j
    assert express(B.k, B) == B.k
    assert express(B.i, C) == (cos(q3)*C.i + sin(q3)*C.k)
    assert express(B.j, C) == C.j
    assert express(B.k, C) == (-sin(q3)*C.i + cos(q3)*C.k)

    assert express(C.i, N) == (
        (cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*N.i +
        (sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*N.j -
        sin(q3)*cos(q2)*N.k)
    assert express(C.j, N) == (
        -sin(q1)*cos(q2)*N.i + cos(q1)*cos(q2)*N.j + sin(q2)*N.k)
    assert express(C.k, N) == (
        (sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*N.i +
        (sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*N.j +
        cos(q2)*cos(q3)*N.k)
    assert express(C.i, A) == (cos(q3)*A.i + sin(q2)*sin(q3)*A.j -
            sin(q3)*cos(q2)*A.k)
    assert express(C.j, A) == (cos(q2)*A.j + sin(q2)*A.k)
    assert express(C.k, A) == (sin(q3)*A.i - sin(q2)*cos(q3)*A.j +
            cos(q2)*cos(q3)*A.k)
    assert express(C.i, B) == (cos(q3)*B.i - sin(q3)*B.k)
    assert express(C.j, B) == B.j
    assert express(C.k, B) == (sin(q3)*B.i + cos(q3)*B.k)
    assert express(C.i, C) == C.i
    assert express(C.j, C) == C.j
    assert express(C.k, C) == C.k == (C.k)

    #  Check to make sure Vectors get converted back to UnitVectors
    assert N.i == express((cos(q1)*A.i - sin(q1)*A.j), N).simplify()
    assert N.j == express((sin(q1)*A.i + cos(q1)*A.j), N).simplify()
    assert N.i == express((cos(q1)*B.i - sin(q1)*cos(q2)*B.j +
            sin(q1)*sin(q2)*B.k), N).simplify()
    assert N.j == express((sin(q1)*B.i + cos(q1)*cos(q2)*B.j -
        sin(q2)*cos(q1)*B.k), N).simplify()
    assert N.k == express((sin(q2)*B.j + cos(q2)*B.k), N).simplify()


    assert A.i == express((cos(q1)*N.i + sin(q1)*N.j), A).simplify()
    assert A.j == express((-sin(q1)*N.i + cos(q1)*N.j), A).simplify()

    assert A.j == express((cos(q2)*B.j - sin(q2)*B.k), A).simplify()
    assert A.k == express((sin(q2)*B.j + cos(q2)*B.k), A).simplify()

    assert A.i == express((cos(q3)*C.i + sin(q3)*C.k), A).simplify()
    assert A.j == express((sin(q2)*sin(q3)*C.i + cos(q2)*C.j -
            sin(q2)*cos(q3)*C.k), A).simplify()

    assert A.k == express((-sin(q3)*cos(q2)*C.i + sin(q2)*C.j +
            cos(q2)*cos(q3)*C.k), A).simplify()
    assert B.i == express((cos(q1)*N.i + sin(q1)*N.j), B).simplify()
    assert B.j == express((-sin(q1)*cos(q2)*N.i +
            cos(q1)*cos(q2)*N.j + sin(q2)*N.k), B).simplify()

    assert B.k == express((sin(q1)*sin(q2)*N.i -
            sin(q2)*cos(q1)*N.j + cos(q2)*N.k), B).simplify()

    assert B.j == express((cos(q2)*A.j + sin(q2)*A.k), B).simplify()
    assert B.k == express((-sin(q2)*A.j + cos(q2)*A.k), B).simplify()
    assert B.i == express((cos(q3)*C.i + sin(q3)*C.k), B).simplify()
    assert B.k == express((-sin(q3)*C.i + cos(q3)*C.k), B).simplify()
    assert C.i == express((cos(q3)*A.i + sin(q2)*sin(q3)*A.j -
            sin(q3)*cos(q2)*A.k), C).simplify()
    assert C.j == express((cos(q2)*A.j + sin(q2)*A.k), C).simplify()
    assert C.k == express((sin(q3)*A.i - sin(q2)*cos(q3)*A.j +
            cos(q2)*cos(q3)*A.k), C).simplify()
    assert C.i == express((cos(q3)*B.i - sin(q3)*B.k), C).simplify()
    assert C.k == express((sin(q3)*B.i + cos(q3)*B.k), C).simplify()
Beispiel #32
0
def test_coordinate_vars():
    """
    Tests the coordinate variables functionality with respect to
    reorientation of coordinate systems.
    """
    A = CoordSysCartesian('A')
    assert BaseScalar('Ax', 0, A, ' ', ' ') == A.x
    assert BaseScalar('Ay', 1, A, ' ', ' ') == A.y
    assert BaseScalar('Az', 2, A, ' ', ' ') == A.z
    assert BaseScalar('Ax', 0, A, ' ', ' ').__hash__() == A.x.__hash__()
    assert isinstance(A.x, BaseScalar) and \
           isinstance(A.y, BaseScalar) and \
           isinstance(A.z, BaseScalar)
    assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z}
    assert A.x.system == A
    B = A.orient_new_axis('B', q, A.k)
    assert B.scalar_map(A) == {
        B.z: A.z,
        B.y: -A.x * sin(q) + A.y * cos(q),
        B.x: A.x * cos(q) + A.y * sin(q)
    }
    assert A.scalar_map(B) == {
        A.x: B.x * cos(q) - B.y * sin(q),
        A.y: B.x * sin(q) + B.y * cos(q),
        A.z: B.z
    }
    assert express(B.x, A, variables=True) == A.x * cos(q) + A.y * sin(q)
    assert express(B.y, A, variables=True) == -A.x * sin(q) + A.y * cos(q)
    assert express(B.z, A, variables=True) == A.z
    assert express(B.x*B.y*B.z, A, variables=True) == \
           A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q))
    assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \
           (B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \
           B.y*cos(q))*A.j + B.z*A.k
    assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \
                            variables=True)) == \
           A.x*A.i + A.y*A.j + A.z*A.k
    assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \
           (A.x*cos(q) + A.y*sin(q))*B.i + \
           (-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k
    assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \
                            variables=True)) == \
           B.x*B.i + B.y*B.j + B.z*B.k
    N = B.orient_new_axis('N', -q, B.k)
    assert N.scalar_map(A) == \
           {N.x: A.x, N.z: A.z, N.y: A.y}
    C = A.orient_new_axis('C', q, A.i + A.j + A.k)
    mapping = A.scalar_map(C)
    assert mapping[A.x] == 2*C.x*cos(q)/3 + C.x/3 - \
           2*C.y*sin(q + pi/6)/3 + C.y/3 - 2*C.z*cos(q + pi/3)/3 + C.z/3
    assert mapping[A.y] == -2*C.x*cos(q + pi/3)/3 + \
           C.x/3 + 2*C.y*cos(q)/3 + C.y/3 - 2*C.z*sin(q + pi/6)/3 + C.z/3
    assert mapping[A.z] == -2*C.x*sin(q + pi/6)/3 + C.x/3 - \
           2*C.y*cos(q + pi/3)/3 + C.y/3 + 2*C.z*cos(q)/3 + C.z/3
    D = A.locate_new('D', a * A.i + b * A.j + c * A.k)
    assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b}
    E = A.orient_new_axis('E', a, A.k, a * A.i + b * A.j + c * A.k)
    assert A.scalar_map(E) == {
        A.z: E.z + c,
        A.x: E.x * cos(a) - E.y * sin(a) + a,
        A.y: E.x * sin(a) + E.y * cos(a) + b
    }
    assert E.scalar_map(A) == {
        E.x: (A.x - a) * cos(a) + (A.y - b) * sin(a),
        E.y: (-A.x + a) * sin(a) + (A.y - b) * cos(a),
        E.z: A.z - c
    }
    F = A.locate_new('F', Vector.zero)
    assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
Beispiel #33
0
    def dot(self, other):
        """
        Returns the dot product of this Vector, either with another
        Vector, or a Dyadic, or a Del operator.
        If 'other' is a Vector, returns the dot product scalar (Sympy
        expression).
        If 'other' is a Dyadic, the dot product is returned as a Vector.
        If 'other' is an instance of Del, returns the directional
        derivate operator as a Python function. If this function is
        applied to a scalar expression, it returns the directional
        derivative of the scalar field wrt this Vector.

        Parameters
        ==========

        other: Vector/Dyadic/Del
            The Vector or Dyadic we are dotting with, or a Del operator .

        Examples
        ========

        >>> from sympy.vector import CoordSysCartesian
        >>> C = CoordSysCartesian('C')
        >>> C.i.dot(C.j)
        0
        >>> C.i & C.i
        1
        >>> v = 3*C.i + 4*C.j + 5*C.k
        >>> v.dot(C.k)
        5
        >>> (C.i & C.delop)(C.x*C.y*C.z)
        C.y*C.z
        >>> d = C.i.outer(C.i)
        >>> C.i.dot(d)
        C.i

        """

        from sympy.vector.functions import express
        #Check special cases
        if isinstance(other, Dyadic):
            if isinstance(self, VectorZero):
                return Vector.zero
            outvec = Vector.zero
            for k, v in other.components.items():
                vect_dot = k.args[0].dot(self)
                outvec += vect_dot * v * k.args[1]
            return outvec
        from sympy.vector.deloperator import Del
        if not isinstance(other, Vector) and not isinstance(other, Del):
            raise TypeError(str(other) + " is not a vector, dyadic or " +
                            "del operator")

        #Check if the other is a del operator
        if isinstance(other, Del):
            def directional_derivative(field):
                field = express(field, other.system, variables = True)
                out = self.dot(other._i) * df(field, other._x)
                out += self.dot(other._j) * df(field, other._y)
                out += self.dot(other._k) * df(field, other._z)
                if out == 0 and isinstance(field, Vector):
                    out = Vector.zero
                return out
            return directional_derivative

        if isinstance(self, VectorZero) or isinstance(other, VectorZero):
            return S(0)

        v1 = express(self, other._sys)
        v2 = express(other, other._sys)
        dotproduct = S(0)
        for x in other._sys.base_vectors():
            dotproduct += (v1.components.get(x, 0) *
                           v2.components.get(x, 0))

        return dotproduct
Beispiel #34
0
    def dot(self, other):
        """
        Returns the dot product of this Vector, either with another
        Vector, or a Dyadic, or a Del operator.
        If 'other' is a Vector, returns the dot product scalar (Sympy
        expression).
        If 'other' is a Dyadic, the dot product is returned as a Vector.
        If 'other' is an instance of Del, returns the directional
        derivate operator as a Python function. If this function is
        applied to a scalar expression, it returns the directional
        derivative of the scalar field wrt this Vector.

        Parameters
        ==========

        other: Vector/Dyadic/Del
            The Vector or Dyadic we are dotting with, or a Del operator .

        Examples
        ========

        >>> from sympy.vector import CoordSysCartesian
        >>> C = CoordSysCartesian('C')
        >>> C.i.dot(C.j)
        0
        >>> C.i & C.i
        1
        >>> v = 3*C.i + 4*C.j + 5*C.k
        >>> v.dot(C.k)
        5
        >>> (C.i & C.delop)(C.x*C.y*C.z)
        C.y*C.z
        >>> d = C.i.outer(C.i)
        >>> C.i.dot(d)
        C.i

        """

        from sympy.vector.functions import express
        #Check special cases
        if isinstance(other, Dyadic):
            if isinstance(self, VectorZero):
                return Vector.zero
            outvec = Vector.zero
            for k, v in other.components.items():
                vect_dot = k.args[0].dot(self)
                outvec += vect_dot * v * k.args[1]
            return outvec
        from sympy.vector.deloperator import Del
        if not isinstance(other, Vector) and not isinstance(other, Del):
            raise TypeError(
                str(other) + " is not a vector, dyadic or " + "del operator")

        #Check if the other is a del operator
        if isinstance(other, Del):

            def directional_derivative(field):
                field = express(field, other.system, variables=True)
                out = self.dot(other._i) * df(field, other._x)
                out += self.dot(other._j) * df(field, other._y)
                out += self.dot(other._k) * df(field, other._z)
                if out == 0 and isinstance(field, Vector):
                    out = Vector.zero
                return out

            return directional_derivative

        if isinstance(self, VectorZero) or isinstance(other, VectorZero):
            return S(0)

        v1 = express(self, other._sys)
        v2 = express(other, other._sys)
        dotproduct = S(0)
        for x in other._sys.base_vectors():
            dotproduct += (v1.components.get(x, 0) * v2.components.get(x, 0))

        return dotproduct
Beispiel #35
0
def test_coordinate_vars():
    """
    Tests the coordinate variables functionality with respect to
    reorientation of coordinate systems.
    """
    A = CoordSys3D('A')
    # Note that the name given on the lhs is different from A.x._name
    assert BaseScalar('A.x', 0, A, 'A_x', r'\mathbf{{x}_{A}}') == A.x
    assert BaseScalar('A.y', 1, A, 'A_y', r'\mathbf{{y}_{A}}') == A.y
    assert BaseScalar('A.z', 2, A, 'A_z', r'\mathbf{{z}_{A}}') == A.z
    assert BaseScalar('A.x', 0, A, 'A_x',
                      r'\mathbf{{x}_{A}}').__hash__() == A.x.__hash__()
    assert isinstance(A.x, BaseScalar) and \
           isinstance(A.y, BaseScalar) and \
           isinstance(A.z, BaseScalar)
    assert A.x * A.y == A.y * A.x
    assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z}
    assert A.x.system == A
    assert A.x.diff(A.x) == 1
    B = A.orient_new_axis('B', q, A.k)
    assert B.scalar_map(A) == {
        B.z: A.z,
        B.y: -A.x * sin(q) + A.y * cos(q),
        B.x: A.x * cos(q) + A.y * sin(q)
    }
    assert A.scalar_map(B) == {
        A.x: B.x * cos(q) - B.y * sin(q),
        A.y: B.x * sin(q) + B.y * cos(q),
        A.z: B.z
    }
    assert express(B.x, A, variables=True) == A.x * cos(q) + A.y * sin(q)
    assert express(B.y, A, variables=True) == -A.x * sin(q) + A.y * cos(q)
    assert express(B.z, A, variables=True) == A.z
    assert expand(express(B.x*B.y*B.z, A, variables=True)) == \
           expand(A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q)))
    assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \
           (B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \
           B.y*cos(q))*A.j + B.z*A.k
    assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \
                            variables=True)) == \
           A.x*A.i + A.y*A.j + A.z*A.k
    assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \
           (A.x*cos(q) + A.y*sin(q))*B.i + \
           (-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k
    assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \
                            variables=True)) == \
           B.x*B.i + B.y*B.j + B.z*B.k
    N = B.orient_new_axis('N', -q, B.k)
    assert N.scalar_map(A) == \
           {N.x: A.x, N.z: A.z, N.y: A.y}
    C = A.orient_new_axis('C', q, A.i + A.j + A.k)
    mapping = A.scalar_map(C)
    assert mapping[A.x].equals(C.x * (2 * cos(q) + 1) / 3 + C.y *
                               (-2 * sin(q + pi / 6) + 1) / 3 + C.z *
                               (-2 * cos(q + pi / 3) + 1) / 3)
    assert mapping[A.y].equals(C.x * (-2 * cos(q + pi / 3) + 1) / 3 + C.y *
                               (2 * cos(q) + 1) / 3 + C.z *
                               (-2 * sin(q + pi / 6) + 1) / 3)
    assert mapping[A.z].equals(C.x * (-2 * sin(q + pi / 6) + 1) / 3 + C.y *
                               (-2 * cos(q + pi / 3) + 1) / 3 + C.z *
                               (2 * cos(q) + 1) / 3)
    D = A.locate_new('D', a * A.i + b * A.j + c * A.k)
    assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b}
    E = A.orient_new_axis('E', a, A.k, a * A.i + b * A.j + c * A.k)
    assert A.scalar_map(E) == {
        A.z: E.z + c,
        A.x: E.x * cos(a) - E.y * sin(a) + a,
        A.y: E.x * sin(a) + E.y * cos(a) + b
    }
    assert E.scalar_map(A) == {
        E.x: (A.x - a) * cos(a) + (A.y - b) * sin(a),
        E.y: (-A.x + a) * sin(a) + (A.y - b) * cos(a),
        E.z: A.z - c
    }
    F = A.locate_new('F', Vector.zero)
    assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
Beispiel #36
0
def test_coordinate_vars():
    """
    Tests the coordinate variables functionality with respect to
    reorientation of coordinate systems.
    """
    A = CoordSys3D("A")
    # Note that the name given on the lhs is different from A.x._name
    assert BaseScalar(0, A, "A_x", r"\mathbf{{x}_{A}}") == A.x
    assert BaseScalar(1, A, "A_y", r"\mathbf{{y}_{A}}") == A.y
    assert BaseScalar(2, A, "A_z", r"\mathbf{{z}_{A}}") == A.z
    assert BaseScalar(0, A, "A_x",
                      r"\mathbf{{x}_{A}}").__hash__() == A.x.__hash__()
    assert (isinstance(A.x, BaseScalar) and isinstance(A.y, BaseScalar)
            and isinstance(A.z, BaseScalar))
    assert A.x * A.y == A.y * A.x
    assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z}
    assert A.x.system == A
    assert A.x.diff(A.x) == 1
    B = A.orient_new_axis("B", q, A.k)
    assert B.scalar_map(A) == {
        B.z: A.z,
        B.y: -A.x * sin(q) + A.y * cos(q),
        B.x: A.x * cos(q) + A.y * sin(q),
    }
    assert A.scalar_map(B) == {
        A.x: B.x * cos(q) - B.y * sin(q),
        A.y: B.x * sin(q) + B.y * cos(q),
        A.z: B.z,
    }
    assert express(B.x, A, variables=True) == A.x * cos(q) + A.y * sin(q)
    assert express(B.y, A, variables=True) == -A.x * sin(q) + A.y * cos(q)
    assert express(B.z, A, variables=True) == A.z
    assert expand(express(B.x * B.y * B.z, A, variables=True)) == expand(
        A.z * (-A.x * sin(q) + A.y * cos(q)) * (A.x * cos(q) + A.y * sin(q)))
    assert (express(B.x * B.i + B.y * B.j + B.z * B.k,
                    A) == (B.x * cos(q) - B.y * sin(q)) * A.i +
            (B.x * sin(q) + B.y * cos(q)) * A.j + B.z * A.k)
    assert (simplify(
        express(B.x * B.i + B.y * B.j + B.z * B.k, A,
                variables=True)) == A.x * A.i + A.y * A.j + A.z * A.k)
    assert (express(A.x * A.i + A.y * A.j + A.z * A.k,
                    B) == (A.x * cos(q) + A.y * sin(q)) * B.i +
            (-A.x * sin(q) + A.y * cos(q)) * B.j + A.z * B.k)
    assert (simplify(
        express(A.x * A.i + A.y * A.j + A.z * A.k, B,
                variables=True)) == B.x * B.i + B.y * B.j + B.z * B.k)
    N = B.orient_new_axis("N", -q, B.k)
    assert N.scalar_map(A) == {N.x: A.x, N.z: A.z, N.y: A.y}
    C = A.orient_new_axis("C", q, A.i + A.j + A.k)
    mapping = A.scalar_map(C)
    assert mapping[A.x].equals(C.x * (2 * cos(q) + 1) / 3 + C.y *
                               (-2 * sin(q + pi / 6) + 1) / 3 + C.z *
                               (-2 * cos(q + pi / 3) + 1) / 3)
    assert mapping[A.y].equals(C.x * (-2 * cos(q + pi / 3) + 1) / 3 + C.y *
                               (2 * cos(q) + 1) / 3 + C.z *
                               (-2 * sin(q + pi / 6) + 1) / 3)
    assert mapping[A.z].equals(C.x * (-2 * sin(q + pi / 6) + 1) / 3 + C.y *
                               (-2 * cos(q + pi / 3) + 1) / 3 + C.z *
                               (2 * cos(q) + 1) / 3)
    D = A.locate_new("D", a * A.i + b * A.j + c * A.k)
    assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b}
    E = A.orient_new_axis("E", a, A.k, a * A.i + b * A.j + c * A.k)
    assert A.scalar_map(E) == {
        A.z: E.z + c,
        A.x: E.x * cos(a) - E.y * sin(a) + a,
        A.y: E.x * sin(a) + E.y * cos(a) + b,
    }
    assert E.scalar_map(A) == {
        E.x: (A.x - a) * cos(a) + (A.y - b) * sin(a),
        E.y: (-A.x + a) * sin(a) + (A.y - b) * cos(a),
        E.z: A.z - c,
    }
    F = A.locate_new("F", Vector.zero)
    assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
def test_coordinate_vars():
    """
    Tests the coordinate variables functionality with respect to
    reorientation of coordinate systems.
    """
    A = CoordSys3D('A')
    # Note that the name given on the lhs is different from A.x._name
    assert BaseScalar(0, A, 'A_x', r'\mathbf{{x}_{A}}') == A.x
    assert BaseScalar(1, A, 'A_y', r'\mathbf{{y}_{A}}') == A.y
    assert BaseScalar(2, A, 'A_z', r'\mathbf{{z}_{A}}') == A.z
    assert BaseScalar(0, A, 'A_x', r'\mathbf{{x}_{A}}').__hash__() == A.x.__hash__()
    assert isinstance(A.x, BaseScalar) and \
           isinstance(A.y, BaseScalar) and \
           isinstance(A.z, BaseScalar)
    assert A.x*A.y == A.y*A.x
    assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z}
    assert A.x.system == A
    assert A.x.diff(A.x) == 1
    B = A.orient_new_axis('B', q, A.k)
    assert B.scalar_map(A) == {B.z: A.z, B.y: -A.x*sin(q) + A.y*cos(q),
                                 B.x: A.x*cos(q) + A.y*sin(q)}
    assert A.scalar_map(B) == {A.x: B.x*cos(q) - B.y*sin(q),
                                 A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z}
    assert express(B.x, A, variables=True) == A.x*cos(q) + A.y*sin(q)
    assert express(B.y, A, variables=True) == -A.x*sin(q) + A.y*cos(q)
    assert express(B.z, A, variables=True) == A.z
    assert expand(express(B.x*B.y*B.z, A, variables=True)) == \
           expand(A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q)))
    assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \
           (B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \
           B.y*cos(q))*A.j + B.z*A.k
    assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \
                            variables=True)) == \
           A.x*A.i + A.y*A.j + A.z*A.k
    assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \
           (A.x*cos(q) + A.y*sin(q))*B.i + \
           (-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k
    assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \
                            variables=True)) == \
           B.x*B.i + B.y*B.j + B.z*B.k
    N = B.orient_new_axis('N', -q, B.k)
    assert N.scalar_map(A) == \
           {N.x: A.x, N.z: A.z, N.y: A.y}
    C = A.orient_new_axis('C', q, A.i + A.j + A.k)
    mapping = A.scalar_map(C)
    assert mapping[A.x].equals(C.x*(2*cos(q) + 1)/3 +
                            C.y*(-2*sin(q + pi/6) + 1)/3 +
                            C.z*(-2*cos(q + pi/3) + 1)/3)
    assert mapping[A.y].equals(C.x*(-2*cos(q + pi/3) + 1)/3 +
                            C.y*(2*cos(q) + 1)/3 +
                            C.z*(-2*sin(q + pi/6) + 1)/3)
    assert mapping[A.z].equals(C.x*(-2*sin(q + pi/6) + 1)/3 +
                            C.y*(-2*cos(q + pi/3) + 1)/3 +
                            C.z*(2*cos(q) + 1)/3)
    D = A.locate_new('D', a*A.i + b*A.j + c*A.k)
    assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b}
    E = A.orient_new_axis('E', a, A.k, a*A.i + b*A.j + c*A.k)
    assert A.scalar_map(E) == {A.z: E.z + c,
                               A.x: E.x*cos(a) - E.y*sin(a) + a,
                               A.y: E.x*sin(a) + E.y*cos(a) + b}
    assert E.scalar_map(A) == {E.x: (A.x - a)*cos(a) + (A.y - b)*sin(a),
                               E.y: (-A.x + a)*sin(a) + (A.y - b)*cos(a),
                               E.z: A.z - c}
    F = A.locate_new('F', Vector.zero)
    assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
Beispiel #38
0
def test_vector():
    """
    Tests the effects of orientation of coordinate systems on
    basic vector operations.
    """
    N = CoordSysCartesian('N')
    A = N.orient_new('A', 'Axis', [q1, N.k])
    B = A.orient_new('B', 'Axis', [q2, A.i])
    C = B.orient_new('C', 'Axis', [q3, B.j])

    #Test to_matrix
    v1 = a*N.i + b*N.j + c*N.k
    assert v1.to_matrix(A) == Matrix([[ a*cos(q1) + b*sin(q1)],
                                      [-a*sin(q1) + b*cos(q1)],
                                      [                     c]])

    #Test dot
    assert N.i.dot(A.i) == cos(q1)
    assert N.i.dot(A.j) == -sin(q1)
    assert N.i.dot(A.k) == 0
    assert N.j.dot(A.i) == sin(q1)
    assert N.j.dot(A.j) == cos(q1)
    assert N.j.dot(A.k) == 0
    assert N.k.dot(A.i) == 0
    assert N.k.dot(A.j) == 0
    assert N.k.dot(A.k) == 1

    assert N.i.dot(A.i + A.j) == -sin(q1) + cos(q1) == \
           (A.i + A.j).dot(N.i)

    assert A.i.dot(C.i) == cos(q3)
    assert A.i.dot(C.j) == 0
    assert A.i.dot(C.k) == sin(q3)
    assert A.j.dot(C.i) == sin(q2)*sin(q3)
    assert A.j.dot(C.j) == cos(q2)
    assert A.j.dot(C.k) == -sin(q2)*cos(q3)
    assert A.k.dot(C.i) == -cos(q2)*sin(q3)
    assert A.k.dot(C.j) == sin(q2)
    assert A.k.dot(C.k) == cos(q2)*cos(q3)

    #Test cross
    assert N.i.cross(A.i) == sin(q1)*A.k
    assert N.i.cross(A.j) == cos(q1)*A.k
    assert N.i.cross(A.k) == -sin(q1)*A.i - cos(q1)*A.j
    assert N.j.cross(A.i) == -cos(q1)*A.k
    assert N.j.cross(A.j) == sin(q1)*A.k
    assert N.j.cross(A.k) == cos(q1)*A.i - sin(q1)*A.j
    assert N.k.cross(A.i) == A.j
    assert N.k.cross(A.j) == -A.i
    assert N.k.cross(A.k) == Vector.zero

    assert N.i.cross(A.i) == sin(q1)*A.k
    assert N.i.cross(A.j) == cos(q1)*A.k
    assert N.i.cross(A.i + A.j) == sin(q1)*A.k + cos(q1)*A.k
    assert (A.i + A.j).cross(N.i) == (-sin(q1) - cos(q1))*N.k

    assert A.i.cross(C.i) == sin(q3)*C.j
    assert A.i.cross(C.j) == -sin(q3)*C.i + cos(q3)*C.k
    assert A.i.cross(C.k) == -cos(q3)*C.j
    assert C.i.cross(A.i) == (-sin(q3)*cos(q2))*A.j + \
           (-sin(q2)*sin(q3))*A.k
    assert C.j.cross(A.i) == (sin(q2))*A.j + (-cos(q2))*A.k
    assert express(C.k.cross(A.i), C).trigsimp() == cos(q3)*C.j
Beispiel #39
0
def curl(vect, coord_sys=None, doit=True):
    """
    Returns the curl of a vector field computed wrt the base scalars
    of the given coordinate system.

    Parameters
    ==========

    vect : Vector
        The vector operand

    coord_sys : CoordSys3D
        The coordinate system to calculate the gradient in.
        Deprecated since version 1.1

    doit : bool
        If True, the result is returned after calling .doit() on
        each component. Else, the returned expression contains
        Derivative instances

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, curl
    >>> R = CoordSys3D('R')
    >>> v1 = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> curl(v1)
    0
    >>> v2 = R.x*R.y*R.z*R.i
    >>> curl(v2)
    R.x*R.y*R.j + (-R.x*R.z)*R.k

    """

    coord_sys = _get_coord_sys_from_expr(vect, coord_sys)

    if len(coord_sys) == 0:
        return Vector.zero
    elif len(coord_sys) == 1:
        coord_sys = coord_sys.pop()
        i, j, k = coord_sys.base_vectors()
        x, y, z = coord_sys.base_scalars()
        h1, h2, h3 = coord_sys.lame_coefficients()
        vectx = vect.dot(i)
        vecty = vect.dot(j)
        vectz = vect.dot(k)
        outvec = Vector.zero
        outvec += (Derivative(vectz * h3, y) -
                   Derivative(vecty * h2, z)) * i / (h2 * h3)
        outvec += (Derivative(vectx * h1, z) -
                   Derivative(vectz * h3, x)) * j / (h1 * h3)
        outvec += (Derivative(vecty * h2, x) -
                   Derivative(vectx * h1, y)) * k / (h2 * h1)

        if doit:
            return outvec.doit()
        return outvec
    else:
        # TODO: use some of the vector calculus properties for this:
        coord_sys = coord_sys.pop()  # get one random coord_sys
        i, j, k = coord_sys.base_vectors()
        x, y, z = coord_sys.base_scalars()
        h1, h2, h3 = coord_sys.lame_coefficients()

        from .functions import express
        vectx = express(vect.dot(i), coord_sys, variables=True)
        vecty = express(vect.dot(j), coord_sys, variables=True)
        vectz = express(vect.dot(k), coord_sys, variables=True)

        # This is a repetition of previous code, it will be removed as soon as
        # we have some better algorithm to deal with this case:
        outvec = Vector.zero
        outvec += (Derivative(vectz * h3, y) -
                   Derivative(vecty * h2, z)) * i / (h2 * h3)
        outvec += (Derivative(vectx * h1, z) -
                   Derivative(vectz * h3, x)) * j / (h1 * h3)
        outvec += (Derivative(vecty * h2, x) -
                   Derivative(vectx * h1, y)) * k / (h2 * h1)

        if doit:
            return outvec.doit()
        return outvec