The sum of the squared entries in a vector or matrix.

sum_squares(expr)

Arguments

expr

An Expression, vector, or matrix.

Value

An Expression representing the sum of squares of the input.

Examples

set.seed(212)
m <- 30
n <- 20
A <- matrix(stats::rnorm(m*n), nrow = m, ncol = n)
b <- matrix(stats::rnorm(m), nrow = m, ncol = 1)

x <- Variable(n)
obj <- Minimize(sum_squares(A %*% x - b))
constr <- list(0 <= x, x <= 1)
prob <- Problem(obj, constr)
result <- solve(prob)

result$value
#> [1] 25.64897
result$getValue(x)
#>                [,1]
#>  [1,]  1.019360e-01
#>  [2,]  1.414355e-01
#>  [3,] -2.012287e-19
#>  [4,] -4.393679e-19
#>  [5,]  1.506072e-20
#>  [6,]  5.633428e-20
#>  [7,] -4.252601e-19
#>  [8,]  2.212576e-01
#>  [9,]  4.730655e-02
#> [10,]  4.641619e-02
#> [11,]  3.571723e-02
#> [12,]  2.292115e-01
#> [13,] -4.452617e-19
#> [14,]  8.158129e-21
#> [15,]  1.813679e-19
#> [16,]  2.612040e-01
#> [17,] -1.293152e-19
#> [18,] -3.681589e-19
#> [19,]  6.762751e-21
#> [20,] -2.353406e-19
result$getDualValue(constr[[1]])
#>             [,1]
#>  [1,]  0.0000000
#>  [2,]  0.0000000
#>  [3,] 12.8031354
#>  [4,] 13.0734124
#>  [5,]  9.6996644
#>  [6,]  3.7263413
#>  [7,] 11.2616650
#>  [8,]  0.0000000
#>  [9,]  0.0000000
#> [10,]  0.0000000
#> [11,]  0.0000000
#> [12,]  0.0000000
#> [13,]  6.0682997
#> [14,]  3.0700359
#> [15,]  0.6838339
#> [16,]  0.0000000
#> [17,]  5.7228351
#> [18,]  0.2429908
#> [19,]  6.9429350
#> [20,]  4.6553261