Skip to content

Commit 21c34de

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#43 from christianbender/added_linear-algebra
Added a linear algebra library
2 parents aa2e942 + d937885 commit 21c34de

File tree

6 files changed

+1139
-0
lines changed

6 files changed

+1139
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Linear algebra library for JavaScript
2+
3+
This library contains some useful classes and functions for dealing with linear algebra in JavaScript.
4+
The library was orginal written in **TypeScript** and then compiles into pure JavaScript.
5+
6+
---
7+
8+
## Overview
9+
10+
- class Vector : This class represents a vector of arbitray size and operations on it.
11+
- constructor Vector(N) : creates a zero vector of size N
12+
- constructor Vector(N, components) : creates a vector of size N with the given components.
13+
- createUnitBasis(pos) : converts this vector in a unit basis vector and returns it.
14+
- component(pos) : returns the specified component (indexing at 0)
15+
- changeComponent(pos, value) : change the specified component.
16+
- toString() : returns a string representation of this vector.
17+
- size() : returns the size of the vector. (not the eulidean length!)
18+
- eulideanLength() : computes the eulidean length of this vector.
19+
- add(other) : vector addition, returns the rersult.
20+
- sub(other) : vector subtraction, returns the rersult.
21+
- dot(other) : computes the dot-product and returns it.
22+
- scalar(s) : scalar (s) multiplication. returns the result.
23+
- norm() : normalizes this vector and returns it.
24+
- equal(other) : returns true if the vectors are equal, otherwise false.
25+
26+
- function unitBasisVector(N,pos) : returns a unit basis vector of size N with a One on position 'pos'
27+
- function randomVectorInt(N,a,b) : returns a random vector with integer components (between 'a' and 'b') of size N.
28+
- function randomVectorFloat(N,a,b) : returns a random vector with floating point components (between 'a' and 'b') of size N.
29+
30+
- class Matrix : This class represents a matrix of arbitrary size and operations on it.
31+
- constructor(rows, cols) : creates a zero matrix of dimension rows x cols.
32+
- constructor(rows, cols, components) : creates a matrix with fix numbers of dimension rows x cols.
33+
- component(x,y) : returns the specified component.
34+
- changeComponent(x,y,value) : changes the specified component with the new value 'value'.
35+
- toString() : returns a string representation of this matrix.
36+
- dimension() : returns the dimension of this matrix as number arras [rows,cols].
37+
- add(other) : returns the result of the matrix addition.
38+
- equal(other) : returns true if the matrices are equal, otherwise false.
39+
- scalar(c) : returns the result of the matrix-scalar multiplication.
40+
---
41+
42+
## Documentation
43+
44+
The module is well documented in its source code. Look in the TypeScript file ```la_lib.ts```.
45+
46+
---
47+
48+
## Usage
49+
50+
You will find the library in the **src** directory its called ```la_lib.js```. You simply need to
51+
include this library in your project **(you don't install anything)**. After that:
52+
53+
```js
54+
var x = LinearAlgebra.Vector(...);
55+
```
56+
57+
The namespace LinearAlgebra contains useful classes and functions for dealing with linear algebra under JavaScript.
58+
59+
Some examples:
60+
61+
```js
62+
// ---------------------------- Examples ------------------------------------------
63+
64+
// creates vectors
65+
var x = new LinearAlgebra.Vector(5, [1, 2, 3, 4, 5]);
66+
var y = new LinearAlgebra.Vector(5, [1, 2, 3, 4, 5]);
67+
68+
// prints size of the vector
69+
console.log(x.size()); // ==> 5
70+
71+
// changes the 2-th component with 7
72+
//x.changeComponent(2,7);
73+
74+
// print the 2-th component.
75+
console.log(x.component(2)); // ==> 3
76+
77+
// prints the full vector as string.
78+
console.log(x.toString()); // ==> (1,2,3,4,5)
79+
80+
// vector addition
81+
console.log(x.add(y).toString()); // ==> (2,3,6,8,10)
82+
83+
//console.log(x.createUnitBasis(1).toString());
84+
85+
// computes the dot-product
86+
console.log(x.dot(y)); // ==> 55
87+
88+
// computes and prints the scalar-product
89+
console.log(x.scalar(5).toString()); // ==> (5,10,15,20,25)
90+
91+
// creates a unit basis vector
92+
console.log(LinearAlgebra.unitBasisVector(3, 0).toString()); // ==> (1,0,0)
93+
94+
// creates random vectors
95+
console.log(LinearAlgebra.randomVectorInt(3, 0, 5).toString());
96+
console.log(LinearAlgebra.randomVectorFloat(3, 0, 5).toString());
97+
```
98+
99+
---
100+
101+
## Tests
102+
103+
Go in the directory of the project and type in:
104+
```npm install```
105+
```npm test```
106+
The test-suite use the JavaScript test-framework **mocha**.
107+
108+
---
109+
110+
## Contributing
111+
112+
You can contribute to this project. Feel free and pull request some new features or documention.
113+
**TODO:** Global functions for special matrices.
114+
**TODO:** Documention of the classes and functions.

linear-algebra-javascript/package-lock.json

Lines changed: 171 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "linear-algebra-javascript",
3+
"version": "1.0.0",
4+
"description": "simple linear algebra library for JavaScript",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "mocha"
11+
},
12+
"author": "Christian Bender",
13+
"license": "MIT",
14+
"dependencies": {
15+
"mocha": "^5.0.2"
16+
}
17+
}

0 commit comments

Comments
 (0)