forked from lengstrom/Polynomial-Problem-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathlib.js
executable file
·90 lines (83 loc) · 2.12 KB
/
mathlib.js
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
function polynomial(poly) {
this.poly = poly;
this.degree = this.poly.length - 1;
// function getstring(){
// degree.
// }
}
function multpoly(poly1,poly2) {
var newpoly = [];
for (i=0;i<=poly1.degree;i++) {
for (n=0;n<=poly2.degree;n++) {
if (newpoly[i+n] != undefined) {
newpoly[i+n] = newpoly[i+n] + poly1.poly[i]*poly2.poly[n]; //>newpoly[i+n] dis guy
}
else {
newpoly[i+n] = poly1.poly[i]*poly2.poly[n]; //>newpoly[i+n] dis guy
}
}
}
return new polynomial(newpoly);
}
function addpoly(poly1,poly2) {
var newpoly = poly2.poly;
for (i=0;i<=poly1.degree;i++) {
if(newpoly[i] != undefined) {
newpoly[i] = newpoly[i] + poly1.poly[i];
}
else {
newpoly[i] = poly1.poly[i];
}
}
return new polynomial(newpoly);
}
function subpoly(poly1,poly2) {
var newpoly = poly1.poly;
for (i=0;i<=poly2.degree;i++) {
if(newpoly[i] != undefined) {
newpoly[i] = newpoly[i] - poly2.poly[i];
}
else {
newpoly[i] = poly2.poly[i]*-1;
}
}
return new polynomial(newpoly);
}
function genimags(A,B) {
self.imag = new polynomial([B*B]);
self.foo = new polynomial([1,-1*A]);
self.bar = new multpoly(self.foo,self.foo);
self.newpoly = new addpoly(self.bar,self.imag);
return self.newpoly;
}
var bar = new polynomial([1,2]);
var foo = new polynomial([1,3]);
console.log(addpoly(bar,foo).poly);
console.log(multpoly(bar,foo).poly);
console.log(genimags(1,2).poly);
function generatepolynomial(degree) {
var poley = new polynomial([1]);
var answer = '';
for (i=0;i<=degree) {
isneg = 0;
first = Math.floor(Math.random()*10);
zeroth = Math.floor(Math.random()*10);
tempoly = new polynomial([first,zeroth]);
if (Math.floor(math.random()*11)>=5) {
zeroth = -1*zeroth;
isneg = 1;
}
poley[1] = first;
poley[0] = zeroth;
if (isneg == 1) {
answer = answer + '(' + poley[1] + 'x ' + poley[2] + ')';
}
else {
answer = answer + '(' + poley[1] + 'x + ' + poley[2] + ')'; //umwutlol
}
answer = new multpoly([answer,tempoly]);
console.log(answer.poly);
}
console.log(answer.poley)
}
generatepolynomial(4);