Esempio n. 1
0
 def test_kron(self):
     const_mn = sp.rand(m, n, dens)
     const_np = sp.rand(m, p, dens)
     true_value = sp.csc_matrix(sp.kron(const_mn, const_np))
     test_value = sym.kron(sym.as_sym_matrix(const_mn),
                           sym.as_sym_matrix(const_np)).value
     self.assertAlmostEqualMatrices(test_value, true_value)
Esempio n. 2
0
def get_coefficients(lin_op):
    """Converts a linear op into coefficients.

    Parameters
    ----------
    lin_op : LinOp
        The linear op to convert.

    Returns
    -------
    list
        A list of (id, coefficient) tuples.
    """
    # VARIABLE converts to a giant identity matrix.
    if lin_op.type == lo.VARIABLE:
        coeffs = var_coeffs(lin_op)
    #elif lin_op.type == lo.PARAM:
     #   coeffs = param_coeffs(lin_op)
    # Constants convert directly to their value.
    elif lin_op.type in CONSTANT_TYPES:
        mat = const_mat(lin_op)
        coeffs = [(lo.CONSTANT_ID, flatten(mat))]
        #coeffs = [(lo.CONSTANT_ID, mat.as_vector())]
    elif lin_op.type in TYPE_TO_FUNC:
        # A coefficient matrix for each argument.
        coeff_mats = TYPE_TO_FUNC[lin_op.type](lin_op)
        coeffs = []
        for coeff_mat, arg in zip(coeff_mats, lin_op.args):
            rh_coeffs = get_coefficients(arg)
            coeffs += mul_by_const(coeff_mat, rh_coeffs)
    else:
        raise Exception("Unknown linear operator '%s'" % lin_op.type)
    coeffs = [(c[0], sym.as_sym_matrix(c[1])) for c in coeffs] # All coeffs as SymMatrix
    return coeffs
Esempio n. 3
0
def rmul_mat(lin_op):
    """Returns the coefficient matrix for RMUL linear op.

    Parameters
    ----------
    lin_op : LinOp
        The rmul linear op.

    Returns
    -------
    list of SciPy CSC matrix or scalar.
        The matrix for the multiplication on the right operator.
    """
    constant = const_mat(lin_op.data)
    if isinstance(constant, sym.SymMatrix):
        sym_eye = sym.as_sym_matrix(sp.csc_matrix(sp.eye(lin_op.size[0])))
        constant = sym.kron(sym.transpose(constant), sym_eye)
                           
    else:
        # Scalars don't need to be replicated.
        if not intf.is_scalar(constant):
            # Matrix is the kronecker product of constant.T and identity.
            # Each column in the product is a linear combination of the
            # columns of the left hand multiple.
            constant = sp.kron(constant.T, sp.eye(lin_op.size[0])).tocsc()
    return [constant]
Esempio n. 4
0
 def test_reciprocals(self):
     const_mn = sp.csc_matrix(sp.rand(n, n, dens))
     new_data = 1.0 / const_mn.data
     true_value = sp.csc_matrix(
         (new_data, const_mn.indices, const_mn.indptr),
         shape=const_mn.shape)
     test_value = sym.reciprocals(sym.as_sym_matrix(const_mn)).value
     self.assertAlmostEqualMatrices(test_value, true_value)
Esempio n. 5
0
def const_mat(lin_op):
    """Returns the matrix for a constant type.

    Parameters
    ----------
    lin_op : LinOp
        The linear op.

    Returns
    -------
    A numerical constant.
    """
    if lin_op.type == lo.PARAM:
        name = lin_op.data.name()
        if name in CBP_TO_SPARSITY.keys():
            sprs = CBP_TO_SPARSITY[name]
            sprs = sp.csc_matrix(sprs)
            coeff = sym.as_sym_matrix(lin_op.data, sparsity=sprs)
        else:
            coeff = sym.as_sym_matrix(lin_op.data)
    elif lin_op.type in [lo.SCALAR_CONST, lo.DENSE_CONST, lo.SPARSE_CONST]:
        coeff = lin_op.data
    return coeff
Esempio n. 6
0
 def test_kron(self):
     const_mn = sp.rand(m, n, dens)
     true_value = sp.csc_matrix(const_mn.T)
     test_value = sym.transpose(sym.as_sym_matrix(const_mn)).value
     self.assertAlmostEqualMatrices(test_value, true_value)
Esempio n. 7
0
 def test_diag(self):
     const_n1 = sp.rand(n, 1, dens)
     true_value = sp.diags(np.squeeze(const_n1.toarray(), 1))
     test_value = sym.diag(sym.as_sym_matrix(const_n1)).value
     self.assertAlmostEqualMatrices(test_value, true_value)
Esempio n. 8
0
along with CVXPY-CODEGEN.  If not, see <http://www.gnu.org/licenses/>.
"""

import unittest
import cvxpy_codegen.tests.utils as tu
import cvxpy_codegen as cg
import numpy as np
import cvxpy_codegen.linop_sym.sym_matrix as sym
import scipy.sparse as sp

m = 5
n = 6
p = 8
dens = .3
A_sparse = sp.rand(m, n, dens)
A_sym = sym.as_sym_matrix(A_sparse)
A_dense = A_sparse.toarray()
A_param = sym.as_sym_matrix(cg.Parameter(m, n, value=A_sparse))

B_sparse = sp.rand(n, p, dens)
B_sym = sym.as_sym_matrix(B_sparse)
B_dense = B_sparse.toarray()
B_param = sym.as_sym_matrix(cg.Parameter(n, p, value=B_sparse))

C_sparse = sp.rand(m, n, dens)
C_sym = sym.as_sym_matrix(C_sparse)
C_dense = C_sparse.toarray()
C_param = sym.as_sym_matrix(cg.Parameter(m, n, value=C_sparse))

alpha = .5
alpha_sym = sym.SymConst(.5)