clarabel 0.11.1

Clarabel Conic Interior Point Solver for Rust / Python
Documentation
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
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(clippy::too_many_arguments)]

cfg_if::cfg_if! {
    if #[cfg(sdp_pyblas)] {
        // imports via scipy 
        use crate::python::pyblas::*;
    }
    else {
        // standard imports via blas-lapack-rs crates 
        extern crate blas_src;
        extern crate lapack_src;
        use lapack::*;
        use blas::*;
    }
}

pub trait BlasFloatT: 
    private::BlasFloatSealed 
    + XsyevrScalar 
    + XpotrfScalar 
    + XpotrsScalar 
    + XgesddScalar 
    + XgesvdScalar 
    + XgemmScalar 
    + XgemvScalar 
    + XsymvScalar 
    + XsyrkScalar
    + Xsyr2kScalar
    + XgesvScalar
{}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
	// R blas/lapack only provides double precision routines
	impl BlasFloatT for f32 {}
  }
}
impl BlasFloatT for f64 {}

mod private {
  pub trait BlasFloatSealed {}
  cfg_if::cfg_if! {
	if #[cfg(not(feature="sdp-r"))] {
	    // R blas/lapack only provides double precision routines
	    impl BlasFloatSealed for f32 {}     
	}
  }
  impl BlasFloatSealed for f64 {}
}


// --------------------------------------
// ?syevr : Symmetric eigen decomposition
// --------------------------------------

pub trait XsyevrScalar: Sized {
    fn xsyevr(
        jobz: u8, range: u8, uplo: u8, n: i32, a: &mut [Self], lda: i32, vl: Self, vu: Self, il: i32, iu: i32, 
        abstol: Self, m: &mut i32, w: &mut [Self], z: &mut [Self], ldz: i32, isuppz: &mut [i32], 
        work: &mut [Self], lwork: i32, iwork: &mut [i32], liwork: i32, info: &mut i32,
    );
}

