Skip to content

Commit a46f592

Browse files
Updated internal module
1 parent 079ddf3 commit a46f592

File tree

10 files changed

+115
-107
lines changed

10 files changed

+115
-107
lines changed

deriv/deriv.v

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module deriv
22

33
import vsl.func
4-
import vsl.internal
4+
import vsl.internal.prec
55
import math
66

77
fn central_deriv(f func.Fn, x f64, h f64) (f64, f64, f64) {
@@ -18,9 +18,9 @@ fn central_deriv(f func.Fn, x f64, h f64) (f64, f64, f64) {
1818
fph := f.eval(x + h / 2)
1919
r3 := 0.50 * (fp1 - fm1)
2020
r5 := (4.0 / 3.0) * (fph - fmh) - (1.0 / 3.0) * r3
21-
e3 := (math.abs(fp1) + math.abs(fm1)) * internal.f64_epsilon
22-
e5 := 2.0 * (math.abs(fph) + math.abs(fmh)) * internal.f64_epsilon + e3 // The next term is due to finite precision in x+h = O(eps * x)
23-
dy := math.max(math.abs(r3 / h), math.abs(r5 / h)) * (math.abs(x) / h) * internal.f64_epsilon
21+
e3 := (math.abs(fp1) + math.abs(fm1)) * prec.f64_epsilon
22+
e5 := 2.0 * (math.abs(fph) + math.abs(fmh)) * prec.f64_epsilon + e3 // The next term is due to finite precision in x+h = O(eps * x)
23+
dy := math.max(math.abs(r3 / h), math.abs(r5 / h)) * (math.abs(x) / h) * prec.f64_epsilon
2424
/*
2525
The truncation error in the r5 approximation itself is O(h^4).
2626
* However, for safety, we estimate the error from r5-r3, which is
@@ -71,8 +71,8 @@ fn forward_deriv(f func.Fn, x f64, h f64) (f64, f64, f64) {
7171
f4 := f.eval(x + h)
7272
r2 := 2.0 * (f4 - f2)
7373
r4 := (22.0 / 3.0) * (f4 - f3) - (62.0 / 3.0) * (f3 - f2) + (52.0 / 3.0) * (f2 - f1) // Estimate the rounding error for r4
74-
e4 := 2.0 * 20.670 * (math.abs(f4) + math.abs(f3) + math.abs(f2) + math.abs(f1)) * internal.f64_epsilon // The next term is due to finite precision in x+h = O(eps * x)
75-
dy := math.max(math.abs(r2 / h), math.abs(r4 / h)) * math.abs(x / h) * internal.f64_epsilon
74+
e4 := 2.0 * 20.670 * (math.abs(f4) + math.abs(f3) + math.abs(f2) + math.abs(f1)) * prec.f64_epsilon // The next term is due to finite precision in x+h = O(eps * x)
75+
dy := math.max(math.abs(r2 / h), math.abs(r4 / h)) * math.abs(x / h) * prec.f64_epsilon
7676
/*
7777
The truncation error in the r4 approximation itself is O(h^3).
7878
* However, for safety, we estimate the error from r4-r2, which is

diff/diff.v

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module diff
22

33
import vsl.func
4-
import vsl.internal
4+
import vsl.internal.prec
55
import math
66

77
pub fn backward(f func.Fn, x f64) (f64, f64) {
@@ -10,7 +10,7 @@ pub fn backward(f func.Fn, x f64) (f64, f64) {
1010
* size to get a very rough estimate of f''. Use this to estimate
1111
* the step size which will minimize the error in calculating f'.
1212
*/
13-
mut h := internal.sqrt_f64_epsilon
13+
mut h := prec.sqrt_f64_epsilon
1414
mut a := []f64{}
1515
mut d := []f64{}
1616
mut k := 0
@@ -33,12 +33,12 @@ pub fn backward(f func.Fn, x f64) (f64, f64) {
3333
* step size.
3434
*/
3535
mut a2 := math.abs(d[0] + d[1] + d[2])
36-
if a2 < 100.0 * internal.sqrt_f64_epsilon {
37-
a2 = 100.0 * internal.sqrt_f64_epsilon
36+
if a2 < 100.0 * prec.sqrt_f64_epsilon {
37+
a2 = 100.0 * prec.sqrt_f64_epsilon
3838
}
39-
h = math.sqrt(internal.sqrt_f64_epsilon / (2.0 * a2))
40-
if h > 100.0 * internal.sqrt_f64_epsilon {
41-
h = 100.0 * internal.sqrt_f64_epsilon
39+
h = math.sqrt(prec.sqrt_f64_epsilon / (2.0 * a2))
40+
if h > 100.0 * prec.sqrt_f64_epsilon {
41+
h = 100.0 * prec.sqrt_f64_epsilon
4242
}
4343
return (f.eval(x) - f.eval(x - h)) / h, math.abs(10.0 * a2 * h)
4444
}
@@ -49,7 +49,7 @@ pub fn forward(f func.Fn, x f64) (f64, f64) {
4949
* size to get a very rough estimate of f''. Use this to estimate
5050
* the step size which will minimize the error in calculating f'.
5151
*/
52-
mut h := internal.sqrt_f64_epsilon
52+
mut h := prec.sqrt_f64_epsilon
5353
mut a := []f64{}
5454
mut d := []f64{}
5555
mut k := 0
@@ -72,12 +72,12 @@ pub fn forward(f func.Fn, x f64) (f64, f64) {
7272
* step size.
7373
*/
7474
mut a2 := math.abs(d[0] + d[1] + d[2])
75-
if a2 < 100.0 * internal.sqrt_f64_epsilon {
76-
a2 = 100.0 * internal.sqrt_f64_epsilon
75+
if a2 < 100.0 * prec.sqrt_f64_epsilon {
76+
a2 = 100.0 * prec.sqrt_f64_epsilon
7777
}
78-
h = math.sqrt(internal.sqrt_f64_epsilon / (2.0 * a2))
79-
if h > 100.0 * internal.sqrt_f64_epsilon {
80-
h = 100.0 * internal.sqrt_f64_epsilon
78+
h = math.sqrt(prec.sqrt_f64_epsilon / (2.0 * a2))
79+
if h > 100.0 * prec.sqrt_f64_epsilon {
80+
h = 100.0 * prec.sqrt_f64_epsilon
8181
}
8282
return (f.eval(x + h) - f.eval(x)) / h, math.abs(10.0 * a2 * h)
8383
}
@@ -88,7 +88,7 @@ pub fn central(f func.Fn, x f64) (f64, f64) {
8888
* size to get a very rough estimate of f'''. Use this to estimate
8989
* the step size which will minimize the error in calculating f'.
9090
*/
91-
mut h := internal.sqrt_f64_epsilon
91+
mut h := prec.sqrt_f64_epsilon
9292
mut a := []f64{}
9393
mut d := []f64{}
9494
mut k := 0
@@ -111,12 +111,12 @@ pub fn central(f func.Fn, x f64) (f64, f64) {
111111
* step size.
112112
*/
113113
mut a3 := math.abs(d[0] + d[1] + d[2] + d[3])
114-
if a3 < 100.0 * internal.sqrt_f64_epsilon {
115-
a3 = 100.0 * internal.sqrt_f64_epsilon
114+
if a3 < 100.0 * prec.sqrt_f64_epsilon {
115+
a3 = 100.0 * prec.sqrt_f64_epsilon
116116
}
117-
h = math.pow(internal.sqrt_f64_epsilon / (2.0 * a3), 1.0 / 3.0)
118-
if h > 100.0 * internal.sqrt_f64_epsilon {
119-
h = 100.0 * internal.sqrt_f64_epsilon
117+
h = math.pow(prec.sqrt_f64_epsilon / (2.0 * a3), 1.0 / 3.0)
118+
if h > 100.0 * prec.sqrt_f64_epsilon {
119+
h = 100.0 * prec.sqrt_f64_epsilon
120120
}
121121
return (f.eval(x + h) - f.eval(x - h)) / (2.0 * h), math.abs(100.0 * a3 * h * h)
122122
}

examples/ml_kmeans_plot02/main.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module main
22

33
import vsl.ml
44
import vsl.plot
5-
import internal.dataset
5+
import prec.dataset
66

77
// data
88
mut data := ml.data_from_raw_x(dataset.raw_dataset.map([it[0], it[1]]))?

fun/cheb.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module fun
22

33
import math
4-
import vsl.internal
4+
import vsl.internal.prec
55

66
// data for a Chebyshev series over a given interval
77
pub struct ChebSeries {
@@ -28,5 +28,5 @@ pub fn (cs ChebSeries) eval_e(x f64) (f64, f64) {
2828
temp = d
2929
d = y * d - dd + 0.5 * cs.c[0]
3030
e += math.abs(y * temp) + math.abs(dd) + 0.5 * math.abs(cs.c[0])
31-
return d, f64(internal.f64_epsilon) * e + math.abs(cs.c[cs.order])
31+
return d, f64(prec.f64_epsilon) * e + math.abs(cs.c[cs.order])
3232
}

fun/choose.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module fun
22

33
import math
4-
import vsl.internal
4+
import vsl.internal.prec
55

66
// Compute the binomial coefficient
77
pub fn choose(n int, p int) f64 {
@@ -11,7 +11,7 @@ pub fn choose(n int, p int) f64 {
1111
n_f64 := f64(n)
1212
p_f64 := f64(p)
1313
k := math.max(p_f64, n_f64 - p_f64)
14-
if k < internal.max_int_fact_arg {
14+
if k < prec.max_int_fact_arg {
1515
return math.factorial(n_f64) / (math.factorial(p_f64) * math.factorial(n_f64 - p_f64))
1616
}
1717
log_choose := math.log_factorial(n_f64 + 1.0) - math.log_factorial(p_f64 + 1.0) - math.log_factorial(

fun/hypot.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module fun
22

33
import math
44
import vsl.errors
5-
import vsl.internal
5+
import vsl.internal.prec
66

77
pub fn hypot(x f64, y f64) f64 {
88
if math.is_inf(x, 0) || math.is_inf(y, 0) {
@@ -44,7 +44,7 @@ pub fn hypot_e(x f64, y f64) (f64, f64) {
4444
root_term := math.sqrt(1.0 + rat * rat)
4545
if max < math.max_f64 / root_term {
4646
result = max * root_term
47-
result_err = f64(2.0) * internal.f64_epsilon * math.abs(result)
47+
result_err = f64(2.0) * prec.f64_epsilon * math.abs(result)
4848
} else {
4949
errors.vsl_panic('overflow in hypot_e function', .eovrflw)
5050
}

fun/trig.v

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module fun
22

33
import math
4-
import vsl.internal
4+
import vsl.internal.prec
55

66
// sinh(x) series
77
// double-precision for |x| < 1.0
@@ -86,7 +86,7 @@ const (
8686
pub fn sin_e(x f64) (f64, f64) {
8787
sgn_x := if x < 0 { -1 } else { 1 }
8888
abs_x := math.abs(x)
89-
if abs_x < internal.root4_f64_epsilon {
89+
if abs_x < prec.root4_f64_epsilon {
9090
x2 := x * x
9191
return x * (1.0 - x2 / 6.0), math.abs(x * x2 * x2 / 100.0)
9292
} else {
@@ -115,22 +115,22 @@ pub fn sin_e(x f64) (f64, f64) {
115115
result = 1.0 - 0.5 * z * z * (1.0 - z * z * cos_cs_val)
116116
}
117117
result *= sgn_result
118-
if abs_x > 1.0 / internal.f64_epsilon {
118+
if abs_x > 1.0 / prec.f64_epsilon {
119119
result_err = math.abs(result)
120-
} else if abs_x > 100.0 / internal.sqrt_f64_epsilon {
121-
result_err = 2.0 * abs_x * internal.f64_epsilon * math.abs(result)
122-
} else if abs_x > 0.1 / internal.sqrt_f64_epsilon {
123-
result_err = 2.0 * internal.sqrt_f64_epsilon * math.abs(result)
120+
} else if abs_x > 100.0 / prec.sqrt_f64_epsilon {
121+
result_err = 2.0 * abs_x * prec.f64_epsilon * math.abs(result)
122+
} else if abs_x > 0.1 / prec.sqrt_f64_epsilon {
123+
result_err = 2.0 * prec.sqrt_f64_epsilon * math.abs(result)
124124
} else {
125-
result_err = 2.0 * internal.f64_epsilon * math.abs(result)
125+
result_err = 2.0 * prec.f64_epsilon * math.abs(result)
126126
}
127127
return result, result_err
128128
}
129129
}
130130

131131
pub fn cos_e(x f64) (f64, f64) {
132132
abs_x := math.abs(x)
133-
if abs_x < internal.root4_f64_epsilon {
133+
if abs_x < prec.root4_f64_epsilon {
134134
x2 := x * x
135135
return 1.0 - 0.5 * x2, math.abs(x2 * x2 / 12.0)
136136
} else {
@@ -162,14 +162,14 @@ pub fn cos_e(x f64) (f64, f64) {
162162
result = z * (1.0 + z * z * sin_cs_val)
163163
}
164164
result *= sgn_result
165-
if abs_x > 1.0 / internal.f64_epsilon {
165+
if abs_x > 1.0 / prec.f64_epsilon {
166166
result_err = math.abs(result)
167-
} else if abs_x > 100.0 / internal.sqrt_f64_epsilon {
168-
result_err = 2.0 * abs_x * internal.f64_epsilon * math.abs(result)
169-
} else if abs_x > 0.1 / internal.sqrt_f64_epsilon {
170-
result_err = 2.0 * internal.sqrt_f64_epsilon * math.abs(result)
167+
} else if abs_x > 100.0 / prec.sqrt_f64_epsilon {
168+
result_err = 2.0 * abs_x * prec.f64_epsilon * math.abs(result)
169+
} else if abs_x > 0.1 / prec.sqrt_f64_epsilon {
170+
result_err = 2.0 * prec.sqrt_f64_epsilon * math.abs(result)
171171
} else {
172-
result_err = 2.0 * internal.f64_epsilon * math.abs(result)
172+
result_err = 2.0 * prec.f64_epsilon * math.abs(result)
173173
}
174174
return result, result_err
175175
}
@@ -178,7 +178,7 @@ pub fn cos_e(x f64) (f64, f64) {
178178
pub fn sin(x f64) f64 {
179179
sgn_x := if x < 0 { -1 } else { 1 }
180180
abs_x := math.abs(x)
181-
if abs_x < internal.root4_f64_epsilon {
181+
if abs_x < prec.root4_f64_epsilon {
182182
x2 := x * x
183183
return x * (1.0 - x2 / 6.0)
184184
} else {
@@ -212,7 +212,7 @@ pub fn sin(x f64) f64 {
212212

213213
pub fn cos(x f64) f64 {
214214
abs_x := math.abs(x)
215-
if abs_x < internal.root4_f64_epsilon {
215+
if abs_x < prec.root4_f64_epsilon {
216216
x2 := x * x
217217
return 1.0 - 0.5 * x2
218218
} else {

internal/machine.v

Lines changed: 0 additions & 56 deletions
This file was deleted.

internal/prec/machine.v

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module prec
2+
3+
import math.internal as mathinternal
4+
5+
// contants to do fine tuning of precision for the functions
6+
// implemented in pure V.
7+
// This can be fine tuned for each function, but the default
8+
// values are good enough for most cases.
9+
// Optimizing this in Vlib makes direct impact on the performance
10+
// of VSL programs.
11+
pub const (
12+
f64_epsilon = mathinternal.f64_epsilon
13+
sqrt_f64_epsilon = mathinternal.sqrt_f64_epsilon
14+
root3_f64_epsilon = mathinternal.root3_f64_epsilon
15+
root4_f64_epsilon = mathinternal.root4_f64_epsilon
16+
root5_f64_epsilon = mathinternal.root5_f64_epsilon
17+
root6_f64_epsilon = mathinternal.root6_f64_epsilon
18+
log_f64_epsilon = mathinternal.log_f64_epsilon
19+
f64_min = mathinternal.f64_min
20+
sqrt_f64_min = mathinternal.sqrt_f64_min
21+
root3_f64_min = mathinternal.root3_f64_min
22+
root4_f64_min = mathinternal.root4_f64_min
23+
root5_f64_min = mathinternal.root5_f64_min
24+
root6_f64_min = mathinternal.root6_f64_min
25+
log_f64_min = mathinternal.log_f64_min
26+
f64_max = mathinternal.f64_max
27+
sqrt_f64_max = mathinternal.sqrt_f64_max
28+
root3_f64_max = mathinternal.root3_f64_max
29+
root4_f64_max = mathinternal.root4_f64_max
30+
root5_f64_max = mathinternal.root5_f64_max
31+
root6_f64_max = mathinternal.root6_f64_max
32+
log_f64_max = mathinternal.log_f64_max
33+
f32_epsilon = mathinternal.f32_epsilon
34+
sqrt_f32_epsilon = mathinternal.sqrt_f32_epsilon
35+
root3_f32_epsilon = mathinternal.root3_f32_epsilon
36+
root4_f32_epsilon = mathinternal.root4_f32_epsilon
37+
root5_f32_epsilon = mathinternal.root5_f32_epsilon
38+
root6_f32_epsilon = mathinternal.root6_f32_epsilon
39+
log_f32_epsilon = mathinternal.log_f32_epsilon
40+
f32_min = mathinternal.f32_min
41+
sqrt_f32_min = mathinternal.sqrt_f32_min
42+
root3_f32_min = mathinternal.root3_f32_min
43+
root4_f32_min = mathinternal.root4_f32_min
44+
root5_f32_min = mathinternal.root5_f32_min
45+
root6_f32_min = mathinternal.root6_f32_min
46+
log_f32_min = mathinternal.log_f32_min
47+
f32_max = mathinternal.f32_max
48+
sqrt_f32_max = mathinternal.sqrt_f32_max
49+
root3_f32_max = mathinternal.root3_f32_max
50+
root4_f32_max = mathinternal.root4_f32_max
51+
root5_f32_max = mathinternal.root5_f32_max
52+
root6_f32_max = mathinternal.root6_f32_max
53+
log_f32_max = mathinternal.log_f32_max
54+
sflt_epsilon = mathinternal.sflt_epsilon
55+
sqrt_sflt_epsilon = mathinternal.sqrt_sflt_epsilon
56+
root3_sflt_epsilon = mathinternal.root3_sflt_epsilon
57+
root4_sflt_epsilon = mathinternal.root4_sflt_epsilon
58+
root5_sflt_epsilon = mathinternal.root5_sflt_epsilon
59+
root6_sflt_epsilon = mathinternal.root6_sflt_epsilon
60+
log_sflt_epsilon = mathinternal.log_sflt_epsilon
61+
max_int_fact_arg = mathinternal.max_int_fact_arg
62+
max_f64_fact_arg = mathinternal.max_f64_fact_arg
63+
max_long_f64_fact_arg = mathinternal.max_long_f64_fact_arg
64+
)

0 commit comments

Comments
 (0)