This function vectorizes an expression, then unvectorizes it into a new shape. Entries are stored in column-major order.

reshape_expr(expr, new_dim)

## Arguments

expr

An Expression, vector, or matrix.

new_dim

The new dimensions.

## Value

An Expression representing the reshaped input.

## Examples

x <- Variable(4)
mat <- cbind(c(1,-1), c(2,-2))
vec <- matrix(1:4)
expr <- reshape_expr(x,c(2,2))
obj <- Minimize(sum(mat %*% expr))
prob <- Problem(obj, list(x == vec))
result <- solve(prob)
result$value #> [1] 0 A <- Variable(2,2) c <- 1:4 expr <- reshape_expr(A,c(4,1)) obj <- Minimize(t(expr) %*% c) constraints <- list(A == cbind(c(-1,-2), c(3,4))) prob <- Problem(obj, constraints) result <- solve(prob) result$value
#> [1] 20
result$getValue(expr) #> [,1] #> [1,] -1 #> [2,] -2 #> [3,] 3 #> [4,] 4 result$getValue(reshape_expr(expr,c(2,2)))
#>      [,1] [,2]
#> [1,]   -1    3
#> [2,]   -2    4

C <- Variable(3,2)
expr <- reshape_expr(C,c(2,3))
mat <- rbind(c(1,-1), c(2,-2))
C_mat <- rbind(c(1,4), c(2,5), c(3,6))
obj <- Minimize(sum(mat %*% expr))
prob <- Problem(obj, list(C == C_mat))
result <- solve(prob)
result$value #> [1] -9 result$getValue(expr)
#>      [,1] [,2] [,3]
#> [1,]    1    3    5
#> [2,]    2    4    6

a <- Variable()
c <- cbind(c(1,-1), c(2,-2))
expr <- reshape_expr(c * a,c(1,4))
obj <- Minimize(expr %*% (1:4))
prob <- Problem(obj, list(a == 2))
result <- solve(prob)
result$value #> [1] -6 result$getValue(expr)
#>      [,1] [,2] [,3] [,4]
#> [1,]    2   -2    4   -4

expr <- reshape_expr(c * a,c(4,1))
obj <- Minimize(t(expr) %*% (1:4))
prob <- Problem(obj, list(a == 2))
result <- solve(prob)
result$value #> [1] -6 result$getValue(expr)
#>      [,1]
#> [1,]    2
#> [2,]   -2
#> [3,]    4
#> [4,]   -4