dracoblue.net

Linear least squares in Javascript

Today I ran into the problem, that I had a graph full of data points, which obviously did not form a straight line.

There is a method called

Linear least squares to calculate the straight line with least difference to the original data points.

Since I needed the method in javascript, here is what I came up with in the end.

function findLineByLeastSquares(values_x, values_y) {
    var sum_x = 0;
    var sum_y = 0;
    var sum_xy = 0;
    var sum_xx = 0;
    var count = 0;
    
    /*
     * We'll use those variables for faster read/write access.
     */
    var x = 0;
    var y = 0;
    var values_length = values_x.length;

    if (values_length != values_y.length) {
        throw new Error('The parameters values_x and values_y need to have same size!');
    }
    
    /*
     * Nothing to do.
     */
    if (values_length === 0) {
        return [ [], [] ];
    }
    
    /*
     * Calculate the sum for each of the parts necessary.
     */
    for (var v = 0; v < values_length; v++) {
        x = values_x[v];
        y = values_y[v];
        sum_x += x;
        sum_y += y;
        sum_xx += x*x;
        sum_xy += x*y;
        count++;
    }
    
    /*
     * Calculate m and b for the formular:
     * y = x * m + b
     */
    var m = (count*sum_xy - sum_x*sum_y) / (count*sum_xx - sum_x*sum_x);
    var b = (sum_y/count) - (m*sum_x)/count;
    
    /*
     * We will make the x and y result line now
     */
    var result_values_x = [];
    var result_values_y = [];
    
    for (var v = 0; v < values_length; v++) {
        x = values_x[v];
        y = x * m + b;
        result_values_x.push(x);
        result_values_y.push(y);
    }
    
    return [result_values_x, result_values_y];
}

In Algorithm, JavaScript by DracoBlue @ 2010-02-13 | 267 Words

Projects Section cleaned up

On my

projects section I added some new projects.

The projects at "Active Projects" are active in development and even lots of new features will be added.

The new projects section "Maintained Projects" instead is meant to be used for projects, which have no active feature development, but are still maintained in case of version upgrade of the parent project or if a bug is filled.

The projects at "Previous Projects" are projects, which are not developed actively and currently not maintained. They may be revived someday :).

In by DracoBlue @ 2010-01-31 | 94 Words

How to use postgres with nodeJS

With NodeJS (built on V8) it's pretty simple to access libraries written in C/C++.

Since NodeJS is 100% non-blocking I/O you'll need to tell your libraries to do the same. Even though libmysql is not yet capable of doing that, you may use postgres for this.

Ryan's node_postgres is a binary binding.

Compiling is easy as usual:

$ node-waf configure build
This may not work if a bug in node-waf is still present. So you get the error: Version mismatch: waf 1.5.9 <> wafadmin 1.5.10. To fix that edit /usr/local/bin/node-waf and change 1.5.9 to 1.5.10.

If you receive the error: The program pk_config could not be found, you'll need to install postgres dev libraries package first. On debian/ubuntu you do that the following way:

$ sudo apt-get install libpq-dev

It may fail to build then with the message: "Build failed", "cxx binding.cc -> binding_1.o". I am currently running into the same issue and will post an update as soon as I got this fixed.

There is also an pure Javascript implementation of the Postgres API available at

postgres-js.

In JavaScript, node.JS by DracoBlue @ 2009-12-23 | 179 Words

Installing jsl (JavascriptLint) on Linux

I tried to figure out how to compile and run jsl. As I just needed the binary, here is how I finally got it working in a pretty nice way:

First of all download latest JavaScript Lint.

Extract all files in that folder and go right into the src folder.

Run:

$ make -f Makefile.ref

If all dependencies are there, you'll have a folder Linux_ALL_DBG.OBJ created. Go right into that and there is a file called "jsl". Copy that file to your bin folder (~/bin, /usr/local/bin, /usr/bin, /usr/sbin ... wherever you appriciate).

Now you are capable of running:

$ jsl -process file.js

whereever you want!

In JavaScript by DracoBlue @ 2009-12-23 | 106 Words

Encode/Decode special xml characters in Javascript

When you want to convert htmlspecialchars in javascript to not so dangerous text and decode those html entities back again, you may have some convenient methods on a dom entity (like mootools .get('html') and .get('text')).

If you want to do that simple work on simple strings, I use the following functions:

var xml_special_to_escaped_one_map = {
    '&': '&amp;',
    '"': '&quot;',
    '<': '&lt;',
    '>': '&gt;'
};
 
var escaped_one_to_xml_special_map = {
    '&amp;': '&',
    '&quot;': '"',
    '&lt;': '<',
    '&gt;': '>'
};
 
function encodeXml(string) {
    return string.replace(/([\&"<>])/g, function(str, item) {
        return xml_special_to_escaped_one_map[item];
    });
};
 
function decodeXml(string) {
    return string.replace(/(&quot;|&lt;|&gt;|&amp;)/g,
        function(str, item) {
            return escaped_one_to_xml_special_map[item];
    });
}

In JavaScript, Mootools by DracoBlue @ 2009-12-23 | 160 Words

Page 21 - Page 22 - Page 23