{"action":"created","issue":{"url":"https://api.github.com/repos/vkostyukov/la4j/issues/253","labels_url":"https://api.github.com/repos/vkostyukov/la4j/issues/253/labels{/name}","comments_url":"https://api.github.com/repos/vkostyukov/la4j/issues/253/comments","events_url":"https://api.github.com/repos/vkostyukov/la4j/issues/253/events","html_url":"https://github.com/vkostyukov/la4j/issues/253","id":54521254,"number":253,"title":"Setting elements to zero in CRSMatrix","user":{"login":"rabbanyk","id":5085359,"avatar_url":"https://avatars.githubusercontent.com/u/5085359?v=3","gravatar_id":"","url":"https://api.github.com/users/rabbanyk","html_url":"https://github.com/rabbanyk","followers_url":"https://api.github.com/users/rabbanyk/followers","following_url":"https://api.github.com/users/rabbanyk/following{/other_user}","gists_url":"https://api.github.com/users/rabbanyk/gists{/gist_id}","starred_url":"https://api.github.com/users/rabbanyk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rabbanyk/subscriptions","organizations_url":"https://api.github.com/users/rabbanyk/orgs","repos_url":"https://api.github.com/users/rabbanyk/repos","events_url":"https://api.github.com/users/rabbanyk/events{/privacy}","received_events_url":"https://api.github.com/users/rabbanyk/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/vkostyukov/la4j/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/vkostyukov/la4j/milestones/3","labels_url":"https://api.github.com/repos/vkostyukov/la4j/milestones/3/labels","id":445537,"number":3,"title":"la4j-0.6.0","description":"","creator":{"login":"vkostyukov","id":1098539,"avatar_url":"https://avatars.githubusercontent.com/u/1098539?v=3","gravatar_id":"","url":"https://api.github.com/users/vkostyukov","html_url":"https://github.com/vkostyukov","followers_url":"https://api.github.com/users/vkostyukov/followers","following_url":"https://api.github.com/users/vkostyukov/following{/other_user}","gists_url":"https://api.github.com/users/vkostyukov/gists{/gist_id}","starred_url":"https://api.github.com/users/vkostyukov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkostyukov/subscriptions","organizations_url":"https://api.github.com/users/vkostyukov/orgs","repos_url":"https://api.github.com/users/vkostyukov/repos","events_url":"https://api.github.com/users/vkostyukov/events{/privacy}","received_events_url":"https://api.github.com/users/vkostyukov/received_events","type":"User","site_admin":false},"open_issues":41,"closed_issues":6,"state":"open","created_at":"2013-10-04T10:55:00Z","updated_at":"2015-01-15T23:50:33Z","due_on":null,"closed_at":null},"comments":10,"created_at":"2015-01-15T23:41:25Z","updated_at":"2015-01-16T00:33:29Z","closed_at":null,"body":"Hi there, \r\n\r\nI was trying to set some elements in a Sparse Matrix (CRSMatrix) to zero, using nonZeroIterator, or apply procedure, and it seems to mess with the matrix and cause non valid results for further operations, in my case subtract. To be more specific, I'm extending the CRS with the following method:\r\n\r\n```java\r\npublic void zeroDiagonalsInPlace() {\r\n\t\tMatrixIterator it = nonZeroIterator();\r\n\t\twhile (it.hasNext()) {\r\n\t\t\tdouble x= it.next();\r\n\t\t\tif (it.rowIndex() == it.columnIndex()) \r\n\t\t                it.set(0);\r\n\t\t}\r\n\t}\r\nor \r\n\tMatrixProcedure procedure = new MatrixProcedure() {\r\n\t\t\t@Override\r\n\t\t\tpublic void apply(int i, int j, double value) {\r\n\t\t\t\tif (i==j)\r\n\t\t\t\t\tset(i,j,0);\r\n\t\t\t}\r\n\t\t};\r\n         this.eachNonZero(procedure );\r\n```\r\nThe method seems to do the job but then I am subtracting another matrix from this resulted matrix, and the results are wrong. See example bellow, \r\nA = \r\n1.000 0.000 0.000 1.000 1.000 \r\n0.000 1.000 0.000 0.000 0.000 \r\n0.000 0.000 1.000 0.000 0.000 \r\n1.000 0.000 0.000 1.000 1.000 \r\n1.000 0.000 0.000 1.000 1.000 \r\n\r\nB =\r\n1.000 0.000 0.000 1.000 1.000 \r\n0.000 1.000 1.000 1.000 0.000 \r\n0.000 1.000 2.000 2.000 1.000 \r\n1.000 1.000 2.000 3.000 2.000 \r\n1.000 0.000 1.000 2.000 2.000 \r\n\r\n```java\r\nA.zeroDiagonalsInPlace()\r\nB.zeroDiagonalsInPlace()\r\n```\r\nA = \r\n0.000 0.000 0.000 1.000 1.000 \r\n0.000 0.000 0.000 0.000 0.000 \r\n0.000 0.000 0.000 0.000 0.000 \r\n1.000 0.000 0.000 0.000 1.000 \r\n1.000 0.000 0.000 1.000 0.000 \r\nB = \r\n0.000 0.000 0.000 1.000 1.000 \r\n0.000 0.000 1.000 1.000 0.000 \r\n0.000 1.000 0.000 2.000 1.000 \r\n1.000 1.000 2.000 0.000 2.000 \r\n1.000 0.000 1.000 2.000 0.000 \r\n```java\r\nA.subtract(B)\r\n```\r\n 0.000  0.000  0.000  0.000  0.000 \r\n 1.000  0.000 -1.000  1.000  1.000 \r\n 0.000 -1.000  0.000 -2.000 -1.000 \r\n-1.000 -1.000 -2.000  0.000 -2.000 \r\n-1.000  0.000 -1.000 -2.000  0.000 \r\n "},"comment":{"url":"https://api.github.com/repos/vkostyukov/la4j/issues/comments/70189626","html_url":"https://github.com/vkostyukov/la4j/issues/253#issuecomment-70189626","issue_url":"https://api.github.com/repos/vkostyukov/la4j/issues/253","id":70189626,"user":{"login":"rabbanyk","id":5085359,"avatar_url":"https://avatars.githubusercontent.com/u/5085359?v=3","gravatar_id":"","url":"https://api.github.com/users/rabbanyk","html_url":"https://github.com/rabbanyk","followers_url":"https://api.github.com/users/rabbanyk/followers","following_url":"https://api.github.com/users/rabbanyk/following{/other_user}","gists_url":"https://api.github.com/users/rabbanyk/gists{/gist_id}","starred_url":"https://api.github.com/users/rabbanyk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rabbanyk/subscriptions","organizations_url":"https://api.github.com/users/rabbanyk/orgs","repos_url":"https://api.github.com/users/rabbanyk/repos","events_url":"https://api.github.com/users/rabbanyk/events{/privacy}","received_events_url":"https://api.github.com/users/rabbanyk/received_events","type":"User","site_admin":false},"created_at":"2015-01-16T00:33:29Z","updated_at":"2015-01-16T00:33:29Z","body":"I think the problem is with having a row with all zero elements. See for example the following which is not using the set method:\r\n```java\r\n                double [][] a = {{1,0,1,0,0},{0,0,0,0,0},{0,0,0,0,0},{1,0,0,0,0},{1,0,0,1,0}};\r\n\t\tCRSMatrix m = new CRSMatrix(a);\r\n\t\t\r\n\t\tMatrixIterator it = m.nonZeroIterator();\r\n\t\twhile (it.hasNext()) {\r\n\t\t\t  double x = it.next();\r\n\t\t\t  int i = it.rowIndex();\r\n\t\t\t  int j = it.columnIndex();\r\n\t\t\t  System.err.println(\"[\" + i + \",\" + j + \"] = \" + x);\r\n\t\t\t}\r\n\t\treturn;\r\n```\r\ngives\r\n[0,0] = 1.0\r\n[0,2] = 1.0\r\n[1,0] = 1.0\r\n[1,0] = 1.0\r\n[1,3] = 1.0"}}