Live data from Hacker News

Show HN: Advanced Mathematics Library for Node.js and JavaScript

github.com

11–20 of 49 posts

Re: Show HN: Advanced Mathematics Library for Node.js and JavaScript

#11

This is pretty cool. I've been using CoffeeScript to post-process MD simulation results because it's such an easy language to get things done in. Tools like this will make it even easier. Although, could someone clarify for me the floating point issues mentioned by Steve? I wasn't aware there were issues...

https://en.wikipedia.org/wiki/Floating_point#Accuracy_proble...

It is the same as trying to store 1/3 = 0.333... as a finite decimal. Basically, a floating point number only has a finite space to put the component after the decimal point, so it gets truncated and made inaccurate, just like truncating 1/3 to 0.3333 will mean that calculations become wrong: 3*0.3333 = 0.9999 ≠ 1.

Re: Show HN: Advanced Mathematics Library for Node.js and JavaScript

#12
post #5

I've used http://www.numericjs.com/ to handle basic vector/matrix arithmetic and for numerically solving minimization problems to create constraint-based UIs. Can you give an overview of how your library is different? Is it just a different set of math tools or is the architecture somehow different? Thanks!

Constraint-based UIs are not something I'm familiar with, but sound interesting. Can you expand on that for me please?

Re: Show HN: Advanced Mathematics Library for Node.js and JavaScript

#13

This is pretty cool. I've been using CoffeeScript to post-process MD simulation results because it's such an easy language to get things done in. Tools like this will make it even easier. Although, could someone clarify for me the floating point issues mentioned by Steve? I wasn't aware there were issues...

The classic paper on the subject is What Every Computer Scientist should know about Floating-Point Arithmetic.

http://www.cse.msu.edu/~cse320/Documents/FloatingPoint.pdf

Re: Show HN: Advanced Mathematics Library for Node.js and JavaScript

#14

This is pretty cool. I've been using CoffeeScript to post-process MD simulation results because it's such an easy language to get things done in. Tools like this will make it even easier. Although, could someone clarify for me the floating point issues mentioned by Steve? I wasn't aware there were issues...

Glad you like it! I'd love to know how it goes if you make use of it. In your browser console, run: 0.3 + 0.544 You'll get back 0.8440000000000001 In practice (especially if in the browser), the difference is either irrelevant (in CSS 1px == 1.1px) or not significant. But in accumulation, it could be problematic.

I should point out this is normal for floating-point numbers. Erlang and Ruby do the exact same thing:

  $ erl
  1> 0.2 + 0.1.
  0.30000000000000004
  2> 0.94 - 0.01.
  0.9299999999999999
  3> 0.3 + 0.544.
  0.8440000000000001

  $ irb
  1.9.2-p290 :001 > 0.2 + 0.1
   => 0.30000000000000004 
  1.9.2-p290 :002 > 0.94 - 0.01
   => 0.9299999999999999 
  1.9.2-p290 :003 > 0.3 + 0.544
   => 0.8440000000000001 
  1.9.2-p290 :004 >

Re: Show HN: Advanced Mathematics Library for Node.js and JavaScript

#15

This is pretty cool. I've been using CoffeeScript to post-process MD simulation results because it's such an easy language to get things done in. Tools like this will make it even easier. Although, could someone clarify for me the floating point issues mentioned by Steve? I wasn't aware there were issues...

Out of interest, what sort of MD/post-processing do you need to do? I've been in a similar position recently, but using CoffeeScript wouldn't have occurred to me.

By the by, for typical MD processing, as long as you're using sensible units you shouldn't need to worry about floating point precision. Just don't use SI units, because then you might end up with some very very small numbers and that's when it'll start to bite you!

Re: Show HN: Advanced Mathematics Library for Node.js and JavaScript

#16
post #14

Earlier quoted context omitted.

Glad you like it! I'd love to know how it goes if you make use of it. In your browser console, run: 0.3 + 0.544 You'll get back 0.8440000000000001 In practice (especially if in the browser), the difference is either irrelevant (in CSS 1px == 1.1px) or not significant. But in accumulation, it could be problematic.

