forked from lvandeve/jmat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmat_demo.html
257 lines (223 loc) · 9.3 KB
/
jmat_demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>Jmat.js Demo</title>
<style type="text/css">
body {
color: #444;
margin: 0;
font: 16px Monospace, sans-serif
}
</style>
</head>
<body>
<script type="text/javascript" src="jmat.js" ></script>
<script type="text/javascript" src="jmat_plot.js" ></script>
<script type="text/javascript" src="jmat_test.js" ></script>
<script type="text/javascript">
/*
Copyright (c) 2011-2014, Lode Vandevenne
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function makeElement(parent, tag) {
var el = document.createElement(tag);
parent.appendChild(el);
return el;
}
function makeAbsElement(px, py, parent, tag) {
var el = document.createElement(tag);
el.style.position = 'absolute';
el.style.left = '' + Math.floor(px) + 'px';
el.style.top = '' + Math.floor(py) + 'px';
parent.appendChild(el);
return el;
}
function makeRelElement(px, py, parent, tag) {
var el = document.createElement(tag);
el.style.position = 'relative';
el.style.left = '' + Math.floor(px) + 'px';
el.style.top = '' + Math.floor(py) + 'px';
parent.appendChild(el);
return el;
}
function makeRelDivAt(px, py, parent) {
return makeRelElement(px, py, parent, 'div');
}
function makeBlockDiv(parent) {
var el = document.createElement('div');
parent.appendChild(el);
return el;
}
function makeBlockDivAt(px, py, parent) {
var el = makeBlockDiv(parent);
el.style.paddingLeft = '' + Math.floor(px) + 'px';
el.style.paddingTop = '' + Math.floor(py) + 'px';
parent.appendChild(el);
return el;
}
function makeRelDiv(parent) {
var el = document.createElement('div');
el.style.position = 'relative';
parent.appendChild(el);
return el;
}
function makeDropDown(x, y, options, parent) {
var sel = makeAbsElement(x, y, parent, 'select');
for(var i = 0; i < options.length; i++) {
makeElement(sel, 'option').innerHTML = options[i];
}
return sel;
}
function makeLabeledDropDown(x, y, label, options, parent) {
var div = makeAbsElement(x, y, parent, 'div');
div.innerHTML = label;
return makeDropDown(x, y + 16, options, parent);
}
var fun1d = [
'--',
'gamma', 'digamma', 'trigamma', 'loggamma', 'gamma_inv',
'airy', 'bairy', 'airy_deriv', 'bairy_deriv',
'erf', 'erfc', 'erf_inv', 'erfc_inv', 'erfi', 'dawson', 'faddeeva',
'zeta', 'eta', 'dilog', 'trilog',
'exp', 'log', 'sqrt',
'lambertw', 'lambertwm',
'sin', 'cos', 'tan',
'asin', 'acos', 'atan',
'sinh', 'cosh', 'tanh',
'asinh', 'acosh', 'atanh',
'sinc',
'abs', 'arg', 'frac', 'fracn',
'bitnot',
'minkowski',
'x', 'inv',
];
var fun2d = [
'--',
'gamma_p', 'gamma_q', 'incgamma_lower', 'incgamma_upper', 'polygamma',
'beta',
'besselj', 'bessely', 'besseli', 'besselk', 'hankelh1', 'hankelh2',
'lambertwb',
'polylog', 'hurwitzzeta',
'binomial', 'permutation', 'stirling2',
'theta1', 'theta2', 'theta3', 'theta4',
'hypergeometric0F1',
'atan2',
'agm', 'ghm',
'tetration',
'mod', 'rem',
'bitand', 'bitor', 'bitxor', 'lshift', 'rshift',
'add', 'sub', 'mul', 'div', 'pow', 'logy',
];
var headerEl = makeBlockDiv(document.body);
headerEl.innerHTML = '<h2>Jmat.js</h2>';
headerEl.style.backgroundColor = '#2b6';
headerEl.style.padding = '2px';
headerEl.style.textAlign = 'center';
headerEl.style.color = 'white';
var bodyEl = makeRelDivAt(0, 0, document.body);
bodyEl.style.margin = '50px';
var el = makeBlockDivAt(0, 0, bodyEl);
el.innerHTML = 'Jmat.js is a mathematics library in JavaScript, supporting complex special functions, matrices and statistical distributions. This demo allows plotting several of the Jmat functions, and evaluating arbitrary Jmat code.';
el.innerHTML += '<p/><a href="https://github.com/lvandeve/jmat">GitHub page</a>';
var plotTitle = makeBlockDiv(bodyEl);
plotTitle.innerHTML = '<h2>Plot demo</h2>';
plotTitle.style.margin = '5px';
var plotContainerEl = makeRelDivAt(15, 0, bodyEl);
plotContainerEl.style.width = '400px';
plotContainerEl.style.height = '400px';
var plotDropdownsBlock = makeBlockDiv(bodyEl);
var plotDropdowns = makeRelDiv(plotDropdownsBlock);
plotDropdowns.style.height = '70px';
var el0 = makeLabeledDropDown(0, 0, 'real', fun1d, plotDropdowns);
el0.onchange = function() {
Jmat.stopPlotting();
var f = fun1d[el0.selectedIndex];
if(Jmat[f]) Jmat.plotReal(Jmat[f], plotContainerEl, {p:1}, f);
};
var el1 = makeLabeledDropDown(130, 0, 'complex', fun1d, plotDropdowns);
el1.onchange = function() {
Jmat.stopPlotting();
var f = fun1d[el1.selectedIndex];
if(Jmat[f]) Jmat.plotComplex(Jmat[f], plotContainerEl, {p:1}, f);
};
var el2 = makeLabeledDropDown(260, 0, '2d', fun2d, plotDropdowns);
el2.onchange = function() {
Jmat.stopPlotting();
var f = fun2d[el2.selectedIndex];
if(Jmat[f]) Jmat.plot2D(Jmat[f], plotContainerEl, {p:1}, f);
};
Jmat.plotComplex(Jmat['gamma'], plotContainerEl, {p:1}, 'gamma');
var plotInfo = makeBlockDiv(bodyEl);
var info = makeRelDivAt(0, 0, plotInfo);
info.innerHTML =
'Plot types: "Real": function of 1 argument plotted with classic X/Y real plot. "Complex": function of 1 argument plotted in 2D for complex arguments with complex color wheel. "2D": function of 2 arguments plotted for real arguments in 2D with result as complex color wheel.<p/>' +
'Complex color wheel legend: red = positive, cyan = negative, black = zero, white = infinity, darker = lower abs, lighter = higher abs, acid green = positive imaginary, purple = negative imaginary, other hues = other complex arguments, grey = NaN<p/>';
var evalTitle = makeBlockDiv(bodyEl);
evalTitle.innerHTML = '<h2>Eval demo</h2>';
evalTitle.style.margin = '5px';
var evalContainer = makeBlockDiv(bodyEl);
var area = makeElement(evalContainer, 'textarea');
area.style.width = '700px';
area.style.height = '50px';
area.value = 'Jmat.fft([[1,2],[3,4]]).toString()';
var button = makeRelDivAt(0, 5, evalContainer);
button.innerHTML = 'eval';
button.style.textAlign = 'center';
button.style.backgroundColor = '#eee';
button.style.border = '1px solid black';
button.style.width = '50px';
button.style.height = '20px';
var result = makeBlockDivAt(0, 10, evalContainer);
result.innerHTML = '_';
result.style.width = '500px';
button.onclick = function() {
result.innerHTML = 'answer: ' + eval(area.value);
};
var examples = makeBlockDivAt(0, 10, evalContainer);
examples.style.color = '#aaa';
examples.style.width = '1000px';
examples.style.fontSize = 'small';
examples.innerHTML = 'Examples:<br/>' +
'<ul>' +
'<li>Complex(\'1+2i\').add(Complex(\'3+4i\')) </li>' +
'<li>Jmat.besselj(5, Complex(5.5, 1)) </li>' +
'<li>Jmat.fft([[1,2],[3,4]]).toString()</li>' +
'<li>Jmat.eig([[1,2],[3,4]]).l.toString()</li>' +
'<li>Jmat.gamma(5.5).add(Jmat.trigamma(5.5)) </li>' +
'<li>Jmat.factorize(30030) </li>' +
'<li>Jmat.plot2D(Jmat.gamma_p, plotContainerEl); </li>' +
'<li>Jmat.plotComplex(function(z) { return Jmat.polygamma(4, z); }, plotContainerEl, {p:1}); </li>' +
'<li>Jmat.plotReal(function(x) { return Jmat.cdf_studentt(x, 2); }, plotContainerEl, {p:1}); </li>' +
'<li>Jmat.plotComplex(function(z) { return Jmat.theta2(z, 0.2); }, plotContainerEl, {p:1}); </li>' +
'<li>Jmat.plotReal(function(x) { return Jmat.pdf_laplace(x, 0, 1); }, plotContainerEl, {xsize:10, ysize:2}); </li>' +
'<li>Jmat.plotComplex(function(c) { var i = 0; var z = Complex(0); for(;;) { if(z.abs() > 2) break; z = z.mul(z).add(c); i++; if(i > 60) return Complex(0); } return Complex.polar(1, i * Math.PI / 60); }, plotContainerEl, {p:1, s:4}); </li>' +
'</ul>' +
'ops: add, sub, mul, div, pow, sqrt, cos, sign, ceil, zeta, ... <br/>' +
'matrix ops: determinant, eig, evd, rank, trace, norm, norm2, svd, qr, pseudoinverse, ... <br/>' +
'stat ops: pdf_, cdf_ or qf_ + , normal, cauchy, studentt, chi_square, gamma, ... <br/>';
info = makeBlockDivAt(0, 10, bodyEl);
info.innerHTML = 'This software is provided \'as-is\', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.<p/>' +
'Copyright (c) 2011-2014 by Lode Vandevenne.';
</script>
</body>
</html>