0

I got the ValueError as given below.

ValueError: matrices are not aligned for copy error

It was traced to the following line (I did not write this code, I am trying to use it):

x1[:] =  _dotproduct(x1, u)

The dot product is like numpy dot product, it works FINE, printing _dotproduct(x1, u) give a valid answer. It is x1[:] that is not working.

What does [:] mean? I have never seen that.

Also how can I solve the error of alignment?

Edit:
I have now traced the error to x1[:], so instead of this can I do the following:

hh=len(x1)

x1[0:hh]=_dotproduct(x1, u)?

Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
user3456863
  • 45
  • 2
  • 8

1 Answers1

0

In this case, since it's on the left side of the = sign, it's a slice assignment. The object x1 remains the same object, but all its contents are replaced with the sequence on the right. Without the [:], x1 would be assigned to a totally different object.

Using a slice assignment means that if there are other references to the same variable in your program, all of these will see the new contents. For example, the caller of a function passes in a container and the function replaces its contents. This wouldn't be possible without the slice assignment.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • Thank you but it is the left side x1[:] that is giving rise to the alignment error. As noted in my comment, this gives a clue but not a solution. http://starship.python.net/pipermail/mmtk/2008/001447.html – user3456863 Aug 18 '14 at 22:32
  • Sorry, I thought your actual question was "What does [:] mean?" – kindall Aug 18 '14 at 22:33
  • sorry, I am terrible at explaining things and English is not my strong point. So terribly sorry for the confusion. It was a two part question now I edit it. – user3456863 Aug 18 '14 at 22:37