Skip to content

Instantly share code, notes, and snippets.

@xaviervia
Last active August 29, 2015 14:04
Show Gist options
  • Save xaviervia/34c4bd3c40f6f3025690 to your computer and use it in GitHub Desktop.
Save xaviervia/34c4bd3c40f6f3025690 to your computer and use it in GitHub Desktop.
Playing with mathjs Matrices
// sudo npm install -g washington
// npm install mathjs
// washington index
var math = require("mathjs")
math.type.Matrix.prototype.toString = function () {
var string = " ┌ "
for (var i = 0; i < this.size()[1]; i ++)
string += " "
string += "┐"
this.forEach((function (value, index) {
if (index[1] == 0)
string += "\n │ "
string += value + " "
if (index[1] + 1 == this.size()[1])
string += "│"
}).bind(this))
string += "\n └ "
for (var i = 0; i < this.size()[1]; i ++)
string += " "
string += "┘"
return string
}
var arena = require("washington")
arena("Multiplying something by the identity matrix", function () {
var matrix = math.matrix(
[ [1, 1, 0],
[0, 0, 1],
[0, 1, 0] ] )
var displace = math.matrix(
[ [0, 1, 0],
[0, 0, 1],
[1, 0, 0] ] )
var identity = math.eye(3)
console.log("Original:")
console.log(matrix.toString())
console.log("Identity:")
console.log(identity.toString())
console.log("Displace:")
console.log(displace.toString())
console.log("Multiplication by identity:")
console.log(math.multiply(matrix, identity).toString())
console.log("Multiplication by displace:")
console.log(math.multiply(matrix, displace).toString())
arena("Determinant is " + math.det(identity))
})
arena("Displace one to the bottom", function () {
var matrix = math.matrix([
[4, 5, 3],
[2, 4, 5],
[8, 7, 4]
])
console.log("Source:")
console.log(matrix.toString())
var displace = math.matrix([
[0, 0, 1],
[1, 0, 0],
[0, 1, 0]
])
console.log("One down:")
console.log(displace.toString())
arena("Displaced down:\n" +
math.multiply(matrix, displace).toString())
})
arena("Lets try displacing a vector", function () {
var vector = math.matrix([
[1, 2, 3]
])
console.log("Vector:")
console.log(vector.toString())
var displace = math.matrix([
[0, 1, 0],
[0, 0, 1],
[1, 0, 0]
])
console.log("Displace right:")
console.log(displace.toString())
console.log("Right:")
console.log(math.multiply(vector, displace).toString())
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment