mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
merge: Fix spellings (#821)
* chore: remove codespell from ci * feat: add codespell workflow * fix: codespell workflow * fix: ignore spellings in directory * chore: fix spellings ./Dynamic-Programming/KadaneAlgo.js:2: contiguos ==> contiguous ./Dynamic-Programming/KadaneAlgo.js:14: posible ==> possible * chore: fix spelling ./Dynamic-Programming/SieveOfEratosthenes.js:4: upto ==> up to * chore: fix spellings ./Dynamic-Programming/MaxNonAdjacentSum.js:22: Exmaple ==> Example * chore: fix spelling ./Project-Euler/test/Problem010.test.js:4: upto ==> up to ./Project-Euler/test/Problem010.test.js:8: upto ==> up to ./Project-Euler/test/Problem010.test.js:12: upto ==> up to * chore: fix spelling ./String/AlphaNumericPalindrome.js:10: recieves ==> receives ./String/AlphaNumericPalindrome.js:10: sting ==> string ./String/AlphaNumericPalindrome.js:46: varaible ==> variable * chore: fix spelling ./String/DiceCoefficient.js:3: stings ==> strings * chore: fix spelling ./String/test/DiceCoefficient.test.js:9: atleast ==> at least * chore: fix spelling ./String/test/MaxWord.test.js:8: ba ==> be * chore: ignore `PermutateString.test.js` * chore: fix spelling ./String/test/CheckVowels.test.js:62: occurances ==> occurrences * chore: ignore `SubsequenceRecursive.js` * chore: fix spelling ./Conversions/TemperatureConversion.js:2: arguement ==> argument * chore: fix spelling ./Conversions/RailwayTimeConversion.js:7: Formate ==> Format ./Conversions/RailwayTimeConversion.js:8: Formate ==> Format * chore: remove Linear Algebra The deleted directory hosted a package which are not accepted by this repository. * Auto-update DIRECTORY.md * chore: fix spelling * chore: fix spellings * merge: Created composite Simpson's integration method. Tests included. (#819) * Created composite Simpson's integration method.Tests included * Minor corrections * Auto-update DIRECTORY.md * Styled with standard.js * chore: remove blank line * chore: remove blank line Co-authored-by: ggkogkou <ggkogkou@ggkogkou.gr> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com> * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: remove codespell from ci * feat: add codespell workflow * fix: codespell workflow * fix: ignore spellings in directory * chore: fix spellings ./Dynamic-Programming/KadaneAlgo.js:2: contiguos ==> contiguous ./Dynamic-Programming/KadaneAlgo.js:14: posible ==> possible * chore: fix spelling ./Dynamic-Programming/SieveOfEratosthenes.js:4: upto ==> up to * chore: fix spellings ./Dynamic-Programming/MaxNonAdjacentSum.js:22: Exmaple ==> Example * chore: fix spelling ./Project-Euler/test/Problem010.test.js:4: upto ==> up to ./Project-Euler/test/Problem010.test.js:8: upto ==> up to ./Project-Euler/test/Problem010.test.js:12: upto ==> up to * chore: fix spelling ./String/AlphaNumericPalindrome.js:10: recieves ==> receives ./String/AlphaNumericPalindrome.js:10: sting ==> string ./String/AlphaNumericPalindrome.js:46: varaible ==> variable * chore: fix spelling ./String/DiceCoefficient.js:3: stings ==> strings * chore: fix spelling ./String/test/DiceCoefficient.test.js:9: atleast ==> at least * chore: fix spelling ./String/test/MaxWord.test.js:8: ba ==> be * chore: ignore `PermutateString.test.js` * chore: fix spelling ./String/test/CheckVowels.test.js:62: occurances ==> occurrences * chore: ignore `SubsequenceRecursive.js` * chore: fix spelling ./Conversions/TemperatureConversion.js:2: arguement ==> argument * chore: fix spelling ./Conversions/RailwayTimeConversion.js:7: Formate ==> Format ./Conversions/RailwayTimeConversion.js:8: Formate ==> Format * chore: remove Linear Algebra The deleted directory hosted a package which are not accepted by this repository. * Auto-update DIRECTORY.md * chore: fix spelling * chore: fix spellings * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: no need to check filenames Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: ggkogkou <76820848+ggkogkou@users.noreply.github.com> Co-authored-by: ggkogkou <ggkogkou@ggkogkou.gr>
This commit is contained in:
9
.github/workflows/ci.yml
vendored
9
.github/workflows/ci.yml
vendored
@ -20,11 +20,4 @@ jobs:
|
||||
|
||||
- name: 💄 Code style
|
||||
run: npm run style
|
||||
codespell:
|
||||
name: Check for spelling errors
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: codespell-project/actions-codespell@master
|
||||
with:
|
||||
check_filenames: true
|
||||
|
||||
|
13
.github/workflows/codespell.yml
vendored
Normal file
13
.github/workflows/codespell.yml
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
name: codespell
|
||||
on: [push, pull_request]
|
||||
jobs:
|
||||
codespell:
|
||||
name: Check for spelling errors
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: codespell-project/actions-codespell@master
|
||||
with:
|
||||
# file types to ignore
|
||||
skip: "*.json,*.yml,DIRECTORY.md,PermutateString.test.js,SubsequenceRecursive.js"
|
||||
|
@ -2,7 +2,7 @@
|
||||
Problem: Given two numbers, n and k, make all unique combinations of k numbers from 1 to n and in sorted order
|
||||
|
||||
What is combinations?
|
||||
- Combinations is selecting items froms a collections without considering order of selection
|
||||
- Combinations is selecting items from a collections without considering order of selection
|
||||
|
||||
Example:
|
||||
- We have an apple, a banana, and a jackfruit
|
||||
|
@ -4,8 +4,8 @@
|
||||
some changes on hours and minutes and if the time in 'PM' it means the only
|
||||
want some changes in hour value.
|
||||
|
||||
Input Formate -> 07:05:45PM
|
||||
Output Formate -> 19:05:45
|
||||
Input Format -> 07:05:45PM
|
||||
Output Format -> 19:05:45
|
||||
|
||||
Problem & Explanation Source : https://www.mathsisfun.com/time.html
|
||||
*/
|
||||
|
@ -1,5 +1,5 @@
|
||||
// This files has functions to convert different temperature units
|
||||
// Functions take temperature value as a arguement and returns corresponding converted value
|
||||
// Functions take temperature value as a argument and returns corresponding converted value
|
||||
|
||||
const celsiusToFahrenheit = (celsius) => {
|
||||
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
|
||||
@ -40,7 +40,7 @@ const fahrenheitToRankine = (fahrenheit) => {
|
||||
const kelvinToCelsius = (kelvin) => {
|
||||
// Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin
|
||||
// Wikipedia reference: https://en.wikipedia.org/wiki/Celsius
|
||||
return Math.round((kelvin) - 273.15)
|
||||
return Math.round((kelvin) - 273.15)
|
||||
}
|
||||
|
||||
const kelvinToFahrenheit = (kelvin) => {
|
||||
|
@ -135,12 +135,6 @@
|
||||
* [SHA1](https://github.com/TheAlgorithms/Javascript/blob/master/Hashes/SHA1.js)
|
||||
* [SHA256](https://github.com/TheAlgorithms/Javascript/blob/master/Hashes/SHA256.js)
|
||||
|
||||
## Linear-Algebra
|
||||
* src
|
||||
* [la_lib](https://github.com/TheAlgorithms/Javascript/blob/master/Linear-Algebra/src/la_lib.js)
|
||||
* test
|
||||
* [test](https://github.com/TheAlgorithms/Javascript/blob/master/Linear-Algebra/test/test.js)
|
||||
|
||||
## Maths
|
||||
* [Abs](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Abs.js)
|
||||
* [AliquotSum](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/AliquotSum.js)
|
||||
|
@ -2,7 +2,7 @@
|
||||
* [NumberOfLocalMaximumPoints](https://www.geeksforgeeks.org/find-indices-of-all-local-maxima-and-local-minima-in-an-array/) is an algorithm to find relative bigger numbers compared to their neighbors
|
||||
*
|
||||
* Notes:
|
||||
* - like the other similar local maxima search function find relative maxima points in array but doesnt stop at one but returns total point count
|
||||
* - like the other similar local maxima search function find relative maxima points in array but doesn't stop at one but returns total point count
|
||||
* - runs on array A of size n and returns the local maxima count using divide and conquer methodology
|
||||
*
|
||||
* @complexity: O(n) (on average )
|
||||
|
@ -1,17 +1,17 @@
|
||||
import { LocalMaximomPoint } from '../LocalMaximomPoint'
|
||||
|
||||
describe('LocalMaximomPoint tests', () => {
|
||||
it('test boundry maximom points - last element', () => {
|
||||
describe('LocalMaximumPoint tests', () => {
|
||||
it('test boundary maximum points - last element', () => {
|
||||
const Array = [1, 2, 3, 4, 5, 6, 12]
|
||||
expect(LocalMaximomPoint(Array)).toEqual(6)
|
||||
})
|
||||
|
||||
it('test boundry maximom points - first element', () => {
|
||||
it('test boundary maximum points - first element', () => {
|
||||
const Array2 = [13, 6, 5, 4, 3, 2, 1]
|
||||
expect(LocalMaximomPoint(Array2)).toEqual(0)
|
||||
})
|
||||
|
||||
it('test boundry maximom points - should find first maximom point from the top', () => {
|
||||
it('test boundary maximum points - should find first maximom point from the top', () => {
|
||||
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
|
||||
const Array = [13, 2, 3, 4, 5, 6, 12]
|
||||
expect(LocalMaximomPoint(Array)).toEqual(6)
|
||||
|
@ -1,41 +1,41 @@
|
||||
import { NumberOfLocalMaximumPoints } from '../NumberOfLocalMaximumPoints'
|
||||
|
||||
describe('LocalMaximomPoint tests', () => {
|
||||
it('test boundry maximom points - last element', () => {
|
||||
it('test boundary maximum points - last element', () => {
|
||||
const Array = [1, 2, 3, 4, 5, 6, 12]
|
||||
expect(NumberOfLocalMaximumPoints(Array)).toEqual(1)
|
||||
})
|
||||
|
||||
it('test boundry maximom points - first element', () => {
|
||||
it('test boundary maximum points - first element', () => {
|
||||
const Array = [13, 6, 5, 4, 3, 2, 1]
|
||||
expect(NumberOfLocalMaximumPoints(Array)).toEqual(1)
|
||||
})
|
||||
|
||||
it('test boundry maximom points - both boundries have maximum points', () => {
|
||||
it('test boundary maximum points - both boundaries have maximum points', () => {
|
||||
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
|
||||
const Array = [13, 2, 3, 4, 5, 6, 12]
|
||||
expect(NumberOfLocalMaximumPoints(Array)).toEqual(2)
|
||||
})
|
||||
|
||||
it('multiple maximom points in the middle', () => {
|
||||
it('multiple maximum points in the middle', () => {
|
||||
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
|
||||
const Array = [1, 3, 2, 5, 6, 9, 2, 7, 12, 1, 0]
|
||||
expect(NumberOfLocalMaximumPoints(Array)).toEqual(3)
|
||||
})
|
||||
|
||||
it('multiple maximom points in the middle with one at end', () => {
|
||||
it('multiple maximum points in the middle with one at end', () => {
|
||||
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
|
||||
const Array = [1, 3, 2, 5, 6, 9, 2, 7, 12, 1, 10]
|
||||
expect(NumberOfLocalMaximumPoints(Array)).toEqual(4)
|
||||
})
|
||||
|
||||
it('multiple maximom points in the middle with one at start', () => {
|
||||
it('multiple maximum points in the middle with one at start', () => {
|
||||
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
|
||||
const Array = [10, 3, 2, 5, 6, 9, 2, 7, 12, 1, 0]
|
||||
expect(NumberOfLocalMaximumPoints(Array)).toEqual(3)
|
||||
})
|
||||
|
||||
it('multiple maximom points in the middle with two more at both ends', () => {
|
||||
it('multiple maximum points in the middle with two more at both ends', () => {
|
||||
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
|
||||
const Array = [10, 3, 11, 5, 6, 9, 2, 7, 12, 1, 10]
|
||||
expect(NumberOfLocalMaximumPoints(Array)).toEqual(5)
|
||||
|
@ -57,7 +57,7 @@ class MinPriorityQueue {
|
||||
output(this.heap.slice(1))
|
||||
}
|
||||
|
||||
// heap reverse can be done by performing swaping the first
|
||||
// heap reverse can be done by performing swapping the first
|
||||
// element with the last, removing the last element to
|
||||
// new array and calling sink function.
|
||||
heapReverse () {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/* Kadane's algorithm is one of the most efficient ways to
|
||||
* calculate the maximum contiguos subarray sum for a given array.
|
||||
* calculate the maximum contiguous subarray sum for a given array.
|
||||
* Below is the implementation of kadanes's algorithm along with
|
||||
* some sample test cases.
|
||||
* There might be a special case in this problem if al the elements
|
||||
@ -11,7 +11,7 @@
|
||||
|
||||
export function kadaneAlgo (array) {
|
||||
let cummulativeSum = 0
|
||||
let maxSum = Number.NEGATIVE_INFINITY // maxSum has the least posible value
|
||||
let maxSum = Number.NEGATIVE_INFINITY // maxSum has the least possible value
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
cummulativeSum = cummulativeSum + array[i]
|
||||
if (maxSum < cummulativeSum) {
|
||||
|
@ -19,7 +19,7 @@ function maximumNonAdjacentSum (nums) {
|
||||
return Math.max(maxExcluding, maxIncluding)
|
||||
}
|
||||
|
||||
// Exmaple
|
||||
// Example
|
||||
|
||||
// maximumNonAdjacentSum([1, 2, 3]))
|
||||
// maximumNonAdjacentSum([1, 5, 3, 7, 2, 2, 6]))
|
||||
|
@ -1,7 +1,7 @@
|
||||
function sieveOfEratosthenes (n) {
|
||||
/*
|
||||
* Calculates prime numbers till a number n
|
||||
* :param n: Number upto which to calculate primes
|
||||
* :param n: Number up to which to calculate primes
|
||||
* :return: A boolean list containing only primes
|
||||
*/
|
||||
const primes = new Array(n + 1)
|
||||
|
@ -34,7 +34,7 @@ function pad (str, bits) {
|
||||
* @return {array} - array of original string split into chunks
|
||||
*
|
||||
* @example
|
||||
* chunkify("this is a test", 2); // ["th", "is", " i", "s ", "a ", "te", "st"]
|
||||
* chunkify("this is a test", 2)
|
||||
*/
|
||||
function chunkify (str, size) {
|
||||
const chunks = []
|
||||
|
@ -45,7 +45,7 @@ function pad (str, bits) {
|
||||
* @return {array} - array of original string split into chunks
|
||||
*
|
||||
* @example
|
||||
* chunkify("this is a test", 2); // ["th", "is", " i", "s ", "a ", "te", "st"]
|
||||
* chunkify("this is a test", 2)
|
||||
*/
|
||||
function chunkify (str, size) {
|
||||
const chunks = []
|
||||
@ -76,7 +76,7 @@ function rotateRight (bits, turns) {
|
||||
* @return {string} - processed message
|
||||
*/
|
||||
function preProcess (message) {
|
||||
// covert message to binary representation padded to
|
||||
// convert message to binary representation padded to
|
||||
// 8 bits, and add 1
|
||||
let m = message.split('')
|
||||
.map(e => e.charCodeAt(0))
|
||||
|
@ -1,114 +0,0 @@
|
||||
# Linear algebra library for JavaScript
|
||||
|
||||
This library contains some useful classes and functions for dealing with linear algebra in JavaScript.
|
||||
The library was orginal written in **TypeScript** and then compiles into pure JavaScript.
|
||||
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
- class Vector : This class represents a vector of arbitray size and operations on it.
|
||||
- constructor Vector(N) : creates a zero vector of size N
|
||||
- constructor Vector(N, components) : creates a vector of size N with the given components.
|
||||
- createUnitBasis(pos) : converts this vector in a unit basis vector and returns it.
|
||||
- component(pos) : returns the specified component (indexing at 0)
|
||||
- changeComponent(pos, value) : change the specified component.
|
||||
- toString() : returns a string representation of this vector.
|
||||
- size() : returns the size of the vector. (not the eulidean length!)
|
||||
- eulideanLength() : computes the eulidean length of this vector.
|
||||
- add(other) : vector addition, returns the rersult.
|
||||
- sub(other) : vector subtraction, returns the rersult.
|
||||
- dot(other) : computes the dot-product and returns it.
|
||||
- scalar(s) : scalar (s) multiplication. returns the result.
|
||||
- norm() : normalizes this vector and returns it.
|
||||
- equal(other) : returns true if the vectors are equal, otherwise false.
|
||||
|
||||
- function unitBasisVector(N,pos) : returns a unit basis vector of size N with a One on position 'pos'
|
||||
- function randomVectorInt(N,a,b) : returns a random vector with integer components (between 'a' and 'b') of size N.
|
||||
- function randomVectorFloat(N,a,b) : returns a random vector with floating point components (between 'a' and 'b') of size N.
|
||||
|
||||
- class Matrix : This class represents a matrix of arbitrary size and operations on it.
|
||||
- constructor(rows, cols) : creates a zero matrix of dimension rows x cols.
|
||||
- constructor(rows, cols, components) : creates a matrix with fix numbers of dimension rows x cols.
|
||||
- component(x,y) : returns the specified component.
|
||||
- changeComponent(x,y,value) : changes the specified component with the new value 'value'.
|
||||
- toString() : returns a string representation of this matrix.
|
||||
- dimension() : returns the dimension of this matrix as number arras [rows,cols].
|
||||
- add(other) : returns the result of the matrix addition.
|
||||
- equal(other) : returns true if the matrices are equal, otherwise false.
|
||||
- scalar(c) : returns the result of the matrix-scalar multiplication.
|
||||
---
|
||||
|
||||
## Documentation
|
||||
|
||||
The module is well documented in its source code. Look in the TypeScript file ```la_lib.ts```.
|
||||
|
||||
---
|
||||
|
||||
## Usage
|
||||
|
||||
You will find the library in the **src** directory its called ```la_lib.js```. You simply need to
|
||||
include this library in your project **(you don't install anything)**. After that:
|
||||
|
||||
```js
|
||||
var x = LinearAlgebra.Vector(...);
|
||||
```
|
||||
|
||||
The namespace LinearAlgebra contains useful classes and functions for dealing with linear algebra under JavaScript.
|
||||
|
||||
Some examples:
|
||||
|
||||
```js
|
||||
// ---------------------------- Examples ------------------------------------------
|
||||
|
||||
// creates vectors
|
||||
var x = new LinearAlgebra.Vector(5, [1, 2, 3, 4, 5]);
|
||||
var y = new LinearAlgebra.Vector(5, [1, 2, 3, 4, 5]);
|
||||
|
||||
// prints size of the vector
|
||||
console.log(x.size()); // ==> 5
|
||||
|
||||
// changes the 2-th component with 7
|
||||
//x.changeComponent(2,7);
|
||||
|
||||
// print the 2-th component.
|
||||
console.log(x.component(2)); // ==> 3
|
||||
|
||||
// prints the full vector as string.
|
||||
console.log(x.toString()); // ==> (1,2,3,4,5)
|
||||
|
||||
// vector addition
|
||||
console.log(x.add(y).toString()); // ==> (2,3,6,8,10)
|
||||
|
||||
//console.log(x.createUnitBasis(1).toString());
|
||||
|
||||
// computes the dot-product
|
||||
console.log(x.dot(y)); // ==> 55
|
||||
|
||||
// computes and prints the scalar-product
|
||||
console.log(x.scalar(5).toString()); // ==> (5,10,15,20,25)
|
||||
|
||||
// creates a unit basis vector
|
||||
console.log(LinearAlgebra.unitBasisVector(3, 0).toString()); // ==> (1,0,0)
|
||||
|
||||
// creates random vectors
|
||||
console.log(LinearAlgebra.randomVectorInt(3, 0, 5).toString());
|
||||
console.log(LinearAlgebra.randomVectorFloat(3, 0, 5).toString());
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Tests
|
||||
|
||||
Go in the directory of the project and type in:
|
||||
```npm install```
|
||||
```npm test```
|
||||
The test-suite use the JavaScript test-framework **mocha**.
|
||||
|
||||
---
|
||||
|
||||
## Contributing
|
||||
|
||||
You can contribute to this project. Feel free and pull request some new features or documention.
|
||||
**TODO:** Global functions for special matrices.
|
||||
**TODO:** Documention of the classes and functions.
|
171
Linear-Algebra/package-lock.json
generated
171
Linear-Algebra/package-lock.json
generated
@ -1,171 +0,0 @@
|
||||
{
|
||||
"name": "linear-algebra-javascript",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"balanced-match": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
|
||||
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
|
||||
},
|
||||
"brace-expansion": {
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
|
||||
"requires": {
|
||||
"balanced-match": "1.0.0",
|
||||
"concat-map": "0.0.1"
|
||||
}
|
||||
},
|
||||
"browser-stdout": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz",
|
||||
"integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw=="
|
||||
},
|
||||
"commander": {
|
||||
"version": "2.11.0",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz",
|
||||
"integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ=="
|
||||
},
|
||||
"concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
|
||||
},
|
||||
"debug": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
|
||||
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
}
|
||||
},
|
||||
"diff": {
|
||||
"version": "3.3.1",
|
||||
"resolved": "https://registry.npmjs.org/diff/-/diff-3.3.1.tgz",
|
||||
"integrity": "sha512-MKPHZDMB0o6yHyDryUOScqZibp914ksXwAMYMTHj6KO8UeKsRYNJD3oNCKjTqZon+V488P7N/HzXF8t7ZR95ww=="
|
||||
},
|
||||
"escape-string-regexp": {
|
||||
"version": "1.0.5",
|
||||
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
|
||||
"integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ="
|
||||
},
|
||||
"fs.realpath": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
|
||||
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
|
||||
},
|
||||
"glob": {
|
||||
"version": "7.1.2",
|
||||
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
|
||||
"integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
|
||||
"requires": {
|
||||
"fs.realpath": "1.0.0",
|
||||
"inflight": "1.0.6",
|
||||
"inherits": "2.0.3",
|
||||
"minimatch": "3.0.4",
|
||||
"once": "1.4.0",
|
||||
"path-is-absolute": "1.0.1"
|
||||
}
|
||||
},
|
||||
"growl": {
|
||||
"version": "1.10.3",
|
||||
"resolved": "https://registry.npmjs.org/growl/-/growl-1.10.3.tgz",
|
||||
"integrity": "sha512-hKlsbA5Vu3xsh1Cg3J7jSmX/WaW6A5oBeqzM88oNbCRQFz+zUaXm6yxS4RVytp1scBoJzSYl4YAEOQIt6O8V1Q=="
|
||||
},
|
||||
"has-flag": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz",
|
||||
"integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE="
|
||||
},
|
||||
"he": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz",
|
||||
"integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0="
|
||||
},
|
||||
"inflight": {
|
||||
"version": "1.0.6",
|
||||
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
|
||||
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
|
||||
"requires": {
|
||||
"once": "1.4.0",
|
||||
"wrappy": "1.0.2"
|
||||
}
|
||||
},
|
||||
"inherits": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
|
||||
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
|
||||
},
|
||||
"minimatch": {
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
|
||||
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
|
||||
"requires": {
|
||||
"brace-expansion": "1.1.11"
|
||||
}
|
||||
},
|
||||
"minimist": {
|
||||
"version": "0.0.8",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
|
||||
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
|
||||
},
|
||||
"mkdirp": {
|
||||
"version": "0.5.1",
|
||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
|
||||
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
|
||||
"requires": {
|
||||
"minimist": "0.0.8"
|
||||
}
|
||||
},
|
||||
"mocha": {
|
||||
"version": "5.0.2",
|
||||
"resolved": "https://registry.npmjs.org/mocha/-/mocha-5.0.2.tgz",
|
||||
"integrity": "sha512-nmlYKMRpJZLxgzk0bRhcvlpjSisbi0x1JiRl7kctadOMPmecUie7WwCZmcyth+PzX5txKbpcMIvDZCAlx9ISxg==",
|
||||
"requires": {
|
||||
"browser-stdout": "1.3.1",
|
||||
"commander": "2.11.0",
|
||||
"debug": "3.1.0",
|
||||
"diff": "3.3.1",
|
||||
"escape-string-regexp": "1.0.5",
|
||||
"glob": "7.1.2",
|
||||
"growl": "1.10.3",
|
||||
"he": "1.1.1",
|
||||
"mkdirp": "0.5.1",
|
||||
"supports-color": "4.4.0"
|
||||
}
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
},
|
||||
"once": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
|
||||
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
|
||||
"requires": {
|
||||
"wrappy": "1.0.2"
|
||||
}
|
||||
},
|
||||
"path-is-absolute": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
|
||||
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
|
||||
},
|
||||
"supports-color": {
|
||||
"version": "4.4.0",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.4.0.tgz",
|
||||
"integrity": "sha512-rKC3+DyXWgK0ZLKwmRsrkyHVZAjNkfzeehuFWdGGcqGDTZFH73+RH6S/RDAAxl9GusSjZSUWYLmT9N5pzXFOXQ==",
|
||||
"requires": {
|
||||
"has-flag": "2.0.0"
|
||||
}
|
||||
},
|
||||
"wrappy": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
|
||||
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
|
||||
}
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
{
|
||||
"name": "linear-algebra-javascript",
|
||||
"version": "1.0.0",
|
||||
"description": "simple linear algebra library for JavaScript",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha"
|
||||
},
|
||||
"author": "Christian Bender",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"mocha": "^5.0.2"
|
||||
}
|
||||
}
|
@ -1,313 +0,0 @@
|
||||
/*
|
||||
author: Christian Bender
|
||||
license: MIT-license
|
||||
|
||||
The namespace LinearAlgebra contains useful classes and functions for dealing with
|
||||
linear algebra under JavaScript.
|
||||
*/
|
||||
let LinearAlgebra;
|
||||
(function (LinearAlgebra) {
|
||||
/*
|
||||
class: Vector
|
||||
This class represents a vector of arbitrary size and operations on it.
|
||||
*/
|
||||
const Vector = /** @class */ (function () {
|
||||
// constructor
|
||||
function Vector (N, comps) {
|
||||
if (comps === undefined) {
|
||||
comps = []
|
||||
}
|
||||
this.components = new Array(N)
|
||||
if (comps.length === 0) {
|
||||
for (let i = 0; i < N; i++) {
|
||||
this.components[i] = 0.0
|
||||
}
|
||||
} else {
|
||||
if (N === comps.length) {
|
||||
this.components = comps
|
||||
} else {
|
||||
throw new Error('Vector: invalid size!')
|
||||
}
|
||||
}
|
||||
} // end of constructor
|
||||
// returns the size of this vector.
|
||||
// not the eulidean length!
|
||||
Vector.prototype.size = function () {
|
||||
return this.components.length
|
||||
}
|
||||
// computes the eulidean length.
|
||||
Vector.prototype.eulideanLength = function () {
|
||||
let sum = 0
|
||||
for (let i = 0; i < this.components.length; i++) {
|
||||
sum += this.components[i] * this.components[i]
|
||||
}
|
||||
return Math.sqrt(sum)
|
||||
}
|
||||
// getter for the components of the vector.
|
||||
// returns a specified component (index)
|
||||
Vector.prototype.component = function (index) {
|
||||
return this.components[index]
|
||||
}
|
||||
// setter for a specified component of this vector.
|
||||
Vector.prototype.changeComponent = function (index, value) {
|
||||
if (index >= 0 && index < this.components.length) {
|
||||
this.components[index] = value
|
||||
} else {
|
||||
throw new Error('changeComponent: index out of bounds!')
|
||||
}
|
||||
}
|
||||
// vector addition
|
||||
Vector.prototype.add = function (other) {
|
||||
if (this.size() === other.size()) {
|
||||
const SIZE = this.size()
|
||||
const ans = new Vector(SIZE)
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
ans.changeComponent(i, (this.components[i] + other.component(i)))
|
||||
}
|
||||
return ans
|
||||
} else {
|
||||
throw new Error('add: vector must have same size!')
|
||||
}
|
||||
}
|
||||
// vector subtraction
|
||||
Vector.prototype.sub = function (other) {
|
||||
if (this.size() === other.size()) {
|
||||
const SIZE = this.size()
|
||||
const ans = new Vector(SIZE)
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
ans.changeComponent(i, (this.components[i] - other.component(i)))
|
||||
}
|
||||
return ans
|
||||
} else {
|
||||
throw new Error('add: vector must have same size!')
|
||||
}
|
||||
}
|
||||
// dot-product
|
||||
Vector.prototype.dot = function (other) {
|
||||
let sum = 0
|
||||
if (other.size() === this.size()) {
|
||||
const SIZE = other.size()
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
sum += this.components[i] * other.component(i)
|
||||
}
|
||||
return sum
|
||||
} else {
|
||||
throw new Error('dot: vectors must have same size!')
|
||||
}
|
||||
}
|
||||
// scalar multiplication
|
||||
Vector.prototype.scalar = function (s) {
|
||||
const SIZE = this.size()
|
||||
const ans = new Vector(SIZE)
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
ans.changeComponent(i, (this.components[i] * s))
|
||||
}
|
||||
return ans
|
||||
}
|
||||
// returns a string representation of this vector.
|
||||
Vector.prototype.toString = function () {
|
||||
let ans = '('
|
||||
const SIZE = this.components.length
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
if (i < SIZE - 1) {
|
||||
ans += this.components[i] + ','
|
||||
} else {
|
||||
ans += this.components[i] + ')'
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
// converts this vector in a unit basis vector and returns it.
|
||||
// the One is on position 'pos'
|
||||
Vector.prototype.createUnitBasis = function (pos) {
|
||||
if (pos >= 0 && pos < this.components.length) {
|
||||
for (let i = 0; i < this.components.length; i++) {
|
||||
if (i === pos) {
|
||||
this.components[i] = 1.0
|
||||
} else {
|
||||
this.components[i] = 0.0
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error('createUnitBasis: index out of bounds')
|
||||
}
|
||||
return this
|
||||
}
|
||||
// normalizes this vector and returns it.
|
||||
Vector.prototype.norm = function () {
|
||||
const SIZE = this.size()
|
||||
const quotient = 1.0 / this.eulideanLength()
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
this.components[i] = this.components[i] * quotient
|
||||
}
|
||||
return this
|
||||
}
|
||||
// returns true if the vectors are equal otherwise false.
|
||||
Vector.prototype.equal = function (other) {
|
||||
let ans = true
|
||||
const SIZE = this.size()
|
||||
const EPSILON = 0.001
|
||||
if (SIZE === other.size()) {
|
||||
for (let i = 0; i < SIZE; i++) {
|
||||
if (Math.abs(this.components[i] - other.component(i)) > EPSILON) {
|
||||
ans = false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ans = false
|
||||
}
|
||||
return ans
|
||||
}
|
||||
return Vector
|
||||
}()) // end of class Vector
|
||||
LinearAlgebra.Vector = Vector
|
||||
// -------------- global functions ---------------------------------
|
||||
// returns a unit basis vector of size N with a One on position 'pos'
|
||||
function unitBasisVector (N, pos) {
|
||||
const ans = new Vector(N)
|
||||
for (let i = 0; i < N; i++) {
|
||||
if (i === pos) {
|
||||
ans.changeComponent(i, 1.0)
|
||||
} else {
|
||||
ans.changeComponent(i, 0)
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
LinearAlgebra.unitBasisVector = unitBasisVector
|
||||
// returns a random vector with integer components (between 'a' and 'b') of size N.
|
||||
function randomVectorInt (N, a, b) {
|
||||
const ans = new Vector(N)
|
||||
for (let i = 0; i < N; i++) {
|
||||
ans.changeComponent(i, (Math.floor((Math.random() * b) + a)))
|
||||
}
|
||||
return ans
|
||||
}
|
||||
LinearAlgebra.randomVectorInt = randomVectorInt
|
||||
// returns a random vector with floating point components (between 'a' and 'b') of size N.
|
||||
function randomVectorFloat (N, a, b) {
|
||||
const ans = new Vector(N)
|
||||
for (let i = 0; i < N; i++) {
|
||||
ans.changeComponent(i, ((Math.random() * b) + a))
|
||||
}
|
||||
return ans
|
||||
}
|
||||
LinearAlgebra.randomVectorFloat = randomVectorFloat
|
||||
// ------------------ end of global functions -----------------------------
|
||||
/*
|
||||
class: Matrix
|
||||
This class represents a matrix of arbitrary size and operations on it.
|
||||
*/
|
||||
const Matrix = /** @class */ (function () {
|
||||
// constructor for zero-matrix or fix number matrix.
|
||||
function Matrix (row, col, comps) {
|
||||
if (comps === undefined) {
|
||||
comps = []
|
||||
}
|
||||
if (comps.length === 0) {
|
||||
this.matrix = []
|
||||
let rowVector = []
|
||||
for (let i = 0; i < row; i++) {
|
||||
for (let j = 0; j < col; j++) {
|
||||
rowVector[j] = 0
|
||||
}
|
||||
this.matrix[i] = rowVector
|
||||
rowVector = []
|
||||
}
|
||||
} else {
|
||||
this.matrix = comps
|
||||
}
|
||||
this.rows = row
|
||||
this.cols = col
|
||||
}
|
||||
// returns the specified component.
|
||||
Matrix.prototype.component = function (x, y) {
|
||||
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols) {
|
||||
return this.matrix[x][y]
|
||||
} else {
|
||||
throw new Error('component: index out of bounds')
|
||||
}
|
||||
}
|
||||
// changes the specified component with value 'value'.
|
||||
Matrix.prototype.changeComponent = function (x, y, value) {
|
||||
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols) {
|
||||
this.matrix[x][y] = value
|
||||
} else {
|
||||
throw new Error('changeComponent: index out of bounds')
|
||||
}
|
||||
}
|
||||
// returns a string representation of this matrix.
|
||||
Matrix.prototype.toString = function () {
|
||||
let ans = ''
|
||||
for (let i = 0; i < this.rows; i++) {
|
||||
ans += '|'
|
||||
for (let j = 0; j < this.cols; j++) {
|
||||
if (j < this.cols - 1) {
|
||||
ans += this.matrix[i][j] + ','
|
||||
} else {
|
||||
if (i < this.rows - 1) {
|
||||
ans += this.matrix[i][j] + '|\n'
|
||||
} else {
|
||||
ans += this.matrix[i][j] + '|'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
// returns the dimension rows x cols as number array
|
||||
Matrix.prototype.dimension = function () {
|
||||
const ans = []
|
||||
ans[0] = this.rows
|
||||
ans[1] = this.cols
|
||||
return ans
|
||||
}
|
||||
// matrix addition. returns the result.
|
||||
Matrix.prototype.add = function (other) {
|
||||
if (this.rows === other.dimension()[0] &&
|
||||
this.cols === other.dimension()[1]) {
|
||||
const ans = new Matrix(this.rows, this.cols)
|
||||
for (let i = 0; i < this.rows; i++) {
|
||||
for (let j = 0; j < this.cols; j++) {
|
||||
ans.changeComponent(i, j, (this.matrix[i][j] + other.component(i, j)))
|
||||
}
|
||||
}
|
||||
return ans
|
||||
} else {
|
||||
throw new Error('add: matrices must have same dimension!')
|
||||
}
|
||||
}
|
||||
// returns true if the matrices are equal, otherwise false.
|
||||
Matrix.prototype.equal = function (other) {
|
||||
let ans = true
|
||||
const EPSILON = 0.001
|
||||
if (this.rows === other.dimension()[0] &&
|
||||
this.cols === other.dimension()[1]) {
|
||||
for (let i = 0; i < this.rows; i++) {
|
||||
for (let j = 0; j < this.cols; j++) {
|
||||
if (Math.abs(this.matrix[i][j] - other.component(i, j)) > EPSILON) {
|
||||
ans = false
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ans = false
|
||||
}
|
||||
return ans
|
||||
}
|
||||
// matrix-scalar multiplication
|
||||
Matrix.prototype.scalar = function (c) {
|
||||
const ans = new Matrix(this.rows, this.cols)
|
||||
for (let i = 0; i < this.rows; i++) {
|
||||
for (let j = 0; j < this.cols; j++) {
|
||||
ans.changeComponent(i, j, (this.matrix[i][j] * c))
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
||||
return Matrix
|
||||
}()) // end of class Matrix
|
||||
LinearAlgebra.Matrix = Matrix
|
||||
})(LinearAlgebra || (LinearAlgebra = {})) // end of namespace LinearAlgebra
|
||||
|
||||
export { LinearAlgebra }
|
@ -1,213 +0,0 @@
|
||||
/*
|
||||
author: Christian Bender
|
||||
license: MIT-license
|
||||
|
||||
This file contains the test-suite for the linear algebra library.
|
||||
The tests use javascript test-framework mocha
|
||||
*/
|
||||
|
||||
/* eslint-disable */
|
||||
|
||||
import { LinearAlgebra } from '../src/la_lib'
|
||||
import * as assert from 'assert'
|
||||
|
||||
// file is included here
|
||||
// Tests goes here
|
||||
|
||||
// creating some vectors
|
||||
describe('Create Vectors', function () {
|
||||
describe('#toString()', function () {
|
||||
it('should return a string representation', function () {
|
||||
assert.strictEqual((new LinearAlgebra.Vector(3, [1, 2, 3])).toString(), '(1,2,3)')
|
||||
})
|
||||
})
|
||||
describe('#unitBasisVector()', function () {
|
||||
it('should return a unit basis vector', function () {
|
||||
assert.strictEqual(LinearAlgebra.unitBasisVector(3, 1).toString(), '(0,1,0)')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// operations on it.
|
||||
describe('Vector operations', function () {
|
||||
describe('#add()', function () {
|
||||
it('should return vector (2,4,6)', function () {
|
||||
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
|
||||
var y = new LinearAlgebra.Vector(3, [1, 2, 3])
|
||||
assert.strictEqual((x.add(y)).toString(), '(2,4,6)')
|
||||
})
|
||||
})
|
||||
describe('#sub()', function () {
|
||||
it('should return vector (0,0,0)', function () {
|
||||
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
|
||||
var y = new LinearAlgebra.Vector(3, [1, 2, 3])
|
||||
assert.strictEqual((x.sub(y)).toString(), '(0,0,0)')
|
||||
})
|
||||
})
|
||||
describe('#dot()', function () {
|
||||
it('should return the dot-product', function () {
|
||||
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
|
||||
var y = new LinearAlgebra.Vector(3, [5, 6, 7])
|
||||
assert.strictEqual(x.dot(y), 38)
|
||||
})
|
||||
})
|
||||
describe('#scalar()', function () {
|
||||
it('should return the scalar product', function () {
|
||||
var x = new LinearAlgebra.Vector(3, [1, 2, 3])
|
||||
assert.strictEqual(x.scalar(2).toString(), '(2,4,6)')
|
||||
})
|
||||
})
|
||||
describe('#norm()', function () {
|
||||
it('should return the normalizes vector', function () {
|
||||
var x = new LinearAlgebra.Vector(4, [9, 0, 3, 1])
|
||||
var y = x.norm()
|
||||
assert.ok(Math.abs(y.component(0) - (9.0 / Math.sqrt(91))) <= 0.01)
|
||||
})
|
||||
})
|
||||
describe('#eulideanLength()', function () {
|
||||
it('should return the eulidean length of the vector', function () {
|
||||
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
|
||||
assert.ok(Math.abs(x.eulideanLength() - 3) <= 0.001)
|
||||
})
|
||||
})
|
||||
describe('#size()', function () {
|
||||
it('should return the size (not eulidean length!) of the vector', function () {
|
||||
var x = LinearAlgebra.randomVectorInt(10, 1, 5)
|
||||
assert.strictEqual(x.size(), 10)
|
||||
})
|
||||
})
|
||||
describe('#equal()', function () {
|
||||
it('should compares two vectors', function () {
|
||||
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
|
||||
var y = new LinearAlgebra.Vector(3, [1, 2, 3])
|
||||
assert.ok(x.equal(x))
|
||||
assert.ok(!x.equal(y))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('Methods on vectors', function () {
|
||||
describe('#component()', function () {
|
||||
it('should return the specified component', function () {
|
||||
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
|
||||
assert.strictEqual(x.component(1), 2)
|
||||
})
|
||||
})
|
||||
describe('#changeComponent()', function () {
|
||||
it('should return the changed vector', function () {
|
||||
var x = new LinearAlgebra.Vector(3, [1, 2, 2])
|
||||
x.changeComponent(1, 5)
|
||||
assert.strictEqual(x.toString(), '(1,5,2)')
|
||||
})
|
||||
})
|
||||
describe('#toString()', function () {
|
||||
it('should return a string representation of the vector', function () {
|
||||
var x = new LinearAlgebra.Vector(4, [9, 0, 3, 1])
|
||||
assert.strictEqual(x.toString(), '(9,0,3,1)')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('class Matrix', function () {
|
||||
describe('#component()', function () {
|
||||
it('should return the specified component', function () {
|
||||
var A = new LinearAlgebra.Matrix(2, 2)
|
||||
assert.strictEqual(A.component(0, 1), 0)
|
||||
var B = new LinearAlgebra.Matrix(2, 2, [
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
])
|
||||
assert.strictEqual(B.component(1, 0), 3)
|
||||
})
|
||||
})
|
||||
describe('#toString()', function () {
|
||||
it('should return a string representation of the matrix', function () {
|
||||
var A = new LinearAlgebra.Matrix(2, 2, [
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
])
|
||||
assert.strictEqual(A.toString(), '|1,2|\n|3,4|')
|
||||
})
|
||||
})
|
||||
describe('#dimension()', function () {
|
||||
it('should return the dimension of the matrix as number array', function () {
|
||||
var A = new LinearAlgebra.Matrix(3, 2, [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6]
|
||||
])
|
||||
assert.strictEqual(A.dimension()[0], 3)
|
||||
assert.strictEqual(A.dimension()[1], 2)
|
||||
})
|
||||
})
|
||||
describe('#changeComponent()', function () {
|
||||
it('should change the specified component of the matrix', function () {
|
||||
var A = new LinearAlgebra.Matrix(3, 2, [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6]
|
||||
])
|
||||
A.changeComponent(1, 0, 5)
|
||||
assert.strictEqual(A.component(1, 0), 5)
|
||||
})
|
||||
})
|
||||
describe('#equal()', function () {
|
||||
it('should compares the matrices', function () {
|
||||
var A = new LinearAlgebra.Matrix(3, 2, [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6]
|
||||
])
|
||||
var B = new LinearAlgebra.Matrix(3, 2, [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6]
|
||||
])
|
||||
var C = new LinearAlgebra.Matrix(2, 2, [
|
||||
[1, 2],
|
||||
[3, 4]
|
||||
])
|
||||
var D = new LinearAlgebra.Matrix(2, 2, [
|
||||
[1, 2],
|
||||
[5, 4]
|
||||
])
|
||||
assert.ok(A.equal(B))
|
||||
assert.ok(!A.equal(C))
|
||||
assert.ok(!C.equal(D))
|
||||
})
|
||||
})
|
||||
describe('#add()', function () {
|
||||
it('should return the result of the matrix addition', function () {
|
||||
var A = new LinearAlgebra.Matrix(3, 2, [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6]
|
||||
])
|
||||
var B = new LinearAlgebra.Matrix(3, 2, [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6]
|
||||
])
|
||||
var C = A.add(B)
|
||||
assert.strictEqual(C.component(1, 0), 6)
|
||||
assert.strictEqual(C.component(1, 1), 8)
|
||||
assert.strictEqual(C.component(0, 0), 2)
|
||||
})
|
||||
})
|
||||
describe('#scalar()', function () {
|
||||
it('should return the result of the matrix-scalar multiplication', function () {
|
||||
var A = new LinearAlgebra.Matrix(3, 2, [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6]
|
||||
])
|
||||
var B = A.scalar(2)
|
||||
var C = new LinearAlgebra.Matrix(3, 2, [
|
||||
[2, 4],
|
||||
[6, 8],
|
||||
[10, 12]
|
||||
])
|
||||
assert.ok(B.equal(C))
|
||||
})
|
||||
})
|
||||
})
|
@ -12,13 +12,13 @@
|
||||
// = exponent(x*x, floor(n/2)) ; if n is odd
|
||||
// = x*exponent(x*x, floor(n/2)) ; if n is even
|
||||
const exponent = (x, n) => {
|
||||
let ans = 1
|
||||
let answer = 1
|
||||
while (n > 0) {
|
||||
if (n % 2 !== 0) ans *= x
|
||||
if (n % 2 !== 0) answer *= x
|
||||
n = Math.floor(n / 2)
|
||||
if (n > 0) x *= x
|
||||
}
|
||||
return ans
|
||||
return answer
|
||||
}
|
||||
|
||||
export { exponent }
|
||||
|
@ -5,6 +5,6 @@
|
||||
*/
|
||||
|
||||
export const decimalIsolate = (number) => {
|
||||
const ans = parseFloat((number + '').replace(/^[-\d]+./, '.'))
|
||||
return isNaN(ans) === true ? 0 : ans
|
||||
const answer = parseFloat((number + '').replace(/^[-\d]+./, '.'))
|
||||
return isNaN(answer) === true ? 0 : answer
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
Problem Statment and Explanation :
|
||||
Problem Statement and Explanation :
|
||||
Triangular => https://en.wikipedia.org/wiki/Triangular_number
|
||||
Tetrahedral => https://en.wikipedia.org/wiki/Tetrahedral_number
|
||||
Pentatope => https://en.wikipedia.org/wiki/Pentatope_number
|
||||
|
@ -22,27 +22,27 @@ const Identity = (n) => {
|
||||
return res
|
||||
}
|
||||
|
||||
const MatMult = (matA, matB) => {
|
||||
// Input: matA: 2D Array of Numbers of size n x n
|
||||
// matB: 2D Array of Numbers of size n x n
|
||||
// Output: matA x matB: 2D Array of Numbers of size n x n
|
||||
const MatMult = (matrixA, matrixB) => {
|
||||
// Input: matrixA: 2D Array of Numbers of size n x n
|
||||
// matrixB: 2D Array of Numbers of size n x n
|
||||
// Output: matrixA x matrixB: 2D Array of Numbers of size n x n
|
||||
// Complexity: O(n^3)
|
||||
const n = matA.length
|
||||
const matC = []
|
||||
const n = matrixA.length
|
||||
const matrixC = []
|
||||
for (let i = 0; i < n; i++) {
|
||||
matC[i] = []
|
||||
matrixC[i] = []
|
||||
for (let j = 0; j < n; j++) {
|
||||
matC[i][j] = 0
|
||||
matrixC[i][j] = 0
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
for (let k = 0; k < n; k++) {
|
||||
matC[i][j] += matA[i][k] * matB[k][j]
|
||||
matrixC[i][j] += matrixA[i][k] * matrixB[k][j]
|
||||
}
|
||||
}
|
||||
}
|
||||
return matC
|
||||
return matrixC
|
||||
}
|
||||
|
||||
export const MatrixExponentiationRecursive = (mat, m) => {
|
||||
|
@ -17,7 +17,7 @@ const matrixCheck = (matrix) => {
|
||||
}
|
||||
}
|
||||
|
||||
// tests to see if the matrices have a like side, i.e. the row length on the first matrix matches the column length on the second matrix, or vise versa.
|
||||
// tests to see if the matrices have a like side, i.e. the row length on the first matrix matches the column length on the second matrix, or vice versa.
|
||||
const twoMatricesCheck = (first, second) => {
|
||||
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [first.length, second.length, matrixCheck(first), matrixCheck(second)]
|
||||
if (firstRowLength !== secondColLength || secondRowLength !== firstColLength) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
const sieveOfEratosthenes = (n) => {
|
||||
/*
|
||||
* Calculates prime numbers till a number n
|
||||
* :param n: Number upto which to calculate primes
|
||||
* :param n: Number up to which to calculate primes
|
||||
* :return: A boolean list containing only primes
|
||||
*/
|
||||
const primes = new Array(n + 1)
|
||||
|
@ -59,7 +59,7 @@ function integralEvaluation (N, a, b, func) {
|
||||
|
||||
result *= temp
|
||||
|
||||
if (Number.isNaN(result)) { throw Error('Result is NaN. The input interval doesnt belong to the functions domain') }
|
||||
if (Number.isNaN(result)) { throw Error("Result is NaN. The input interval doesn't belong to the functions domain") }
|
||||
|
||||
return result
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
import { calculateSumOfPrimeNumbers } from '../Problem010'
|
||||
|
||||
describe('checkAnagram', () => {
|
||||
it('Return the sum of prime numbers upto but less than 14', () => {
|
||||
it('Return the sum of prime numbers up to but less than 14', () => {
|
||||
const SUT = calculateSumOfPrimeNumbers(14)
|
||||
expect(SUT).toBe(41)
|
||||
})
|
||||
it('Return the sum of prime numbers upto but less than 10', () => {
|
||||
it('Return the sum of prime numbers up to but less than 10', () => {
|
||||
const SUT = calculateSumOfPrimeNumbers(10)
|
||||
expect(SUT).toBe(17)
|
||||
})
|
||||
it('Return the sum of prime numbers upto but less than 100', () => {
|
||||
it('Return the sum of prime numbers up to but less than 100', () => {
|
||||
const SUT = calculateSumOfPrimeNumbers(100)
|
||||
expect(SUT).toBe(1060)
|
||||
})
|
||||
|
@ -7,7 +7,7 @@
|
||||
/*****************************************************************************
|
||||
* What is a palindrome? https://en.wikipedia.org/wiki/Palindrome
|
||||
*
|
||||
* The function alphaNumericPlaindrome() recieves a sting with varying formats
|
||||
* The function alphaNumericPlaindrome() receives a string with varying formats
|
||||
* like "racecar", "RaceCar", and "race CAR"
|
||||
* The string can also have special characters
|
||||
* like "2A3*3a2", "2A3 3a2", and "2_A3*3#A2"
|
||||
@ -43,7 +43,7 @@ const alphaNumericPlaindrome = (str) => {
|
||||
// iterate through the arr and check the condition of palindrome
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (arr[i] !== arrRev[arr.length - 1 - i]) {
|
||||
// if the string is not palindrome then we change palin varaible to 1
|
||||
// if the string is not palindrome then we change palin variable to 1
|
||||
palin = 1
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* The Sørensen–Dice coefficient is a statistic used to gauge the similarity of two samples.
|
||||
* Applied to strings, it can give you a value between 0 and 1 (included) which tells you how similar they are.
|
||||
* Dice coefficient is calculated by comparing the bigrams of both stings,
|
||||
* Dice coefficient is calculated by comparing the bigrams of both strings,
|
||||
* a bigram is a substring of the string of length 2.
|
||||
* read more: https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
|
||||
*/
|
||||
|
@ -59,7 +59,7 @@ describe('Test the checkVowels function', () => {
|
||||
expect(countVowels).toBe(0)
|
||||
})
|
||||
|
||||
it('should count multiple occurances of the same vowel in the input', () => {
|
||||
it('should count multiple occurrences of the same vowel in the input', () => {
|
||||
const value = 'aaaaa'
|
||||
const countVowels = checkVowels(value)
|
||||
expect(countVowels).toBe(5)
|
||||
|
@ -6,7 +6,7 @@ describe('diceCoefficient', () => {
|
||||
expect(diceCoefficient('abc', 'abc')).toBe(1)
|
||||
expect(diceCoefficient('', '')).toBe(1)
|
||||
|
||||
// string length needs to be atleast 2 (unless equal)
|
||||
// string length needs to be at least 2 (unless equal)
|
||||
expect(diceCoefficient('a', '')).toBe(0)
|
||||
expect(diceCoefficient('', 'a')).toBe(0)
|
||||
|
||||
|
@ -5,8 +5,8 @@ describe('Testing the maxWord function', () => {
|
||||
expect(() => maxWord(10)).toThrow()
|
||||
})
|
||||
it('get the max word', () => {
|
||||
const string = 'ba ba ba ba banana'
|
||||
const string = 'be be be be a a banana'
|
||||
const mostOccurringWord = maxWord(string)
|
||||
expect(mostOccurringWord).toBe('ba')
|
||||
expect(mostOccurringWord).toBe('be')
|
||||
})
|
||||
})
|
||||
|
@ -11,7 +11,7 @@ describe('Breadth First Tree Traversal', () => {
|
||||
root.right.right = new Node(9)
|
||||
binaryTree.root = root
|
||||
|
||||
// Vizualization :
|
||||
// Visualization :
|
||||
//
|
||||
// 7
|
||||
// / \
|
||||
|
Reference in New Issue
Block a user