Skip to content

Commit

Permalink
Code cleanup and improvement
Browse files Browse the repository at this point in the history
Added the option to do quadratic analysis of two datapoints
  • Loading branch information
JaapvanEkris authored Jan 9, 2024
1 parent 85a716c commit e0aabf4
Showing 1 changed file with 56 additions and 32 deletions.
88 changes: 56 additions & 32 deletions app/engine/utils/FullTSQuadraticSeries.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,60 +36,74 @@ function createTSQuadraticSeries (maxSeriesLength = 0) {
let _C = 0

function push (x, y) {

Check failure on line 38 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Block must not be padded by blank lines

Check failure on line 38 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Block must not be padded by blank lines
const linearResidu = createTSLinearSeries(maxSeriesLength)

Check failure on line 39 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected tab character

Check failure on line 39 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Trailing spaces not allowed

Check failure on line 39 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Mixed spaces and tabs

Check failure on line 39 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected tab character

Check failure on line 39 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Trailing spaces not allowed

Check failure on line 39 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Mixed spaces and tabs

Check failure on line 40 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

More than 1 blank line not allowed

Check failure on line 40 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

More than 1 blank line not allowed
// Invariant: A contains all a's (as in the general formula y = a * x^2 + b * x + c)
// Where the a's are labeled in the Binary Search Tree with their xi when they BEGIN in the point (xi, yi)
// Where the a's are labeled in the Binary Search Tree with their Xi when they BEGIN in the point (Xi, Yi)

if (maxSeriesLength > 0 && X.length() >= maxSeriesLength) {
// The maximum of the array has been reached, so when pushing the x,y the array gets shifted,
// thus we have to remove the a's belonging to the current position X0 as well before this value is trashed
// The maximum of the array has been reached, so when pushing the new datapoint (x,y), the array will get shifted,
// thus we have to remove all the A's that start with the old position X0 BEFORE this value gets thrown away
A.remove(X.get(0))
}

X.push(x)
Y.push(y)

// Calculate the coefficient a for the new interval by adding the newly added datapoint
if (X.length() > 2) {
// There are at least two points in the X and Y arrays, so let's add the new datapoint
let i = 0
let j = 0
while (i < X.length() - 2) {
j = i + 1
while (j < X.length() - 1) {
A.push(X.get(i), calculateA(i, j, X.length() - 1))
j++
switch (true) {
case (X.length() > 2):
// There are now at least three datapoints in the X and Y arrays, so let's calculate the A portion belonging for the new datapoint via Quadratic Theil-Sen regression
// First we calculate the A for the formula
let i = 0

Check failure on line 58 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected lexical declaration in case block

Check failure on line 58 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected lexical declaration in case block
let j = 0

Check failure on line 59 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected lexical declaration in case block

Check failure on line 59 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected lexical declaration in case block
while (i < X.length() - 2) {
j = i + 1
while (j < X.length() - 1) {
A.push(X.get(i), calculateA(i, j, X.length() - 1))
j++
}
i++
}
i++
}
_A = A.median()

// Calculate the remaining two coefficients for this new interval
i = 0
linearResidu.reset()
while (i < X.length() - 1) {
linearResidu.push(X.get(i), Y.get(i) - (_A * Math.pow(X.get(i), 2)))
i++
}
_B = linearResidu.coefficientA()
_C = linearResidu.coefficientB()
} else {
_A = 0
_B = 0
_C = 0

Check failure on line 68 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected tab character

Check failure on line 68 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Trailing spaces not allowed

Check failure on line 68 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Mixed spaces and tabs

Check failure on line 68 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected tab character

Check failure on line 68 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Trailing spaces not allowed

Check failure on line 68 in app/engine/utils/FullTSQuadraticSeries.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Mixed spaces and tabs

_A = A.median()

// Next, we calculate the B and C via Linear regression over the residu
i = 0
const linearResidu = createTSLinearSeries(maxSeriesLength)
while (i < X.length() - 1) {
linearResidu.push(X.get(i), Y.get(i) - (_A * Math.pow(X.get(i), 2)))
i++
}
_B = linearResidu.coefficientA()
_C = linearResidu.coefficientB()
break
case (X.length() === 2 && X.get(1) - X.get(0) !== 0):
// There are only two datapoints, so we need to be creative to get to a quadratic solution
// As we know this is part of a 'linear' acceleration, we know that the second derivative should obey 2 * _A = angular acceleration = 2 * angular distance / (delta t)^2
_A = (Y.get(1) - Y.get(0)) / Math.pow(X.get(1) - X.get(0), 2)
// As the first derivative should match angular velocity (= angular acceleration * (delta t))
_B = -2 * _A * X.get(0)
_C = 0
break
default:
_A = 0
_B = 0
_C = 0
}
}

function firstDerivativeAtPosition (position) {
if (X.length() > 2 && position < X.length()) {
if (X.length() > 1 && position < X.length()) {
return ((_A * 2 * X.get(position)) + _B)
} else {
return 0
}
}

function secondDerivativeAtPosition (position) {
if (X.length() > 2 && position < X.length()) {
if (X.length() > 1 && position < X.length()) {
return (_A * 2)
} else {
return 0
Expand Down Expand Up @@ -221,6 +235,14 @@ function createTSQuadraticSeries (maxSeriesLength = 0) {
return Y.maximum()
}

function xAverage () {
return X.average()
}

function yAverage () {
return Y.average()
}

function xSeries () {
return X.series()
}
Expand Down Expand Up @@ -276,6 +298,8 @@ function createTSQuadraticSeries (maxSeriesLength = 0) {
minimumY,
maximumX,
maximumY,
xAverage,
yAverage,
xSum,
ySum,
xSeries,
Expand Down

0 comments on commit e0aabf4

Please sign in to comment.