News for hackers. Stuff that matters.
Re:Show HN: Advanced Mathematics Library for Node.js and Jav... by Xcelerate on Tuesday December 11, 2012 @03:48PM (#4906715)
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 < y then x else {index: i, val: y}), {index: null, val: Infinity}

        centerOfMass = (coords) ->
                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 Jav... by Osmium on Tuesday December 11, 2012 @04:01PM (#4906786)
Thanks, that's really interesting! I'll check it out next time. Do you see any advantage with this over, e.g., Python w/NumPy?
Re:Show HN: Advanced Mathematics Library for Node.js and Jav... by Xcelerate on Tuesday December 11, 2012 @04:08PM (#4906825)
I can't really comment on that actually. For some reason, of all the programming languages I know, I never bothered to learn Python. If you need to make sure your results are 100% accurate then I would recommend a library that has been designed and tested by others, so NumPy would probably be better than creating your own routines. I do really like the clean syntax and support for functional(-ish) programming that CoffeeScript provides though.