00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 #define spINSIDE_SPARSE
00063 #include "spConfig.h"
00064 #include "spmatrix.h"
00065 #include "spDefs.h"
00066 #include "spmalloc.h"
00067
00068
00069 static void Translate();
00070 static EnlargeMatrix();
00071 static ExpandTranslationArrays();
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 void
00090 spClear( eMatrix )
00091
00092 char *eMatrix;
00093 {
00094 MatrixPtr Matrix = (MatrixPtr)eMatrix;
00095 register ElementPtr pElement;
00096 register int I;
00097
00098
00099 ASSERT( IS_SPARSE( Matrix ) );
00100
00101
00102 #if spCOMPLEX
00103 if (Matrix->PreviousMatrixWasComplex OR Matrix->Complex)
00104 { for (I = Matrix->Size; I > 0; I--)
00105 { pElement = Matrix->FirstInCol[I];
00106 while (pElement != NULL)
00107 { pElement->Real = 0.0;
00108 pElement->Imag = 0.0;
00109 pElement = pElement->NextInCol;
00110 }
00111 }
00112 }
00113 else
00114 #endif
00115 { for (I = Matrix->Size; I > 0; I--)
00116 { pElement = Matrix->FirstInCol[I];
00117 while (pElement != NULL)
00118 { pElement->Real = 0.0;
00119 pElement = pElement->NextInCol;
00120 }
00121 }
00122 }
00123
00124
00125 Matrix->TrashCan.Real = 0.0;
00126 #if spCOMPLEX
00127 Matrix->TrashCan.Imag = 0.0;
00128 #endif
00129
00130 Matrix->Error = spOKAY;
00131 Matrix->Factored = NO;
00132 Matrix->SingularCol = 0;
00133 Matrix->SingularRow = 0;
00134 Matrix->PreviousMatrixWasComplex = Matrix->Complex;
00135 return;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 RealNumber *
00184 spGetElement( eMatrix, Row, Col )
00185
00186 char *eMatrix;
00187 int Row, Col;
00188 {
00189 MatrixPtr Matrix = (MatrixPtr)eMatrix;
00190 RealNumber *pElement;
00191 ElementPtr spcFindElementInCol();
00192
00193
00194
00195 ASSERT( IS_SPARSE( Matrix ) AND Row >= 0 AND Col >= 0 );
00196
00197 if ((Row == 0) OR (Col == 0))
00198 return &Matrix->TrashCan.Real;
00199
00200 #if NOT TRANSLATE
00201 ASSERT(Matrix->NeedsOrdering);
00202 #endif
00203
00204 #if TRANSLATE
00205 Translate( Matrix, &Row, &Col );
00206 if (Matrix->Error == spNO_MEMORY) return NULL;
00207 #endif
00208
00209 #if NOT TRANSLATE
00210 #if NOT EXPANDABLE
00211 ASSERT(Row <= Matrix->Size AND Col <= Matrix->Size);
00212 #endif
00213
00214 #if EXPANDABLE
00215
00216 if ((Row > Matrix->Size) OR (Col > Matrix->Size))
00217 EnlargeMatrix( Matrix, MAX(Row, Col) );
00218 if (Matrix->Error == spNO_MEMORY) return NULL;
00219 #endif
00220 #endif
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 if ((Row != Col) OR ((pElement = (RealNumber *)Matrix->Diag[Row]) == NULL))
00233 {
00234
00235
00236
00237
00238
00239
00240 pElement = (RealNumber*)spcFindElementInCol( Matrix,
00241 &(Matrix->FirstInCol[Col]),
00242 Row, Col, YES );
00243 }
00244 return pElement;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 ElementPtr
00290 spcFindElementInCol( Matrix, LastAddr, Row, Col, CreateIfMissing )
00291
00292 MatrixPtr Matrix;
00293 register ElementPtr *LastAddr;
00294 register int Row;
00295 int Col;
00296 SPBOOLEAN CreateIfMissing;
00297 {
00298 register ElementPtr pElement;
00299 ElementPtr spcCreateElement();
00300
00301
00302 pElement = *LastAddr;
00303
00304
00305 while (pElement != NULL)
00306 { if (pElement->Row < Row)
00307 {
00308
00309 LastAddr = &(pElement->NextInCol);
00310 pElement = pElement->NextInCol;
00311 }
00312 else if (pElement->Row == Row)
00313 {
00314
00315 return pElement;
00316 }
00317 else break;
00318 }
00319
00320
00321 if (CreateIfMissing)
00322 return spcCreateElement( Matrix, Row, Col, LastAddr, NO );
00323 else return NULL;
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333 #if TRANSLATE
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 static void
00368 Translate( Matrix, Row, Col )
00369
00370 MatrixPtr Matrix;
00371 int *Row, *Col;
00372 {
00373 register int IntRow, IntCol, ExtRow, ExtCol;
00374
00375
00376 ExtRow = *Row;
00377 ExtCol = *Col;
00378
00379
00380 if ((ExtRow > Matrix->AllocatedExtSize) OR
00381 (ExtCol > Matrix->AllocatedExtSize))
00382 {
00383 ExpandTranslationArrays( Matrix, MAX(ExtRow, ExtCol) );
00384 if (Matrix->Error == spNO_MEMORY) return;
00385 }
00386
00387
00388 if ((ExtRow > Matrix->ExtSize) OR (ExtCol > Matrix->ExtSize))
00389 Matrix->ExtSize = MAX(ExtRow, ExtCol);
00390
00391
00392 if ((IntRow = Matrix->ExtToIntRowMap[ExtRow]) == -1)
00393 { Matrix->ExtToIntRowMap[ExtRow] = ++Matrix->CurrentSize;
00394 Matrix->ExtToIntColMap[ExtRow] = Matrix->CurrentSize;
00395 IntRow = Matrix->CurrentSize;
00396
00397 #if NOT EXPANDABLE
00398 ASSERT(IntRow <= Matrix->Size);
00399 #endif
00400
00401 #if EXPANDABLE
00402
00403 if (IntRow > Matrix->Size)
00404 EnlargeMatrix( Matrix, IntRow );
00405 if (Matrix->Error == spNO_MEMORY) return;
00406 #endif
00407
00408 Matrix->IntToExtRowMap[IntRow] = ExtRow;
00409 Matrix->IntToExtColMap[IntRow] = ExtRow;
00410 }
00411
00412
00413 if ((IntCol = Matrix->ExtToIntColMap[ExtCol]) == -1)
00414 { Matrix->ExtToIntRowMap[ExtCol] = ++Matrix->CurrentSize;
00415 Matrix->ExtToIntColMap[ExtCol] = Matrix->CurrentSize;
00416 IntCol = Matrix->CurrentSize;
00417
00418 #if NOT EXPANDABLE
00419 ASSERT(IntCol <= Matrix->Size);
00420 #endif
00421
00422 #if EXPANDABLE
00423
00424 if (IntCol > Matrix->Size)
00425 EnlargeMatrix( Matrix, IntCol );
00426 if (Matrix->Error == spNO_MEMORY) return;
00427 #endif
00428
00429 Matrix->IntToExtRowMap[IntCol] = ExtCol;
00430 Matrix->IntToExtColMap[IntCol] = ExtCol;
00431 }
00432
00433 *Row = IntRow;
00434 *Col = IntCol;
00435 return;
00436 }
00437 #endif
00438
00439
00440
00441
00442
00443
00444 #if QUAD_ELEMENT
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 int
00480 spGetAdmittance( Matrix, Node1, Node2, Template )
00481
00482 char *Matrix;
00483 int Node1, Node2;
00484 struct spTemplate *Template;
00485 {
00486
00487
00488 Template->Element1 = spGetElement(Matrix, Node1, Node1 );
00489 Template->Element2 = spGetElement(Matrix, Node2, Node2 );
00490 Template->Element3Negated = spGetElement( Matrix, Node2, Node1 );
00491 Template->Element4Negated = spGetElement( Matrix, Node1, Node2 );
00492 if
00493 ( (Template->Element1 == NULL)
00494 OR (Template->Element2 == NULL)
00495 OR (Template->Element3Negated == NULL)
00496 OR (Template->Element4Negated == NULL)
00497 ) return spNO_MEMORY;
00498
00499 if (Node1 == 0)
00500 SWAP( RealNumber*, Template->Element1, Template->Element2 );
00501
00502 return spOKAY;
00503 }
00504 #endif
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514 #if QUAD_ELEMENT
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567 int
00568 spGetQuad( Matrix, Row1, Row2, Col1, Col2, Template )
00569
00570 char *Matrix;
00571 int Row1, Row2, Col1, Col2;
00572 struct spTemplate *Template;
00573 {
00574
00575 Template->Element1 = spGetElement( Matrix, Row1, Col1);
00576 Template->Element2 = spGetElement( Matrix, Row2, Col2 );
00577 Template->Element3Negated = spGetElement( Matrix, Row2, Col1 );
00578 Template->Element4Negated = spGetElement( Matrix, Row1, Col2 );
00579 if
00580 ( (Template->Element1 == NULL)
00581 OR (Template->Element2 == NULL)
00582 OR (Template->Element3Negated == NULL)
00583 OR (Template->Element4Negated == NULL)
00584 ) return spNO_MEMORY;
00585
00586 if (Template->Element1 == &((MatrixPtr)Matrix)->TrashCan.Real)
00587 SWAP( RealNumber *, Template->Element1, Template->Element2 );
00588
00589 return spOKAY;
00590 }
00591 #endif
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601 #if QUAD_ELEMENT
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642 int
00643 spGetOnes(Matrix, Pos, Neg, Eqn, Template)
00644
00645 char *Matrix;
00646 int Pos, Neg, Eqn;
00647 struct spTemplate *Template;
00648 {
00649
00650 Template->Element4Negated = spGetElement( Matrix, Neg, Eqn );
00651 Template->Element3Negated = spGetElement( Matrix, Eqn, Neg );
00652 Template->Element2 = spGetElement( Matrix, Pos, Eqn );
00653 Template->Element1 = spGetElement( Matrix, Eqn, Pos );
00654 if
00655 ( (Template->Element1 == NULL)
00656 OR (Template->Element2 == NULL)
00657 OR (Template->Element3Negated == NULL)
00658 OR (Template->Element4Negated == NULL)
00659 ) return spNO_MEMORY;
00660
00661 spADD_REAL_QUAD( *Template, 1.0 );
00662 return spOKAY;
00663 }
00664 #endif
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712 ElementPtr
00713 spcCreateElement( Matrix, Row, Col, LastAddr, Fillin )
00714
00715 MatrixPtr Matrix;
00716 int Row;
00717 register int Col;
00718 register ElementPtr *LastAddr;
00719 SPBOOLEAN Fillin;
00720 {
00721 register ElementPtr pElement, pLastElement;
00722 ElementPtr pCreatedElement, spcGetElement(), spcGetFillin();
00723
00724
00725
00726 if (Matrix->RowsLinked)
00727 {
00728
00729 if (Fillin)
00730 { pElement = spcGetFillin( Matrix );
00731 Matrix->Fillins++;
00732 }
00733 else
00734 { pElement = spcGetElement( Matrix );
00735 Matrix->NeedsOrdering = YES;
00736 }
00737 if (pElement == NULL) return NULL;
00738
00739
00740 if (Row == Col) Matrix->Diag[Row] = pElement;
00741
00742
00743 pCreatedElement = pElement;
00744 pElement->Row = Row;
00745 pElement->Col = Col;
00746 pElement->Real = 0.0;
00747 #if spCOMPLEX
00748 pElement->Imag = 0.0;
00749 #endif
00750 #if INITIALIZE
00751 pElement->pInitInfo = NULL;
00752 #endif
00753
00754
00755 pElement->NextInCol = *LastAddr;
00756 *LastAddr = pElement;
00757
00758
00759 pElement = Matrix->FirstInRow[Row];
00760 pLastElement = NULL;
00761 while (pElement != NULL)
00762 {
00763
00764 if (pElement->Col < Col)
00765 {
00766
00767 pLastElement = pElement;
00768 pElement = pElement->NextInRow;
00769 }
00770 else pElement = NULL;
00771 }
00772
00773
00774 pElement = pCreatedElement;
00775 if (pLastElement == NULL)
00776 {
00777
00778 pElement->NextInRow = Matrix->FirstInRow[Row];
00779 Matrix->FirstInRow[Row] = pElement;
00780 }
00781 else
00782
00783 {
00784 pElement->NextInRow = pLastElement->NextInRow;
00785 pLastElement->NextInRow = pElement;
00786 }
00787
00788 }
00789 else
00790 {
00791
00792
00793
00794
00795
00796
00797 pElement = spcGetElement( Matrix );
00798 if (pElement == NULL) return NULL;
00799
00800
00801 if (Row == Col) Matrix->Diag[Row] = pElement;
00802
00803
00804 pCreatedElement = pElement;
00805 pElement->Row = Row;
00806 #if DEBUG
00807 pElement->Col = Col;
00808 #endif
00809 pElement->Real = 0.0;
00810 #if spCOMPLEX
00811 pElement->Imag = 0.0;
00812 #endif
00813 #if INITIALIZE
00814 pElement->pInitInfo = NULL;
00815 #endif
00816
00817
00818 pElement->NextInCol = *LastAddr;
00819 *LastAddr = pElement;
00820 }
00821
00822 Matrix->Elements++;
00823 return pCreatedElement;
00824 }
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 spcLinkRows( Matrix )
00860
00861 MatrixPtr Matrix;
00862 {
00863 register ElementPtr pElement, *FirstInRowEntry;
00864 register ArrayOfElementPtrs FirstInRowArray;
00865 register int Col;
00866
00867
00868 FirstInRowArray = Matrix->FirstInRow;
00869 for (Col = Matrix->Size; Col >= 1; Col--)
00870 {
00871
00872 pElement = Matrix->FirstInCol[Col];
00873
00874 while (pElement != NULL)
00875 { pElement->Col = Col;
00876 FirstInRowEntry = &FirstInRowArray[pElement->Row];
00877 pElement->NextInRow = *FirstInRowEntry;
00878 *FirstInRowEntry = pElement;
00879 pElement = pElement->NextInCol;
00880 }
00881 }
00882 Matrix->RowsLinked = YES;
00883 return 0;
00884 }
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909 static
00910 EnlargeMatrix( Matrix, NewSize )
00911
00912 MatrixPtr Matrix;
00913 register int NewSize;
00914 {
00915 register int I, OldAllocatedSize = Matrix->AllocatedSize;
00916
00917
00918 Matrix->Size = NewSize;
00919
00920 if (NewSize <= OldAllocatedSize)
00921 return 0;
00922
00923
00924 NewSize = (int) MAX( NewSize, EXPANSION_FACTOR * OldAllocatedSize );
00925 Matrix->AllocatedSize = NewSize;
00926
00927 if (( SPREALLOC(Matrix->IntToExtColMap, int, NewSize+1)) == NULL)
00928 { Matrix->Error = spNO_MEMORY;
00929 return 0;
00930 }
00931 if (( SPREALLOC(Matrix->IntToExtRowMap, int, NewSize+1)) == NULL)
00932 { Matrix->Error = spNO_MEMORY;
00933 return 0;
00934 }
00935 if (( SPREALLOC(Matrix->Diag, ElementPtr, NewSize+1)) == NULL)
00936 { Matrix->Error = spNO_MEMORY;
00937 return 0;
00938 }
00939 if (( SPREALLOC(Matrix->FirstInCol, ElementPtr, NewSize+1)) == NULL)
00940 { Matrix->Error = spNO_MEMORY;
00941 return 0;
00942 }
00943 if (( SPREALLOC(Matrix->FirstInRow, ElementPtr, NewSize+1)) == NULL)
00944 { Matrix->Error = spNO_MEMORY;
00945 return 0;
00946 }
00947
00948
00949
00950
00951
00952 SPFREE( Matrix->MarkowitzRow );
00953 SPFREE( Matrix->MarkowitzCol );
00954 SPFREE( Matrix->MarkowitzProd );
00955 SPFREE( Matrix->DoRealDirect );
00956 SPFREE( Matrix->DoCmplxDirect );
00957 SPFREE( Matrix->Intermediate );
00958 Matrix->InternalVectorsAllocated = NO;
00959
00960
00961 for (I = OldAllocatedSize+1; I <= NewSize; I++)
00962 { Matrix->IntToExtColMap[I] = I;
00963 Matrix->IntToExtRowMap[I] = I;
00964 Matrix->Diag[I] = NULL;
00965 Matrix->FirstInRow[I] = NULL;
00966 Matrix->FirstInCol[I] = NULL;
00967 }
00968
00969 return 0;
00970 }
00971
00972
00973
00974
00975
00976
00977
00978
00979 #if TRANSLATE
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998 static
00999 ExpandTranslationArrays( Matrix, NewSize )
01000
01001 MatrixPtr Matrix;
01002 register int NewSize;
01003 {
01004 register int I, OldAllocatedSize = Matrix->AllocatedExtSize;
01005
01006
01007 Matrix->ExtSize = NewSize;
01008
01009 if (NewSize <= OldAllocatedSize)
01010 return 0;
01011
01012
01013 NewSize = (int) MAX( NewSize, EXPANSION_FACTOR * OldAllocatedSize );
01014 Matrix->AllocatedExtSize = NewSize;
01015
01016 if (( SPREALLOC(Matrix->ExtToIntRowMap, int, NewSize+1)) == NULL)
01017 { Matrix->Error = spNO_MEMORY;
01018 return 0;
01019 }
01020 if (( SPREALLOC(Matrix->ExtToIntColMap, int, NewSize+1)) == NULL)
01021 { Matrix->Error = spNO_MEMORY;
01022 return 0;
01023 }
01024
01025
01026 for (I = OldAllocatedSize+1; I <= NewSize; I++)
01027 { Matrix->ExtToIntRowMap[I] = -1;
01028 Matrix->ExtToIntColMap[I] = -1;
01029 }
01030
01031 return 0;
01032 }
01033 #endif
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043 #if INITIALIZE
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076 void
01077 spInstallInitInfo( pElement, pInitInfo )
01078
01079 RealNumber *pElement;
01080 char *pInitInfo;
01081 {
01082
01083 ASSERT(pElement != NULL);
01084
01085 ((ElementPtr)pElement)->pInitInfo = pInitInfo;
01086 }
01087
01088
01089 char *
01090 spGetInitInfo( pElement )
01091
01092 RealNumber *pElement;
01093 {
01094
01095 ASSERT(pElement != NULL);
01096
01097 return (char *)((ElementPtr)pElement)->pInitInfo;
01098 }
01099
01100
01101 int
01102 spInitialize( eMatrix, pInit )
01103
01104 char *eMatrix;
01105 int (*pInit)();
01106 {
01107 MatrixPtr Matrix = (MatrixPtr)eMatrix;
01108 register ElementPtr pElement;
01109 int J, Error, Col;
01110
01111
01112 ASSERT( IS_SPARSE( Matrix ) );
01113
01114 #if spCOMPLEX
01115
01116 if (Matrix->PreviousMatrixWasComplex AND NOT Matrix->Complex)
01117 { for (J = Matrix->Size; J > 0; J--)
01118 { pElement = Matrix->FirstInCol[J];
01119 while (pElement != NULL)
01120 { pElement->Imag = 0.0;
01121 pElement = pElement->NextInCol;
01122 }
01123 }
01124 }
01125 #endif
01126
01127
01128 for (J = Matrix->Size; J > 0; J--)
01129 { pElement = Matrix->FirstInCol[J];
01130 Col = Matrix->IntToExtColMap[J];
01131 while (pElement != NULL)
01132 { if (pElement->pInitInfo == NULL)
01133 { pElement->Real = 0.0;
01134 # if spCOMPLEX
01135 pElement->Imag = 0.0;
01136 # endif
01137 }
01138 else
01139 { Error = (*pInit)((RealNumber *)pElement, pElement->pInitInfo,
01140 Matrix->IntToExtRowMap[pElement->Row], Col);
01141 if (Error)
01142 { Matrix->Error = spFATAL;
01143 return Error;
01144 }
01145
01146 }
01147 pElement = pElement->NextInCol;
01148 }
01149 }
01150
01151
01152 Matrix->TrashCan.Real = 0.0;
01153 #if spCOMPLEX
01154 Matrix->TrashCan.Imag = 0.0;
01155 #endif
01156
01157 Matrix->Error = spOKAY;
01158 Matrix->Factored = NO;
01159 Matrix->SingularCol = 0;
01160 Matrix->SingularRow = 0;
01161 Matrix->PreviousMatrixWasComplex = Matrix->Complex;
01162 return 0;
01163 }
01164 #endif