a-
- Write a class that defines a two-dimensional Cartesian coordinate. Write the class such that the following Python program executes correctly.
#/usr/bin/env python3.2 # # Your header here # # Two-dimensional Cartesian coordinate # class definition here class Cartesian2D: def __init__(self): # etc. def main( ): a = Cartesian2D(2.3, 3.4) b = Cartesian2D(4.5, 1.8) c = Cartesian2D(8.1, 0.3) print("The distance from a to b is {}".format(a.distanceTo(b))) print("The distance from b to c is {}".format(b.distanceTo(c))) d = a + b print("a + b = ({},{})".format(d.x, d.y)) d = c - b print("c - b = ({}, {})".format(d.x, d.y)) print("The length of a is {}".format(a.length())) print("The length of b is {}".format(b.length())) print("The length of c is {}".format(c.length())) unita = a.normalize() unitb = b.normalize() unitc = c.normalize() print("The length of unit a is {}".format(unita.length())) print("The length of unit b is {}".format(unitb.length())) print("The length of unit c is {}".format(unitc.length())) s = 4 d = unita * s print(d) print("The length of d is {}".format(d.length())) e = unitb * s f = dot(a, b) g = dot(unita, unitb) h = dot(d, e) print("dot(a, b) = {}".format(f)) print("dot(unita, unitb = {}".format(g)) print("dot(d, e) = {}".format(h)) if __name__ == "__main__": main( )The two-dimensional Cartesian coordinate and main function must all be in the same file and named cartesian_demo.py.
b-
- Write a class that defines a quaternion object. Quaternions are a hypercomplex numbers that are not commutative, but they are associative. A definition of what a quaternion is and an explanation of the operations performed on a quaternion are given at Wolfram Research’s MathWorld. Write the class such that the following Python program executes correctly.
#/usr/bin/env python3.2 # # Your header here # # Quaternions class definition here clas Quaternion: def __init__(self): # etc. def main( ): # Quaternion a & b are defined by four constants, which # define a + b i + c j + d k where a...d are parameter 1...4 of # the constructor a = Quaternion(.3, 3.2, 6.5, 1.1) b = Quaternion(.5, 4.7, 7.6, 2.2) c = a + b print(c) c = a - b print(c) print(a.conjugate()) print(b.conjugate()) print(c.conjugate()) print(a*b) print(b*a) print(a*c) print(c*a) print("a's angle {} and axis {}".format(a.angle(), a.axis())) print("b's angle {} and axis {}".format(b.angle(), b.axis())) print("c's angle {} and axis {}".format(c.angle(), c.axis())) a.norm() b.norm() c.norm() print("a's angle {} and axis {}".format(a.angle(), a.axis())) print("b's angle {} and axis {}".format(b.angle(), b.axis())) print("c's angle {} and axis {}".format(c.angle(), c.axis())) if __name__ == "__main__": main( )The quaternion class and main function must all be in the same file and named quaternion_demo.py.


0 comments