RunMat
GitHub

linprog — Solve linear programming minimization problems with inequality constraints, equality constraints, and bounds.

linprog minimizes f' * x subject to A*x <= b, Aeq*x == beq, and lb <= x <= ub. Supported forms are x = linprog(f,A,b), x = linprog(f,A,b,Aeq,beq), x = linprog(f,A,b,Aeq,beq,lb,ub), and the two-, three-, and four-output variants.

Syntax

x = linprog(f, A, b)
x = linprog(f, A, b, Aeq, beq)
x = linprog(f, A, b, Aeq, beq, lb, ub)
[x, fval] = linprog(f, A, b)
[x, fval] = linprog(f, A, b, Aeq, beq)
[x, fval] = linprog(f, A, b, Aeq, beq, lb, ub)
All supported linprog forms
x = linprog(f, A, b)
x = linprog(f, A, b, Aeq, beq)
x = linprog(f, A, b, Aeq, beq, lb, ub)
[x, fval] = linprog(f, A, b)
[x, fval] = linprog(f, A, b, Aeq, beq)
[x, fval] = linprog(f, A, b, Aeq, beq, lb, ub)
[x, fval, exitflag] = linprog(f, A, b)
[x, fval, exitflag] = linprog(f, A, b, Aeq, beq)
[x, fval, exitflag] = linprog(f, A, b, Aeq, beq, lb, ub)
[x, fval, exitflag, output] = linprog(f, A, b)
[x, fval, exitflag, output] = linprog(f, A, b, Aeq, beq)
[x, fval, exitflag, output] = linprog(f, A, b, Aeq, beq, lb, ub)

Inputs

NameTypeRequiredDefaultDescription
fNumericArrayYesLinear objective vector.
ANumericArrayYesInequality constraint matrix.
bNumericArrayYesInequality constraint right-hand side.
AeqNumericArrayNo[]Equality constraint matrix.
beqNumericArrayNo[]Equality constraint right-hand side.
lbNumericArrayNo[]Lower bounds.
ubNumericArrayNo[]Upper bounds.

Returns

NameTypeDescription
xNumericArrayOptimal decision vector.
fvalNumericScalarObjective value f'*x at the solution.
exitflagNumericScalarSolver status code.
outputAnyDiagnostic metadata struct.

Returned values from linprog depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:linprog:InvalidArgumentThe argument count or optional argument grammar is invalid.linprog: invalid argument
RunMat:linprog:InvalidInputObjective, constraint, or bound dimensions/types are invalid.linprog: invalid input

How linprog works

  • f, b, beq, lb, and ub must be numeric vectors with compatible lengths.
  • A and Aeq must be numeric matrices with one column per element of f.
  • [] may be passed for omitted optional Aeq, beq, lb, or ub arguments.
  • exitflag is 1 when an optimum is found, -2 when no feasible point is found, and -3 when the objective is unbounded below.
  • The fourth output is a struct with iterations, algorithm, constrviolation, and message fields.
  • Advanced MATLAB options, algorithms, problem structs, and lambda outputs are intentionally unsupported in this initial implementation.

Examples

Solve a bounded linear program

f = [-1; -2];
A = [1 1];
b = 4;
[x, fval, exitflag] = linprog(f, A, b, [], [], [0; 0], [])

Expected output:

x =
     0
     4
fval =
    -8
exitflag =
     1

Use equality constraints and lower bounds

f = [1; 2];
A = [];
b = [];
Aeq = [1 1];
beq = 3;
[x, fval] = linprog(f, A, b, Aeq, beq, [1; 0], [])

Expected output:

x =
     3
     0
fval =
     3

Use a finite lower bound on one variable

f = [1; 0];
[x, fval, exitflag] = linprog(f, [], [], [], [], [2; -Inf], [])

Expected output:

x =
     2
     0
fval =
     2
exitflag =
     1

Optimize along an equality face

f = [-1; 0; 0];
A = [1 0 0];
b = 1;
Aeq = [0 0 1];
beq = 0;
[x, fval, exitflag] = linprog(f, A, b, Aeq, beq, [], [])

Expected output:

x =
     1
     0
     0
fval =
    -1
exitflag =
     1

Inspect infeasible status

[x, fval, exitflag, output] = linprog(1, [], [], [], [], 2, 1);
exitflag

Expected output:

exitflag =
    -2

Using linprog with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how linprog changes the result.

Run a small linprog example, explain the result, then change one input and compare the output.

FAQ

Does linprog support optimoptions or MATLAB problem structs?

No. This implementation supports the common positional numeric call forms only.

Does linprog run on the GPU?

No. It gathers GPU-resident inputs and runs the active-set solve on the host.

Elementwise

abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times

Trigonometry

acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh

Reduction

all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var

Signal

blackman · butter · conv · conv2 · deconv · filter · hamming · hann · sawtooth · sinc · square

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · pinv · rank · rcond · rref

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how linprog is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.