Пример #1
0
def matvec_plus_vec_times_scalar(mat1, vec1, vec2, scalar):
    # TODO: fix docstring
    """

    :param mat1: a numpy ndarray of shape (m, n).
    :param vec1: a coniclifts Variable of shape (n,) or (n, 1)
    :param vec2: a numpy ndarray of shape (m,) or (m, 1).
    :param scalar: a coniclifts Variable of size 1.
    :return:

    Return A_rows, A_cols, A_vals as if they were generated by a compile() call on
    con = (mat @ vecvar + vec * singlevar >= 0).
    """
    if isinstance(scalar, Expression):
        if scalar.size == 1:
            scalar = scalar.item()
        else:
            raise ValueError('Argument `scalar` must have size 1.')
    # We can now assume that "scalar" is a ScalarExpression.
    if isinstance(vec1, Variable):
        b = scalar.offset * vec2
        a2c = scalar.atoms_to_coeffs.items()
        s_covec = np.array([co for (sv, co) in a2c])
        s_indices = [sv.id for (sv, co) in a2c]
        mat2 = np.outer(vec2, s_covec)  # rank 1
        mat = np.hstack((mat1, mat2))
        indices = vec1.scalar_variable_ids + s_indices
        A_vals, A_rows, A_cols = _matvec_by_var_indices(mat, indices)
    else:
        mat = np.hstack([mat1, np.reshape(vec2, (-1, 1))])
        if isinstance(scalar, ScalarExpression):
            scalar = Expression([scalar])
        expr = concatenate((vec1, scalar))
        A_vals, A_rows, A_cols, b = matvec(mat, expr)
    return A_vals, A_rows, A_cols, b
Пример #2
0
    def test_concatenate(self):
        a = np.array([[1, 2], [3, 4]])
        a_cl = Variable(shape=a.shape)
        a_cl.value = a
        b = np.array([[5, 6]])
        b_cl = Variable(shape=b.shape)
        b_cl.value = b
        expected = np.concatenate((a, b))
        actual = aff.concatenate((a_cl, b_cl))
        assert np.allclose(expected, actual.value)
        expected1 = np.concatenate((b, a))
        actual1 = aff.concatenate((b_cl, a_cl))
        assert np.allclose(expected1, actual1.value)

        a_cl.value = 1 + a
        assert not np.allclose(expected, actual.value)
        assert not np.allclose(expected1, actual1.value)
Пример #3
0
def matvec_plus_matvec(mat1, vec1, mat2, vec2):
    # TODO: fix docstring
    """
    :param mat1: a numpy ndarray of shape (m, n1).
    :param vec1: a coniclifts Variable of shape (n1,) or (n1, 1).
    :param mat2: a numpy ndarray of shape (m, n2)
    :param vec2: a coniclifts Variable of shape (n2,) or (n2, 1).
    :return: A_vals, A_rows, A_cols so that the coniclifts Expression
        expr = mat1 @ vecvar1 + mat2 @ vecvar2 would have "expr >= 0"
        compile to A_vals, A_rows, A_cols, np.zeros((m,)), [].
    """
    mat = np.hstack([mat1, mat2])
    if isinstance(vec1, Variable) and isinstance(vec2, Variable):
        num_rows = mat.shape[0]
        b = np.zeros(num_rows)
        ids = vec1.scalar_variable_ids + vec2.scalar_variable_ids
        A_vals, A_rows, A_cols = _matvec_by_var_indices(mat, ids)
    else:
        vec = concatenate((vec1, vec2))
        A_vals, A_rows, A_cols, b = matvec(mat, vec)
    return A_vals, A_rows, A_cols, b