diff --git a/deriv/deriv.v b/deriv/deriv.v index 47a2b33cd..77c4bb96c 100644 --- a/deriv/deriv.v +++ b/deriv/deriv.v @@ -7,10 +7,10 @@ import math fn central_deriv(f func.Fn, x f64, h f64) (f64, f64, f64) { /* Compute the derivative using the 5-point rule (x-h, x-h/2, x, - * x+h/2, x+h). Note that the central point is not used. - * Compute the error using the difference between the 5-point and - * the 3-point rule (x-h,x,x+h). Again the central point is not - * used. + * x+h/2, x+h). Note that the central point is not used. + * Compute the error using the difference between the 5-point and + * the 3-point rule (x-h,x,x+h). Again the central point is not + * used. */ fm1 := f.eval(x - h) fp1 := f.eval(x + h) @@ -23,9 +23,9 @@ fn central_deriv(f func.Fn, x f64, h f64) (f64, f64, f64) { dy := math.max(math.abs(r3 / h), math.abs(r5 / h)) * (math.abs(x) / h) * prec.f64_epsilon /* The truncation error in the r5 approximation itself is O(h^4). - * However, for safety, we estimate the error from r5-r3, which is - * O(h^2). By scaling h we will minimise this estimated error, not - * the actual truncation error in r5. + * However, for safety, we estimate the error from r5-r3, which is + * O(h^2). By scaling h we will minimise this estimated error, not + * the actual truncation error in r5. */ result := r5 / h abserr_trunc := math.abs((r5 - r3) / h) // Estimated truncation error O(h^2) @@ -40,15 +40,15 @@ pub fn central(f func.Fn, x f64, h f64) (f64, f64) { if round < trunc && (round > 0.0 && trunc > 0.0) { /* Compute an optimised stepsize to minimize the total error, - * using the scaling of the truncation error (O(h^2)) and - * rounding error (O(1/h)). + * using the scaling of the truncation error (O(h^2)) and + * rounding error (O(1/h)). */ h_opt := h * math.pow(round / (2.0 * trunc), 1.0 / 3.0) r_opt, round_opt, trunc_opt := central_deriv(f, x, h_opt) error_opt := round_opt + trunc_opt /* Check that the new error is smaller, and that the new derivative - * is consistent with the error bounds of the original estimate. + * is consistent with the error bounds of the original estimate. */ if error_opt < error && math.abs(r_opt - r_0) < 4.0 * error { result = r_opt @@ -61,9 +61,9 @@ pub fn central(f func.Fn, x f64, h f64) (f64, f64) { fn forward_deriv(f func.Fn, x f64, h f64) (f64, f64, f64) { /* Compute the derivative using the 4-point rule (x+h/4, x+h/2, - * x+3h/4, x+h). - * Compute the error using the difference between the 4-point and - * the 2-point rule (x+h/2,x+h). + * x+3h/4, x+h). + * Compute the error using the difference between the 4-point and + * the 2-point rule (x+h/2,x+h). */ f1 := f.eval(x + h / 4.0) f2 := f.eval(x + h / 2.0) @@ -75,9 +75,9 @@ fn forward_deriv(f func.Fn, x f64, h f64) (f64, f64, f64) { dy := math.max(math.abs(r2 / h), math.abs(r4 / h)) * math.abs(x / h) * prec.f64_epsilon /* The truncation error in the r4 approximation itself is O(h^3). - * However, for safety, we estimate the error from r4-r2, which is - * O(h). By scaling h we will minimise this estimated error, not - * the actual truncation error in r4. + * However, for safety, we estimate the error from r4-r2, which is + * O(h). By scaling h we will minimise this estimated error, not + * the actual truncation error in r4. */ result := r4 / h abserr_trunc := math.abs((r4 - r2) / h) // Estimated truncation error O(h) @@ -92,15 +92,15 @@ pub fn forward(f func.Fn, x f64, h f64) (f64, f64) { if round < trunc && (round > 0.0 && trunc > 0.0) { /* Compute an optimised stepsize to minimize the total error, - * using the scaling of the estimated truncation error (O(h)) and - * rounding error (O(1/h)). + * using the scaling of the estimated truncation error (O(h)) and + * rounding error (O(1/h)). */ h_opt := h * math.pow(round / trunc, 1.0 / 2.0) r_opt, round_opt, trunc_opt := forward_deriv(f, x, h_opt) error_opt := round_opt + trunc_opt /* Check that the new error is smaller, and that the new derivative - * is consistent with the error bounds of the original estimate. + * is consistent with the error bounds of the original estimate. */ if error_opt < error && math.abs(r_opt - r_0) < 4.0 * error { result = r_opt diff --git a/diff/diff.v b/diff/diff.v index 108115055..5d7ead0e9 100644 --- a/diff/diff.v +++ b/diff/diff.v @@ -7,8 +7,8 @@ import math pub fn backward(f func.Fn, x f64) (f64, f64) { /* Construct a divided difference table with a fairly large step - * size to get a very rough estimate of f''. Use this to estimate - * the step size which will minimize the error in calculating f'. + * size to get a very rough estimate of f''. Use this to estimate + * the step size which will minimize the error in calculating f'. */ mut h := prec.sqrt_f64_epsilon mut a := []f64{} @@ -17,7 +17,7 @@ pub fn backward(f func.Fn, x f64) (f64, f64) { mut i := 0 /* Algorithm based on description on pg. 204 of Conte and de Boor - * (CdB) - coefficients of Newton form of polynomial of degree 2. + * (CdB) - coefficients of Newton form of polynomial of degree 2. */ for i = 0; i < 3; i++ { a << x + (f64(i) - 2.0) * h @@ -30,7 +30,7 @@ pub fn backward(f func.Fn, x f64) (f64, f64) { } /* Adapt procedure described on pg. 282 of CdB to find best value of - * step size. + * step size. */ mut a2 := math.abs(d[0] + d[1] + d[2]) if a2 < 100.0 * prec.sqrt_f64_epsilon { @@ -46,8 +46,8 @@ pub fn backward(f func.Fn, x f64) (f64, f64) { pub fn forward(f func.Fn, x f64) (f64, f64) { /* Construct a divided difference table with a fairly large step - * size to get a very rough estimate of f''. Use this to estimate - * the step size which will minimize the error in calculating f'. + * size to get a very rough estimate of f''. Use this to estimate + * the step size which will minimize the error in calculating f'. */ mut h := prec.sqrt_f64_epsilon mut a := []f64{} @@ -56,7 +56,7 @@ pub fn forward(f func.Fn, x f64) (f64, f64) { mut i := 0 /* Algorithm based on description on pg. 204 of Conte and de Boor - * (CdB) - coefficients of Newton form of polynomial of degree 2. + * (CdB) - coefficients of Newton form of polynomial of degree 2. */ for i = 0; i < 3; i++ { a << x + f64(i) * h @@ -69,7 +69,7 @@ pub fn forward(f func.Fn, x f64) (f64, f64) { } /* Adapt procedure described on pg. 282 of CdB to find best value of - * step size. + * step size. */ mut a2 := math.abs(d[0] + d[1] + d[2]) if a2 < 100.0 * prec.sqrt_f64_epsilon { @@ -85,8 +85,8 @@ pub fn forward(f func.Fn, x f64) (f64, f64) { pub fn central(f func.Fn, x f64) (f64, f64) { /* Construct a divided difference table with a fairly large step - * size to get a very rough estimate of f'''. Use this to estimate - * the step size which will minimize the error in calculating f'. + * size to get a very rough estimate of f'''. Use this to estimate + * the step size which will minimize the error in calculating f'. */ mut h := prec.sqrt_f64_epsilon mut a := []f64{} @@ -95,7 +95,7 @@ pub fn central(f func.Fn, x f64) (f64, f64) { mut i := 0 /* Algorithm based on description on pg. 204 of Conte and de Boor - * (CdB) - coefficients of Newton form of polynomial of degree 3. + * (CdB) - coefficients of Newton form of polynomial of degree 3. */ for i = 0; i < 4; i++ { a << x + (f64(i) - 2.0) * h @@ -108,7 +108,7 @@ pub fn central(f func.Fn, x f64) (f64, f64) { } /* Adapt procedure described on pg. 282 of CdB to find best value of - * step size. + * step size. */ mut a3 := math.abs(d[0] + d[1] + d[2] + d[3]) if a3 < 100.0 * prec.sqrt_f64_epsilon { diff --git a/plot/show.v b/plot/show.v index fd676540a..63a9b566a 100644 --- a/plot/show.v +++ b/plot/show.v @@ -70,28 +70,28 @@ pub fn (p Plot) get_plotly_script(element_id string, config PlotlyScriptConfig) content: 'import "https://cdn.plot.ly/plotly-2.26.2.min.js"; function removeEmptyFieldsDeeply(obj) { - if (Array.isArray(obj)) { - return obj.map(removeEmptyFieldsDeeply); - } - if (typeof obj === "object") { - const newObj = Object.fromEntries( - Object.entries(obj) - .map(([key, value]) => [key, removeEmptyFieldsDeeply(value)]) - .filter(([_, value]) => !!value) - ); - return Object.keys(newObj).length > 0 ? newObj : undefined; - } - return obj; + if (Array.isArray(obj)) { + return obj.map(removeEmptyFieldsDeeply); + } + if (typeof obj === "object") { + const newObj = Object.fromEntries( + Object.entries(obj) + .map(([key, value]) => [key, removeEmptyFieldsDeeply(value)]) + .filter(([_, value]) => !!value) + ); + return Object.keys(newObj).length > 0 ? newObj : undefined; + } + return obj; } const layout = ${layout_json}; const traces_with_type_json = ${traces_with_type_json}; const data = [...traces_with_type_json] - .map(({ type, trace: { CommonTrace, _type, ...trace } }) => ({ type, ...CommonTrace, ...trace })); + .map(({ type, trace: { CommonTrace, _type, ...trace } }) => ({ type, ...CommonTrace, ...trace })); const payload = { - data: removeEmptyFieldsDeeply(data), - layout: removeEmptyFieldsDeeply(layout), + data: removeEmptyFieldsDeeply(data), + layout: removeEmptyFieldsDeeply(layout), }; Plotly.newPlot("${element_id}", payload);' @@ -106,14 +106,14 @@ fn (p Plot) get_html(element_id string, config PlotConfig) string { return ' - - ${title} - - -
- - ${*plot_script} - + + ${title} + + +
+ + ${*plot_script} + ' } diff --git a/poly/poly.v b/poly/poly.v index 30e842e2f..b3015fabb 100644 --- a/poly/poly.v +++ b/poly/poly.v @@ -88,13 +88,13 @@ pub fn solve_cubic(a f64, b f64, c f64) []f64 { return [-a / 3.0, -a / 3.0, -a / 3.0] } else if cr2 == cq3 { /* - this test is actually r2 == q3, written in a form suitable - for exact computation with integers + This test is actually r2 == q3, written in a form suitable + for exact computation with integers */ /* Due to finite precision some double roots may be missed, and - considered to be a pair of complex roots z = x +/- epsilon i - close to the real axis. + considered to be a pair of complex roots z = x +/- epsilon i + close to the real axis. */ sqrt_q := math.sqrt(q) if r > 0.0 {