Python Forum
Issue with def norm in class Vector
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with def norm in class Vector
#1
Hi guys I am trying to calculate the norm(magnitude) of a Vector using class and when i run the code I get:
Error:
return math.sqrt(sum( i**2 for i in self )) TypeError: 'Vector' object is not iterable
import math

x = 2.
y = 8.
z = 6.


class Vector(object):
    def __init__(self, data):
        self.data = data
        self.rows = len(data)
    
    def __mul__(self, other):
        assert len(self.data) == len(other.data)
        result = 0.
        for vi, vj in zip(self.data, other.data):
            result += vi * vj
        return result

    def __rmul__(self, a):
        data = [a * d for d in self.data]
        return Vector(data)

    def __add__(self, other):
        assert len(self.data) == len(other.data)
        data = [i + j for (i, j) in zip(self.data, other.data)]
        return Vector(data)
    
    def norm(self):
        return math.sqrt(sum( i**2 for i in self ))
        
    def __sub__(self, other):
        assert len(self.data) == len(other.data)
        data = [i - j for (i, j) in zip(self.data, other.data)]
        return Vector(data)
    
    def __str__(self):
        return '{0}'.format(self.data)

I = Vector([1., 1., 1.])
v = Vector([1. + x, 2. + y, 3. + z])
w = Vector([4. + x, 5. + y, 6. + z])
Final = ( 2 * v - w ) + w.norm() * I
print(Final.data)
Reply
#2
maybe you mean self.data?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Presumably, you want to iterate over the data attribute.

    def norm(self):
        return math.sqrt(sum( i**2 for i in self.data ))
Reply
#4
As a side note - you have Vector.rows but never use it. Instead you continue to use len(self.data) and len(other.data)

You may want to use @property

@property
def rows(self):
    return len(self.data)
then
assert self.rows == other.rows
and you are certain that self.rows will always yield the correct len
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Wow guys thanks a lot I am new to python and classes seem a bit strange to understand !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Child class inheritance issue eakanathan 3 1,341 Apr-21-2022, 12:03 PM
Last Post: deanhystad
Question How to understand the vector/direction mason321 0 1,114 Dec-14-2021, 10:57 PM
Last Post: mason321
  How to find vector of a 3D plot mason321 0 1,018 Nov-13-2021, 05:05 PM
Last Post: mason321
  3D vector class with inheritance from 2D vector class buss0140 4 3,153 Dec-20-2020, 08:44 PM
Last Post: deanhystad
  Issue referencing new instance from other class nanok66 3 2,232 Jul-31-2020, 02:07 AM
Last Post: nanok66
  Class issue Reldaing 2 2,027 Mar-31-2020, 10:15 PM
Last Post: Reldaing
  Problem in creating a vector termo 11 3,916 Oct-10-2019, 03:09 PM
Last Post: stullis
  Creating Vector from a Program ericvrocha 3 1,894 Oct-08-2019, 07:43 AM
Last Post: newbieAuggie2019
  How to combine two float64 column vector? dsarica 1 2,409 Apr-17-2019, 07:22 PM
Last Post: Yoriz
  2d vector library advice kristofferorum 1 3,808 Oct-20-2016, 09:09 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020