New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with function `_wclear` in cs_amd.js #536

Closed
josdejong opened this Issue Jan 8, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@josdejong
Owner

josdejong commented Jan 8, 2016

Yesterday I upgraded to node.js v5.4, and now a few unit tests of math.js are failing. The reason for that is that the argument w of the function _wclear in file cs_amd.js is mutated inside the function, which is not allowed in strict mode.

https://github.com/josdejong/mathjs/blob/master/lib/function/algebra/sparse/cs_amd.js#L552-L562

  var _wclear = function(mark, lemax, w, n) {
    if (mark < 2 || (mark + lemax < 0)) {
      for (var k = 0; k < n; k++) {
        if (w[k] !== 0)
          w[k] = 1;
      }
      mark = 2 ;
    }
    // at this point, w [0..n-1] < mark holds
    return mark;
  };

I first tried to fix this by copying w inside the function, mutate that, and return both mark and the copied w as an object. However, that didn't work as apparently all invocations of _wclear pass a number for w rather than an array.

@rjbaucells when I remove the for loop which mutates w, all unit tests still pass. However, I think the code was there for some reason. Do you know if this for loop can be safely removed, or are there other side effects that we should be aware of?

  // ... this seems to work ...
  var _wclear = function(mark, lemax) {
    if (mark < 2 || (mark + lemax < 0)) {
      mark = 2 ;
    }
    // at this point, w [0..n-1] < mark holds
    return mark;
  };

@josdejong josdejong added the bug label Jan 8, 2016

@rjbaucells

This comment has been minimized.

Collaborator

rjbaucells commented Jan 8, 2016

Jos,

I think there is a bug in the _wclear() function. The w parameter is an offset within the actual array W holding the data. See:

https://github.com/josdejong/mathjs/blob/master/lib/function/algebra/sparse/cs_amd.js#L55-L63

The correct implementation should be passing the W array into the function and the loop should be like:

for (var k = 0; k < n; k++) {
  if (W[w + k] !== 0)
    W[w + k] = 1;
}

I will not be able to do anything on the next few days but you could try with the following implementation:

var _wclear = function(mark, lemax, W, w, n) {
    if (mark < 2 || (mark + lemax < 0)) {
        for (var k = 0; k < n; k++) {
            if (W[w + k] !== 0)
                W[w + k] = 1;
        }
        mark = 2 ;
    }
    // at this point, w [0..n-1] < mark holds
    return mark;
};

And modifying the calling code:

https://github.com/josdejong/mathjs/blob/develop/lib/function/algebra/sparse/cs_amd.js#L164

mark = _wclear(mark, lemax, W, w, n);

https://github.com/josdejong/mathjs/blob/develop/lib/function/algebra/sparse/cs_amd.js#L278

mark = _wclear(mark + lemax, lemax, W, w, n);

https://github.com/josdejong/mathjs/blob/develop/lib/function/algebra/sparse/cs_amd.js#L499

var mark = _wclear(0, 0, W, w, n);
@josdejong

This comment has been minimized.

Owner

josdejong commented Jan 8, 2016

Thanks for your fast reply, now I understand the intention. I think I can fix it this weekend, I'm only not sure how to to test it to prevent any future regression, but maybe you can have a look at that later on.

josdejong added a commit that referenced this issue Jan 8, 2016

Fixed a bug in an internal method used for sparse matrices, see #536.…
… Still needs extra unit tests to verify the behavior
@josdejong

This comment has been minimized.

Owner

josdejong commented Jan 8, 2016

Your proposed fix solves the issue that popped up in node.js 5.4. It would be great if you could add a unit test to verify this behavior later on but it's not critical right now I think.

@josdejong josdejong closed this Apr 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment