Created
June 1, 2012 19:06
-
-
Save jacksonwillis/2854448 to your computer and use it in GitHub Desktop.
TwoByTwoMatrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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