Question
Please answer question #2, which means the methods for vector addition, negation and subtraction.

t Programming for Math and Science Lab 4 March 25 and 28, 2019. Instructions: Write a module vector.py (but submitted as Last
class Vector: class_wide variable-42 def Linit (self,dim-e,data None): self.dim -dim Today self.data - [key:data[key] for key
t Programming for Math and Science Lab 4 March 25 and 28, 2019. Instructions: Write a module vector.py (but submitted as LastNameFirst Name.Lab4.py) w hich im- plements the dictionary based representation Vector elass, Send to rryhame fordham.edu March 31, 11:59 p.m. Please subanit on.time as the syllabus's no late work policy will henceforth be in force. The gonl is to create the following class object class Vector: def _init _(self, dim, t self.n dim self.f- It is a kind of sparse vector representation, where the dictionary E kepe track of the nonzero components of the vector. For example >>>x-vector (6, {1:0.1, 5:-2.0, 3:20.0)) represents the vector x = (, 0.1, 0, 20, 0,-2.0) in R6. Using the empty dictionary, >>>O-Vector (5, {}) creates the zero vector O- (0,0,0,0,0) R. Specifications Your Vector class must have the following: 1. A step in initthat gets rid zero entries at initialization; as in (0:1.0, 1:0.0, 2:0.0, 3:-1.01 x - Vector (4, t) print (x.5) 0:1.0, 3:-1.0 2. "The methods _add...-eg.., -sub.. fr vector addition, negation and subtraction, respectively. The nddition and subtraction methods access only the non-zero entries The methodsmul., rmul.., truediv. for scalar multiplication (as in 4x), reverse scalar mul- tiplication x "times" 44x, scalar division (x/3). The multiplication operator doubles as a dot product. So if x and y are both instances of the Vector class, thenx y yields the dot product between the two vectors. The implementation entries only when they are both nonzero. 4. The methods norm and proj. Here, x.norm) yields the length of the vector x and x.norm(y) calculates the projection of the vector x onto y 5. A formatted str method for readable printing. Example, if x is a Vector with x.n - 7 and x.t- (3:1, 4:-2.7, 0:10.321), then >»print (x) yields the aligned and formatted string 10.321 1 2.7 dimension -7 6. At least two uses of exception handling or raising an exception- as might be required in dealing with the crunts the number of elements in a container, So we use the alternative word "norn erroneous addition of a Vector with a string, or a division by zero, for example. i Python, the leagth funetion en instend
class Vector: class_wide variable-42 def Linit (self,dim-e,data None): self.dim -dim Today self.data - [key:data[key] for key in data if data[key]I-e. return self.p +other.p return .format(self.p) def add(self, other): def str (self): cs2 04 Ch11 MG 3169.HEIC IMG 3169 HEIC.p IMG 3169.HE 3,024x4,032 df df.zip pptx 2.1 MB Cloud Drive Previous 7 Days Documents top Buddhism Term cs2 03 class intr Guidelines for Paper S...19.docx te Disc IMG 0644 (1).HEIC o.pptx Blog Po.. . 1 docx All L5 (Python ElDoc) F1 vector.py saving...done
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here are the code for the first 4 sub parts

================================================================================

class Vector():
    def __init__(self, dim, f):
        self.n = dim
        self.f = {}
        for key, val in f.items():
            if val is not 0:
                self.f[key] = val

    def __add__(self, other):
        for i in range(self.n):
            if self.f.get(i) == None and other.f.get(i) == None:
                continue
            elif self.f.get(i) is not None and other.f.get(i) == None:
                continue
            elif self.f.get(i) == None and other.f.get(i) is not None:
                self.f[i] = other.f.get(i)
            else:
                self.f[i] = self.f.get(i) + other.f.get(i)

    def __neg__(self):
        for key, val in self.f.items():
            self.f[key] = -val

    def __sub__(self, other):
        for i in range(self.n):
            if self.f.get(i) == None and other.f.get(i) == None:
                continue
            elif self.f.get(i) is not None and other.f.get(i) == None:
                continue
            elif self.f.get(i) == None and other.f.get(i) is not None:
                self.f[i] = -other.f.get(i)
            else:
                self.f[i] = self.f.get(i) - other.f.get(i)

    def __mul__(self, other):
        for i in range(self.n):
            if self.f.get(i) == None and other.f.get(i) == None:
                continue
            elif self.f.get(i) is not None and other.f.get(i) == None:
                self.f[i] = 0
            elif self.f.get(i) == None and other.f.get(i) is not None:
                self.f[i] = 0
            else:
                self.f[i] = self.f.get(i) * other.f.get(i)

    def __rmul__(self, other):
        for k, v in self.f.items():
            self.f[k] = v * other

    def __truediv__(self, other):
        for k, v in self.f.items():
            self.f[k] = v / other

    def norm(self):
        sum_square=sum(k*k for k in self.f.keys())
        return sum_square**(1.0/self.n)



f = {0: 1.0, 1: 0, 2: 0, 3: -1.0}
v = Vector(4, f)
print(v.f)

=================================================================================

Add a comment
Know the answer?
Add Answer to:
T Programming for Math and Science Lab 4 March 25 and 28, 2019. Instructions: Write a module vect...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python REALLY NEED HELP !!!!!!!!! [10pts] Write the class Vector that supports the basic vector operations....

    Python REALLY NEED HELP !!!!!!!!! [10pts] Write the class Vector that supports the basic vector operations. Such operations are addition (+) and subtraction (-) of vectors of the same length, dot product (*) and multiplication (*) of a vector by a scalar. All methods must return (not print) the result. Your class should also support the rich comparison for equality (==) - You must use the special methods for those 4 operators in order to override their behavior - You...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT