-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDefaultSolver.hpp
More file actions
701 lines (576 loc) · 29.4 KB
/
DefaultSolver.hpp
File metadata and controls
701 lines (576 loc) · 29.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#pragma once
#include "CscMatrix.hpp"
#include "DefaultInfo.hpp"
#include "DefaultSettings.hpp"
#include "DefaultSolution.hpp"
#include "SupportedConeT.hpp"
#include <Eigen/Eigen>
#include <memory>
#include <vector>
namespace clarabel
{
using RustObjectHandle = void *;
using RustDefaultSolverHandle_f64 = RustObjectHandle;
using RustDefaultSolverHandle_f32 = RustObjectHandle;
template<typename T = double>
class DefaultSolver
{
static_assert(std::is_same<T, float>::value || std::is_same<T, double>::value, "T must be float or double");
private:
struct ConvertedCscMatrix;
RustObjectHandle handle = nullptr;
// Helper function for converting a Eigen sparse matrix into a temporary object of type ConvertedCscMatrix<T>
// The temporary object is used to provide the problem data for constructing the solver.
static ConvertedCscMatrix eigen_sparse_to_clarabel(const Eigen::SparseMatrix<T, Eigen::ColMajor> &matrix)
{
// Make a copy of data in SparseMatrix to convert StorageIndex to uintptr_t
std::vector<uintptr_t> row_indices(matrix.nonZeros());
std::vector<uintptr_t> col_pointers(matrix.outerSize() + 1);
// Convert to uintptr_t
for (int k = 0; k < matrix.nonZeros(); ++k)
{
row_indices[k] = matrix.innerIndexPtr()[k];
}
for (int k = 0; k < matrix.outerSize(); ++k)
{
col_pointers[k] = matrix.outerIndexPtr()[k];
}
col_pointers[matrix.outerSize()] = matrix.nonZeros();
// No conversion needed for nz values
const T *nzval_ptr = matrix.nonZeros() == 0 ? nullptr : matrix.valuePtr();
ConvertedCscMatrix csc_matrix(static_cast<uintptr_t>(matrix.rows()), static_cast<uintptr_t>(matrix.cols()),
std::move(col_pointers), std::move(row_indices), nzval_ptr);
return std::move(csc_matrix);
}
static void check_dimensions(const Eigen::SparseMatrix<T, Eigen::ColMajor> &P,
const Eigen::Ref<const Eigen::VectorX<T>> &q,
const Eigen::SparseMatrix<T, Eigen::ColMajor> &A,
const Eigen::Ref<const Eigen::VectorX<T>> &b,
const std::vector<SupportedConeT<T>> &cones)
{
if (P.rows() != P.cols())
{
throw std::invalid_argument("P must be a square matrix");
}
if (P.rows() != q.size())
{
throw std::invalid_argument("P and q must have the same number of rows");
}
if (A.cols() != P.cols())
{
throw std::invalid_argument("A and P must have the same number of columns");
}
if (A.rows() != b.size())
{
throw std::invalid_argument("A and b must have the same number of rows");
}
unsigned int p = 0;
for (const auto &cone : cones)
{
p += cone.nvars();
}
if (p != b.size())
{
throw std::invalid_argument("Constraint dimensions inconsistent with size of cones");
}
}
public:
// Lifetime of problem data: matrices P, A, vectors q, b, cones and the settings are copied when the DefaultSolver
// object is created in Rust. Eigen::SparseMatrix objects need to be converted to the format supported by Clarabel.
// Since the problem data is copied, the converted data exists as local variables in the constructor and is not
// stored as part of this class.
DefaultSolver(const Eigen::SparseMatrix<T, Eigen::ColMajor> &P,
const Eigen::Ref<Eigen::VectorX<T>> &q,
const Eigen::SparseMatrix<T, Eigen::ColMajor> &A,
const Eigen::Ref<Eigen::VectorX<T>> &b,
const std::vector<SupportedConeT<T>> &cones,
const DefaultSettings<T> &settings);
DefaultSolver(void* handle);
~DefaultSolver();
DefaultSolver(const DefaultSolver &) = delete;
DefaultSolver(DefaultSolver &&other);
DefaultSolver &operator=(const DefaultSolver &) = delete;
DefaultSolver &operator=(DefaultSolver &&other);
void solve();
// The solution can only be obtained when the solver is in the Solved state, and the DefaultSolution object is only
// valid when the solver is alive.
DefaultSolution<T> solution() const;
DefaultInfo<T> info() const;
// termination callbacks
// -------------------------------
void set_termination_callback(
int (*callback)(clarabel::DefaultInfo<T>&,void*), void* userdata);
void unset_termination_callback();
// problem data updating functions
// -------------------------------
// update P
void update_P(const Eigen::SparseMatrix<T, Eigen::ColMajor> &P);
void update_P(const Eigen::Ref<Eigen::VectorX<T>> &Pnzval);
void update_P(const T* Pnzval, uintptr_t nnzP);
void update_P(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<T>> &values);
void update_P(const uintptr_t* index, const T* values, uintptr_t nvals);
// update A
void update_A(const Eigen::SparseMatrix<T, Eigen::ColMajor> &A);
void update_A(const Eigen::Ref<Eigen::VectorX<T>> &Anzval);
void update_A(const T* Pnzval, uintptr_t nnzA);
void update_A(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<T>> &values);
void update_A(const uintptr_t* index, const T* values, uintptr_t nvals);
// update q
void update_q(const Eigen::Ref<Eigen::VectorX<T>> &Anzval);
void update_q(const T* values, uintptr_t nvals);
void update_q(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<T>> &values);
void update_q(const uintptr_t* index, const T* values, uintptr_t nvals);
// update b
void update_b(const Eigen::Ref<Eigen::VectorX<T>> &Anzval);
void update_b(const T* values, uintptr_t nvals);
void update_b(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<T>> &values);
void update_b(const uintptr_t* index, const T* values, uintptr_t nvals);
// Read / write to JSON file
#ifdef FEATURE_SERDE
void save_to_file(const std::string &filename);
static DefaultSolver<T> load_from_file(const std::string &filename);
#endif
// print stream configurations
void print_to_stdout();
void print_to_file(const std::string &filename);
void print_to_buffer();
std::string get_print_buffer();
};
template<typename T>
DefaultSolver<T>::DefaultSolver(DefaultSolver &&other) : handle(other.handle)
{
other.handle = nullptr;
}
template<typename T>
DefaultSolver<T> &DefaultSolver<T>::operator=(DefaultSolver &&other)
{
if (this != &other){
handle = other.handle;
other.handle = nullptr;
}
return *this;
}
template<typename T>
struct DefaultSolver<T>::ConvertedCscMatrix
{
uintptr_t m;
uintptr_t n;
const std::vector<uintptr_t> colptr;
const std::vector<uintptr_t> rowval;
const T *nzval;
ConvertedCscMatrix(
uintptr_t m, uintptr_t n, std::vector<uintptr_t> &&colptr, std::vector<uintptr_t> &&rowval, const T *nzval)
: m(m), n(n), colptr(std::move(colptr)), rowval(std::move(rowval)), nzval(nzval)
{
}
};
extern "C" {
RustDefaultSolverHandle_f64 clarabel_DefaultSolver_f64_new(const CscMatrix<double> *P,
const double *q,
const CscMatrix<double> *A,
const double *b,
uintptr_t n_cones,
const SupportedConeT<double> *cones,
const DefaultSettings<double> *settings);
RustDefaultSolverHandle_f32 clarabel_DefaultSolver_f32_new(const CscMatrix<float> *P,
const float *q,
const CscMatrix<float> *A,
const float *b,
uintptr_t n_cones,
const SupportedConeT<float> *cones,
const DefaultSettings<float> *settings);
void clarabel_DefaultSolver_f64_solve(RustDefaultSolverHandle_f64 solver);
void clarabel_DefaultSolver_f32_solve(RustDefaultSolverHandle_f32 solver);
void clarabel_DefaultSolver_f64_free(RustDefaultSolverHandle_f64 solver);
void clarabel_DefaultSolver_f32_free(RustDefaultSolverHandle_f32 solver);
DefaultSolution<double>::ClarabelDefaultSolution
clarabel_DefaultSolver_f64_solution(RustDefaultSolverHandle_f64 solver);
DefaultSolution<float>::ClarabelDefaultSolution clarabel_DefaultSolver_f32_solution(RustDefaultSolverHandle_f32 solver);
DefaultInfo<double> clarabel_DefaultSolver_f64_info(RustDefaultSolverHandle_f64 solver);
DefaultInfo<float> clarabel_DefaultSolver_f32_info(RustDefaultSolverHandle_f32 solver);
void clarabel_DefaultSolver_f64_set_termination_callback(RustDefaultSolverHandle_f64 solver, int (*callback)(DefaultInfo<double>& ,void*),void* userdata);
void clarabel_DefaultSolver_f32_set_termination_callback(RustDefaultSolverHandle_f32 solver, int (*callback)(DefaultInfo<float>&, void*),void* userdata);
void clarabel_DefaultSolver_f64_unset_termination_callback(RustDefaultSolverHandle_f64 solver);
void clarabel_DefaultSolver_f32_unset_termination_callback(RustDefaultSolverHandle_f32 solver);
void clarabel_DefaultSolver_f64_update_P_csc(RustDefaultSolverHandle_f64 solver, const CscMatrix<double> *P);
void clarabel_DefaultSolver_f32_update_P_csc(RustDefaultSolverHandle_f32 solver, const CscMatrix<float> *P);
void clarabel_DefaultSolver_f64_update_P(RustDefaultSolverHandle_f64 solver, const double *Pnzval, uintptr_t nnzP);
void clarabel_DefaultSolver_f32_update_P(RustDefaultSolverHandle_f32 solver, const float *Pnzval, uintptr_t nnzP);
void clarabel_DefaultSolver_f64_update_P_partial(RustDefaultSolverHandle_f64 solver, const uintptr_t* index, const double *values, uintptr_t nvals);
void clarabel_DefaultSolver_f32_update_P_partial(RustDefaultSolverHandle_f32 solver, const uintptr_t* index, const float *values, uintptr_t nvals);
void clarabel_DefaultSolver_f64_update_A_csc(RustDefaultSolverHandle_f64 solver, const CscMatrix<double> *A);
void clarabel_DefaultSolver_f32_update_A_csc(RustDefaultSolverHandle_f32 solver, const CscMatrix<float> *A);
void clarabel_DefaultSolver_f64_update_A(RustDefaultSolverHandle_f64 solver, const double *Anzval, uintptr_t nnzA);
void clarabel_DefaultSolver_f32_update_A(RustDefaultSolverHandle_f32 solver, const float *Anzval, uintptr_t nnzA);
void clarabel_DefaultSolver_f64_update_A_partial(RustDefaultSolverHandle_f64 solver, const uintptr_t* index, const double *values, uintptr_t nvals);
void clarabel_DefaultSolver_f32_update_A_partial(RustDefaultSolverHandle_f32 solver, const uintptr_t* index, const float *values, uintptr_t nvals);
void clarabel_DefaultSolver_f64_update_q(RustDefaultSolverHandle_f64 solver, const double *values, uintptr_t n);
void clarabel_DefaultSolver_f32_update_q(RustDefaultSolverHandle_f32 solver, const float *values, uintptr_t n);
void clarabel_DefaultSolver_f64_update_q_partial(RustDefaultSolverHandle_f64 solver, const uintptr_t* index, const double *values, uintptr_t nvals);
void clarabel_DefaultSolver_f32_update_q_partial(RustDefaultSolverHandle_f32 solver, const uintptr_t* index, const float *values, uintptr_t nvals);
void clarabel_DefaultSolver_f64_update_b(RustDefaultSolverHandle_f64 solver, const double *values, uintptr_t n);
void clarabel_DefaultSolver_f32_update_b(RustDefaultSolverHandle_f32 solver, const float *values, uintptr_t n);
void clarabel_DefaultSolver_f64_update_b_partial(RustDefaultSolverHandle_f64 solver, const uintptr_t* index, const double *values, uintptr_t nvals);
void clarabel_DefaultSolver_f32_update_b_partial(RustDefaultSolverHandle_f32 solver, const uintptr_t* index, const float *values, uintptr_t nvals);
#ifdef FEATURE_SERDE
void clarabel_DefaultSolver_f64_save_to_file(RustDefaultSolverHandle_f64 solver, const char *filename);
void clarabel_DefaultSolver_f32_save_to_file(RustDefaultSolverHandle_f32 solver, const char *filename);
RustDefaultSolverHandle_f64 clarabel_DefaultSolver_f64_load_from_file( const char *filename);
RustDefaultSolverHandle_f32 clarabel_DefaultSolver_f32_load_from_file( const char *filename);
#endif
void clarabel_DefaultSolver_f64_print_to_stdout(RustDefaultSolverHandle_f64 solver);
void clarabel_DefaultSolver_f32_print_to_stdout(RustDefaultSolverHandle_f32 solver);
void clarabel_DefaultSolver_f64_print_to_file(RustDefaultSolverHandle_f64 solver, const char *filename);
void clarabel_DefaultSolver_f32_print_to_file(RustDefaultSolverHandle_f32 solver, const char *filename);
void clarabel_DefaultSolver_f64_print_to_buffer(RustDefaultSolverHandle_f64 solver);
void clarabel_DefaultSolver_f32_print_to_buffer(RustDefaultSolverHandle_f32 solver);
const char* clarabel_DefaultSolver_f64_get_print_buffer(RustDefaultSolverHandle_f64 solver);
const char* clarabel_DefaultSolver_f32_get_print_buffer(RustDefaultSolverHandle_f32 solver);
void clarabel_free_print_buffer(const char *s);
} // extern "C"
// Convert unique_ptr P, A to CscMatrix objects, then init the solver
// The CscMatrix objects are only used to pass the information needed to Rust.
// The colptr and rowptr are stored in the ConvertedCscMatrix objects, which are kept alive by the unique_ptr.
// No conversion is needed for nzval, so the Eigen sparse matrices must be kept alive until the solver is destroyed.
template<>
inline DefaultSolver<double>::DefaultSolver(const Eigen::SparseMatrix<double, Eigen::ColMajor> &P,
const Eigen::Ref<Eigen::VectorX<double>> &q,
const Eigen::SparseMatrix<double, Eigen::ColMajor> &A,
const Eigen::Ref<Eigen::VectorX<double>> &b,
const std::vector<SupportedConeT<double>> &cones,
const DefaultSettings<double> &settings)
{
// Rust wrapper will assume the pointers represent matrices with the right dimensions.
// segfault will occur if the dimensions are incorrect
check_dimensions(P, q, A, b, cones);
ConvertedCscMatrix matrix_P = DefaultSolver<double>::eigen_sparse_to_clarabel(P);
ConvertedCscMatrix matrix_A = DefaultSolver<double>::eigen_sparse_to_clarabel(A);
CscMatrix<double> p(matrix_P.m, matrix_P.n, matrix_P.colptr.data(), matrix_P.rowval.data(), matrix_P.nzval);
CscMatrix<double> a(matrix_A.m, matrix_A.n, matrix_A.colptr.data(), matrix_A.rowval.data(), matrix_A.nzval);
this->handle = clarabel_DefaultSolver_f64_new(&p, q.data(), &a, b.data(), cones.size(), cones.data(), &settings);
}
template<>
inline DefaultSolver<float>::DefaultSolver(const Eigen::SparseMatrix<float, Eigen::ColMajor> &P,
const Eigen::Ref<Eigen::VectorX<float>> &q,
const Eigen::SparseMatrix<float, Eigen::ColMajor> &A,
const Eigen::Ref<Eigen::VectorX<float>> &b,
const std::vector<SupportedConeT<float>> &cones,
const DefaultSettings<float> &settings)
{
// Rust wrapper will assume the pointers represent matrices with the right dimensions.
// segfault will occur if the dimensions are incorrect
check_dimensions(P, q, A, b, cones);
ConvertedCscMatrix matrix_P = DefaultSolver<float>::eigen_sparse_to_clarabel(P);
ConvertedCscMatrix matrix_A = DefaultSolver<float>::eigen_sparse_to_clarabel(A);
CscMatrix<float> p(matrix_P.m, matrix_P.n, matrix_P.colptr.data(), matrix_P.rowval.data(), matrix_P.nzval);
CscMatrix<float> a(matrix_A.m, matrix_A.n, matrix_A.colptr.data(), matrix_A.rowval.data(), matrix_A.nzval);
this->handle = clarabel_DefaultSolver_f32_new(&p, q.data(), &a, b.data(), cones.size(), cones.data(), &settings);
}
template<>
inline DefaultSolver<double>::DefaultSolver(void* handle){
this->handle = handle;
}
template<>
inline DefaultSolver<float>::DefaultSolver(void* handle){
this->handle = handle;
}
template<>
inline DefaultSolver<double>::~DefaultSolver()
{
if (handle != nullptr) // handle is null when an exception is thrown in the constructor
clarabel_DefaultSolver_f64_free(handle);
}
template<>
inline DefaultSolver<float>::~DefaultSolver()
{
if (handle != nullptr)
clarabel_DefaultSolver_f32_free(handle);
}
template<>
inline void DefaultSolver<double>::solve()
{
clarabel_DefaultSolver_f64_solve(handle);
}
template<>
inline void DefaultSolver<float>::solve()
{
clarabel_DefaultSolver_f32_solve(handle);
}
template<>
inline DefaultSolution<double> DefaultSolver<double>::solution() const
{
auto solution = clarabel_DefaultSolver_f64_solution(handle);
return std::move(DefaultSolution<double>(solution));
}
template<>
inline DefaultSolution<float> DefaultSolver<float>::solution() const
{
auto solution = clarabel_DefaultSolver_f32_solution(handle);
return std::move(DefaultSolution<float>(solution));
}
template<>
inline DefaultInfo<double> DefaultSolver<double>::info() const
{
return clarabel_DefaultSolver_f64_info(handle);
}
template<>
inline DefaultInfo<float> DefaultSolver<float>::info() const
{
return clarabel_DefaultSolver_f32_info(handle);
}
template<>
inline void DefaultSolver<double>::set_termination_callback(int (*callback)(DefaultInfo<double>&, void*), void* userdata) {
clarabel_DefaultSolver_f64_set_termination_callback(this->handle, callback,userdata);
}
template<>
inline void DefaultSolver<float>::set_termination_callback(int (*callback)(DefaultInfo<float>&, void*), void* userdata) {
clarabel_DefaultSolver_f32_set_termination_callback(this->handle, callback, userdata);
}
template<>
inline void DefaultSolver<double>::unset_termination_callback() {
clarabel_DefaultSolver_f64_unset_termination_callback(this->handle);
}
template<>
inline void DefaultSolver<float>::unset_termination_callback() {
clarabel_DefaultSolver_f32_unset_termination_callback(this->handle);
}
// update P
template<>
inline void DefaultSolver<double>::update_P(const Eigen::SparseMatrix<double, Eigen::ColMajor> &P){
ConvertedCscMatrix matrix_P = DefaultSolver<double>::eigen_sparse_to_clarabel(P);
CscMatrix<double> mat(matrix_P.m, matrix_P.n, matrix_P.colptr.data(), matrix_P.rowval.data(), matrix_P.nzval);
clarabel_DefaultSolver_f64_update_P_csc(this->handle,&mat);
}
template<>
inline void DefaultSolver<float>::update_P(const Eigen::SparseMatrix<float, Eigen::ColMajor> &P){
ConvertedCscMatrix matrix_P = DefaultSolver<float>::eigen_sparse_to_clarabel(P);
CscMatrix<float> mat(matrix_P.m, matrix_P.n, matrix_P.colptr.data(), matrix_P.rowval.data(), matrix_P.nzval);
clarabel_DefaultSolver_f32_update_P_csc(this->handle,&mat);
}
template<>
inline void DefaultSolver<double>::update_P(const Eigen::Ref<Eigen::VectorX<double>> &nzval){
clarabel_DefaultSolver_f64_update_P(this->handle,nzval.data(), nzval.size());
}
template<>
inline void DefaultSolver<float>::update_P(const Eigen::Ref<Eigen::VectorX<float>> &nzval){
clarabel_DefaultSolver_f32_update_P(this->handle,nzval.data(), nzval.size());
}
template<>
inline void DefaultSolver<double>::update_P(const double *nzval, uintptr_t nnz){
clarabel_DefaultSolver_f64_update_P(this->handle, nzval, nnz);
}
template<>
inline void DefaultSolver<float>::update_P(const float *nzval, uintptr_t nnz){
clarabel_DefaultSolver_f32_update_P(this->handle, nzval, nnz);
}
template<>
inline void DefaultSolver<double>::update_P(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<double>> &values){
if(index.size() != values.size()){
throw std::invalid_argument("index and values must have the same size");
}
clarabel_DefaultSolver_f64_update_P_partial(this->handle, index.data(), values.data(), index.size());
}
template<>
inline void DefaultSolver<float>::update_P(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<float>> &values){
if(index.size() != values.size()){
throw std::invalid_argument("index and values must have the same size");
}
clarabel_DefaultSolver_f32_update_P_partial(this->handle, index.data(), values.data(), index.size());
}
template<>
inline void DefaultSolver<double>::update_P(const uintptr_t* index, const double *values, uintptr_t nvals){
clarabel_DefaultSolver_f64_update_P_partial(this->handle, index, values, nvals);
}
template<>
inline void DefaultSolver<float>::update_P(const uintptr_t* index, const float *values, uintptr_t nvals){
clarabel_DefaultSolver_f32_update_P_partial(this->handle, index, values, nvals);
}
// update A
template<>
inline void DefaultSolver<double>::update_A(const Eigen::SparseMatrix<double, Eigen::ColMajor> &A){
ConvertedCscMatrix matrix_A = DefaultSolver<double>::eigen_sparse_to_clarabel(A);
CscMatrix<double> mat(matrix_A.m, matrix_A.n, matrix_A.colptr.data(), matrix_A.rowval.data(), matrix_A.nzval);
clarabel_DefaultSolver_f64_update_A_csc(this->handle,&mat);
}
template<>
inline void DefaultSolver<float>::update_A(const Eigen::SparseMatrix<float, Eigen::ColMajor> &A){
ConvertedCscMatrix matrix_A = DefaultSolver<float>::eigen_sparse_to_clarabel(A);
CscMatrix<float> mat(matrix_A.m, matrix_A.n, matrix_A.colptr.data(), matrix_A.rowval.data(), matrix_A.nzval);
clarabel_DefaultSolver_f32_update_A_csc(this->handle,&mat);
}
template<>
inline void DefaultSolver<double>::update_A(const Eigen::Ref<Eigen::VectorX<double>> &nzval){
clarabel_DefaultSolver_f64_update_A(this->handle,nzval.data(), nzval.size());
}
template<>
inline void DefaultSolver<float>::update_A(const Eigen::Ref<Eigen::VectorX<float>> &nzval){
clarabel_DefaultSolver_f32_update_A(this->handle,nzval.data(), nzval.size());
}
template<>
inline void DefaultSolver<double>::update_A(const double *nzval, uintptr_t nnz){
clarabel_DefaultSolver_f64_update_A(this->handle, nzval, nnz);
}
template<>
inline void DefaultSolver<float>::update_A(const float *nzval, uintptr_t nnz){
clarabel_DefaultSolver_f32_update_A(this->handle, nzval, nnz);
}
template<>
inline void DefaultSolver<double>::update_A(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<double>> &values){
if(index.size() != values.size()){
throw std::invalid_argument("index and values must have the same size");
}
clarabel_DefaultSolver_f64_update_A_partial(this->handle, index.data(), values.data(), index.size());
}
template<>
inline void DefaultSolver<float>::update_A(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<float>> &values){
if(index.size() != values.size()){
throw std::invalid_argument("index and values must have the same size");
}
clarabel_DefaultSolver_f32_update_A_partial(this->handle, index.data(), values.data(), index.size());
}
template<>
inline void DefaultSolver<double>::update_A(const uintptr_t* index, const double *values, uintptr_t nvals){
clarabel_DefaultSolver_f64_update_A_partial(this->handle, index, values, nvals);
}
template<>
inline void DefaultSolver<float>::update_A(const uintptr_t* index, const float *values, uintptr_t nvals){
clarabel_DefaultSolver_f32_update_A_partial(this->handle, index, values, nvals);
}
// update q
template<>
inline void DefaultSolver<double>::update_q(const Eigen::Ref<Eigen::VectorX<double>> &values){
clarabel_DefaultSolver_f64_update_q(this->handle, values.data(), values.size());
}
template<>
inline void DefaultSolver<float>::update_q(const Eigen::Ref<Eigen::VectorX<float>> &values){
clarabel_DefaultSolver_f32_update_q(this->handle, values.data(), values.size());
}
template<>
inline void DefaultSolver<double>::update_q(const double *values, uintptr_t n){
clarabel_DefaultSolver_f64_update_q(this->handle, values, n);
}
template<>
inline void DefaultSolver<float>::update_q(const float *values, uintptr_t n){
clarabel_DefaultSolver_f32_update_q(this->handle, values, n);
}
template<>
inline void DefaultSolver<double>::update_q(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<double>> &values){
if(index.size() != values.size()){
throw std::invalid_argument("index and values must have the same size");
}
clarabel_DefaultSolver_f64_update_q_partial(this->handle, index.data(), values.data(), index.size());
}
template<>
inline void DefaultSolver<float>::update_q(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<float>> &values){
if(index.size() != values.size()){
throw std::invalid_argument("index and values must have the same size");
}
clarabel_DefaultSolver_f32_update_q_partial(this->handle, index.data(), values.data(), index.size());
}
template<>
inline void DefaultSolver<double>::update_q(const uintptr_t* index, const double *values, uintptr_t nvals){
clarabel_DefaultSolver_f64_update_q_partial(this->handle, index, values, nvals);
}
template<>
inline void DefaultSolver<float>::update_q(const uintptr_t* index, const float *values, uintptr_t nvals){
clarabel_DefaultSolver_f32_update_q_partial(this->handle, index, values, nvals);
}
// update b
template<>
inline void DefaultSolver<double>::update_b(const Eigen::Ref<Eigen::VectorX<double>> &values){
clarabel_DefaultSolver_f64_update_b(this->handle, values.data(), values.size());
}
template<>
inline void DefaultSolver<float>::update_b(const Eigen::Ref<Eigen::VectorX<float>> &values){
clarabel_DefaultSolver_f32_update_b(this->handle, values.data(), values.size());
}
template<>
inline void DefaultSolver<double>::update_b(const double *values, uintptr_t n){
clarabel_DefaultSolver_f64_update_b(this->handle, values, n);
}
template<>
inline void DefaultSolver<float>::update_b(const float *values, uintptr_t n){
clarabel_DefaultSolver_f32_update_b(this->handle, values, n);
}
template<>
inline void DefaultSolver<double>::update_b(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<double>> &values){
if(index.size() != values.size()){
throw std::invalid_argument("index and values must have the same size");
}
clarabel_DefaultSolver_f64_update_b_partial(this->handle, index.data(), values.data(), index.size());
}
template<>
inline void DefaultSolver<float>::update_b(const Eigen::Ref<Eigen::VectorX<uintptr_t>> &index, const Eigen::Ref<Eigen::VectorX<float>> &values){
if(index.size() != values.size()){
throw std::invalid_argument("index and values must have the same size");
}
clarabel_DefaultSolver_f32_update_b_partial(this->handle, index.data(), values.data(), index.size());
}
template<>
inline void DefaultSolver<double>::update_b(const uintptr_t* index, const double *values, uintptr_t nvals){
clarabel_DefaultSolver_f64_update_b_partial(this->handle, index, values, nvals);
}
template<>
inline void DefaultSolver<float>::update_b(const uintptr_t* index, const float *values, uintptr_t nvals){
clarabel_DefaultSolver_f32_update_b_partial(this->handle, index, values, nvals);
}
#ifdef FEATURE_SERDE
template<>
inline void DefaultSolver<double>::save_to_file(const std::string &filename){
clarabel_DefaultSolver_f64_save_to_file(this->handle, filename.c_str());
}
template<>
inline void DefaultSolver<float>::save_to_file(const std::string &filename){
clarabel_DefaultSolver_f32_save_to_file(this->handle, filename.c_str());
}
template<>
inline DefaultSolver<double> DefaultSolver<double>::load_from_file(const std::string &filename){
RustDefaultSolverHandle_f64 handle = clarabel_DefaultSolver_f64_load_from_file(filename.c_str());
return DefaultSolver<double>(handle);
}
template<>
inline DefaultSolver<float> DefaultSolver<float>::load_from_file(const std::string &filename){
RustDefaultSolverHandle_f32 handle = clarabel_DefaultSolver_f32_load_from_file(filename.c_str());
return DefaultSolver<float>(handle);
}
#endif // FEATURE_SERDE
// print configurations
template<>
inline void DefaultSolver<double>::print_to_stdout(){
clarabel_DefaultSolver_f64_print_to_stdout(this->handle);
}
template<>
inline void DefaultSolver<float>::print_to_stdout(){
clarabel_DefaultSolver_f32_print_to_stdout(this->handle);
}
template<>
inline void DefaultSolver<double>::print_to_file(const std::string &filename){
clarabel_DefaultSolver_f64_print_to_file(this->handle, filename.c_str());
}
template<>
inline void DefaultSolver<float>::print_to_file(const std::string &filename){
clarabel_DefaultSolver_f32_print_to_file(this->handle, filename.c_str());
}
template<>
inline void DefaultSolver<double>::print_to_buffer(){
clarabel_DefaultSolver_f64_print_to_buffer(this->handle);
}
template<>
inline void DefaultSolver<float>::print_to_buffer(){
clarabel_DefaultSolver_f32_print_to_buffer(this->handle);
}
template<>
inline std::string DefaultSolver<double>::get_print_buffer(){
const char* buffer = clarabel_DefaultSolver_f64_get_print_buffer(this->handle);
std::string str(buffer);
clarabel_free_print_buffer(buffer);
return str;
}
template<>
inline std::string DefaultSolver<float>::get_print_buffer(){
const char* buffer = clarabel_DefaultSolver_f32_get_print_buffer(this->handle);
std::string str(buffer);
clarabel_free_print_buffer(buffer);
return str;
}
} // namespace clarabel