From 12613bb5660dff329e52af44288e0a72556f4694 Mon Sep 17 00:00:00 2001 From: ulises-jeremias Date: Tue, 17 Oct 2023 01:12:16 -0300 Subject: [PATCH] Updated constructors --- deriv/deriv_test.v | 24 +++++------ diff/diff_test.v | 24 +++++------ dist/hist.v | 6 +-- dist/hist_test.v | 2 +- examples/data_analysis_example/main.v | 4 +- examples/deriv_example/main.v | 2 +- examples/diff_example/main.v | 2 +- examples/dist_histogram/main.v | 2 +- examples/io_h5_dataset/main.v | 2 +- examples/io_h5_relax/main.v | 2 +- examples/iter_lazy_generation/main.v | 2 +- examples/la_triplet01/main.v | 4 +- examples/ml_kmeans/main.v | 4 +- examples/ml_kmeans_plot/main.v | 4 +- examples/ml_knn_plot/main.v | 4 +- examples/ml_linreg01/main.v | 4 +- examples/ml_linreg02/main.v | 4 +- examples/ml_linreg_plot/main.v | 4 +- examples/ml_multilinreg_plot/main.v | 34 --------------- examples/ml_sentiment_analysis/main.v | 10 ++--- examples/mpi_basic_example/main.v | 2 +- examples/mpi_bcast_example/README.md | 4 +- examples/mpi_bcast_example/main.v | 2 +- examples/roots_bisection_solver/main.v | 4 +- float/float32/axpy_test.v | 4 +- float/float32/f32_test_util.v | 12 +++--- float/float32/scal_test.v | 2 +- float/float64/axpy_test.v | 4 +- float/float64/f64_test_util.v | 12 +++--- float/float64/gemv_test.v | 2 +- float/float64/scal_test.v | 2 +- fun/datainterp.v | 4 +- fun/datainterp_test.v | 4 +- fun/interpcubic.v | 4 +- fun/interpcubic_test.v | 8 ++-- fun/interpquad.v | 4 +- fun/interpquad_test.v | 8 ++-- fun/sinusoid.v | 8 ++-- fun/sinusoid_test.v | 4 +- func/func.v | 12 +++--- gm/basicgm.v | 16 +++---- gm/bins.v | 8 ++-- graph/README.md | 4 +- graph/graph.v | 4 +- graph/graph_test.v | 4 +- inout/h5/hdf5_nix.c.v | 4 +- inout/h5/readhdf5_test.v | 2 +- inout/h5/three_d_test.v | 2 +- inout/h5/two_d_test.v | 2 +- inout/h5/typeshdf5_test.v | 2 +- iter/README.md | 4 +- iter/comb.v | 12 +++--- iter/inf_iters.v | 6 +-- iter/inf_iters_test.v | 14 +++--- iter/perm.v | 6 +-- iter/prod.v | 4 +- iter/ranges.v | 18 ++++---- iter/ranges_test.v | 12 ++---- la/blas.v | 6 +-- la/blas_test.v | 2 +- la/densesol.v | 2 +- la/densesol_test.v | 4 +- la/jacobi_test.v | 6 +-- la/matrix.v | 30 ++++++------- la/matrix_ops.v | 6 +-- la/matrix_ops_test.v | 26 +++++------ la/matrix_test.v | 60 +++++++++++++------------- la/sparse_config.v | 8 ++-- la/sparse_matrix.v | 6 +-- la/sparse_matrix_test.v | 4 +- ml/data.v | 32 +++++++------- ml/data_test.v | 4 +- ml/kmeans.v | 8 ++-- ml/kmeans_test.v | 4 +- ml/knn.v | 6 +-- ml/knn_test.v | 14 +++--- ml/linreg.v | 12 +++--- ml/linreg_test.v | 8 ++-- ml/logreg.v~ | 10 ++--- ml/nlp/lancaster_stemmer.v | 4 +- ml/paramsreg.v | 4 +- ml/paramsreg_test.v | 2 +- ml/workspace.v | 2 +- ml/workspace_test.v | 4 +- mpi/mpi_default.c.v | 4 +- mpi/mpi_nix.c.v | 4 +- roots/bisection.v | 4 +- roots/brent.v | 4 +- roots/roots_test.v | 6 +-- vcl/kernel.c.v | 16 +++---- vcl/vcl.c.v | 6 +-- 91 files changed, 324 insertions(+), 368 deletions(-) delete mode 100644 examples/ml_multilinreg_plot/main.v diff --git a/deriv/deriv_test.v b/deriv/deriv_test.v index c4f3c42ee..f455642b9 100644 --- a/deriv/deriv_test.v +++ b/deriv/deriv_test.v @@ -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) diff --git a/diff/diff_test.v b/diff/diff_test.v index fa4c63b41..ec4bbe54c 100644 --- a/diff/diff_test.v +++ b/diff/diff_test.v @@ -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) diff --git a/dist/hist.v b/dist/hist.v index ee9a1f1f9..4ad416316 100644 --- a/dist/hist.v +++ b/dist/hist.v @@ -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) @@ -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 } diff --git a/dist/hist_test.v b/dist/hist_test.v index dd7b9a06b..90ddfbd38 100644 --- a/dist/hist_test.v +++ b/dist/hist_test.v @@ -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 diff --git a/examples/data_analysis_example/main.v b/examples/data_analysis_example/main.v index cbfa53eee..a0c93500f 100644 --- a/examples/data_analysis_example/main.v +++ b/examples/data_analysis_example/main.v @@ -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], @@ -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 diff --git a/examples/deriv_example/main.v b/examples/deriv_example/main.v index 8af660af7..785992cb3 100644 --- a/examples/deriv_example/main.v +++ b/examples/deriv_example/main.v @@ -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) diff --git a/examples/diff_example/main.v b/examples/diff_example/main.v index f45b2fec1..02b1a8743 100644 --- a/examples/diff_example/main.v +++ b/examples/diff_example/main.v @@ -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) diff --git a/examples/dist_histogram/main.v b/examples/dist_histogram/main.v index dce40162d..60d9f12b4 100644 --- a/examples/dist_histogram/main.v +++ b/examples/dist_histogram/main.v @@ -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, diff --git a/examples/io_h5_dataset/main.v b/examples/io_h5_dataset/main.v index 3043f4e17..8628597ab 100644 --- a/examples/io_h5_dataset/main.v +++ b/examples/io_h5_dataset/main.v @@ -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() diff --git a/examples/io_h5_relax/main.v b/examples/io_h5_relax/main.v index 5e00764ea..7043fdcb4 100644 --- a/examples/io_h5_relax/main.v +++ b/examples/io_h5_relax/main.v @@ -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)! diff --git a/examples/iter_lazy_generation/main.v b/examples/iter_lazy_generation/main.v index 47078dac8..9a5789d69 100644 --- a/examples/iter_lazy_generation/main.v +++ b/examples/iter_lazy_generation/main.v @@ -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) } diff --git a/examples/la_triplet01/main.v b/examples/la_triplet01/main.v index 893b1ce2b..280e6c18e 100644 --- a/examples/la_triplet01/main.v +++ b/examples/la_triplet01/main.v @@ -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)! @@ -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], diff --git a/examples/ml_kmeans/main.v b/examples/ml_kmeans/main.v index 2f328433d..a6f753d66 100644 --- a/examples/ml_kmeans/main.v +++ b/examples/ml_kmeans/main.v @@ -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], @@ -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], diff --git a/examples/ml_kmeans_plot/main.v b/examples/ml_kmeans_plot/main.v index a69441fd9..532e2f6c3 100644 --- a/examples/ml_kmeans_plot/main.v +++ b/examples/ml_kmeans_plot/main.v @@ -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], diff --git a/examples/ml_knn_plot/main.v b/examples/ml_knn_plot/main.v index 31b40b161..edb9bd670 100644 --- a/examples/ml_knn_plot/main.v +++ b/examples/ml_knn_plot/main.v @@ -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 := { diff --git a/examples/ml_linreg01/main.v b/examples/ml_linreg01/main.v index 12f1a329b..06ce7cd04 100644 --- a/examples/ml_linreg01/main.v +++ b/examples/ml_linreg01/main.v @@ -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 diff --git a/examples/ml_linreg02/main.v b/examples/ml_linreg02/main.v index 3fa1ca6fb..a35204023 100644 --- a/examples/ml_linreg02/main.v +++ b/examples/ml_linreg02/main.v @@ -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() diff --git a/examples/ml_linreg_plot/main.v b/examples/ml_linreg_plot/main.v index 59d7f3c15..b6847d9c9 100644 --- a/examples/ml_linreg_plot/main.v +++ b/examples/ml_linreg_plot/main.v @@ -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() diff --git a/examples/ml_multilinreg_plot/main.v b/examples/ml_multilinreg_plot/main.v deleted file mode 100644 index 796939dfa..000000000 --- a/examples/ml_multilinreg_plot/main.v +++ /dev/null @@ -1,34 +0,0 @@ -module main - -import vsl.ml - -fn main() { - xy := [ - [0.99, 90.01], - [1.02, 89.05], - [1.15, 91.43], - [1.29, 93.74], - [1.46, 96.73], - [1.36, 94.45], - [0.87, 87.59], - [1.23, 91.77], - [1.55, 99.42], - [1.40, 93.65], - [1.19, 93.54], - [1.15, 92.52], - [0.98, 90.56], - [1.01, 89.54], - [1.11, 89.85], - [1.20, 90.39], - [1.26, 93.25], - [1.32, 93.41], - [1.43, 94.98], - [0.95, 87.33], - ] - mut data := ml.data_from_raw_xy(xy)! - mut reg := ml.new_multi_lin_reg(mut data, 3, 'linear regression') - - reg.train() - - reg.plot()! -} diff --git a/examples/ml_sentiment_analysis/main.v b/examples/ml_sentiment_analysis/main.v index 2db2a6d8e..480161853 100644 --- a/examples/ml_sentiment_analysis/main.v +++ b/examples/ml_sentiment_analysis/main.v @@ -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. @@ -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 @@ -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()), diff --git a/examples/mpi_basic_example/main.v b/examples/mpi_basic_example/main.v index dd6709c17..c5b7ddd72 100644 --- a/examples/mpi_basic_example/main.v +++ b/examples/mpi_basic_example/main.v @@ -25,7 +25,7 @@ for i := start; i < endp1; i++ { } // Communicator -comm := mpi.new_communicator([])! +comm := mpi.Communicator.new([])! // Barrier comm.barrier() diff --git a/examples/mpi_bcast_example/README.md b/examples/mpi_bcast_example/README.md index b802b0793..e2b9e6ab9 100644 --- a/examples/mpi_bcast_example/README.md +++ b/examples/mpi_bcast_example/README.md @@ -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. @@ -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() ``` diff --git a/examples/mpi_bcast_example/main.v b/examples/mpi_bcast_example/main.v index dd6709c17..c5b7ddd72 100644 --- a/examples/mpi_bcast_example/main.v +++ b/examples/mpi_bcast_example/main.v @@ -25,7 +25,7 @@ for i := start; i < endp1; i++ { } // Communicator -comm := mpi.new_communicator([])! +comm := mpi.Communicator.new([])! // Barrier comm.barrier() diff --git a/examples/roots_bisection_solver/main.v b/examples/roots_bisection_solver/main.v index 142368f0c..3e83fd304 100644 --- a/examples/roots_bisection_solver/main.v +++ b/examples/roots_bisection_solver/main.v @@ -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 diff --git a/float/float32/axpy_test.v b/float/float32/axpy_test.v index b053734d6..baa93c165 100644 --- a/float/float32/axpy_test.v +++ b/float/float32/axpy_test.v @@ -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 { @@ -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) diff --git a/float/float32/f32_test_util.v b/float/float32/f32_test_util.v index 21de04f69..5c259e372 100644 --- a/float/float32/f32_test_util.v +++ b/float/float32/f32_test_util.v @@ -6,8 +6,8 @@ import math const ( // Offset sets for testing alignment handling in Unitary assembly functions align1 = [0, 1] - align2 = new_inc_set(0, 1) - align3 = new_inc_to_set(0, 1) + align2 = IncSet.new(0, 1) + align3 = IncToSet.new(0, 1) ) struct IncSet { @@ -15,8 +15,8 @@ struct IncSet { y int } -// new_inc_set will generate all (x,y) combinations of the input increment set. -fn new_inc_set(inc ...int) []IncSet { +// IncSet.new will generate all (x,y) combinations of the input increment set. +fn IncSet.new(inc ...int) []IncSet { n := inc.len mut inc_set := []IncSet{len: n * n} for i, x in inc { @@ -33,8 +33,8 @@ struct IncToSet { y int } -// new_inc_to_set will generate all (dst,x,y) combinations of the input increment set. -fn new_inc_to_set(inc ...int) []IncToSet { +// IncToSet.new will generate all (dst,x,y) combinations of the input increment set. +fn IncToSet.new(inc ...int) []IncToSet { n := inc.len mut inc_to_set := []IncToSet{len: n * n * n} for i, dst in inc { diff --git a/float/float32/scal_test.v b/float/float32/scal_test.v index da00ba218..8156cacf0 100644 --- a/float/float32/scal_test.v +++ b/float/float32/scal_test.v @@ -142,7 +142,7 @@ fn test_scal_inc_to() { for test in float32.scal_tests { n := test.x.len - for inc in new_inc_set(1, 2, 3, 4, 7, 10) { + for inc in IncSet.new(1, 2, 3, 4, 7, 10) { xg := guard_inc_vector(test.x, x_gd_val, inc.x, gd_ln) dg := guard_inc_vector(random_slice(n, 1), dst_gd_val, inc.y, gd_ln) x := xg[gd_ln..xg.len - gd_ln] diff --git a/float/float64/axpy_test.v b/float/float64/axpy_test.v index f93a55427..ec020f9b2 100644 --- a/float/float64/axpy_test.v +++ b/float/float64/axpy_test.v @@ -153,7 +153,7 @@ fn test_axpy_inc() { for test in float64.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 { @@ -193,7 +193,7 @@ fn test_axpy_inc_to() { for test in float64.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) diff --git a/float/float64/f64_test_util.v b/float/float64/f64_test_util.v index de72f0934..b93f171e6 100644 --- a/float/float64/f64_test_util.v +++ b/float/float64/f64_test_util.v @@ -6,8 +6,8 @@ import math const ( // Offset sets for testing alignment handling in Unitary assembly functions align1 = [0, 1] - align2 = new_inc_set(0, 1) - align3 = new_inc_to_set(0, 1) + align2 = IncSet.new(0, 1) + align3 = IncToSet.new(0, 1) ) struct IncSet { @@ -15,8 +15,8 @@ struct IncSet { y int } -// new_inc_set will generate all (x,y) combinations of the input increment set. -pub fn new_inc_set(inc ...int) []IncSet { +// IncSet.new will generate all (x,y) combinations of the input increment set. +pub fn IncSet.new(inc ...int) []IncSet { n := inc.len mut inc_set := []IncSet{len: n * n} for i, x in inc { @@ -33,8 +33,8 @@ struct IncToSet { y int } -// new_inc_to_set will generate all (dst,x,y) combinations of the input increment set. -pub fn new_inc_to_set(inc ...int) []IncToSet { +// IncToSet.new will generate all (dst,x,y) combinations of the input increment set. +pub fn IncToSet.new(inc ...int) []IncToSet { n := inc.len mut inc_to_set := []IncToSet{len: n * n * n} for i, dst in inc { diff --git a/float/float64/gemv_test.v b/float/float64/gemv_test.v index deb2da7be..3c4dd7a2b 100644 --- a/float/float64/gemv_test.v +++ b/float/float64/gemv_test.v @@ -565,7 +565,7 @@ fn dgemvcomp(mut test DgemvCase, trans bool, case DgemvSubcase) { assert equal_strided(test_x, x, 1) assert equal_strided(test.a, a, 1) - for j, inc in new_inc_set(-1, 1, 2, 3, 90) { + for j, inc in IncSet.new(-1, 1, 2, 3, 90) { mut want, mut incy := case.want.clone(), inc.y if inc.x < 0 && inc.y < 0 { want = case.want_rev_xy.clone() diff --git a/float/float64/scal_test.v b/float/float64/scal_test.v index 8ce3e5102..2226fc537 100644 --- a/float/float64/scal_test.v +++ b/float/float64/scal_test.v @@ -142,7 +142,7 @@ fn test_scal_inc_to() { for test in float64.scal_tests { n := test.x.len - for inc in new_inc_set(1, 2, 3, 4, 7, 10) { + for inc in IncSet.new(1, 2, 3, 4, 7, 10) { xg := guard_inc_vector(test.x, x_gd_val, inc.x, gd_ln) dg := guard_inc_vector(random_slice(n, 1), dst_gd_val, inc.y, gd_ln) x := xg[gd_ln..xg.len - gd_ln] diff --git a/fun/datainterp.v b/fun/datainterp.v index 4739a9949..0df301438 100644 --- a/fun/datainterp.v +++ b/fun/datainterp.v @@ -30,7 +30,7 @@ pub mut: dy f64 // error estimate } -// new_data_interp creates new interpolator for data point sets xx and yy (with same lengths) +// DataInterp.new creates new interpolator for data point sets xx and yy (with same lengths) // // type -- type of interpolator // "lin" : linear @@ -39,7 +39,7 @@ pub mut: // p -- order of interpolator // xx -- x-data // yy -- y-data -pub fn new_data_interp(itype string, p int, xx []f64, yy []f64) &DataInterp { +pub fn DataInterp.new(itype string, p int, xx []f64, yy []f64) &DataInterp { mut o := &DataInterp{ itype: itype } diff --git a/fun/datainterp_test.v b/fun/datainterp_test.v index 42d5d21d7..e30d5cf53 100644 --- a/fun/datainterp_test.v +++ b/fun/datainterp_test.v @@ -6,7 +6,7 @@ fn test_interp01() { xx := [0.0, 1, 2, 3, 4, 5] yy := [0.50, 0.20, 0.20, 0.05, 0.01, 0.00] - mut o := new_data_interp('lin', 1, xx, yy) + mut o := DataInterp.new('lin', 1, xx, yy) for i, x in xx { assert float64.veryclose(o.p(x), yy[i]) @@ -26,7 +26,7 @@ fn test_interp02() { yy := [0.50, 0.20, 0.20, 0.05, 0.01, 0.00] for p in [1, 2, 3] { - mut o := new_data_interp('poly', p, xx, yy) + mut o := DataInterp.new('poly', p, xx, yy) for i, x in xx { assert float64.veryclose(o.p(x), yy[i]) diff --git a/fun/interpcubic.v b/fun/interpcubic.v index cddbf31e7..e59ac9db1 100644 --- a/fun/interpcubic.v +++ b/fun/interpcubic.v @@ -16,8 +16,8 @@ pub mut: tol_den f64 } -// new_interp_cubic returns a new InterpCubic instance -pub fn new_interp_cubic() &InterpCubic { +// InterpCubic.new returns a new InterpCubic instance +pub fn InterpCubic.new() &InterpCubic { return &InterpCubic{ tol_den: 1e-15 } diff --git a/fun/interpcubic_test.v b/fun/interpcubic_test.v index 80f1e74f8..5a69b3ed3 100644 --- a/fun/interpcubic_test.v +++ b/fun/interpcubic_test.v @@ -16,7 +16,7 @@ fn test_interp_cubic_01() { x3, y3 := 12.0, 0.0 // intepolator - mut interp := new_interp_cubic() + mut interp := InterpCubic.new() interp.fit_4points(x0, y0, x1, y1, x2, y2, x3, y3)! // check model and derivatives @@ -49,7 +49,7 @@ fn test_interp_cubic_02() { y0, y1, y2, y3 := ycor(x0), ycor(x1), ycor(x2), ycor(x3) // intepolator - mut interp := new_interp_cubic() + mut interp := InterpCubic.new() interp.fit_4points(x0, y0, x1, y1, x2, y2, x3, y3)! // check model and derivatives @@ -80,7 +80,7 @@ fn test_interp_cubic_03() { y0, y1, y2, y3 := ycor(x0), ycor(x1), ycor(x2), ycor(x3) // intepolator - mut interp := new_interp_cubic() + mut interp := InterpCubic.new() interp.fit_4points(x0, y0, x1, y1, x2, y2, x3, y3)! // check model and derivatives @@ -112,7 +112,7 @@ fn test_interp_cubic_04() { x3, d3 := 8.0, 0.0 // intepolator - mut interp := new_interp_cubic() + mut interp := InterpCubic.new() interp.fit_3points_d(x0, y0, x1, y1, x2, y2, x3, d3)! // check model and derivatives diff --git a/fun/interpquad.v b/fun/interpquad.v index 958c89608..68db37427 100644 --- a/fun/interpquad.v +++ b/fun/interpquad.v @@ -15,8 +15,8 @@ pub mut: tol_den f64 } -// new_interp_quad returns a new InterpQuad instance -pub fn new_interp_quad() &InterpQuad { +// InterpQuad.new returns a new InterpQuad instance +pub fn InterpQuad.new() &InterpQuad { return &InterpQuad{ tol_den: 1e-15 } diff --git a/fun/interpquad_test.v b/fun/interpquad_test.v index ca61d1051..44baa7203 100644 --- a/fun/interpquad_test.v +++ b/fun/interpquad_test.v @@ -15,7 +15,7 @@ fn test_interp_quad_01() { x2, y2 := 3.0, 5.0 // intepolator - mut interp := new_interp_quad() + mut interp := InterpQuad.new() interp.fit_3points(x0, y0, x1, y1, x2, y2)! // check model and derivatives @@ -45,7 +45,7 @@ fn test_interp_quad_02() { x2, y2 := 3.0, -1.0 // intepolator - mut interp := new_interp_quad() + mut interp := InterpQuad.new() interp.fit_3points(x0, y0, x1, y1, x2, y2)! // check model and derivatives @@ -75,7 +75,7 @@ fn test_interp_quad_03() { x2, d2 := -1.0, -4.0 // intepolator - mut interp := new_interp_quad() + mut interp := InterpQuad.new() interp.fit_2points_d(x0, y0, x1, y1, x2, d2)! // check model and derivatives @@ -105,7 +105,7 @@ fn test_interp_quad_04() { x2, d2 := -1.0, 4.0 // intepolator - mut interp := new_interp_quad() + mut interp := InterpQuad.new() interp.fit_2points_d(x0, y0, x1, y1, x2, d2)! // check model and derivatives diff --git a/fun/sinusoid.v b/fun/sinusoid.v index 6996824c6..c01f2513e 100644 --- a/fun/sinusoid.v +++ b/fun/sinusoid.v @@ -28,12 +28,12 @@ pub mut: b []f64 // B0, b1, B2, ... (if series mode) } -// new_sinusoid_essential creates a new sinusoid object with the "essential" parameters set +// Sinusoid.essential creates a new sinusoid object with the "essential" parameters set // t -- period; e.g. [s] // a0 -- mean value; e.g. [m] // c1 -- amplitude; e.g. [m] // theta -- phase shift; e.g. [rad] -pub fn new_sinusoid_essential(t f64, a0 f64, c1 f64, theta f64) &Sinusoid { +pub fn Sinusoid.essential(t f64, a0 f64, c1 f64, theta f64) &Sinusoid { // input mut o := &Sinusoid{} o.period = t @@ -54,12 +54,12 @@ pub fn new_sinusoid_essential(t f64, a0 f64, c1 f64, theta f64) &Sinusoid { return o } -// new_sinusoid_basis creates a new sinusoid object with the "basis" parameters set +// Sinusoid.basis creates a new sinusoid object with the "basis" parameters set // t -- period; e.g. [s] // a0 -- mean value; e.g. [m] // a1 -- coefficient of the cos term // b1 -- coefficient of the sin term -pub fn new_sinusoid_basis(t f64, a0 f64, a1 f64, b1 f64) &Sinusoid { +pub fn Sinusoid.basis(t f64, a0 f64, a1 f64, b1 f64) &Sinusoid { // coefficients c1 := math.sqrt(a1 * a1 + b1 * b1) theta := math.atan2(-b1, a1) diff --git a/fun/sinusoid_test.v b/fun/sinusoid_test.v index af2bb798f..bf17b7459 100644 --- a/fun/sinusoid_test.v +++ b/fun/sinusoid_test.v @@ -11,8 +11,8 @@ fn test_sinusoid_01() { a0 := 1.7 // mean value c1 := 1.0 // amplitude theta := pi / 3.0 // phase shift [rad] - sa := new_sinusoid_essential(t, a0, c1, theta) - sb := new_sinusoid_basis(t, a0, sa.a[1], sa.b[1]) + sa := Sinusoid.essential(t, a0, c1, theta) + sb := Sinusoid.basis(t, a0, sa.a[1], sa.b[1]) // check setup data assert float64.tolerance(sa.period, sb.period, 1e-15) diff --git a/func/func.v b/func/func.v index 6916e0469..82ad0e9c9 100644 --- a/func/func.v +++ b/func/func.v @@ -18,9 +18,9 @@ mut: params []f64 } -// new_func returns an arbitrary function with parameters +// Fn.new returns an arbitrary function with parameters [inline] -pub fn new_func(f Fn) Fn { +pub fn Fn.new(f Fn) Fn { return f } @@ -53,9 +53,9 @@ mut: params []f64 } -// new_func_fdf returns an arbitrary function returning two values, r1, r2 +// FnFdf.new returns an arbitrary function returning two values, r1, r2 [inline] -pub fn new_func_fdf(fn_fdf FnFdf) FnFdf { +pub fn FnFdf.new(fn_fdf FnFdf) FnFdf { return fn_fdf } @@ -84,9 +84,9 @@ mut: params []f64 } -// new_func_vec returns an arbitrary vector-valued function with parameters +// FnVec.new returns an arbitrary vector-valued function with parameters [inline] -pub fn new_func_vec(f FnVec) FnVec { +pub fn FnVec.new(f FnVec) FnVec { return f } diff --git a/gm/basicgm.v b/gm/basicgm.v index d1d5ebd26..47594357f 100644 --- a/gm/basicgm.v +++ b/gm/basicgm.v @@ -19,8 +19,8 @@ pub: b &Point } -// new_point creates a new point -pub fn new_point(x f64, y f64, z f64) &Point { +// Point.new creates a new point +pub fn Point.new(x f64, y f64, z f64) &Point { return &Point{x, y, z} } @@ -51,8 +51,8 @@ pub fn (o &Segment) len() f64 { } // New creates a new Segment scaled by m and starting from A -pub fn (o &Segment) new_scaled(m f64) &Segment { - return new_segment(o.a.clone(), new_point(o.a.x + m * (o.b.x - o.a.x), o.a.y + +pub fn (o &Segment) scaled(m f64) &Segment { + return Segment.new(o.a.clone(), Point.new(o.a.x + m * (o.b.x - o.a.x), o.a.y + m * (o.b.y - o.a.y), o.a.z + m * (o.b.z - o.a.z))) } @@ -66,8 +66,8 @@ pub fn (o &Segment) str() string { return '{${o.a} ${o.b}} len=${o.len()}' } -// new_segment creates a new segment from a to b -pub fn new_segment(a &Point, b &Point) &Segment { +// Segment.new creates a new segment from a to b +pub fn Segment.new(a &Point, b &Point) &Segment { return &Segment{a, b} } @@ -94,8 +94,8 @@ pub fn vector_add(alpha f64, u []f64, beta f64, v []f64) []f64 { // dist_point_line computes the distance from p to line passing through a -> b pub fn dist_point_line(p &Point, a &Point, b &Point, tol f64) f64 { - ns := new_segment(a, b) - vs := new_segment(p, a) + ns := Segment.new(a, b) + vs := Segment.new(p, a) nn := ns.len() if nn < tol { // point-point distance $if debug { diff --git a/gm/bins.v b/gm/bins.v index 145ccf70d..ca7e427b8 100644 --- a/gm/bins.v +++ b/gm/bins.v @@ -39,11 +39,11 @@ pub mut: all []&Bin // [nbins] all bins (there will be an extra "ghost" bin along each dimension) } -// new_bins initialise bins structure +// Bins.new initialise bins structure // xmin -- [ndim] min/initial coordinates of the whole space (box/cube) // xmax -- [ndim] max/final coordinates of the whole space (box/cube) // ndiv -- [ndim] number of divisions for xmax-xmin -pub fn new_bins(xmin []f64, xmax []f64, ndiv_ []int) &Bins { +pub fn Bins.new(xmin []f64, xmax []f64, ndiv_ []int) &Bins { mut ndiv := ndiv_.clone() mut o := &Bins{} // check for out-of-range values @@ -252,7 +252,7 @@ pub fn (mut o Bins) find_along_segment(xi_ []f64, xf_ []f64, tol f64) []int { z += o.size[2] / 2.0 } // check if bin is near line - p := new_point(x, y, z) + p := Point.new(x, y, z) d := dist_point_line(p, pi, pf, tol) if d <= btol { sbins << bin @@ -308,7 +308,7 @@ fn point_from_vector(v []f64, dim int) &Point { if dim == 3 { z = v[3] } - return new_point(x, y, z) + return Point.new(x, y, z) } // nactive returns the number of active bins; i.e. non-nil bins diff --git a/graph/README.md b/graph/README.md index 95e2417dc..29b95c79c 100644 --- a/graph/README.md +++ b/graph/README.md @@ -30,7 +30,7 @@ edges := [[0, 1], [0, 3], [1, 2], [2, 3]] weights_e := [5.0, 10.0, 3.0, 1.0] verts := [][]f64{} weights_v := []f64{} -g := graph.new_graph(edges, weights_e, verts, weights_v) +g := graph.Graph.new(edges, weights_e, verts, weights_v) // print distance matrix print(g.str_dist_matrix()) ``` @@ -87,7 +87,7 @@ edges := [[0, 1], [0, 3], [1, 2], [2, 3]] weights_e := [5.0, 10.0, 3.0, 1.0] verts := [][]f64{} weights_v := []f64{} -g := graph.new_graph(edges, weights_e, verts, weights_v) +g := graph.Graph.new(edges, weights_e, verts, weights_v) // compute paths g.shortest_paths(.fw) // print shortest path from 0 to 2 diff --git a/graph/graph.v b/graph/graph.v index 2eca7c957..cf4b82df6 100644 --- a/graph/graph.v +++ b/graph/graph.v @@ -26,12 +26,12 @@ pub: next [][]int // [nverts][nverts] next tree connection. -1 means no connection } -// new_graph initialises graph +// Graph.new initialises graph // edges -- [nedges][2] edges (connectivity) // weights_e -- [nedges] weights of edges // verts -- [nverts][ndim] vertices // weights_v -- [nverts] weights of vertices -pub fn new_graph(edges [][]int, weights_e []f64, verts [][]f64, weights_v []f64) &Graph { +pub fn Graph.new(edges [][]int, weights_e []f64, verts [][]f64, weights_v []f64) &Graph { mut key2edge := map[int]int{} mut shares := map[int][]int{} for k, edge in edges { diff --git a/graph/graph_test.v b/graph/graph_test.v index 032225966..d39188cab 100644 --- a/graph/graph_test.v +++ b/graph/graph_test.v @@ -11,7 +11,7 @@ fn test_graph01() { * 1 ––––––––→ 2 indicate weights * [3] */ - g := new_graph([[0, 1], [0, 3], [1, 2], [2, 3]], [5.0, 10, 3, 1], [], []) + g := Graph.new([[0, 1], [0, 3], [1, 2], [2, 3]], [5.0, 10, 3, 1], [], []) assert g.shares.keys().len == 4 // nverts assert g.key2edge.keys().len == 4 // nedges assert g.dist.len == 4 // nverts @@ -71,7 +71,7 @@ fn test_graph02() { * 1 ←–––– 0 ––––→ 2 * [6] [8] */ - g := new_graph([[4, 5], [1, 4], [0, 1], [0, 2], [5, 2], [2, 3], + g := Graph.new([[4, 5], [1, 4], [0, 1], [0, 2], [5, 2], [2, 3], [5, 3]], [3.0, 11, 6, 8, 7, 9, 4], [], []) assert g.shares.keys().len == 6 // nverts assert g.key2edge.keys().len == 7 // nedges diff --git a/inout/h5/hdf5_nix.c.v b/inout/h5/hdf5_nix.c.v index 277932b5c..814807719 100644 --- a/inout/h5/hdf5_nix.c.v +++ b/inout/h5/hdf5_nix.c.v @@ -97,8 +97,8 @@ mut: filedesc Hdf5HidT } -// new_file creates a new HDF5 file, or truncates an existing file. -pub fn new_file(filename string) !Hdf5File { +// Hdf5File.new creates a new HDF5 file, or truncates an existing file. +pub fn Hdf5File.new(filename string) !Hdf5File { mut f := Hdf5File{ filedesc: C.H5Fcreate(unsafe { filename.str }, C.H5F_ACC_TRUNC, C.H5P_DEFAULT, C.H5P_DEFAULT) diff --git a/inout/h5/readhdf5_test.v b/inout/h5/readhdf5_test.v index 10447b7eb..fe7685ea7 100644 --- a/inout/h5/readhdf5_test.v +++ b/inout/h5/readhdf5_test.v @@ -16,7 +16,7 @@ fn testsuite_begin() { assert os.exists_in_system_path(h5.h5dump) - f := new_file(h5.testfile)! + f := Hdf5File.new(h5.testfile)! f.write_dataset1d('Shortarray', h5.shortarray)! f.close() } diff --git a/inout/h5/three_d_test.v b/inout/h5/three_d_test.v index 4944bb78f..bf332f8d6 100644 --- a/inout/h5/three_d_test.v +++ b/inout/h5/three_d_test.v @@ -45,7 +45,7 @@ fn test_3d() { } } - f := new_file(h5.testfile)! + f := Hdf5File.new(h5.testfile)! f.write_dataset3d('i8array', i8array)! f.write_dataset3d('u8array', u8array)! diff --git a/inout/h5/two_d_test.v b/inout/h5/two_d_test.v index 5e09f52a7..7d0b71855 100644 --- a/inout/h5/two_d_test.v +++ b/inout/h5/two_d_test.v @@ -43,7 +43,7 @@ fn test_2d() { } } - f := new_file(h5.testfile)! + f := Hdf5File.new(h5.testfile)! f.write_dataset2d('i8array', i8array)! f.write_dataset2d('u8array', u8array)! diff --git a/inout/h5/typeshdf5_test.v b/inout/h5/typeshdf5_test.v index 39930aeaf..99f8e991e 100644 --- a/inout/h5/typeshdf5_test.v +++ b/inout/h5/typeshdf5_test.v @@ -32,7 +32,7 @@ fn testsuite_begin() { assert os.exists_in_system_path(h5.h5dump) - f := new_file(h5.testfile)! + f := Hdf5File.new(h5.testfile)! f.write_dataset1d('i8array', h5.i8array)! f.write_dataset1d('u8array', h5.u8array)! diff --git a/iter/README.md b/iter/README.md index c51b87883..62b7c825c 100644 --- a/iter/README.md +++ b/iter/README.md @@ -17,9 +17,9 @@ pub fn combinations(data []f64, r int) [][]f64 This case is optimal to generate combinations in a lazy way, optimizing memory use: ```v ignore -// new_combinations_iter will return an iterator that allows +// CombinationsIter.new will return an iterator that allows // lazy computation for all length `r` combinations of `data` -pub fn new_combinations_iter(data []f64, r int) CombinationsIter +pub fn CombinationsIter.new(data []f64, r int) CombinationsIter // next will return next combination if possible pub fn (mut o CombinationsIter) next() ?[]f64 diff --git a/iter/comb.v b/iter/comb.v index 5987ac05a..e5fe09d1a 100644 --- a/iter/comb.v +++ b/iter/comb.v @@ -7,7 +7,7 @@ import vsl.util // While waiting on https://github.com/vlang/v/issues/7753 to be fixed, the function // assumes f64 array input. Will be easy to change to generic later pub fn combinations(data []f64, r int) [][]f64 { - mut combinations := new_combinations_iter(data, r) + mut combinations := CombinationsIter.new(data, r) mut result := [][]f64{cap: int(combinations.size)} for comb in combinations { result << comb @@ -25,9 +25,9 @@ pub: data []f64 } -// new_combinations_iter will return an iterator that allows +// CombinationsIter.new will return an iterator that allows // lazy computation for all length `r` combinations of `data` -pub fn new_combinations_iter(data []f64, r int) CombinationsIter { +pub fn CombinationsIter.new(data []f64, r int) CombinationsIter { n := data.len if r > n { return CombinationsIter{ @@ -84,7 +84,7 @@ pub fn (mut o CombinationsIter) next() ?[]f64 { // as I could manage. // Using f64 array instead of generic while waiting on https://github.com/vlang/v/issues/7753 pub fn combinations_with_replacement(data []f64, r int) [][]f64 { - mut combinations := new_combinations_with_replacement_iter(data, r) + mut combinations := CombinationsWithReplacementIter.new(data, r) mut result := [][]f64{cap: int(combinations.size)} for comb in combinations { result << comb @@ -102,9 +102,9 @@ pub: data []f64 } -// new_combinations_with_replacement_iter will return an iterator that allows +// CombinationsWithReplacementIter.new will return an iterator that allows // lazy computation for all length `r` combinations with replacement of `data` -pub fn new_combinations_with_replacement_iter(data []f64, r int) CombinationsWithReplacementIter { +pub fn CombinationsWithReplacementIter.new(data []f64, r int) CombinationsWithReplacementIter { n := data.len if r > n { return CombinationsWithReplacementIter{ diff --git a/iter/inf_iters.v b/iter/inf_iters.v index 9298096b7..883f8ee0b 100644 --- a/iter/inf_iters.v +++ b/iter/inf_iters.v @@ -7,7 +7,7 @@ pub mut: state f64 } -pub fn new_count_iter(start f64, step f64) Counter { +pub fn Counter.new(start f64, step f64) Counter { return Counter{ step: step state: start @@ -27,7 +27,7 @@ pub: data []f64 } -pub fn new_cycle_iter(data []f64) Cycler { +pub fn Cycler.new(data []f64) Cycler { return Cycler{ data: data idx: 0 @@ -45,7 +45,7 @@ pub: item f64 } -pub fn new_repeat_iter(item f64) Repeater { +pub fn Repeater.new(item f64) Repeater { return Repeater{ item: item } diff --git a/iter/inf_iters_test.v b/iter/inf_iters_test.v index fc1c79c72..3ad6e04f6 100644 --- a/iter/inf_iters_test.v +++ b/iter/inf_iters_test.v @@ -1,7 +1,7 @@ module iter fn test_counter_1() { - mut counter := new_count_iter(10, 1) + mut counter := Counter.new(10, 1) assert counter.next()? == 10 assert counter.next()? == 11 assert counter.next()? == 12 @@ -10,7 +10,7 @@ fn test_counter_1() { } fn test_counter_2() { - mut counter := new_count_iter(0, 3) + mut counter := Counter.new(0, 3) assert counter.next()? == 0 assert counter.next()? == 3 assert counter.next()? == 6 @@ -19,7 +19,7 @@ fn test_counter_2() { } fn test_counter_3() { - mut counter := new_count_iter(3, -1) + mut counter := Counter.new(3, -1) assert counter.next()? == 3 assert counter.next()? == 2 assert counter.next()? == 1 @@ -29,7 +29,7 @@ fn test_counter_3() { } fn test_counter_4() { - mut counter := new_count_iter(4, -3) + mut counter := Counter.new(4, -3) assert counter.next()? == 4 assert counter.next()? == 1 assert counter.next()? == -2 @@ -39,7 +39,7 @@ fn test_counter_4() { } fn test_cycle_1() { - mut cycler := new_cycle_iter([1.0, 2.0, 3.0]) + mut cycler := Cycler.new([1.0, 2.0, 3.0]) assert cycler.next()? == 1 assert cycler.next()? == 2 assert cycler.next()? == 3 @@ -48,7 +48,7 @@ fn test_cycle_1() { } fn test_cycle_2() { - mut cycler := new_cycle_iter([10.0, 0.0, 42.0, 12.0]) + mut cycler := Cycler.new([10.0, 0.0, 42.0, 12.0]) assert cycler.next()? == 10 assert cycler.next()? == 0 assert cycler.next()? == 42 @@ -58,7 +58,7 @@ fn test_cycle_2() { } fn test_repeat() { - r := new_repeat_iter(3) + r := Repeater.new(3) assert r.next()? == 3 assert r.next()? == 3 assert r.next()? == 3 diff --git a/iter/perm.v b/iter/perm.v index 5ad678a3e..369c87bdc 100644 --- a/iter/perm.v +++ b/iter/perm.v @@ -14,9 +14,9 @@ pub: data []f64 } -// new_permutations_iter will return an iterator that allows +// PermutationsIter.new will return an iterator that allows // lazy computation for all length `r` permutations of `data` -pub fn new_permutations_iter(data []f64, r int) PermutationsIter { +pub fn PermutationsIter.new(data []f64, r int) PermutationsIter { n := data.len if r > n { return PermutationsIter{ @@ -70,7 +70,7 @@ pub fn (mut o PermutationsIter) next() ?[]f64 { // permutations returns successive `r` length permutations of elements in `data` pub fn permutations(data []f64, r int) [][]f64 { - mut perms := new_permutations_iter(data, r) + mut perms := PermutationsIter.new(data, r) mut result := [][]f64{cap: int(perms.size)} for perm in perms { result << perm diff --git a/iter/prod.v b/iter/prod.v index 3920916e6..4b7cf281a 100644 --- a/iter/prod.v +++ b/iter/prod.v @@ -12,7 +12,7 @@ pub: data [][]f64 } -pub fn new_product_iterator(data [][]f64) ProductIterator { +pub fn ProductIterator.new(data [][]f64) ProductIterator { return ProductIterator{ repeat_lengths: calc_repeat_lengths(data.map(it.len)).map(u64(it)) indices_to_grab: []int{len: data.len, init: -1} @@ -39,7 +39,7 @@ pub fn (mut o ProductIterator) next() ?[]f64 { // Cartesian product of the arrays in `data` pub fn product(data [][]f64) [][]f64 { - products := new_product_iterator(data) + products := ProductIterator.new(data) mut result := [][]f64{cap: int(products.size)} for prod in products { result << prod diff --git a/iter/ranges.v b/iter/ranges.v index 5c7442fcd..2597d6f35 100644 --- a/iter/ranges.v +++ b/iter/ranges.v @@ -20,7 +20,7 @@ pub struct IntIterParams { step i64 = 1 } -// new_int_iter returns an iterator of evenly spaced integers numbers in the half-open interval `[start, stop)`. +// IntIter.new returns an iterator of evenly spaced integers numbers in the half-open interval `[start, stop)`. //---------- // parameters: // IntIterParams { @@ -28,7 +28,7 @@ pub struct IntIterParams { // -stop i64 [required]: the end of the range (exclusive). // -step i64 = 1: the step between the numbers. //} -pub fn new_int_iter(params IntIterParams) !IntIter { +pub fn IntIter.new(params IntIterParams) !IntIter { if params.step == 0 { return errors.error(@MOD + '.' + @FN + ': step cannot be 0', .erange) } @@ -76,7 +76,7 @@ pub struct FloatIterParams { step f64 = 1.0 } -// new_float_iter returns an iterator of evenly spaced floats in the half-open interval `[start, stop)`. +// FloatIter.new returns an iterator of evenly spaced floats in the half-open interval `[start, stop)`. //---------- // parameters: // FloatIterParams { @@ -84,7 +84,7 @@ pub struct FloatIterParams { // -stop f64 [required]: the end of the range (exclusive). // -step f64 = 1: the step between the numbers. //} -pub fn new_float_iter(params FloatIterParams) !FloatIter { +pub fn FloatIter.new(params FloatIterParams) !FloatIter { if params.step == 0 { return errors.error(@MOD + '.' + @FN + ': step cannot be 0', .erange) } @@ -132,7 +132,7 @@ pub struct LinearIterParams { endpoint bool = true } -// new_linear_iter returns an iterator of `len` evenly spaced floats in the interval `[start, stop]`. +// LinearIter.new returns an iterator of `len` evenly spaced floats in the interval `[start, stop]`. // The endpoint of the interval can optionally be excluded. //---------- // parameters: @@ -142,7 +142,7 @@ pub struct LinearIterParams { // -len i64 = 50: Number of samples to generate. Must be non-negative. // -endpoint bool = true: If true, `stop` is the last sample. Otherwise, it is not included. //} -pub fn new_linear_iter(params LinearIterParams) !LinearIter { +pub fn LinearIter.new(params LinearIterParams) !LinearIter { if params.len < 0 { return errors.error(@MOD + '.' + @FN + ': number of samples must be non negative', .erange) @@ -197,7 +197,7 @@ pub struct LogIterParams { endpoint bool = true } -// log_iter returns an iterator of `len` numbers evenly spaced on a logarithmic scale. +// LogIter.new returns an iterator of `len` numbers evenly spaced on a logarithmic scale. // The sequence starts at `base ^ start` and ends in `base ^ stop` (if `endpoint` = true). //------- // parameters: @@ -208,13 +208,13 @@ pub struct LogIterParams { // -base f64 = 10.0: The base of the log space. // -endpoint bool = true: If true, bas ^ stop is the last sample. Otherwise, it is not included. //} -pub fn new_log_iter(params LogIterParams) !LogIter { +pub fn LogIter.new(params LogIterParams) !LogIter { if params.len < 0 { return errors.error(@MOD + '.' + @FN + ': number of samples must be non negative', .erange) } return LogIter{ - linear_iter: new_linear_iter( + linear_iter: LinearIter.new( start: params.start stop: params.stop len: params.len diff --git a/iter/ranges_test.v b/iter/ranges_test.v index 26107fac1..6102417aa 100644 --- a/iter/ranges_test.v +++ b/iter/ranges_test.v @@ -14,9 +14,7 @@ fn test_int_iter() { [i64(-88), -84, -80, -76], []i64{len: 0}] for i, values in args { - r := new_int_iter(start: values[0], stop: values[1], step: values[2]) or { - panic('this should not happen') - } + r := IntIter.new(start: values[0], stop: values[1], step: values[2])! for j, n in r { assert n == expected[i][j] } @@ -52,9 +50,7 @@ fn test_float_iter() { ] for i, values in args { - r := new_float_iter(start: values[0], stop: values[1], step: values[2]) or { - panic('this should not happen') - } + r := FloatIter.new(start: values[0], stop: values[1], step: values[2])! for j, n in r { if !n.eq_epsilon(expected[i][j]) { println('${n}, ${expected[i][j]}') @@ -92,9 +88,7 @@ fn test_linear_iter() { []f64{}] for i, lim in limits { - l := new_linear_iter(start: lim[0], stop: lim[1], len: lens[i], endpoint: endpoints[i]) or { - panic('this should not happen') - } + l := LinearIter.new(start: lim[0], stop: lim[1], len: lens[i], endpoint: endpoints[i])! for j, n in l { assert n.eq_epsilon(expected[i][j]) } diff --git a/la/blas.v b/la/blas.v index 6000c57d5..53514a776 100644 --- a/la/blas.v +++ b/la/blas.v @@ -189,7 +189,7 @@ pub fn matrix_tr_vector_mul[T](alpha T, a &Matrix[T], u []T) []T { // pub fn vector_vector_tr_mul[T](alpha T, u []T, v []T) &Matrix[T] { $if T is f64 { - mut m := new_matrix[f64](u.len, v.len) + mut m := Matrix.new[f64](u.len, v.len) if m.m < 9 && m.n < 9 { for i in 0 .. m.m { for j in 0 .. m.n { @@ -201,9 +201,9 @@ pub fn vector_vector_tr_mul[T](alpha T, u []T, v []T) &Matrix[T] { mut a := []f64{len: u.len * v.len} vlas.dger(m.m, m.n, alpha, arr_to_f64arr[T](u), 1, arr_to_f64arr[T](v), 1, mut a, int(math.max(m.m, m.n))) - return matrix_raw(u.len, v.len, a) + return Matrix.raw(u.len, v.len, a) } $else { - mut m := new_matrix[T](u.len, v.len) + mut m := Matrix.new[T](u.len, v.len) for i in 0 .. m.m { for j in 0 .. m.n { diff --git a/la/blas_test.v b/la/blas_test.v index 8e3300f41..3570741a9 100644 --- a/la/blas_test.v +++ b/la/blas_test.v @@ -2,7 +2,7 @@ module la fn test_mat_vec_mul() { expected := [8.0, 45, -3, 3, 19] - a := matrix_deep2([ + a := Matrix.deep2([ [2.0, 3, 0, 0, 0], [3.0, 0, 4, 0, 6], [0.0, -1, -3, 2, 0], diff --git a/la/densesol.v b/la/densesol.v index 39411f0fa..8f8a14b21 100644 --- a/la/densesol.v +++ b/la/densesol.v @@ -9,7 +9,7 @@ import vsl.vlas pub fn den_solve(mut x []f64, a &Matrix[f64], b []f64, preserve_a bool) { mut a_ := unsafe { a } if preserve_a { - a_ = new_matrix[f64](a.m, a.n) + a_ = Matrix.new[f64](a.m, a.n) a_.data = a.data.clone() } for i in 0 .. x.len { diff --git a/la/densesol_test.v b/la/densesol_test.v index b26960d67..555065e64 100644 --- a/la/densesol_test.v +++ b/la/densesol_test.v @@ -8,7 +8,7 @@ const ( fn test_den_solve() { // case 1 - mat1 := matrix_deep2([ + mat1 := Matrix.deep2([ [1.0, 0], [0.0, 2], ]) @@ -17,7 +17,7 @@ fn test_den_solve() { den_solve(mut x1, mat1, b1, false) assert float64.arrays_tolerance(x1, [1.0, 0.5], la.debsesol_tests_tol) // case 2 - mat2 := matrix_deep2([ + mat2 := Matrix.deep2([ [2.0, 0, 0, -5.6], [0.0, 3, 0, 1.2], [0.0, 0, 4, 4.5], diff --git a/la/jacobi_test.v b/la/jacobi_test.v index c4920cc76..d816f0b2b 100644 --- a/la/jacobi_test.v +++ b/la/jacobi_test.v @@ -3,16 +3,16 @@ module la import vsl.float.float64 fn test_jacobi01() { - mut a := matrix_deep2([ + mut a := Matrix.deep2([ [2.0, 0, 0], [0.0, 2, 0], [0.0, 0, 2], ]) - mut q := new_matrix[f64](3, 3) + mut q := Matrix.new[f64](3, 3) mut v := []f64{len: 3} - mut expected_q := matrix_deep2([ + mut expected_q := Matrix.deep2([ [1.0, 0, 0], [0.0, 1, 0], [0.0, 0, 1], diff --git a/la/matrix.v b/la/matrix.v index 7dbf22aaa..e2f646521 100644 --- a/la/matrix.v +++ b/la/matrix.v @@ -12,8 +12,8 @@ pub mut: data []T } -// new_matrix allocates a new (empty) Matrix with given (m,n) (row/col sizes) -pub fn new_matrix[T](m int, n int) &Matrix[T] { +// Matrix.new allocates a new (empty) Matrix with given (m,n) (row/col sizes) +pub fn Matrix.new[T](m int, n int) &Matrix[T] { data := []T{len: m * n} return &Matrix[T]{ m: m @@ -22,21 +22,21 @@ pub fn new_matrix[T](m int, n int) &Matrix[T] { } } -// matrix_deep2 allocates a new Matrix from given (Deep2) nested slice. +// Matrix.deep2 allocates a new Matrix from given (Deep2) nested slice. // NOTE: make sure to have at least 1x1 item -pub fn matrix_deep2[T](a [][]T) &Matrix[T] { - mut o := new_matrix[T](a.len, a[0].len) +pub fn Matrix.deep2[T](a [][]T) &Matrix[T] { + mut o := Matrix.new[T](a.len, a[0].len) o.set_from_deep2(a) return o } -// matrix_raw creates a new Matrix using given raw data +// Matrix.raw creates a new Matrix using given raw data // Input: // rawdata -- data organized as column-major; e.g. Fortran format // NOTE: // (1) rawdata is not copied! // (2) the external slice rawdata should not be changed or deleted -pub fn matrix_raw[T](m int, n int, rawdata []T) &Matrix[T] { +pub fn Matrix.raw[T](m int, n int, rawdata []T) &Matrix[T] { return &Matrix[T]{ m: m n: n @@ -91,14 +91,14 @@ pub fn (o &Matrix[T]) get_deep2() [][]T { // clone returns a copy of this matrix pub fn (o &Matrix[T]) clone() &Matrix[T] { - mut clone := new_matrix[T](o.m, o.n) + mut clone := Matrix.new[T](o.m, o.n) clone.data = o.data.clone() return clone } // transpose returns the transpose matrix pub fn (o &Matrix[T]) transpose() &Matrix[T] { - mut tran := new_matrix[T](o.n, o.m) + mut tran := Matrix.new[T](o.n, o.m) for i := 0; i < o.n; i++ { for j := 0; j < o.m; j++ { tran.set(i, j, o.get(j, i)) @@ -223,7 +223,7 @@ pub fn (o &Matrix[T]) extract_cols(start int, endp1 int) !&Matrix[T] { .efailed) } ncol := endp1 - start - mut reduced := new_matrix[T](o.m, ncol) + mut reduced := Matrix.new[T](o.m, ncol) for i in 0 .. o.m { for j := 0; j < ncol; j++ { reduced.set(i, j, o.get(i, j + start)) @@ -241,7 +241,7 @@ pub fn (o &Matrix[T]) extract_rows(start int, endp1 int) !&Matrix[T] { .efailed) } nrow := endp1 - start - mut reduced := new_matrix[T](nrow, o.n) + mut reduced := Matrix.new[T](nrow, o.n) reduced.data = o.data[start * o.m..endp1 * o.m] return reduced } @@ -266,8 +266,8 @@ pub fn (o &Matrix[T]) split_by_col(j int) !(&Matrix[T], &Matrix[T]) { if j < 0 || j >= o.n { return errors.error('j=${j} must be in range [0, ${o.n})', .efailed) } - mut left := new_matrix[T](o.m, j) - mut right := new_matrix[T](o.m, o.n - j) + mut left := Matrix.new[T](o.m, j) + mut right := Matrix.new[T](o.m, o.n - j) for i in 0 .. o.m { for k := 0; k < j; k++ { left.set(i, k, o.get(i, k)) @@ -285,8 +285,8 @@ pub fn (o &Matrix[T]) split_by_row(i int) !(&Matrix[T], &Matrix[T]) { if i < 0 || i >= o.m { return errors.error('i=${i} must be in range [0, ${o.m})', .efailed) } - mut top := new_matrix[T](i, o.n) - mut bottom := new_matrix[T](o.m - i, o.n) + mut top := Matrix.new[T](i, o.n) + mut bottom := Matrix.new[T](o.m - i, o.n) for j in 0 .. o.n { for k := 0; k < i; k++ { top.set(k, j, o.get(k, j)) diff --git a/la/matrix_ops.v b/la/matrix_ops.v index 10e73f572..36741541c 100644 --- a/la/matrix_ops.v +++ b/la/matrix_ops.v @@ -124,8 +124,8 @@ pub fn matrix_inv(mut ai Matrix[f64], mut a Matrix[f64], calc_det bool) f64 { } // singular value decomposition mut s := []f64{len: int(math.min(a.m, a.n))} - mut u := new_matrix[f64](a.m, a.m) - mut vt := new_matrix[f64](a.n, a.n) + mut u := Matrix.new[f64](a.m, a.m) + mut vt := Matrix.new[f64](a.n, a.n) matrix_svd(mut s, mut u, mut vt, mut a, true) // pseudo inverse tol_s := 1e-8 // TODO: improve this tolerance with a better estimate @@ -149,7 +149,7 @@ pub fn matrix_inv(mut ai Matrix[f64], mut a Matrix[f64], calc_det bool) f64 { // "F" or "" (default) => Frobenius pub fn matrix_cond_num(mut a Matrix[f64], normtype string) f64 { mut res := 0.0 - mut ai := new_matrix[f64](a.m, a.n) + mut ai := Matrix.new[f64](a.m, a.n) matrix_inv(mut ai, mut a, false) if normtype == 'I' { res = a.norm_inf() * ai.norm_inf() diff --git a/la/matrix_ops_test.v b/la/matrix_ops_test.v index b1c8156fb..d0ee4571d 100644 --- a/la/matrix_ops_test.v +++ b/la/matrix_ops_test.v @@ -19,12 +19,12 @@ fn tolerance_equal[T](data1 []T, data2 []T) bool { } fn test_det() { - mat_a := matrix_deep2([ + mat_a := Matrix.deep2([ [3.0, 8.0], [4.0, 6.0], ]) assert matrix_det(mat_a) == -14 - mat_b := matrix_deep2([ + mat_b := Matrix.deep2([ [6.0, 1.0, 1.0], [4.0, -2.0, 5.0], [2.0, 8.0, 7.0], @@ -34,46 +34,46 @@ fn test_det() { fn test_matrix_inv_small() { // case 1 - mat1 := matrix_deep2([ + mat1 := Matrix.deep2([ [1.0, 0.0], [0.0, 1.0], ]) - mut inv1 := new_matrix[f64](2, 2) + mut inv1 := Matrix.new[f64](2, 2) det1 := matrix_inv_small(mut inv1, mat1, la.matrix_tests_tol) assert tolerance_equal(mat1.data, inv1.data) assert math.abs(det1 - 1.0) <= la.matrix_tests_tol // case 2 - mat2 := matrix_deep2([ + mat2 := Matrix.deep2([ [2.0, 3.0], [3.0, 4.0], ]) - ex2 := matrix_deep2([ + ex2 := Matrix.deep2([ [-4.0, 3.0], [3.0, -2.0], ]) - mut inv2 := new_matrix[f64](2, 2) + mut inv2 := Matrix.new[f64](2, 2) det2 := matrix_inv_small(mut inv2, mat2, la.matrix_tests_tol) assert tolerance_equal(inv2.data, ex2.data) assert math.abs(det2 + 1.0) <= la.matrix_tests_tol // case 3 - mat3 := matrix_deep2([ + mat3 := Matrix.deep2([ [-2.0, 2.0, 0.0], [2.0, 1.0, 3.0], [-2.0, 4.0, -2.0], ]) - ex3 := matrix_deep2([ + ex3 := Matrix.deep2([ [-7.0 / 12, 1.0 / 6, 1.0 / 4], [-1.0 / 12, 1.0 / 6, 1.0 / 4], [5.0 / 12, 1.0 / 6, -1.0 / 4], ]) - mut inv3 := new_matrix[f64](3, 3) + mut inv3 := Matrix.new[f64](3, 3) det3 := matrix_inv_small(mut inv3, mat3, la.matrix_tests_tol) assert tolerance_equal(inv3.data, ex3.data) assert math.abs(det3 - 24.0) <= la.matrix_tests_tol // case 4 - mat4 := matrix_deep2([[14.0]]) - ex4 := matrix_deep2([[1.0 / 14]]) - mut inv4 := new_matrix[f64](1, 1) + mat4 := Matrix.deep2([[14.0]]) + ex4 := Matrix.deep2([[1.0 / 14]]) + mut inv4 := Matrix.new[f64](1, 1) det4 := matrix_inv_small(mut inv4, mat4, la.matrix_tests_tol) assert tolerance_equal(inv4.data, ex4.data) assert math.abs(det4 - 14.0) <= la.matrix_tests_tol diff --git a/la/matrix_test.v b/la/matrix_test.v index 462d0fed0..d7e4386b6 100644 --- a/la/matrix_test.v +++ b/la/matrix_test.v @@ -3,7 +3,7 @@ module la import math fn test_new_matrix() { - mat := new_matrix[f64](3, 2) + mat := Matrix.new[f64](3, 2) expected_data := []f64{len: 6, init: 0.0} assert mat.m == 3 assert mat.n == 2 @@ -16,7 +16,7 @@ fn test_matrix_deep2() { [21.0, 22.0, 23.0, 24.0], [31.0, 32.0, 33.0, 34.0], ] - mat := matrix_deep2(original_array) + mat := Matrix.deep2(original_array) assert mat.m == 3 assert mat.n == 4 // The data is stored in column-major format @@ -28,19 +28,19 @@ fn test_set_from_deep2() { [1.0, 1.0, 2.0], [5.0, 4.0, 2.0], ] - mut mat := new_matrix[f64](2, 3) + mut mat := Matrix.new[f64](2, 3) mat.set_from_deep2(original_array) assert mat.data == [1.0, 1, 2, 5, 4, 2] } fn test_set_diag() { - mut mat := new_matrix[f64](4, 4) + mut mat := Matrix.new[f64](4, 4) mat.set_diag(36) assert mat.data == [36.0, 0, 0, 0, 0, 36.0, 0, 0, 0, 0, 36.0, 0, 0, 0, 0, 36.0] } fn test_set_and_get() { - mut mat := matrix_deep2([ + mut mat := Matrix.deep2([ [10.0, 11.0, 12.0, 13.0], [20.0, 21.0, 22.0, 23.0], [30.0, 31.0, 32.0, 33.0], @@ -57,12 +57,12 @@ fn test_get_deep2() { [3.62, 0.15, -0.5, 35.5], [-4.1, 0.62, -984, 45.1], ] - mat := matrix_deep2(original_array) + mat := Matrix.deep2(original_array) assert mat.get_deep2() == original_array } fn test_matrix_clone() { - mat1 := matrix_deep2([ + mat1 := Matrix.deep2([ [0.1, 0.2, 0.3], [1.1, 1.2, 1.3], ]) @@ -73,11 +73,11 @@ fn test_matrix_clone() { } fn test_transpose() { - mat1 := matrix_deep2([ + mat1 := Matrix.deep2([ [1.0, 2.0, 3.0], [4.0, 5.0, 6.0], ]) - mat2 := matrix_deep2([ + mat2 := Matrix.deep2([ [1.0, 4.0], [2.0, 5.0], [3.0, 6.0], @@ -97,32 +97,32 @@ fn test_transpose() { } fn test_copy_into_and_matrix_raw() { - mat_a := matrix_raw(3, 4, []f64{len: 12, init: 42}) - mut mat_b := new_matrix[f64](3, 4) + mat_a := Matrix.raw(3, 4, []f64{len: 12, init: 42}) + mut mat_b := Matrix.new[f64](3, 4) mat_a.copy_into(mut mat_b, 0.5) assert mat_b.data == []f64{len: 12, init: 21} } fn test_add() { - mut mat := matrix_raw(1, 2, [0.1, 0.2]) + mut mat := Matrix.raw(1, 2, [0.1, 0.2]) mat.add(0, 1, 1) assert mat.get(0, 1) == 1.2 } fn test_fill() { - mat_a := matrix_raw(3, 4, []f64{len: 12, init: 54.3}) - mut mat_b := new_matrix[f64](3, 4) + mat_a := Matrix.raw(3, 4, []f64{len: 12, init: 54.3}) + mut mat_b := Matrix.new[f64](3, 4) mat_b.fill(54.3) assert mat_a.data == mat_b.data } fn test_clear_rc() { - mut mat_a := matrix_deep2([ + mut mat_a := Matrix.deep2([ [1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [4.0, 3.0, 2.0, 1.0], ]) - mat_b := matrix_deep2([ + mat_b := Matrix.deep2([ [1.0, 2.0, 3.0, 4.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], @@ -132,12 +132,12 @@ fn test_clear_rc() { } fn test_clear_bry() { - mut mat_a := matrix_deep2([ + mut mat_a := Matrix.deep2([ [1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], ]) - mat_b := matrix_deep2([ + mat_b := Matrix.deep2([ [1.0, 0.0, 0.0], [0.0, 5.0, 0.0], [0.0, 0.0, 1.0], @@ -147,18 +147,18 @@ fn test_clear_bry() { } fn test_max_diff() { - mat_a := matrix_raw(3, 2, [1.0, 5, 3, 6, 7, 3]) - mat_b := matrix_raw(3, 2, [4.0, 19, 42, 3, -31, 5]) + mat_a := Matrix.raw(3, 2, [1.0, 5, 3, 6, 7, 3]) + mat_b := Matrix.raw(3, 2, [4.0, 19, 42, 3, -31, 5]) assert mat_a.max_diff(mat_b) == 39 } fn test_largest() { - mat := matrix_raw(3, 3, [1.0, 5, 3, 6, -74, 3, 24, 62, -39]) + mat := Matrix.raw(3, 3, [1.0, 5, 3, 6, -74, 3, 24, 62, -39]) assert mat.largest(74) == 1 } fn test_col_get_row_get_col() { - mat := matrix_deep2([ + mat := Matrix.deep2([ [11.0, 12.0, 13.0], [21.0, 22.0, 23.0], [31.0, 32.0, 33.0], @@ -169,7 +169,7 @@ fn test_col_get_row_get_col() { } fn test_extract_cols_and_set_col() { - mut mat := matrix_deep2([ + mut mat := Matrix.deep2([ [11.0, 12.0, 13.0], [21.0, 22.0, 23.0], [31.0, 32.0, 33.0], @@ -184,12 +184,12 @@ fn test_extract_cols_and_set_col() { } fn test_frobenius_norm() { - mat_a := matrix_deep2([ + mat_a := Matrix.deep2([ [1.0, 2.0], [3.0, 4.0], ]) assert mat_a.norm_frob() == math.sqrt(30) - mat_b := matrix_deep2([ + mat_b := Matrix.deep2([ [1.0, 4.0, 6.0], [7.0, 9.0, 10.0], ]) @@ -197,12 +197,12 @@ fn test_frobenius_norm() { } fn test_infinite_norm() { - mat_a := matrix_deep2([ + mat_a := Matrix.deep2([ [1.0, -7.0], [-2.0, -3.0], ]) assert mat_a.norm_inf() == 8 - mat_b := matrix_deep2([ + mat_b := Matrix.deep2([ [5.0, -4.0, 2.0], [-1.0, 2.0, 3.0], [-2.0, 1.0, 0.0], @@ -211,14 +211,14 @@ fn test_infinite_norm() { } fn test_apply() { - mat_a := matrix_raw(1, 4, [0.0, 2.0, 4.0, 6.0]) - mut mat_b := new_matrix[f64](1, 4) + mat_a := Matrix.raw(1, 4, [0.0, 2.0, 4.0, 6.0]) + mut mat_b := Matrix.new[f64](1, 4) mat_b.apply(3.0 / 2.0, mat_a) assert mat_b.data == [0.0, 3.0, 6.0, 9.0] } fn test_str_and_print_functions() { - mat := *matrix_raw(2, 2, [1.0, 2.0, 3.0, 4.0]) + mat := *Matrix.raw(2, 2, [1.0, 2.0, 3.0, 4.0]) assert mat.str() == '1 2 \n3 4 ' assert mat.print('%g ') == '1 2 \n3 4 ' assert mat.print_v('%g') == '[][]f64{\n {1,2},\n {3,4},\n}' diff --git a/la/sparse_config.v b/la/sparse_config.v index c4d4b54a5..66ac022dc 100644 --- a/la/sparse_config.v +++ b/la/sparse_config.v @@ -17,10 +17,10 @@ pub mut: mumps_max_memory_per_processor int // MUMPS control parameters (check MUMPS solver manual) ICNTL(23) default = 2000Mb } -// new_sparse_config returns a new SparseConfig +// SparseConfig.new returns a new SparseConfig // Input: // comm -- may be nil -pub fn new_sparse_config() SparseConfig { +pub fn SparseConfig.new() SparseConfig { mut o := SparseConfig{ mumps_increase_of_working_space_pct: 100 mumps_max_memory_per_processor: 2000 @@ -30,8 +30,8 @@ pub fn new_sparse_config() SparseConfig { return o } -// new_sparse_config_with_comm returns a new SparseConfig -pub fn new_sparse_config_with_comm(comm &mpi.Communicator) SparseConfig { +// SparseConfig.with_comm returns a new SparseConfig +pub fn SparseConfig.with_comm(comm &mpi.Communicator) SparseConfig { mut o := SparseConfig{ mumps_increase_of_working_space_pct: 100 mumps_max_memory_per_processor: 2000 diff --git a/la/sparse_matrix.v b/la/sparse_matrix.v index 1bfd66bae..1f469db2a 100644 --- a/la/sparse_matrix.v +++ b/la/sparse_matrix.v @@ -34,8 +34,8 @@ mut: x []T } -// new_triplet returns a new Triplet. This is a wrapper to new(Triplet) followed by init() -pub fn new_triplet[T](m int, n int, max int) &Triplet[T] { +// Triplet.new returns a new Triplet. This is a wrapper to new(Triplet) followed by init() +pub fn Triplet.new[T](m int, n int, max int) &Triplet[T] { mut o := &Triplet[T]{} o.init(m, n, max) return o @@ -133,7 +133,7 @@ pub fn (o Triplet[T]) size() (int, int) { // to_dense returns the dense matrix corresponding to this Triplet pub fn (o Triplet[T]) to_dense() &Matrix[T] { - mut a := new_matrix[T](o.m, o.n) + mut a := Matrix.new[T](o.m, o.n) for k := 0; k < o.max; k++ { a.add(o.i[k], o.j[k], o.x[k]) } diff --git a/la/sparse_matrix_test.v b/la/sparse_matrix_test.v index cd0593d14..07b73c58c 100644 --- a/la/sparse_matrix_test.v +++ b/la/sparse_matrix_test.v @@ -1,7 +1,7 @@ module la fn test_triplet() { - mut a := new_triplet[f64](4, 4, 6) + mut a := Triplet.new[f64](4, 4, 6) a.put(1, 0, 1.0)! a.put(0, 1, 2.0)! @@ -10,7 +10,7 @@ fn test_triplet() { a.put(2, 3, 5.0)! a.put(3, 3, 6.0)! - mut expected_matrix := matrix_deep2([ + mut expected_matrix := Matrix.deep2([ [0.0, 2, 0, 0], [1.0, 0, 4, 0], [0.0, 0, 0, 5], diff --git a/ml/data.v b/ml/data.v index 484884d19..0929b4686 100644 --- a/ml/data.v +++ b/ml/data.v @@ -29,7 +29,7 @@ pub mut: y []T // [nb_samples] y values [optional] } -// new_data returns a new object to hold ML data +// Data.new returns a new object to hold ML data // Input: // nb_samples -- number of data samples (rows in x) // nb_features -- number of features (columns in x) @@ -38,9 +38,9 @@ pub mut: // x and y must be set using set() method // Output: // new object -pub fn new_data[T](nb_samples int, nb_features int, use_y bool, allocate bool) !&Data[T] { +pub fn Data.new[T](nb_samples int, nb_features int, use_y bool, allocate bool) !&Data[T] { x := if allocate { - la.new_matrix[T](nb_samples, nb_features) + la.Matrix.new[T](nb_samples, nb_features) } else { &la.Matrix[T](unsafe { nil }) } @@ -87,12 +87,12 @@ pub fn (mut o Data[T]) set_x(x &la.Matrix[T]) ! { o.observable.notify_update() } -// data_from_raw_x returns a new object with data set from raw x values +// Data.from_raw_x returns a new object with data set from raw x values // Input: // xraw -- [nb_samples][nb_features] table with x values (NO y values) // Output: // new object -pub fn data_from_raw_x[T](xraw [][]T) !&Data[T] { +pub fn Data.from_raw_x[T](xraw [][]T) !&Data[T] { // check nb_samples := xraw.len if nb_samples < 1 { @@ -100,7 +100,7 @@ pub fn data_from_raw_x[T](xraw [][]T) !&Data[T] { } // allocate new object nb_features := xraw[0].len - mut o := new_data[T](nb_samples, nb_features, true, true)! + mut o := Data.new[T](nb_samples, nb_features, true, true)! // copy data from raw table to x matrix for i := 0; i < nb_samples; i++ { for j := 0; j < nb_features; j++ { @@ -110,11 +110,11 @@ pub fn data_from_raw_x[T](xraw [][]T) !&Data[T] { return o } -// data_from_raw_xy_sep accepts two parameters: xraw [][]T and -// yraw []T. It acts similarly to data_from_raw_xy, but instead +// Data.from_raw_xy_sep accepts two parameters: xraw [][]T and +// yraw []T. It acts similarly to Data.from_raw_xy, but instead // of using the last column of xraw as the y data, it uses yraw // instead. -pub fn data_from_raw_xy_sep[T](xraw [][]T, yraw []T) !&Data[T] { +pub fn Data.from_raw_xy_sep[T](xraw [][]T, yraw []T) !&Data[T] { // check nb_samples := xraw.len if nb_samples < 1 { @@ -122,7 +122,7 @@ pub fn data_from_raw_xy_sep[T](xraw [][]T, yraw []T) !&Data[T] { } // allocate new object nb_features := xraw[0].len - mut o := new_data[T](nb_samples, nb_features, false, true)! + mut o := Data.new[T](nb_samples, nb_features, false, true)! // copy data from raw table to x matrix for i := 0; i < nb_samples; i++ { for j := 0; j < nb_features; j++ { @@ -135,13 +135,13 @@ pub fn data_from_raw_xy_sep[T](xraw [][]T, yraw []T) !&Data[T] { return o } -// data_from_raw_xy returns a new object with data set from raw Xy values +// Data.from_raw_xy returns a new object with data set from raw Xy values // Input: // Xyraw -- [nb_samples][nb_features+1] table with x and y raw values, // where the last column contains y-values // Output: // new object -pub fn data_from_raw_xy[T](xyraw [][]T) !&Data[T] { +pub fn Data.from_raw_xy[T](xyraw [][]T) !&Data[T] { // check nb_samples := xyraw.len if nb_samples < 1 { @@ -149,7 +149,7 @@ pub fn data_from_raw_xy[T](xyraw [][]T) !&Data[T] { } // allocate new object nb_features := xyraw[0].len - 1 // -1 because of y column - mut o := new_data[T](nb_samples, nb_features, true, true)! + mut o := Data.new[T](nb_samples, nb_features, true, true)! // copy data from raw table to x and y arrays for i := 0; i < nb_samples; i++ { for j := 0; j < nb_features; j++ { @@ -163,7 +163,7 @@ pub fn data_from_raw_xy[T](xyraw [][]T) !&Data[T] { // clone returns a deep copy of this object removing the observers pub fn (o &Data[T]) clone() !&Data[T] { use_y := o.y.len > 0 - mut p := new_data[T](o.nb_samples, o.nb_features, use_y, true)! + mut p := Data.new[T](o.nb_samples, o.nb_features, use_y, true)! o.x.copy_into(mut p.x, 1) if use_y { p.y = o.y.clone() @@ -209,8 +209,8 @@ pub fn (o &Data[T]) split(ratio f64) !(&Data[T], &Data[T]) { m1, m2 := o.x.split_by_row(nb_samples1)! - mut o1 := new_data[T](nb_samples1, nb_features, false, false)! - mut o2 := new_data[T](nb_samples2, nb_features, false, false)! + mut o1 := Data.new[T](nb_samples1, nb_features, false, false)! + mut o2 := Data.new[T](nb_samples2, nb_features, false, false)! o1.set(m1, o.y[..nb_samples1])! o2.set(m2, o.y[nb_samples1..])! diff --git a/ml/data_test.v b/ml/data_test.v index c24cdffdf..08fe78c6f 100644 --- a/ml/data_test.v +++ b/ml/data_test.v @@ -18,7 +18,7 @@ fn check_xy_01(x &la.Matrix[f64], y []f64) { } fn test_data_01() { - data := data_from_raw_xy([ + data := Data.from_raw_xy([ [-1.0, 0, -3, 0], [-2.0, 3, 3, 1], [3.0, 1, 4, 1], @@ -33,7 +33,7 @@ fn test_data_01() { } fn test_split() { - data := data_from_raw_xy([ + data := Data.from_raw_xy([ [-1.0, 0, -3, 0], [-2.0, 3, 3, 1], [3.0, 1, 4, 1], diff --git a/ml/kmeans.v b/ml/kmeans.v index 8716cc096..79d1033ef 100644 --- a/ml/kmeans.v +++ b/ml/kmeans.v @@ -20,20 +20,20 @@ pub mut: nb_members []int // [nb_classes] number of members in each class } -// new_kmeans returns a new K-means model -pub fn new_kmeans(mut data Data[f64], nb_classes int, name string) &Kmeans { +// Kmeans.new returns a new K-means model +pub fn Kmeans.new(mut data Data[f64], nb_classes int, name string) &Kmeans { // classes classes := []int{len: data.nb_samples} centroids := [][]f64{len: nb_classes} nb_members := []int{len: nb_classes} // stat - mut stat := stat_from_data(mut data, 'stat_${name}') + mut stat := Stats.from_data(mut data, 'stat_${name}') stat.update() // bins ndiv := [10, 10] // TODO: make this a parameter - bins := gm.new_bins(stat.min_x, stat.max_x, ndiv) // TODO: make sure minx and maxx are 2D or 3D; i.e. nb_features ≤ 2 + bins := gm.Bins.new(stat.min_x, stat.max_x, ndiv) // TODO: make sure minx and maxx are 2D or 3D; i.e. nb_features ≤ 2 mut o := Kmeans{ name: name data: data diff --git a/ml/kmeans_test.v b/ml/kmeans_test.v index bc834a657..60c358641 100644 --- a/ml/kmeans_test.v +++ b/ml/kmeans_test.v @@ -2,7 +2,7 @@ module ml fn test_kmeans_01() { // data - mut data := data_from_raw_x([ + mut data := Data.from_raw_x([ [0.1, 0.7], [0.3, 0.7], [0.1, 0.9], @@ -15,7 +15,7 @@ fn test_kmeans_01() { // model nb_classes := 2 - mut model := new_kmeans(mut data, nb_classes, 'kmeans') + mut model := Kmeans.new(mut data, nb_classes, 'kmeans') model.set_centroids([ // class 0 [0.4, 0.6], diff --git a/ml/knn.v b/ml/knn.v index 62a1239bf..3ab026899 100644 --- a/ml/knn.v +++ b/ml/knn.v @@ -25,13 +25,13 @@ mut: distance f64 } -// new_knn accepts a `vml.ml.Data` parameter called `data`, that will be used +// KNN.new accepts a `vml.ml.Data` parameter called `data`, that will be used // to predict values with `KNN.predict`. You can use the following piece of code to // make your life easier: -// ```mut knn := new_knn(mut data_from_raw_xy_sep([[0.0, 0.0], [10.0, 10.0]], [0.0, 1.0]))``` +// ```mut knn := KNN.new(mut Data.from_raw_xy_sep([[0.0, 0.0], [10.0, 10.0]], [0.0, 1.0]))``` // If you predict with `knn.predict(1, [9.0, 9.0])`, it should return 1.0 as it is the closest // to [10.0, 10.0] (which is class 1.0). -pub fn new_knn(mut data Data[f64], name string) !&KNN { +pub fn KNN.new(mut data Data[f64], name string) !&KNN { if data.x.data.len == 0 { return errors.error('with name ${name} expects `data.x` to have at least one element.', .einval) diff --git a/ml/knn_test.v b/ml/knn_test.v index 95f81b686..64070375d 100644 --- a/ml/knn_test.v +++ b/ml/knn_test.v @@ -19,8 +19,8 @@ fn test_knn_predict() { 1, 1, ] - mut data := data_from_raw_xy_sep(x, y)! - mut knn := new_knn(mut data, 'knn')! + mut data := Data.from_raw_xy_sep(x, y)! + mut knn := KNN.new(mut data, 'knn')! knn.train() assert knn.predict(k: 1, to_pred: [0.333333, 0.66666])! == 0.0 assert knn.predict(k: 1, to_pred: [11.0, 9.3])! == 1.0 @@ -43,16 +43,16 @@ fn test_knn_predict_with_data_change() { 1, 1, ] - mut data := data_from_raw_xy_sep(x, y)! + mut data := Data.from_raw_xy_sep(x, y)! - mut knn := new_knn(mut data, 'knn')! + mut knn := KNN.new(mut data, 'knn')! assert knn.predict(k: 1, to_pred: [0.333333, 0.66666])! == 0.0 assert knn.predict(k: 1, to_pred: [11.0, 9.3])! == 1.0 x << [1.0, 2.0] y << 1 - m := la.matrix_deep2(x) + m := la.Matrix.deep2(x) data.set(m, y)! assert knn.predict(k: 1, to_pred: [0.333333, 0.66666])! == 0.0 @@ -83,8 +83,8 @@ fn test_knn_predict_with_weights() { 2: 1 3: 1 } - mut train_data := data_from_raw_xy_sep(x, y)! - mut knn := new_knn(mut train_data, 'knn')! + mut train_data := Data.from_raw_xy_sep(x, y)! + mut knn := KNN.new(mut train_data, 'knn')! knn.set_weights(w)! assert knn.predict(k: 5, to_pred: [9.8])! == 2.0 diff --git a/ml/linreg.v b/ml/linreg.v index be38899e4..b712d83ba 100644 --- a/ml/linreg.v +++ b/ml/linreg.v @@ -18,14 +18,14 @@ pub mut: params &ParamsReg[f64] } -// new_lin_reg returns a new LinReg object +// LinReg.new returns a new LinReg object // Input: // data -- x,y data // name -- unique name of this (observer) object -pub fn new_lin_reg(mut data Data[f64], name string) &LinReg { - mut stat := stat_from_data(mut data, 'stat_' + name) +pub fn LinReg.new(mut data Data[f64], name string) &LinReg { + mut stat := Stats.from_data(mut data, 'stat_' + name) stat.update() - params := new_params_reg[f64](data.nb_features) + params := ParamsReg.new[f64](data.nb_features) mut reg := &LinReg{ name: name data: data @@ -110,8 +110,8 @@ pub fn (mut o LinReg) train() { r = la.matrix_tr_vector_mul(1.0, x, y) // r := a = xᵀy r = la.vector_add(1.0, r, -t * m_1, s) // r := a - (t/m)s // K matrix - mut b := la.new_matrix[f64](n, n) - mut k := la.new_matrix[f64](n, n) + mut b := la.Matrix.new[f64](n, n) + mut k := la.Matrix.new[f64](n, n) b = la.vector_vector_tr_mul(1.0 * m_1, s, s) // b := (1/m) ssᵀ la.matrix_tr_matrix_mul(mut k, 1, x, x) // k := A = xᵀx la.matrix_add(mut k, 1, k, -1, b) // k := A - b diff --git a/ml/linreg_test.v b/ml/linreg_test.v index 2c9e1f00f..04537d688 100644 --- a/ml/linreg_test.v +++ b/ml/linreg_test.v @@ -25,8 +25,8 @@ fn test_lin_reg() { [1.43, 94.98], [0.95, 87.33], ] - mut data := data_from_raw_xy(xy)! - mut reg := new_lin_reg(mut data, 'linear regression') + mut data := Data.from_raw_xy(xy)! + mut reg := LinReg.new(mut data, 'linear regression') reg.train() // TODO: Fix this test @@ -56,8 +56,8 @@ fn test_lin_reg_pred() { [1.43, 94.98], [0.95, 87.33], ] - mut data := data_from_raw_xy(xy)! - mut reg := new_lin_reg(mut data, 'linear regression') + mut data := Data.from_raw_xy(xy)! + mut reg := LinReg.new(mut data, 'linear regression') // set regularization parameter reg.params.set_lambda(1e12) // very high bias => constant line diff --git a/ml/logreg.v~ b/ml/logreg.v~ index fbf5f2cad..6b314dd41 100644 --- a/ml/logreg.v~ +++ b/ml/logreg.v~ @@ -18,13 +18,13 @@ mut: hmy []f64 // vector e = h(l) - y [nb_samples] } -// new_log_reg returns a new LogReg object +// LogReg.new returns a new LogReg object // Input: // data -- x,y data // params -- θ, b, λ // name -- unique name of this (observer) object -pub fn new_log_reg(mut data Data, params &ParamsReg, name string) LogReg { - mut stat := stat_from_data(mut data, "stat_" + name) +pub fn LogReg.new(mut data Data, params &ParamsReg, name string) LogReg { + mut stat := Stats.from_data(mut data, "stat_" + name) stat.update() mut log_reg := LogReg{ name: name @@ -123,8 +123,8 @@ pub fn (o LogReg) allocate_hessian() ([]f64, []f64, la.Matrix, la.Matrix) { n := o.data.nb_features d := []f64{len: m} v := []f64{len: n} - dm := la.new_matrix(m, n) - hm := la.new_matrix(n, n) + dm := la.Matrix.new(m, n) + hm := la.Matrix.new(n, n) return d, v, dm, hm } diff --git a/ml/nlp/lancaster_stemmer.v b/ml/nlp/lancaster_stemmer.v index a70b2f0b7..826df2b08 100644 --- a/ml/nlp/lancaster_stemmer.v +++ b/ml/nlp/lancaster_stemmer.v @@ -140,9 +140,9 @@ pub mut: ] } -// new_lancaster_stemmer returns a LancasterStemmer struct with a +// LancasterStemmer.new returns a LancasterStemmer struct with a // predefined set of stemming rules. -pub fn new_lancaster_stemmer(strip_prefix bool) LancasterStemmer { +pub fn LancasterStemmer.new(strip_prefix bool) LancasterStemmer { return LancasterStemmer{ strip_prefix: strip_prefix } diff --git a/ml/paramsreg.v b/ml/paramsreg.v index 76ad4e885..eeeb17ad3 100644 --- a/ml/paramsreg.v +++ b/ml/paramsreg.v @@ -19,8 +19,8 @@ pub mut: bkp_degree int // copy of degree } -// new_params_reg returns a new object to hold regression parameters -pub fn new_params_reg[T](nb_features int) &ParamsReg[T] { +// ParamsReg.new returns a new object to hold regression parameters +pub fn ParamsReg.new[T](nb_features int) &ParamsReg[T] { theta := []T{len: nb_features} bkp_theta := []T{len: nb_features} return &ParamsReg[T]{ diff --git a/ml/paramsreg_test.v b/ml/paramsreg_test.v index b8376045d..4143faa67 100644 --- a/ml/paramsreg_test.v +++ b/ml/paramsreg_test.v @@ -4,7 +4,7 @@ import vsl.float.float64 fn test_params_reg() { nb_features := 3 - mut params := new_params_reg[f64](nb_features) + mut params := ParamsReg.new[f64](nb_features) params.theta[0] = 1 params.theta[1] = 2 params.theta[2] = 3 diff --git a/ml/workspace.v b/ml/workspace.v index d3668d6bf..71adf6cd6 100644 --- a/ml/workspace.v +++ b/ml/workspace.v @@ -28,7 +28,7 @@ pub mut: } // stat returns a new Stat object -pub fn stat_from_data[T](mut data Data[T], name string) &Stat[T] { +pub fn Stats.from_data[T](mut data Data[T], name string) &Stat[T] { mut o := &Stat[T]{ name: name data: data diff --git a/ml/workspace_test.v b/ml/workspace_test.v index 60a981eee..1c445bf98 100644 --- a/ml/workspace_test.v +++ b/ml/workspace_test.v @@ -38,10 +38,10 @@ fn test_stat_01() { [1.43, 94.98], [0.95, 87.33], ] - mut data := data_from_raw_xy(xy)! + mut data := Data.from_raw_xy(xy)! // stat - mut stat := stat_from_data(mut data, 'stat') + mut stat := Stats.from_data(mut data, 'stat') // notify update data.notify_update() diff --git a/mpi/mpi_default.c.v b/mpi/mpi_default.c.v index 5c71fb10f..c1fb96d72 100644 --- a/mpi/mpi_default.c.v +++ b/mpi/mpi_default.c.v @@ -40,10 +40,10 @@ pub fn world_size() int { // Communicator holds the World Communicator or a subset Communicator pub struct Communicator {} -// new_communicator creates a new communicator or returns the World Communicator +// Communicator.new creates a new communicator or returns the World Communicator // ranks -- World indices of processors in this Communicator. // use nil or empty to get the World Communicator -pub fn new_communicator(ranks []int) !&Communicator { +pub fn Communicator.new(ranks []int) !&Communicator { return errors.error('MPI is not supported on this platform', .efailed) } diff --git a/mpi/mpi_nix.c.v b/mpi/mpi_nix.c.v index 938d4fc57..b6074401c 100644 --- a/mpi/mpi_nix.c.v +++ b/mpi/mpi_nix.c.v @@ -81,11 +81,11 @@ mut: group MPI_Group } -// new_communicator creates a new communicator or returns the World Communicator +// Communicator.new creates a new communicator or returns the World Communicator // ranks -- World indices of processors in this Communicator. // use nil or empty to get the World Communicator // Note there is currently no means to use groups. -pub fn new_communicator(ranks []int) !&Communicator { +pub fn Communicator.new(ranks []int) !&Communicator { mut o := &Communicator{ comm: MPI_Comm(C.MPI_COMM_WORLD) group: unsafe { nil } diff --git a/roots/bisection.v b/roots/bisection.v index 7de6d037f..290be5d81 100644 --- a/roots/bisection.v +++ b/roots/bisection.v @@ -28,8 +28,8 @@ pub struct BisectionParams { n_max int = 100 } -// new_bisection creates a new Bisection object with the given parameters -pub fn new_bisection(f func.Fn, params BisectionParams) &Bisection { +// Bisection.new creates a new Bisection object with the given parameters +pub fn Bisection.new(f func.Fn, params BisectionParams) &Bisection { return &Bisection{ f: f xmin: params.xmin diff --git a/roots/brent.v b/roots/brent.v index 177bb9290..8df75e105 100644 --- a/roots/brent.v +++ b/roots/brent.v @@ -22,8 +22,8 @@ pub mut: n_iter int } -// new_brent creates a new Brent object -pub fn new_brent(f func.Fn) &Brent { +// Brent.new creates a new Brent object +pub fn Brent.new(f func.Fn) &Brent { return &Brent{ f: f tol: 1e-6 diff --git a/roots/roots_test.v b/roots/roots_test.v index ecf97b7c8..b0a6a9257 100644 --- a/roots/roots_test.v +++ b/roots/roots_test.v @@ -19,8 +19,8 @@ fn fdf_cos(x f64, _ []f64) (f64, f64) { } fn test_root_bisection() { - f := func.new_func(f: f_cos) - mut solver := new_bisection(f, + f := func.Fn.new(f: f_cos) + mut solver := Bisection.new(f, xmin: 0.0 xmax: 3.0 epsrel: roots.epsrel @@ -33,7 +33,7 @@ fn test_root_bisection() { fn test_root_newton() { x0 := f64(0.5) - f := func.new_func_fdf(fdf: fdf_cos) + f := func.FnFdf.new(fdf: fdf_cos) result := newton(f, x0, roots.epsrel, roots.epsabs, roots.n_max)! assert float64.soclose(result, math.pi / 2.00, roots.epsabs) } diff --git a/vcl/kernel.c.v b/vcl/kernel.c.v index 44de929ff..df57ec373 100644 --- a/vcl/kernel.c.v +++ b/vcl/kernel.c.v @@ -20,7 +20,10 @@ pub fn (d &Device) kernel(name string) !&Kernel { if ret == invalid_kernel_name { return error("kernel with name '${name}' not found") } - return new_kernel(d, k) + return &Kernel{ + d: d + k: k + } } pub struct UnsupportedArgumentTypeError { @@ -34,7 +37,7 @@ pub fn (err UnsupportedArgumentTypeError) msg() string { return 'cl: unsupported argument type for index ${err.index}: ${err.value}' } -fn new_unsupported_argument_type_error(index int, value ArgumentType) IError { +fn UnsupportedArgumentTypeError.new(index int, value ArgumentType) IError { return UnsupportedArgumentTypeError{ index: index value: value @@ -95,13 +98,6 @@ fn release_kernel(k &Kernel) { cl_release_kernel(k.k) } -fn new_kernel(d &Device, k ClKernel) &Kernel { - return &Kernel{ - d: d - k: k - } -} - fn (k &Kernel) set_args(args ...ArgumentType) ! { for i, arg in args { k.set_arg(i, arg)! @@ -180,7 +176,7 @@ fn (k &Kernel) set_arg(index int, arg ArgumentType) ! { return k.set_arg_buffer(index, arg.buf) } else { - return new_unsupported_argument_type_error(index, arg) + return UnsupportedArgumentTypeError.new(index, arg) } } } diff --git a/vcl/vcl.c.v b/vcl/vcl.c.v index 5e15bf8e5..a7ae3c861 100644 --- a/vcl/vcl.c.v +++ b/vcl/vcl.c.v @@ -19,7 +19,7 @@ pub fn get_devices(device_type DeviceType) ![]&Device { return error_from_code(ret) } for d in device_ids { - device := new_device(d)! + device := Device.new(d)! devices << device } } @@ -36,7 +36,7 @@ pub fn get_default_device() !&Device { if ret != success { return error_from_code(ret) } - return new_device(id) + return Device.new(id) } fn get_platforms() ![]ClPlatformId { @@ -50,7 +50,7 @@ fn get_platforms() ![]ClPlatformId { return error_or_default(ret, platform_ids) } -fn new_device(id ClDeviceId) !&Device { +fn Device.new(id ClDeviceId) !&Device { mut d := &Device{ id: id }