First time here? Check out the FAQ!

Ask Your Question
1

How to assign a vector

asked 8 years ago

Wolfgang gravatar image

updated 7 years ago

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.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
0

answered 8 years ago

slelievre gravatar image

Use this:

b = a[:]

to copy the vector into an independent vector.

Preview: (hide)
link
0

answered 8 years ago

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.

Preview: (hide)
link

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: 8 years ago

Seen: 389 times

Last updated: Jan 17 '17