Ask Your Question

How to assign a vector

asked 2017-01-15 17:44:05 -0500

Wolfgang gravatar image

I have the following code:

a=b=vector(ring, 2)
a = ([r1,r2])
b =a 
a[0] = s1

Then b[0] changes too but I want b[0] be r1. And I do not want to do something like this:

b = ([a[0],a[1]])

which works but inconvenient because of possible high dimensions of vectors.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-01-17 05:31:06 -0500

ndomes gravatar image

Attention, pitfall:

a = b = vector(QQ,2)

is NOT a shortform of

a =  vector(QQ,2)
b =  vector(QQ,2)

The first creates exactly one vector object, a an b refer to the same object. The second creates two independent objects, a and b refer to different objects.

print a == b, a is b

shows the difference.

edit flag offensive delete link more

answered 2017-01-17 03:16:36 -0500

Use this:

b = a[:]

to copy the vector into an independent vector.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-01-15 17:43:12 -0500

Seen: 33 times

Last updated: Jan 17