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 5359bb0 commit f709b7d
Show file tree
Hide file tree
Showing 22 changed files with 725 additions and 673 deletions.
30 changes: 16 additions & 14 deletions examples/deriv_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ fn pow(x f64, _ []f64) f64 {
return math.pow(x, 1.5)
}

f := func.Fn.new(f: pow)
println('f(x) = x^(3/2)')
fn main() {
f := func.Fn.new(f: pow)
println('f(x) = x^(3/2)')

mut expected := 1.5 * math.sqrt(2.0)
mut result, mut abserr := deriv.central(f, 2.0, 1e-8)
println('x = 2.0')
println("f'(x) = ${result} +/- ${abserr}")
println('exact = ${expected}')
mut expected := 1.5 * math.sqrt(2.0)
mut result, mut abserr := deriv.central(f, 2.0, 1e-8)
println('x = 2.0')
println("f'(x) = ${result} +/- ${abserr}")
println('exact = ${expected}')

assert float64.soclose(result, expected, abserr)
assert float64.soclose(result, expected, abserr)

expected = 0.0
result, abserr = deriv.forward(f, 0.0, 1e-8)
println('x = 0.0')
println("f'(x) = ${result} +/- ${abserr}")
println('exact = ${expected}')
expected = 0.0
result, abserr = deriv.forward(f, 0.0, 1e-8)
println('x = 0.0')
println("f'(x) = ${result} +/- ${abserr}")
println('exact = ${expected}')

assert float64.soclose(result, expected, abserr)
assert float64.soclose(result, expected, abserr)
}
30 changes: 16 additions & 14 deletions examples/diff_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ fn pow(x f64, _ []f64) f64 {
return math.pow(x, 1.5)
}

f := func.Fn.new(f: pow)
println('f(x) = x^(3/2)')
fn main() {
f := func.Fn.new(f: pow)
println('f(x) = x^(3/2)')

mut expected := 1.5 * math.sqrt(2.0)
mut result, mut abserr := diff.central(f, 2.0)
println('x = 2.0')
println("f'(x) = ${result} +/- ${abserr}")
println('exact = ${expected}')
mut expected := 1.5 * math.sqrt(2.0)
mut result, mut abserr := diff.central(f, 2.0)
println('x = 2.0')
println("f'(x) = ${result} +/- ${abserr}")
println('exact = ${expected}')

assert float64.soclose(result, expected, abserr)
assert float64.soclose(result, expected, abserr)

expected = 0.0
result, abserr = diff.forward(f, 0.0)
println('x = 0.0')
println("f'(x) = ${result} +/- ${abserr}")
println('exact = ${expected}')
expected = 0.0
result, abserr = diff.forward(f, 0.0)
println('x = 0.0')
println("f'(x) = ${result} +/- ${abserr}")
println('exact = ${expected}')

assert float64.soclose(result, expected, abserr)
assert float64.soclose(result, expected, abserr)
}
72 changes: 37 additions & 35 deletions examples/dist_histogram/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,45 @@ module main

import vsl.dist

lims := [0.0, 1, 2, 3, 4, 5]
fn main() {
lims := [0.0, 1, 2, 3, 4, 5]

mut hist := dist.Histogram.new(lims)
mut hist := dist.Histogram.new(lims)

values := [
0.0,
0.1,
0.2,
0.3,
0.9,
1,
1,
1,
1.2,
1.3,
1.4,
1.5,
1.99,
2,
2.5,
3,
3.5,
4.1,
4.5,
4.9,
-3,
-2,
-1,
5,
6,
7,
8,
]
values := [
0.0,
0.1,
0.2,
0.3,
0.9,
1,
1,
1,
1.2,
1.3,
1.4,
1.5,
1.99,
2,
2.5,
3,
3.5,
4.1,
4.5,
4.9,
-3,
-2,
-1,
5,
6,
7,
8,
]

hist.count(values, true)!
labels := hist.gen_labels('%g')!
hist.count(values, true)!
labels := hist.gen_labels('%g')!

hist_text := dist.text_hist(labels, hist.counts, 0)!
hist_text := dist.text_hist(labels, hist.counts, 0)!

println(hist_text)
println(hist_text)
}
18 changes: 10 additions & 8 deletions examples/io_h5_dataset/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import vsl.inout.h5
import math.stats
import rand

linedata := []f64{len: 21, init: (0 * index) + rand.f64()}
mut meanv := 0.0
hdffile := 'hdffile.h5'
fn main() {
linedata := []f64{len: 21, init: (0 * index) + rand.f64()}
mut meanv := 0.0
hdffile := 'hdffile.h5'

meanv = stats.mean(linedata)
meanv = stats.mean(linedata)

f := h5.Hdf5File.new(hdffile)!
f.write_dataset1d('/randdata', linedata)!
f.write_attribute('/randdata', 'mean', meanv)!
f.close()
f := h5.Hdf5File.new(hdffile)!
f.write_dataset1d('/randdata', linedata)!
f.write_attribute('/randdata', 'mean', meanv)!
f.close()
}
52 changes: 27 additions & 25 deletions examples/io_h5_relax/main.v
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import vsl.inout.h5
import math

