Skip to content

Commit

Permalink
Resolve linter complaints, remove unused import(rebase over master)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed May 26, 2024
1 parent b66e4d9 commit caec193
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
38 changes: 19 additions & 19 deletions deriv/deriv.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions diff/diff.v
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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{}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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{}
Expand All @@ -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
Expand All @@ -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 {
Expand Down
46 changes: 23 additions & 23 deletions plot/show.v
Original file line number Diff line number Diff line change
Expand Up @@ -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);'
Expand All @@ -106,14 +106,14 @@ fn (p Plot) get_html(element_id string, config PlotConfig) string {

return '<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
</head>
<body>
<div id="${element_id}"></div>
${*plot_script}
</body>
<head>
<title>${title}</title>
</head>
<body>
<div id="${element_id}"></div>
${*plot_script}
</body>
</html>'
}

Expand Down
8 changes: 4 additions & 4 deletions poly/poly.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit caec193

Please sign in to comment.