SCM

[#6659] sparseVector wrong sum or index

Date:
2020-02-25 10:08
Priority:
3
State:
Closed
Submitted by:
Georg Kindermann (kindermann)
Assigned to:
Martin Maechler (mmaechler)
Hardware:
All
Product:
None
Operating System:
Linux
Component:
None
Version:
None
Severity:
None
Resolution:
Fixed
URL:
Summary:
sparseVector wrong sum or index

Detailed description
When calculating the sum of sparseVector's the result seems to be wrong. It looks like that the positions, where the additions were done, is mixed up. When casting to a vector the result is correct.

library(Matrix)
x <- sparseVector(numeric(1), 1, length=200)
i <- 1:16*12
y1 <- sparseVector(1:3, 13:15, 16)
y2 <- sparseVector(1:6, c(5, 6, 7, 9, 14, 15), 16)
y3 <- sparseVector(1:9, 7:15, 16)
x[i] <- x[i] + y1
x[i] <- x[i] + y2
x[i]
# [1] . . . . 1 2 3 . 4 . . . 1 7 9 .
y3
# [1] . . . . . . 1 2 3 4 5 6 7 8 9 .
x[i] + y3 #gives wrong result
# [1] . . . . 1 2 8 2 12 4 5 6 10 12 10 .
as.vector(x[i]) + y3 #gives correct result
# [1] . . . . 1 2 4 2 7 4 5 6 8 15 18 .

sessionInfo()
#R version 3.6.2 (2019-12-12)
#Platform: x86_64-pc-linux-gnu (64-bit)
#Running under: Debian GNU/Linux 10 (buster)
#
#other attached packages:
#[1] Matrix_1.2-18

Comments:

Message  ↓
Date: 2020-03-02 14:45
Sender: Georg Kindermann

Many thanks for fixing the bug so fast!

Date: 2020-03-02 14:11
Sender: Martin Maechler

Bug fixed (here on R-forge with svn r3324 | mmaechler | 2020-02-26

Date: 2020-02-25 11:17
Sender: Martin Maechler

This *is* a bug in Matrix dealing with sparseVector.

Actually I think the bug is in the `[<-` method, not in `+`:

If I try to simplify the bogous example and use (very early)
xi <- x[i] and then replace all x[i] by xi, there's no problem.

Thank you for reporting it.

Attached Files:

Changes

Field Old Value Date By
status_idOpen2020-03-02 14:12mmaechler
close_dateNone2020-03-02 14:12mmaechler
ResolutionAccepted As Bug2020-03-02 14:11mmaechler
assigned_tonone2020-02-25 11:17mmaechler
HardwarePC2020-02-25 11:17mmaechler
ResolutionNone2020-02-25 11:17mmaechler
Thanks to:
Vienna University of Economics and Business Powered By FusionForge