// A simple 1d relaxation problem. Write the results
// and two attributes of the calculation to an HDF5 file.
// To see the results, use `h5dump ex1_hdffile.h5`
mut linedata := []f64{len: 21}
mut newv := 0.0
hdffile := 'ex1_hdffile.h5'
mut rounds := i32(0)
fn main() {
// A simple 1d relaxation problem. Write the results
// and two attributes of the calculation to an HDF5 file.
// To see the results, use `h5dump ex1_hdffile.h5`
mut linedata := []f64{len: 21}
mut newv := 0.0
hdffile := 'ex1_hdffile.h5'
mut rounds := i32(0)

linedata[0] = -2.0
linedata[20] = 3.0
mut maxdiff := 0.0
linedata[0] = -2.0
linedata[20] = 3.0
mut maxdiff := 0.0

for loop in 0 .. 1000 {
maxdiff = -math.max_f64
for i in 1 .. linedata.len - 1 {
newv = (linedata[i - 1] + linedata[i] + linedata[i + 1]) / 3.00
maxdiff = math.max(maxdiff, math.abs(newv - linedata[i]))
linedata[i] = newv
}
rounds = loop
if maxdiff < 0.0001 {
break
for loop in 0 .. 1000 {
maxdiff = -math.max_f64
for i in 1 .. linedata.len - 1 {
newv = (linedata[i - 1] + linedata[i] + linedata[i + 1]) / 3.00
maxdiff = math.max(maxdiff, math.abs(newv - linedata[i]))
linedata[i] = newv
}
rounds = loop
if maxdiff < 0.0001 {
break
}
}
f := h5.Hdf5File.new(hdffile)!
f.write_dataset1d('linedata', linedata)!
f.write_attribute('linedata', 'rounds', rounds)!
f.write_attribute('linedata', 'maxdiff', maxdiff)!
f.close()
}
f := h5.Hdf5File.new(hdffile)!
f.write_dataset1d('linedata', linedata)!
f.write_attribute('linedata', 'rounds', rounds)!
f.write_attribute('linedata', 'maxdiff', maxdiff)!
f.close()
18 changes: 13 additions & 5 deletions examples/iter_lazy_generation/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ module main

import vsl.iter

data := [1.0, 2.0, 3.0]
r := 3
mut combs := iter.CombinationsIter.new(data, r)
for comb in combs {
print(comb)
fn main() {
data := [1.0, 2.0, 3.0]
r := 3
mut count := 0

mut combs := iter.CombinationsIter.new(data, r)

for comb in combs {
print(comb)
count += 1
}

assert count == 3
}
32 changes: 17 additions & 15 deletions examples/la_triplet01/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ module main

import vsl.la

mut a := la.Triplet.new[f64](4, 4, 6)
fn main() {
mut a := la.Triplet.new[f64](4, 4, 6)

a.put(1, 0, 1.0)!
a.put(0, 1, 2.0)!
a.put(3, 1, 3.0)!
a.put(1, 2, 4.0)!
a.put(2, 3, 5.0)!
a.put(3, 3, 6.0)!
a.put(1, 0, 1.0)!
a.put(0, 1, 2.0)!
a.put(3, 1, 3.0)!
a.put(1, 2, 4.0)!
a.put(2, 3, 5.0)!
a.put(3, 3, 6.0)!

mut expected_matrix := la.Matrix.deep2([
[0.0, 2, 0, 0],
[1.0, 0, 4, 0],
[0.0, 0, 0, 5],
[0.0, 3, 0, 6],
])
mut expected_matrix := la.Matrix.deep2([
[0.0, 2, 0, 0],
[1.0, 0, 4, 0],
[0.0, 0, 0, 5],
[0.0, 3, 0, 6],
])

m := a.to_dense()
m := a.to_dense()

assert expected_matrix.equals(m)
assert expected_matrix.equals(m)
}
82 changes: 42 additions & 40 deletions examples/ml_kmeans/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,51 @@ module main

import vsl.ml

// data
mut data := ml.Data.from_raw_x([
[0.1, 0.7],
[0.3, 0.7],
[0.1, 0.9],
[0.3, 0.9],
[0.7, 0.1],
[0.9, 0.1],
[0.7, 0.3],
[0.9, 0.3],
])!
fn main() {
// data
mut data := ml.Data.from_raw_x([
[0.1, 0.7],
[0.3, 0.7],
[0.1, 0.9],
[0.3, 0.9],
[0.7, 0.1],
[0.9, 0.1],
[0.7, 0.3],
[0.9, 0.3],
])!

// model
nb_classes := 2
mut model := ml.Kmeans.new(mut data, nb_classes, 'kmeans')
model.set_centroids([
// class 0
[0.4, 0.6],
// class 1
[0.6, 0.4],
])
// model
nb_classes := 2
mut model := ml.Kmeans.new(mut data, nb_classes, 'kmeans')
model.set_centroids([
// class 0
[0.4, 0.6],
// class 1
[0.6, 0.4],
])

// initial classes
model.find_closest_centroids()
// initial classes
model.find_closest_centroids()

// initial computation of centroids
model.compute_centroids()
// initial computation of centroids
model.compute_centroids()

// train
model.train(epochs: 6)
// train
model.train(epochs: 6)

// test
expected_classes := [
0,
0,
0,
0,
1,
1,
1,
1,
]
for i, c in model.classes {
assert c == expected_classes[i]
println('class ${i}: ${c}')
// test
expected_classes := [
0,
0,
0,
0,
1,
1,
1,
1,
]
for i, c in model.classes {
assert c == expected_classes[i]
println('class ${i}: ${c}')
}
}
Loading

0 comments on commit f709b7d

Please sign in to comment.