Skip to content

Commit

Permalink
Updated plotly method and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Nov 5, 2023
1 parent f709b7d commit 7604e75
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 145 deletions.
30 changes: 3 additions & 27 deletions deriv/deriv_test.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module deriv

import vsl.func
import vsl.float.float64
import math

fn f1(x f64, _ []f64) f64 {
Expand Down Expand Up @@ -102,16 +103,7 @@ fn test_deriv() {
}

fn deriv_test(deriv_method string, f func.Fn, df func.Fn, x f64) bool {
expected := df.eval(x)
h := 1e-5
result, _ := if deriv_method == 'backward' {
backward(f, x, h)
} else if deriv_method == 'forward' {
forward(f, x, h)
} else {
central(f, x, h)
}
return compare(result, expected)
return deriv_near_test(deriv_method, f, df, x, 1e-5)
}

fn deriv_near_test(deriv_method string, f func.Fn, df func.Fn, x f64, tolerance f64) bool {
Expand All @@ -124,21 +116,5 @@ fn deriv_near_test(deriv_method string, f func.Fn, df func.Fn, x f64, tolerance
} else {
central(f, x, h)
}
return compare_near(result, expected, tolerance)
}

// Helper methods for comparing floats
[inline]
fn compare(x f64, y f64) bool {
return compare_near(x, y, 1e-5)
}

fn compare_near(x f64, y f64, tolerance f64) bool {
// Special case for zeroes
if x < tolerance && x > (-1.0 * tolerance) && y < tolerance && y > (-1.0 * tolerance) {
return true
}
deriv := math.abs(x - y)
mean := math.abs(x + y) / 2.0
return if math.is_nan(deriv / mean) { true } else { ((deriv / mean) < tolerance) }
return float64.tolerance(result, expected, tolerance)
}
31 changes: 4 additions & 27 deletions diff/diff_test.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module diff

import vsl.func
import math
import vsl.float.float64
import vsl.func

fn f1(x f64, _ []f64) f64 {
return math.exp(x)
Expand Down Expand Up @@ -102,15 +103,7 @@ fn test_diff() {
}

fn diff_test(diff_method string, f func.Fn, df func.Fn, x f64) bool {
expected := df.eval(x)
result, _ := if diff_method == 'backward' {
backward(f, x)
} else if diff_method == 'forward' {
forward(f, x)
} else {
central(f, x)
}
return compare(result, expected)
return diff_near_test(diff_method, f, df, x, 1e-5)
}

fn diff_near_test(diff_method string, f func.Fn, df func.Fn, x f64, tolerance f64) bool {
Expand All @@ -122,21 +115,5 @@ fn diff_near_test(diff_method string, f func.Fn, df func.Fn, x f64, tolerance f6
} else {
central(f, x)
}
return compare_near(result, expected, tolerance)
}

// Helper methods for comparing floats
[inline]
fn compare(x f64, y f64) bool {
return compare_near(x, y, 1e-5)
}

fn compare_near(x f64, y f64, tolerance f64) bool {
// Special case for zeroes
if x < tolerance && x > (-1.0 * tolerance) && y < tolerance && y > (-1.0 * tolerance) {
return true
}
diff := math.abs(x - y)
mean := math.abs(x + y) / 2.0
return if math.is_nan(diff / mean) { true } else { ((diff / mean) < tolerance) }
return float64.tolerance(result, expected, tolerance)
}
103 changes: 46 additions & 57 deletions easings/easings_test.v
Original file line number Diff line number Diff line change
@@ -1,187 +1,176 @@
import vsl.easings
import math
import vsl.float.float64

// Linear tests
fn test_linear_interpolation() {
assert compare(easings.linear_interpolation(2), 2.0)
assert float64.tolerance(easings.linear_interpolation(2), 2.0, 1e-5)
}

// Quadratic tests
fn test_quadratic_ease_in() {
assert compare(easings.quadratic_ease_in(2), 4.0)
assert float64.tolerance(easings.quadratic_ease_in(2), 4.0, 1e-5)
}

fn test_quadratic_ease_out() {
assert compare(easings.quadratic_ease_out(5), -15.0)
assert float64.tolerance(easings.quadratic_ease_out(5), -15.0, 1e-5)
}

fn test_quadratic_ease_in_out() {
// p < 0.5
assert compare(easings.quadratic_ease_in_out(0.4), 0.32)
assert float64.tolerance(easings.quadratic_ease_in_out(0.4), 0.32, 1e-5)
// p >= 0.5
assert compare(easings.quadratic_ease_in_out(0.6), 0.68)
assert float64.tolerance(easings.quadratic_ease_in_out(0.6), 0.68, 1e-5)
}

// Cubic tests
fn test_cubic_ease_in() {
assert compare(easings.cubic_ease_in(3), 27.0)
assert float64.tolerance(easings.cubic_ease_in(3), 27.0, 1e-5)
}

fn test_cubic_ease_out() {
assert compare(easings.cubic_ease_out(4), 28.0)
assert float64.tolerance(easings.cubic_ease_out(4), 28.0, 1e-5)
}

fn test_cubic_ease_in_out() {
// p < 0.5
assert compare(easings.cubic_ease_in_out(0.4), 0.256)
assert float64.tolerance(easings.cubic_ease_in_out(0.4), 0.256, 1e-5)
// p >= 0.5
assert compare(easings.cubic_ease_in_out(0.6), 0.744)
assert float64.tolerance(easings.cubic_ease_in_out(0.6), 0.744, 1e-5)
}

// Quadratic tests
fn test_quartic_ease_in() {
assert compare(easings.quartic_ease_in(3), 81.0)
assert float64.tolerance(easings.quartic_ease_in(3), 81.0, 1e-5)
}

fn test_quartic_ease_out() {
assert compare(easings.quartic_ease_out(3), -15.0)
assert float64.tolerance(easings.quartic_ease_out(3), -15.0, 1e-5)
}

fn test_quartic_ease_in_out() {
// p < 0.5
assert compare(easings.quartic_ease_in_out(0.4), 0.2048)
assert float64.tolerance(easings.quartic_ease_in_out(0.4), 0.2048, 1e-5)
// p >= 0.5
assert compare(easings.quartic_ease_in_out(0.6), 0.7952)
assert float64.tolerance(easings.quartic_ease_in_out(0.6), 0.7952, 1e-5)
}

// Quintic tests
fn test_quintic_ease_in() {
assert compare(easings.quintic_ease_in(4), 1024.0)
assert float64.tolerance(easings.quintic_ease_in(4), 1024.0, 1e-5)
}

fn test_quintic_ease_out() {
assert compare(easings.quintic_ease_out(4), 244.0)
assert float64.tolerance(easings.quintic_ease_out(4), 244.0, 1e-5)
}

fn test_quintic_ease_in_out() {
// p < 0.5
assert compare(easings.quintic_ease_in_out(0.4), 0.16384)
assert float64.tolerance(easings.quintic_ease_in_out(0.4), 0.16384, 1e-5)
// p >= 0.5
assert compare(easings.quintic_ease_in_out(0.6), 0.83616)
assert float64.tolerance(easings.quintic_ease_in_out(0.6), 0.83616, 1e-5)
}

// Sine tests
fn test_sine_ease_in() {
assert compare(easings.sine_ease_in(3), 1.0)
assert float64.tolerance(easings.sine_ease_in(3), 1.0, 1e-5)
}

fn test_sine_ease_out() {
assert compare(easings.sine_ease_out(3), 0.000000)
assert float64.tolerance(easings.sine_ease_out(3), 0.000000, 1e-5)
}

fn test_sine_ease_in_out() {
assert compare(easings.sine_ease_in_out(3), 1.0)
assert float64.tolerance(easings.sine_ease_in_out(3), 1.0, 1e-5)
}

// Circular tests
fn test_circular_ease_in() {
assert compare(easings.circular_ease_in(0.4), 0.083485)
assert float64.tolerance(easings.circular_ease_in(0.4), 0.083485, 1e-5)
}

fn test_circular_ease_out() {
assert compare(easings.circular_ease_out(0.4), 0.80)
assert float64.tolerance(easings.circular_ease_out(0.4), 0.80, 1e-5)
}

fn test_circular_ease_in_out() {
// p < 0.5
assert compare(easings.circular_ease_in_out(0.4), 0.20)
assert float64.tolerance(easings.circular_ease_in_out(0.4), 0.20, 1e-5)
// p >= 0.5
assert compare(easings.circular_ease_in_out(0.6), 0.80)
assert float64.tolerance(easings.circular_ease_in_out(0.6), 0.80, 1e-5)
}

// Exponential tests
fn test_exponential_ease_in() {
assert compare(easings.exponential_ease_in(2), 1024.0)
assert float64.tolerance(easings.exponential_ease_in(2), 1024.0, 1e-5)
}

fn test_exponential_ease_out() {
assert compare(easings.exponential_ease_out(2), 0.999999)
assert float64.tolerance(easings.exponential_ease_out(2), 0.999999, 1e-5)
}

fn test_exponential_ease_in_out() {
// p = 0
assert compare(easings.exponential_ease_in_out(0), 0.0)
assert float64.tolerance(easings.exponential_ease_in_out(0), 0.0, 1e-5)
// p = 1
assert compare(easings.exponential_ease_in_out(1), 1.0)
assert float64.tolerance(easings.exponential_ease_in_out(1), 1.0, 1e-5)
// p < 0.5
assert compare(easings.exponential_ease_in_out(0.4), 0.125)
assert float64.tolerance(easings.exponential_ease_in_out(0.4), 0.125, 1e-5)
// p >= 0.5
assert compare(easings.exponential_ease_in_out(0.6), 0.875)
assert float64.tolerance(easings.exponential_ease_in_out(0.6), 0.875, 1e-5)
}

// Elastic tests
fn test_elastic_ease_in() {
assert compare(easings.elastic_ease_in(2), 0.0)
assert float64.tolerance(easings.elastic_ease_in(2), 0.0, 1e-5)
}

fn test_elastic_ease_out() {
assert compare(easings.elastic_ease_out(2), 1.0)
assert float64.tolerance(easings.elastic_ease_out(2), 1.0, 1e-5)
}

fn test_elastic_ease_in_out() {
// p < 0.5
assert compare(easings.elastic_ease_in_out(0.4), 0.073473)
assert float64.tolerance(easings.elastic_ease_in_out(0.4), 0.073473, 1e-5)
// p >= 0.5
assert compare(easings.elastic_ease_in_out(0.6), 1.073473)
assert float64.tolerance(easings.elastic_ease_in_out(0.6), 1.073473, 1e-5)
}

// Back tests
fn test_back_ease_in() {
assert compare(easings.back_ease_in(2), 8.0)
assert float64.tolerance(easings.back_ease_in(2), 8.0, 1e-5)
}

fn test_back_ease_out() {
assert compare(easings.back_ease_out(2), 2.0)
assert float64.tolerance(easings.back_ease_out(2), 2.0, 1e-5)
}

fn test_back_ease_in_out() {
// p < 0.5
assert compare(easings.back_ease_in_out(0.4), 0.020886)
assert float64.tolerance(easings.back_ease_in_out(0.4), 0.020886, 1e-5)
// p >= 0.5
assert compare(easings.back_ease_in_out(0.6), 0.979114)
assert float64.tolerance(easings.back_ease_in_out(0.6), 0.979114, 1e-5)
}

// Bounce tests
fn test_bounce_ease_in() {
assert compare(easings.bounce_ease_in(2), -6.562500)
assert float64.tolerance(easings.bounce_ease_in(2), -6.562500, 1e-5)
}

fn test_bounce_ease_out() {
// p < 4 / 11.0
assert compare(easings.bounce_ease_out(0.35), 0.926406)
assert float64.tolerance(easings.bounce_ease_out(0.35), 0.926406, 1e-5)
// p < 8.0 / 11.0
assert compare(easings.bounce_ease_out(0.70), 0.916750)
assert float64.tolerance(easings.bounce_ease_out(0.70), 0.916750, 1e-5)
// p < 9.0 / 10.0
assert compare(easings.bounce_ease_out(0.89), 0.980365)
assert float64.tolerance(easings.bounce_ease_out(0.89), 0.980365, 1e-5)
// p >= 9.0 / 10.0
assert compare(easings.bounce_ease_out(0.91), 0.990280)
assert float64.tolerance(easings.bounce_ease_out(0.91), 0.990280, 1e-5)
}

fn test_bounce_ease_in_out() {
// p < 0.5
assert compare(easings.bounce_ease_in_out(0.4), 0.348750)
assert float64.tolerance(easings.bounce_ease_in_out(0.4), 0.348750, 1e-5)
// p >= 0.5
assert compare(easings.bounce_ease_in_out(0.6), 0.651250)
}

// Helper method for comparing floats
fn compare(x f64, y f64) bool {
tolerance := 1e-5
// Special case for zeroes
if x < tolerance && x > (-1.0 * tolerance) && y < tolerance && y > (-1.0 * tolerance) {
return true
}
diff := math.abs(x - y)
mean := math.abs(x + y) / 2.0
return if math.is_nan(diff / mean) { true } else { ((diff / mean) < tolerance) }
assert float64.tolerance(easings.bounce_ease_in_out(0.6), 0.651250, 1e-5)
}
Loading

0 comments on commit 7604e75

Please sign in to comment.