Skip to content

Commit

Permalink
Updated constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Oct 17, 2023
1 parent 78f987b commit 12613bb
Show file tree
Hide file tree
Showing 91 changed files with 324 additions and 368 deletions.
24 changes: 12 additions & 12 deletions deriv/deriv_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ fn df6(x f64, _ []f64) f64 {
}

fn test_deriv() {
f1_ := func.new_func(f: f1)
df1_ := func.new_func(f: df1)
f2_ := func.new_func(f: f2)
df2_ := func.new_func(f: df2)
f3_ := func.new_func(f: f3)
df3_ := func.new_func(f: df3)
f4_ := func.new_func(f: f4)
df4_ := func.new_func(f: df4)
f5_ := func.new_func(f: f5)
df5_ := func.new_func(f: df5)
f6_ := func.new_func(f: f6)
df6_ := func.new_func(f: df6)
f1_ := func.Fn.new(f: f1)
df1_ := func.Fn.new(f: df1)
f2_ := func.Fn.new(f: f2)
df2_ := func.Fn.new(f: df2)
f3_ := func.Fn.new(f: f3)
df3_ := func.Fn.new(f: df3)
f4_ := func.Fn.new(f: f4)
df4_ := func.Fn.new(f: df4)
f5_ := func.Fn.new(f: f5)
df5_ := func.Fn.new(f: df5)
f6_ := func.Fn.new(f: f6)
df6_ := func.Fn.new(f: df6)

assert deriv_test('central', f1_, df1_, 1.0)
assert deriv_test('forward', f1_, df1_, 1.0)
Expand Down
24 changes: 12 additions & 12 deletions diff/diff_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ fn df6(x f64, _ []f64) f64 {
}

fn test_diff() {
f1_ := func.new_func(f: f1)
df1_ := func.new_func(f: df1)
f2_ := func.new_func(f: f2)
df2_ := func.new_func(f: df2)
f3_ := func.new_func(f: f3)
df3_ := func.new_func(f: df3)
f4_ := func.new_func(f: f4)
df4_ := func.new_func(f: df4)
f5_ := func.new_func(f: f5)
df5_ := func.new_func(f: df5)
f6_ := func.new_func(f: f6)
df6_ := func.new_func(f: df6)
f1_ := func.Fn.new(f: f1)
df1_ := func.Fn.new(f: df1)
f2_ := func.Fn.new(f: f2)
df2_ := func.Fn.new(f: df2)
f3_ := func.Fn.new(f: f3)
df3_ := func.Fn.new(f: df3)
f4_ := func.Fn.new(f: f4)
df4_ := func.Fn.new(f: df4)
f5_ := func.Fn.new(f: f5)
df5_ := func.Fn.new(f: df5)
f6_ := func.Fn.new(f: f6)
df6_ := func.Fn.new(f: df6)

assert diff_test('central', f1_, df1_, 1.0)
assert diff_test('forward', f1_, df1_, 1.0)
Expand Down
6 changes: 3 additions & 3 deletions dist/hist.v
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn text_hist(labels []string, counts []int, barlen int) !string {

// build_text_hist builds a text histogram
pub fn build_text_hist(xmin f64, xmax f64, nstations int, values []f64, numfmt string, barlen int) !string {
mut hist := new_histogram(util.lin_space(xmin, xmax, nstations))
mut hist := Histogram.new(util.lin_space(xmin, xmax, nstations))
hist.count(values, true)!
labels := hist.gen_labels(numfmt)!
return text_hist(labels, hist.counts, barlen)
Expand All @@ -72,8 +72,8 @@ pub mut:
counts []int // counts
}

// new_histogram returns an histogram struct from a given list of stations
pub fn new_histogram(stations []f64) &Histogram {
// Histogram.new returns an histogram struct from a given list of stations
pub fn Histogram.new(stations []f64) &Histogram {
return &Histogram{
stations: stations
}
Expand Down
2 changes: 1 addition & 1 deletion dist/hist_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module dist

fn test_hist() {
lims := [0.0, 1, 2, 3, 4, 5]
mut hist := new_histogram(lims)
mut hist := Histogram.new(lims)

mut idx := hist.find_bin(-3.3)!
assert idx == -1
Expand Down
4 changes: 2 additions & 2 deletions examples/data_analysis_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import vsl.plot

fn main() {
// Example data: two features (X1 and X2) and a label (y)
mut data := ml.data_from_raw_xy([
mut data := ml.Data.from_raw_xy([
[1.0, 2.0, 0.0],
[2.0, 3.0, 0.0],
[3.0, 3.0, 0.0],
Expand Down Expand Up @@ -127,7 +127,7 @@ fn main() {
plt_3d.show()!

// Basic statistics analysis
mut stat := ml.stat_from_data(mut data, 'Example Data')
mut stat := ml.Stats.from_data(mut data, 'Example Data')
stat.update()

// Visualize statistics in a bar chart
Expand Down
2 changes: 1 addition & 1 deletion examples/deriv_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn pow(x f64, _ []f64) f64 {
return math.pow(x, 1.5)
}

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

mut expected := 1.5 * math.sqrt(2.0)
Expand Down
2 changes: 1 addition & 1 deletion examples/diff_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn pow(x f64, _ []f64) f64 {
return math.pow(x, 1.5)
}

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

mut expected := 1.5 * math.sqrt(2.0)
Expand Down
2 changes: 1 addition & 1 deletion examples/dist_histogram/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import vsl.dist

lims := [0.0, 1, 2, 3, 4, 5]

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

values := [
0.0,
Expand Down
2 changes: 1 addition & 1 deletion examples/io_h5_dataset/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ hdffile := 'hdffile.h5'

meanv = stats.mean(linedata)

f := h5.new_file(hdffile)!
f := h5.Hdf5File.new(hdffile)!
f.write_dataset1d('/randdata', linedata)!
f.write_attribute('/randdata', 'mean', meanv)!
f.close()
2 changes: 1 addition & 1 deletion examples/io_h5_relax/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ for loop in 0 .. 1000 {
break
}
}
f := h5.new_file(hdffile)!
f := h5.Hdf5File.new(hdffile)!
f.write_dataset1d('linedata', linedata)!
f.write_attribute('linedata', 'rounds', rounds)!
f.write_attribute('linedata', 'maxdiff', maxdiff)!
Expand Down
2 changes: 1 addition & 1 deletion examples/iter_lazy_generation/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import vsl.iter

data := [1.0, 2.0, 3.0]
r := 3
mut combs := iter.new_combinations_iter(data, r)
mut combs := iter.CombinationsIter.new(data, r)
for comb in combs {
print(comb)
}
4 changes: 2 additions & 2 deletions examples/la_triplet01/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module main

import vsl.la

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

a.put(1, 0, 1.0)!
a.put(0, 1, 2.0)!
Expand All @@ -11,7 +11,7 @@ a.put(1, 2, 4.0)!
a.put(2, 3, 5.0)!
a.put(3, 3, 6.0)!

mut expected_matrix := la.matrix_deep2([
mut expected_matrix := la.Matrix.deep2([
[0.0, 2, 0, 0],
[1.0, 0, 4, 0],
[0.0, 0, 0, 5],
Expand Down
4 changes: 2 additions & 2 deletions examples/ml_kmeans/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module main
import vsl.ml

// data
mut data := ml.data_from_raw_x([
mut data := ml.Data.from_raw_x([
[0.1, 0.7],
[0.3, 0.7],
[0.1, 0.9],
Expand All @@ -16,7 +16,7 @@ mut data := ml.data_from_raw_x([

// model
nb_classes := 2
mut model := ml.new_kmeans(mut data, nb_classes, 'kmeans')
mut model := ml.Kmeans.new(mut data, nb_classes, 'kmeans')
model.set_centroids([
// class 0
[0.4, 0.6],
Expand Down
4 changes: 2 additions & 2 deletions examples/ml_kmeans_plot/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import vsl.ml
import internal.dataset

// data
mut data := ml.data_from_raw_x(dataset.raw_dataset.map([it[0], it[1]]))!
mut data := ml.Data.from_raw_x(dataset.raw_dataset.map([it[0], it[1]]))!

// model
nb_classes := 3
mut model := ml.new_kmeans(mut data, nb_classes, 'kmeans')
mut model := ml.Kmeans.new(mut data, nb_classes, 'kmeans')
model.set_centroids([
// class 0
[3.0, 3],
Expand Down
4 changes: 2 additions & 2 deletions examples/ml_knn_plot/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ fn main() {
[7.0, 6.0],
]
y := [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0]
mut data := ml.data_from_raw_xy_sep(x, y)!
mut data := ml.Data.from_raw_xy_sep(x, y)!

// Create a KNN model
mut knn := ml.new_knn(mut data, 'Example KNN')!
mut knn := ml.KNN.new(mut data, 'Example KNN')!

// Set weights to give more importance to class 1
weights := {
Expand Down
4 changes: 2 additions & 2 deletions examples/ml_linreg01/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ xy := [
[1.43, 94.98],
[0.95, 87.33],
]
mut data := ml.data_from_raw_xy(xy)!
mut reg := ml.new_lin_reg(mut data, 'linear regression')
mut data := ml.Data.from_raw_xy(xy)!
mut reg := ml.LinReg.new(mut data, 'linear regression')

reg.train()
// TODO: Fix this test
Expand Down
4 changes: 2 additions & 2 deletions examples/ml_linreg02/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ xy := [
[1.43, 94.98],
[0.95, 87.33],
]
mut data := ml.data_from_raw_xy(xy)!
mut reg := ml.new_lin_reg(mut data, 'linear regression')
mut data := ml.Data.from_raw_xy(xy)!
mut reg := ml.LinReg.new(mut data, 'linear regression')

reg.train()

Expand Down
4 changes: 2 additions & 2 deletions examples/ml_linreg_plot/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fn main() {
[1.43, 94.98],
[0.95, 87.33],
]
mut data := ml.data_from_raw_xy(xy)!
mut reg := ml.new_lin_reg(mut data, 'linear regression')
mut data := ml.Data.from_raw_xy(xy)!
mut reg := ml.LinReg.new(mut data, 'linear regression')

reg.train()

Expand Down
34 changes: 0 additions & 34 deletions examples/ml_multilinreg_plot/main.v

This file was deleted.

10 changes: 5 additions & 5 deletions examples/ml_sentiment_analysis/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ for row in dataset {
// Well, stemming is keeping the radicals of words so that terms such
// as "try", "tried" and "trying" are considered the same token: "try".
// Let's create a stemmer:
mut lancaster := nlp.new_lancaster_stemmer(true) // Parameter is strip_prefix. If true, "kilogram" becomes "gram", for example.
mut lancaster := nlp.LancasterStemmer.new(true) // Parameter is strip_prefix. If true, "kilogram" becomes "gram", for example.

// List of sentences as ngrams, read the comments
// below to understand.
Expand Down Expand Up @@ -110,8 +110,8 @@ for i in 0 .. dataset.len {

// Amazing! We have all we need to train a sentiment analysis model with
// bag of words. Check it out:
mut training_data := ml.data_from_raw_xy_sep(vectorized, labels)!
mut bow_knn := ml.new_knn(mut training_data, 'BagOfWordsKNN')!
mut training_data := ml.Data.from_raw_xy_sep(vectorized, labels)!
mut bow_knn := ml.KNN.new(mut training_data, 'BagOfWordsKNN')!

sentence1 := 'I think today is a good day' // should be positive
sentence2 := 'I hate grape juice, it tastes bad.' // should be negative
Expand Down Expand Up @@ -175,8 +175,8 @@ for sent in ngrams {
tf_idf_rows << tf_idf_sentence
}

training_data = ml.data_from_raw_xy_sep(tf_idf_rows, labels)!
mut tf_idf_knn := ml.new_knn(mut training_data, 'TfIdfKNN')!
training_data = ml.Data.from_raw_xy_sep(tf_idf_rows, labels)!
mut tf_idf_knn := ml.KNN.new(mut training_data, 'TfIdfKNN')!

tfidf := fn (sent string, mut lan nlp.LancasterStemmer, document [][][]string, unique [][]string) ![]f64 {
sent_tokenized := nlp.remove_stopwords_en(nlp.tokenize(nlp.remove_punctuation(sent).to_lower()),
Expand Down
2 changes: 1 addition & 1 deletion examples/mpi_basic_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ for i := start; i < endp1; i++ {
}

// Communicator
comm := mpi.new_communicator([])!
comm := mpi.Communicator.new([])!

// Barrier
comm.barrier()
Expand Down
4 changes: 2 additions & 2 deletions examples/mpi_bcast_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ for i := start; i < endp1; i++ {
- `mpi.finalize()`: Cleanup and finalize MPI at the end of the program.
- `mpi.world_rank()`: Get the rank of the current process.
- `mpi.world_size()`: Get the total number of processes.
- `mpi.new_communicator([])`: Create a new communicator.
- `mpi.Communicator.new([])`: Create a new communicator.
Now, let's dive into the MPI functions used in the example.

Expand All @@ -125,7 +125,7 @@ Now, let's dive into the MPI functions used in the example.
Synchronize all processes:

```v ignore
comm := mpi.new_communicator([])!
comm := mpi.Communicator.new([])!
comm.barrier()
```

Expand Down
2 changes: 1 addition & 1 deletion examples/mpi_bcast_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ for i := start; i < endp1; i++ {
}

// Communicator
comm := mpi.new_communicator([])!
comm := mpi.Communicator.new([])!

// Barrier
comm.barrier()
Expand Down
4 changes: 2 additions & 2 deletions examples/roots_bisection_solver/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ fn f_cos(x f64, _ []f64) f64 {
return math.cos(x)
}

f := func.new_func(f: f_cos)
f := func.Fn.new(f: f_cos)

mut solver := roots.new_bisection(f)
mut solver := roots.Bisection.new(f)

solver.xmin = 0.0
solver.xmax = 3.0
Expand Down
4 changes: 2 additions & 2 deletions float/float32/axpy_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn test_axpy_inc() {
for test in float32.axpy_tests {
n := test.x.len

for inc in new_inc_set(-7, -4, -3, -2, -1, 1, 2, 3, 4, 7) {
for inc in IncSet.new(-7, -4, -3, -2, -1, 1, 2, 3, 4, 7) {
mut ix := 0
mut iy := 0
if inc.x < 0 {
Expand Down Expand Up @@ -193,7 +193,7 @@ fn test_axpy_inc_to() {
for test in float32.axpy_tests {
n := test.x.len

for inc in new_inc_to_set(-7, -4, -3, -2, -1, 1, 2, 3, 4, 7) {
for inc in IncToSet.new(-7, -4, -3, -2, -1, 1, 2, 3, 4, 7) {
mut ix := 0
mut iy := 0
mut idst := u32(0)
Expand Down
Loading

0 comments on commit 12613bb

Please sign in to comment.