I should point out this is normal for floating-point numbers. Erlang and Ruby do the exact same thing: $ erl 1> 0.2 + 0.1. 0.30000000000000004 2> 0.94 - 0.01. 0.9299999999999999 3> 0.3 + 0.544. 0.8440000000000001 $ irb 1.9.2-p290 :001 > 0.2 + 0.1 => 0.30000000000000004 1.9.2-p290 :002 > 0.94 - 0.01 => 0.9299999999999999 1.9.2-p290 :003 > 0.3 + 0.544 => 0.8440000000000001 1.9.2-p290 :004 >

That's why you don't compare floating point numbers with the = operator.

You use something like:

    fabs( a-b ) 
where epsilon is usually defined by the language as a very small quantity (example values in C are 1E-8 for a float, 1E-15 for double), or you can just use a hard coded value appropriate to the values you are comparing.

Re: Show HN: Advanced Mathematics Library for Node.js and JavaScript

#17
post #11

This is pretty cool. I've been using CoffeeScript to post-process MD simulation results because it's such an easy language to get things done in. Tools like this will make it even easier. Although, could someone clarify for me the floating point issues mentioned by Steve? I wasn't aware there were issues...

https://en.wikipedia.org/wiki/Floating_point#Accuracy_proble... It is the same as trying to store 1/3 = 0.333... as a finite decimal. Basically, a floating point number only has a finite space to put the component after the decimal point, so it gets truncated and made inaccurate, just like truncating 1/3 to 0.3333 will mean that calculations become wrong: 3*0.3333 = 0.9999 ≠ 1.

Ah, ok. Thank you. This isn't a problem for me then. It appears to be just typical floating-point math. I was thinking perhaps JavaScript did something else weird that I didn't know about.

Re: Show HN: Advanced Mathematics Library for Node.js and JavaScript

#19
post #15

This is pretty cool. I've been using CoffeeScript to post-process MD simulation results because it's such an easy language to get things done in. Tools like this will make it even easier. Although, could someone clarify for me the floating point issues mentioned by Steve? I wasn't aware there were issues...

Out of interest, what sort of MD/post-processing do you need to do? I've been in a similar position recently, but using CoffeeScript wouldn't have occurred to me. By the by, for typical MD processing, as long as you're using sensible units you shouldn't need to worry about floating point precision. Just don't use SI units, because then you might end up with some very very small numbers and that's when it'll start to…

For instance, I've been simulating some graphitic crystallites and part of the analysis includes analyzing the motion of the individual crystallite planes.

This is how easy that is:

(Note: it appears the horizontal scroll bar for this code is hidden by default for OS X browsers.)

    eigenvalues = require 'eigenvalues.js'

    regressCoordinatesToNormal = (coords) ->
        min = (array) ->
                array.reduce ((x, y, i) -> if x.val 
                coords.reduce ((x, y) -> (x[i] + (y[i] / coords.length) for v, i in y)), [0, 0, 0]

        normalUnitVector = (coords) ->
                c = centerOfMass coords
                matrix = for row in [0..2]
                        for col in [0..2]
                                ((coord[row] - c[row]) * (coord[col] - c[col]) for coord in coords).reduce (x, y) -> x + y
                results = eigenvalues.calculateEigenvalues matrix
                results.eigenvectors[min(results.real).index]

        normalUnitVector coords
This code just takes in a list of coordinates and performs a linear plane regression on them to return the normal vector of the plane. Then if I want the vertical distance between two planes of atoms (these are circular planes that kind of wobble around), then it's just:

    normals = (regressCoordinatesToNormal coords for coords in planes)
    planeToPlaneVector = distanceVector planeCenterOfMass[0], planeCenterOfMass[1]
    projectionDistance = dotProduct planeToPlaneVector, normals[0]
I left out some details, but you can get the gist of it.

Re: Show HN: Advanced Mathematics Library for Node.js and JavaScript

#20
Any interest in adding various statistical regression functions? I had to write some for JavaScript recently, one of which can be found at http://stackoverflow.com/questions/13590922/how-can-i-use-d3...

Seems to work for me, but I haven't had it reviewed by a real mathematician yet.

Post reply on HN