Create Matrix class - Node.js Data Structure

Node.js examples for Data Structure:Matrix

Description

Create Matrix class

Demo Code

  Matrix: function (a, b, c, d, e, f) {
    var Matrix = FractalJS.util.Matrix;
    if (f === undefined) {
      this.a = 1;// ww w . j  a  v  a2  s.  c o  m
      this.c = 0;
      this.e = 0;
      this.b = 0;
      this.d = 1;
      this.f = 0;
    } else {
      this.a = a;
      this.c = c;
      this.e = e;
      this.b = b;
      this.d = d;
      this.f = f;
    }
    this.isInvertible = function () {
      var deter = this.a * this.d - this.b * this.c;
      return Math.abs(deter) > 1e-15;
    };
    this.inverseGaussJordan = function () {
      function gje(M, c1i, c2i, f) {
        var c1 = M[c1i];
        var c2 = M[c2i];
        for (var i = 0; i < 6; i++) {
          // console.log("multiply factor", f, "by member", c2[i])
          c1[i] += c2[i] * f;
        }
      }

      function gjet(M, c1i, f) {
        var c1 = M[c1i];
        for (var i = 0; i < 6; i++) {
          // console.log("multiply factor", f, "by member", c1[i], "res", c1[i] * f)
          c1[i] = c1[i] * f;
        }
      }
      var M = [
        [a, c, e, 1, 0, 0],
        [b, d, f, 0, 1, 0],
        [0, 0, 1, 0, 0, 1]
      ];
      // console.log("START\n"+str(M));
      gje(M, 1, 2, -M[1][2]); // c2 = c2 + c3 * -f
      // console.log("c2=c2-fc3\n"+str(M));
      gje(M, 0, 2, -M[0][2]); // c1 = c1 + c3 * -e
      // console.log("c2=c2-ec3\n"+str(M));
      gje(M, 1, 0, -M[1][0] / M[0][0]);
      // console.log("c2=c2-?c3\n"+str(M));
      gje(M, 0, 1, -M[0][1] / M[1][1]);
      // console.log("c2=c2-?c3\n"+str(M));
      gjet(M, 0, 1 / M[0][0]);
      // console.log("c1 norm\n"+str(M));
      gjet(M, 1, 1 / M[1][1]);
      // console.log("c1 norm\n"+str(M));
      return new Matrix(M[0][3], M[1][3], M[0][4], M[1][4], M[0][5], M[1][5]);
    };
    this.inverse = function () {
      if (!this.isInvertible()) {
        return this.inverseGaussJordan();
      } else {
        var a = this.a,
          b = this.b,
          c = this.c,
          d = this.d,
          e = this.e,
          f = this.f;
        var dt = a * d - b * c;
        return new Matrix(d / dt, -b / dt, -c / dt, a / dt, (c * f - d * e) / dt, -(a * f - b * e) / dt);
      }
    };
    this.multiply = function (o) {
      return new Matrix(
        this.a * o.a + this.c * o.b,
        this.b * o.a + this.d * o.b,
        this.a * o.c + this.c * o.d,
        this.b * o.c + this.d * o.d,
        this.a * o.e + this.c * o.f + this.e,
        this.b * o.e + this.d * o.f + this.f
      );
    };
    this.rotate = function (angle) {
      var cos = Math.cos(angle),
        sin = Math.sin(angle);
      return this.multiply(new Matrix(cos, sin, -sin, cos, 0, 0));
    };
    this.isIdentity = function () {
      return this.a === 1 && this.b === 0 && this.c === 0 && this.d === 1 && this.e === 0 && this.f === 0;
    };
    this.clone = function (angle) {
      return new Matrix(this.a, this.b, this.c, this.d, this.e, this.f);
    };
    this.applyTo = function (x, y) {
      return {
        x: x * this.a + y * this.c + this.e,
        y: x * this.b + y * this.d + this.f
      };
    };
    // this method is used to pass a matrix to a worker
    this.params = function () {
      return {
        a: a,
        b: b,
        c: c,
        d: d,
        e: e,
        f: f
      };
    };
  }

};

Related Tutorials