cvxgrp / CVXR

An R modeling language for convex optimization problems.

Home Page:https://cvxr.rbind.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Base Apply and Elementwise Function

btaute opened this issue · comments

Is there a way to use base::apply and a CVXR elementwise function to apply an elementwise function to every column of a matrix? Assuming the length of the vector matches the number of rows of the matrix, I'd like to be able to make my objective function the following:

new_matrix <- apply(constant_matrix, 2, function(x) x * variable_vector)
objective <- sum_entries( max_entries( as.matrix(new_matrix), axis = 2 ) )

I get the following error when solving the problem:
Error in min(constant) : invalid 'type' (list) of argument

(In case my code is unclear, my goal is to multiply a variable vector by every column of a constant matrix, then get the max of each of those scaled columns, and then sum all of those maxes.)

Thanks!

The short answer is yes, you can. If you post the question to SO as noted in the FAQ we will be happy to answer this for the larger community.