\(\|x\|_2 = \left(\sum_{i=1}^n x_i^2\right)^{1/2}\).

norm2(x, axis = NA_real_, keepdims = FALSE)

Arguments

x

An Expression, vector, or matrix.

axis

(Optional) The dimension across which to apply the function: 1 indicates rows, 2 indicates columns, and NA indicates rows and columns. The default is NA.

keepdims

(Optional) Should dimensions be maintained when applying the atom along an axis? If FALSE, result will be collapsed into an \(n x 1\) column vector. The default is FALSE.

Value

An Expression representing the Euclidean norm of the input.

Examples

a <- Variable()
prob <- Problem(Minimize(norm2(a)), list(a <= -2))
result <- solve(prob)
result$value
#> [1] 2
result$getValue(a)
#> [1] -2

prob <- Problem(Maximize(-norm2(a)), list(a <= -2))
result <- solve(prob)
result$value
#> [1] -2
result$getValue(a)
#> [1] -2

x <- Variable(2)
z <- Variable(2)
prob <- Problem(Minimize(norm2(x - z) + 5), list(x >= c(2,3), z <= c(-1,-4)))
result <- solve(prob)
result$value
#> [1] 12.61577
result$getValue(x)
#>      [,1]
#> [1,]    2
#> [2,]    3
result$getValue(z)
#>      [,1]
#> [1,]   -1
#> [2,]   -4

prob <- Problem(Minimize(norm2(t(x - z)) + 5), list(x >= c(2,3), z <= c(-1,-4)))
result <- solve(prob)
result$value
#> [1] 12.61577
result$getValue(x)
#>      [,1]
#> [1,]    2
#> [2,]    3
result$getValue(z)
#>      [,1]
#> [1,]   -1
#> [2,]   -4