Skip to content

Instantly share code, notes, and snippets.

@jacksonwillis
Created June 1, 2012 19:06
Show Gist options
  • Save jacksonwillis/2854448 to your computer and use it in GitHub Desktop.
Save jacksonwillis/2854448 to your computer and use it in GitHub Desktop.
TwoByTwoMatrix
class TwoByTwoMatrix
constructor: ([[@a, @b], [@c, @d]]) ->
toString: -> "[#{@a} #{@b}\n #{@c} #{@d}]"
determinant: -> (@a * @d) - (@b * @c)
inverse: ->
D = @determinant()
new TwoByTwoMatrix [[@d/D, -@b/D], [-@c/D, @a/D]]
alert new TwoByTwoMatrix([[1, 2], [3, 4]]).inverse().toString()
var TwoByTwoMatrix;
TwoByTwoMatrix = (function() {
function TwoByTwoMatrix(_arg) {
var _ref, _ref1;
(_ref = _arg[0], this.a = _ref[0], this.b = _ref[1]), (_ref1 = _arg[1], this.c = _ref1[0], this.d = _ref1[1]);
}
TwoByTwoMatrix.prototype.toString = function() {
return "[" + this.a + " " + this.b + "\n " + this.c + " " + this.d + "]";
};
TwoByTwoMatrix.prototype.determinant = function() {
return (this.a * this.d) - (this.b * this.c);
};
TwoByTwoMatrix.prototype.inverse = function() {
var D;
D = this.determinant();
return new TwoByTwoMatrix([[this.d / D, -this.b / D], [-this.c / D, this.a / D]]);
};
return TwoByTwoMatrix;
})();
alert(new TwoByTwoMatrix([[1, 2], [3, 4]]).inverse().toString());
class TwoByTwoMatrix
([[@a, @b], [@c, @d]]) ->
toString: -> "[#{@a} #{@b}\n #{@c} #{@d}]"
determinant: -> (@a * @d) - (@b * @c)
inverse: ->
D = @determinant!
new TwoByTwoMatrix [[@d/D, -@b/D], [-@c/D, @a/D]]
alert new TwoByTwoMatrix [[1, 2], [3, 4]] .inverse!.toString!
var c;c=function(){function b(a){var b;b=a[0];this.a=b[0];this.b=b[1];a=a[1];this.c=a[0];this.d=a[1]}b.prototype.toString=function(){return"["+this.a+" "+this.b+"\n "+this.c+" "+this.d+"]"};b.prototype.e=function(){return this.a*this.d-this.b*this.c};b.prototype.inverse=function(){var a;a=this.e();return new b([[this.d/a,-this.b/a],[-this.c/a,this.a/a]])};return b}();alert((new c([[1,2],[3,4]])).inverse().toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment