diff --git a/lib/function/matrix/concat.js b/lib/function/matrix/concat.js index d63cd6248..dfdf86990 100644 --- a/lib/function/matrix/concat.js +++ b/lib/function/matrix/concat.js @@ -37,7 +37,7 @@ function factory (type, config, load, typed) { * @param {... Array | Matrix} args Two or more matrices * @return {Array | Matrix} Concatenated matrix */ - return typed('concat', { + var concat = typed('concat', { // TODO: change signature to '...Array | Matrix, dim?' when supported '...Array | Matrix | number | BigNumber': function (args) { var i; @@ -104,6 +104,10 @@ function factory (type, config, load, typed) { return asMatrix ? matrix(res) : res; } }); + + concat.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return concat; } /** diff --git a/lib/function/matrix/cross.js b/lib/function/matrix/cross.js index b9e60d06e..f94bc1f92 100644 --- a/lib/function/matrix/cross.js +++ b/lib/function/matrix/cross.js @@ -36,7 +36,7 @@ function factory (type, config, load, typed) { * @param {Array | Matrix} y Second vector * @return {Array | Matrix} Returns the cross product of `x` and `y` */ - return typed('cross', { + var cross = typed('cross', { 'Matrix, Matrix': function (x, y) { return matrix(_cross(x.toArray(), y.toArray())); }, @@ -52,6 +52,10 @@ function factory (type, config, load, typed) { 'Array, Array': _cross }); + cross.toTex = '\\left(${args[0]}\\right)\\times\\left(${args[1]}\\right)'; + + return cross; + /** * Calculate the cross product for two arrays * @param {Array} x First vector diff --git a/lib/function/matrix/det.js b/lib/function/matrix/det.js index d4bc798f6..85418ee2a 100644 --- a/lib/function/matrix/det.js +++ b/lib/function/matrix/det.js @@ -37,7 +37,7 @@ function factory (type, config, load, typed) { * @param {Array | Matrix} x A matrix * @return {number} The determinant of `x` */ - return typed('det', { + var det = typed('det', { 'any': function (x) { return object.clone(x); }, @@ -91,6 +91,10 @@ function factory (type, config, load, typed) { } }); + det.toTex = '\\det\\left(${args[0]}\\right)'; + + return det; + /** * Calculate the determinant of a matrix * @param {Array[]} matrix A square, two dimensional matrix diff --git a/lib/function/matrix/diag.js b/lib/function/matrix/diag.js index 7d3b7670d..9d2010ae1 100644 --- a/lib/function/matrix/diag.js +++ b/lib/function/matrix/diag.js @@ -45,7 +45,7 @@ function factory (type, config, load, typed) { * * @returns {Matrix | Array} Diagonal matrix from input vector, or diagonal from input matrix. */ - return typed('diag', { + var diag = typed('diag', { // FIXME: simplify this huge amount of signatures as soon as typed-function supports optional arguments 'Array': function (x) { @@ -97,6 +97,10 @@ function factory (type, config, load, typed) { } }); + diag.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return diag; + /** * Creeate diagonal matrix from a vector or vice versa * @param {Array | Matrix} x diff --git a/lib/function/matrix/dot.js b/lib/function/matrix/dot.js index 16a450c1e..c0ad1e279 100644 --- a/lib/function/matrix/dot.js +++ b/lib/function/matrix/dot.js @@ -29,7 +29,7 @@ function factory (type, config, load, typed) { * @param {Array | Matrix} y Second vector * @return {number} Returns the dot product of `x` and `y` */ - return typed('dot', { + var dot = typed('dot', { 'Matrix, Matrix': function (x, y) { return _dot(x.toArray(), y.toArray()); }, @@ -44,6 +44,10 @@ function factory (type, config, load, typed) { 'Array, Array': _dot }); + + dot.toTex = '\\left(${args[0]}\\cdot${args[1]}\\right)'; + + return dot; /** * Calculate the dot product for two arrays diff --git a/lib/function/matrix/eye.js b/lib/function/matrix/eye.js index 1bcf33e10..53f409963 100644 --- a/lib/function/matrix/eye.js +++ b/lib/function/matrix/eye.js @@ -37,7 +37,7 @@ function factory (type, config, load, typed) { * * @return {Matrix | Array | number} A matrix with ones on the diagonal. */ - return typed('eye', { + var eye = typed('eye', { '': function () { return (config.matrix === 'matrix') ? matrix([]) : []; }, @@ -79,6 +79,10 @@ function factory (type, config, load, typed) { } }); + eye.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return eye; + function _eyeVector (size, format) { switch (size.length) { case 0: return format ? matrix(format) : []; diff --git a/lib/function/matrix/flatten.js b/lib/function/matrix/flatten.js index e9e94d018..fda07e9dd 100644 --- a/lib/function/matrix/flatten.js +++ b/lib/function/matrix/flatten.js @@ -24,7 +24,7 @@ function factory (type, config, load, typed) { * @param {Matrix | Array} x Matrix to be flattened * @return {Matrix | Array} Returns the flattened matrix */ - return typed('flatten', { + var flatten = typed('flatten', { 'Array': function (x) { return _flatten(clone(x)); }, @@ -35,6 +35,10 @@ function factory (type, config, load, typed) { return matrix(flat); } }); + + flatten.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return flatten; } exports.name = 'flatten'; diff --git a/lib/function/matrix/inv.js b/lib/function/matrix/inv.js index 9ecf958b7..573450b4f 100644 --- a/lib/function/matrix/inv.js +++ b/lib/function/matrix/inv.js @@ -199,6 +199,8 @@ function factory (type, config, load, typed) { } } + inv.toTex = '\\left(${args[0]}\\right)^{-1}'; + return inv; } diff --git a/lib/function/matrix/ones.js b/lib/function/matrix/ones.js index 73ce53dd7..a844a2cfa 100644 --- a/lib/function/matrix/ones.js +++ b/lib/function/matrix/ones.js @@ -39,7 +39,7 @@ function factory (type, config, load, typed) { * * @return {Array | Matrix | number} A matrix filled with ones */ - return typed('ones', { + var ones = typed('ones', { '': function () { return (config.matrix === 'array') ? _ones([]) @@ -74,6 +74,10 @@ function factory (type, config, load, typed) { } }); + ones.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return ones; + /** * Create an Array or Matrix with ones * @param {Array} size diff --git a/lib/function/matrix/range.js b/lib/function/matrix/range.js index c67ccf92b..f820dd92b 100644 --- a/lib/function/matrix/range.js +++ b/lib/function/matrix/range.js @@ -49,7 +49,7 @@ function factory (type, config, load, typed) { * @param {*} args Parameters describing the ranges `start`, `end`, and optional `step`. * @return {Array | Matrix} range */ - return typed('range', { + var range = typed('range', { // TODO: simplify signatures when typed-function supports default values and optional arguments // TODO: a number or boolean should not be converted to string here @@ -92,6 +92,10 @@ function factory (type, config, load, typed) { }); + range.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return range; + function _out(arr) { return config.matrix === 'array' ? arr : matrix(arr); } diff --git a/lib/function/matrix/resize.js b/lib/function/matrix/resize.js index d89632fb2..23cb2b956 100644 --- a/lib/function/matrix/resize.js +++ b/lib/function/matrix/resize.js @@ -38,7 +38,7 @@ function factory (type, config, load, typed) { * @return {* | Array | Matrix} A resized clone of matrix `x` */ // TODO: rework resize to a typed-function - return function resize (x, size, defaultValue) { + var resize = function resize (x, size, defaultValue) { if (arguments.length != 2 && arguments.length != 3) { throw new ArgumentsError('resize', arguments.length, 2, 3); } @@ -88,6 +88,10 @@ function factory (type, config, load, typed) { } }; + resize.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return resize; + /** * Resize a string * @param {string} str diff --git a/lib/function/matrix/size.js b/lib/function/matrix/size.js index 7db4d2a5c..b50c0de31 100644 --- a/lib/function/matrix/size.js +++ b/lib/function/matrix/size.js @@ -28,7 +28,7 @@ function factory (type, config, load, typed) { * @param {boolean | number | Complex | Unit | string | Array | Matrix} x A matrix * @return {Array | Matrix} A vector with size of `x`. */ - return typed('size', { + var size = typed('size', { 'Matrix': function (x) { // TODO: return the same matrix type as the input return matrix(x.size()); @@ -45,6 +45,10 @@ function factory (type, config, load, typed) { return (config.matrix === 'array') ? [] : matrix([]); } }); + + size.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return size; } exports.name = 'size'; diff --git a/lib/function/matrix/squeeze.js b/lib/function/matrix/squeeze.js index 32234cd4c..30a7357ef 100644 --- a/lib/function/matrix/squeeze.js +++ b/lib/function/matrix/squeeze.js @@ -35,7 +35,7 @@ function factory (type, config, load, typed) { * @param {Matrix | Array} x Matrix to be squeezed * @return {Matrix | Array} Squeezed matrix */ - return typed('squeeze', { + var squeeze = typed('squeeze', { 'Array': function (x) { return array.squeeze(object.clone(x)); }, @@ -51,6 +51,10 @@ function factory (type, config, load, typed) { return object.clone(x); } }); + + squeeze.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return squeeze; } exports.name = 'squeeze'; diff --git a/lib/function/matrix/subset.js b/lib/function/matrix/subset.js index f99fc5a30..1a65cc110 100644 --- a/lib/function/matrix/subset.js +++ b/lib/function/matrix/subset.js @@ -41,7 +41,7 @@ function factory (type, config, load, typed) { * math.matrix elements will be left undefined. * @return {Array | Matrix | string} Either the retrieved subset or the updated matrix. */ - return typed('subset', { + var subset = typed('subset', { // get subset 'Array, Index': function (value, index) { var m = matrix(value); @@ -80,6 +80,10 @@ function factory (type, config, load, typed) { 'string, Index, string, string': _setSubstring }); + subset.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return subset; + /** * Retrieve a subset of a string * @param {string} str string from which to get a substring diff --git a/lib/function/matrix/trace.js b/lib/function/matrix/trace.js index 1ef06108a..d8e6ffdef 100644 --- a/lib/function/matrix/trace.js +++ b/lib/function/matrix/trace.js @@ -137,6 +137,8 @@ function factory (type, config, load, typed) { } throw new RangeError('Matrix must be square (size: ' + format(size) + ')'); }; + + trace.toTex = '\\mathrm{tr}\\left(${args[0]}\\right)'; return trace; } diff --git a/lib/function/matrix/transpose.js b/lib/function/matrix/transpose.js index e09117a86..2f2496c64 100644 --- a/lib/function/matrix/transpose.js +++ b/lib/function/matrix/transpose.js @@ -4,6 +4,7 @@ var clone = require('../../util/object').clone; var format = require('../../util/string').format; function factory (type, config, load, typed) { + var latex = require('../../util/latex'); var matrix = load(require('../../type/matrix/function/matrix')); @@ -166,6 +167,8 @@ function factory (type, config, load, typed) { }); }; + transpose.toTex = '\\left(${args[0]}\\right)' + latex.operators['transpose']; + return transpose; } diff --git a/lib/function/matrix/zeros.js b/lib/function/matrix/zeros.js index 0dc6f28c0..44c9e9bc7 100644 --- a/lib/function/matrix/zeros.js +++ b/lib/function/matrix/zeros.js @@ -37,7 +37,7 @@ function factory (type, config, load, typed) { * * @return {Array | Matrix} A matrix filled with zeros */ - return typed('zeros', { + var zeros = typed('zeros', { '': function () { return (config.matrix === 'array') ? _zeros([]) @@ -72,6 +72,10 @@ function factory (type, config, load, typed) { } }); + zeros.toTex = '\\mathrm{${name}}\\left(${args}\\right)'; + + return zeros; + /** * Create an Array or Matrix with zeros * @param {Array} size diff --git a/lib/util/latex.js b/lib/util/latex.js index 7e54daf4f..79ad90502 100644 --- a/lib/util/latex.js +++ b/lib/util/latex.js @@ -116,25 +116,6 @@ exports.functions = { 2: '\\left(\\left(${args[0]}\\right)${args[1]}\\right)' }, - //matrix - 'concat': exports.defaultTemplate, - 'cross': '\\left(${args[0]}\\right)\\times\\left(${args[1]}\\right)', - 'det': '\\det\\left(${args[0]}\\right)', - 'diag': exports.defaultTemplate, - 'dot': '\\left(${args[0]}\\cdot${args[1]}\\right)', - 'eye': exports.defaultTemplate, - 'flatten': exports.defaultTemplate, - 'inv': '\\left(${args[0]}\\right)^{-1}', - 'ones': exports.defaultTemplate, - 'range': exports.defaultTemplate, - 'resize': exports.defaultTemplate, - 'size': exports.defaultTemplate, - 'squeeze': exports.defaultTemplate, - 'subset': exports.defaultTemplate, - 'trace': '\\mathrm{tr}\\left(${args[0]}\\right)', - 'transpose': '\\left(${args[0]}\\right)' + exports.operators['transpose'], - 'zeros': exports.defaultTemplate, - //probability 'combinations': '\\binom{${args[0]}}{${args[1]}}', 'distribution': exports.defaultTemplate,