narray icon indicating copy to clipboard operation
narray copied to clipboard

mul! behaves different to *=

Open cptjazz opened this issue 13 years ago • 1 comments

It seems #mul! and #*= behave different when using ranges. #*= correctly modifies the original array, #mul! does not.

Is this desired? (if yes -- it is not mentioned in the spec here http://narray.rubyforge.org/SPEC.en)

irb(main):017:0> a
=> NArray.float(5):
[ 0.0253152, 0.88346, 0.792297, 0.498892, 0.374336 ]

irb(main):018:0> a[1...3].mul!(-1.0)
=> NArray.float(2):
[ -0.88346, -0.792297 ]

irb(main):019:0> a
=> NArray.float(5):
[ 0.0253152, 0.88346, 0.792297, 0.498892, 0.374336 ]

but

irb(main):020:0> a[1...3] *= (-1.0)
=> NArray.float(2):
[ -0.88346, -0.792297 ]

irb(main):021:0> a
=> NArray.float(5):
[ 0.0253152, -0.88346, -0.792297, 0.498892, 0.374336 ]

cptjazz avatar Jul 20 '12 21:07 cptjazz

This behaviour is quite natural in view of Ruby language.

In Ruby language, a[1...3] *= (-1.0) is equal to a[1...3] = a[1...3] * (-1.0)

On the other hand a[1...3].mul!(-1.0) means b = a[1...3]; b.mul!(-1.0)

masa16 avatar Jul 23 '12 05:07 masa16