Skip to content

Commit

Permalink
Forbid division by zero by catching too small weight vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
GFabien committed May 22, 2024
1 parent af5e5aa commit 3402cf9
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions R/block_project.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ block_project.block <- function(x) {
#' @export
block_project.dual_block <- function(x) {
if (any(x$alpha != 0)) {
x$alpha <- x$alpha / drop(sqrt(t(x$alpha) %*% x$K %*% x$alpha))
alpha_norm <- t(x$alpha) %*% x$K %*% x$alpha
if (alpha_norm > 0) {
x$alpha <- x$alpha / drop(sqrt(alpha_norm))
} else {
x$alpha <- x$alpha * 0
}
}
x$a <- pm(t(x$x), x$alpha, na.rm = x$na.rm)

Expand All @@ -26,7 +31,12 @@ block_project.dual_block <- function(x) {
#' @export
block_project.primal_regularized_block <- function(x) {
if (any(x$a != 0)) {
x$a <- x$M %*% x$a / drop(sqrt(t(x$a) %*% x$M %*% x$a))
a_norm <- sqrt(t(x$a) %*% x$M %*% x$a)
if (a_norm > 0) {
x$a <- x$M %*% x$a / drop(sqrt(a_norm))
} else {
x$a <- x$a * 0
}
}

x$Y <- pm(x$x, x$a, na.rm = x$na.rm)
Expand All @@ -36,9 +46,12 @@ block_project.primal_regularized_block <- function(x) {
#' @export
block_project.dual_regularized_block <- function(x) {
if (any(x$alpha != 0)) {
x$alpha <- x$M %*% x$alpha / drop(sqrt(
t(x$alpha) %*% x$M %*% x$K %*% x$alpha
))
alpha_norm <- t(x$alpha) %*% x$M %*% x$K %*% x$alpha
if (alpha_norm > 0) {
x$alpha <- x$M %*% x$alpha / drop(sqrt(alpha_norm))
} else {
x$alpha <- x$alpha * 0
}
}
x$a <- pm(t(x$x), x$alpha, na.rm = x$na.rm)

Expand Down

0 comments on commit 3402cf9

Please sign in to comment.