or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

atoms.mdconstraints.mderrors.mdexpressions.mdindex.mdproblems.mdsolvers.mdtransforms.md
tile.json

tessl/pypi-cvxpy

A domain-specific language for modeling convex optimization problems in Python.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/cvxpy@1.7.x

To install, run

npx @tessl/cli install tessl/pypi-cvxpy@1.7.0

index.mddocs/

CVXPY

A domain-specific language for modeling convex optimization problems in Python. CVXPY enables developers to express optimization problems in a natural mathematical way rather than the restrictive standard form required by solvers, supporting convex optimization, mixed-integer convex optimization, geometric programs, and quasiconvex programs.

Package Information

  • Package Name: cvxpy
  • Package Type: pypi
  • Language: Python
  • Installation: pip install cvxpy
  • Version: 1.7.2

Core Imports

import cvxpy as cp

Common pattern for accessing all CVXPY functionality:

import cvxpy as cp
import numpy as np

Basic Usage

import cvxpy as cp
import numpy as np

# Create optimization variables
x = cp.Variable()
y = cp.Variable()

# Define constraints
constraints = [x + y == 1, x - y >= 1]

# Define objective (minimize x^2 + y^2)
objective = cp.Minimize(x**2 + y**2)

# Create and solve the problem
problem = cp.Problem(objective, constraints)
problem.solve()

# Get results
print(f"Status: {problem.status}")
print(f"Optimal value: {problem.value}")
print(f"x = {x.value}, y = {y.value}")

Architecture

CVXPY follows a disciplined convex programming (DCP) approach with these key components:

  • Expressions: Mathematical expressions built from Variables, Parameters, and Constants
  • Atoms: Pre-defined mathematical functions that preserve convexity
  • Constraints: Restrictions defining the feasible set (equality, inequality, cone constraints)
  • Problems: Combination of an objective and constraints
  • Solvers: Backend optimization engines that solve the transformed problem

This design enables automatic problem verification, transformation to solver-compatible formats, and integration with multiple solvers while maintaining mathematical rigor through DCP rules.

Capabilities

Core Expressions

Fundamental building blocks for optimization problems including variables, parameters, constants, and the base expression class that enables mathematical operations.

class Variable:
    def __init__(self, shape=(), name=None, **kwargs): ...

class Parameter:
    def __init__(self, shape=(), name=None, value=None, **kwargs): ...

class Constant:
    def __init__(self, value): ...

Core Expressions

Problem Formulation

Classes for defining and solving optimization problems, including objective functions and the main Problem class that coordinates solving.

class Problem:
    def __init__(self, objective, constraints=None): ...
    def solve(self, solver=None, **kwargs): ...

class Minimize:
    def __init__(self, expr): ...

class Maximize:  
    def __init__(self, expr): ...

Problem Formulation

Constraints

Constraint types that define the feasible region of optimization problems, including equality, inequality, and specialized cone constraints.

class Zero:  # Equality constraints
    def __init__(self, expr): ...

class NonNeg:  # x >= 0
    def __init__(self, expr): ...

class PSD:  # Positive semidefinite
    def __init__(self, expr): ...

class SOC:  # Second-order cone
    def __init__(self, t, X): ...

Constraints

Mathematical Functions (Atoms)

Pre-defined mathematical functions organized by category that preserve convexity properties and enable complex mathematical expressions.

# Linear algebra
def matmul(lh_exp, rh_exp): ...
def trace(expr): ...
def diag(expr, k=0): ...

# Norms and distances  
def norm(x, p=2, axis=None): ...
def norm1(x): ...
def norm2(x, axis=None): ...

# Elementwise functions
def abs(x): ...
def square(x): ...
def sqrt(x): ...
def exp(x): ...
def log(x): ...

Mathematical Functions

Solvers and Settings

Solver interface, status constants, and configuration options for controlling optimization behavior and accessing different solver backends.

# Status constants
OPTIMAL: str
INFEASIBLE: str  
UNBOUNDED: str
SOLVER_ERROR: str

# Solver names
CLARABEL: str
SCS: str
OSQP: str
CVXOPT: str

# Utility functions
def installed_solvers(): ...
def get_num_threads(): ...
def set_num_threads(num_threads): ...

Solvers and Settings

Transform Functions

Problem transformation utilities for advanced optimization techniques including linearization and partial optimization.

def linearize(expr, var_id): ...
def partial_optimize(expr, vars): ...
def suppfunc(expr): ...

Transform Functions

Error Handling

Exception classes for different types of optimization and solver errors, plus warning control functions.

class DCPError(Exception): ...
class DGPError(Exception): ...  
class SolverError(Exception): ...

def disable_warnings(): ...
def enable_warnings(): ...
def warnings_enabled(): ...

Error Handling

Version Information

Version string for the CVXPY package.

__version__: str  # Version string (e.g., "1.7.2")

Types

class Expression:
    """Base class for all mathematical expressions in CVXPY."""
    def __add__(self, other): ...
    def __sub__(self, other): ...
    def __mul__(self, other): ...
    def __truediv__(self, other): ...
    def __pow__(self, other): ...
    def __neg__(self): ...
    
    @property  
    def value(self): ...
    @property
    def shape(self): ...
    @property
    def size(self): ...

class Constraint:
    """Base class for all constraints."""
    @property
    def value(self): ...
    
class Objective:
    """Base class for optimization objectives."""
    @property
    def value(self): ...