/
oblas.go
789 lines (749 loc) · 22.4 KB
/
oblas.go
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
// Copyright 2016 The Gosl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package oblas implements lower-level linear algebra routines using OpenBLAS
// for maximum efficiency. This package uses column-major representation for matrices.
//
// Example of col-major data:
// _ _
// | 0 3 |
// A = | 1 4 | ⇒ a = [0, 1, 2, 3, 4, 5]
// |_ 2 5 _|(m x n)
//
// a[i+j*m] = A[i][j]
//
// NOTE: the functions here do not check for the limits of indices. Be careful.
// Panic may occur then.
//
package oblas
/*
#include "openblas_cblas.h"
#include <lapacke.h>
static inline double* cpt(double complex* p) { return (double*)p; }
*/
import "C"
import (
"unsafe"
"github.com/cpmech/gosl/chk"
)
// SetNumThreads sets the number of threads in OpenBLAS
func SetNumThreads(n int) {
C.openblas_set_num_threads(C.int(n))
}
// Ddot forms the dot product of two vectors. Uses unrolled loops for increments equal to one.
//
// See: http://www.netlib.org/lapack/explore-html/d5/df6/ddot_8f.html
func Ddot(n int, x []float64, incx int, y []float64, incy int) (res float64) {
cres := C.cblas_ddot(
C.blasint(n),
(*C.double)(unsafe.Pointer(&x[0])),
C.blasint(incx),
(*C.double)(unsafe.Pointer(&y[0])),
C.blasint(incy),
)
return float64(cres)
}
// Dscal scales a vector by a constant. Uses unrolled loops for increment equal to 1.
//
// See: http://www.netlib.org/lapack/explore-html/d4/dd0/dscal_8f.html
func Dscal(n int, alpha float64, x []float64, incx int) {
C.cblas_dscal(
C.blasint(n),
C.double(alpha),
(*C.double)(unsafe.Pointer(&x[0])),
C.blasint(incx),
)
}
// Daxpy computes constant times a vector plus a vector.
//
// See: http://www.netlib.org/lapack/explore-html/d9/dcd/daxpy_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-axpy
//
// y := alpha*x + y
//
func Daxpy(n int, alpha float64, x []float64, incx int, y []float64, incy int) {
C.cblas_daxpy(
C.blasint(n),
C.double(alpha),
(*C.double)(unsafe.Pointer(&x[0])),
C.blasint(incx),
(*C.double)(unsafe.Pointer(&y[0])),
C.blasint(incy),
)
}
// Zaxpy computes constant times a vector plus a vector.
//
// See: http://www.netlib.org/lapack/explore-html/d7/db2/zaxpy_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-axpy
//
// y := alpha*x + y
//
func Zaxpy(n int, alpha complex128, x []complex128, incx int, y []complex128, incy int) {
C.cblas_zaxpy(
C.blasint(n),
C.cpt((*C.complexdouble)(unsafe.Pointer(&alpha))),
C.cpt((*C.complexdouble)(unsafe.Pointer(&x[0]))),
C.blasint(incx),
C.cpt((*C.complexdouble)(unsafe.Pointer(&y[0]))),
C.blasint(incy),
)
}
// Dgemv performs one of the matrix-vector operations
//
// See: http://www.netlib.org/lapack/explore-html/dc/da8/dgemv_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-gemv
//
// y := alpha*A*x + beta*y, or y := alpha*A**T*x + beta*y,
//
// where alpha and beta are scalars, x and y are vectors and A is an
// m by n matrix.
// trans=false y := alpha*A*x + beta*y.
//
// trans=true y := alpha*A**T*x + beta*y.
func Dgemv(trans bool, m, n int, alpha float64, a []float64, lda int, x []float64, incx int, beta float64, y []float64, incy int) {
C.cblas_dgemv(
cblasColMajor,
cTrans(trans),
C.blasint(m),
C.blasint(n),
C.double(alpha),
(*C.double)(unsafe.Pointer(&a[0])),
C.blasint(lda),
(*C.double)(unsafe.Pointer(&x[0])),
C.blasint(incx),
C.double(beta),
(*C.double)(unsafe.Pointer(&y[0])),
C.blasint(incy),
)
}
// Zgemv performs one of the matrix-vector operations.
//
// See: http://www.netlib.org/lapack/explore-html/db/d40/zgemv_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-gemv
//
// y := alpha*A*x + beta*y, or y := alpha*A**T*x + beta*y, or
//
// y := alpha*A**H*x + beta*y,
//
// where alpha and beta are scalars, x and y are vectors and A is an
// m by n matrix.
func Zgemv(trans bool, m, n int, alpha complex128, a []complex128, lda int, x []complex128, incx int, beta complex128, y []complex128, incy int) {
C.cblas_zgemv(
cblasColMajor,
cTrans(trans),
C.blasint(m),
C.blasint(n),
C.cpt((*C.complexdouble)(unsafe.Pointer(&alpha))),
C.cpt((*C.complexdouble)(unsafe.Pointer(&a[0]))),
C.blasint(lda),
C.cpt((*C.complexdouble)(unsafe.Pointer(&x[0]))),
C.blasint(incx),
C.cpt((*C.complexdouble)(unsafe.Pointer(&beta))),
C.cpt((*C.complexdouble)(unsafe.Pointer(&y[0]))),
C.blasint(incy),
)
}
// Dger performs the rank 1 operation
//
// See: http://www.netlib.org/lapack/explore-html/dc/da8/dger_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-ger
//
// A := alpha*x*y**T + A,
//
// where alpha is a scalar, x is an m element vector, y is an n element
// vector and A is an m by n matrix.
func Dger(m, n int, alpha float64, x []float64, incx int, y []float64, incy int, a []float64, lda int) {
C.cblas_dger(
cblasColMajor,
C.blasint(m),
C.blasint(n),
C.double(alpha),
(*C.double)(unsafe.Pointer(&x[0])),
C.blasint(incx),
(*C.double)(unsafe.Pointer(&y[0])),
C.blasint(incy),
(*C.double)(unsafe.Pointer(&a[0])),
C.blasint(lda),
)
}
// Dgemm performs one of the matrix-matrix operations
//
// false,false: C_{m,n} := α ⋅ A_{m,k} ⋅ B_{k,n} + β ⋅ C_{m,n}
// false,true: C_{m,n} := α ⋅ A_{m,k} ⋅ B_{n,k} + β ⋅ C_{m,n}
// true, false: C_{m,n} := α ⋅ A_{k,m} ⋅ B_{k,n} + β ⋅ C_{m,n}
// true, true: C_{m,n} := α ⋅ A_{k,m} ⋅ B_{n,k} + β ⋅ C_{m,n}
//
// see: http://www.netlib.org/lapack/explore-html/d7/d2b/dgemm_8f.html
//
// see: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-gemm
//
// C := alpha*op( A )*op( B ) + beta*C,
//
// where op( X ) is one of
//
// op( X ) = X or op( X ) = X**T,
//
// alpha and beta are scalars, and A, B and C are matrices, with op( A )
// an m by k matrix, op( B ) a k by n matrix and C an m by n matrix.
func Dgemm(transA, transB bool, m, n, k int, alpha float64, a []float64, lda int, b []float64, ldb int, beta float64, c []float64, ldc int) {
C.cblas_dgemm(
cblasColMajor,
cTrans(transA),
cTrans(transB),
C.blasint(m),
C.blasint(n),
C.blasint(k),
C.double(alpha),
(*C.double)(unsafe.Pointer(&a[0])),
C.blasint(lda),
(*C.double)(unsafe.Pointer(&b[0])),
C.blasint(ldb),
C.double(beta),
(*C.double)(unsafe.Pointer(&c[0])),
C.blasint(ldc),
)
}
// Zgemm performs one of the matrix-matrix operations
//
// see: http://www.netlib.org/lapack/explore-html/d7/d76/zgemm_8f.html
//
// see: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-gemm
//
// C := alpha*op( A )*op( B ) + beta*C,
//
// where op( X ) is one of
//
// op( X ) = X or op( X ) = X**T or op( X ) = X**H,
//
// alpha and beta are scalars, and A, B and C are matrices, with op( A )
// an m by k matrix, op( B ) a k by n matrix and C an m by n matrix.
func Zgemm(transA, transB bool, m, n, k int, alpha complex128, a []complex128, lda int, b []complex128, ldb int, beta complex128, c []complex128, ldc int) {
C.cblas_zgemm(
cblasColMajor,
cTrans(transA),
cTrans(transB),
C.blasint(m),
C.blasint(n),
C.blasint(k),
C.cpt((*C.complexdouble)(unsafe.Pointer(&alpha))),
C.cpt((*C.complexdouble)(unsafe.Pointer(&a[0]))),
C.blasint(lda),
C.cpt((*C.complexdouble)(unsafe.Pointer(&b[0]))),
C.blasint(ldb),
C.cpt((*C.complexdouble)(unsafe.Pointer(&beta))),
C.cpt((*C.complexdouble)(unsafe.Pointer(&c[0]))),
C.blasint(ldc),
)
}
// Dgesv computes the solution to a real system of linear equations.
//
// See: http://www.netlib.org/lapack/explore-html/d8/d72/dgesv_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-gesv
//
// The system is:
//
// A * X = B,
//
// where A is an N-by-N matrix and X and B are N-by-NRHS matrices.
//
// The LU decomposition with partial pivoting and row interchanges is
// used to factor A as
//
// A = P * L * U,
//
// where P is a permutation matrix, L is unit lower triangular, and U is
// upper triangular. The factored form of A is then used to solve the
// system of equations A * X = B.
//
// NOTE: matrix 'a' will be modified
func Dgesv(n, nrhs int, a []float64, lda int, ipiv []int32, b []float64, ldb int) {
if len(ipiv) != n {
chk.Panic("len(ipiv) must be equal to n. %d != %d\n", len(ipiv), n)
}
info := C.LAPACKE_dgesv(
C.int(lapackColMajor),
C.lapack_int(n),
C.lapack_int(nrhs),
(*C.double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
(*C.lapack_int)(unsafe.Pointer(&ipiv[0])),
(*C.double)(unsafe.Pointer(&b[0])),
C.lapack_int(ldb),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Zgesv computes the solution to a complex system of linear equations.
//
// See: http://www.netlib.org/lapack/explore-html/d1/ddc/zgesv_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-gesv
//
// The system is:
//
// A * X = B,
//
// where A is an N-by-N matrix and X and B are N-by-NRHS matrices.
//
// The LU decomposition with partial pivoting and row interchanges is
// used to factor A as
//
// A = P * L * U,
//
// where P is a permutation matrix, L is unit lower triangular, and U is
// upper triangular. The factored form of A is then used to solve the
// system of equations A * X = B.
//
// NOTE: matrix 'a' will be modified
func Zgesv(n, nrhs int, a []complex128, lda int, ipiv []int32, b []complex128, ldb int) {
if len(ipiv) != n {
chk.Panic("len(ipiv) must be equal to n. %d != %d\n", len(ipiv), n)
}
info := C.LAPACKE_zgesv(
C.int(lapackColMajor),
C.lapack_int(n),
C.lapack_int(nrhs),
(*C.lapack_complex_double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
(*C.lapack_int)(unsafe.Pointer(&ipiv[0])),
(*C.lapack_complex_double)(unsafe.Pointer(&b[0])),
C.lapack_int(ldb),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Dgesvd computes the singular value decomposition (SVD) of a real M-by-N matrix A, optionally computing the left and/or right singular vectors.
//
// See: http://www.netlib.org/lapack/explore-html/d8/d2d/dgesvd_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-gesvd
//
// The SVD is written
//
// A = U * SIGMA * transpose(V)
//
// where SIGMA is an M-by-N matrix which is zero except for its
// min(m,n) diagonal elements, U is an M-by-M orthogonal matrix, and
// V is an N-by-N orthogonal matrix. The diagonal elements of SIGMA
// are the singular values of A; they are real and non-negative, and
// are returned in descending order. The first min(m,n) columns of
// U and V are the left and right singular vectors of A.
//
// Note that the routine returns V**T, not V.
//
// NOTE: matrix 'a' will be modified
func Dgesvd(jobu, jobvt rune, m, n int, a []float64, lda int, s []float64, u []float64, ldu int, vt []float64, ldvt int, superb []float64) {
info := C.LAPACKE_dgesvd(
C.int(lapackColMajor),
C.char(jobu),
C.char(jobvt),
C.lapack_int(m),
C.lapack_int(n),
(*C.double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
(*C.double)(unsafe.Pointer(&s[0])),
(*C.double)(unsafe.Pointer(&u[0])),
C.lapack_int(ldu),
(*C.double)(unsafe.Pointer(&vt[0])),
C.lapack_int(ldvt),
(*C.double)(unsafe.Pointer(&superb[0])),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Zgesvd computes the singular value decomposition (SVD) of a complex M-by-N matrix A, optionally computing the left and/or right singular vectors.
//
// See: http://www.netlib.org/lapack/explore-html/d6/d42/zgesvd_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-gesvd
//
// The SVD is written
//
// A = U * SIGMA * conjugate-transpose(V)
//
// where SIGMA is an M-by-N matrix which is zero except for its
// min(m,n) diagonal elements, U is an M-by-M unitary matrix, and
// V is an N-by-N unitary matrix. The diagonal elements of SIGMA
// are the singular values of A; they are real and non-negative, and
// are returned in descending order. The first min(m,n) columns of
// U and V are the left and right singular vectors of A.
//
// Note that the routine returns V**H, not V.
//
// NOTE: matrix 'a' will be modified
func Zgesvd(jobu, jobvt rune, m, n int, a []complex128, lda int, s []float64, u []complex128, ldu int, vt []complex128, ldvt int, superb []float64) {
info := C.LAPACKE_zgesvd(
C.int(lapackColMajor),
C.char(jobu),
C.char(jobvt),
C.lapack_int(m),
C.lapack_int(n),
(*C.lapack_complex_double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
(*C.double)(unsafe.Pointer(&s[0])),
(*C.lapack_complex_double)(unsafe.Pointer(&u[0])),
C.lapack_int(ldu),
(*C.lapack_complex_double)(unsafe.Pointer(&vt[0])),
C.lapack_int(ldvt),
(*C.double)(unsafe.Pointer(&superb[0])),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Dgetrf computes an LU factorization of a general M-by-N matrix A using partial pivoting with row interchanges.
//
// See: http://www.netlib.org/lapack/explore-html/d3/d6a/dgetrf_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-getrf
//
// The factorization has the form
// A = P * L * U
// where P is a permutation matrix, L is lower triangular with unit
// diagonal elements (lower trapezoidal if m > n), and U is upper
// triangular (upper trapezoidal if m < n).
//
// This is the right-looking Level 3 BLAS version of the algorithm.
//
// NOTE: (1) matrix 'a' will be modified
// (2) ipiv indices are 1-based (i.e. Fortran)
func Dgetrf(m, n int, a []float64, lda int, ipiv []int32) {
info := C.LAPACKE_dgetrf(
C.int(lapackColMajor),
C.lapack_int(m),
C.lapack_int(n),
(*C.double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
(*C.lapack_int)(unsafe.Pointer(&ipiv[0])),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Zgetrf computes an LU factorization of a general M-by-N matrix A using partial pivoting with row interchanges.
//
// See: http://www.netlib.org/lapack/explore-html/dd/dd1/zgetrf_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-getrf
//
// The factorization has the form
// A = P * L * U
// where P is a permutation matrix, L is lower triangular with unit
// diagonal elements (lower trapezoidal if m > n), and U is upper
// triangular (upper trapezoidal if m < n).
//
// This is the right-looking Level 3 BLAS version of the algorithm.
//
// NOTE: (1) matrix 'a' will be modified
// (2) ipiv indices are 1-based (i.e. Fortran)
func Zgetrf(m, n int, a []complex128, lda int, ipiv []int32) {
info := C.LAPACKE_zgetrf(
C.int(lapackColMajor),
C.lapack_int(m),
C.lapack_int(n),
(*C.lapack_complex_double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
(*C.lapack_int)(unsafe.Pointer(&ipiv[0])),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Dgetri computes the inverse of a matrix using the LU factorization computed by DGETRF.
//
// See: http://www.netlib.org/lapack/explore-html/df/da4/dgetri_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-getri
//
// This method inverts U and then computes inv(A) by solving the system
// inv(A)*L = inv(U) for inv(A).
func Dgetri(n int, a []float64, lda int, ipiv []int32) {
info := C.LAPACKE_dgetri(
C.int(lapackColMajor),
C.lapack_int(n),
(*C.double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
(*C.lapack_int)(unsafe.Pointer(&ipiv[0])),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Zgetri computes the inverse of a matrix using the LU factorization computed by Zgetrf.
//
// See: http://www.netlib.org/lapack/explore-html/d0/db3/zgetri_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-getri
//
// This method inverts U and then computes inv(A) by solving the system
// inv(A)*L = inv(U) for inv(A).
func Zgetri(n int, a []complex128, lda int, ipiv []int32) {
info := C.LAPACKE_zgetri(
C.int(lapackColMajor),
C.lapack_int(n),
(*C.lapack_complex_double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
(*C.lapack_int)(unsafe.Pointer(&ipiv[0])),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Dsyrk performs one of the symmetric rank k operations
//
// See: http://www.netlib.org/lapack/explore-html/dc/d05/dsyrk_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-syrk
//
// C := alpha*A*A**T + beta*C,
//
// or
//
// C := alpha*A**T*A + beta*C,
//
// where alpha and beta are scalars, C is an n by n symmetric matrix
// and A is an n by k matrix in the first case and a k by n matrix
// in the second case.
func Dsyrk(up, trans bool, n, k int, alpha float64, a []float64, lda int, beta float64, c []float64, ldc int) {
C.cblas_dsyrk(
cblasColMajor,
cUplo(up),
cTrans(trans),
C.blasint(n),
C.blasint(k),
C.double(alpha),
(*C.double)(unsafe.Pointer(&a[0])),
C.blasint(lda),
C.double(beta),
(*C.double)(unsafe.Pointer(&c[0])),
C.blasint(ldc),
)
}
// Zsyrk performs one of the symmetric rank k operations
//
// See: http://www.netlib.org/lapack/explore-html/de/d54/zsyrk_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-syrk
//
// C := alpha*A*A**T + beta*C,
//
// or
//
// C := alpha*A**T*A + beta*C,
//
// where alpha and beta are scalars, C is an n by n symmetric matrix
// and A is an n by k matrix in the first case and a k by n matrix
// in the second case.
func Zsyrk(up, trans bool, n, k int, alpha complex128, a []complex128, lda int, beta complex128, c []complex128, ldc int) {
C.cblas_zsyrk(
cblasColMajor,
cUplo(up),
cTrans(trans),
C.blasint(n),
C.blasint(k),
C.cpt((*C.complexdouble)(unsafe.Pointer(&alpha))),
C.cpt((*C.complexdouble)(unsafe.Pointer(&a[0]))),
C.blasint(lda),
C.cpt((*C.complexdouble)(unsafe.Pointer(&beta))),
C.cpt((*C.complexdouble)(unsafe.Pointer(&c[0]))),
C.blasint(ldc),
)
}
// Zherk performs one of the hermitian rank k operations
//
// See: http://www.netlib.org/lapack/explore-html/d1/db1/zherk_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-cblas-herk
//
// C := alpha*A*A**H + beta*C,
//
// or
//
// C := alpha*A**H*A + beta*C,
//
// where alpha and beta are real scalars, C is an n by n hermitian
// matrix and A is an n by k matrix in the first case and a k by n
// matrix in the second case.
func Zherk(up, trans bool, n, k int, alpha float64, a []complex128, lda int, beta float64, c []complex128, ldc int) {
C.cblas_zherk(
cblasColMajor,
cUplo(up),
cTrans(trans),
C.blasint(n),
C.blasint(k),
C.double(alpha),
C.cpt((*C.complexdouble)(unsafe.Pointer(&a[0]))),
C.blasint(lda),
C.double(beta),
C.cpt((*C.complexdouble)(unsafe.Pointer(&c[0]))),
C.blasint(ldc),
)
}
// Dpotrf computes the Cholesky factorization of a real symmetric positive definite matrix A.
//
// See: http://www.netlib.org/lapack/explore-html/d0/d8a/dpotrf_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-potrf
//
// The factorization has the form
//
// A = U**T * U, if UPLO = 'U'
//
// or
//
// A = L * L**T, if UPLO = 'L'
//
// where U is an upper triangular matrix and L is lower triangular.
//
// This is the block version of the algorithm, calling Level 3 BLAS.
func Dpotrf(up bool, n int, a []float64, lda int) {
info := C.LAPACKE_dpotrf(
C.int(lapackColMajor),
lUplo(up),
C.lapack_int(n),
(*C.double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Zpotrf computes the Cholesky factorization of a complex Hermitian positive definite matrix A.
//
// See: http://www.netlib.org/lapack/explore-html/d1/db9/zpotrf_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-potrf
//
// The factorization has the form
//
// A = U**H * U, if UPLO = 'U'
//
// or
//
// A = L * L**H, if UPLO = 'L'
//
// where U is an upper triangular matrix and L is lower triangular.
//
// This is the block version of the algorithm, calling Level 3 BLAS.
func Zpotrf(up bool, n int, a []complex128, lda int) {
info := C.LAPACKE_zpotrf(
C.int(lapackColMajor),
lUplo(up),
C.lapack_int(n),
(*C.lapack_complex_double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// Dgeev computes for an N-by-N real nonsymmetric matrix A, the
// eigenvalues and, optionally, the left and/or right eigenvectors.
//
// See: http://www.netlib.org/lapack/explore-html/d9/d28/dgeev_8f.html
//
// See: https://software.intel.com/en-us/mkl-developer-reference-c-geev
//
// See: https://www.nag.co.uk/numeric/fl/nagdoc_fl26/html/f08/f08naf.html
//
// The right eigenvector v(j) of A satisfies
//
// A * v(j) = lambda(j) * v(j)
//
// where lambda(j) is its eigenvalue.
//
// The left eigenvector u(j) of A satisfies
//
// u(j)**H * A = lambda(j) * u(j)**H
//
// where u(j)**H denotes the conjugate-transpose of u(j).
//
// The computed eigenvectors are normalized to have Euclidean norm
// equal to 1 and largest component real.
func Dgeev(calcVl, calcVr bool, n int, a []float64, lda int, wr []float64, wi, vl []float64, ldvl int, vr []float64, ldvr int) {
var vvl, vvr *C.double
if calcVl {
vvl = (*C.double)(unsafe.Pointer(&vl[0]))
} else {
ldvl = 1
}
if calcVr {
vvr = (*C.double)(unsafe.Pointer(&vr[0]))
} else {
ldvr = 1
}
info := C.LAPACKE_dgeev(
C.int(lapackColMajor),
jobVlr(calcVl),
jobVlr(calcVr),
C.lapack_int(n),
(*C.double)(unsafe.Pointer(&a[0])),
C.lapack_int(lda),
(*C.double)(unsafe.Pointer(&wr[0])),
(*C.double)(unsafe.Pointer(&wi[0])),
vvl,
C.lapack_int(ldvl),
vvr,
C.lapack_int(ldvr),
)
if info != 0 {
chk.Panic("lapack failed\n")
}
}
// auxiliary //////////////////////////////////////////////////////////////////////////////////////
// constants
const (
// Lapack matrix layout
lapackRowMajor int = 101
lapackColMajor int = 102
// CBLAS_ORDER;
cblasRowMajor uint32 = 101
cblasColMajor uint32 = 102
// CBLAS_TRANSPOSE;
cblasNoTrans uint32 = 111
cblasTrans uint32 = 112
cblasConjTrans uint32 = 113
cblasConjNoTrans uint32 = 114
// CBLAS_UPLO;
cblasUpper uint32 = 121
cblasLower uint32 = 122
// CBLAS_DIAG;
cblasNonUnit uint32 = 131
cblasUnit uint32 = 132
// CBLAS_SIDE;
cblasLeft uint32 = 141
cblasRight uint32 = 142
)
func cTrans(trans bool) uint32 {
if trans {
return cblasTrans
}
return cblasNoTrans
}
func cUplo(up bool) uint32 {
if up {
return cblasUpper
}
return cblasLower
}
func lUplo(up bool) C.char {
if up {
return 'U'
}
return 'L'
}
func jobVlr(doCalc bool) C.char {
if doCalc {
return 'V'
}
return 'N'
}