Python Extension Packages in Javascript and NodeJS
OTHER License
(Python Extension Packages in Javascript)
Python Extension Packages in Javascript is open-source implementation of some common libraries used in the scientific python programming. The main goal of this project is to improve migration of python language to javascript.
Copyright 2016 Alvaro Fernandez
License: MIT/X11
Just include the following libraries in your html.
<script type="text/javascript" src="ss.js"></script>
<script type="text/javascript" src="Numpy.js"></script>
<script type="text/javascript" src="PolySolve.js"></script>
<script type="text/javascript" src="Scipy.js"></script>
importing a library
Python
>>> import numpy as np
> np = numpy;
Python
>>> import numpy
>>> numpy.array([[1,2],[3,4]])
array([[1, 2], [3, 4]])
> numpy.array([[1,2],[3,4]]);
[[1, 2], [3, 4]]
Python
>>> import numpy
>>> numpy.linspace(2.0, 3.0, 5)
array([ 2. , 2.25, 2.5 , 2.75, 3. ])
> numpy.linspace(2.0, 3.0, 5);
[2, 2.25, 2.5, 2.75, 3]
Python
>>> import numpy
>>> numpy.logspace(2.0, 3.0, 5)
array([100.,177.827941,316.22776602,562.34132519,1000.])
> numpy.logspace(2.0, 3.0, 5);
[100, 177.82794100389228, 316.22776601683796, 562.341325190349, 1000]
Python
>>> import numpy
>>> numpy.exp(2.4)
11.023176380641601
>>> numpy.exp([2.4, 3.1])
array([ 11.02317638, 22.19795128])
> numpy.exp(2.4);
11.0231763806416
> numpy.exp([2.4, 3.2])
[11.0231763806416, 24.53253019710935]
Python
>>> import numpy
>>> numpy.arange(3)
array([0, 1, 2])
>>> numpy.arange(3,7)
array([3, 4, 5, 6])
>>> numpy.arange(3,7,2)
array([3, 5])
> numpy.arange(3);
[0, 1, 2]
> numpy.arange(3,7)
[3, 4, 5, 6]
> numpy.arange(3,7,2)
[3, 5]
Python
>>> import numpy
>>> x1 = range(6)
>>> numpy.power(x1, 3)
array([ 0, 1, 8, 27, 64, 125])
>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> np.power(x1, x2)
array([ 0., 1., 8., 27., 16., 5.])
> x1 = numpy.range(6);
[0, 1, 2, 3, 4, 5]
> numpy.power(x1, 3);
[0, 1, 8, 27, 64, 125]
> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0];
[1, 2, 3, 3, 2, 1]
> numpy.power(x1, x2);
[0, 1, 8, 27, 16, 5]
Python
>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.shape
(3, 2)
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.shape;
[3, 2]
Python
>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.size
6
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.size;
6
Python
>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.ndim
2
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.ndim;
2
Python
>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.strides
(16, 8)
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.strides;
[2, 1]
# Very important: in Javascript the element has 1 byte
Python
>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.dtype
dtype('int64')
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.dtype;
"Number"
Python
>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.ravel()
array([0, 1, 2, 3, 4, 5])
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.ravel();
[0, 1, 2, 3, 4, 5]
Python
>>> import numpy
>>> a = numpy.arange(6).reshape((3, 2))
>>> a.T
array([[0, 2, 4],
[1, 3, 5]])
>>> a.transpose()
array([[0, 2, 4],
[1, 3, 5]])
> a = numpy.arange(6).reshape([3, 2]);
[[0, 1], [2, 3], [4, 5]]
> a.T;
[[0, 2, 4], [1, 3, 5]]
> a.transpose();
[[0, 2, 4], [1, 3, 5]]
Python
>>> import numpy
>>> a = numpy.arange(6).reshape(3, 2)
>>> b = numpy.arange(6,12).reshape(2, 3)
>>> a.dot(b)
array([[ 9, 10, 11],
[39, 44, 49],
[69, 78, 87]])
> a = numpy.arange(6).reshape(3, 2);
[[0, 1], [2, 3], [4, 5]]
> b = numpy.arange(6,12).reshape(2, 3);
[[6, 7, 8], [9, 10, 11]]
> a.dot(b);
[[9, 10, 11], [39, 44, 49], [69, 78, 87]]
Python
>>> import numpy
>>> numpy.zeros(5)
array([ 0., 0., 0., 0., 0.])
>>> numpy.zeros((3, 2))
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
> numpy.zeros(5);
[0, 0, 0, 0, 0]
> numpy.zeros([3, 2]);
[[0, 0],[0, 0],[0, 0]]
Python
>>> import numpy
>>> numpy.ones(5)
array([ 1., 1., 1., 1., 1.])
>>> numpy.ones((3, 2))
array([[ 1., 1.],
[ 1., 1.],
[ 1., 1.]])
> numpy.ones(5);
[1, 1, 1, 1, 1]
> numpy.ones([3, 2]);
[[1, 1],[1, 1],[1, 1]]
Python
>>> import numpy
>>> numpy.random.random()
0.298740136734731
>>> numpy.random.random(2)
array([ 0.05538307, 0.74942997])
>>> numpy.random.random([2,2])
array([[ 0.51655267, 0.57323634],
[ 0.82552349, 0.10818737]])
> numpy.random.random();
0.298740136734731
> numpy.random.random(2);
[0.05538307, 0.74942997]
> numpy.random.random([2,2]);
[[0.51655267, 0.57323634], [0.82552349, 0.10818737]]
Python
>>> import numpy
>>> x = numpy.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
>>> y = numpy.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
>>> numpy.polyfit(x, y, 3)
array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254])
> x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0];
[0, 1, 2, 3, 4, 5]
> y = [0.0, 0.8, 0.9, 0.1, -0.8, -1.0];
[0, 0.8, 0.9, 0.1, -0.8, -1]
> numpy.polyfit(x, y, 3);
[0.0870370370370341, -0.8134920634920405, 1.6931216931216477, -0.039682539682528106]
Python
>>> import numpy
>>> from scipy import interpolate
>>> x = numpy.arange(0, 10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> y = numpy.exp(-x/3.0)
array([ 1. , 0.71653131, 0.51341712, 0.36787944, 0.26359714,
0.1888756 , 0.13533528, 0.09697197, 0.06948345, 0.04978707])
>>> f = interpolate.interp1d(x, y)
>>> f(2.4)
array(0.4552020478881322)
> x = numpy.arange(0, 10);
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> tmp = x.map(function(v) {
return -v/3.0;
});
[-0, -0.3333333333333333, -0.6666666666666666, -1, -1.3333333333333333, -1.6666666666666667, -2, -2.3333333333333335, -2.6666666666666665, -3]
> y = numpy.exp(tmp);
[1, 0.7165313105737892, 0.513417119032592, 0.3678794411714424, 0.26359713811572677, 0.1888756028375618, 0.1353352832366127, 0.09697196786440504, 0.06948345122280153, 0.04978706836786395]
> var interpolation = new interpolate();
undefined
> interpolation.interp1d(x, y);
undefined
> interpolation.eval(2.4);
0.4552020478881322
> interpolation.eval([3.1, 2.4]);
[0.35745121086587084, 0.4552020478881322]
Python
>>> from scipy import stats
>>> x = [0.6,0.1,0.7,0.7,0.3,0.6,0.1,0.6,0.7,0.8]
>>> y = [0.8,0.8,0.2,0.1,0.8,0.3,0.5,0.9,0.1,0.2]
>>> slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
>>> slope
-0.72818791946308714
>>> intercept
0.84865771812080526
> var Stats = new stats();
undefined
> x = [ 0.6, 0.1, 0.7, 0.7, 0.3, 0.6, 0.1, 0.6, 0.7, 0.8 ];
[0.6, 0.1, 0.7, 0.7, 0.3, 0.6, 0.1, 0.6, 0.7, 0.8]
> y = [ 0.8, 0.8, 0.2, 0.1, 0.8, 0.3, 0.5, 0.9, 0.1, 0.2 ];
[0.8, 0.8, 0.2, 0.1, 0.8, 0.3, 0.5, 0.9, 0.1, 0.2]
> Stats.linregress(x, y);
undefined
> Stats.get_slope();
-0.7281879194630861
> Stats.get_intercept();
0.8486577181208048
This is very important, the test was executed in a MacBookPro i5
The python Code:
import time
import numpy
def test():
x = numpy.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
y = numpy.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
start = time.time()
for num in range(1,10000):
numpy.polyfit(x, y, 3)
end = time.time()
microsecs = end - start
print microsecs * 1000
test()
function test() {
x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0];
y = [0.0, 0.8, 0.9, 0.1, -0.8, -1.0];
var start = +new Date();
for (var i=0;i<10000;i++)
numpy.polyfit(x, y, 3)
var end = +new Date();
var diff = end - start;
alert(diff);
}
test();
Python: 1604 milliseconds Javascript: 14 milliseconds
(Python Extension Packages in Javascript)
Python Extension Packages in Javascript is open-source implementation of some common libraries used in the scientific python programming. The main goal of this project is to improve migration of python language to javascript.
Copyright 2016 Alvaro Fernandez
License: MIT/X11
$ npm install pyextjs
> require('pyextjs');
> numpy.linspace(2.0,3.0,5);
Just include the following libraries in your html.
<!doctype html>
<html>
<head>
<script type="text/javascript" src="../js/ss.js"></script>
<script type="text/javascript" src="../js/Numpy.js"></script>
<script type="text/javascript" src="../js/PolySolve.js"></script>
<script type="text/javascript" src="../js/Scipy.js"></script>
<script type="text/javascript">
// Use Numpy & Scipy like python in javascript
function ready() {
var ls = numpy.linspace(2.0,3.0,5);
}
</script>
</head>
</html>
##Performance
This is very important, the test was executed in a MacBookPro i5
The python Code:
import time
import numpy
def test():
x = numpy.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
y = numpy.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
start = time.time()
for num in range(1,10000):
numpy.polyfit(x, y, 3)
end = time.time()
microsecs = end - start
print microsecs * 1000
test()
function test() {
x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0];
y = [0.0, 0.8, 0.9, 0.1, -0.8, -1.0];
var start = +new Date();
for (var i=0;i<10000;i++)
numpy.polyfit(x, y, 3)
var end = +new Date();
var diff = end - start;
alert(diff);
}
test();
Python: 1604 milliseconds Javascript: 14 milliseconds
Javascript! Very fast!!!