-
Notifications
You must be signed in to change notification settings - Fork 3
/
matvec.cxx
1266 lines (1254 loc) · 30.5 KB
/
matvec.cxx
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
//* matrix and vector classes */
// include diagmat*vector and the complex version
#include "matvec.h"
#include <sys/types.h>
#include <sys/times.h>
matrix::matrix(int rows,int columns)
{
/* constructor for matrix */
if (rows*columns<=0) exit(1); // test for small size
Rows=rows;
Columns=columns;
TheMatrix = new double[rows*columns];
b=new double *[rows];
b[0]=TheMatrix;
for (int i=0;i<Rows*Columns;i++) TheMatrix[i]=0.;
if (!TheMatrix) exit(1); // test for excessive memory
}
matrix::matrix(const matrix& m)
{
/* copy constructor for matrix */
Rows=m.Rows;
Columns=m.Columns;
TheMatrix = new double[Rows*Columns];
if (!TheMatrix) exit(1); // test for excessive memory
for (int i=0;i<Rows*Columns;i++) TheMatrix[i]=m.TheMatrix[i];
}
matrix& matrix::operator=(const matrix& arg)
{
#ifdef DEBUG
if (Rows != arg.Rows || Columns != arg.Columns) {
std::cerr<<"matrix sizes not matching"<<std::endl;
exit(1);
}
#endif
Rows=arg.Rows;
Columns=arg.Columns;
for (int i=0;i<Rows*Columns;i++) TheMatrix[i]=arg.TheMatrix[i];
return *this;
}
matrix::~matrix()
{
delete[] TheMatrix;
}
matrix operator+(const matrix& arg1, const matrix& arg2)
{
matrix sum(arg1.Rows,arg2.Columns);
int Rows=arg1.Rows;
int Columns=arg2.Columns;
for (int i=0;i<Rows*Columns;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]+arg2.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
matrix operator-(const matrix& arg1, const matrix& arg2)
{
matrix dif(arg1.Rows,arg2.Columns);
int Rows=arg1.Rows;
int Columns=arg2.Columns;
for (int i=0;i<Rows*Columns;i++)
dif.TheMatrix[i]=arg1.TheMatrix[i]-arg2.TheMatrix[i];
if (!dif.TheMatrix) exit(1);
return dif;
}
const matrix operator*(const matrix& arg1, const matrix& arg2)
{
// matrix product
matrix prod(arg1.Rows,arg2.Columns);
int Rows=arg1.Rows;
int Columns=arg2.Columns;
#ifdef BLAS
char transa = 'N';
char transb = 'N';
int m=arg1.Rows;
int n=arg2.Columns;
int k=arg1.Columns;
double alpha=1.;
double beta=0.;
int lda=arg1.Rows;
int ldb=arg2.Rows;
int ldc=prod.Rows;
FORTRAN(dgemm)(&transa,&transb,&m,&n,&k,&alpha,arg1.TheMatrix,&lda,
arg2.TheMatrix,&ldb,&beta,prod.TheMatrix,&ldc);
return prod;
#else
for (int i=0;i<Rows;i++) {
for (int j=0;j<Columns;j++) {
prod.TheMatrix[i+j*Rows]=0.;
for (int k=0;k<arg1.Columns;k++)
prod.TheMatrix[i+j*Rows]+=arg1.TheMatrix[i+k*Rows]
*arg2.TheMatrix[k+j*arg2.Rows];
}
}
if (!prod.TheMatrix) exit(1);
return prod;
#endif
}
const matrix transpose(const matrix& arg1)
{
int Rows=arg1.Rows;
int Columns=arg1.Columns;
matrix trans(Columns,Rows);
for (int i=0;i<Rows;i++) for (int j=0;j<Columns;j++)
trans.TheMatrix[j+i*Columns]=arg1.TheMatrix[i+j*Rows];
if (!trans.TheMatrix) exit(1);
return trans;
}
const matrix operator*(const double arg1, const matrix& arg2)
{
// multiplication by a scalar
matrix prod(arg2.Rows,arg2.Columns);
int Rows=arg2.Rows;
int Columns=arg2.Columns;
for (int i=0;i<Rows;i++) for (int j=0;j<Columns;j++)
prod.TheMatrix[i+j*Rows]=arg1*arg2.TheMatrix[i+j*Rows];
return prod;
}
const vector operator*(const double arg1, const vector& arg2)
{
// multiplication by a scalar
vector prod(arg2.Sz);
int Sz=arg2.Sz;
for (int i=0;i<Sz;i++) prod.TheVector[i]=arg1*arg2.TheVector[i];
return prod;
}
const vector operator*(const matrix& arg1, const vector& arg2)
{
// matrix vector product
vector prod(arg1.Rows);
int Rows=arg1.Rows;
int Sz=arg2.Sz;
#ifdef BLAS
char trans = 'N';
int m=arg1.Rows;
int n=arg1.Columns;
double alpha=1.;
double beta=0.;
int lda=arg1.Rows;
int incx=1;
int incy=1;
FORTRAN(dgemv)(&trans,&m,&n,&alpha,arg1.TheMatrix,&lda,
arg2.TheVector,&incx,&beta,prod.TheVector,&incy);
return prod;
#endif
for (int i=0;i<Rows;i++) {
prod.TheVector[i]=0.;
for (int k=0;k<arg1.Columns;k++)
prod.TheVector[i]+=arg1.TheMatrix[i+k*Rows]*arg2.TheVector[k];
}
return prod;
}
//vector operator*(const symmatrix& arg1, const vector& arg2)
//{
// matrix vector product
// vector prod(arg2.Sz);
// int Rows=arg1.Rows;
// int Sz=arg2.Sz;
// #ifdef BLAS
// #endif
// for (int i=0;i<Rows;i++) {
// prod.TheVector[i]=0.;
// for (int k=0;k<arg1.Columns;k++)
// prod.TheVector[i]+=arg1.TheMatrix[i+k*Rows]*arg2.TheVector[k];
// }
// return prod;
//}
const matrix operator*(const matrix& arg1, const diagmat& arg2)
{
int Rows=arg1.Rows;
int Columns=arg1.Columns;
matrix prod(Rows,Columns);
int Sz=arg2.Sz;
for (int i=0;i<Rows;i++)
for (int j=0;j<Columns;j++)
prod.TheMatrix[i+j*Rows]=arg1.TheMatrix[i+j*Rows]*arg2.TheMatrix[j];
return prod;
}
const cmatrix operator*(const cmatrix& arg1, const cdiagmat& arg2)
{
int Rows=arg1.Rows;
int Columns=arg1.Columns;
cmatrix prod(Rows,Columns);
int Sz=arg2.Sz;
for (int i=0;i<Rows;i++)
for (int j=0;j<Columns;j++)
prod.TheMatrix[i+j*Rows]=arg1.TheMatrix[i+j*Rows]*arg2.TheMatrix[j];
return prod;
}
const matrix operator*(const diagmat& arg1, const matrix& arg2)
{
int Rows=arg2.Rows;
int Columns=arg2.Columns;
matrix prod(Rows,Columns);
int Sz=arg1.Sz;
for (int i=0;i<Rows;i++)
for (int j=0;j<Columns;j++)
prod.TheMatrix[i+j*Rows]=arg2.TheMatrix[i+j*Rows]*arg1.TheMatrix[i];
return prod;
}
const vector operator*(const diagmat& arg1, const vector& arg2)
{
int Sz=arg1.Sz;
vector prod(Sz);
//for (int i=0;i<Sz;i++)
// prod.TheVector[i]=arg1.TheMatrix[i]*arg2.TheVector[i];
FORTRAN(dmatvecprod)(prod.TheVector,arg1.TheMatrix,arg2.TheVector,&Sz);
return prod;
}
const cvector operator*(const cdiagmat& arg1, const cvector& arg2)
{
int Sz=arg1.Sz;
cvector prod(Sz);
for (int i=0;i<Sz;i++)
prod.TheVector[i]=arg1.TheMatrix[i]*arg2.TheVector[i];
return prod;
}
const cmatrix operator*(const cdiagmat& arg1, const cmatrix& arg2)
{
int Rows=arg2.Rows;
int Columns=arg2.Columns;
cmatrix prod(Rows,Columns);
int Sz=arg1.Sz;
for (int i=0;i<Rows;i++)
for (int j=0;j<Columns;j++)
prod.TheMatrix[i+j*Rows]=arg2.TheMatrix[i+j*Rows]*arg1.TheMatrix[i];
return prod;
}
matrix operator+(const matrix& arg1, const diagmat& arg2)
{
matrix sum=arg1;
int sz=arg2.Sz;
for (int i=0;i<sz;i++)
sum.TheMatrix[i+i*sz]+=arg2.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
cmatrix operator+(const cmatrix& arg1, const cdiagmat& arg2)
{
cmatrix sum=arg1;
int sz=arg2.Sz;
for (int i=0;i<sz;i++)
sum.TheMatrix[i+i*sz]+=arg2.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
matrix operator+(const diagmat& arg1, const matrix& arg2)
{
matrix sum=arg2;
int sz=arg1.Sz;
for (int i=0;i<sz;i++)
sum.TheMatrix[i+i*sz]+=arg1.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
cmatrix operator+(const cdiagmat& arg1, const cmatrix& arg2)
{
cmatrix sum=arg2;
int sz=arg1.Sz;
for (int i=0;i<sz;i++)
sum.TheMatrix[i+i*sz]+=arg1.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
matrix operator-(const matrix& arg1, const diagmat& arg2)
{
matrix sum=arg1;
int sz=arg2.Sz;
for (int i=0;i<sz;i++)
sum.TheMatrix[i+i*sz]-=arg2.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
cmatrix operator-(const cmatrix& arg1, const cdiagmat& arg2)
{
cmatrix sum=arg1;
int sz=arg2.Sz;
for (int i=0;i<sz;i++)
sum.TheMatrix[i+i*sz]-=arg2.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
matrix operator-(const diagmat& arg1, const matrix& arg2)
{
matrix sum=(-1)*arg2;
int sz=arg1.Sz;
for (int i=0;i<sz;i++)
sum.TheMatrix[i+i*sz]+=arg1.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
cmatrix operator-(const cdiagmat& arg1, const cmatrix& arg2)
{
cmatrix sum=complex(1.,0)*arg2;
int sz=arg1.Sz;
for (int i=0;i<sz;i++)
sum.TheMatrix[i+i*sz]+=arg1.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
const diagmat operator*(const diagmat& arg1, const diagmat& arg2)
{
int Sz=arg1.Sz;
diagmat prod(Sz);
for (int i=0;i<Sz;i++)
prod.TheMatrix[i]=arg1.TheMatrix[i]*arg2.TheMatrix[i];
return prod;
}
const cdiagmat operator*(const cdiagmat& arg1, const cdiagmat& arg2)
{
int Sz=arg1.Sz;
cdiagmat prod(Sz);
for (int i=0;i<Sz;i++)
prod.TheMatrix[i]=arg1.TheMatrix[i]*arg2.TheMatrix[i];
return prod;
}
const diagmat operator*(const double arg1, const diagmat& arg2)
{
int Sz=arg2.Sz;
diagmat prod(Sz);
for (int i=0;i<Sz;i++)
prod.TheMatrix[i]=arg1*arg2.TheMatrix[i];
return prod;
}
const cdiagmat operator*(const complex arg1, const cdiagmat& arg2)
{
int Sz=arg2.Sz;
cdiagmat prod(Sz);
for (int i=0;i<Sz;i++)
prod.TheMatrix[i]=arg1*arg2.TheMatrix[i];
return prod;
}
const diagmat operator*(const diagmat& arg1, const double arg2)
{
int Sz=arg1.Sz;
diagmat prod(Sz);
for (int i=0;i<Sz;i++)
prod.TheMatrix[i]=arg1.TheMatrix[i]*arg2;
return prod;
}
const cdiagmat operator*(const cdiagmat& arg1, const complex arg2)
{
int Sz=arg1.Sz;
cdiagmat prod(Sz);
for (int i=0;i<Sz;i++)
prod.TheMatrix[i]=arg1.TheMatrix[i]*arg2;
return prod;
}
diagmat operator+(const diagmat& arg1, const diagmat& arg2)
{
int Sz=arg1.Sz;
diagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]+arg2.TheMatrix[i];
return sum;
}
cdiagmat operator+(const cdiagmat& arg1, const cdiagmat& arg2)
{
int Sz=arg1.Sz;
cdiagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]+arg2.TheMatrix[i];
return sum;
}
diagmat operator+(const double arg1, const diagmat& arg2)
{
int Sz=arg2.Sz;
diagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1+arg2.TheMatrix[i];
return sum;
}
cdiagmat operator+(const complex arg1, const cdiagmat& arg2)
{
int Sz=arg2.Sz;
cdiagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1+arg2.TheMatrix[i];
return sum;
}
diagmat operator+(const diagmat& arg1, const double arg2)
{
int Sz=arg1.Sz;
diagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]+arg2;
return sum;
}
cdiagmat operator+(const cdiagmat& arg1, const complex arg2)
{
int Sz=arg1.Sz;
cdiagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]+arg2;
return sum;
}
diagmat operator-(const diagmat& arg1, const diagmat& arg2)
{
int Sz=arg1.Sz;
diagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]-arg2.TheMatrix[i];
return sum;
}
cdiagmat operator-(const cdiagmat& arg1, const cdiagmat& arg2)
{
int Sz=arg1.Sz;
cdiagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]-arg2.TheMatrix[i];
return sum;
}
diagmat operator-(const double arg1, const diagmat& arg2)
{
int Sz=arg2.Sz;
diagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1-arg2.TheMatrix[i];
return sum;
}
cdiagmat operator-(const complex arg1, const cdiagmat& arg2)
{
int Sz=arg2.Sz;
cdiagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1-arg2.TheMatrix[i];
return sum;
}
diagmat operator-(const diagmat& arg1, const double arg2)
{
int Sz=arg1.Sz;
diagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]-arg2;
return sum;
}
cdiagmat operator-(const cdiagmat& arg1, const complex arg2)
{
int Sz=arg1.Sz;
cdiagmat sum(Sz);
for (int i=0;i<Sz;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]-arg2;
return sum;
}
diagmat inverse(const diagmat& v)
{
int Sz=v.Sz;
diagmat invr(Sz);
for (int i=0;i<Sz;i++)
invr.TheMatrix[i]=1./v.TheMatrix[i];
return invr;
}
matrix inverse(const matrix& a)
{
// for symmetric positive definite matrix
matrix inv=a;
char UPLO='U';
int INFO=0;
int N=a.Rows;
int LDA=N;
FORTRAN(dpotrf)(&UPLO,&N,inv.TheMatrix,&LDA,&INFO);
FORTRAN(dpotri)(&UPLO,&N,inv.TheMatrix,&LDA,&INFO);
if (INFO != 0) {
if (INFO < 0) {std::cerr<<"illegal value for argument "<<INFO;
std::cerr<<"in dpotri (inverse)"<<std::endl;}
if (INFO > 0) std::cerr<<"inverse could not be computed"<<std::endl;
}
// fill the lower triangle
for (int i=0;i<N;i++)
for (int j=0;j<i;j++)
inv(i,j)=inv(j,i);
return inv;
}
double det(const matrix& a)
{
int n=a.Rows;
int IFAIL=0;
matrix A=transpose(a)*a;
/* vector WKSPCE(n);
f03aaf_(A.TheMatrix,&n,&n,&DET,WKSPCE.TheVector,&IFAIL);
std::cerr<<"det to be implemented"<<std::endl;
exit(1); */
vector v=diag(A);
double DET=1.;
for (int i=0;i<n;i++) DET*=v(i);
DET=sqrt(DET);
std::cout<<DET<<std::endl;
return DET;
}
cdiagmat inverse(const cdiagmat& v)
{
int Sz=v.Sz;
cdiagmat invr(Sz);
for (int i=0;i<Sz;i++)
invr.TheMatrix[i]=complex(1.,0.)/v.TheMatrix[i];
return invr;
}
diagmat sqrtdiagmat(const diagmat& v)
{
int Sz=v.Sz;
diagmat sq(Sz);
for (int i=0;i<Sz;i++)
sq.TheMatrix[i]=sqrt(v.TheMatrix[i]);
return sq;
}
cdiagmat sqrtdiagmat(const cdiagmat& v)
{
int Sz=v.Sz;
cdiagmat sq(Sz);
for (int i=0;i<Sz;i++)
sq.TheMatrix[i]=sqrt(v.TheMatrix[i]);
return sq;
}
const double operator*(const vector& arg1, const vector& arg2)
{
int Sz=arg2.Sz;
double dot=0.;
//for (int i=0;i<Sz;i++)
// dot+=arg1.TheVector[i]*arg2.TheVector[i];
FORTRAN(dotprod)(arg1.TheVector,arg2.TheVector,&Sz,&dot);
return dot;
}
vector operator+(const vector& arg1, const vector& arg2)
{
vector sum(arg1.Sz);
int Sz=arg1.Sz;
for (int i=0;i<Sz;i++)
sum.TheVector[i]=arg1.TheVector[i]+arg2.TheVector[i];
if (!sum.TheVector) exit(1);
return sum;
}
vector operator+(const double& arg1, const vector& arg2)
{
vector sum(arg2.Sz);
int Sz=arg2.Sz;
for (int i=0;i<Sz;i++)
sum.TheVector[i]=arg1+arg2.TheVector[i];
if (!sum.TheVector) exit(1);
return sum;
}
cvector operator+(const complex& arg1, const cvector& arg2)
{
cvector sum(arg2.Sz);
int Sz=arg2.Sz;
for (int i=0;i<Sz;i++)
sum.TheVector[i]=arg1+arg2.TheVector[i];
if (!sum.TheVector) exit(1);
return sum;
}
vector operator+(const vector& arg1, const double& arg2)
{
vector sum(arg1.Sz);
int Sz=arg1.Sz;
for (int i=0;i<Sz;i++)
sum.TheVector[i]=arg2+arg1.TheVector[i];
if (!sum.TheVector) exit(1);
return sum;
}
cvector operator+(const cvector& arg1, const complex& arg2)
{
cvector sum(arg1.Sz);
int Sz=arg1.Sz;
for (int i=0;i<Sz;i++)
sum.TheVector[i]=arg2+arg1.TheVector[i];
if (!sum.TheVector) exit(1);
return sum;
}
vector operator-(const vector& arg1, const vector& arg2)
{
vector diff(arg1.Sz);
int Sz=arg1.Sz;
for (int i=0;i<Sz;i++)
diff.TheVector[i]=arg1.TheVector[i]-arg2.TheVector[i];
if (!diff.TheVector) exit(1);
return diff;
}
vector::vector(int sz)
{
if (sz<=0) exit(1); // test for small size
Sz=sz;
TheVector= new double[sz];
if (!TheVector) exit(1); // test for excessive memory
for (int i=0;i<Sz;i++) TheVector[i]=0.;
}
diagmat::diagmat(int sz)
{
if (sz<=0) exit(1); // test for small size
Sz=sz;
TheMatrix= new double[sz];
for (int i=0;i<Sz;i++) TheMatrix[i]=0.;
if (!TheMatrix) exit(1); // test for excessive memory
}
cdiagmat::cdiagmat(int sz)
{
if (sz<=0) exit(1); // test for small size
Sz=sz;
TheMatrix= new complex[sz];
for (int i=0;i<Sz;i++) TheMatrix[i]=complex(0.,0.);
if (!TheMatrix) exit(1); // test for excessive memory
}
vector::vector(const vector& v)
{
/* copy constructor for vector */
Sz=v.Sz;
TheVector= new double[Sz];
if (!TheVector) exit(1); // test for excessive memory
for (int i=0;i<Sz;i++) TheVector[i]=v.TheVector[i];
}
diagmat::diagmat(const diagmat& v)
{
/* copy constructor for diagmat */
Sz=v.Sz;
TheMatrix= new double[Sz];
if (!TheMatrix) exit(1); // test for excessive memory
for (int i=0;i<Sz;i++) TheMatrix[i]=v.TheMatrix[i];
}
cdiagmat::cdiagmat(const cdiagmat& v)
{
/* copy constructor for diagmat */
Sz=v.Sz;
TheMatrix= new complex[Sz];
if (!TheMatrix) exit(1); // test for excessive memory
for (int i=0;i<Sz;i++) TheMatrix[i]=v.TheMatrix[i];
}
vector& vector::operator=(const vector& arg)
{
#ifdef DEBUG
if (Sz != arg.Sz) {
std::cerr<<"vector sizes not matching"<<std::endl;
exit(1);
}
#endif
Sz=arg.Sz;
for (int i=0;i<Sz;i++) TheVector[i]=arg.TheVector[i];
return *this;
}
diagmat& diagmat::operator=(const diagmat& arg)
{
#ifdef DEBUG
if (Sz != arg.Sz) {
std::cerr<<"diagmat sizes not matching"<<std::endl;
exit(1);
}
#endif
Sz=arg.Sz;
for (int i=0;i<Sz;i++) TheMatrix[i]=arg.TheMatrix[i];
return *this;
}
cdiagmat& cdiagmat::operator=(const cdiagmat& arg)
{
#ifdef DEBUG
if (Sz != arg.Sz) {
std::cerr<<"cdiagmat sizes not matching"<<std::endl;
exit(1);
}
#endif
Sz=arg.Sz;
for (int i=0;i<Sz;i++) TheMatrix[i]=arg.TheMatrix[i];
return *this;
}
vector::~vector()
{
delete[] TheVector;
}
diagmat::~diagmat()
{
delete[] TheMatrix;
}
cdiagmat::~cdiagmat()
{
delete[] TheMatrix;
}
vector inverse(const vector& v)
{
int Sz=v.Sz;
vector inv(Sz);
for (int i=0;i<Sz;i++) inv.TheVector[i]=1./v.TheVector[i];
if (!inv.TheVector) exit(1);
return inv;
}
vector sqrtvec(const vector& v)
{
int Sz=v.Sz;
vector sqrtv(Sz);
for (int i=0;i<Sz;i++) sqrtv.TheVector[i]=sqrt(v.TheVector[i]);
if (!sqrtv.TheVector) exit(1);
return sqrtv;
}
vector compdiag(const matrix& PRe,const matrix& PIm)
{
int nsize=PIm.Rows;
vector eval(nsize);
vector WK1(nsize);
vector WK2(nsize);
vector WK3(nsize);
int IFAIL=0;
/*f02awf_(PRe.TheMatrix,&nsize,PIm.TheMatrix,&nsize,&nsize,eval.TheVector,
WK1.TheVector,WK2.TheVector,WK3.TheVector,&IFAIL);*/
std::cerr<<"compdiag to be implemented"<<std::endl;
exit(1);
if (!eval.TheVector) exit(1);
return eval;
}
vector compgene(cmatrix& A,cmatrix& B)
{
int i;
int N=A.Rows;
int IFAIL=0;
int MATV=0;
int* ITER=new int[N];
vector ALFR(N);
vector ALFI(N);
vector BETA(N);
double EPS1=0.;
matrix AR(N,N);
matrix AI(N,N);
matrix BR(N,N);
matrix BI(N,N);
for (i=0;i<N;i++) for (int j=0;j<N;j++) {
AR(i,j)=real(A(i,j));
AI(i,j)=imag(A(i,j));
BR(i,j)=real(B(i,j));
BI(i,j)=imag(B(i,j));
}
matrix VR(N,N);
matrix VI(N,N);
/* f02gjf_(&N,AR.TheMatrix,&N,AI.TheMatrix,&N,BR.TheMatrix,&N,
BI.TheMatrix,&N,&EPS1,ALFR.TheVector,ALFI.TheVector,
BETA.TheVector,&MATV,VR.TheMatrix,&N,
VI.TheMatrix,&N,ITER,&IFAIL); */
std::cerr<<"compgene to be implemented"<<std::endl;
exit(1);
if (IFAIL != 0) std::cerr <<"error in compgene "<<std::endl;
if (!ALFR.TheVector) exit(1);
for (i=0;i<N;i++) ALFR(i)=ALFR(i)/BETA(i);
// sort !
char ORDER='A';
int M1=1;
int M2=N;
/* m01caf_(ALFR.TheVector,&M1,&M2,&ORDER,&IFAIL);*/
std::cerr<<"sort to be implemented"<<std::endl;
exit(1);
if (IFAIL != 0) std::cerr <<"error in m01caf_ "<<std::endl;
return ALFR;
}
vector hermdiag(matrix& PRe,matrix& PIm)
{
int nsize=PIm.Rows;
vector eval(nsize);
vector WK1(nsize);
vector WK2(nsize);
vector WK3(nsize);
matrix TR(nsize,nsize);
matrix TI(nsize,nsize);
int IFAIL=0;
/* f02axf_(PRe.TheMatrix,&nsize,PIm.TheMatrix,&nsize,&nsize,
eval.TheVector,TR.TheMatrix,&nsize,TI.TheMatrix,&nsize,WK1.TheVector,
WK2.TheVector, WK3.TheVector,&IFAIL);*/
std::cerr<<"hermdiag to be implemented"<<std::endl;
exit(1);
PRe=TR;
PIm=TI;
if (IFAIL != 0) std::cerr <<"error in hermdiag "<<std::endl;
if (!eval.TheVector) exit(1);
return eval;
}
cmatrix complexm(const matrix &Re,const matrix &Im)
{
cmatrix cm(Re.Rows,Re.Columns);
for (int i=0;i<Re.Rows;i++) {
for (int j=0;j<Re.Columns;j++) {
cm(i,j)=complex(Re(i,j),Im(i,j));
}
}
return cm;
}
cmatrix complexm(const matrix &Re)
{
cmatrix cm(Re.Rows,Re.Columns);
for (int i=0;i<Re.Rows;i++) {
for (int j=0;j<Re.Columns;j++) {
cm(i,j)=complex(Re(i,j),0.);
}
}
return cm;
}
matrix realm(cmatrix &a)
{
matrix m(a.Rows,a.Columns);
for (int i=0;i<a.Rows;i++) {
for (int j=0;j<a.Columns;j++) {
m(i,j)=real(a(i,j));
}
}
return m;
}
matrix imagm(cmatrix &a)
{
matrix m(a.Rows,a.Columns);
for (int i=0;i<a.Rows;i++) {
for (int j=0;j<a.Columns;j++) {
m(i,j)=imag(a(i,j));
}
}
return m;
}
cdiagmat complexm(diagmat &Re,diagmat &Im)
{
cdiagmat cm(Re.Sz);
for (int i=0;i<Re.Sz;i++) cm(i)=complex(Re(i),Im(i));
return cm;
}
cdiagmat complexm(diagmat &Re)
{
cdiagmat cm(Re.Sz);
for (int i=0;i<Re.Sz;i++) cm(i)=complex(Re(i));
return cm;
}
diagmat realm(cdiagmat &a)
{
diagmat m(a.Sz);
for (int i=0;i<a.Sz;i++) m(i)=real(a(i));
return m;
}
diagmat imagm(cdiagmat &a)
{
diagmat m(a.Sz);
for (int i=0;i<a.Sz;i++) m(i)=imag(a(i));
return m;
}
vector hermdl(cmatrix& a)
{
char UPLO='U';
char JOBZ='V';
int INFO=0;
int N=a.Rows;
int LDA=N;
int LWORK=2*N;
cvector WORK(LWORK);
vector W(N);
vector RWORK(3*N);
FORTRAN(cheev)(&JOBZ,&UPLO,&N,a.TheMatrix,&LDA,W.TheVector,WORK.TheVector,
&LWORK,RWORK.TheVector,&INFO);
if (INFO != 0) std::cerr<<"diagonalization failed"<<std::endl;
return W;
}
// construct identity matrix
vector diaggen(matrix& a)
{
int N=a.Rows;
char JOBVL='V';
char JOBVR='V';
int INFO=0;
int LDA=N;
vector WR(N);
vector WI(N);
int LDVL=N;
int LDVR=N;
matrix VL(LDVL,N);
matrix VR(LDVR,N);
int LWORK=4*N;
vector WORK(LWORK);
vector W(N);
FORTRAN(dgeev)(&JOBVL,&JOBVR,&N,a.TheMatrix,&LDA,WR.TheVector,
WI.TheVector,VL.TheMatrix,&LDVL,VR.TheMatrix,&LDVR,WORK.TheVector,
&LWORK,&INFO);
if (INFO != 0) std::cerr<<"diagonalization failed"<<std::endl;
return WR;
}
vector diag(matrix& a)
// returns eigenvalues in a vector and transforms the matrix argument
// for symmetric matrices
{
int N=a.Rows;
char UPLO='U';
char JOBZ='V';
int INFO=0;
int LDA=N;
int LWORK=3*N;
vector WORK(LWORK);
vector W(N);
FORTRAN(dsyev)(&JOBZ,&UPLO,&N,a.TheMatrix,&LDA,W.TheVector,WORK.TheVector,
&LWORK,&INFO);
if (INFO != 0) std::cerr<<"diagonalization failed"<<std::endl;
return W;
}
cvector diag(cmatrix& a,cmatrix &VL,cmatrix &VR)
// returns eigenvalues in a vector and transforms the matrix argument
// for symmetric matrices
{
int N=a.Rows;
char JOBVL='V';
char JOBVR='V';
int INFO=0;
int LDA=N;
int LDVL=N;
int LDVR=N;
int LWORK=2*N;
cvector WORK(LWORK);
cvector W(N);
vector RWORK(2*N);
FORTRAN(zgeev)(&JOBVL,&JOBVR,&N,a.TheMatrix,&LDA,W.TheVector,VL.TheMatrix,&LDVL,
VR.TheMatrix,&LDVR,WORK.TheVector,&LWORK,
RWORK.TheVector,&INFO);
if (INFO != 0) std::cerr<<"diagonalization failed"<<std::endl;
return W;
}
cmatrix inverse(const cmatrix &A)
{
int N=A.Rows;
int INFO=0;
int LDA=N;
int LDB=N;
int NRHS=N;
int* IPIV=new int[N];
cmatrix B(N,N);
cmatrix AP=A;
for (int i=0;i<N;i++) B(i,i)=complex(1.,0.);
FORTRAN(zgesv)(&N,&NRHS,AP.TheMatrix,&LDA,IPIV,B.TheMatrix,&LDB,&INFO);
return B;
}
// implement plain ql
matrix ql(vector &D,vector &E)
{
int N=D.Sz;
char COMPZ='I';
matrix Z(N,N); for (int i=0;i<N;i++) Z(i,i)=1.;
int INFO=0;
int LDZ=N;
vector WORK(2*N-2);
FORTRAN(dsteqr)(&COMPZ,&N,D.TheVector,E.TheVector,Z.TheMatrix,&LDZ,
WORK.TheVector,&INFO);
return Z;
}
// complex stuff
cmatrix::cmatrix(int rows,int columns)
{
/* constructor for cmatrix */
if (rows*columns<=0) exit(1); // test for small size
Rows=rows;
Columns=columns;
TheMatrix = new complex[rows*columns];
for (int i=0;i<Rows*Columns;i++) TheMatrix[i]=complex(0.,0.);
if (!TheMatrix) exit(1); // test for excessive memory
}
cmatrix::cmatrix(const cmatrix& m)
{
/* copy constructor for cmatrix */
Rows=m.Rows;
Columns=m.Columns;
TheMatrix = new complex[Rows*Columns];
if (!TheMatrix) exit(1); // test for excessive memory
for (int i=0;i<Rows*Columns;i++) TheMatrix[i]=m.TheMatrix[i];
}
cmatrix& cmatrix::operator=(const cmatrix& arg)
{
#ifdef DEBUG
if (Rows != arg.Rows || Columns != arg.Columns) {
std::cerr<<"matrix sizes not matching"<<std::endl;
exit(1);
}
#endif
Rows=arg.Rows;
Columns=arg.Columns;
for (int i=0;i<Rows*Columns;i++) TheMatrix[i]=arg.TheMatrix[i];
return *this;
}
cmatrix::~cmatrix()
{
delete[] TheMatrix;
}
complex& cmatrix::operator() (int rindex,int cindex) const
{
#ifdef DEBUG
if (rindex < 0 || rindex >=Rows || cindex< 0 || cindex >=Columns) {
std::cerr<<"Indices exceed cmatrix size:\n"<<"rindex="<<rindex<<",cindex="<<cindex<<"Actual cmatrix size is: "<<Rows<<" by "<<Columns<<std::endl;
exit(1);
}
#endif
return TheMatrix[cindex*Rows+rindex];
}
cmatrix operator+(const cmatrix& arg1, const cmatrix& arg2)
{
cmatrix sum(arg1.Rows,arg2.Columns);
int Rows=arg1.Rows;
int Columns=arg2.Columns;
for (int i=0;i<Rows*Columns;i++)
sum.TheMatrix[i]=arg1.TheMatrix[i]+arg2.TheMatrix[i];
if (!sum.TheMatrix) exit(1);
return sum;
}
cmatrix operator-(const cmatrix& arg1, const cmatrix& arg2)