cvxgrp / CVXR
1
//   Copyright 2017 Steven Diamond
2
//
3
//   Licensed under the Apache License, Version 2.0 (the "License");
4
//   you may not use this file except in compliance with the License.
5
//   You may obtain a copy of the License at
6
//
7
//       http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//   Unless required by applicable law or agreed to in writing, software
10
//   distributed under the License is distributed on an "AS IS" BASIS,
11
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//   See the License for the specific language governing permissions and
13
//   limitations under the License.
14

15
#ifndef LINOP_H
16
#define LINOP_H
17

18
#include <vector>
19
#include <cassert>
20
#include <iostream>
21
#include "Utils.h"
22

23
/* ID for all coefficient matrices associated with linOps of CONSTANT_TYPE */
24
static const int CONSTANT_ID = -1;
25

26
/* TYPE of each LinOP */
27
enum operatortype {
28
	VARIABLE,
29
	PROMOTE,
30
	MUL_EXPR,
31
	RMUL_EXPR,
32
	MUL_ELEM,
33
	DIV,
34
	SUM,
35
	NEG,
36
	INDEX,
37
	TRANSPOSE,
38
	SUM_ENTRIES,
39
	TRACE,
40
	RESHAPE,
41
	DIAG_VEC,
42
	DIAG_MAT,
43
	UPPER_TRI,
44
	CONV,
45
	HSTACK,
46
	VSTACK,
47
	SCALAR_CONST,
48
	DENSE_CONST,
49
	SPARSE_CONST,
50
	NO_OP,
51
	KRON
52
};
53

54
/* linOp TYPE */
55
typedef operatortype OperatorType;
56

57
/* LinOp Class mirrors the CVXPY linOp class. Data fields are determined
58
 	 by the TYPE of LinOp. No error checking is performed on the data fields,
59
 	 and the semantics of SIZE, ARGS, and DATA depends on the linop TYPE. */
60 6
class LinOp {
61
public:
62
	OperatorType type;
63
	std::vector<int> size;
64
	/* Children LinOps in the tree */
65
	std::vector<LinOp*> args;
66

67
	/* Sparse Data Fields */
68
	bool sparse; // True only if linOp has sparse_data
69
	Matrix sparse_data;
70

71
	/* Dense Data Field */
72
	Eigen::MatrixXd dense_data;
73

74
	/* Slice Data: stores slice data as (row_slice, col_slice)
75
	 * where slice = (start, end, step_size) */
76
	std::vector<std::vector<int> > slice;
77

78
#ifdef _R_INTERFACE_  
79
        /* almost uuid */
80
        std::string id;
81
	/* Constructor */
82 6
	LinOp(){
83 6
          id = genRandomId();
84
#ifdef _R_DEBUG
85
          Rcpp::Rcout << "LinOp id " << id << " Created!" << std::endl;
86
#endif
87 6
	  sparse = false; // sparse by default
88
	}
89

90 6
        ~LinOp() {
91
#ifdef _R_DEBUG
92
           Rcpp::Rcout << "LinOp id " << id << " ; type " << type << " Destroyed!!" << std::endl;
93
#endif
94
         }
95

96
#else
97
	/* Constructor */
98
	LinOp(){
99
		sparse = false; // sparse by default
100
	}
101
#endif
102
	/* Checks if LinOp is constant type */
103 6
	bool has_constant_type() {
104 6
		return  type == SCALAR_CONST || type == DENSE_CONST
105 6
		        || type == SPARSE_CONST;
106
	}
107

108
	/* Initializes DENSE_DATA. MATRIX is a pointer to the data of a 2D
109
	 * numpy array, ROWS and COLS are the size of the ARRAY.
110
	 *
111
	 * MATRIX must be a contiguous array of doubles aligned in fortran
112
	 * order.
113
	 *
114
	 * NOTE: The function prototype must match the type-map in CVXCanon.i
115
	 * exactly to compile and run properly.
116
	 */
117
	void set_dense_data(double* matrix, int rows, int cols) {
118
		dense_data = Eigen::Map<Eigen::MatrixXd> (matrix, rows, cols);
119
	}
120

121
	/* Initializes SPARSE_DATA from a sparse matrix in COO format.
122
	 * DATA, ROW_IDXS, COL_IDXS are assumed to be contiguous 1D numpy arrays
123
	 * where (DATA[i], ROW_IDXS[i], COLS_IDXS[i]) is a (V, I, J) triplet in
124
	 * the matrix. ROWS and COLS should refer to the size of the matrix.
125
	 *
126
	 * NOTE: The function prototype must match the type-map in CVXCanon.i
127
	 * exactly to compile and run properly.
128
	 */
129
	void set_sparse_data(double *data, int data_len, double *row_idxs,
130
	                     int rows_len, double *col_idxs, int cols_len,
131
	                     int rows, int cols) {
132

133
		assert(rows_len == data_len && cols_len == data_len);
134
		sparse = true;
135
		Matrix sparse_coeffs(rows, cols);
136
		std::vector<Triplet> tripletList;
137
		tripletList.reserve(data_len);
138
		for (int idx = 0; idx < data_len; idx++) {
139
			tripletList.push_back(Triplet(int(row_idxs[idx]), int(col_idxs[idx]),
140
			                              data[idx]));
141
		}
142
		sparse_coeffs.setFromTriplets(tripletList.begin(), tripletList.end());
143
		sparse_coeffs.makeCompressed();
144
		sparse_data = sparse_coeffs;
145
	}
146
};
147
#endif

Read our documentation on viewing source code .

Loading