Ask Your Question
0

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

asked 2019-11-25 11:46:00 +0100

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))
edit retag flag offensive reopen merge delete

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 2019-11-25 12:36:46 +0100

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.

edit flag offensive delete link more

Comments

1

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

ortollj gravatar imageortollj ( 2019-11-25 13:09:59 +0100 )edit

Question Tools

1 follower

Stats

Asked: 2019-11-25 11:46:00 +0100

Seen: 488 times

Last updated: Nov 25 '19