Skip to content

Commit

Permalink
Merge pull request #77 from palatej/snapshot
Browse files Browse the repository at this point in the history
Splines, new jars
  • Loading branch information
annasmyk authored Dec 12, 2024
2 parents 3bc96ad + 012c441 commit 2deef78
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 29 deletions.
7 changes: 6 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export(autocorrelations)
export(autocorrelations_inverse)
export(autocorrelations_partial)
export(bowmanshenton)
export(bsplines)
export(calendar_td)
export(cdf_chi2)
export(cdf_gamma)
Expand Down Expand Up @@ -221,10 +222,14 @@ export(lp_variable)
export(ls_variable)
export(mad)
export(modelling_context)
export(monotonic_cspline)
export(national_calendar)
export(natural_cspline)
export(periodic.contrasts)
export(periodic.dummies)
export(periodic_splines)
export(periodic_bsplines)
export(periodic_cspline)
export(periodic_csplines)
export(r2jd_calendarts)
export(ramp_variable)
export(random_chi2)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ to [Semantic Versioning](https://semver.org/spec/v3.2.3.html).

## [Unreleased]

### Changed

* New JARS

### Added

* Spline functions (periodic, b-splines, cardinal splines)

## [3.3.0] - 2024-10-28

### Changed
Expand Down
112 changes: 107 additions & 5 deletions R/splines.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,121 @@
#' Period splines
#' Periodic B-Splines
#'
#' @param order Order of the splines (4 for cubic)
#' @param period Period of the splines (1 by default)
#' @param knots Knots of the splines (in [0, period[]])
#' @param pos Requested positions (in [0, period[]])
#' @param knots Knots of the splines (in [0, period[)
#' @param pos Requested positions (in [0, period[). The rows of the returned matrix
#' will correspond to those positions
#'
#' @return A matrix (len(pos) x len(knots))
#' @export
#'
#' @examples
periodic_splines <- function(order = 4, period = 1, knots, pos) {
#' s<-periodic_bsplines(knots = c(0,.2,.3, .9,.95), pos=seq(0,1,0.01))
#' matplot(s, type='l')
periodic_bsplines <- function(order = 4, period = 1, knots, pos) {
jm <- .jcall(
"jdplus/toolkit/base/r/math/BSplines", "Ljdplus/toolkit/base/core//math/matrices/Matrix;",
"jdplus/toolkit/base/r/math/BSplines", "Ljdplus/toolkit/base/api/math/matrices/Matrix;",
"periodic", as.integer(order), as.numeric(period), .jarray(as.numeric(knots)), .jarray(as.numeric(pos))
)
res <- .jd2r_matrix(jm)
return(res)
}

#' B-Splines
#'
#' @param order Order of the splines (4 for cubic)
#' @param knots Knots of the splines (in [0, period[)
#' @param pos Requested positions (in [0, period[). The rows of the returned matrix
#' will correspond to those positions
#'
#' @return A matrix (len(pos) x len(knots))
#' @export
#'
#' @examples
#' s<-bsplines(knots = c(0,.2,.3, .9,.95, 1), pos=seq(0,1,0.01))
#' matplot(s, type='l')
bsplines <- function(order = 4,knots, pos) {
jm <- .jcall(
"jdplus/toolkit/base/r/math/BSplines", "Ljdplus/toolkit/base/api/math/matrices/Matrix;",
"of", as.integer(order), .jarray(as.numeric(knots)), .jarray(as.numeric(pos))
)
res <- .jd2r_matrix(jm)
return(res)
}

#' Natural cubic spline
#'
#' @param x Abscissas of the knots
#' @param y Ordinates of the knots
#' @param pos Requested positions
#'
#' @return An array corresponding to the values of the spline at the requested positions
#' @export
#'
#' @examples
#' s<-natural_cspline(x = c(0,.2,.3, .9,.95), y= c(1,3,5,8,12), pos=seq(0,1,0.01))
#' plot(s, type='l')
natural_cspline <- function(x, y, pos) {
return (.jcall(
"jdplus/toolkit/base/r/math/CubicSplines", "[D",
"natural", .jarray(as.numeric(x)), .jarray(as.numeric(y)), .jarray(as.numeric(pos))
))
}

#' Monotonic cubic spline
#'
#' @param x Abscissas of the knots
#' @param y Ordinates of the knots
#' @param pos Requested positions
#'
#' @return An array corresponding to the values of the spline at the requested positions
#' @export
#'
#' @examples
#' s<-monotonic_cspline(x = c(0,.2,.3, .9,.95), y= c(1,3,5,8,12), pos=seq(0,1,0.01))
#' plot(s, type='l')
monotonic_cspline <- function(x, y, pos) {
return (.jcall(
"jdplus/toolkit/base/r/math/CubicSplines", "[D",
"monotonic", .jarray(as.numeric(x)), .jarray(as.numeric(y)), .jarray(as.numeric(pos))
))
}

#' Periodic cubic spline
#'
#' @param x Abscissas of the knots
#' @param y Ordinates of the knots
#' @param pos Requested positions
#'
#' @return An array corresponding to the values of the spline at the requested positions
#' @export
#'
#' @examples
#' s<-periodic_cspline(x = c(0,.2,.3, .9,.95, 1), y= c(1,3,8,5,12, 1), pos=seq(0,1,0.01))
#' plot(s, type='l')
periodic_cspline <- function(x, y, pos) {
return (.jcall(
"jdplus/toolkit/base/r/math/CubicSplines", "[D",
"periodic", .jarray(as.numeric(x)), .jarray(as.numeric(y)), .jarray(as.numeric(pos))
))
}

#' Periodic cardinal cubic splines
#'
#' @param x Abscissas of the knots
#' @param pos Requested positions
#' @return A matrix (len(pos) x len(knots))
#' @export
#'
#' @examples
#' s<-periodic_csplines(x = c(0,.2,.3, .9,.95, 1), pos=seq(0,1,0.01))
#' matplot(s, type='l')
periodic_csplines <- function(x, pos) {
jm <- .jcall(
"jdplus/toolkit/base/r/math/CubicSplines", "Ljdplus/toolkit/base/api/math/matrices/Matrix;",
"periodicCardinalSplines", .jarray(as.numeric(x)), .jarray(as.numeric(pos))
)
res <- .jd2r_matrix(jm)
return(res)
}

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
26 changes: 26 additions & 0 deletions man/bsplines.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/monotonic_cspline.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/natural_cspline.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/periodic_bsplines.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/periodic_cspline.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/periodic_csplines.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 0 additions & 23 deletions man/periodic_splines.Rd

This file was deleted.

0 comments on commit 2deef78

Please sign in to comment.