First time here? Check out the FAQ!

Ask Your Question
0

test if v.norm <> zero pb [closed]

asked 5 years ago

ortollj gravatar image

HI I do not understand why if v.norm !=0 does not work here.

ZeroDivisionError: rational division by zero

def normalizeVectorL(vectorList) :
    vNormList=[]
    for l in vectorList :
        lMatrixList=[]
        for v in l :
            v=vector(v)
            z=zero_vector(v.length())
            #if v != z  :
            if v.norm() != 0 :

                show("v :\t",v,"v.norm() :\t",v.norm())
                lMatrixList.append(matrix(v/v.norm()).transpose())
            else :
                lMatrixList.append(matrix(v).transpose())

        vNormList.append(lMatrixList)
    return vNormList

VVVlist=[]
VVVlist0=[]
VVVlist0.append([1,1,1])
VVVlist0.append([1,1,2])
VVVlist0.append([1,0,0])
VVVlist.append(VVVlist0)

VVVlist1=[]
VVVlist1.append([1,3,1])
VVVlist1.append([1,2,2])
VVVlist1.append([0,0,0])
VVVlist.append(VVVlist1)

show(VVVlist)
show(normalizeVectorList(VVVlist))
Preview: (hide)

Closed for the following reason not a real question by ortollj
close date 2019-11-25 13:14:44.643826

1 Answer

Sort by » oldest newest most voted
0

answered 5 years ago

Iguananaut gravatar image

It works for me, except I had to change normalizeVectorList to normalizeVectorL which is the name of the function you defined in your example. Please make sure you are running the actual code you think you're running by creating and verifying a Minimal, Complete, Reproducible Example.

As an aside I'm a little confused by this construction and the one below it:

VVVlist=[]
VVVlist0=[]
VVVlist0.append([1,1,1])
VVVlist0.append([1,1,2])
VVVlist0.append([1,0,0])
VVVlist.append(VVVlist0)

This is just equivalent to writing:

VVVlist = [[[1, 1, 1], [1, 1, 2], [1, 0, 0]]]

and so on. You can nest list literals as much as you need to and I think it makes for much clearer code.

Preview: (hide)
link

Comments

1

OOps, sorry I changed the name, and I did not see the old one was still present !

ortollj gravatar imageortollj ( 5 years ago )

Question Tools

1 follower

Stats

Asked: 5 years ago

Seen: 683 times

Last updated: Nov 25 '19