Skip to content

Calculations done with sparse matrices may yield different results than when done with dense matrices #1401

@cleydyr

Description

@cleydyr

Using "mathjs": "^5.4.2"

function fun(horario, carga, disponibilidade) {
  const [d, t] = disponibilidade.size();
  return math.chain(horario)
    .multiply(carga.map(v => math.sign(v)))
    .bitNot()
    .bitOr(math.transpose(disponibilidade))
    .bitNot()
    .sum()
    .done();
}

const H3 = math.matrix([
  [0, 0, 1, 0, 0, 1, 1, 0, 0],
  [0, 1, 0, 0, 1, 0, 0, 0, 1],
  [1, 0, 0, 1, 0, 0, 0, 0, 1],
  [1, 0, 0, 1, 0, 0, 0, 1, 0]
]);

const H3_ = math.matrix([
  [0, 0, 1, 0, 0, 1, 1, 0, 0],
  [0, 1, 0, 0, 1, 0, 0, 0, 1],
  [1, 0, 0, 1, 0, 0, 0, 0, 1],
  [1, 0, 0, 1, 0, 0, 0, 1, 0]
], 'sparse');

const K = math.matrix(
  [  [ 2, 0, 0, 0, 0 ],
     [ 0, 0, 1, 0, 0 ],
     [ 0, 0, 0, 1, 0 ],
     [ 0, 0, 2, 0, 0 ],
     [ 0, 1, 0, 0, 0 ],
     [ 0, 0, 1, 0, 0 ],
     [ 1, 0, 0, 0, 0 ],
     [ 0, 0, 0, 0, 1 ],
     [ 0, 0, 0, 2, 0 ] ],
     'sparse'
);

const D = [
  [1, 1, 1, 0],
  [0, 1, 0, 0],
  [1, 1, 1, 1],
  [1, 1, 1, 1],
  [0, 1, 0, 1]
]

assert.equal(fun(H3, K, D), fun(H3_, K, D))

Results in an assertion error.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions