-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
193 lines (172 loc) · 5.31 KB
/
server.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
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
const express = require('express');
const multer = require('multer');
const app = express();
var rref = require('rref');
const { simplify, parse } = require('mathjs')
const STATUS_OK = 200;
const CLIENT_ERROR_CODE = 400;
const SERVER_ERROR_CODE = 500;
const CLIENT_ERROR_MESSAGE = "Missing one or more of the required params.";
const SERVER_ERROR_MESSAGE = "An error occurred on the server. Try again later.";
const MIN_POINTS = 3;
app.use(multer().none());
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.get('/', async (req, res) => {
res.redirect('/index.html');
});
app.get('/cubic', async (req, res) => {
let points = Object.values(req.query);
if(!points) {
res.status(CLIENT_ERROR_CODE).send(CLIENT_ERROR_MESSAGE);
} else if(points.length < (MIN_POINTS*2)) {
res.type("text");
res.status(CLIENT_ERROR_CODE).send("Invalid number of points. Must provide 3 or more points.");
} else {
xPoints = [];
yPoints = [];
unfilteredXPoints = [];
unfilteredYPoints = [];
for(let i = 0; i < points.length; i++) {
let num = eval(points[i]);
let uNum = points[i];
if(i % 2 === 0) {
xPoints.push(num);
unfilteredXPoints.push(uNum);
} else {
yPoints.push(num);
unfilteredYPoints.push(uNum);
}
}
if(containsDuplicate(xPoints)) {
res.type("text");
res.status(CLIENT_ERROR_CODE).send("Invalid coordinates, x-coordinates cannot be repeated. Leads to infinite slope.");
}
let result = calculateCubic(xPoints, yPoints);
res.json(result);
}
});
/////////////////////////////////////////////////////////////
function calculateCubic(xPoints, yPoints) {
// sorting points by x-coordinates
let points = [];
for(let i = 0; i < xPoints.length; i++) {
points.push({
x: xPoints[i],
y: yPoints[i]
});
}
points.sort(function(a, b) {
return ((a.x < b.x) ? -1 : ((a.x == b.x) ? 0 : 1));
});
for (var i = 0; i < points.length; i++) {
xPoints[i] = points[i].x;
yPoints[i] = points[i].y;
}
let hVals = [];
for(let i = 0; i < xPoints.length-1; i++) {
hVals.push(xPoints[i+1] - xPoints[i]);
}
let rows = []; // a coefficients
let matrix = [];
for(let i = 0; i < xPoints.length-2; i++) {
let row = [];
for(let x = 0; x < i; x++) {
row.push(0);
}
let a0 = hVals[i] / 6;
let a1 = (hVals[i] + hVals[i+1]) / 3;
let a2 = hVals[i+1] / 6;
let y = ((yPoints[i+2] - yPoints[i+1]) / hVals[i+1]) - ((yPoints[i+1] - yPoints[i]) / hVals[i]);
if(i == 0) {
row.push(0);
} else {
row.push(a0);
}
row.push(a1);
if(i == xPoints.length-3) {
row.push((0));
} else {
row.push(a2);
}
for(let j = 0; j < (xPoints.length-3)-i; j++) {
row.push(0);
}
row.push(y);
rows.push(row);
}
matrix = JSON.parse(JSON.stringify(rows));
let rrefResult = rref(rows);
let aVals = [];
aVals.push(0);
for(const row of rrefResult) {
aVals.push(row[row.length-1]);
}
aVals.push(0);
let bVals = getBVals(aVals, hVals, yPoints);
let cVals = getCVals(aVals, hVals, yPoints);
let equations = getEQs(aVals, bVals, cVals, hVals, xPoints);
let simplifiedEQ = [];
for(const eq of equations) {
simplifiedEQ.push(simplify(parse(eq)).toString());
}
let domain = getDomain(xPoints);
let result = {
"equations": equations,
"simplified-equations": simplifiedEQ,
"domains": domain,
"matrix-before": matrix,
"matrix-after": rrefResult,
"x": xPoints,
"y": yPoints,
"a": aVals,
"b": bVals,
"c": cVals,
"h": hVals
};
return result;
}
function getBVals(aVals, hVals, yPoints,) {
let bVals = [];
for(let i = 0; i < hVals.length; i++) {
bVals.push((yPoints[i] / hVals[i]) - ((aVals[i] * hVals[i]) / 6));
}
return bVals;
}
function getCVals(aVals, hVals, yPoints,) {
let cVals = [];
for(let i = 0; i < hVals.length; i++) {
cVals.push((yPoints[i+1] / hVals[i]) - ((aVals[i+1] * hVals[i]) / 6));
}
return cVals;
}
function getEQs(aVals, bVals, cVals, hVals, xPoints) {
let equations = [];
for(let i = 0; i < xPoints.length-1; i++) {
let eq = ""
+ aVals[i] + "*(((" + xPoints[i+1] + "-x)^3) / (6*" + hVals[i] + "))+"
+ aVals[i+1] + "*(((x-" + xPoints[i] + ")^3) / (6*" + hVals[i] + "))+"
+ bVals[i] + "*(" + xPoints[i+1] + "-x)+"
+ cVals[i] + "*(x-" + xPoints[i] + ")";
equations.push(eq);
//simplify(parse(eq)).toString()
}
return equations;
}
function getDomain(xPoints) {
let domain = [];
for(let i = 0; i < xPoints.length-1; i++) {
let result = {
min: xPoints[i],
max: xPoints[i+1]
};
domain.push(result);
}
return domain;
}
function containsDuplicate(arr){
return new Set(arr).size !== arr.length
}
app.use(express.static("public"));
const PORT = process.env.PORT || 8080;
app.listen(PORT);