@zushah/chalkboard 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -8
- package/README.md +19 -17
- package/docs/README.md +1 -1
- package/examples/README.md +1 -1
- package/examples/fluid.js +1 -1
- package/examples/hyperbolics.js +4 -4
- package/examples/mandelbrot.js +1 -1
- package/examples/matr-donut.js +1 -1
- package/examples/newton.js +4 -4
- package/examples/quat-donut.js +1 -1
- package/package.json +5 -2
- package/src/Chalkboard.js +836 -451
- package/src/ChalkboardProcessing.js +706 -328
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
/*
|
|
2
2
|
The Chalkboard Library
|
|
3
|
-
Version 1.
|
|
3
|
+
Version 1.6.0 released 12/25/2023
|
|
4
4
|
Authored by Zushah ===> https://www.github.com/Zushah
|
|
5
5
|
Available under the MIT License ===> https://www.opensource.org/license/mit/
|
|
6
6
|
|
|
7
7
|
The Chalkboard library is a JavaScript namespace that provides a plethora of both practical and abstract mathematical functionalities for its user.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
Repository ===> https://www.github.com/Zushah/Chalkboard
|
|
10
|
+
Website ===> https://zushah.github.io/Chalkboard/home.html
|
|
11
11
|
*/
|
|
12
12
|
var Chalkboard = {
|
|
13
13
|
README: function() {
|
|
14
|
-
console.log("The Chalkboard Library\nVersion 1.
|
|
14
|
+
console.log("The Chalkboard Library\nVersion 1.6.0 released 12/25/2023\nAuthored by Zushah ===> https://www.github.com/Zushah\nAvailable under the MIT License ===> https://www.opensource.org/license/mit/\n\nThe Chalkboard library is a JavaScript namespace that provides a plethora of both practical and abstract mathematical functionalities for its user.\n\nRepository ===> https://www.github.com/Zushah/Chalkboard\nWebsite ===> https://zushah.github.io/Chalkboard/home.html");
|
|
15
15
|
},
|
|
16
16
|
LOGO: function(x, y, s) {
|
|
17
17
|
x = x || width / 2;
|
|
@@ -47,9 +47,45 @@ var Chalkboard = {
|
|
|
47
47
|
},
|
|
48
48
|
numb: {
|
|
49
49
|
random: function(inf, sup) {
|
|
50
|
-
inf
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
if(inf === undefined) {
|
|
51
|
+
inf = 0;
|
|
52
|
+
}
|
|
53
|
+
if(sup === undefined) {
|
|
54
|
+
sup = 1;
|
|
55
|
+
}
|
|
56
|
+
return inf + (sup - inf) * Math.random();
|
|
57
|
+
},
|
|
58
|
+
exponential: function(l) {
|
|
59
|
+
if(l === undefined) {
|
|
60
|
+
l = 1;
|
|
61
|
+
}
|
|
62
|
+
return l <= 0 ? 0 : -Math.log(Math.random()) / l;
|
|
63
|
+
},
|
|
64
|
+
Gaussian: function(height, mean, deviation) {
|
|
65
|
+
var u1 = Math.random(), u2 = Math.random();
|
|
66
|
+
var random = Chalkboard.real.sqrt(-2 * Chalkboard.real.ln(u1)) * Chalkboard.trig.cos(Chalkboard.PI(2) * u2);
|
|
67
|
+
return random * height * Chalkboard.real.sqrt(deviation) + mean;
|
|
68
|
+
},
|
|
69
|
+
Bernoullian: function(p) {
|
|
70
|
+
if(p === undefined) {
|
|
71
|
+
p = 0.5;
|
|
72
|
+
}
|
|
73
|
+
return Math.random() < p ? 1 : 0;
|
|
74
|
+
},
|
|
75
|
+
Poissonian: function(l) {
|
|
76
|
+
if(l === undefined) {
|
|
77
|
+
l = 1;
|
|
78
|
+
}
|
|
79
|
+
if(l > 0) {
|
|
80
|
+
var L = Chalkboard.E(-l);
|
|
81
|
+
var p = 1, k = 0;
|
|
82
|
+
for(; p > L; ++k) {
|
|
83
|
+
p *= Math.random();
|
|
84
|
+
}
|
|
85
|
+
return k - 1;
|
|
86
|
+
} else {
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
53
89
|
},
|
|
54
90
|
factorial: function(num) {
|
|
55
91
|
if(num >= 0) {
|
|
@@ -236,18 +272,39 @@ var Chalkboard = {
|
|
|
236
272
|
}
|
|
237
273
|
return sequence[num];
|
|
238
274
|
},
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
275
|
+
Goldbach: function(num) {
|
|
276
|
+
if(num % 2 === 0) {
|
|
277
|
+
if(num !== 4) {
|
|
278
|
+
var a = num / 2, b = num / 2;
|
|
279
|
+
if(a % 2 === 0) {
|
|
280
|
+
a--;
|
|
281
|
+
b++;
|
|
282
|
+
}
|
|
283
|
+
while(a >= 3) {
|
|
284
|
+
if(Chalkboard.numb.isPrime(a) && Chalkboard.numb.isPrime(b)) {
|
|
285
|
+
return [a, b];
|
|
286
|
+
}
|
|
287
|
+
a -= 2;
|
|
288
|
+
b += 2;
|
|
289
|
+
}
|
|
290
|
+
return undefined;
|
|
291
|
+
} else {
|
|
292
|
+
return [2, 2];
|
|
247
293
|
}
|
|
294
|
+
} else {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
Euler: function(num) {
|
|
299
|
+
if(num > 0) {
|
|
300
|
+
var factors = Chalkboard.numb.factors(num);
|
|
301
|
+
for(var i = 0; i < factors.length; i++) {
|
|
302
|
+
num *= (factors[i] - 1) / factors[i];
|
|
303
|
+
}
|
|
304
|
+
return num;
|
|
305
|
+
} else {
|
|
306
|
+
return undefined;
|
|
248
307
|
}
|
|
249
|
-
var z = u * Chalkboard.real.sqrt(-2 * Chalkboard.real.ln(s) / s);
|
|
250
|
-
return z * height * deviation + mean;
|
|
251
308
|
}
|
|
252
309
|
},
|
|
253
310
|
real: {
|
|
@@ -255,7 +312,9 @@ var Chalkboard = {
|
|
|
255
312
|
type = type || "expl";
|
|
256
313
|
if(type === "expl") {
|
|
257
314
|
return {definition: definition, type: type};
|
|
258
|
-
}
|
|
315
|
+
} else if(type === "inve") {
|
|
316
|
+
return {definition: definition, type: type};
|
|
317
|
+
} else if(type === "pola") {
|
|
259
318
|
return {definition: definition, type: type};
|
|
260
319
|
} else if(type === "curv") {
|
|
261
320
|
return definition.length === 2 ? {definition: [definition[0], definition[1]], type: type} : {definition: [definition[0], definition[1], definition[2]], type: type};
|
|
@@ -264,7 +323,7 @@ var Chalkboard = {
|
|
|
264
323
|
} else if(type === "mult") {
|
|
265
324
|
return {definition: definition, type: type};
|
|
266
325
|
} else {
|
|
267
|
-
return "TypeError: Parameter \"type\" must be either \"expl\", \"pola\", \"curv\", \"surf\", or \"mult\".";
|
|
326
|
+
return "TypeError: Parameter \"type\" must be either \"expl\", \"inve\", \"pola\", \"curv\", \"surf\", or \"mult\".";
|
|
268
327
|
}
|
|
269
328
|
},
|
|
270
329
|
parse: function(str, init) {
|
|
@@ -275,6 +334,9 @@ var Chalkboard = {
|
|
|
275
334
|
if(func.type === "expl") {
|
|
276
335
|
var f = Chalkboard.real.parse("x => " + func.definition);
|
|
277
336
|
return f(val);
|
|
337
|
+
} else if(func.type === "inve") {
|
|
338
|
+
var f = Chalkboard.real.parse("y => " + func.definition);
|
|
339
|
+
return f(val);
|
|
278
340
|
} else if(func.type === "pola") {
|
|
279
341
|
var r = Chalkboard.real.parse("O => " + func.definition);
|
|
280
342
|
return r(val);
|
|
@@ -310,7 +372,11 @@ var Chalkboard = {
|
|
|
310
372
|
}
|
|
311
373
|
},
|
|
312
374
|
pow: function(base, num) {
|
|
313
|
-
|
|
375
|
+
if(base === 0 && num === 0) {
|
|
376
|
+
return 1;
|
|
377
|
+
} else {
|
|
378
|
+
return Math.exp(num * Math.log(base));
|
|
379
|
+
}
|
|
314
380
|
},
|
|
315
381
|
log: function(base, num) {
|
|
316
382
|
return Chalkboard.real.ln(num) / Chalkboard.real.ln(base);
|
|
@@ -328,7 +394,8 @@ var Chalkboard = {
|
|
|
328
394
|
return undefined;
|
|
329
395
|
}
|
|
330
396
|
},
|
|
331
|
-
|
|
397
|
+
root: function(num, index) {
|
|
398
|
+
index = index || 3;
|
|
332
399
|
return Math.exp(Math.log(num) / index);
|
|
333
400
|
},
|
|
334
401
|
tetration: function(base, num) {
|
|
@@ -529,6 +596,18 @@ var Chalkboard = {
|
|
|
529
596
|
sqrt: function(comp) {
|
|
530
597
|
return Chalkboard.comp.new(Chalkboard.real.sqrt((comp.a + Chalkboard.real.sqrt((comp.a * comp.a) + (comp.b * comp.b))) / 2), Chalkboard.numb.sgn(comp.b) * Chalkboard.real.sqrt((-comp.a + Chalkboard.real.sqrt((comp.a * comp.a) + (comp.b * comp.b))) / 2));
|
|
531
598
|
},
|
|
599
|
+
root: function(comp, index) {
|
|
600
|
+
index = index || 3;
|
|
601
|
+
if(Number.isInteger(index) && index > 0) {
|
|
602
|
+
var result = [];
|
|
603
|
+
var r = Chalkboard.comp.mag(comp);
|
|
604
|
+
var t = Chalkboard.comp.arg(comp);
|
|
605
|
+
for(var i = 0; i < index; i++) {
|
|
606
|
+
result.push(Chalkboard.comp.new(Chalkboard.real.root(r, index) * Chalkboard.trig.cos((t + Chalkboard.PI(2 * i)) / index), Chalkboard.real.root(r, index) * Chalkboard.trig.sin((t + Chalkboard.PI(2 * i)) / index)));
|
|
607
|
+
}
|
|
608
|
+
return result;
|
|
609
|
+
}
|
|
610
|
+
},
|
|
532
611
|
rotate: function(comp, rad) {
|
|
533
612
|
return Chalkboard.comp.new(Chalkboard.comp.mag(comp) * Chalkboard.trig.cos(Chalkboard.comp.arg(comp) + rad), Chalkboard.comp.mag(comp) * Chalkboard.trig.sin(Chalkboard.comp.arg(comp) + rad));
|
|
534
613
|
},
|
|
@@ -538,17 +617,6 @@ var Chalkboard = {
|
|
|
538
617
|
conjugate: function(comp) {
|
|
539
618
|
return Chalkboard.comp.new(comp.a, -comp.b);
|
|
540
619
|
},
|
|
541
|
-
root: function(comp, n) {
|
|
542
|
-
if(Number.isInteger(n) && n > 0) {
|
|
543
|
-
var result = [];
|
|
544
|
-
var r = Chalkboard.comp.mag(comp);
|
|
545
|
-
var t = Chalkboard.comp.arg(comp);
|
|
546
|
-
for(var i = 0; i < n; i++) {
|
|
547
|
-
result.push(Chalkboard.comp.new(Chalkboard.real.nrt(r, n) * Chalkboard.trig.cos((t + Chalkboard.PI(2 * i)) / n), Chalkboard.real.nrt(r, n) * Chalkboard.trig.sin((t + Chalkboard.PI(2 * i)) / n)));
|
|
548
|
-
}
|
|
549
|
-
return result;
|
|
550
|
-
}
|
|
551
|
-
},
|
|
552
620
|
dist: function(comp_1, comp_2) {
|
|
553
621
|
return Chalkboard.real.sqrt(((comp_2.a - comp_1.a) * (comp_2.a - comp_1.a)) + ((comp_2.b - comp_1.b) * (comp_2.b - comp_1.b)));
|
|
554
622
|
},
|
|
@@ -698,85 +766,101 @@ var Chalkboard = {
|
|
|
698
766
|
}
|
|
699
767
|
},
|
|
700
768
|
plot: {
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
769
|
+
CONTEXT: "ctx",
|
|
770
|
+
xyplane: function(config) {
|
|
771
|
+
config = config || {};
|
|
772
|
+
config = {
|
|
773
|
+
size: config.size || 1,
|
|
774
|
+
stroke: config.stroke || color(0),
|
|
775
|
+
origin: config.origin || [width / 2, height / 2],
|
|
776
|
+
strokeWeight: config.strokeWeight || 2
|
|
777
|
+
};
|
|
778
|
+
config.size /= 100;
|
|
707
779
|
pushMatrix();
|
|
708
|
-
translate(origin[0], origin[1]);
|
|
709
|
-
stroke(
|
|
710
|
-
strokeWeight(
|
|
711
|
-
for(var i = Math.floor(-origin[0] /
|
|
712
|
-
line(i /
|
|
713
|
-
}
|
|
714
|
-
for(var i = Math.floor(-origin[1] /
|
|
715
|
-
line(-origin[0], i /
|
|
716
|
-
}
|
|
717
|
-
strokeWeight(
|
|
718
|
-
line(-origin[0], 0, width - origin[0], 0);
|
|
719
|
-
line(0, -origin[1], 0, width - origin[1]);
|
|
780
|
+
translate(config.origin[0], config.origin[1]);
|
|
781
|
+
stroke(config.stroke);
|
|
782
|
+
strokeWeight(config.strokeWeight / 4);
|
|
783
|
+
for(var i = Math.floor(-config.origin[0] / config.size); i <= (width - config.origin[0]) / config.size; i++) {
|
|
784
|
+
line(i / config.size, -config.origin[1], i / config.size, width - config.origin[1]);
|
|
785
|
+
}
|
|
786
|
+
for(var i = Math.floor(-config.origin[1] / config.size); i <= (width - config.origin[1]) / config.size; i++) {
|
|
787
|
+
line(-config.origin[0], i / config.size, width - config.origin[0], i / config.size);
|
|
788
|
+
}
|
|
789
|
+
strokeWeight(config.strokeWeight);
|
|
790
|
+
line(-config.origin[0], 0, width - config.origin[0], 0);
|
|
791
|
+
line(0, -config.origin[1], 0, width - config.origin[1]);
|
|
720
792
|
popMatrix();
|
|
721
793
|
},
|
|
722
|
-
rOplane: function(
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
794
|
+
rOplane: function(config) {
|
|
795
|
+
config = config || {};
|
|
796
|
+
config = {
|
|
797
|
+
size: config.size || 1,
|
|
798
|
+
stroke: config.stroke || color(0),
|
|
799
|
+
origin: config.origin || [width / 2, height / 2],
|
|
800
|
+
strokeWeight: config.strokeWeight || 2
|
|
801
|
+
};
|
|
802
|
+
config.size /= 100;
|
|
728
803
|
pushMatrix();
|
|
729
|
-
translate(origin[0], origin[1]);
|
|
804
|
+
translate(config.origin[0], config.origin[1]);
|
|
730
805
|
noFill();
|
|
731
|
-
stroke(
|
|
732
|
-
strokeWeight(
|
|
733
|
-
for(var i = 0; i <=
|
|
734
|
-
ellipse(0, 0, 2 * i /
|
|
735
|
-
}
|
|
736
|
-
strokeWeight(
|
|
737
|
-
line(-origin[0], 0, width - origin[0], 0);
|
|
738
|
-
line(0, -origin[1], 0, width - origin[1]);
|
|
806
|
+
stroke(config.stroke);
|
|
807
|
+
strokeWeight(config.strokeWeight / 4);
|
|
808
|
+
for(var i = 0; i <= config.size * width / 2; i++) {
|
|
809
|
+
ellipse(0, 0, 2 * i / config.size, 2 * i / config.size);
|
|
810
|
+
}
|
|
811
|
+
strokeWeight(config.strokeWeight);
|
|
812
|
+
line(-config.origin[0], 0, width - config.origin[0], 0);
|
|
813
|
+
line(0, -config.origin[1], 0, width - config.origin[1]);
|
|
739
814
|
popMatrix();
|
|
740
815
|
},
|
|
741
|
-
function: function(func,
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
816
|
+
function: function(func, config) {
|
|
817
|
+
config = config || {};
|
|
818
|
+
config = {
|
|
819
|
+
size: config.size || 1,
|
|
820
|
+
stroke: config.stroke || color(0),
|
|
821
|
+
domain: config.domain || (func.type === "comp" ? [[-10, 10], [-10, 10]] : [-10, 10]),
|
|
822
|
+
origin: config.origin || [width / 2, height / 2],
|
|
823
|
+
strokeWeight: config.strokeWeight || 2
|
|
824
|
+
};
|
|
825
|
+
config.size /= 100;
|
|
748
826
|
var data = [];
|
|
749
827
|
pushMatrix();
|
|
750
|
-
translate(origin[0], origin[1]);
|
|
828
|
+
translate(config.origin[0], config.origin[1]);
|
|
751
829
|
noFill();
|
|
752
|
-
strokeWeight(
|
|
753
|
-
stroke(
|
|
830
|
+
strokeWeight(config.strokeWeight);
|
|
831
|
+
stroke(config.stroke);
|
|
754
832
|
beginShape();
|
|
755
833
|
if(func.type === "expl") {
|
|
756
834
|
var f = Chalkboard.real.parse("x => " + func.definition);
|
|
757
|
-
for(var i = domain[0] /
|
|
758
|
-
vertex(i, -f(i *
|
|
835
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i++) {
|
|
836
|
+
vertex(i, -f(i * config.size) / config.size);
|
|
759
837
|
data.push([i, f(i)]);
|
|
760
838
|
}
|
|
839
|
+
} else if(func.type === "inve") {
|
|
840
|
+
var f = Chalkboard.real.parse("y => " + func.definition);
|
|
841
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i++) {
|
|
842
|
+
vertex(f(i * config.size) / config.size, -i);
|
|
843
|
+
data.push([f(i), i]);
|
|
844
|
+
}
|
|
761
845
|
} else if(func.type === "pola") {
|
|
762
846
|
var r = Chalkboard.real.parse("O => " + func.definition);
|
|
763
|
-
for(var i = domain[0] /
|
|
764
|
-
vertex(r(i *
|
|
847
|
+
for(var i = config.domain[0] / config.size; i < config.domain[1] / config.size; i++) {
|
|
848
|
+
vertex(r(i * config.size) / config.size * Chalkboard.trig.cos(i * config.size), -r(i * config.size) / config.size * Chalkboard.trig.sin(i * config.size));
|
|
765
849
|
data.push([i, r(i)]);
|
|
766
850
|
}
|
|
767
851
|
} else if(func.type === "curv") {
|
|
768
852
|
var x = Chalkboard.real.parse("t => " + func.definition[0]),
|
|
769
853
|
y = Chalkboard.real.parse("t => " + func.definition[1]);
|
|
770
|
-
for(var i = domain[0] /
|
|
771
|
-
vertex(x(i *
|
|
854
|
+
for(var i = config.domain[0] / config.size; i < config.domain[1] / config.size; i++) {
|
|
855
|
+
vertex(x(i * config.size) / config.size, -y(i * config.size) / config.size);
|
|
772
856
|
data.push([x(i), y(i)]);
|
|
773
857
|
}
|
|
774
858
|
} else if(func.type === "comp") {
|
|
775
859
|
var u = Chalkboard.comp.parse("(a, b) => " + func.definition[0]),
|
|
776
860
|
v = Chalkboard.comp.parse("(a, b) => " + func.definition[1]);
|
|
777
|
-
for(var i = domain[0][0] /
|
|
778
|
-
for(var j = domain[1][0] /
|
|
779
|
-
var z = Chalkboard.comp.new(u(i *
|
|
861
|
+
for(var i = config.domain[0][0] / config.size; i <= config.domain[0][1] / config.size; i += 5) {
|
|
862
|
+
for(var j = config.domain[1][0] / config.size; j <= config.domain[1][1] / config.size; j += 5) {
|
|
863
|
+
var z = Chalkboard.comp.new(u(i * config.size, j * config.size) / config.size, v(i * config.size, j * config.size) / config.size);
|
|
780
864
|
noStroke();
|
|
781
865
|
if(z.a === 0 && z.b === 0) {
|
|
782
866
|
fill(0, 0, 0);
|
|
@@ -792,23 +876,27 @@ var Chalkboard = {
|
|
|
792
876
|
}
|
|
793
877
|
}
|
|
794
878
|
} else {
|
|
795
|
-
return "TypeError: Property \"type\" of parameter \"func\" must be either \"expl\", \"pola\", \"curv\", or \"comp\".";
|
|
879
|
+
return "TypeError: Property \"type\" of parameter \"func\" must be either \"expl\", \"inve\", \"pola\", \"curv\", or \"comp\".";
|
|
796
880
|
}
|
|
797
881
|
endShape();
|
|
798
882
|
popMatrix();
|
|
799
883
|
return data;
|
|
800
884
|
},
|
|
801
|
-
barplot: function(arr, bins,
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
885
|
+
barplot: function(arr, bins, config) {
|
|
886
|
+
config = config || {};
|
|
887
|
+
config = {
|
|
888
|
+
size: config.size || 1,
|
|
889
|
+
stroke: config.stroke || color(0),
|
|
890
|
+
fill: config.fill || color(255),
|
|
891
|
+
origin: config.origin || [width / 2, height / 2],
|
|
892
|
+
strokeWeight: config.strokeWeight || 2
|
|
893
|
+
};
|
|
894
|
+
config.size /= 100;
|
|
807
895
|
pushMatrix();
|
|
808
|
-
translate(origin[0], origin[1]);
|
|
809
|
-
strokeWeight(
|
|
810
|
-
stroke(
|
|
811
|
-
fill(
|
|
896
|
+
translate(config.origin[0], config.origin[1]);
|
|
897
|
+
strokeWeight(config.strokeWeight);
|
|
898
|
+
stroke(config.stroke);
|
|
899
|
+
fill(config.fill);
|
|
812
900
|
var bars = [];
|
|
813
901
|
for(var i = 0; i < bins.length; i++) {
|
|
814
902
|
if(i === 0) {
|
|
@@ -823,25 +911,28 @@ var Chalkboard = {
|
|
|
823
911
|
for(var i = 0; i < bars.length; i++) {
|
|
824
912
|
counts.push(bars[i].length);
|
|
825
913
|
}
|
|
826
|
-
var x = 0, width = counts.length / (2 *
|
|
914
|
+
var x = 0, width = counts.length / (2 * config.size);
|
|
827
915
|
for(var i = 0; i < counts.length; i++) {
|
|
828
|
-
rect(x - width, 0, 1 /
|
|
829
|
-
x += 1 /
|
|
916
|
+
rect(x - width, 0, 1 / config.size, -counts[i] / config.size);
|
|
917
|
+
x += 1 / config.size;
|
|
830
918
|
}
|
|
831
919
|
popMatrix();
|
|
832
920
|
return bars;
|
|
833
921
|
},
|
|
834
|
-
lineplot: function(arr, bins,
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
922
|
+
lineplot: function(arr, bins, config) {
|
|
923
|
+
config = config || {};
|
|
924
|
+
config = {
|
|
925
|
+
size: config.size || 1,
|
|
926
|
+
stroke: config.stroke || color(0),
|
|
927
|
+
origin: config.origin || [width / 2, height / 2],
|
|
928
|
+
strokeWeight: config.strokeWeight || 2
|
|
929
|
+
};
|
|
930
|
+
config.size /= 100;
|
|
840
931
|
pushMatrix();
|
|
841
|
-
translate(origin[0], origin[1]);
|
|
932
|
+
translate(config.origin[0], config.origin[1]);
|
|
842
933
|
noFill();
|
|
843
|
-
strokeWeight(
|
|
844
|
-
stroke(
|
|
934
|
+
strokeWeight(config.strokeWeight);
|
|
935
|
+
stroke(config.stroke);
|
|
845
936
|
var verts = [];
|
|
846
937
|
for(var i = 0; i < bins.length; i++) {
|
|
847
938
|
if(i === 0) {
|
|
@@ -858,77 +949,89 @@ var Chalkboard = {
|
|
|
858
949
|
}
|
|
859
950
|
beginShape();
|
|
860
951
|
for(var i = 0; i < counts.length; i++) {
|
|
861
|
-
vertex(i /
|
|
952
|
+
vertex(i / config.size, -counts[i] / config.size);
|
|
862
953
|
}
|
|
863
954
|
endShape();
|
|
864
955
|
popMatrix();
|
|
865
956
|
return verts;
|
|
866
957
|
},
|
|
867
|
-
scatterplot: function(arr1, arr2,
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
958
|
+
scatterplot: function(arr1, arr2, config) {
|
|
959
|
+
config = config || {};
|
|
960
|
+
config = {
|
|
961
|
+
size: config.size || 1,
|
|
962
|
+
stroke: config.stroke || color(0),
|
|
963
|
+
origin: config.origin || [width / 2, height / 2],
|
|
964
|
+
strokeWeight: config.strokeWeight || 5
|
|
965
|
+
};
|
|
966
|
+
config.size /= 100;
|
|
873
967
|
var data = [];
|
|
874
968
|
pushMatrix();
|
|
875
|
-
translate(origin[0], origin[1]);
|
|
876
|
-
strokeWeight(
|
|
877
|
-
stroke(
|
|
969
|
+
translate(config.origin[0], config.origin[1]);
|
|
970
|
+
strokeWeight(config.strokeWeight);
|
|
971
|
+
stroke(config.stroke);
|
|
878
972
|
if(arr1.length === arr2.length) {
|
|
879
973
|
for(var i = 0; i < arr1.length; i++) {
|
|
880
|
-
point(arr1[i] /
|
|
974
|
+
point(arr1[i] / config.size - arr1.length / (2 * config.size), -arr2[i] / config.size + arr1.length / (2 * config.size));
|
|
881
975
|
data.push([arr1[i], arr2[i]]);
|
|
882
976
|
}
|
|
883
977
|
}
|
|
884
978
|
popMatrix();
|
|
885
979
|
return data;
|
|
886
980
|
},
|
|
887
|
-
comp: function(comp,
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
981
|
+
comp: function(comp, config) {
|
|
982
|
+
config = config || {};
|
|
983
|
+
config = {
|
|
984
|
+
size: config.size || 1,
|
|
985
|
+
stroke: config.stroke || color(0),
|
|
986
|
+
origin: config.origin || [width / 2, height / 2],
|
|
987
|
+
strokeWeight: config.strokeWeight || 5
|
|
988
|
+
};
|
|
989
|
+
config.size /= 100;
|
|
990
|
+
stroke(config.stroke);
|
|
991
|
+
strokeWeight(config.strokeWeight);
|
|
895
992
|
pushMatrix();
|
|
896
|
-
translate(origin[0], origin[1]);
|
|
897
|
-
point(comp.a /
|
|
993
|
+
translate(config.origin[0], config.origin[1]);
|
|
994
|
+
point(comp.a / config.size, -comp.b / config.size);
|
|
898
995
|
popMatrix();
|
|
899
996
|
noStroke();
|
|
900
997
|
noFill();
|
|
901
998
|
return [[comp.a], [comp.b]];
|
|
902
999
|
},
|
|
903
|
-
vec2: function(vec2,
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1000
|
+
vec2: function(vec2, config) {
|
|
1001
|
+
config = config || {};
|
|
1002
|
+
config = {
|
|
1003
|
+
size: config.size || 1,
|
|
1004
|
+
stroke: config.stroke || color(0),
|
|
1005
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1006
|
+
strokeWeight: config.strokeWeight || 2
|
|
1007
|
+
};
|
|
1008
|
+
config.size /= 100;
|
|
1009
|
+
stroke(config.stroke);
|
|
1010
|
+
strokeWeight(config.strokeWeight);
|
|
911
1011
|
pushMatrix();
|
|
912
|
-
translate(origin[0], origin[1]);
|
|
913
|
-
line(0, 0, vec2.x /
|
|
1012
|
+
translate(config.origin[0], config.origin[1]);
|
|
1013
|
+
line(0, 0, vec2.x / config.size, -vec2.y / config.size);
|
|
914
1014
|
popMatrix();
|
|
915
1015
|
return [[vec2.x], [vec2.y]];
|
|
916
1016
|
},
|
|
917
|
-
field: function(vec2field,
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
1017
|
+
field: function(vec2field, config) {
|
|
1018
|
+
config = config || {};
|
|
1019
|
+
config = {
|
|
1020
|
+
size: config.size || 1,
|
|
1021
|
+
stroke: config.stroke || color(0),
|
|
1022
|
+
domain: config.domain || [[-10, 10], [-10, 10]],
|
|
1023
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1024
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1025
|
+
res: config.res || 25
|
|
1026
|
+
};
|
|
1027
|
+
config.size /= 100;
|
|
925
1028
|
var data = [];
|
|
926
|
-
stroke(
|
|
927
|
-
strokeWeight(
|
|
1029
|
+
stroke(config.stroke);
|
|
1030
|
+
strokeWeight(config.strokeWeight);
|
|
928
1031
|
pushMatrix();
|
|
929
|
-
translate(origin[0], origin[1]);
|
|
930
|
-
for(var i = domain[0][0] /
|
|
931
|
-
for(var j = domain[1][0] /
|
|
1032
|
+
translate(config.origin[0], config.origin[1]);
|
|
1033
|
+
for(var i = config.domain[0][0] / config.size; i <= config.domain[0][1] / config.size; i += config.res) {
|
|
1034
|
+
for(var j = config.domain[1][0] / config.size; j <= config.domain[1][1] / config.size; j += config.res) {
|
|
932
1035
|
var v = Chalkboard.vec2.fromField(vec2field, Chalkboard.vec2.new(i, j));
|
|
933
1036
|
line(i, j, i + v.x, j + v.y);
|
|
934
1037
|
data.push([i + v.x, j + v.y]);
|
|
@@ -937,230 +1040,275 @@ var Chalkboard = {
|
|
|
937
1040
|
popMatrix();
|
|
938
1041
|
return data;
|
|
939
1042
|
},
|
|
940
|
-
vec3: function(vec3,
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
1043
|
+
vec3: function(vec3, config) {
|
|
1044
|
+
config = config || {};
|
|
1045
|
+
config = {
|
|
1046
|
+
size: config.size || 1,
|
|
1047
|
+
stroke: config.stroke || color(0),
|
|
1048
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1049
|
+
strokeWeight: config.strokeWeight || 2
|
|
1050
|
+
};
|
|
1051
|
+
config.size /= 100;
|
|
1052
|
+
stroke(config.stroke);
|
|
1053
|
+
strokeWeight(config.strokeWeight);
|
|
948
1054
|
pushMatrix();
|
|
949
|
-
translate(origin[0], origin[1]);
|
|
950
|
-
line(0, 0, (vec3.x /
|
|
1055
|
+
translate(config.origin[0], config.origin[1]);
|
|
1056
|
+
line(0, 0, (vec3.x / config.size) / (vec3.z * 0.25 + 1), (-vec3.y / config.size) / (vec3.z * 0.25 + 1));
|
|
951
1057
|
popMatrix();
|
|
952
1058
|
return [[vec3.x], [vec3.y], [vec3.z]];
|
|
953
1059
|
},
|
|
954
|
-
matr: function(matr,
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
1060
|
+
matr: function(matr, config) {
|
|
1061
|
+
config = config || {};
|
|
1062
|
+
config = {
|
|
1063
|
+
size: config.size || 1,
|
|
1064
|
+
stroke: config.stroke || color(0),
|
|
1065
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1066
|
+
strokeWeight: config.strokeWeight || 2
|
|
1067
|
+
};
|
|
1068
|
+
config.size /= 100;
|
|
960
1069
|
var plotposx = Chalkboard.vec2.new(matr[0][0], matr[1][0]);
|
|
961
1070
|
var plotnegx = Chalkboard.vec2.new(-matr[0][0], -matr[1][0]);
|
|
962
1071
|
var plotposy = Chalkboard.vec2.new(matr[0][1], matr[1][1]);
|
|
963
1072
|
var plotnegy = Chalkboard.vec2.new(-matr[0][1], -matr[1][1]);
|
|
964
1073
|
for(var i = -10; i <= 10; i++) {
|
|
965
|
-
Chalkboard.vec2.plot(plotposx,
|
|
966
|
-
Chalkboard.vec2.plot(plotnegx,
|
|
967
|
-
Chalkboard.vec2.plot(plotposy,
|
|
968
|
-
Chalkboard.vec2.plot(plotnegy,
|
|
1074
|
+
Chalkboard.vec2.plot(plotposx, {origin: [config.origin[0], config.origin[1] + (i / config.size) * matr[1][1]], strokeWeight: config.strokeWeight / 4});
|
|
1075
|
+
Chalkboard.vec2.plot(plotnegx, {origin: [config.origin[0], config.origin[1] + (i / config.size) * matr[1][1]], strokeWeight: config.strokeWeight / 4});
|
|
1076
|
+
Chalkboard.vec2.plot(plotposy, {origin: [config.origin[0] + (i / config.size) * matr[0][0], config.origin[1]], strokeWeight: config.strokeWeight / 4});
|
|
1077
|
+
Chalkboard.vec2.plot(plotnegy, {origin: [config.origin[0] + (i / config.size) * matr[0][0], config.origin[1]], strokeWeight: config.strokeWeight / 4});
|
|
969
1078
|
}
|
|
970
1079
|
var plotposaxisx = Chalkboard.vec2.new(matr[0][0], matr[1][0]);
|
|
971
1080
|
var plotnegaxisx = Chalkboard.vec2.new(-matr[0][0], -matr[1][0]);
|
|
972
1081
|
var plotposaxisy = Chalkboard.vec2.new(matr[0][1], matr[1][1]);
|
|
973
1082
|
var plotnegaxisy = Chalkboard.vec2.new(-matr[0][1], -matr[1][1]);
|
|
974
|
-
Chalkboard.vec2.plot(plotposaxisx,
|
|
975
|
-
Chalkboard.vec2.plot(plotnegaxisx,
|
|
976
|
-
Chalkboard.vec2.plot(plotposaxisy,
|
|
977
|
-
Chalkboard.vec2.plot(plotnegaxisy,
|
|
1083
|
+
Chalkboard.vec2.plot(plotposaxisx, config);
|
|
1084
|
+
Chalkboard.vec2.plot(plotnegaxisx, config);
|
|
1085
|
+
Chalkboard.vec2.plot(plotposaxisy, config);
|
|
1086
|
+
Chalkboard.vec2.plot(plotnegaxisy, config);
|
|
978
1087
|
return matr;
|
|
979
1088
|
},
|
|
980
|
-
dfdx: function(func,
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1089
|
+
dfdx: function(func, config) {
|
|
1090
|
+
config = config || {};
|
|
1091
|
+
config = {
|
|
1092
|
+
size: config.size || 1,
|
|
1093
|
+
stroke: config.stroke || color(0),
|
|
1094
|
+
domain: config.domain || [-10, 10],
|
|
1095
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1096
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1097
|
+
res: config.res || 25
|
|
1098
|
+
};
|
|
1099
|
+
config.size /= 100;
|
|
988
1100
|
var data = [];
|
|
989
1101
|
pushMatrix();
|
|
990
|
-
translate(origin[0], origin[1]);
|
|
1102
|
+
translate(config.origin[0], config.origin[1]);
|
|
991
1103
|
noFill();
|
|
992
|
-
strokeWeight(
|
|
993
|
-
stroke(
|
|
1104
|
+
strokeWeight(config.strokeWeight);
|
|
1105
|
+
stroke(config.stroke);
|
|
994
1106
|
beginShape();
|
|
995
|
-
for(var i = domain[0] /
|
|
996
|
-
|
|
997
|
-
|
|
1107
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i += config.res) {
|
|
1108
|
+
if(func.type === "expl") {
|
|
1109
|
+
vertex(i, -Chalkboard.calc.dfdx(func, i * config.size) / config.size);
|
|
1110
|
+
data.push([i, Chalkboard.calc.dfdx(func, i)]);
|
|
1111
|
+
} else if(func.type === "inve") {
|
|
1112
|
+
vertex(Chalkboard.calc.dfdx(func, i * config.size) / config.size, -i);
|
|
1113
|
+
data.push([Chalkboard.calc.dfdx(func, i), i]);
|
|
1114
|
+
}
|
|
998
1115
|
}
|
|
999
1116
|
endShape();
|
|
1000
1117
|
popMatrix();
|
|
1001
1118
|
return data;
|
|
1002
1119
|
},
|
|
1003
|
-
d2fdx2: function(func,
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1120
|
+
d2fdx2: function(func, config) {
|
|
1121
|
+
config = config || {};
|
|
1122
|
+
config = {
|
|
1123
|
+
size: config.size || 1,
|
|
1124
|
+
stroke: config.stroke || color(0),
|
|
1125
|
+
domain: config.domain || [-10, 10],
|
|
1126
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1127
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1128
|
+
res: config.res || 25
|
|
1129
|
+
};
|
|
1130
|
+
config.size /= 100;
|
|
1011
1131
|
var data = [];
|
|
1012
1132
|
pushMatrix();
|
|
1013
|
-
translate(origin[0], origin[1]);
|
|
1133
|
+
translate(config.origin[0], config.origin[1]);
|
|
1014
1134
|
noFill();
|
|
1015
|
-
strokeWeight(
|
|
1016
|
-
stroke(
|
|
1135
|
+
strokeWeight(config.strokeWeight);
|
|
1136
|
+
stroke(config.stroke);
|
|
1017
1137
|
beginShape();
|
|
1018
|
-
for(var i = domain[0] /
|
|
1019
|
-
|
|
1020
|
-
|
|
1138
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i += config.res) {
|
|
1139
|
+
if(func.type === "expl") {
|
|
1140
|
+
vertex(i, -Chalkboard.calc.d2fdx2(func, i * config.size) / config.size);
|
|
1141
|
+
data.push([i, Chalkboard.calc.d2fdx2(func, i)]);
|
|
1142
|
+
} else if(func.type === "inve") {
|
|
1143
|
+
vertex(Chalkboard.calc.d2fdx2(func, i * config.size) / config.size, -i);
|
|
1144
|
+
data.push([Chalkboard.calc.d2fdx2(func, i), i]);
|
|
1145
|
+
}
|
|
1021
1146
|
}
|
|
1022
1147
|
endShape();
|
|
1023
1148
|
popMatrix();
|
|
1024
1149
|
return data;
|
|
1025
1150
|
},
|
|
1026
|
-
fxdx: function(func,
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1151
|
+
fxdx: function(func, config) {
|
|
1152
|
+
config = config || {};
|
|
1153
|
+
config = {
|
|
1154
|
+
size: config.size || 1,
|
|
1155
|
+
stroke: config.stroke || color(0),
|
|
1156
|
+
domain: config.domain || [-10, 10],
|
|
1157
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1158
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1159
|
+
res: config.res || 25
|
|
1160
|
+
};
|
|
1161
|
+
config.size /= 100;
|
|
1034
1162
|
var data = [];
|
|
1035
1163
|
pushMatrix();
|
|
1036
|
-
translate(origin[0], origin[1]);
|
|
1164
|
+
translate(config.origin[0], config.origin[1]);
|
|
1037
1165
|
noFill();
|
|
1038
|
-
strokeWeight(
|
|
1039
|
-
stroke(
|
|
1166
|
+
strokeWeight(config.strokeWeight);
|
|
1167
|
+
stroke(config.stroke);
|
|
1040
1168
|
beginShape();
|
|
1041
|
-
for(var i = domain[0] /
|
|
1042
|
-
|
|
1043
|
-
|
|
1169
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i += config.res) {
|
|
1170
|
+
if(func.type === "expl") {
|
|
1171
|
+
vertex(i, -Chalkboard.calc.fxdx(func, 0, i * config.size) / config.size);
|
|
1172
|
+
data.push([i, Chalkboard.calc.fxdx(func, 0, i)]);
|
|
1173
|
+
} else if(func.type === "inve") {
|
|
1174
|
+
vertex(Chalkboard.calc.fxdx(func, 0, i * config.size) / config.size, -i);
|
|
1175
|
+
data.push([Chalkboard.calc.fxdx(func, 0, i), i]);
|
|
1176
|
+
}
|
|
1044
1177
|
}
|
|
1045
1178
|
endShape();
|
|
1046
1179
|
popMatrix();
|
|
1047
1180
|
return data;
|
|
1048
1181
|
},
|
|
1049
|
-
convolution: function(func_1, func_2,
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1182
|
+
convolution: function(func_1, func_2, config) {
|
|
1183
|
+
config = config || {};
|
|
1184
|
+
config = {
|
|
1185
|
+
size: config.size || 1,
|
|
1186
|
+
stroke: config.stroke || color(0),
|
|
1187
|
+
domain: config.domain || [-10, 10],
|
|
1188
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1189
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1190
|
+
res: config.res || 25
|
|
1191
|
+
};
|
|
1192
|
+
config.size /= 100;
|
|
1057
1193
|
var data = [];
|
|
1058
1194
|
pushMatrix();
|
|
1059
|
-
translate(origin[0], origin[1]);
|
|
1195
|
+
translate(config.origin[0], config.origin[1]);
|
|
1060
1196
|
noFill();
|
|
1061
|
-
strokeWeight(
|
|
1062
|
-
stroke(
|
|
1197
|
+
strokeWeight(config.strokeWeight);
|
|
1198
|
+
stroke(config.stroke);
|
|
1063
1199
|
beginShape();
|
|
1064
|
-
for(var i = domain[0] /
|
|
1065
|
-
vertex(i, -Chalkboard.calc.convolution(func_1, func_2, i *
|
|
1200
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i += config.res) {
|
|
1201
|
+
vertex(i, -Chalkboard.calc.convolution(func_1, func_2, i * config.size) / config.size);
|
|
1066
1202
|
data.push([i, Chalkboard.calc.convolution(func_1, func_2, i)]);
|
|
1067
1203
|
}
|
|
1068
1204
|
endShape();
|
|
1069
1205
|
popMatrix();
|
|
1070
1206
|
return data;
|
|
1071
1207
|
},
|
|
1072
|
-
correlation: function(func_1, func_2,
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1208
|
+
correlation: function(func_1, func_2, config) {
|
|
1209
|
+
config = config || {};
|
|
1210
|
+
config = {
|
|
1211
|
+
size: config.size || 1,
|
|
1212
|
+
stroke: config.stroke || color(0),
|
|
1213
|
+
domain: config.domain || [-10, 10],
|
|
1214
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1215
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1216
|
+
res: config.res || 25
|
|
1217
|
+
};
|
|
1218
|
+
config.size /= 100;
|
|
1080
1219
|
var data = [];
|
|
1081
1220
|
pushMatrix();
|
|
1082
|
-
translate(origin[0], origin[1]);
|
|
1221
|
+
translate(config.origin[0], config.origin[1]);
|
|
1083
1222
|
noFill();
|
|
1084
|
-
strokeWeight(
|
|
1085
|
-
stroke(
|
|
1223
|
+
strokeWeight(config.strokeWeight);
|
|
1224
|
+
stroke(config.stroke);
|
|
1086
1225
|
beginShape();
|
|
1087
|
-
for(var i = domain[0] /
|
|
1088
|
-
vertex(i, -Chalkboard.calc.correlation(func_1, func_2, i *
|
|
1226
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i += config.res) {
|
|
1227
|
+
vertex(i, -Chalkboard.calc.correlation(func_1, func_2, i * config.size) / config.size);
|
|
1089
1228
|
data.push([i, Chalkboard.calc.correlation(func_1, func_2, i)]);
|
|
1090
1229
|
}
|
|
1091
1230
|
endShape();
|
|
1092
1231
|
popMatrix();
|
|
1093
1232
|
return data;
|
|
1094
1233
|
},
|
|
1095
|
-
autocorrelation: function(func,
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1234
|
+
autocorrelation: function(func, config) {
|
|
1235
|
+
config = config || {};
|
|
1236
|
+
config = {
|
|
1237
|
+
size: config.size || 1,
|
|
1238
|
+
stroke: config.stroke || color(0),
|
|
1239
|
+
domain: config.domain || [-10, 10],
|
|
1240
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1241
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1242
|
+
res: config.res || 25
|
|
1243
|
+
};
|
|
1244
|
+
config.size /= 100;
|
|
1103
1245
|
var data = [];
|
|
1104
1246
|
pushMatrix();
|
|
1105
|
-
translate(origin[0], origin[1]);
|
|
1247
|
+
translate(config.origin[0], config.origin[1]);
|
|
1106
1248
|
noFill();
|
|
1107
|
-
strokeWeight(
|
|
1108
|
-
stroke(
|
|
1249
|
+
strokeWeight(config.strokeWeight);
|
|
1250
|
+
stroke(config.stroke);
|
|
1109
1251
|
beginShape();
|
|
1110
|
-
for(var i = domain[0] /
|
|
1111
|
-
vertex(i, -Chalkboard.calc.autocorrelation(func, i *
|
|
1252
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i += config.res) {
|
|
1253
|
+
vertex(i, -Chalkboard.calc.autocorrelation(func, i * config.size) / config.size);
|
|
1112
1254
|
data.push([i, Chalkboard.calc.autocorrelation(func, i)]);
|
|
1113
1255
|
}
|
|
1114
1256
|
endShape();
|
|
1115
1257
|
popMatrix();
|
|
1116
1258
|
return data;
|
|
1117
1259
|
},
|
|
1118
|
-
Taylor: function(func, n, a,
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1260
|
+
Taylor: function(func, n, a, config) {
|
|
1261
|
+
config = config || {};
|
|
1262
|
+
config = {
|
|
1263
|
+
size: config.size || 1,
|
|
1264
|
+
stroke: config.stroke || color(0),
|
|
1265
|
+
domain: config.domain || [-10, 10],
|
|
1266
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1267
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1268
|
+
res: config.res || 25
|
|
1269
|
+
};
|
|
1270
|
+
config.size /= 100;
|
|
1126
1271
|
var data = [];
|
|
1127
1272
|
pushMatrix();
|
|
1128
|
-
translate(origin[0], origin[1]);
|
|
1273
|
+
translate(config.origin[0], config.origin[1]);
|
|
1129
1274
|
noFill();
|
|
1130
|
-
strokeWeight(
|
|
1131
|
-
stroke(
|
|
1275
|
+
strokeWeight(config.strokeWeight);
|
|
1276
|
+
stroke(config.stroke);
|
|
1132
1277
|
beginShape();
|
|
1133
|
-
for(var i = domain[0] /
|
|
1134
|
-
vertex(i, -Chalkboard.calc.Taylor(func, i *
|
|
1278
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i += config.res) {
|
|
1279
|
+
vertex(i, -Chalkboard.calc.Taylor(func, i * config.size, n, a) / config.size);
|
|
1135
1280
|
data.push([i, Chalkboard.calc.Taylor(func, i, n, a)]);
|
|
1136
1281
|
}
|
|
1137
1282
|
endShape();
|
|
1138
1283
|
popMatrix();
|
|
1139
1284
|
return data;
|
|
1140
1285
|
},
|
|
1141
|
-
Laplace: function(func,
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1286
|
+
Laplace: function(func, config) {
|
|
1287
|
+
config = config || {};
|
|
1288
|
+
config = {
|
|
1289
|
+
size: config.size || 1,
|
|
1290
|
+
stroke: config.stroke || color(0),
|
|
1291
|
+
domain: config.domain || [-10, 10],
|
|
1292
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1293
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1294
|
+
res: config.res || 25
|
|
1295
|
+
};
|
|
1296
|
+
config.size /= 100;
|
|
1149
1297
|
var data = [];
|
|
1150
1298
|
pushMatrix();
|
|
1151
|
-
translate(origin[0], origin[1]);
|
|
1299
|
+
translate(config.origin[0], config.origin[1]);
|
|
1152
1300
|
noFill();
|
|
1153
|
-
strokeWeight(
|
|
1154
|
-
stroke(
|
|
1301
|
+
strokeWeight(config.strokeWeight);
|
|
1302
|
+
stroke(config.stroke);
|
|
1155
1303
|
beginShape();
|
|
1156
|
-
if(domain[0] >= 0) {
|
|
1157
|
-
for(var i = domain[0] /
|
|
1158
|
-
vertex(i, -Chalkboard.calc.Laplace(func, i *
|
|
1304
|
+
if(config.domain[0] >= 0) {
|
|
1305
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i += config.res) {
|
|
1306
|
+
vertex(i, -Chalkboard.calc.Laplace(func, i * config.size) / config.size);
|
|
1159
1307
|
data.push([i, Chalkboard.calc.Laplace(func, i)]);
|
|
1160
1308
|
}
|
|
1161
1309
|
} else {
|
|
1162
|
-
for(var i = 0; i <= domain[1] /
|
|
1163
|
-
vertex(i, -Chalkboard.calc.Laplace(func, i *
|
|
1310
|
+
for(var i = 0; i <= config.domain[1] / config.size; i += config.res) {
|
|
1311
|
+
vertex(i, -Chalkboard.calc.Laplace(func, i * config.size) / config.size);
|
|
1164
1312
|
data.push([i, Chalkboard.calc.Laplace(func, i)]);
|
|
1165
1313
|
}
|
|
1166
1314
|
}
|
|
@@ -1168,23 +1316,26 @@ var Chalkboard = {
|
|
|
1168
1316
|
popMatrix();
|
|
1169
1317
|
return data;
|
|
1170
1318
|
},
|
|
1171
|
-
Fourier: function(func,
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1319
|
+
Fourier: function(func, config) {
|
|
1320
|
+
config = config || {};
|
|
1321
|
+
config = {
|
|
1322
|
+
size: config.size || 1,
|
|
1323
|
+
stroke: config.stroke || color(0),
|
|
1324
|
+
domain: config.domain || [-10, 10],
|
|
1325
|
+
origin: config.origin || [width / 2, height / 2],
|
|
1326
|
+
strokeWeight: config.strokeWeight || 2,
|
|
1327
|
+
res: config.res || 25
|
|
1328
|
+
};
|
|
1329
|
+
config.size /= 100;
|
|
1179
1330
|
var data = [];
|
|
1180
1331
|
pushMatrix();
|
|
1181
|
-
translate(origin[0], origin[1]);
|
|
1332
|
+
translate(config.origin[0], config.origin[1]);
|
|
1182
1333
|
noFill();
|
|
1183
|
-
strokeWeight(
|
|
1184
|
-
stroke(
|
|
1334
|
+
strokeWeight(config.strokeWeight);
|
|
1335
|
+
stroke(config.stroke);
|
|
1185
1336
|
beginShape();
|
|
1186
|
-
for(var i = domain[0] /
|
|
1187
|
-
vertex(i, -Chalkboard.calc.Fourier(func, i *
|
|
1337
|
+
for(var i = config.domain[0] / config.size; i <= config.domain[1] / config.size; i += config.res) {
|
|
1338
|
+
vertex(i, -Chalkboard.calc.Fourier(func, i * config.size) / config.size);
|
|
1188
1339
|
data.push([i, Chalkboard.calc.Fourier(func, i)]);
|
|
1189
1340
|
}
|
|
1190
1341
|
endShape();
|
|
@@ -1516,6 +1667,16 @@ var Chalkboard = {
|
|
|
1516
1667
|
}
|
|
1517
1668
|
return result;
|
|
1518
1669
|
},
|
|
1670
|
+
shuffle: function(arr) {
|
|
1671
|
+
var index, temp, rindex;
|
|
1672
|
+
for(index = arr.length - 1; index > 0; index--) {
|
|
1673
|
+
rindex = Math.floor(Chalkboard.numb.random(0, index + 1));
|
|
1674
|
+
temp = arr[index];
|
|
1675
|
+
arr[index] = arr[rindex];
|
|
1676
|
+
arr[rindex] = temp;
|
|
1677
|
+
}
|
|
1678
|
+
return arr;
|
|
1679
|
+
},
|
|
1519
1680
|
norm: function(arr, type) {
|
|
1520
1681
|
type = type || "L2";
|
|
1521
1682
|
var result = 0;
|
|
@@ -1727,6 +1888,23 @@ var Chalkboard = {
|
|
|
1727
1888
|
}
|
|
1728
1889
|
return result;
|
|
1729
1890
|
},
|
|
1891
|
+
subsets: function(arr) {
|
|
1892
|
+
var result = [[]];
|
|
1893
|
+
arr.sort();
|
|
1894
|
+
for(var i = 0; i < arr.length; i++) {
|
|
1895
|
+
if(i === 0 || arr[i] !== arr[i - 1]) {
|
|
1896
|
+
var curr = arr[i];
|
|
1897
|
+
var subsetsWithCurr = [];
|
|
1898
|
+
for(var j = 0; j < result.length; j++) {
|
|
1899
|
+
var subset = result[j].slice();
|
|
1900
|
+
subset.push(curr);
|
|
1901
|
+
subsetsWithCurr.push(subset);
|
|
1902
|
+
}
|
|
1903
|
+
result = result.concat(subsetsWithCurr);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
return result;
|
|
1907
|
+
},
|
|
1730
1908
|
max: function(arr) {
|
|
1731
1909
|
var max = arr[0];
|
|
1732
1910
|
for(var i = 0; i < arr.length; i++) {
|
|
@@ -1760,7 +1938,7 @@ var Chalkboard = {
|
|
|
1760
1938
|
for(var i = 0; i < arr.length; i++) {
|
|
1761
1939
|
result *= arr[i];
|
|
1762
1940
|
}
|
|
1763
|
-
return Chalkboard.real.
|
|
1941
|
+
return Chalkboard.real.root(Math.abs(result), arr.length);
|
|
1764
1942
|
} else if(type === "harmonic") {
|
|
1765
1943
|
for(var i = 0; i < arr.length; i++) {
|
|
1766
1944
|
result += 1 / arr[i];
|
|
@@ -1925,6 +2103,85 @@ var Chalkboard = {
|
|
|
1925
2103
|
Gaussian: function(height, mean, deviation) {
|
|
1926
2104
|
return Chalkboard.real.function(height.toString() + " * Math.exp(-((x - " + mean.toString() + ") * (x - " + mean.toString() + ")) / (2 * " + deviation.toString() + " * " + deviation.toString() + "))");
|
|
1927
2105
|
},
|
|
2106
|
+
regression: function(data, type, degree) {
|
|
2107
|
+
type = type || "linear";
|
|
2108
|
+
degree = degree || 2;
|
|
2109
|
+
if(type === "linear") {
|
|
2110
|
+
var x = 0, y = 0;
|
|
2111
|
+
var xx = 0, xy = 0;
|
|
2112
|
+
for(var i = 0; i < data.length; i++) {
|
|
2113
|
+
x += data[i][0];
|
|
2114
|
+
y += data[i][1];
|
|
2115
|
+
xx += data[i][0] * data[i][0];
|
|
2116
|
+
xy += data[i][0] * data[i][1];
|
|
2117
|
+
}
|
|
2118
|
+
var a = (data.length * xy - x * y) / (data.length * xx - x * x),
|
|
2119
|
+
b = (y / data.length) - (a * x) / data.length;
|
|
2120
|
+
return Chalkboard.real.function(a + " * x + " + b);
|
|
2121
|
+
} else if(type === "polynomial") {
|
|
2122
|
+
var A = Chalkboard.matr.new();
|
|
2123
|
+
for(var i = 0; i < data.length; i++) {
|
|
2124
|
+
A.push([]);
|
|
2125
|
+
for(var j = 0; j <= degree; j++) {
|
|
2126
|
+
A[i].push(Chalkboard.real.pow(data[i][0], j));
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
var AT = Chalkboard.matr.transpose(A);
|
|
2130
|
+
var B = Chalkboard.matr.new();
|
|
2131
|
+
for(var i = 0; i < data.length; i++) {
|
|
2132
|
+
B.push([data[i][1]]);
|
|
2133
|
+
}
|
|
2134
|
+
var ATA = Chalkboard.matr.mul(AT, A);
|
|
2135
|
+
var ATAI = Chalkboard.matr.invert(ATA);
|
|
2136
|
+
var x = Chalkboard.matr.mul(Chalkboard.matr.mul(ATAI, AT), B);
|
|
2137
|
+
var coeff = [];
|
|
2138
|
+
for(var i = 0; i < x.length; i++) {
|
|
2139
|
+
coeff.push(x[i][0]);
|
|
2140
|
+
}
|
|
2141
|
+
var f = coeff[0].toString() + " + " + coeff[1].toString() + " * x";
|
|
2142
|
+
for(var i = 2; i <= degree; i++) {
|
|
2143
|
+
f += " + " + coeff[i].toString() + " * Math.pow(x, " + i + ")";
|
|
2144
|
+
}
|
|
2145
|
+
return Chalkboard.real.function(f);
|
|
2146
|
+
} else if(type === "power") {
|
|
2147
|
+
var arr = [0, 0, 0, 0];
|
|
2148
|
+
for(var i = 0; i < data.length; i++) {
|
|
2149
|
+
arr[0] += Chalkboard.real.ln(data[i][0]);
|
|
2150
|
+
arr[1] += data[i][1] * Chalkboard.real.ln(data[i][0]);
|
|
2151
|
+
arr[2] += data[i][1];
|
|
2152
|
+
arr[3] += Chalkboard.real.ln(data[i][0]) * Chalkboard.real.ln(data[i][0]);
|
|
2153
|
+
}
|
|
2154
|
+
var a = Chalkboard.E((arr[2] - ((data.length * arr[1] - arr[2] * arr[0]) / (data.length * arr[3] - arr[0] * arr[0])) * arr[0]) / data.length),
|
|
2155
|
+
b = (data.length * arr[1] - arr[2] * arr[0]) / (data.length * arr[3] - arr[0] * arr[0]);
|
|
2156
|
+
return Chalkboard.real.function(a + " * Math.pow(x, " + b + ")");
|
|
2157
|
+
} else if(type === "exponential") {
|
|
2158
|
+
var arr = [0, 0, 0, 0, 0, 0];
|
|
2159
|
+
for(var i = 0; i < data.length; i++) {
|
|
2160
|
+
arr[0] += data[i][0];
|
|
2161
|
+
arr[1] += data[i][1];
|
|
2162
|
+
arr[2] += data[i][0] * data[i][0] * data[i][1];
|
|
2163
|
+
arr[3] += data[i][1] * Chalkboard.real.ln(data[i][1]);
|
|
2164
|
+
arr[4] += data[i][0] & data[i][1] * Chalkboard.real.ln(data[i][1]);
|
|
2165
|
+
arr[5] += data[i][0] * data[i][1];
|
|
2166
|
+
}
|
|
2167
|
+
var a = Chalkboard.E((arr[2] * arr[3] - arr[5] * arr[4]) / (arr[1] * arr[2] - arr[5] * arr[5])),
|
|
2168
|
+
b = (arr[1] * arr[4] - arr[5] * arr[3]) / (arr[1] * arr[2] - arr[5] * arr[5]);
|
|
2169
|
+
return Chalkboard.real.function(a + "* Math.exp(" + b + " * x)");
|
|
2170
|
+
} else if(type === "logarithmic") {
|
|
2171
|
+
var arr = [0, 0, 0, 0];
|
|
2172
|
+
for(var i = 0; i < data.length; i++) {
|
|
2173
|
+
arr[0] += Chalkboard.real.ln(data[i][0]);
|
|
2174
|
+
arr[1] += data[i][1] * Chalkboard.real.ln(data[i][0]);
|
|
2175
|
+
arr[2] += data[i][1];
|
|
2176
|
+
arr[3] += Chalkboard.real.ln(data[i][0]) * Chalkboard.real.ln(data[i][0]);
|
|
2177
|
+
}
|
|
2178
|
+
var a = (arr[2] - ((data.length * arr[1] - arr[2] * arr[0]) / (data.length * arr[3] - arr[0] * arr[0])) * arr[0]) / data.length,
|
|
2179
|
+
b = (data.length * arr[1] - arr[2] * arr[0]) / (data.length * arr[3] - arr[0] * arr[0]);
|
|
2180
|
+
return Chalkboard.real.function(a + " + " + b + " * Math.log(x)");
|
|
2181
|
+
} else {
|
|
2182
|
+
return "TypeError: Parameter \"type\" must be either \"linear\", \"polynomial\", \"power\", \"exponential\", or \"logarithmic\".";
|
|
2183
|
+
}
|
|
2184
|
+
},
|
|
1928
2185
|
toVector: function(arr, type) {
|
|
1929
2186
|
if(type === "vec2") {
|
|
1930
2187
|
return Chalkboard.vec2.new(arr[0], arr[1]);
|
|
@@ -2403,7 +2660,8 @@ var Chalkboard = {
|
|
|
2403
2660
|
},
|
|
2404
2661
|
matr: {
|
|
2405
2662
|
new: function(matrix) {
|
|
2406
|
-
|
|
2663
|
+
matrix = Array.from(arguments);
|
|
2664
|
+
return matrix;
|
|
2407
2665
|
},
|
|
2408
2666
|
rows: function(matr) {
|
|
2409
2667
|
return matr.length;
|
|
@@ -2411,12 +2669,54 @@ var Chalkboard = {
|
|
|
2411
2669
|
cols: function(matr) {
|
|
2412
2670
|
return matr[0].length;
|
|
2413
2671
|
},
|
|
2414
|
-
|
|
2415
|
-
|
|
2672
|
+
push: function(matr, type, rowORcol, elements) {
|
|
2673
|
+
rowORcol -= 1;
|
|
2674
|
+
if(type === "row") {
|
|
2675
|
+
matr.splice(rowORcol, 0, elements);
|
|
2676
|
+
return matr;
|
|
2677
|
+
} else if(type === "col") {
|
|
2678
|
+
for(var i = 0; i < Chalkboard.matr.rows(matr); i++) {
|
|
2679
|
+
matr[i].splice(rowORcol, 0, elements[i]);
|
|
2680
|
+
}
|
|
2681
|
+
return matr;
|
|
2682
|
+
} else {
|
|
2683
|
+
return "TypeError: Parameter \"type\" must be either \"row\" or \"col\".";
|
|
2684
|
+
}
|
|
2685
|
+
},
|
|
2686
|
+
pull: function(matr, type, rowORcol) {
|
|
2687
|
+
rowORcol -= 1;
|
|
2688
|
+
if(type === "row") {
|
|
2689
|
+
matr.splice(rowORcol, 1);
|
|
2690
|
+
return matr;
|
|
2691
|
+
} else if(type === "col") {
|
|
2692
|
+
for(var i = 0; i < Chalkboard.matr.rows(matr); i++) {
|
|
2693
|
+
matr[i].splice(rowORcol, 1);
|
|
2694
|
+
}
|
|
2695
|
+
return matr;
|
|
2696
|
+
} else {
|
|
2697
|
+
return "TypeError: Parameter \"type\" must be either \"row\" or \"col\".";
|
|
2698
|
+
}
|
|
2699
|
+
},
|
|
2700
|
+
fill: function(element, rows, cols) {
|
|
2701
|
+
if(Number.isInteger(rows) && Number.isInteger(cols) && rows > 0 && cols > 0) {
|
|
2702
|
+
var result = Chalkboard.matr.new();
|
|
2703
|
+
for(var i = 0; i < rows; i++) {
|
|
2704
|
+
result.push([]);
|
|
2705
|
+
for(var j = 0; j < cols; j++) {
|
|
2706
|
+
result[i].push(element);
|
|
2707
|
+
}
|
|
2708
|
+
}
|
|
2709
|
+
return result;
|
|
2710
|
+
} else {
|
|
2711
|
+
return undefined;
|
|
2712
|
+
}
|
|
2713
|
+
},
|
|
2714
|
+
empty: function(rows, cols) {
|
|
2715
|
+
if(Number.isInteger(rows) && Number.isInteger(cols) && rows > 0 && cols > 0) {
|
|
2416
2716
|
var result = Chalkboard.matr.new();
|
|
2417
|
-
for(var i = 0; i <
|
|
2717
|
+
for(var i = 0; i < rows; i++) {
|
|
2418
2718
|
result.push([]);
|
|
2419
|
-
for(var j = 0; j <
|
|
2719
|
+
for(var j = 0; j < cols; j++) {
|
|
2420
2720
|
result[i].push(null);
|
|
2421
2721
|
}
|
|
2422
2722
|
}
|
|
@@ -2425,11 +2725,11 @@ var Chalkboard = {
|
|
|
2425
2725
|
return undefined;
|
|
2426
2726
|
}
|
|
2427
2727
|
},
|
|
2428
|
-
identity: function(
|
|
2429
|
-
if(Number.isInteger(
|
|
2728
|
+
identity: function(size) {
|
|
2729
|
+
if(Number.isInteger(size) && size > 0) {
|
|
2430
2730
|
var result = Chalkboard.matr.new();
|
|
2431
|
-
for(var i = 0; i <
|
|
2432
|
-
result.push(Array(
|
|
2731
|
+
for(var i = 0; i < size; i++) {
|
|
2732
|
+
result.push(Array(size).fill(0));
|
|
2433
2733
|
result[i][i] = 1;
|
|
2434
2734
|
}
|
|
2435
2735
|
return result;
|
|
@@ -2437,14 +2737,12 @@ var Chalkboard = {
|
|
|
2437
2737
|
return undefined;
|
|
2438
2738
|
}
|
|
2439
2739
|
},
|
|
2440
|
-
random: function(
|
|
2441
|
-
if(Number.isInteger(
|
|
2442
|
-
inf = inf || 0;
|
|
2443
|
-
sup = sup || 1;
|
|
2740
|
+
random: function(rows, cols, inf, sup) {
|
|
2741
|
+
if(Number.isInteger(rows) && Number.isInteger(cols) && rows > 0 && cols > 0) {
|
|
2444
2742
|
var result = Chalkboard.matr.new();
|
|
2445
|
-
for(var i = 0; i <
|
|
2743
|
+
for(var i = 0; i < rows; i++) {
|
|
2446
2744
|
result.push([]);
|
|
2447
|
-
for(var j = 0; j <
|
|
2745
|
+
for(var j = 0; j < cols; j++) {
|
|
2448
2746
|
result[i].push(Chalkboard.numb.random(inf, sup));
|
|
2449
2747
|
}
|
|
2450
2748
|
}
|
|
@@ -2454,7 +2752,7 @@ var Chalkboard = {
|
|
|
2454
2752
|
}
|
|
2455
2753
|
},
|
|
2456
2754
|
cofactor: function(matr, row, col) {
|
|
2457
|
-
return matr.slice(0, row - 1).concat(matr.slice(row)).map(function
|
|
2755
|
+
return matr.slice(0, row - 1).concat(matr.slice(row)).map(function(row) {
|
|
2458
2756
|
return row.slice(0, col - 1).concat(row.slice(col));
|
|
2459
2757
|
});
|
|
2460
2758
|
},
|
|
@@ -2491,15 +2789,15 @@ var Chalkboard = {
|
|
|
2491
2789
|
}
|
|
2492
2790
|
},
|
|
2493
2791
|
rank: function(matr) {
|
|
2494
|
-
return Chalkboard.matr.reduce(matr).filter(function
|
|
2495
|
-
return row.some(function
|
|
2792
|
+
return Chalkboard.matr.reduce(matr).filter(function(row) {
|
|
2793
|
+
return row.some(function(element) {
|
|
2496
2794
|
return element !== 0;
|
|
2497
2795
|
});
|
|
2498
2796
|
}).length;
|
|
2499
2797
|
},
|
|
2500
2798
|
rowspace: function(matr) {
|
|
2501
|
-
return Chalkboard.matr.reduce(matr).filter(function
|
|
2502
|
-
return row.some(function
|
|
2799
|
+
return Chalkboard.matr.reduce(matr).filter(function(row) {
|
|
2800
|
+
return row.some(function(element) {
|
|
2503
2801
|
return element !== 0;
|
|
2504
2802
|
});
|
|
2505
2803
|
});
|
|
@@ -2508,15 +2806,15 @@ var Chalkboard = {
|
|
|
2508
2806
|
return Chalkboard.matr.transpose(Chalkboard.matr.rowspace(Chalkboard.matr.transpose(matr)));
|
|
2509
2807
|
},
|
|
2510
2808
|
nullspace: function(matr) {
|
|
2511
|
-
var augmented = matr.map(function
|
|
2809
|
+
var augmented = matr.map(function(row) {
|
|
2512
2810
|
return row.slice().concat(Array(Chalkboard.matr.rows(matr)).fill(0));
|
|
2513
2811
|
});
|
|
2514
2812
|
var reduced = Chalkboard.matr.reduce(augmented);
|
|
2515
|
-
return reduced.filter(function
|
|
2516
|
-
return row.slice(0, Chalkboard.matr.rows(matr)).every(function
|
|
2813
|
+
return reduced.filter(function(row) {
|
|
2814
|
+
return row.slice(0, Chalkboard.matr.rows(matr)).every(function(element) {
|
|
2517
2815
|
return element === 0;
|
|
2518
2816
|
});
|
|
2519
|
-
}).map(function
|
|
2817
|
+
}).map(function(row) {
|
|
2520
2818
|
return row.slice(Chalkboard.matr.rows(matr));
|
|
2521
2819
|
});
|
|
2522
2820
|
},
|
|
@@ -2572,6 +2870,78 @@ var Chalkboard = {
|
|
|
2572
2870
|
return undefined;
|
|
2573
2871
|
}
|
|
2574
2872
|
},
|
|
2873
|
+
LUdecomp: function(matr) {
|
|
2874
|
+
if(Chalkboard.matr.rows(matr) === Chalkboard.matr.cols(matr)) {
|
|
2875
|
+
var L = Chalkboard.matr.identity(Chalkboard.matr.rows(matr)),
|
|
2876
|
+
U = Chalkboard.matr.zero(Chalkboard.matr.empty(Chalkboard.matr.rows(matr)));
|
|
2877
|
+
for(var j = 0; j < Chalkboard.matr.cols(matr); j++) {
|
|
2878
|
+
for(var i = 0; i <= j; i++) {
|
|
2879
|
+
var sum = 0;
|
|
2880
|
+
for(var k = 0; k < i; k++) {
|
|
2881
|
+
sum += L[i][k] * U[k][j];
|
|
2882
|
+
}
|
|
2883
|
+
U[i][j] = matr[i][j] - sum;
|
|
2884
|
+
}
|
|
2885
|
+
for(var i = j + 1; i < Chalkboard.matr.rows(matr); i++) {
|
|
2886
|
+
var sum = 0;
|
|
2887
|
+
for(var k = 0; k < j; k++) {
|
|
2888
|
+
sum += L[i][k] * U[k][j];
|
|
2889
|
+
}
|
|
2890
|
+
L[i][j] = (matr[i][j] - sum) / U[j][j];
|
|
2891
|
+
}
|
|
2892
|
+
}
|
|
2893
|
+
return {L: L, U: U};
|
|
2894
|
+
} else {
|
|
2895
|
+
return undefined;
|
|
2896
|
+
}
|
|
2897
|
+
},
|
|
2898
|
+
QRdecomp: function(matr) {
|
|
2899
|
+
if(Chalkboard.matr.rows(matr) === Chalkboard.matr.cols(matr)) {
|
|
2900
|
+
var Q = Chalkboard.matr.zero(Chalkboard.matr.empty(Chalkboard.matr.rows(matr))),
|
|
2901
|
+
R = Chalkboard.matr.zero(Chalkboard.matr.empty(Chalkboard.matr.rows(matr)));
|
|
2902
|
+
var dot = function(v1, v2) {
|
|
2903
|
+
var result = 0;
|
|
2904
|
+
for(var i = 0; i < v1.length; i++) {
|
|
2905
|
+
result += v1[i] * v2[i];
|
|
2906
|
+
}
|
|
2907
|
+
return result;
|
|
2908
|
+
}
|
|
2909
|
+
var mag = function(v) {
|
|
2910
|
+
var result = 0;
|
|
2911
|
+
for(var i = 0; i < v.length; i++) {
|
|
2912
|
+
result += v[i] * v[i];
|
|
2913
|
+
}
|
|
2914
|
+
return Math.sqrt(result);
|
|
2915
|
+
}
|
|
2916
|
+
for(var j = 0; j < Chalkboard.matr.rows(matr); j++) {
|
|
2917
|
+
var v = matr.map(function(row) {
|
|
2918
|
+
return row[j];
|
|
2919
|
+
});
|
|
2920
|
+
for(var i = 0; i < j; i++) {
|
|
2921
|
+
var q = Q.map(function(row) {
|
|
2922
|
+
return row[i];
|
|
2923
|
+
});
|
|
2924
|
+
var coeff = dot(v, q) / (mag(q) * mag(q));
|
|
2925
|
+
v = v.map(function(e, index) {
|
|
2926
|
+
return e - coeff * q[index];
|
|
2927
|
+
});
|
|
2928
|
+
}
|
|
2929
|
+
for(var i = 0; i < Chalkboard.matr.rows(matr); i++) {
|
|
2930
|
+
Q[i][j] = v[i] / mag(v);
|
|
2931
|
+
}
|
|
2932
|
+
for(var i = 0; i < Chalkboard.matr.rows(matr); i++) {
|
|
2933
|
+
R[j][i] = i < j ? 0 : matr.map(function(row) {
|
|
2934
|
+
return row[i];
|
|
2935
|
+
}).reduce(function(a, v, index) {
|
|
2936
|
+
return a + v * Q[index][j];
|
|
2937
|
+
}, 0);
|
|
2938
|
+
}
|
|
2939
|
+
}
|
|
2940
|
+
return {Q: Q, R: R};
|
|
2941
|
+
} else {
|
|
2942
|
+
return undefined;
|
|
2943
|
+
}
|
|
2944
|
+
},
|
|
2575
2945
|
zero: function(matr) {
|
|
2576
2946
|
var result = Chalkboard.matr.new();
|
|
2577
2947
|
for(var i = 0; i < Chalkboard.matr.rows(matr); i++) {
|
|
@@ -2888,6 +3258,9 @@ var Chalkboard = {
|
|
|
2888
3258
|
if(func.type === "expl") {
|
|
2889
3259
|
var f = Chalkboard.real.parse("x => " + func.definition);
|
|
2890
3260
|
return (f(val + h) - f(val)) / h;
|
|
3261
|
+
} else if(func.type === "inve") {
|
|
3262
|
+
var f = Chalkboard.real.parse("y => " + func.definition);
|
|
3263
|
+
return (f(val + h) - f(val)) / h;
|
|
2891
3264
|
} else if(func.type === "pola") {
|
|
2892
3265
|
var r = Chalkboard.real.parse("O => " + func.definition);
|
|
2893
3266
|
return (r(val + h) - r(val)) / h;
|
|
@@ -2903,7 +3276,7 @@ var Chalkboard = {
|
|
|
2903
3276
|
return Chalkboard.vec3.new((x(val + h) - x(val)) / h, (y(val + h) - y(val)) / h, (z(val + h) - z(val)) / h);
|
|
2904
3277
|
}
|
|
2905
3278
|
} else {
|
|
2906
|
-
return "TypeError: Parameter \"func\" must be of type \"expl\", \"pola\", or \"curv\".";
|
|
3279
|
+
return "TypeError: Parameter \"func\" must be of type \"expl\", \"inve\", \"pola\", or \"curv\".";
|
|
2907
3280
|
}
|
|
2908
3281
|
},
|
|
2909
3282
|
d2fdx2: function(func, val) {
|
|
@@ -2911,6 +3284,9 @@ var Chalkboard = {
|
|
|
2911
3284
|
if(func.type === "expl") {
|
|
2912
3285
|
var f = Chalkboard.real.parse("x => " + func.definition);
|
|
2913
3286
|
return (f(val + h) - 2 * f(val) + f(val - h)) / (h * h);
|
|
3287
|
+
} else if(func.type === "inve") {
|
|
3288
|
+
var f = Chalkboard.real.parse("y => " + func.definition);
|
|
3289
|
+
return (f(val + h) - 2 * f(val) + f(val - h)) / (h * h);
|
|
2914
3290
|
} else if(func.type === "pola") {
|
|
2915
3291
|
var r = Chalkboard.real.parse("O => " + func.definition);
|
|
2916
3292
|
return (r(val + h) - 2 * r(val) + r(val - h)) / (h * h);
|
|
@@ -2926,7 +3302,7 @@ var Chalkboard = {
|
|
|
2926
3302
|
return Chalkboard.vec3.new((x(val + h) - 2 * x(val) + x(val - h)) / (h * h), (y(val + h) - 2 * y(val) + y(val - h)) / (h * h), (z(val + h) - 2 * z(val) + z(val - h)) / (h * h));
|
|
2927
3303
|
}
|
|
2928
3304
|
} else {
|
|
2929
|
-
return "TypeError: Parameter \"func\" must be of type \"expl\", \"pola\", or \"curv\".";
|
|
3305
|
+
return "TypeError: Parameter \"func\" must be of type \"expl\", \"inve\", \"pola\", or \"curv\".";
|
|
2930
3306
|
}
|
|
2931
3307
|
},
|
|
2932
3308
|
tangent: function(func, val) {
|
|
@@ -3181,7 +3557,7 @@ var Chalkboard = {
|
|
|
3181
3557
|
return "TypeError: Parameter \"func\" must be of type \"comp\".";
|
|
3182
3558
|
}
|
|
3183
3559
|
},
|
|
3184
|
-
|
|
3560
|
+
d2fdz2: function(func, comp) {
|
|
3185
3561
|
var h = 0.00001;
|
|
3186
3562
|
if(func.type === "comp") {
|
|
3187
3563
|
var u = Chalkboard.comp.parse("(a, b) => " + func.definition[0]),
|
|
@@ -3196,10 +3572,12 @@ var Chalkboard = {
|
|
|
3196
3572
|
}
|
|
3197
3573
|
},
|
|
3198
3574
|
fxdx: function(func, a, b) {
|
|
3199
|
-
if(func.type === "expl" || func.type === "pola") {
|
|
3575
|
+
if(func.type === "expl" || func.type === "inve" || func.type === "pola") {
|
|
3200
3576
|
var f;
|
|
3201
3577
|
if(func.type === "expl") {
|
|
3202
3578
|
f = Chalkboard.real.parse("x => " + func.definition);
|
|
3579
|
+
} else if(func.type === "inve") {
|
|
3580
|
+
f = Chalkboard.real.parse("y => " + func.definition);
|
|
3203
3581
|
} else if(func.type === "pola") {
|
|
3204
3582
|
f = Chalkboard.real.parse("O => " + "((" + func.definition + ") * (" + func.definition + ")) / 2");
|
|
3205
3583
|
}
|
|
@@ -3237,7 +3615,7 @@ var Chalkboard = {
|
|
|
3237
3615
|
return Chalkboard.vec3.new((xt * dt) / 3, (yt * dt) / 3, (zt * dt) / 3);
|
|
3238
3616
|
}
|
|
3239
3617
|
} else {
|
|
3240
|
-
return "TypeError: Parameter \"func\" must be of type \"expl\", \"pola\", or \"curv\".";
|
|
3618
|
+
return "TypeError: Parameter \"func\" must be of type \"expl\", \"inve\", \"pola\", or \"curv\".";
|
|
3241
3619
|
}
|
|
3242
3620
|
},
|
|
3243
3621
|
fxydxdy: function(func, a, b, c, d) {
|