Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

javascript - Run Tape test in temperature functions

I am new in backend feature and I have these functions and I need to run automated tests using Tape, this functions is to calculate splines cubic can someone help me? I tried run the test only in getTemperature but no sucess, I don't understand too much of tape so any help will be very appreciated

export default class CubicSpline {
    splines

    constructor(points){
        this.splines = this.calculateCurves(points)
    }

    public getTemperature(x){        
        let n = this.splines.length
        let spline = null;
        
        if (x <= this.splines[0].x0) {
            spline = this.splines[0]
        } else if (x >= this.splines[n - 1].x0) {
            spline = this.splines[n - 1]
        } else {
            var i = 0
            var j = n - 1
            while (i + 1 < j) {
                // let k = i + (j - i) / 2 // Todo this was original line from swift. Discuss with Nate
                let k = i + Math.floor((j - i) / 2)
                if (x <= this.splines[k].x0) {
                    j = k
                } else {
                    i = k
                }
            }
            spline = this.splines[j]
        }

        return this.calculateTemperature(spline, x);
    }

    private calculateTemperature(spline, x){
        return spline.coeficients.a + spline.coeficients.b * (x - spline.x0) + spline.coeficients.c / 2.0 * Math.pow(x - spline.x0, 2) + spline.coeficients.d / 6.0 * Math.pow(x - spline.x0, 3)
    }

    private calculateCurves(points){
        let n = points.length
        let x = points.map(point => point.x)
        let y = points.map(point => point.y)
        
        var splines = []

        for(let i = 0; i<n; i++) {
            let spline = {x0: x[i], coeficients: {a: y[i], b: 0, c: 0, d: 0}}
            splines.push(spline)
        }

        splines[0].coeficients.c = 0
        splines[n-1].coeficients.c = 0
        
        // Resolve 3diagonal quation system with Thomas method
        var alpha = new Array(n-1).fill(0)
        var beta  = new Array(n-1).fill(0)

        for(let i = 1; i<n-1; i++) {
            let hi      = x[i] - x[i - 1]
            let hi1     = x[i + 1] - x[i]
            let A = hi
            let C = 2.0 * (hi + hi1)
            let B = hi1
            let F = 6.0 * ((y[i + 1] - y[i]) / hi1 - (y[i] - y[i - 1]) / hi)
            let z = (A * alpha[i - 1] + C)
            alpha[i] = -B / z
            beta[i] = (F - A * beta[i - 1]) / z
        }
        
        // Find solution:
        for(let i = n-2; i>=0; i--) {
            splines[i].coeficients.c = alpha[i] * splines[i + 1].coeficients.c + beta[i]
        }
        
        for(let i = n-1; i>=1; i--) {
            let hi = x[i] - x[i - 1]
            splines[i].coeficients.d = (splines[i].coeficients.c - splines[i - 1].coeficients.c) / hi
            splines[i].coeficients.b = hi * (2.0 * splines[i].coeficients.c + splines[i - 1].coeficients.c) / 6.0 + (y[i] - y[i - 1]) / hi
        }
        
        return splines
    }
}

I used Nodejs and tape to run this on backend

question from:https://stackoverflow.com/questions/65938599/run-tape-test-in-temperature-functions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...