def test_is_vector(self): v1, v2, zero, one, nabla, C, vn1, vn2, x, y, z = self._get_vars() # one argument assert not VecMul(1).is_Vector assert not VecMul(x).is_Vector assert VecMul(one).is_Vector assert VecMul(v1).is_Vector # two arguments assert VecMul(one, x).is_Vector assert VecMul(0, one).is_Vector assert not VecMul(0, x).is_Vector assert VecMul(v1, 2).is_Vector assert not VecMul(v1.mag, 2).is_Vector assert VecMul(v1.mag, v2).is_Vector assert VecMul(v1 + v2, v2.mag).is_Vector assert VecMul(v1 + v2, 3).is_Vector assert VecMul(v1 + v2, (v1 + v2).mag).is_Vector # with dot product and nested mul/dot assert not VecMul(3, VecDot(v1, v2)).is_Vector assert VecMul(3, VecMul(v1, VecDot(v1, v2))).is_Vector assert VecMul(3, v1 + v2, VecMul(VecDot(v1, v2), x)).is_Vector # with cross product and nested mul/cross assert VecMul(3, VecCross(v1, v2)).is_Vector assert VecMul(3, v1.mag, VecMul(x, VecCross(v1, v2))).is_Vector # with cross and dot products assert VecMul(3, VecDot(v1, v2), VecMul(x, VecCross(v1, v2))).is_Vector assert not VecMul(3, VecDot(v1, v2), VecDot(VecMul(x, VecCross(v1, v2)), v1)).is_Vector
def test_doit(self): v1, v2, zero, one, nabla, C, vn1, vn2, x, y, z = self._get_vars() expr = VecAdd(v1, v2, v1, evaluate=False) assert not self._check_args(expr, VecAdd(VecMul(2, v1), v2)) assert self._check_args(expr.doit(), VecAdd(VecMul(2, v1), v2)) expr = VecAdd(v1, v2, VecAdd(v1, v2, evaluate=False), evaluate=False) assert not self._check_args(expr, VecAdd(VecMul(2, v1), VecMul(2, v2))) assert self._check_args(expr.doit(), VecAdd(VecMul(2, v1), VecMul(2, v2))) r = vn1 + vn2 assert VecAdd(vn1, vn2).doit() == r expr = VecAdd(v1.mag, VecDot(vn1, vn2)) assert self._check_args(expr.doit(deep=False), expr) assert self._check_args(expr.doit(), VecAdd(v1.mag, vn1 & vn2)) # test the rule default_sort_key used into VecAdd.doit assert VecAdd(v1, one, v2).doit().args == (one, v1, v2) assert VecAdd(v1, VecCross(v1, v2), one).doit().args == (VecCross(v1, v2), one, v1) # test the rule merge_explicit used into VecAdd.doit assert VecAdd(vn1, vn2).doit() == vn1 + vn2 assert self._check_args( VecAdd(vn1, vn2, one).doit(), VecAdd(one, vn1 + vn2))
def test_is_vector(self): v1, v2, zero, one, nabla, C, vn1, vn2, x, y, z = self._get_vars() assert VecCross(v1, zero).is_Vector assert VecCross(v1, v2).is_Vector assert VecCross(vn1, vn2).is_Vector assert VecCross(v1, vn1).is_Vector assert VecCross((v1 & v2) * one, v2).is_Vector
def test_doit(self): v1, v2, zero, one, nabla, C, vn1, vn2 = self._get_vars() assert Magnitude(VecAdd(v1, zero, evaluate=False)).doit() == v1.mag assert Magnitude(vn1).doit() == sqrt(14) assert Magnitude(vn2).doit() == sqrt(x**2 + y**2 + z**2) assert isinstance( Magnitude(VecCross(vn1, vn2)).doit(deep=False), Magnitude) assert isinstance(Magnitude(VecCross(vn1, vn2)).doit(), Pow)
def test_creation(self): v1, v2, zero, one, nabla, C, vn1, vn2, x, y, z = self._get_vars() assert VecCross(v1, zero) == zero assert VecCross(zero, v1) == zero assert VecCross(v1, v1) == zero assert VecCross(vn1, vn1) == Vector.zero assert VecCross(v1, v1.norm) assert VecCross(vn1, v1.norm) assert VecCross(nabla, v1) assert VecCross(v1, vn1) assert VecCross(vn1, v1) def func(*args): with self.assertRaises(TypeError) as context: VecCross(*args) func(x, v1) func(v1, x) func(v1, v1.mag) func(v1 & v2, v1.mag) func(v1 & v2, v1) func(0, v1) func(nabla, nabla) func(v1, nabla)
def test_doit(self): v1, v2, zero, one, nabla, C, vn1, vn2, x, y, z = self._get_vars() assert VecCross(vn1, vn2).doit() - vn1.cross(vn2) == Vector.zero assert VecCross(nabla, vn2).doit() - curl(vn2) == Vector.zero vn3 = C.x * C.y * C.z * C.i + C.x * C.y * C.z * C.j + C.x * C.y * C.z * C.k assert VecCross(nabla, vn3).doit() - curl(vn3) == Vector.zero expr = VecCross(v2 * VecDot(vn1, vn2), v1).doit() assert isinstance(expr.args[0].args[0], Add) expr = VecCross(v2 * VecDot(vn1, vn2), v1).doit(deep=False) assert isinstance(expr.args[0], VecMul)
def test_veccross(self): v1, v2, zero, one, nabla, C, vn1, vn2, x, y, z = self._get_vars() expr = VecCross(v1, v2) assert isinstance(expr.diff(x), VecAdd) assert isinstance(expr.diff(x, evaluate=False), D) assert self._check_args( expr.diff(x), (v1.diff(x) ^ v2) + (v1 ^ v2.diff(x)) ) expr = v1.cu.diff(x, 2) assert isinstance(expr, D)
def test_reverse(self): v1, v2, zero, one, nabla, C, vn1, vn2, x, y, z = self._get_vars() assert VecCross(v1, v2).reverse == -VecCross(v2, v1) assert VecCross(vn1, v2).reverse == -VecCross(v2, vn1) assert VecCross(v1, vn2).reverse == -VecCross(vn2, v1) assert VecCross(vn1, vn2).reverse == -VecCross(vn2, vn1)
def func(*args): with self.assertRaises(TypeError) as context: VecCross(*args)