Ask Your Question
1

How to assign a vector

asked 2017-01-16 00:44:05 +0200

Wolfgang gravatar image

updated 2017-08-01 12:08:59 +0200

FrédéricC 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
0

answered 2017-01-17 12:31:06 +0200

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
0

answered 2017-01-17 10:16:36 +0200

slelievre gravatar image

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

Stats

Asked: 2017-01-16 00:43:12 +0200

Seen: 284 times

Last updated: Jan 17 '17