add_column, add_columnex, str_add_column

Add a column to the lp.

unsigned char add_column(lprec *lp, REAL *column);

unsigned char add_columnex(lprec *lp, int count, REAL *column, int *rowno);

unsigned char str_add_column(lprec *lp, char *col_string);

Return Value

add_column, add_columnex, and str_add_column return TRUE (1) if the operation was successful. A return value of FALSE (0) indicates an error.



Pointer to previously created lp model. See return value of make_lp, copy_lp, read_lp, read_LP, read_mps, read_freemps, read_MPS, read_freeMPS, read_XLI


Number of elements in column and rowno.


An array with 1+get_Nrows (count for add_columnex, if rowno is different from NULL) elements that contains the values of the column.


A zero-based array with count elements that contains the row numbers of the column. However this variable can also be NULL. In that case element i in the variable column is row i.


A string with row elements that contains the values of the column. Each element must be separated by space(s).


The add_column, add_columnex, str_add_column functions add a column to the model (at the end) and sets all values of the column at once.

Note that for add_column (and add_columnex when rowno is NULL) element 0 of the array is the value of the objective function for that column. Column 1 is element 1, column 2 is element 2, ...

str_add_column should only be used in small or demo code since it is not performant and uses more memory.

add_columnex has the possibility to specify only the non-zero elements. In that case rowno specifies the row numbers of the non-zero elements. Both column and rowno are then zero-based arrays. This will speed up building the model considerably if there are a lot of zero values. In most cases the matrix is sparse and has many zero value. Note that add_columnex behaves the same as add_column when rowno is NULL.
For add_columnex, column and rowno can both be NULL. In that case an empty column is added.

Thus it is almost always better to use add_columnex instead of add_column. add_columnex is always at least as performant as add_column.

Note that if you have to add many columns, performance can be improved by a call to resize_lp.


#include <stdio.h>
#include <stdlib.h>
#include "lp_lib.h"

int main(void)
  lprec *lp;
  REAL column[1+3];     /* must be 1 more than number of rows ! */
  REAL sparsecolumn[3]; /* must be the number of non-zero values */
  int rowno[3];

  /* Create a new LP model */
  lp = make_lp(3, 0);
  if(lp == NULL) {
    fprintf(stderr, "Unable to create new LP model\n");

  column[0] = 1.0; /* the objective value */
  column[1] = 2.0;
  column[2] = 0.0;
  column[3] = 3.0;
  add_column(lp, column);
  rowno[0] = 0; sparsecolumn[0] = 1.0; /* the objective value */
  rowno[1] = 1; sparsecolumn[1] = 2.0;
  rowno[2] = 3; sparsecolumn[2] = 3.0;
  add_columnex(lp, 3, sparsecolumn, rowno);


lp_solve API reference

See Also make_lp, copy_lp, copy_lp, read_lp, read_LP, read_mps, read_freemps, read_MPS, read_freeMPS, read_XLI, set_obj_fn, set_obj_fnex, str_set_obj_fn, set_obj, set_column, set_columnex, del_column, set_add_rowmode, is_add_rowmode, resize_lp, add_constraint, add_constraintex, str_add_constraint, set_row, set_rowex, get_column, get_columnex, get_row, get_rowex, get_mat, column_in_lp