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 PROBLEMDATA_H
16
#define PROBLEMDATA_H
17

18
#include <vector>
19
#include <map>
20

21
/* Stores the result of calling BUILD_MATRIX on a collection of LinOp
22
 * trees. */
23 6
class ProblemData {
24
public:
25
	/* COO sparse matrix representation. V stores the data, I the row indices
26
	 * and J the column indices. */
27
	std::vector<double> V;
28
	std::vector<int> I;
29
	std::vector<int> J;
30

31
	/* Dense matrix representation of the constant vector */
32
	std::vector<double> const_vec;
33

34
	/* Map of variable_id to column in the problemData matrix */
35
	std::map<int, int> id_to_col;
36

37
	/* Map of constant linOp's to row in the problemData matrix  */
38
	std::map<int, int> const_to_row;
39

40
	/*******************************************
41
	 * The functions below return problemData vectors as contiguous 1d
42
	 * numpy arrays.
43
	 *
44
	 * Note the function prototypes must match CVXCanon.i exactly to
45
	 * properly run and compile.
46
	 *
47
	 * Each function is wrapped using SWIG's numpy.i typemap, so can
48
	 * be called in python using
49
	 *
50
	 * 				problemData.getV(N)
51
	 *
52
	 * where N is the length of the vector V. The double *pointer VALUES
53
	 * is generated by the wrapper, which allocates space for NUM_VALUES
54
	 * elements. Thus, NUM_VALUES must be exactly the length of the array.
55
	 ********************************************/
56

57
	/**
58
	 * Returns the data vector V as a contiguous 1D numpy array.
59
	 */
60
	void getV(double* values, int num_values) {
61
		for (int i = 0; i < num_values; i++) {
62
			values[i] = V[i];
63
		}
64
	}
65

66
	/**
67
	 * Returns the row index vector I as a contiguous 1D numpy array.
68
	 */
69
	void getI(double* values, int num_values) {
70
		for (int i = 0; i < num_values; i++) {
71
			values[i] = I[i];
72
		}
73
	}
74

75
	/**
76
	 * Returns the column index vector J as a contiguous 1D numpy array.
77
	 */
78
	void getJ(double* values, int num_values) {
79
		for (int i = 0; i < num_values; i++) {
80
			values[i] = J[i];
81
		}
82
	}
83

84
	/**
85
	 * Returns the CONST_VEC as a contiguous 1D numpy array.
86
	 */
87
	void getConstVec(double* values, int num_values) {
88
		for (int i = 0; i < num_values; i++) {
89
			values[i] = const_vec[i];
90
		}
91
	}
92
#ifdef _R_INTERFACE_
93 6
  ~ProblemData() {
94
#ifdef _R_DEBUG_
95
    Rcpp::Rcout << "ProblemData was destroyed!" <<std::endl;
96
#endif
97
  }
98
#endif
99
};
100

101
#endif

Read our documentation on viewing source code .

Loading