Lalgebra
Introduction
Provides a series linear algebra routines.
Installation
$ npm install Lalgebra
Features
- Lalgebra linear algebra
API
Lalgebra
Initialize Lalgebra
var Lalgebra = ;
Linear Algebra
Lalgebra.matrix(Array[,row,column,opt])
Is a constructor of a object matrix, the form of Array param have to be like Array
= [[x_11,...x_1n],[x_21,...y_2n],...[x_m1,...x_mn]], the row param is the matrix row number and column is a array (can be just a number if every row has same column) with column matrix number to be build. The instance properties are row, column,array and det which are the number of row and column, the array is the array self passed to constructor.
Options Object
The options object accept are:
force:
Boolean with default values true, this options force a object if passed to be a array.
deep:
Boolean with default values true, this options force when a object if passed and it is forced to be a array with recursive steps deeply.
The Det property is obvious. The instance methods are _ , x, plus, pow, adj, inv, map, truncate, trans and scalar: the first is a method with integers parameters i,j that is the i,j member of matrix object, the second is the product by another matrix, accept as parameters matrix objects, plus method adds the object matrix to matrix parameters passed to the method, pow calculates the power of matrix and accepts as parameter the power n (integer), adj calculates the matrix adjoint, inv calculates the matrix inverse, map apply the map over matrix, truncate is a mapping that truncate the matrix's numbers to "n" parameter the digits, trans calculates the matrix transposed and finally the last calculates the scalar product with the number passed as parameter to method. The matrix constructor has the class methods adj, det, inv, minor, pscalar, sum, trans, multiply, map and pow that calculates: the adjoint, determinant, inverse, minor, scalar product, sum, transposed, multiplication, mapping, create and power, the parameters of each one are obviously. Every method return a matrix object such way that can be chained another methods.
var Matrix = matrix;var matrix =011146;var mat = ;; // return a matrix of 5x6 fulfilled with 3matrow == 2; // Truematcolumn == 2 // Truematarray ; // [[0,1.1],[1,4.6]]mat === 0 ; // Truemat // return a Matrix formed by the first row (1x2 matrix)mat // return a Matrix formed by the second row (1x2 matrix)mat // return a Matrix formed by the second column (2x1 matrix) etcmat// calculate the direct product// [// [ 0x0 , 1.1 x 1.1 ]// [ 1x1 , 4.6 x 4.6 ]// mat// return the diagonal like a nx1 matrix :// [// [ 0 ]// [ 4.6]// ]matxmatmat;// [[5,24.5],[22.3,107.5]] or chainedmatxmatxmat // etcmat // return a vector formed with the row (up-down) [0,1.1,1,4.6] mat // return a vector formed with the columns (right-left) [0,1,1.1,4.6] mat // [[0,4.4],[4,18.4]] or chainedmat // etcmat; //[[0,1],[1,4]]mat // [[0,0],[0,0]] or chainedmat // etcmat; // [[1.1,5.1],[4.6,22.3]] or chainedmat //[[2.2,10.2],[9.2,44.6]]matfilter // works like Array.prototype.filter // with the variants filterByPositionRow and filterByPositionColumnmat// calculate the direct pow// [// [ 0^3 , 1.1^3 ]// [ 1^3 , 4.6^3 ]// ]mat2 = mat// concat right// [// [ 0 , 1.1 , 0 , 1]// [ 1 , 4.6 , -1 , 0]// ]mat// concat Left// [// [ 0 , 1, 0 , 1.1 ]// [ -1 , 0, 1 , 4.6 ]// ]mat// concat Down// [// [ 0 , 1.1 ]// [ 1 , 4.6 ]// [ 0 , 1 ]// [ -1 , 0 ]// ]mat// concat Up// [// [ 0 , 1 ]// [ -1 , 0 ]// [ 0 , 1.1 ]// [ 1 , 4.6 ]// ] { return x/2 ;} { return x/2 ;}var matrix1 =f1 f2;var mat1 = ; { return 2 ;} { return 3 ;}var matrix2 =f3 f4;var mat2 = ;var matrix3 =1 2;var mat3 = ;mat1// Left apply// [// [ f1(1) , f2(2) ]// ]mat1// Left apply// [// [ f1( f3() ) , f2( f4() ) ]// ]mat3// Right apply// [// [ 1*f1() , 2*f2() ]// ]// There are a important and usable variant applyByRow and applyByColumn// that work on similarly way.Matrix //[[0,0],[0,0]]Matrix //[[1,1],[1,1]]Matrix //[[1.1,5.1],[4.6,22.3]]Matrix // [[4.6,-1.1],[-1,0]] equivalent mat.adj()Matrix // [[9.2,-2.2],[-2,0]]Matrix // -1.1 equivalent mat.det()Matrix // [[-4.2,1],[0.9,0]] equivalent mat.inv()Matrix // [[4.6]] equivalent mat.minor(1,1)Matrix // [[0,2.2],[2,9.2]] equivalent mat.scalar(2)Matrix // [[0,5.5],[5,23]]Matrix // [[24.5,108.2],[107.5,518.8]]Matrix // [[0,1],[1.1,4.6]] equivalent mat.trans()//Exemple of mapping:{return item/j-i+1} ;Matrix // equivalent mat.map(mapping)mat // return a matrix with the Square root over every elementmat // iterate over every matrix's element. The mapping has to receive as params the element and indexes. matforEachColumn // Like forEach but receive the Columns as argument like nx1 matrixmatforEachRow// Like forEach but receive the Rows as argument like 1xn matrix { return i*j-1 ;}//How create a matrix of nxm.Matrix// [[0,1,2],// [1,3,5]]// If you pass a number of row and column you can build a matrix different to array given// the elements are taken cyclically from array given var C1= 23023 //var C2= 1-1334var C3= 232000var C4= 1-11-133331-11-1// here C1 and C3 are equivalent mathematically, same to C2 and C4C1xC2 // [ [ 13, 5, 13, 5 ], [ 0, 0, 0, 0 ] ] the same to C3.x(C4)// you can build a matrix without array, property that is stored as [[]]var C5 = // the row and column dimensions are 2 and 3 respectively.C5array = 4562// later is stored the array property and the matrix now is equivalent to:// [// [ 4, 5, 6, 4 ],// [ 2, 2, 2, 2 ],// [ 4, 5, 6, 4 ]// ]// The column can be differentvar C6= 237334// This generates a matrix like// [// [ 2, 3, 2 ],// [ 7, 7, 7, 7 ],// [ 2, 3, 2 ]// ]// the arrays are converted to column vectors or nx1 matrixvar // is equivalent matrix with array// [// [2],// [3],// [4],// [5],// ]// If you try make operations with not matrix objects// they are tried to convert to matrix.matx5 // this is equivalent mat.x(Matrix([[5]])) // return {b:'hola'}// if the param to filter is not a function, is tried to convert to array to generate a matrix Object// their elements are used like Boolean to make the filter.// toObject try to convert the matrix to array var AA = assert // And obtain the elements of matrix elements with the tensor notationassert // trueassert// trueassert// true var _B = // =>// [// [ 0 , 0 ],// [ 0 , -1 ],// [ , , 1, 0 ],// [ , , 3, 2 ]// ] assert; // should returns true
Lalgebra.vector(Array)
Constructor of a vector object with instance property array that is the array self passed as parameter, matrix (Here the vectors are matrixes of nx1) and the instance methods dot(Vector)
that calculates the dot product, sum(Vector[,Vector,...])
, pscalar(Number)
and cross(Vector)
that calculates the cross product. In another hand the constructor has the class method: dotp(Vector,Vector)
, sum(Vector,Vector[,Vector...])
, scalarp(Number,Vector)
and crossp(Vector,Vector[,Vector,...])
. Here the vectors behave as nx1 matrix, because of has all the methods and properties of matrix in matrix property.
var Vector = vector;var vector =0115;var V = ;V; // [0,2.2,10] equivalent Vector.sum(V,V)V ; //[0,2.2,10] equivalent Vector.pscalar(2,V)V; // 26.21 equivalent Vector.dot(V.V)V; // [0,0,0] equivalent Vector.cross(V,V),remember this // operation is only defined for three dimension vectors.// How create a vector of n dimension. {return n*n-4;}Vector // [-3,0,5,12]// mapping a vector {return n*item;}V;// [0,2.2,15] equivalent Vector.map(mapp,V)
Lalgebra.AL.solveLE(Array,Array)
Solve the linear equation system:
a_11x_1+a_12 x_2+...a_1n x_n = b_1
. .
. .
. .
a_n1x_1+a_n2 x_2+...a_nn x_n = b_n
to do that is necessary pass the matrix [[a_11,a_12...a_1n]...,[a_n1,a_n2...a_nn]]firstly and the result array [b_1,b_2...,b_n]. Return the array solution for the system [x_1,x_2,...,x_n].
var solveLE = solveLE;var mat = 0116146-5010-09 ;var result = 560; ; //[6.36,0.68,0.7,]
Contributing
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code. For any bugs report please contact to me via e-mail: dev@futurecommerce.mx.
Licence
The MIT License (MIT)
Copyright (c) 2015 Jesús Edel Cereceres with Andrés González and Marco Godínez as collaborators, 4yopping and all the related trademarks.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.