forked from carvalho/numlua
-
Notifications
You must be signed in to change notification settings - Fork 4
/
lmatrix.c
4904 lines (4697 loc) · 142 KB
/
lmatrix.c
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* {=================================================================
*
* lmatrix.c
* Multidimensional matrix library for NumericLua
* Luis Carvalho ([email protected])
* See Copyright Notice in numlua.h
*
* ==================================================================} */
#include <lua.h>
#include <lauxlib.h>
#include <float.h>
#include <string.h>
#include <stdlib.h>
#include "numlua.h"
/* Constants */
static int one = 1;
static int two = 2;
static lua_Number minusone = -1;
static lua_Number oned = 1.;
static nl_Complex onec = 1.;
/* TODO [wishlist]:
*
* _cshift_(m, shift [, what]): if `m` is 2D, `shift` can be vector depending
* on `what`
*
* _eoshift_(m, shift [, boundary] [, what]): if `m` is 2D, `shift` and
* `boundary` can be vector depending on `what`
*
* `concat` is a _builder_ from lower equal dimensions to a higher dimension
* Options: m1, m2, ..., mn with dims (d1, d2, ..., dp)
* (i) Increase dim on dim k: (d1, d2, d{k-1}, n, dk, ..., dp) ["join"]
* (ii) Across dim k: (d1, d2, ..., sum(dk), ..., dp) ["concat"]
* - particular cases: rowcat (dim=2, k=1), colcat (dim=2, k=2), c (dim=1)
*
* advanced linear algebra: `schur` (_gees), `hess` (_gehrd), `qz`
*/
static int matrix_mt_ = 0;
#define MATRIX_MT ((void *)&matrix_mt_)
#define SECTION_STUB ((nl_Section *)&matrix_mt_)
#define DATA_STUB ((lua_Number *)&matrix_mt_)
/* error messages and checks */
#define CONF_ERROR "dimensions are not conformable"
#define DOMC_ERROR "domains are not consistent"
#define checkvector(L,m,a) \
luaL_argcheck(L, (m)->ndims==1, (a), "vector expected")
#define checkrealvector(L,m,a) \
luaL_argcheck(L, (m)->ndims==1 && !(m)->iscomplex, \
(a), "real vector expected")
#define checkarray(L,m,a) \
luaL_argcheck(L, (m)->ndims==2, (a), "array expected")
#define check2dmatrix(L,m,a) \
luaL_argcheck(L, (m)->ndims <= 2, (a), "two-dimensional matrix expected")
#define checksquare(L,m,a) \
luaL_argcheck(L, (m)->ndims==2 && (m)->dim[0]==(m)->dim[1], \
(a), "square matrix expected")
#define checksection(L,m,a) \
luaL_argcheck(L, !(m)->section, (a), "sections are not allowed")
#define check2dsection(L,m,a) \
luaL_argcheck(L, (m)->stride==1 \
&& (!(m)->section \
|| ((m)->section[0].step==1 && (m)->section[1].step==1)), \
(a), "only simple array sections are allowed")
/* note: simple array sections: unitary steps */
/* {=====================================================================
* Auxiliary
* ======================================================================} */
/* i must be != 0 */
#define CIRC(i,n) ((i)>0 ? (((i)-1)%(n)+1) : ((i)+1)%(n)+(n))
#define conformable(a,b) \
((a)->size==(b)->size && (a)->iscomplex==(b)->iscomplex)
#define LD(m,i) (((m)->section) ? (m)->section[i].ld : (m)->dim[i])
#define STEP(m,i) (((m)->section) ? (m)->section[i].step : 1)
#define DSHIFT(m,i) ((m)->data + nl_msshift((m),(i)))
#define CSHIFT(m,i) (CPX((m)->data) + nl_msshift((m),(i)))
/* overall stride in section `m` from element-order index `eo` (zero-based)
* note that nl_msshift(m, 0) == 0 */
NUMLUA_API int nl_msshift (nl_Matrix *m, int eo) {
int i, d, shift = 0;
int stride = m->stride;
for (i = 0; i < m->ndims; i++) {
d = eo % m->dim[i];
shift += (d * m->section[i].step) * stride;
stride *= m->section[i].ld;
eo = (eo - d) / m->dim[i];
}
return shift;
}
/* [[ Internal ]] */
/* meant for use on functions that have MT as env */
static nl_Matrix *tomatrix (lua_State *L, int narg) {
nl_Matrix *p = NULL;
if (lua_type(L, narg) == LUA_TUSERDATA /* userdata? */
&& lua_getmetatable(L, narg)) { /* has metatable? */
if (lua_rawequal(L, -1, lua_upvalueindex(1))) /* MT == upvalue? */
p = (nl_Matrix *) lua_touserdata(L, narg);
lua_pop(L, 1); /* remove metatable */
}
return p;
}
static nl_Matrix *checkmatrix (lua_State *L, int narg) {
nl_Matrix *p = tomatrix(L, narg);
if (p == NULL) nl_typeerror(L, narg, MATRIX_LIBNAME);
return p;
}
/* assumes data block is at top of stack if data != NULL or creates a new data
* block otherwise */
#define blocksize(c,n) ((n) * ((c) ? sizeof(nl_Complex) : sizeof(lua_Number)))
static nl_Matrix *pushmatrix (lua_State *L, int iscomplex, int ndims,
int *dim, int stride, int size, nl_Section *section, lua_Number *data) {
lua_Number *mdata = (data != NULL) ? data
: lua_newuserdata(L, blocksize(iscomplex, size));
nl_Matrix *m = lua_newuserdata(L, sizeof(nl_Matrix)
+ (ndims - 1) * sizeof(int)
+ ((section) ? ndims * sizeof(nl_Section) : 0));
int i;
lua_pushvalue(L, lua_upvalueindex(1)); /* metatable */
lua_pushvalue(L, -2);
lua_pushvalue(L, -4); /* data block */
lua_rawset(L, -3); /* mt[matrix] = data */
m->ndims = ndims;
m->stride = stride;
m->size = size;
m->iscomplex = iscomplex;
m->section = NULL;
m->data = mdata;
if (dim) {
for (i = 0; i < ndims; i++)
m->dim[i] = dim[i];
}
if (section) {
m->section = (nl_Section *)((int *)(m + 1) + ndims - 1);
if (section != SECTION_STUB) { /* fill? */
for (i = 0; i < ndims; i++) {
m->section[i].ld = section[i].ld;
m->section[i].step = section[i].step;
}
}
}
lua_setmetatable(L, -2);
if (mdata != DATA_STUB)
lua_replace(L, -2); /* pop data block */
return m;
}
#define pushframe(L,m) \
pushmatrix(L, (m)->iscomplex, (m)->ndims, (m)->dim, 1, (m)->size, \
NULL, NULL)
/* NOTE: setdata operates column-wise */
static void setdatatoscalar (int iscomplex, int size, nl_Complex s,
int stride, int shift, lua_Number *data) {
int i;
if (iscomplex) {
nl_Complex *e = CPX(data) + shift;
for (i = 0; i < size; i++, e += stride)
*e = s;
}
else {
lua_Number *e = data + shift;
lua_Number rs = creal(s);
for (i = 0; i < size; i++, e += stride)
*e = rs;
}
}
static void setdatatovector (nl_Matrix *v,
int stride, int shift, lua_Number *data) {
int i;
if (v->section) {
if (v->iscomplex) {
nl_Complex *e = CPX(data) + shift;
for (i = 0; i < v->size; i++, e += stride)
*e = *(CSHIFT(v, i));
}
else {
lua_Number *e = data + shift;
for (i = 0; i < v->size; i++, e += stride)
*e = *(DSHIFT(v, i));
}
}
else { /* regular copy */
if (v->stride >= 0) {
if (v->iscomplex)
ZCOPY(&v->size, CPX(v->data), &v->stride, CPX(data) + shift, &stride);
else
DCOPY(&v->size, v->data, &v->stride, data + shift, &stride);
}
else {
if (v->iscomplex) {
nl_Complex *e = CPX(data) + shift;
for (i = 0; i < v->size; i++, e += stride)
*e = CPX(v->data)[i * v->stride];
}
else {
lua_Number *e = data + shift;
for (i = 0; i < v->size; i++, e += stride)
*e = v->data[i * v->stride];
}
}
}
}
/* diagonal entries are not set; assumes matrix is 2d section */
static void settriangtoscalar (nl_Complex vc, char what, nl_Matrix *m) {
int i, j, s;
int n = (m->dim[0] < m->dim[1]) ? m->dim[0] : m->dim[1]; /* min */
int ldm = LD(m, 0);
if (m->iscomplex) {
if (what == 'l' || what == 'L') { /* lower? */
for (i = 0; i < n; i++) {
s = i * (ldm + 1) + 1;
for (j = 0; j < m->dim[0] - i - 1; j++)
CPX(m->data)[(s + j) * m->stride] = vc;
}
}
else { /* upper */
for (i = 0; i < n; i++) {
s = i * (ldm + 1) + ldm;
for (j = 0; j < m->dim[1] - i - 1; j++)
CPX(m->data)[(s + j * ldm) * m->stride] = vc;
}
}
}
else {
lua_Number rvc = creal(vc);
if (what == 'l' || what == 'L') { /* lower? */
for (i = 0; i < n; i++) {
s = i * (ldm + 1) + 1;
for (j = 0; j < m->dim[0] - i - 1; j++)
m->data[(s + j) * m->stride] = rvc;
}
}
else { /* upper */
for (i = 0; i < n; i++) {
s = i * (ldm + 1) + ldm;
for (j = 0; j < m->dim[1] - i - 1; j++)
m->data[(s + j * ldm) * m->stride] = rvc;
}
}
}
}
/* destination (`m`) should have no larger dimensions than source (`v`) */
static void settriangtovector (nl_Matrix *v, char what, nl_Matrix *m) {
int i, l, sv, sm;
int n = (m->dim[0] < m->dim[1]) ? m->dim[0] : m->dim[1]; /* min */
int ldv = LD(v, 0), ldm = LD(m, 0);
if (what == 'l' || what == 'L') { /* lower? */
l = m->dim[0] - 1; /* except diagonal */
if (m->iscomplex)
for (i = 0; i < n; i++, l--) {
sv = (i * (ldv + 1) + 1) * v->stride;
sm = (i * (ldm + 1) + 1) * m->stride;
ZCOPY(&l, CPX(v->data) + sv, &v->stride,
CPX(m->data) + sm, &m->stride);
}
else
for (i = 0; i < n; i++, l--) {
sv = (i * (ldv + 1) + 1) * v->stride;
sm = (i * (ldm + 1) + 1) * m->stride;
DCOPY(&l, v->data + sv, &v->stride, m->data + sm, &m->stride);
}
}
else { /* upper */
int stv = ldv * v->stride;
int stm = ldm * m->stride;
l = m->dim[1] - 1; /* except diagonal */
if (m->iscomplex)
for (i = 0; i < n; i++, l--) {
sv = (i * (ldv + 1) + ldv) * v->stride;
sm = (i * (ldm + 1) + ldm) * m->stride;
ZCOPY(&l, CPX(v->data) + sv, &stv, CPX(m->data) + sm, &stm);
}
else
for (i = 0; i < n; i++, l--) {
sv = (i * (ldv + 1) + ldv) * v->stride;
sm = (i * (ldm + 1) + ldm) * m->stride;
DCOPY(&l, v->data + sv, &stv, m->data + sm, &stm);
}
}
}
/* sets submatrix of m given by (n, stride, size, shift) to value v at position
* narg in stack, where v can be a number/complex or a conformable matrix to
* the submatrix */
static void settoarg (lua_State *L, nl_Matrix *m, int n, int stride, int size,
int shift, int narg) {
int i, iscomplex;
nl_Complex vc = nl_tocomplex(L, narg, &iscomplex);
nl_Matrix *s = NULL;
if (m->section) { /* setup subsection frame? */
if (n > 0) {
int ndims = m->ndims - n;
s = lua_newuserdata(L, sizeof(nl_Matrix) + (ndims - 1) * sizeof(int));
s->ndims = ndims;
s->stride = stride;
for (i = 0; i < ndims; i++)
s->dim[i] = m->dim[n + i];
s->section = m->section + n; /* remaining sections */
}
else s = m;
}
/* number/complex? */
if (iscomplex) {
if (m->section) {
if (m->iscomplex) {
nl_Complex *e = CPX(m->data) + shift;
for (i = 0; i < size; i++)
e[nl_msshift(s, i)] = vc;
}
else {
lua_Number *e = m->data + shift;
lua_Number rvc = creal(vc);
for (i = 0; i < size; i++)
e[nl_msshift(s, i)] = rvc;
}
}
else /* regular copy */
setdatatoscalar(m->iscomplex, size, vc, stride, shift, m->data);
}
/* conformable matrix? */
else if (lua_type(L, narg) == LUA_TUSERDATA) {
nl_Matrix *v = checkmatrix(L, narg);
luaL_argcheck(L, size == v->size && m->iscomplex == v->iscomplex, narg,
CONF_ERROR);
if (m->section) {
if (v->section) {
if (m->iscomplex) {
nl_Complex *e = CPX(m->data) + shift;
for (i = 0; i < size; i++)
e[nl_msshift(s, i)] = *(CSHIFT(v, i));
}
else {
lua_Number *e = m->data + shift;
for (i = 0; i < size; i++)
e[nl_msshift(s, i)] = *(DSHIFT(v, i));
}
}
else {
if (m->iscomplex) {
nl_Complex *e = CPX(m->data) + shift;
nl_Complex *f = CPX(v->data);
for (i = 0; i < size; i++, f += v->stride)
e[nl_msshift(s, i)] = *f;
}
else {
lua_Number *e = m->data + shift;
lua_Number *f = v->data;
for (i = 0; i < size; i++, f += v->stride)
e[nl_msshift(s, i)] = *f;
}
}
}
else
setdatatovector(v, stride, shift, m->data);
}
if (m->section) lua_pop(L, 1); /* remove frame */
}
/* {=====================================================================
* API
* ======================================================================} */
NUMLUA_API nl_Matrix *nl_tomatrix (lua_State *L, int narg) {
nl_Matrix *p = NULL;
if (lua_type(L, narg) == LUA_TUSERDATA /* userdata? */
&& lua_getmetatable(L, narg)) { /* has metatable? */
lua_pushlightuserdata(L, MATRIX_MT);
lua_rawget(L, LUA_REGISTRYINDEX);
if (lua_rawequal(L, -1, -2)) /* right MT? */
p = lua_touserdata(L, narg);
lua_pop(L, 2); /* MTs */
}
return p;
}
NUMLUA_API nl_Matrix *nl_checkmatrix (lua_State *L, int narg) {
nl_Matrix *p = nl_tomatrix(L, narg);
if (p == NULL) nl_typeerror(L, narg, MATRIX_LIBNAME);
return p;
}
/* assumes data block is at top of stack, cannot build sections */
NUMLUA_API nl_Matrix *nl_pushmatrix (lua_State *L, int iscomplex, int ndims,
int *dim, int stride, int size, lua_Number *data) {
lua_Number *mdata = (data != NULL) ? data
: lua_newuserdata(L, blocksize(iscomplex, size));
nl_Matrix *m = lua_newuserdata(L, sizeof(nl_Matrix)
+ (ndims - 1) * sizeof(int));
int i;
nl_getmetatable(L, MATRIX_MT);
lua_pushvalue(L, -2);
lua_pushvalue(L, -4); /* data block */
lua_rawset(L, -3); /* mt[matrix] = data */
m->ndims = ndims;
m->stride = stride;
m->size = size;
m->iscomplex = iscomplex;
m->section = NULL;
m->data = mdata;
if (dim) {
for (i = 0; i < ndims; i++)
m->dim[i] = dim[i];
}
lua_setmetatable(L, -2);
if (mdata != DATA_STUB)
lua_replace(L, -2); /* pop data block */
return m;
}
/* {======= Linear Algebra (API) =======} */
/* returns the transpose of `m` in the stack */
static nl_Matrix *nl_transpose (lua_State *L, nl_Matrix *m, int hermitian) {
nl_Matrix *t;
if (m->ndims == 1) {
t = pushframe(L, m);
setdatatovector(m, 1, 0, t->data);
if (hermitian && t->iscomplex) /* take conjugate? */
DSCAL(&t->size, &minusone, t->data + 1, &two);
}
else {
int i, j;
int stride = m->stride * STEP(m, 0);
int ld = m->stride * LD(m, 0) * STEP(m, 1);
t = pushmatrix(L, m->iscomplex, m->ndims, NULL, 1, m->size, NULL, NULL);
t->dim[0] = m->dim[1];
t->dim[1] = m->dim[0];
if (m->iscomplex) {
nl_Complex *e, *f;
for (j = 0; j < m->dim[1]; j++) { /* each column of m */
e = CPX(m->data) + j * ld;
f = CPX(t->data) + j;
for (i = 0; i < m->dim[0]; i++) {
*f = (hermitian) ? conj(*e) : *e;
e += stride;
f += t->dim[0];
}
}
}
else {
lua_Number *e, *f;
for (j = 0; j < m->dim[1]; j++) { /* each column of m */
e = m->data + j * ld;
f = t->data + j;
for (i = 0; i < m->dim[0]; i++) {
*f = *e;
e += stride;
f += t->dim[0];
}
}
}
}
return t;
}
static void nl_trmul (nl_Matrix *x, nl_Matrix *a,
char uplo, int invert, char trans, char side) {
char diag = 'N'; /* not unit triangular */
int n;
int lda = LD(a, 0);
if (x->ndims == 1) { /* vector? */
int incx = x->stride;
n = x->size;
if (x->iscomplex) {
if (invert)
ZTRSV(&uplo, &trans, &diag, &n, CPX(a->data), &lda,
CPX(x->data), &incx, 1, 1, 1);
else
ZTRMV(&uplo, &trans, &diag, &n, CPX(a->data), &lda,
CPX(x->data), &incx, 1, 1, 1);
}
else {
if (invert)
DTRSV(&uplo, &trans, &diag, &n, a->data, &lda,
x->data, &incx, 1, 1, 1);
else
DTRMV(&uplo, &trans, &diag, &n, a->data, &lda,
x->data, &incx, 1, 1, 1);
}
}
else { /* matrix */
int m = x->dim[0];
int ldb = LD(x, 0);
n = x->dim[1];
if (x->iscomplex) {
if (invert)
ZTRSM(&side, &uplo, &trans, &diag, &m, &n, &onec,
CPX(a->data), &lda, CPX(x->data), &ldb, 1, 1, 1, 1);
else
ZTRMM(&side, &uplo, &trans, &diag, &m, &n, &onec,
CPX(a->data), &lda, CPX(x->data), &ldb, 1, 1, 1, 1);
}
else {
if (invert)
DTRSM(&side, &uplo, &trans, &diag, &m, &n, (lua_Number *) &onec,
a->data, &lda, x->data, &ldb, 1, 1, 1, 1);
else
DTRMM(&side, &uplo, &trans, &diag, &m, &n, (lua_Number *) &onec,
a->data, &lda, x->data, &ldb, 1, 1, 1, 1);
}
}
}
static void nl_hemul (nl_Matrix *x, nl_Matrix *a, int inner, char what,
lua_Number alpha) {
int n = x->dim[0]; /* order */
int ldx = LD(x, 0);
int i, l, p;
char uplo = 'L';
if (what == 'u' || what == 'U') uplo = 'U';
if (a->ndims == 1) { /* vector? */
int inca = a->stride;
if (x->iscomplex)
ZHER(&uplo, &n, &alpha, CPX(a->data), &inca, CPX(x->data), &ldx, 1);
else
DSYR(&uplo, &n, &alpha, a->data, &inca, x->data, &ldx, 1);
}
else {
char trans = inner ? 'C' : 'N';
int k = inner ? a->dim[0] : a->dim[1];
int lda = LD(a, 0);
if (x->iscomplex)
ZHERK(&uplo, &trans, &n, &k, &alpha, CPX(a->data), &lda, &oned,
CPX(x->data), &ldx, 1, 1);
else
DSYRK(&uplo, &trans, &n, &k, &alpha, a->data, &lda, &oned,
x->data, &ldx, 1, 1);
}
if (what == 'f' || what == 'F') { /* reflect lower to upper triangle? */
if (x->iscomplex) {
for (i = 1; i < n; i++) {
l = n - i; p = (i - 1) * (ldx + 1);
ZCOPY(&l, CPX(x->data) + p + 1, &one, CPX(x->data) + p + ldx, &ldx);
}
}
else {
for (i = 1; i < n; i++) {
l = n - i; p = (i - 1) * (ldx + 1);
DCOPY(&l, x->data + p + 1, &one, x->data + p + ldx, &ldx);
}
}
}
}
static void nl_mmul (nl_Matrix *c, nl_Matrix *a, nl_Matrix *b,
char transa, char transb, nl_Complex alpha) {
int m, n;
if (b->ndims == 1) { /* b vector? */
if (a->ndims == 1) { /* rank 1 operation? */
int ldc = LD(c, 0);
int inca = a->stride;
int incb = b->stride;
m = c->dim[0]; n = c->dim[1];
if (c->iscomplex)
ZGERC(&m, &n, &alpha, CPX(a->data), &inca, CPX(b->data), &incb,
CPX(c->data), &ldc);
else
DGER(&m, &n, (lua_Number *) &alpha, a->data, &inca, b->data, &incb,
c->data, &ldc);
}
else {
int lda = LD(a, 0);
int incb = b->stride;
int incc = c->stride;
m = a->dim[0]; n = a->dim[1];
if (c->iscomplex)
ZGEMV(&transa, &m, &n, &alpha, CPX(a->data), &lda,
CPX(b->data), &incb, &onec, CPX(c->data), &incc, 1);
else
DGEMV(&transa, &m, &n, (lua_Number *) &alpha, a->data, &lda,
b->data, &incb, &oned, c->data, &incc, 1);
}
}
else { /* a and b are matrices */
int lda = LD(a, 0);
int ldb = LD(b, 0);
int ldc = LD(c, 0);
int k = (transa == 'n' || transa == 'N') ? a->dim[1] : a->dim[0];
m = c->dim[0]; n = c->dim[1];
if (c->iscomplex)
ZGEMM(&transa, &transb, &m, &n, &k, &alpha, CPX(a->data), &lda,
CPX(b->data), &ldb, &onec, CPX(c->data), &ldc, 1, 1);
else
DGEMM(&transa, &transb, &m, &n, &k, (lua_Number *) &alpha,
a->data, &lda, b->data, &ldb, &oned, c->data, &ldc, 1, 1);
}
}
#define SPPOW(x,p) \
do { \
if ((x) != 0) { \
a = fabs(x); \
if (scale < a) { \
s = 1 + s * pow(scale / a, (p)); \
scale = a; \
} \
else \
s += pow(a / scale, (p)); \
} \
} while (0)
#define SNPOW(x,p) \
do { \
if ((x) != 0) { \
a = fabs(x); \
if (scale > a) { \
s = 1 + s * pow(scale / a, (p)); \
scale = a; \
} \
else \
s += pow(a / scale, (p)); \
} \
} while (0)
/* slight generalization of LAPACK's dlange */
/* `argm` is only referenced if what == 'm'/'M', and returns the argmax */
static lua_Number nl_norm (nl_Matrix *m, char what, lua_Number p, int *argm) {
lua_Number a, norm = 0;
int i;
if (what == 0 && p == 0) { /* return which(m, nil, "#")? */
int n = 0;
if (m->section) {
if (m->iscomplex) {
nl_Complex *e = CPX(m->data);
for (i = 0; i < m->size; i++, e = CSHIFT(m, i))
if (cabs(*e) != 0) n++;
}
else {
lua_Number *e = m->data;
for (i = 0; i < m->size; i++, e = DSHIFT(m, i))
if (*e != 0) n++;
}
}
else {
if (m->iscomplex) {
nl_Complex *e = CPX(m->data);
for (i = 0; i < m->size; i++, e += m->stride)
if (cabs(*e) != 0) n++;
}
else {
lua_Number *e = m->data;
for (i = 0; i < m->size; i++, e += m->stride)
if (*e != 0) n++;
}
}
return n;
}
switch (what) {
case 'm': case 'M': { /* max(abs(A(i,j))) */
if (m->section) {
if (m->iscomplex) {
nl_Complex *e = CPX(m->data);
for (i = 0; i < m->size; i++, e = CSHIFT(m, i)) {
a = cabs(*e);
if (norm < a) {
*argm = i + 1;
norm = a;
}
}
}
else {
lua_Number *e = m->data;
for (i = 0; i < m->size; i++, e = DSHIFT(m, i)) {
a = fabs(*e);
if (norm < a) {
*argm = i + 1;
norm = a;
}
}
}
}
else {
if (m->iscomplex) {
i = IZAMAX(&m->size, CPX(m->data), &m->stride) - 1;
*argm = i + 1;
norm = cabs(CPX(m->data)[i * m->stride]);
}
else {
i = IDAMAX(&m->size, m->data, &m->stride) - 1;
*argm = i + 1;
norm = fabs(m->data[i * m->stride]);
}
}
break;
}
case 'o': case 'O': { /* one norm: max column sum */
if (m->ndims == 1) { /* vector? */
if (m->iscomplex)
norm = DZASUM(&m->size, CPX(m->data), &m->stride);
else
norm = DASUM(&m->size, m->data, &m->stride);
}
else {
int stride = m->stride * STEP(m, 0);
int ld = m->stride * LD(m, 0) * STEP(m, 1);
if (m->iscomplex) {
nl_Complex *e = CPX(m->data);
for (i = 0; i < m->dim[1]; i++) { /* for each column */
a = DZASUM(&m->dim[0], e, &stride);
if (norm < a) norm = a;
e += ld;
}
}
else {
lua_Number *e = m->data;
for (i = 0; i < m->dim[1]; i++) { /* for each column */
a = DASUM(&m->dim[0], e, &stride);
if (norm < a) norm = a;
e += ld;
}
}
}
break;
}
case 'i': case 'I': { /* sup norm: max row sum */
if (m->ndims == 1) { /* vector? */
if (m->iscomplex) {
i = IZAMAX(&m->size, CPX(m->data), &m->stride) - 1;
*argm = i + 1;
norm = cabs(CPX(m->data)[i * m->stride]);
}
else {
i = IDAMAX(&m->size, m->data, &m->stride) - 1;
*argm = i + 1;
norm = fabs(m->data[i * m->stride]);
}
}
else {
int stride = m->stride * LD(m, 0) * STEP(m, 1);
int ld = m->stride * STEP(m, 0);
if (m->iscomplex) {
nl_Complex *e = CPX(m->data);
for (i = 0; i < m->dim[1]; i++) { /* for each row */
a = DZASUM(&m->dim[0], e, &stride);
if (norm < a) {
*argm = i + 1;
norm = a;
}
e += ld;
}
}
else {
lua_Number *e = m->data;
for (i = 0; i < m->dim[1]; i++) { /* for each row */
a = DASUM(&m->dim[0], e, &stride);
if (norm < a) {
*argm = i + 1;
norm = a;
}
e += ld;
}
}
}
break;
}
default: { /* p norm */
lua_Number scale = 1, s = 0;
if (m->section) {
if (m->iscomplex) {
nl_Complex *e = CPX(m->data);
if (p > 0) {
for (i = 0; i < m->size; i++, e = CSHIFT(m, i)) {
SPPOW(creal(*e), p);
SPPOW(cimag(*e), p);
}
}
else {
for (i = 0; i < m->size; i++, e = CSHIFT(m, i)) {
SNPOW(creal(*e), p);
SNPOW(cimag(*e), p);
}
}
}
else {
lua_Number *e = m->data;
if (p > 0) {
for (i = 0; i < m->size; i++, e = DSHIFT(m, i))
SPPOW(*e, p);
}
else {
for (i = 0; i < m->size; i++, e = DSHIFT(m, i))
SNPOW(*e, p);
}
}
norm = scale * pow(s, 1 / p);
}
else {
if (p == 2) {
if (m->iscomplex)
norm = DZNRM2(&m->size, CPX(m->data), &m->stride);
else
norm = DNRM2(&m->size, m->data, &m->stride);
}
else {
if (m->iscomplex) {
nl_Complex *e = CPX(m->data);
if (p > 0) {
for (i = 0; i < m->size; i++, e += m->stride) {
SPPOW(creal(*e), p);
SPPOW(cimag(*e), p);
}
}
else {
for (i = 0; i < m->size; i++, e += m->stride) {
SNPOW(creal(*e), p);
SNPOW(cimag(*e), p);
}
}
}
else {
lua_Number *e = m->data;
if (p > 0) {
for (i = 0; i < m->size; i++, e += m->stride)
SPPOW(*e, p);
}
else {
for (i = 0; i < m->size; i++, e += m->stride)
SNPOW(*e, p);
}
}
norm = scale * pow(s, 1 / p);
}
}
}
}
return norm;
}
static int nl_chol (nl_Matrix *a, char uplo) {
int n = a->dim[0], lda = LD(a, 0);
int info;
if (a->iscomplex)
ZPOTRF(&uplo, &n, CPX(a->data), &lda, &info, 1);
else
DPOTRF(&uplo, &n, a->data, &lda, &info, 1);
return info;
}
static int nl_lu (nl_Matrix *a, nl_Buffer *ipiv) {
int m = a->dim[0], n = a->dim[1], lda = LD(a, 0);
int info;
if (a->iscomplex)
ZGETRF(&m, &n, CPX(a->data), &lda, ipiv->data.bint, &info);
else
DGETRF(&m, &n, a->data, &lda, ipiv->data.bint, &info);
return info;
}
/* return the reciprocal of the condition number of `m` using 1-norm
* what = 'd' for diagonal, 'u'/'l' for triangular, 'p' for symmetric posdef
* or 'g' for general (default). For 'p' and 'g', assumes that the argument is
* actually a factorized (Cholesky or LU, resp.) matrix.
* `ipiv` is only referenced when what=='g'/'G', and it should have size at
* least `m->dim[0]` */
static lua_Number nl_rcond (lua_State *L, nl_Matrix *m, char what,
int *ipiv, int *info) {
lua_Number norm, rcond;
nl_Buffer *work = NULL, *xwork = NULL;
int n = m->dim[0], lda = LD(m, 0);
char cnorm = '1';
int rfinfo;
if (what == 'd' || what == 'D') { /* diagonal? */
/* note that 1 / ||M^-1||_1 = min_{i=1,n} { M[i][i] } */
int i, index;
int stride = (lda + 1) * m->stride; /* stride on diagonal */
if (m->iscomplex) {
nl_Complex *e = CPX(m->data);
index = (IZAMAX(&n, CPX(m->data), &stride) - 1) * stride;
rcond = norm = cabs(CPX(m->data)[index]);
for (i = 0; i < n; i++, e += stride) {
lua_Number a = cabs(*e);
if (rcond > a) rcond = a;
if (rcond == 0) break;
}
}
else {
lua_Number *e = m->data;
index = (IDAMAX(&n, m->data, &stride) - 1) * stride;
rcond = norm = fabs(m->data[index]);
for (i = 0; i < n; i++, e += stride) {
lua_Number a = fabs(*e);
if (rcond > a) rcond = a;
if (rcond == 0) break;
}
}
*info = 0;
return rcond / norm;
}
switch (what) {
case 'l': case 'L': case 'u': case 'U': { /* triangular? */
char diag = 'N';
xwork = nl_getbuffer(L, n);
if (what == 'l') what = 'L';
if (what == 'u') what = 'U';
if (m->iscomplex) {
work = nl_getbuffer(L, 4 * n);
ZTRCON(&cnorm, &what, &diag, &m->dim[0], CPX(m->data), &lda,
&rcond, CPX(work->data.bnum), xwork->data.bnum, info, 1, 1, 1);
}
else {
work = nl_getbuffer(L, 3 * n);
DTRCON(&cnorm, &what, &diag, &m->dim[0], m->data, &lda,
&rcond, work->data.bnum, xwork->data.bint, info, 1, 1, 1);
}
rfinfo = 0;
break;
}
case 'p': case 'P': { /* symmetric posdef? */
char uplo = 'L';
if (m->iscomplex) {
ZPOTRF(&uplo, &n, CPX(m->data), &lda, &rfinfo, 1);
if (rfinfo == 0) {
norm = nl_norm(m, 'o', 0, NULL);
work = nl_getbuffer(L, 4 * n);
xwork = nl_getbuffer(L, n);
ZPOCON(&uplo, &n, CPX(m->data), &lda, &norm, &rcond,
CPX(work->data.bnum), xwork->data.bnum, info, 1);
}
else rcond = 0;
}
else {
DPOTRF(&uplo, &n, m->data, &lda, &rfinfo, 1);
if (rfinfo == 0) {
norm = nl_norm(m, 'o', 0, NULL);
work = nl_getbuffer(L, 3 * n);
xwork = nl_getbuffer(L, n);
DPOCON(&uplo, &n, m->data, &lda, &norm, &rcond,
work->data.bnum, xwork->data.bint, info, 1);
} else rcond = 0;
}
break;
}
default: { /* general */
if (m->iscomplex) {
ZGETRF(&n, &n, CPX(m->data), &lda, ipiv, &rfinfo);
if (rfinfo == 0) {
norm = nl_norm(m, 'o', 0, NULL);
work = nl_getbuffer(L, 4 * n);
xwork = nl_getbuffer(L, 2 * n);
ZGECON(&cnorm, &n, CPX(m->data), &lda, &norm, &rcond,
CPX(work->data.bnum), xwork->data.bnum, info, 1);
}
else rcond = 0;
}
else {
DGETRF(&n, &n, m->data, &lda, ipiv, &rfinfo);
if (rfinfo == 0) {
norm = nl_norm(m, 'o', 0, NULL);
work = nl_getbuffer(L, 4 * n);
xwork = nl_getbuffer(L, n);
DGECON(&cnorm, &n, m->data, &lda, &norm, &rcond,
work->data.bnum, xwork->data.bint, info, 1);
}
else rcond = 0;
}
}
}
if (rfinfo == 0) {
nl_freebuffer(work);
nl_freebuffer(xwork);
}
return rcond;
}
/* Hints:
* - when inverting, rcond is computed by default;
* - rcond and inv decompose the input automatically; if that is not desired,
* you have to decompose the input and use 'L'/'U' for rcond and inv;
*/