Skip to content

Complex bundling, ComplexInfinity and floor #2114

@PFPF

Description

@PFPF

Hi! While using the library, I had a few questions/comments:
(1) Is there a way to only load the parts about numbers and complex numbers? Basically I only need functions from ℂ to ℂ (complex abs, sqrt, log etc, maybe also logicals), never need things like matrices and units. I'm not aiming to reduce the bundle size, but mainly for performance, and I hope to get rid of the unnecessary branchings (and also prevent some risks like #2088). Do you have suggestions about what I can do?
(2) It threw me off today that math.evaluate("i/0").toString() and math.evaluate("-(i/0)").toString() are both "Infinity"---the same "Infinity" as the actually-negatable Infinity. Can we call it something else like ComplexInfinity?
(3) Just a sidenote (I already fixed it in my code :)): I think the performance of math.floor has some potential to improve:

let c = -1e-200;
console.time('mathjs'); for(let i=0;i<10000;i++) math.floor(c); console.timeEnd('mathjs'); // around 500ms
console.time('builtin'); for(let i=0;i<10000;i++) Math.floor(c); console.timeEnd('builtin'); // around 1ms

Looking at the code, I think the main bottleneck---when the input is a number---is the two(!) calls of math.round (which is understandably pretty slow). I'll suggest the following workaround: when x has type number, we know (hopefully) that math.round(x) is equal to either Math.floor(x) or Math.ceil(x), so just try both of them! In the worst case, we call Math.floor and Math.ceil once and nearlyEqual twice, which still seems to be much faster than calling math.round.
Also, to me it makes more sense if this nearlyEqual business is off by default in math.floor. Currently it's off for complex numbers anyway (math.floor(math.Complex(-1e-16, -1e-16)) == Complex(-1, -1), as compared with math.floor(-1e-16) == 0). I'm leaning towards letting floor be consistent with js, and call the one with nearlyEqual math.toleratingFloor or something similar. That's just my personal preference though.

Pardon me for the wall of text! Many thanks for this awesome library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions