From afa896519a19bc79fc88bc32d13fa826217f6587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Have=CC=81?= Date: Mon, 16 Oct 2023 17:46:10 +0200 Subject: [PATCH] Add R binding for LinearRegression --- DESCRIPTION | 2 + NAMESPACE | 5 + R/extendr-wrappers.R | 35 +- src/rust/Cargo.lock | 1118 ++++++++++++++++++++++- src/rust/Cargo.toml | 6 +- src/rust/build.rs | 23 + src/rust/src/fit_and_predict.rs | 2 +- src/rust/src/lib.rs | 19 +- src/rust/src/linear_regression.rs | 2 +- src/rust/src/r_binding/conversion.rs | 33 + src/rust/src/r_binding/mod.rs | 61 ++ src/rust/src/version.rs | 73 ++ tests/testthat.R | 6 + tests/testthat/test-full_version.R | 4 + tests/testthat/test-linear_regression.R | 9 + 15 files changed, 1367 insertions(+), 31 deletions(-) create mode 100644 src/rust/build.rs create mode 100644 src/rust/src/r_binding/conversion.rs create mode 100644 src/rust/src/r_binding/mod.rs create mode 100644 src/rust/src/version.rs create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-full_version.R create mode 100644 tests/testthat/test-linear_regression.R diff --git a/DESCRIPTION b/DESCRIPTION index 9405658..78d05ae 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,9 @@ Description: New package structure to refactor libkriging packages = 2) Encoding: UTF-8 +Suggests: testthat, utils, foreach SystemRequirements: Cargo (Rust's package manager), rustc URL: https://github.com/libKriging Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.1 +Config/rextendr/version: 0.3.1.9000 diff --git a/NAMESPACE b/NAMESPACE index 6ae9268..33f6540 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,2 +1,7 @@ # Generated by roxygen2: do not edit by hand +S3method("$",LinearRegression) +S3method("[[",LinearRegression) +export(LinearRegression) +export(full_version) +useDynLib(libkrigingtemplate, .registration = TRUE) diff --git a/R/extendr-wrappers.R b/R/extendr-wrappers.R index f7fde92..7ee451d 100644 --- a/R/extendr-wrappers.R +++ b/R/extendr-wrappers.R @@ -1,8 +1,39 @@ +# Generated by extendr: Do not edit by hand + +# nolint start + +# +# This file was created with the following call: +# .Call("wrap__make_libkrigingtemplate_wrappers", use_symbols = TRUE, package_name = "libkrigingtemplate") + #' @docType package #' @usage NULL #' @useDynLib libkrigingtemplate, .registration = TRUE NULL -#' Return string `"Hello world!"` to R. +#' Return version string +#' @export +full_version <- function() .Call(wrap__full_version) + +linear_regression_fit1 <- function(y, x) invisible(.Call(wrap__linear_regression_fit1, y, x)) + +linear_regression_fit2 <- function(y, x) invisible(.Call(wrap__linear_regression_fit2, y, x)) + +#' First linear regression implementation test +#' @export +LinearRegression <- new.env(parent = emptyenv()) + +LinearRegression$fit <- function(y, x) .Call(wrap__LinearRegression__fit, y, x) + +LinearRegression$predict <- function(x) .Call(wrap__LinearRegression__predict, self, x) + +#' @rdname LinearRegression +#' @usage NULL +#' @export +`$.LinearRegression` <- function (self, name) { func <- LinearRegression[[name]]; environment(func) <- environment(); func } + #' @export -hello_world <- function() .Call(wrap__hello_world) +`[[.LinearRegression` <- `$.LinearRegression` + + +# nolint end diff --git a/src/rust/Cargo.lock b/src/rust/Cargo.lock index 7f0bab4..804db86 100644 --- a/src/rust/Cargo.lock +++ b/src/rust/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aho-corasick" version = "1.1.2" @@ -26,12 +32,50 @@ dependencies = [ "num-traits", ] +[[package]] +name = "arc-swap" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6" + [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + +[[package]] +name = "bstr" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c79ad7fb2dd38f3dabd76b09c6a5a20c038fc0213ef1e9afd30eb777f120f019" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + +[[package]] +name = "btoi" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd6407f73a9b8b6162d8a2ef999fe6afd7cc15902ebf42c5cd296addf17e0ad" +dependencies = [ + "num-traits", +] + [[package]] name = "bytemuck" version = "1.14.0" @@ -44,6 +88,46 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clru" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8191fa7302e03607ff0e237d4246cc043ff5b3cb9409d995172ba3bea16b807" + +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "deranged" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f32d04922c60427da6f9fef14d042d9edddef64cb9d4ce0d64d0685fbeb1fd3" +dependencies = [ + "powerfmt", +] + +[[package]] +name = "dunce" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" + +[[package]] +name = "errno" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "extendr-api" version = "0.6.0" @@ -52,6 +136,7 @@ checksum = "c7d55a0174c4df17c467fb59b3f836bec31d1af6d56c4182cfdf34a62d1553a4" dependencies = [ "extendr-macros", "libR-sys", + "ndarray", "once_cell", "paste", ] @@ -67,6 +152,52 @@ dependencies = [ "syn 2.0.32", ] +[[package]] +name = "faster-hex" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "239f7bfb930f820ab16a9cd95afc26f88264cf6905c960b340a615384aa3338a" +dependencies = [ + "serde", +] + +[[package]] +name = "fastrand" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" + +[[package]] +name = "filetime" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4029edd3e734da6fe05b6cd7bd2960760a616bd2ddd0d59a0124746d6272af0" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "windows-sys", +] + +[[package]] +name = "flate2" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "form_urlencoded" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" +dependencies = [ + "percent-encoding", +] + [[package]] name = "futures" version = "0.3.28" @@ -163,21 +294,548 @@ dependencies = [ ] [[package]] -name = "getrandom" -version = "0.2.10" +name = "getrandom" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gix" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06a8c9f9452078f474fecd2880de84819b8c77224ab62273275b646bf785f906" +dependencies = [ + "gix-actor", + "gix-commitgraph", + "gix-config", + "gix-date", + "gix-diff", + "gix-discover", + "gix-features", + "gix-fs", + "gix-glob", + "gix-hash", + "gix-hashtable", + "gix-index", + "gix-lock", + "gix-macros", + "gix-object", + "gix-odb", + "gix-pack", + "gix-path", + "gix-ref", + "gix-refspec", + "gix-revision", + "gix-revwalk", + "gix-sec", + "gix-tempfile", + "gix-trace", + "gix-traverse", + "gix-url", + "gix-utils", + "gix-validate", + "once_cell", + "parking_lot", + "signal-hook", + "smallvec", + "thiserror", + "unicode-normalization", +] + +[[package]] +name = "gix-actor" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e8c6778cc03bca978b2575a03e04e5ba6f430a9dd9b0f1259f0a8a9a5e5cc66" +dependencies = [ + "bstr", + "btoi", + "gix-date", + "itoa", + "thiserror", + "winnow", +] + +[[package]] +name = "gix-bitmap" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ccab4bc576844ddb51b78d81b4a42d73e6229660fa614dfc3d3999c874d1959" +dependencies = [ + "thiserror", +] + +[[package]] +name = "gix-chunk" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b42ea64420f7994000130328f3c7a2038f639120518870436d31b8bde704493" +dependencies = [ + "thiserror", +] + +[[package]] +name = "gix-commitgraph" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4676ede3a7d37e7028e2889830349a6aca22efc1d2f2dd9fa3351c1a8ddb0c6a" +dependencies = [ + "bstr", + "gix-chunk", + "gix-features", + "gix-hash", + "memmap2", + "thiserror", +] + +[[package]] +name = "gix-config" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1108c4ac88248dd25cc8ab0d0dae796e619fb72d92f88e30e00b29d61bb93cc4" +dependencies = [ + "bstr", + "gix-config-value", + "gix-features", + "gix-glob", + "gix-path", + "gix-ref", + "gix-sec", + "memchr", + "once_cell", + "smallvec", + "thiserror", + "unicode-bom", + "winnow", +] + +[[package]] +name = "gix-config-value" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea7505b97f4d8e7933e29735a568ba2f86d8de466669d9f0e8321384f9972f47" +dependencies = [ + "bitflags 2.4.1", + "bstr", + "gix-path", + "libc", + "thiserror", +] + +[[package]] +name = "gix-date" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc7df669639582dc7c02737642f76890b03b5544e141caba68a7d6b4eb551e0d" +dependencies = [ + "bstr", + "itoa", + "thiserror", + "time", +] + +[[package]] +name = "gix-diff" +version = "0.35.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45e342d148373bd9070d557e6fb1280aeae29a3e05e32506682d027278501eb" +dependencies = [ + "gix-hash", + "gix-object", + "thiserror", +] + +[[package]] +name = "gix-discover" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da4cacda5ee9dd1b38b0e2506834e40e66c08cf050ef55c344334c76745f277b" +dependencies = [ + "bstr", + "dunce", + "gix-hash", + "gix-path", + "gix-ref", + "gix-sec", + "thiserror", +] + +[[package]] +name = "gix-features" +version = "0.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f414c99e1a7abc69b21f3225a6539d203b0513f1d1d448607c4ea81cdcf9ee59" +dependencies = [ + "crc32fast", + "flate2", + "gix-hash", + "gix-trace", + "libc", + "once_cell", + "prodash", + "sha1_smol", + "thiserror", + "walkdir", +] + +[[package]] +name = "gix-fs" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "404795da3d4c660c9ab6c3b2ad76d459636d1e1e4b37b0c7ff68eee898c298d4" +dependencies = [ + "gix-features", +] + +[[package]] +name = "gix-glob" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ac79c444193b0660fe0c0925d338bd338bd643e32138784dccfb12c628b892" +dependencies = [ + "bitflags 2.4.1", + "bstr", + "gix-features", + "gix-path", +] + +[[package]] +name = "gix-hash" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1884c7b41ea0875217c1be9ce91322f90bde433e91d374d0e1276073a51ccc60" +dependencies = [ + "faster-hex", + "thiserror", +] + +[[package]] +name = "gix-hashtable" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "409268480841ad008e81c17ca5a293393fbf9f2b6c2f85b8ab9de1f0c5176a16" +dependencies = [ + "gix-hash", + "hashbrown", + "parking_lot", +] + +[[package]] +name = "gix-index" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9599fc30b3d6aad231687a403f85dfa36ae37ccf1b68ee1f621ad5b7fc7a0d" +dependencies = [ + "bitflags 2.4.1", + "bstr", + "btoi", + "filetime", + "gix-bitmap", + "gix-features", + "gix-fs", + "gix-hash", + "gix-lock", + "gix-object", + "gix-traverse", + "itoa", + "memmap2", + "smallvec", + "thiserror", +] + +[[package]] +name = "gix-lock" +version = "9.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1568c3d90594c60d52670f325f5db88c2d572e85c8dd45fabc23d91cadb0fd52" +dependencies = [ + "gix-tempfile", + "gix-utils", + "thiserror", +] + +[[package]] +name = "gix-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d8acb5ee668d55f0f2d19a320a3f9ef67a6999ad483e11135abcc2464ed18b6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.32", +] + +[[package]] +name = "gix-object" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e5528d5b2c984044d547e696e44a8c45fa122e83cd8c2ac1da69bd474336be8" +dependencies = [ + "bstr", + "btoi", + "gix-actor", + "gix-date", + "gix-features", + "gix-hash", + "gix-validate", + "itoa", + "smallvec", + "thiserror", + "winnow", +] + +[[package]] +name = "gix-odb" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0446eca295459deb3d6dd6ed7d44a631479f1b7381d8087166605c7a9f717c6" +dependencies = [ + "arc-swap", + "gix-date", + "gix-features", + "gix-hash", + "gix-object", + "gix-pack", + "gix-path", + "gix-quote", + "parking_lot", + "tempfile", + "thiserror", +] + +[[package]] +name = "gix-pack" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be19ee650300d7cbac5829b637685ec44a8d921a7c2eaff8a245d8f2f008870c" +dependencies = [ + "clru", + "gix-chunk", + "gix-features", + "gix-hash", + "gix-hashtable", + "gix-object", + "gix-path", + "gix-tempfile", + "memmap2", + "parking_lot", + "smallvec", + "thiserror", +] + +[[package]] +name = "gix-path" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a1d370115171e3ae03c5c6d4f7d096f2981a40ddccb98dfd704c773530ba73b" +dependencies = [ + "bstr", + "gix-trace", + "home", + "once_cell", + "thiserror", +] + +[[package]] +name = "gix-quote" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "475c86a97dd0127ba4465fbb239abac9ea10e68301470c9791a6dd5351cdc905" +dependencies = [ + "bstr", + "btoi", + "thiserror", +] + +[[package]] +name = "gix-ref" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cccbfa8d5cd9b86465f27a521e0c017de54b92d9fd37c143e49c658a2f04f3a" +dependencies = [ + "gix-actor", + "gix-date", + "gix-features", + "gix-fs", + "gix-hash", + "gix-lock", + "gix-object", + "gix-path", + "gix-tempfile", + "gix-validate", + "memmap2", + "thiserror", + "winnow", +] + +[[package]] +name = "gix-refspec" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678ba30d95baa5462df9875628ed40655d5f5b8aba7028de86ed57f36e762c6c" +dependencies = [ + "bstr", + "gix-hash", + "gix-revision", + "gix-validate", + "smallvec", + "thiserror", +] + +[[package]] +name = "gix-revision" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3e80a5992ae446fe1745dd26523b86084e3f1b6b3e35377fe09b4f35ac8f151" +dependencies = [ + "bstr", + "gix-date", + "gix-hash", + "gix-hashtable", + "gix-object", + "gix-revwalk", + "gix-trace", + "thiserror", +] + +[[package]] +name = "gix-revwalk" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b806349bc1f668e09035800e07ac8045da4e39a8925a245d93142c4802224ec1" +dependencies = [ + "gix-commitgraph", + "gix-date", + "gix-hash", + "gix-hashtable", + "gix-object", + "smallvec", + "thiserror", +] + +[[package]] +name = "gix-sec" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92b9542ac025a8c02ed5d17b3fc031a111a384e859d0be3532ec4d58c40a0f28" +dependencies = [ + "bitflags 2.4.1", + "gix-path", + "libc", + "windows", +] + +[[package]] +name = "gix-tempfile" +version = "9.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2762b91ff95e27ff3ea95758c0d4efacd7435a1be3629622928b8276de0f72a8" +dependencies = [ + "gix-fs", + "libc", + "once_cell", + "parking_lot", + "signal-hook", + "signal-hook-registry", + "tempfile", +] + +[[package]] +name = "gix-trace" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b6d623a1152c3facb79067d6e2ecdae48130030cf27d6eb21109f13bd7b836" + +[[package]] +name = "gix-traverse" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ec6358f8373fb018af8fc96c9d2ec6a5b66999e2377dc40b7801351fec409ed" +dependencies = [ + "gix-commitgraph", + "gix-date", + "gix-hash", + "gix-hashtable", + "gix-object", + "gix-revwalk", + "smallvec", + "thiserror", +] + +[[package]] +name = "gix-url" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c79d595b99a6c7ab274f3c991735a0c0f5a816a3da460f513c48edf1c7bf2cc" +dependencies = [ + "bstr", + "gix-features", + "gix-path", + "home", + "thiserror", + "url", +] + +[[package]] +name = "gix-utils" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b85d89dc728613e26e0ed952a19583744e7f5240fcd4aa30d6c824ffd8b52f0f" +dependencies = [ + "fastrand", +] + +[[package]] +name = "gix-validate" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e05cab2b03a45b866156e052aa38619f4ece4adcb2f79978bfc249bc3b21b8c5" +dependencies = [ + "bstr", + "thiserror", +] + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "hashbrown" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" + +[[package]] +name = "home" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "idna" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" dependencies = [ - "cfg-if", - "libc", - "wasi", + "unicode-bidi", + "unicode-normalization", ] [[package]] -name = "glob" -version = "0.3.1" +name = "itoa" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "libR-sys" @@ -198,9 +856,11 @@ dependencies = [ "anyhow", "extendr-api", "nalgebra", + "ndarray", "rand", "rand_distr", "rstest", + "vergen", ] [[package]] @@ -209,6 +869,22 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "linux-raw-sys" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" + +[[package]] +name = "lock_api" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "matrixmultiply" version = "0.3.8" @@ -225,6 +901,24 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +[[package]] +name = "memmap2" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f49388d20533534cd19360ad3d6a7dadc885944aa802ba3995040c5ec11288c6" +dependencies = [ + "libc", +] + +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", +] + [[package]] name = "nalgebra" version = "0.32.3" @@ -252,6 +946,19 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ndarray" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32" +dependencies = [ + "matrixmultiply", + "num-complex", + "num-integer", + "num-traits", + "rawpointer", +] + [[package]] name = "num-complex" version = "0.4.4" @@ -292,18 +999,56 @@ dependencies = [ "libm", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "once_cell" version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "paste" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" +[[package]] +name = "percent-encoding" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" + [[package]] name = "pin-project-lite" version = "0.2.13" @@ -316,6 +1061,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -331,6 +1082,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "prodash" +version = "26.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "794b5bf8e2d19b53dcdcec3e4bba628e20f5b6062503ba89281fa7037dd7bbcf" + [[package]] name = "quote" version = "1.0.33" @@ -386,6 +1143,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" +[[package]] +name = "redox_syscall" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "regex" version = "1.10.1" @@ -459,6 +1225,25 @@ dependencies = [ "semver", ] +[[package]] +name = "rustix" +version = "0.38.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed" +dependencies = [ + "bitflags 2.4.1", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "rustversion" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" + [[package]] name = "safe_arch" version = "0.7.1" @@ -468,12 +1253,72 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "semver" version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" +[[package]] +name = "serde" +version = "1.0.189" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.189" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.32", +] + +[[package]] +name = "sha1_smol" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" + +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "simba" version = "0.8.1" @@ -496,6 +1341,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" + [[package]] name = "syn" version = "1.0.109" @@ -518,18 +1369,152 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempfile" +version = "3.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef" +dependencies = [ + "cfg-if", + "fastrand", + "redox_syscall", + "rustix", + "windows-sys", +] + +[[package]] +name = "thiserror" +version = "1.0.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.32", +] + +[[package]] +name = "time" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a34ab300f2dee6e562c10a046fc05e358b29f9bf92277f30c3c8d82275f6f5" +dependencies = [ + "deranged", + "itoa", + "libc", + "num_threads", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ad70d68dba9e1f8aceda7aa6711965dfec1cac869f311a51bd08b3a2ccbce20" +dependencies = [ + "time-core", +] + +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "typenum" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicode-bidi" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" + +[[package]] +name = "unicode-bom" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98e90c70c9f0d4d1ee6d0a7d04aa06cb9bbd53d8cfbdd62a0269a7c2eb640552" + [[package]] name = "unicode-ident" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "url" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "vergen" +version = "8.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85e7dc29b3c54a2ea67ef4f953d5ec0c4085035c0ae2d325be1c0d2144bd9f16" +dependencies = [ + "anyhow", + "gix", + "rustc_version", + "rustversion", + "time", +] + +[[package]] +name = "walkdir" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -545,3 +1530,118 @@ dependencies = [ "bytemuck", "safe_arch", ] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "winnow" +version = "0.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3b801d0e0a6726477cc207f60162da452f3a95adb368399bef20a946e06f65c" +dependencies = [ + "memchr", +] diff --git a/src/rust/Cargo.toml b/src/rust/Cargo.toml index 27ef517..13933e6 100644 --- a/src/rust/Cargo.toml +++ b/src/rust/Cargo.toml @@ -8,10 +8,14 @@ crate-type = ['staticlib'] [dependencies] anyhow = "1.0" -extendr-api = '0.6' +extendr-api = { version = "0.6", features = ["ndarray"] } nalgebra = "0.32" +ndarray = "*" rand = "0.8" rand_distr = "0.4" +[build-dependencies] +vergen = { version = "8", features = ["git", "gitoxide", "cargo", "rustc", "build"] } + [dev-dependencies] rstest = "0.18" diff --git a/src/rust/build.rs b/src/rust/build.rs new file mode 100644 index 0000000..921fa17 --- /dev/null +++ b/src/rust/build.rs @@ -0,0 +1,23 @@ +/* + * Copyright Nuant (c) 2022. + */ + +use std::error::Error; +use vergen::EmitBuilder; + +fn main() -> Result<(), Box> { + EmitBuilder::builder() + .build_timestamp() + .cargo_debug() + .cargo_target_triple() + .cargo_features() + .git_branch() + .git_commit_date() + .git_commit_timestamp() + .git_describe(true, true, Some("v[0-9]*")) + .git_sha(false) + .rustc_semver() + .rustc_channel() + .emit()?; + Ok(()) +} diff --git a/src/rust/src/fit_and_predict.rs b/src/rust/src/fit_and_predict.rs index 56d53fd..83883c8 100644 --- a/src/rust/src/fit_and_predict.rs +++ b/src/rust/src/fit_and_predict.rs @@ -1,5 +1,5 @@ use crate::math_types::{ColVec, Mat}; -use anyhow::{anyhow, Result}; +use anyhow::Result; pub trait FitAndPredict { fn fit(v: &ColVec, x: Mat) -> Result diff --git a/src/rust/src/lib.rs b/src/rust/src/lib.rs index 8638104..f46e9fb 100644 --- a/src/rust/src/lib.rs +++ b/src/rust/src/lib.rs @@ -2,24 +2,9 @@ mod fit_and_predict; mod linear_regression; mod math_types; mod math_utils; +mod r_binding; mod rectangle_solve; - -use extendr_api::prelude::*; - -/// Return string `"Hello world!"` to R. -/// @export -#[extendr] -fn hello_world() -> &'static str { - "Hello world!" -} - -// Macro to generate exports. -// This ensures exported functions are registered with R. -// See corresponding C code in `entrypoint.c`. -extendr_module! { - mod libkrigingtemplate; - fn hello_world; -} +mod version; #[cfg(test)] mod tests { diff --git a/src/rust/src/linear_regression.rs b/src/rust/src/linear_regression.rs index 44a4378..80a3a07 100644 --- a/src/rust/src/linear_regression.rs +++ b/src/rust/src/linear_regression.rs @@ -3,7 +3,7 @@ use crate::math_types::{ColVec, Mat}; use crate::rectangle_solve::rectangle_solve; use anyhow::{anyhow, Result}; -struct LinearRegression { +pub struct LinearRegression { coef: ColVec, sig2: f64, stderrest: ColVec, diff --git a/src/rust/src/r_binding/conversion.rs b/src/rust/src/r_binding/conversion.rs new file mode 100644 index 0000000..4e89bf4 --- /dev/null +++ b/src/rust/src/r_binding/conversion.rs @@ -0,0 +1,33 @@ +use crate::math_types::{ColVec, Mat}; +use anyhow::{anyhow, Result}; +use extendr_api::{RArray, RMatrix}; + +// Needs additional check to ensure ncols == 1 +pub type RVector = RArray; + +pub fn to_mat(x: RMatrix) -> Result { + Ok(Mat::from_row_slice( + x.nrows(), + x.ncols(), + x.as_real_slice() + .ok_or_else(|| anyhow!("Cannot extract data"))?, + )) +} + +pub fn to_colvec(x: RArray) -> Result { + if x.ncols() == 1 { + Ok(ColVec::from_row_slice( + x.as_real_slice() + .ok_or_else(|| anyhow!("Cannot extract data"))?, + )) + } else { + Err(anyhow!("Single column matrix expected")) + } +} + +pub fn from_colvec(x: ColVec) -> Result> { + Ok(RVector::new_matrix(x.nrows(), x.ncols(), |i, _j| { + debug_assert_eq!(x.ncols(), 1); + x[i] + })) +} diff --git a/src/rust/src/r_binding/mod.rs b/src/rust/src/r_binding/mod.rs new file mode 100644 index 0000000..81a981d --- /dev/null +++ b/src/rust/src/r_binding/mod.rs @@ -0,0 +1,61 @@ +use crate::fit_and_predict::FitAndPredict; +use crate::r_binding::conversion::RVector; +use anyhow::Result; +use conversion::{from_colvec, to_colvec, to_mat}; +use extendr_api::prelude::*; + +mod conversion; + +/// Return version string +/// @export +#[extendr] +fn full_version() -> String { + crate::version::full_version() +} + +struct LinearRegression(crate::linear_regression::LinearRegression); + +/// First linear regression implementation test +/// @export +#[extendr] +impl LinearRegression { + pub fn fit(y: RVector, x: RMatrix) -> Result { + Ok(LinearRegression( + crate::linear_regression::LinearRegression::fit(&to_colvec(y)?, to_mat(x)?)?, + )) + } + + pub fn predict(&self, x: RMatrix) -> Result { + // Could be a named list; cf https://docs.rs/extendr-api/latest/extendr_api/ + // let list = list!(a = 1, b = 2); + + let (a, b) = self.0.predict(&to_mat(x)?); + Ok(r!(List::from_values(&[ + r!(from_colvec(a)), + r!(from_colvec(b)) + ]))) + } +} + +#[extendr] +pub fn linear_regression_fit1(y: ArrayView1, x: ArrayView2) { + println!("{y:?}"); + println!("{x:?}"); +} + +#[extendr] +pub fn linear_regression_fit2(y: RArray, x: RMatrix) { + println!("{y:?}"); + println!("{x:?}"); +} + +// Macro to generate exports. +// This ensures exported functions are registered with R. +// See corresponding C code in `entrypoint.c`. +extendr_module! { + mod libkrigingtemplate; + fn full_version; + impl LinearRegression; + fn linear_regression_fit1; + fn linear_regression_fit2; +} diff --git a/src/rust/src/version.rs b/src/rust/src/version.rs new file mode 100644 index 0000000..2774667 --- /dev/null +++ b/src/rust/src/version.rs @@ -0,0 +1,73 @@ +pub fn short_version() -> String { + let version = format!("v{}", env!("CARGO_PKG_VERSION")); + let commit_description = env!("VERGEN_GIT_DESCRIBE"); + let component_version = format!("{}-{}", env!("CARGO_PKG_NAME"), version); + let note = if component_version == commit_description { + "release" + } else { + "development" + }; + + format!("{version} [{note}]") +} + +pub fn full_version() -> String { + let mut features = env!("VERGEN_CARGO_FEATURES"); + if features.is_empty() { + features = "default" + }; + + display_formatted_pack(features).join("\n") +} + +#[rustfmt::skip] +fn display_formatted_pack(features: &str, +) -> Vec { + vec![ + format!("{:->61}", ""), + format!("Package semver: {}", short_version()), + format!("Build timestamp: {}", env!("VERGEN_BUILD_TIMESTAMP")), + format!("Commit timestamp: {}", env!("VERGEN_GIT_COMMIT_TIMESTAMP")), + format!("Commit branch: {}", env!("VERGEN_GIT_BRANCH")), + format!("Commit SHA: {}", env!("VERGEN_GIT_SHA")), + format!("Commit description: {}", env!("VERGEN_GIT_DESCRIBE")), + format!("rustc version: {} [{}]", env!("VERGEN_RUSTC_SEMVER"), env!("VERGEN_RUSTC_CHANNEL")), + format!("Target: {}", env!("VERGEN_CARGO_TARGET_TRIPLE")), + format!("Profile: {}", get_build_profile_name()), + format!("Features: {features}"), + ] +} + +// cf https://doc.rust-lang.org/cargo/reference/profiles.html#custom-profiles +// The output for each profile will be placed in a directory of the same name as the profile +// in the target directory. +// As in the example above, the output would go into the target/release-lto directory. + +fn get_build_profile_name() -> String { + // The profile name is always the 3rd last part of the path (with 1 based indexing). + // e.g. /code/core/target/cli/build/my-build-info-9f91ba6f99d7a061/out + let profile_name = env!("OUT_DIR") + .split(std::path::MAIN_SEPARATOR) + .nth_back(3) + .unwrap_or_else(|| "unknown") + .to_string(); + + if env!("VERGEN_CARGO_DEBUG") + .parse::() + .unwrap_or_default() + { + format!("{profile_name} [with debug info]") + } else { + profile_name + } +} + +#[cfg(test)] +mod debug { + use crate::version::full_version; + + #[test] + fn display_version() { + println!("{}", full_version()) + } +} diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..894a774 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,6 @@ +## load dependencies +library(testthat) +library(libkrigingtemplate) + +## test package +test_check('libkrigingtemplate') \ No newline at end of file diff --git a/tests/testthat/test-full_version.R b/tests/testthat/test-full_version.R new file mode 100644 index 0000000..e89dd4a --- /dev/null +++ b/tests/testthat/test-full_version.R @@ -0,0 +1,4 @@ +library(libkrigingtemplate) +library(testthat) + +libkrigingtemplate::full_version() \ No newline at end of file diff --git a/tests/testthat/test-linear_regression.R b/tests/testthat/test-linear_regression.R new file mode 100644 index 0000000..4ed6801 --- /dev/null +++ b/tests/testthat/test-linear_regression.R @@ -0,0 +1,9 @@ +library(libkrigingtemplate) +library(testthat) + +nrows <- 20 +ncols <- 10 +X <- matrix(rnorm(nrows*ncols),nrow=nrows) +y <- matrix(runif(nrows)) +rl <- LinearRegression$fit(y, X) +px = rl$predict(X) \ No newline at end of file