{"id":15171935,"url":"https://github.com/generic-matrix/javascript_numpy","last_synced_at":"2025-10-01T06:32:08.058Z","repository":{"id":57283386,"uuid":"227083069","full_name":"generic-matrix/JavaScript_Numpy","owner":"generic-matrix","description":"This library provides a conversion of Numpy library in JavaScript","archived":false,"fork":true,"pushed_at":"2019-12-16T19:07:33.000Z","size":152,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-15T08:53:11.765Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"NikhilAshodariya/JavaScript_Numpy","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/generic-matrix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-10T09:51:16.000Z","updated_at":"2019-12-16T19:07:35.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/generic-matrix/JavaScript_Numpy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/generic-matrix%2FJavaScript_Numpy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/generic-matrix%2FJavaScript_Numpy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/generic-matrix%2FJavaScript_Numpy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/generic-matrix%2FJavaScript_Numpy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/generic-matrix","download_url":"https://codeload.github.com/generic-matrix/JavaScript_Numpy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234837148,"owners_count":18894547,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-27T09:21:40.211Z","updated_at":"2025-10-01T06:32:07.731Z","avatar_url":"https://github.com/generic-matrix.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JavaScript Numpy\n\nThis project aims at providing equivalent of Numpy in JavaScript (A fork of https://github.com/NikhilAshodariya/JavaScript_Numpy)\nOptimized for generics.js \nhttps://www.npmjs.com/package/generics.js\n\n## Prerequisites\n\nNode.js, matrix_deep_clone\n\n## Installing\n\nFirst, head over to nodejs.org and install Node.js.\n\n```\n$ npm install @kaustubhbx/jsnumpy\n```\n\n## Running the tests\n\nFirst install the module matrix_deep_clone then type\n\n```\n$ cd node_modules/jsnumpy/\n```\n\nAfter going inside the library type\n\n```\n$ npm test\n```\n\n## Basics\n\n### Array Creation\n\n```javascript\n\u003e var nj = require('@kaustubhbx/jsnumpy');\n\n\u003e var a = nj.fillWithNumber([2,3,4],5);\n\u003e a\n[\n  [\n    [5, 5, 5, 5],\n    [5, 5, 5, 5],\n    [5, 5, 5, 5]\n  ],\n  [\n    [5, 5, 5, 5],\n    [5, 5, 5, 5],\n    [5, 5, 5, 5]\n  ]\n]\n```\n\n**Note**: The zeroes will create an  array as needed with zeroes.\n\n```javascript\n\u003e var dim = nj.zeroes([3]);\n\u003e dim\n[0,0,0]\n```\n```javascript\n\u003e var dim = nj.zeroes([3,3]);\n\u003e dim\n[[0,0,0],[0,0,0],[0,0,0]]\n```\n\n**Note**: The Tanh,sigmoid,sigmoid derivative and tanh derivative activation function has been added.\n\nExample to use: \n```javascript\n\u003e var mat = nj.activation.tanh([1,2,2])\n\u003e mat\n[ 0.7615941559557646, 0.9640275800758169, 0.9640275800758169 ]\n```\n\n\n**Note**: The fillWithNumber take two parameter the first parameter is the dimension of the required matrix in the above example the function creates a 3D matrix with three rows and four columns it is similar to Numpy function of Python. The default parameter to fill in array is zero. Here number 5 is used to fill the array.\n\n```javascript\n\u003e var dim = nj.get_Dimensions(a);\n\u003e dim\n[2,3,4]\n```\n\n**NOTE**: The function get_Dimensions returns the dimension of the given matrix.\n\n```javascript\n\u003e var dim = [3,3]\n\u003e var minRange = 3;\n\u003e var maxRange = 25;\n\u003e var ans = nj.generateRandomNumbers(dim,minRange,maxRange);\n\u003e ans\n[\n  [22, 10, 20],\n  [5, 8, 11],\n  [10, 8, 22]\n]\n```\n\n**NOTE**: The function dot returns dot product if it's A 2D matrix then matrix multiplication.\n\n```javascript\n\u003e var A = nj.dot([1,2,3],[4,5,6]);\n\u003e ans\n32\n```\n\n**Note**: The function `generateRandomNumbers` provides a matrix of random numbers depending on the input dimension which is the first parameter of the function the default value for minRange is zero and maxRange is totalNumber. It can also generate an ndArray.\n\n### Deep cloning an array\n\n```javascript\n\u003e var a = [[1,2,3],[4,5,6],[7,8,9]];\n\u003e var co = nj.copy(a);\n\u003e co\n[[1,2,3],[4,5,6],[7,8,9]]\n\n\u003e console.log(co===a);\nfalse\n```\n\n## Numerical Range\n\n### linspace\n\n```javascript\nnj.linspace(start, stop, noOfSample);\n```\n\n**NOTE** : The default value of noOfSample is ten. If the value of start or stop is not provided then it gives an error.\n\n```javascript\n\u003e var a = nj.linspace(2, 3);\n[ 2, 2.111111111111111, 2.2222222222222223, 2.3333333333333335, 2.4444444444444446, 2.555555555555556, 2.666666666666667, 2.777777777777778, 2.8888888888888893, 3.0000000000000004]\n```\n\n**NOTE** : In the above example since value of noOfSample is not provided so it is taken as ten.\n\n```javascript\n\u003e var a = nj.linspace(6, 7, 4);\n[ 6, 6.333333333333333, 6.666666666666666, 7 ];\n```\n\n**NOTE** : In the above example the value of noOfSample is four so `nj.linspace` gives four samples which are equally spaced between start and end value.\n\n### arange\n\n```javascript\nnj.arange(start, stop, step = 1);\n```\n\n**NOTE** : The default value of step is one.\n\n```javascript\n\u003e var a = nj.arange(4);\n\u003e a\n[ 0, 1, 2, 3, 4 ]\n```\n\n**NOTE** : When only one parameter is passed then it is taken as last value of the array and the start value is then as zero and step is taken as one.\n\n```javascript\n\u003e var a = nj.arange(2,5);\n\u003e a\n[ 2, 3, 4, 5 ]\n```\n\n**NOTE** : In the above example the start is two and end is five and the default value of step is one. Since step is not provided therefore it's default value is taken.\n\n```javascript\n\u003e var a = nj.arange(4,9,2);\n\u003e a\n[ 4, 6, 8 ]\n```\n\n**NOTE** : In the above example the start is four and the end is nine and step is two.\n\n## Matrix Manipulation\n\n```javascript\n\u003e var ans = nj.generateRandomNumbers(16,3,25);\n\u003e var res = nj.reshape(ans,[4,2,2]);\n\u003e res\n[ [ [ 13, 3 ], [ 21, 16 ] ],\n  [ [ 25, 4 ], [ 9, 15 ] ],\n  [ [ 20, 12 ], [ 17, 14 ] ],\n  [ [ 5, 22 ], [ 11, 24 ] ] ]\n\n\u003e ans = nj.flatten(res);\n\u003e ans\n[ 13, 3, 21, 16, 25, 4, 9, 15, 20, 12, 17, 14, 5, 22, 11, 24 ]\n```\n\n**Note** : In `reshape` function the first parameter is the data and the second parameter is the new dimension to which the data needs to be reshaped. The function `flatten` converts the matrix into a single Array.\n\n## Arithmetic Operation\n\n### Addition\n\n```javascript\n\u003e var a =  [\n  [3, 5, 2],\n  [2, 4, -2],\n  [7, 1, 1]\n];\n\n\u003e var c = [\n  [2, 3, 4],\n  [5, 32, 2],\n  [5, 3, 2]\n];\n\n\u003e var ans = nj.add(a,c);\n\u003e ans\n[\n  [5, 8, 6],\n  [7, 36, 0],\n  [12, 4, 3]\n]\n```\n\nThe `add` function provides adds the first row of matrix a i.e. [3, 5, 5] with the corresponding first row of matrix c i.e. [2, 3, 4], second row of matrix a i.e. [2,4,-2] with the corresponding second row of matrix c i.e. [5, 32, 2] and so on.\n\n```javascript\n\u003e var a = [\n  [3, 5, 2],\n  [2, 4, -2],\n  [7, 1, 1]\n];\n\n\u003e var b = [\n  [1],\n  [2],\n  [3]\n];\n\n\u003e var ans = nj.add(a,b);\nans\n[\n  [4, 6, 3],\n  [4, 6, 0],\n  [10, 4, 4]\n]\n```\n\n**NOTE**: In the above example the `add` function adds the first row of matrix b i.e. one(1) to the first row of matrix a. It performs the same operation on rest of the rows i.e. it adds two(2) to the second row of the matrix a and so on.\n\n```javascript\n\u003e var a = [\n  [3, 5, 2],\n  [2, 4, -2],\n  [7, 1, 1]\n];\n\n\u003e var b = [1, 2, 3];\n\n\u003e var ans = nj.add(a,b);\nans\n[\n  [4, 7, 5],\n  [3, 6, 1],\n  [8, 3, 4]\n]\n```\n\n**NOTE**: In the above example the `add` function adds matrix b first row of matrix b i.e. [1, 2, 3] to the first row, second row and third row of matrix a.\n\n### Subtraction\n\n```javascript\n\u003e var a = [\n  [3, 5, 2],\n  [2, 4, -2],\n  [7, 1, 1]\n];\n\n\u003e var b = [\n  [2, 4, 6],\n  [9, 6, 8],\n  [5, 2, 14]\n];\n\n\u003e var ans = nj.subtract(a,b);\n[\n  [1, 1, -4],\n  [-7, -2, -10],\n  [2, -1, -13]\n]\n```\n\n**NOTE**: The `subtract` function has the same working mechanism like add on ndArray.\n\n### Multiply\n\n```javascript\n\u003e var a = [\n  [3, 5, 2],\n  [2, 4, -2],\n  [7, 1, 1]\n];\n\n\u003e var b = [\n  [2, 4, 6],\n  [9, 6, 8],\n  [5, 2, 14]\n];\n\n\u003e var ans = nj.multiply(a,b);\n\u003e ans\n[\n  [6, 20, 12],\n  [18, 24, -16],\n  [35, 2, 14]\n]\n```\n\n**Note** : The function `multiply` is not a matrix multiply instead it multiplies element by element an has same working mechanism as `add` function.\n\n### Division\n\n```javascript\n\n\u003e var a = [\n  [3, 5, 2],\n  [2, 4, -2],\n  [7, 1, 1]\n];\n\n\u003e var b = [\n  [2, 3, 4],\n  [5, 32, 2],\n  [5, 3, 2]\n];\n\n\u003e var ans = nj.divide(a,b);\nans\n[\n  [1.5, 1.6666666666666667, 0.5],\n  [0.4, 0.125, -1],\n  [1.4, 0.3333333333333333, 0.5]\n]\n\n\u003e var c = [1, 2, 3]\n\u003e ans = nj.divide(a,c);\nans\n[\n  [3, 2.5, 0.6666666666666666],\n  [2, 2, -0.6666666666666666],\n  [7, 0.5, 0.3333333333333333]\n]\n```\n\n**NOTE**: The `divide` function performs division for any dimension ndArray. It has same working mechanism as `add`, `subtract` and `multiply` functions.\n\n## Matrix Multiplication\n\n```javascript\n\u003e var a = [\n  [47, 10, 84, 64],\n  [33, 19, 98, 25],\n];\n\n\u003e var b = [\n  [93, 85],\n  [66, 91],\n  [95, 77],\n  [84, 30]\n];\n\n\u003e var ans = nj.matrixMultiply(a,b);\nans\n[\n  [18387, 13293],\n  [15733, 12830]\n]\n```\n\nThe `matrixMultiply` function multiplies the given matrix according to the matrix multiplication rules.\n\n```javascript\n\u003e var a = [\n  [\n    [83, 41, 41, 27],\n    [66, 63, 51, 15],\n    [10, 39, 59, 44],\n    [47, 4, 15, 97]\n  ],\n\n  [\n    [93, 50, 70, 33],\n    [32, 22, 27, 67],\n    [66, 98, 69, 81],\n    [80, 64, 1, 95]\n  ]\n];\n\n\u003e var b = [\n  [67, 52, 43, 68],\n  [81, 4, 83, 97],\n  [86, 27, 94, 12],\n  [85, 81, 17, 61]\n];\n\n\u003e var ans = nj.matrixMultiply(a,b);\nans\n[\n  [\n    [14703, 7774, 11285, 11760],\n    [15186, 6276, 13116, 12126],\n    [12643, 5833, 9961, 7855],\n    [13008, 10722, 5412, 9681]\n  ],\n  [\n    [19106, 9599, 15290, 14027],\n    [11943, 7908, 6879, 8721],\n    [25179, 12248, 18835, 19763],\n    [18705, 12138, 10461, 17455]\n  ]\n]\n```\n\n**NOTE**: The `matrixMultiply` function can multiply ndArray with the given matrix of ndArray if the shape of both the matrix are compatible else it generates an error.\n\n## Logarithm\n\nThe library supports three logarithmic functions first log10, logE, log.\n\n```javascript\n\u003e var a = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  [5, 2, 1]\n];\n\n\u003e var ans = nj.log10(a);\n\u003e ans\n[\n  [0, 0.30103, 0.47712125],\n  [0.60205999, 0.69897, 0.77815125],\n  [0.84509804, 0.90308999, 0.95424251],\n  [0.69897, 0.30103, 0]\n]\n```\n\n**Note** : The log10 function finds the log to the base 10 of the given nd-Array or of a Number\n\n```javascript\n\u003e var a = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  [5, 2, 1]\n];\n\n\u003e var ans = nj.logE(a);\n\u003e ans\n[\n  [0, 0.69314718, 1.09861229],\n  [1.38629436, 1.60943791, 1.79175947],\n  [1.94591015, 2.07944154, 2.19722458],\n  [1.60943791, 0.69314718, 0]\n]\n```\n\n**Note** : The logE function finds the log to the base e of the given nd-Array or of a Number.\n\n### Log to the base n\n\n```javascript\n\u003e var a = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  [5, 2, 1]\n];\n\u003e var ans = nj.log(a,7);\n[\n  [0, 0.35620719, 0.56457503],\n  [0.71241437, 0.82708748, 0.92078222],\n  [1, 1.06862156, 1.12915007],\n  [0.82708748, 0.35620719, 0]\n]\n```\n\n**NOTE**: The function declaration is `nj.log(exponent, base)`. In the above example we calculate the log of matrix a with base seven(7).\n\n```javascript\n\u003e var a = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  [5, 2, 1]\n];\n\n\u003e var c = [\n  [10],\n  [20],\n  [30],\n  [40]\n];\n\n\u003e var ans = nj.log(a,c);\nans\n[\n  [0, 0.30103, 0.47712125],\n  [0.46275643, 0.53724357, 0.598104],\n  [0.57212503, 0.61138514, 0.64601501],\n  [0.43629453, 0.18790182, 0]\n]\n```\n\n**NOTE**: In the above above example log is calculate for matrix a with base c. In the above operation log of first row of matrix a is calculate with base 10 i.e. [log10(1), log10(2), log10(3)], log of second row of matrix a is calculate with base 20 i.e. [log20(4), log20(5), log20(6)] and so on.\n\n**NOTE**: The library is written in such a way that in above example nj.log(a,c) is not equal to nj.log(c,a);\n\n```javascript\n\u003e var a = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  [5, 2, 1]\n];\n\n\u003e var c = [\n  [10],\n  [20],\n  [30],\n  [40]\n];\n\n\u003e var ans = nj.log(c,a);\nans\n[\n  [Infinity, 3.32192809, 2.09590327],\n  [2.16096405, 1.86135312, 1.67195002],\n  [1.7478697, 1.6356302, 1.54795164],\n  [2.29202967, 5.32192809, Infinity]\n]\n```\n\n**NOTE**: In the above example we have taken nj.log(c,a) that means in the above operation log of first row of matrix c is calculated with bases [1, 2, 3] i.e. the first row of the answer is [log1(10), log2(10), log3(10)] i.e log of ten with base one, log of ten with base two, and log of ten with base three. For the second row bases are [4, 5, 6] i.e. the second row of the anser is [log4(20), log5(20), log6(20)] i.e. log of twenty with base four, log of twenty with base five, and log of twenty with base six. For the rest rows the process remains same.\n\n**NOTE**: The log function provides great flexibility if needed.\n\n## Statistics Operation\n\n```javascript\n\u003e var a =   \n  [\n   [\n      [4, 5, -6],\n      [8, 0, 4]\n   ],\n   [\n      [10, 15, -70],\n      [-30, 70, 40]\n   ],\n   [\n      [12, 3, 10],\n      [80, 50, 40]\n   ]\n ];\n\n\u003e var ans = nj.negative(a);\n\u003e ans\n[\n  [\n    [-4, -5, 6],\n    [-8, -0, -4]\n  ],\n  [\n    [-10, -15, 70],\n    [30, -70, -40]\n  ],\n  [\n    [-12, -3, -10],\n    [-80, -50, -40]\n  ]\n]\n```\n\n**NOTE**: The `negative` function calculates the negative of each element of the given Matrix.\n\n```javascript\n\u003e var res = nj.abs(ans);\n\u003e res\n[\n  [\n    [4, 5, 6],\n    [8, 0, 4]\n  ],\n  [\n    [10, 15, 70],\n    [30, 70, 40]\n  ],\n  [\n    [12, 3, 10],\n    [80, 50, 40]\n  ]\n]\n```\n\n**NOTE**: The abs function finds the absolute value of the given matrix.\n\n```javascript\n\u003e var a = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  [5, 2, 1]\n];\n\u003e var ans = nj.mean(a);\n\u003e ans\n4.416666666666667\n```\n\nThe `mean` function calculates the mean of the matrix provided.\n\n```javascript\n\u003e var b = [\n  [2, 4, 6],\n  [9, 6, 8],\n  [5, 2, 14]\n];\n\u003e var ans = nj.mean(b,0);\n\u003e ans\n[5.333333333333333, 4, 9.333333333333334]\n```\n\n**NOTE**: `mean` function takes two parameter the first one is the data and the second parameter is the axis along which we want to find the mean. If value of the axis is not passed then mean of the entire matrix is found.\n\n```javascript\n\u003e var b = [\n  [2, 4, 6],\n  [9, 6, 8],\n  [5, 2, 14]\n];\n\u003e var ans = nj.mean(b, 1);\n\u003e ans\n[ 4, 7.666666666666667, 7 ]\n```\n\n```javascript\n\u003e ans = nj.square(a);\n\u003e ans\n[\n  [1, 4, 9],\n  [16, 25, 36],\n  [49, 64, 81],\n  [25, 4, 1]\n]\n```\n\n**NOTE**: `square` function calculates the square of each element of the given matrix.\n\n```javascript\n\u003e var b = [\n  [2, 4, 6],\n  [9, 6, 8],\n  [5, 2, 14]\n];\n\n\u003e var ans = nj.squareRoot(b);\nans\n[\n  [1.4142135623730951, 2, 2.449489742783178],\n  [3, 2.449489742783178, 2.8284271247461903],\n  [2.23606797749979, 1.4142135623730951, 3.7416573867739413]\n]\n\n\u003e ans = nj.cubeRoot(b);\n[\n  [1.2599210498948732, 1.5874010519681994, 1.8171205928321397],\n  [2.080083823051904, 1.8171205928321397, 2],\n  [1.7099759466766968, 1.2599210498948732, 2.4101422641752297]\n]\n```\n\nThe `squareRoot`, `cubeRoot` function calculates the squareRoot and cubeRoot of the given ndArray respectively.\n\n```javascript\n\u003e var b = [\n  [2, 4, 6],\n  [9, 6, 8],\n  [5, 2, 14]\n];\n\n\u003e var ans = nj.nThRoot(b,5);\nans\n[\n  [1.148698354997035, 1.3195079107728942, 1.4309690811052556],\n  [1.5518455739153596, 1.4309690811052556, 1.5157165665103982],\n  [1.379729661461215, 1.148698354997035, 1.6952182030724354]\n]\n```\n\n**NOTE**: The `nThRoot` function calculates the nth root of the data. The first parameter of `nThRoot` function is the data and the second parameter is the value whose nth root is required. `nj.nThRoot(b,5)` is not equal to `nj.nThRoot(5,b)`.\n\n```javascript\n\u003e var b = [\n  [2, 4, 6],\n  [9, 6, 8],\n  [5, 2, 14]\n];\n\n\u003e var ans = nj.nThRoot(5, b);\nans\n[\n  [2.23606797749979, 1.4953487812212205, 1.3076604860118306],\n  [1.195813174500402, 1.3076604860118306, 1.2228445449938519],\n  [1.379729661461215, 2.23606797749979, 1.1218283962540023]\n]\n```\n\n**NOTE**: The first row of the ans is [five raise to 1/2, five raise to 1/4, five raise to 1/6]. The second row of the ans is [five raise to 1/9, five raise to 1/6, five raise to 1/8]. The rest of the rows are calculated in similar way.\n\n```javascript\n\u003e ans = nj.sum(a);\n\u003e ans\n53\n```\n\nThe `sum` function returns the sum of all the elements of the matrix.\n\n```javascript\n\u003e ans = nj.totalElements(a);\n\u003e ans\n12\n```\n\nThe `totalElements` function returns the total number of elements in the given Matrix.\n\n```javascript\n\u003e ans = nj.highestElement(a);\n\u003e ans\n9\n```\n\nThe `highestElement` function returns the highest value of the matrix provided.\n\n```javascript\n\u003e ans = nj.lowestElement(a);\n\u003e ans\n1\n\n\u003e ans = nj.range(a);\n\u003e ans\n8\n\n\u003e var d = [1,2,3,4,5,6,7,8];\n\u003e ans = nj.median(d);\n\u003e ans\n4.5\n\n\u003e ans = nj.frequency(a,1);\n\u003e ans\n2\n```\n\n**Note**: In frequency the first parameter is the data and the second parameter is the data whose frequency is required.\n\n```javascript\n\u003e var a = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  [5, 2, 1]\n];\n\n\u003e var ans = nj.allFrequency(a);\n\u003e ans\n{\n  '1': 2,\n  '2': 2,\n  '3': 1,\n  '4': 1,\n  '5': 2,\n  '6': 1,\n  '7': 1,\n  '8': 1,\n  '9': 1\n}\n\u003e ans = nj.populationStandardDeviation(a);\n\u003e ans\n2.5967394084804805\n\n\u003e ans = nj.populationVariance(a);\n\u003e ans\n6.743055555555555\n```\n\n**Note**: nj.sampleVariance, nj.sampleStandardDeviation is also available\n\n```javascript\n\u003e var a = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  [5, 2, 1]\n];\n\n\u003e var ans = nj.mode(a);\n\u003e ans\n[1,2,5]\n\n\u003e ans = nj.mode([1,2,3,4,5]);\n\u003e ans\nundefined\n\n\u003e ans = nj.power(a,3);\n\u003e ans\n[\n  [1, 8, 27],\n  [64, 125, 216],\n  [343, 512, 729],\n  [125, 8, 1]\n]\n\n\u003e ans = nj.power(3,a);\n\u003e ans\n[\n  [3, 9, 27],\n  [81, 243, 729],\n  [2187, 6561, 19683],\n  [243, 9, 3]\n]\n```\n\n**NOTE**: The `power` function returns the cube of element of the matrix a. `nj.power(3,a)` gives a different result it calculates three to the power of first row of matrix a in the above example it calculates 3^1, 3^2, 3^3 for the first row for second row it calculates the three to the power of second row of matrix a in the above example it calculates 3^4, 3^5, 3^6 and so on.\n\n```javascript\n\n\u003e ans = nj.exp(a);\n\u003e ans\n[\n  [2.71828, 7.3890461584, 20.085496391455553],\n  [54.5980031309658, 148.41265995084171, 403.42716529117405],\n  [1096.6279948676927, 2980.9419458889515, 8103.034872671019],\n  [148.41265995084171, 7.3890461584, 2.71828]\n]\n```\n\n**NOTE**: The `exp` function calculates e^a i.e it calculates e raise to the power of each element of matrix a.\n\n## Matrix Operations\n\n```javascript\n\u003e var a = [\n  [1, 2, 3],\n  [4, 5, 6],\n  [7, 8, 9],\n  [5, 2, 1]\n];\n\n\u003e var ans = nj.transpose(a);\n\u003e ans\n[\n  [1, 4, 7, 5],\n  [2, 5, 8, 2],\n  [3, 6, 9, 1]\n]\n```\n\n**NOTE** : The transpose function also works for higher dimensions. The transpose function is very much similar to Numpy transpose of Python.\n\n### Find Matrix Inverse\n\n```javascript\n\u003e var a = [\n  [3, 5, 2],\n  [2, 4, -2],\n  [7, 1, 1]\n];\n\n\u003e var ans = nj.matrixInverse(a);\nans\n[\n  [-0.05263157894736842, 0.02631578947368421, 0.15789473684210525],\n  [0.14035087719298245, 0.09649122807017543, -0.08771929824561403],\n  [0.22807017543859648, -0.2807017543859649, -0.017543859649122806]\n]\n```\n\n**NOTE**: The `matrixInverse` function finds the inverse of the given ndArray. It generates an error if the determinant of the given matrix is zero.\n\n### Find Determinant\n\n```javascript\n\u003e var a = [\n  [10, 32, 34],\n  [54, 50, 26],\n  [17, 83, 91]\n];\n\u003e var ans = nj.determinant(a);\n\u003e ans\n4304\n```\n\n**NOTE**: The `determinant` function finds the Determinant of the given Matrix.\n\n```javascript\n\n\u003e var b =[\n  [34, 59, 51, 63, 69],\n  [60, 61, 21, 60, 32],\n  [51, 43, 45, 7, 50],\n  [42, 90, 46, 86, 9],\n  [83, 6, 87, 60, 35]\n];\n\n\u003e var ans = nj.determinant(b);\n\u003e ans\n-979886677\n```\n\n**NOTE**: The `determinant` function works for N*N dimension matrix.\n\n```javascript\n\u003e var a = [\n  [\n    [4, 28],\n    [66, 6]\n  ],\n\n  [\n    [68, 51],\n    [64, 5]\n  ],\n\n  [\n    [76, 96],\n    [31, 6]\n  ]\n];\n\u003e var ans = nj.determinant(a);\n\u003e ans\n[ -1824, -2924, -2520 ]\n```\n\n```javascript\n\u003e var a = [\n  [\n    [\n      [67, 71],\n      [22, 75]\n    ],\n\n    [\n      [97, 77],\n      [54, 32]\n    ],\n\n    [\n      [91, 50],\n      [79, 51]\n    ]\n  ],\n\n\n  [\n    [\n      [84, 85],\n      [88, 99]\n    ],\n\n    [\n      [33, 58],\n      [51, 50]\n    ],\n\n    [\n      [30, 8],\n      [15, 89]\n    ]\n  ]\n];\n\n\u003e var ans = nj.determinant(a);\n\u003e ans\n[\n  [3463, -1054, 691],\n  [836, -1308, 2550]\n]\n```\n\n**NOTE** : `determinant` function retains the shape of Matrix since the matrix's outer shape was of (2,3) so the ans of `determinant` is also (2,3).\n\n#### Generating Identity Matrix\n\n```javascript\n\u003e var dim = [4, 4];\n\u003e var ans = nj.generateIdentityMatrix(dim);\nans\n[\n  [1, 0, 0, 0],\n  [0, 1, 0, 0],\n  [0, 0, 1, 0],\n  [0, 0, 0, 1]\n]\n```\n\n**NOTE**: `generateIdentityMatrix` function generates an Identity matrix of the given dimension. The function also checks if the dimension given is square. The function can generate an ndArray.\n\n## Trigonometric Functions\n\n**Note** : All the functions like sin, cos, tan allows radian as well as degree.\n\n```javascript\n\u003e var a = [\n  [10, 20, 30],\n  [40, 50, 60],\n  [70, 80, 90],\n  [180, 120, 100]\n];\n\u003e var ans = nj.sin(a);\n\u003e ans\n[\n  [0.174, 0.342, 0.5],\n  [0.643, 0.766, 0.866],\n  [0.94, 0.985, 1],\n  [0, 0.866, 0.985]\n]\n\n\u003e ans = nj.sin(a,\"radian\");\n\u003e ans\n[\n  [-0.544, 0.913, -0.988],\n  [0.745, -0.262, -0.305],\n  [0.774, -0.994, 0.894],\n  [-0.801, 0.581, -0.506]\n]\n```\n\n**NOTE**: `nj.sin(a)` calculates the sin of the angles provided in matrix a. The angle are assumed to be in degree.\n\n**Note**: `nj.sin(a,\"radian\")` takes matrix a as the matrix of angle in radian. nj.cos, nj.tan also exists.\n\n```javascript\n\u003e var a = [\n  [0.1, 0.2, 0.3],\n  [0.4, 0.5, 0.6],\n  [0.7, 0.8, 0.9],\n  [0.75, 0.45, 0.1]\n];\n\u003e var ans = nj.sinInverse(a);\n\u003e ans\n[\n  [6, 12, 17],\n  [24, 30, 37],\n  [44, 53, 64],\n  [49, 27, 6]\n]\n```\n\n**Note**: The output of sinInverse, cosInverse, tanInverse is in degree.\n\n```javascript\n\u003e var a = [\n  [0.1, 0.2, 0.3],\n  [0.4, 0.5, 0.6],\n  [0.7, 0.8, 0.9],\n  [0.75, 0.45, 0.1]\n];\n\n\u003e var ans = nj.hyperbolicSine(a);\n\u003e ans\n[\n  [0.10016675001984403, 0.20133600254109402, 0.3045202934471426],\n  [0.4107523258028155, 0.5210953054937474, 0.6366535821482412],\n  [0.7585837018395334, 0.888105982187623, 1.0265167257081753],\n  [0.82231673193583, 0.46534201693419774, 0.10016675001984403]\n]\n```\n\n**Note**: hyperbolicCosine, hyperbolicTangent also exists.\n\n## Authors\n\n- **Nikhil Ashodariya** -(\u003chttps://github.com/NikhilAshodariya\u003e)\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeneric-matrix%2Fjavascript_numpy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeneric-matrix%2Fjavascript_numpy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeneric-matrix%2Fjavascript_numpy/lists"}