macro_rules! impl_blas_xsyevr {
    ($T:ty, $XSYEVR:path) => {
        impl XsyevrScalar for $T {
            fn xsyevr(
                jobz: u8, range: u8, uplo: u8, n: i32, a: &mut [Self], lda: i32, vl: Self, vu: Self, il: i32, iu: i32, 
                abstol: Self, m: &mut i32, w: &mut [Self], z: &mut [Self], ldz: i32, isuppz: &mut [i32], 
                work: &mut [$T], lwork: i32, iwork: &mut [i32], liwork: i32, info: &mut i32,
            ) {
                unsafe{
                    $XSYEVR(
                        jobz, range, uplo, n, a, lda, vl, vu, il, iu, abstol, m, 
                        w, z, ldz, isuppz, work, lwork, iwork, liwork, info,
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_xsyevr!(f32, ssyevr);      
  }
}
impl_blas_xsyevr!(f64, dsyevr);

// --------------------------------------
// ?potrf : Cholesky decomposition
// --------------------------------------

pub trait XpotrfScalar: Sized {
    fn xpotrf(
        uplo: u8, n: i32, a: &mut [Self], lda: i32, info: &mut i32
    );
}

pub trait XpotrsScalar: Sized {
    fn xpotrs(
        uplo: u8, n: i32, nrhs: i32, a: &[Self], lda: i32,  b: &mut [Self], ldb: i32, info: &mut i32
    );
}

macro_rules! impl_blas_xpotrfs{
    ($T:ty, $XPOTRF:path, $XPOTRS:path) => {
        impl XpotrfScalar for $T {
            fn xpotrf(
                uplo: u8, n: i32, a: &mut [Self], lda: i32, info: &mut i32
            ) {
                unsafe{
                    $XPOTRF(
                        uplo, n, a, lda, info
                    );
                }
            }
        }
        impl XpotrsScalar for $T {
            fn xpotrs(
                uplo: u8, n: i32, nrhs: i32, a: &[Self], lda: i32, b: &mut [Self], ldb: i32, info: &mut i32
            ) {
                unsafe{
                    $XPOTRS(
                        uplo, n, nrhs, a, lda, b, ldb, info
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_xpotrfs!(f32, spotrf, spotrs);      
  }
}
impl_blas_xpotrfs!(f64, dpotrf, dpotrs);


// --------------------------------------
// ?gesdd : SVD (divide and conquer method)
// --------------------------------------

pub trait XgesddScalar: Sized {
    fn xgesdd(
        jobz: u8, m: i32, n: i32, a: &mut [Self], lda: i32, 
        s: &mut [Self], u: &mut [Self], ldu: i32, vt: &mut [Self], ldvt: i32, 
        work: &mut [Self], lwork: i32, iwork: &mut [i32], info: &mut i32
    );
}

macro_rules! impl_blas_xgesdd{
    ($T:ty, $XGESDD:path) => {
        impl XgesddScalar for $T {
            fn xgesdd(
                jobz: u8, m: i32, n: i32, a: &mut [Self], lda: i32, 
                s: &mut [Self], u: &mut [Self], ldu: i32, vt: &mut [Self], ldvt: i32, 
                work: &mut [Self], lwork: i32, iwork: &mut [i32], info: &mut i32
            ) {
                unsafe{
                    $XGESDD(
                        jobz, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, iwork, info
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_xgesdd!(f32, sgesdd);
  }
}
impl_blas_xgesdd!(f64, dgesdd);


// --------------------------------------
// ?gesvd : SVD (QR method)
// --------------------------------------

pub trait XgesvdScalar: Sized {
    fn xgesvd(
        jobu: u8, jobvt: u8,m: i32, n: i32, a: &mut [Self], lda: i32, 
        s: &mut [Self], u: &mut [Self], ldu: i32, vt: &mut [Self], ldvt: i32, 
        work: &mut [Self], lwork: i32, info: &mut i32
    );
}

macro_rules! impl_blas_xgesvd{
    ($T:ty, $XGESVD:path) => {
        impl XgesvdScalar for $T {
            fn xgesvd(
                jobu: u8, jobvt: u8,m: i32, n: i32, a: &mut [Self], lda: i32, 
                s: &mut [Self], u: &mut [Self], ldu: i32, vt: &mut [Self], ldvt: i32, 
                work: &mut [Self], lwork: i32, info: &mut i32
            ) {
                unsafe{
                    $XGESVD(
                        jobu, jobvt, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, info
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_xgesvd!(f32, sgesvd);
  }
}
impl_blas_xgesvd!(f64, dgesvd);


// --------------------------------------
// ?gemm : matrix matrix multiply
// --------------------------------------

pub trait XgemmScalar: Sized {
    fn xgemm(
        transa: u8, transb: u8, m: i32, n: i32, k: i32, alpha: Self, a: &[Self], 
        lda: i32, b: &[Self], ldb: i32, beta: Self, c: &mut [Self], ldc: i32
    );
}

macro_rules! impl_blas_gemm {
    ($T:ty, $XGEMM:path) => {
        impl XgemmScalar for $T {
            fn xgemm(
                transa: u8, transb: u8, m: i32, n: i32, k: i32, alpha: Self, a: &[Self], 
                lda: i32, b: &[Self], ldb: i32, beta: Self, c: &mut [Self], ldc: i32
            ) {
                unsafe{
                    $XGEMM(
                        transa, transb, m, n, k, alpha, a, 
                        lda, b, ldb, beta, c, ldc
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_gemm!(f32, sgemm);
  }
}
impl_blas_gemm!(f64, dgemm);

// --------------------------------------
// ?gemv : matrix vector multiply (general shape)
// --------------------------------------

pub trait XgemvScalar: Sized {
    fn xgemv(
        trans: u8, m: i32, n: i32, alpha: Self, a: &[Self], lda: i32, 
        x: &[Self], incx: i32, beta: Self, y: &mut [Self], incy: i32
    );
}


macro_rules! impl_blas_gemv {
    ($T:ty, $XGEMV:path) => {
        impl XgemvScalar for $T {
            fn xgemv(
                trans: u8, m: i32, n: i32, alpha: Self, a: &[Self], lda: i32, 
                x: &[Self], incx: i32, beta: Self, y: &mut [Self], incy: i32
            ) {
                unsafe{
                    $XGEMV(
                        trans, m, n, alpha, a, lda, x, incx, beta, y, incy
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_gemv!(f32, sgemv);
  }
}
impl_blas_gemv!(f64, dgemv);


// --------------------------------------
// ?symv : matrix vector multiply (symmetric)
// --------------------------------------

pub trait XsymvScalar: Sized {
    fn xsymv(
        uplo: u8, n: i32, alpha: Self, a: &[Self], lda: i32, 
        x: &[Self], incx: i32, beta: Self, y: &mut [Self], incy: i32
    );
}


macro_rules! impl_blas_gsymv {
    ($T:ty, $XSYMV:path) => {
        impl XsymvScalar for $T {
            fn xsymv(
                uplo: u8, n: i32, alpha: Self, a: &[Self], lda: i32, 
                x: &[Self], incx: i32, beta: Self, y: &mut [Self], incy: i32
            ) {
                unsafe{
                    $XSYMV(
                        uplo, n, alpha, a, lda, x, incx, beta, y, incy
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_gsymv!(f32, ssymv);
  }
}
impl_blas_gsymv!(f64, dsymv);


// --------------------------------------
// ?syrk : symmetric rank k update
// --------------------------------------

pub trait XsyrkScalar: Sized {
    fn xsyrk(
        uplo: u8, trans: u8, n: i32, k: i32, alpha: Self, 
        a: &[Self], lda: i32, beta: Self, c: &mut [Self], ldc: i32
    );
}


macro_rules! impl_blas_gsyrk {
    ($T:ty, $XSYRK:path) => {
        impl XsyrkScalar for $T {
            fn xsyrk(
                uplo: u8, trans: u8, n: i32, k: i32, alpha: Self, 
                a: &[Self], lda: i32, beta: Self, c: &mut [Self], ldc: i32
            ) {
                unsafe{
                    $XSYRK(
                        uplo, trans, n, k, alpha, a, lda, beta, c, ldc
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_gsyrk!(f32, ssyrk);
  }
}
impl_blas_gsyrk!(f64, dsyrk);

// --------------------------------------
// ?syrk : symmetric rank 2k update
// --------------------------------------

pub trait Xsyr2kScalar: Sized {
    fn xsyr2k(
        uplo: u8, trans: u8, n: i32, k: i32, alpha: Self, a: &[Self], lda: i32, 
        b: &[Self], ldb: i32, beta: Self, c: &mut [Self], ldc: i32
    );
}


macro_rules! impl_blas_gsyr2k {
    ($T:ty, $XSYR2K:path) => {
        impl Xsyr2kScalar for $T {
            fn xsyr2k(
                uplo: u8, trans: u8, n: i32, k: i32, alpha: Self, a: &[Self], lda: i32, 
                b: &[Self], ldb: i32, beta: Self, c: &mut [Self], ldc: i32
            ) {
                unsafe{
                    $XSYR2K(
                        uplo, trans, n, k, alpha, a, lda, 
                        b, ldb, beta, c, ldc
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_gsyr2k!(f32, ssyr2k);
  }
}
impl_blas_gsyr2k!(f64, dsyr2k);


// --------------------------------------
// ?gesv : Generalized (LU) linear solve, multiple right hand side
// --------------------------------------

pub trait XgesvScalar: Sized {
    fn xgesv(
        n: i32, nrhs: i32, a: &mut [Self], lda: i32, ipiv: &mut [i32], 
                b: &mut [Self], ldb: i32, info:&mut i32,
    );
}

macro_rules! impl_blas_xgesv{
    ($T:ty, $XGESV:path) => {
        impl XgesvScalar for $T {
            fn xgesv(
                n: i32, nrhs: i32, a: &mut [Self], lda: i32, ipiv: &mut [i32], 
                b: &mut [Self], ldb: i32, info:&mut i32,
            ) {
                unsafe{
                    $XGESV(
                        n, nrhs, a, lda, ipiv, b, ldb, info,
                    );
                }
            }
        }
    };
}

cfg_if::cfg_if! {
  if #[cfg(not(feature="sdp-r"))] {
      // R blas/lapack only provides double precision routines
      impl_blas_xgesv!(f32, sgesv);
  }
}
impl_blas_xgesv!(f64, dgesv);