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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
|
/*
* MATRIX BUILD MODULE
*
* Author: Advising professor:
* Kenneth S. Kundert Alberto Sangiovanni-Vincentelli
* UC Berkeley
*
* This file contains the routines associated with clearing, loading and
* preprocessing the matrix for the sparse matrix routines.
*
* >>> User accessible functions contained in this file:
* spClear
* spGetElement
* spGetAdmittance
* spGetQuad
* spGetOnes
* spInstallInitInfo
* spGetInitInfo
* spInitialize
*
* >>> Other functions contained in this file:
* spcFindElementInCol
* Translate
* spcCreateElement
* spcLinkRows
* EnlargeMatrix
* ExpandTranslationArrays
*/
/*
* Revision and copyright information.
*
* Copyright (c) 1985,86,87,88
* by Kenneth S. Kundert and the University of California.
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the copyright notices appear in all copies and
* supporting documentation and that the authors and the University of
* California are properly credited. The authors and the University of
* California make no representations as to the suitability of this
* software for any purpose. It is provided `as is', without express
* or implied warranty.
*/
#ifndef lint
static char copyright[] =
"Sparse1.3: Copyright (c) 1985,86,87,88 by Kenneth S. Kundert";
static char RCSid[] =
"@(#)$Header: /usr/local/cvsroot/scilab/routines/sparse/spBuild.c,v 1.2 2004/02/28 16:43:21 cornet Exp $";
#endif
/*
* IMPORTS
*
* >>> Import descriptions:
* spConfig.h
* Macros that customize the sparse matrix routines.
* spmatrix.h
* Macros and declarations to be imported by the user.
* spDefs.h
* Matrix type and macro definitions for the sparse matrix routines.
*/
#define spINSIDE_SPARSE
#include "spConfig.h"
#include "spmatrix.h"
#include "spDefs.h"
static void Translate();
static EnlargeMatrix();
static ExpandTranslationArrays();
/*
* CLEAR MATRIX
*
* Sets every element of the matrix to zero and clears the error flag.
*
* >>> Arguments:
* Matrix <input> (char *)
* Pointer to matrix that is to be cleared.
*
* >>> Local variables:
* pElement (ElementPtr)
* A pointer to the element being cleared.
*/
void
spClear( eMatrix )
char *eMatrix;
{
MatrixPtr Matrix = (MatrixPtr)eMatrix;
register ElementPtr pElement;
register int I;
/* Begin `spClear'. */
ASSERT( IS_SPARSE( Matrix ) );
/* Clear matrix. */
#if spCOMPLEX
if (Matrix->PreviousMatrixWasComplex OR Matrix->Complex)
{ for (I = Matrix->Size; I > 0; I--)
{ pElement = Matrix->FirstInCol[I];
while (pElement != NULL)
{ pElement->Real = 0.0;
pElement->Imag = 0.0;
pElement = pElement->NextInCol;
}
}
}
else
#endif
{ for (I = Matrix->Size; I > 0; I--)
{ pElement = Matrix->FirstInCol[I];
while (pElement != NULL)
{ pElement->Real = 0.0;
pElement = pElement->NextInCol;
}
}
}
/* Empty the trash. */
Matrix->TrashCan.Real = 0.0;
#if spCOMPLEX
Matrix->TrashCan.Imag = 0.0;
#endif
Matrix->Error = spOKAY;
Matrix->Factored = NO;
Matrix->SingularCol = 0;
Matrix->SingularRow = 0;
Matrix->PreviousMatrixWasComplex = Matrix->Complex;
return;
}
/*
* SINGLE ELEMENT ADDITION TO MATRIX BY INDEX
*
* Finds element [Row,Col] and returns a pointer to it. If element is
* not found then it is created and spliced into matrix. This routine
* is only to be used after spCreate() and before spMNA_Preorder(),
* spFactor() or spOrderAndFactor(). Returns a pointer to the
* Real portion of a MatrixElement. This pointer is later used by
* spADD_xxx_ELEMENT to directly access element.
*
* >>> Returns:
* Returns a pointer to the element. This pointer is then used to directly
* access the element during successive builds.
*
* >>> Arguments:
* Matrix <input> (char *)
* Pointer to the matrix that the element is to be added to.
* Row <input> (int)
* Row index for element. Must be in the range of [0..Size] unless
* the options EXPANDABLE or TRANSLATE are used. Elements placed in
* row zero are discarded. In no case may Row be less than zero.
* Col <input> (int)
* Column index for element. Must be in the range of [0..Size] unless
* the options EXPANDABLE or TRANSLATE are used. Elements placed in
* column zero are discarded. In no case may Col be less than zero.
*
* >>> Local variables:
* pElement (RealNumber *)
* Pointer to the element.
*
* >>> Possible errors:
* spNO_MEMORY
* Error is not cleared in this routine.
*/
RealNumber *
spGetElement( eMatrix, Row, Col )
char *eMatrix;
int Row, Col;
{
MatrixPtr Matrix = (MatrixPtr)eMatrix;
RealNumber *pElement;
ElementPtr spcFindElementInCol();
/* Begin `spGetElement'. */
ASSERT( IS_SPARSE( Matrix ) AND Row >= 0 AND Col >= 0 );
if ((Row == 0) OR (Col == 0))
return &Matrix->TrashCan.Real;
#if NOT TRANSLATE
ASSERT(Matrix->NeedsOrdering);
#endif
#if TRANSLATE
Translate( Matrix, &Row, &Col );
if (Matrix->Error == spNO_MEMORY) return NULL;
#endif
#if NOT TRANSLATE
#if NOT EXPANDABLE
ASSERT(Row <= Matrix->Size AND Col <= Matrix->Size);
#endif
#if EXPANDABLE
/* Re-size Matrix if necessary. */
if ((Row > Matrix->Size) OR (Col > Matrix->Size))
EnlargeMatrix( Matrix, MAX(Row, Col) );
if (Matrix->Error == spNO_MEMORY) return NULL;
#endif
#endif
/*
* The condition part of the following if statement tests to see if the
* element resides along the diagonal, if it does then it tests to see
* if the element has been created yet (Diag pointer not NULL). The
* pointer to the element is then assigned to Element after it is cast
* into a pointer to a RealNumber. This casting makes the pointer into
* a pointer to Real. This statement depends on the fact that Real
* is the first record in the MatrixElement structure.
*/
if ((Row != Col) OR ((pElement = (RealNumber *)Matrix->Diag[Row]) == NULL))
{
/*
* Element does not exist or does not reside along diagonal. Search
* column for element. As in the if statement above, the pointer to the
* element which is returned by spcFindElementInCol is cast into a
* pointer to Real, a RealNumber.
*/
pElement = (RealNumber*)spcFindElementInCol( Matrix,
&(Matrix->FirstInCol[Col]),
Row, Col, YES );
}
return pElement;
}
/*
* FIND ELEMENT BY SEARCHING COLUMN
*
* Searches column starting at element specified at PtrAddr and finds element
* in Row. If Element does not exists, it is created. The pointer to the
* element is returned.
*
* >>> Returned:
* A pointer to the desired element:
*
* >>> Arguments:
* Matrix <input> (MatrixPtr)
* Pointer to Matrix.
* LastAddr <input-output> (ElementPtr *)
* Address of pointer that initially points to the element in Col at which
* the search is started. The pointer in this location may be changed if
* a fill-in is required in and adjacent element. For this reason it is
* important that LastAddr be the address of a FirstInCol or a NextInCol
* rather than a temporary variable.
* Row <input> (int)
* Row being searched for.
* Col (int)
* Column being searched.
* CreateIfMissing <input> (BOOLEAN)
* Indicates what to do if element is not found, create one or return a
* NULL pointer.
*
* Local variables:
* pElement (ElementPtr)
* Pointer used to search through matrix.
*/
ElementPtr
spcFindElementInCol( Matrix, LastAddr, Row, Col, CreateIfMissing )
MatrixPtr Matrix;
register ElementPtr *LastAddr;
register int Row;
int Col;
BOOLEAN CreateIfMissing;
{
register ElementPtr pElement;
ElementPtr spcCreateElement();
/* Begin `spcFindElementInCol'. */
pElement = *LastAddr;
/* Search for element. */
while (pElement != NULL)
{ if (pElement->Row < Row)
{
/* Have not reached element yet. */
LastAddr = &(pElement->NextInCol);
pElement = pElement->NextInCol;
}
else if (pElement->Row == Row)
{
/* Reached element. */
return pElement;
}
else break; /* while loop */
}
/* Element does not exist and must be created. */
if (CreateIfMissing)
return spcCreateElement( Matrix, Row, Col, LastAddr, NO );
else return NULL;
}
#if TRANSLATE
/*
* TRANSLATE EXTERNAL INDICES TO INTERNAL
*
* Convert internal row and column numbers to internal row and column numbers.
* Also updates Ext/Int maps.
*
*
* >>> Arguments:
* Matrix <input> (MatrixPtr)
* Pointer to the matrix.
* Row <input/output> (int *)
* Upon entry Row is either a external row number of an external node
* number. Upon entry, the internal equivalent is supplied.
* Col <input/output> (int *)
* Upon entry Column is either a external column number of an external node
* number. Upon entry, the internal equivalent is supplied.
*
* >>> Local variables:
* ExtCol (int)
* Temporary variable used to hold the external column or node number
* during the external to internal column number translation.
* ExtRow (int)
* Temporary variable used to hold the external row or node number during
* the external to internal row number translation.
* IntCol (int)
* Temporary variable used to hold the internal column or node number
* during the external to internal column number translation.
* IntRow (int)
* Temporary variable used to hold the internal row or node number during
* the external to internal row number translation.
*/
static void
Translate( Matrix, Row, Col )
MatrixPtr Matrix;
int *Row, *Col;
{
register int IntRow, IntCol, ExtRow, ExtCol;
/* Begin `Translate'. */
ExtRow = *Row;
ExtCol = *Col;
/* Expand translation arrays if necessary. */
if ((ExtRow > Matrix->AllocatedExtSize) OR
(ExtCol > Matrix->AllocatedExtSize))
{
ExpandTranslationArrays( Matrix, MAX(ExtRow, ExtCol) );
if (Matrix->Error == spNO_MEMORY) return;
}
/* Set ExtSize if necessary. */
if ((ExtRow > Matrix->ExtSize) OR (ExtCol > Matrix->ExtSize))
Matrix->ExtSize = MAX(ExtRow, ExtCol);
/* Translate external row or node number to internal row or node number. */
if ((IntRow = Matrix->ExtToIntRowMap[ExtRow]) == -1)
{ Matrix->ExtToIntRowMap[ExtRow] = ++Matrix->CurrentSize;
Matrix->ExtToIntColMap[ExtRow] = Matrix->CurrentSize;
IntRow = Matrix->CurrentSize;
#if NOT EXPANDABLE
ASSERT(IntRow <= Matrix->Size);
#endif
#if EXPANDABLE
/* Re-size Matrix if necessary. */
if (IntRow > Matrix->Size)
EnlargeMatrix( Matrix, IntRow );
if (Matrix->Error == spNO_MEMORY) return;
#endif
Matrix->IntToExtRowMap[IntRow] = ExtRow;
Matrix->IntToExtColMap[IntRow] = ExtRow;
}
/* Translate external column or node number to internal column or node number.*/
if ((IntCol = Matrix->ExtToIntColMap[ExtCol]) == -1)
{ Matrix->ExtToIntRowMap[ExtCol] = ++Matrix->CurrentSize;
Matrix->ExtToIntColMap[ExtCol] = Matrix->CurrentSize;
IntCol = Matrix->CurrentSize;
#if NOT EXPANDABLE
ASSERT(IntCol <= Matrix->Size);
#endif
#if EXPANDABLE
/* Re-size Matrix if necessary. */
if (IntCol > Matrix->Size)
EnlargeMatrix( Matrix, IntCol );
if (Matrix->Error == spNO_MEMORY) return;
#endif
Matrix->IntToExtRowMap[IntCol] = ExtCol;
Matrix->IntToExtColMap[IntCol] = ExtCol;
}
*Row = IntRow;
*Col = IntCol;
return;
}
#endif
#if QUAD_ELEMENT
/*
* ADDITION OF ADMITTANCE TO MATRIX BY INDEX
*
* Performs same function as spGetElement except rather than one
* element, all four Matrix elements for a floating component are
* added. This routine also works if component is grounded. Positive
* elements are placed at [Node1,Node2] and [Node2,Node1]. This
* routine is only to be used after spCreate() and before
* spMNA_Preorder(), spFactor() or spOrderAndFactor().
*
* >>> Returns:
* Error code.
*
* >>> Arguments:
* Matrix <input> (char *)
* Pointer to the matrix that component is to be entered in.
* Node1 <input> (int)
* Row and column indices for elements. Must be in the range of [0..Size]
* unless the options EXPANDABLE or TRANSLATE are used. Node zero is the
* ground node. In no case may Node1 be less than zero.
* Node2 <input> (int)
* Row and column indices for elements. Must be in the range of [0..Size]
* unless the options EXPANDABLE or TRANSLATE are used. Node zero is the
* ground node. In no case may Node2 be less than zero.
* Template <output> (struct spTemplate *)
* Collection of pointers to four elements that are later used to directly
* address elements. User must supply the template, this routine will
* fill it.
*
* Possible errors:
* spNO_MEMORY
* Error is not cleared in this routine.
*/
int
spGetAdmittance( Matrix, Node1, Node2, Template )
char *Matrix;
int Node1, Node2;
struct spTemplate *Template;
{
/* Begin `spGetAdmittance'. */
Template->Element1 = spGetElement(Matrix, Node1, Node1 );
Template->Element2 = spGetElement(Matrix, Node2, Node2 );
Template->Element3Negated = spGetElement( Matrix, Node2, Node1 );
Template->Element4Negated = spGetElement( Matrix, Node1, Node2 );
if
( (Template->Element1 == NULL)
OR (Template->Element2 == NULL)
OR (Template->Element3Negated == NULL)
OR (Template->Element4Negated == NULL)
) return spNO_MEMORY;
if (Node1 == 0)
SWAP( RealNumber*, Template->Element1, Template->Element2 );
return spOKAY;
}
#endif /* QUAD_ELEMENT */
#if QUAD_ELEMENT
/*
* ADDITION OF FOUR ELEMENTS TO MATRIX BY INDEX
*
* Similar to spGetAdmittance, except that spGetAdmittance only
* handles 2-terminal components, whereas spGetQuad handles simple
* 4-terminals as well. These 4-terminals are simply generalized
* 2-terminals with the option of having the sense terminals different
* from the source and sink terminals. spGetQuad adds four
* elements to the matrix. Positive elements occur at Row1,Col1
* Row2,Col2 while negative elements occur at Row1,Col2 and Row2,Col1.
* The routine works fine if any of the rows and columns are zero.
* This routine is only to be used after spCreate() and before
* spMNA_Preorder(), spFactor() or spOrderAndFactor()
* unless TRANSLATE is set true.
*
* >>> Returns:
* Error code.
*
* >>> Arguments:
* Matrix <input> (char *)
* Pointer to the matrix that component is to be entered in.
* Row1 <input> (int)
* First row index for elements. Must be in the range of [0..Size]
* unless the options EXPANDABLE or TRANSLATE are used. Zero is the
* ground row. In no case may Row1 be less than zero.
* Row2 <input> (int)
* Second row index for elements. Must be in the range of [0..Size]
* unless the options EXPANDABLE or TRANSLATE are used. Zero is the
* ground row. In no case may Row2 be less than zero.
* Col1 <input> (int)
* First column index for elements. Must be in the range of [0..Size]
* unless the options EXPANDABLE or TRANSLATE are used. Zero is the
* ground column. In no case may Col1 be less than zero.
* Col2 <input> (int)
* Second column index for elements. Must be in the range of [0..Size]
* unless the options EXPANDABLE or TRANSLATE are used. Zero is the
* ground column. In no case may Col2 be less than zero.
* Template <output> (struct spTemplate *)
* Collection of pointers to four elements that are later used to directly
* address elements. User must supply the template, this routine will
* fill it.
* Real <input> (RealNumber)
* Real data to be added to elements.
* Imag <input> (RealNumber)
* Imag data to be added to elements. If matrix is real, this argument
* may be deleted.
*
* Possible errors:
* spNO_MEMORY
* Error is not cleared in this routine.
*/
int
spGetQuad( Matrix, Row1, Row2, Col1, Col2, Template )
char *Matrix;
int Row1, Row2, Col1, Col2;
struct spTemplate *Template;
{
/* Begin `spGetQuad'. */
Template->Element1 = spGetElement( Matrix, Row1, Col1);
Template->Element2 = spGetElement( Matrix, Row2, Col2 );
Template->Element3Negated = spGetElement( Matrix, Row2, Col1 );
Template->Element4Negated = spGetElement( Matrix, Row1, Col2 );
if
( (Template->Element1 == NULL)
OR (Template->Element2 == NULL)
OR (Template->Element3Negated == NULL)
OR (Template->Element4Negated == NULL)
) return spNO_MEMORY;
if (Template->Element1 == &((MatrixPtr)Matrix)->TrashCan.Real)
SWAP( RealNumber *, Template->Element1, Template->Element2 );
return spOKAY;
}
#endif /* QUAD_ELEMENT */
#if QUAD_ELEMENT
/*
* ADDITION OF FOUR STRUCTURAL ONES TO MATRIX BY INDEX
*
* Performs similar function to spGetQuad() except this routine is
* meant for components that do not have an admittance representation.
*
* The following stamp is used:
* Pos Neg Eqn
* Pos [ . . 1 ]
* Neg [ . . -1 ]
* Eqn [ 1 -1 . ]
*
* >>> Returns:
* Error code.
*
* >>> Arguments:
* Matrix <input> (char *)
* Pointer to the matrix that component is to be entered in.
* Pos <input> (int)
* See stamp above. Must be in the range of [0..Size]
* unless the options EXPANDABLE or TRANSLATE are used. Zero is the
* ground row. In no case may Pos be less than zero.
* Neg <input> (int)
* See stamp above. Must be in the range of [0..Size]
* unless the options EXPANDABLE or TRANSLATE are used. Zero is the
* ground row. In no case may Neg be less than zero.
* Eqn <input> (int)
* See stamp above. Must be in the range of [0..Size]
* unless the options EXPANDABLE or TRANSLATE are used. Zero is the
* ground row. In no case may Eqn be less than zero.
* Template <output> (struct spTemplate *)
* Collection of pointers to four elements that are later used to directly
* address elements. User must supply the template, this routine will
* fill it.
*
* Possible errors:
* spNO_MEMORY
* Error is not cleared in this routine.
*/
int
spGetOnes(Matrix, Pos, Neg, Eqn, Template)
char *Matrix;
int Pos, Neg, Eqn;
struct spTemplate *Template;
{
/* Begin `spGetOnes'. */
Template->Element4Negated = spGetElement( Matrix, Neg, Eqn );
Template->Element3Negated = spGetElement( Matrix, Eqn, Neg );
Template->Element2 = spGetElement( Matrix, Pos, Eqn );
Template->Element1 = spGetElement( Matrix, Eqn, Pos );
if
( (Template->Element1 == NULL)
OR (Template->Element2 == NULL)
OR (Template->Element3Negated == NULL)
OR (Template->Element4Negated == NULL)
) return spNO_MEMORY;
spADD_REAL_QUAD( *Template, 1.0 );
return spOKAY;
}
#endif /* QUAD_ELEMENT */
/*
*
* CREATE AND SPLICE ELEMENT INTO MATRIX
*
* This routine is used to create new matrix elements and splice them into the
* matrix.
*
* >>> Returned:
* A pointer to the element that was created is returned.
*
* >>> Arguments:
* Matrix <input> (MatrixPtr)
* Pointer to matrix.
* Row <input> (int)
* Row index for element.
* Col <input> (int)
* Column index for element.
* LastAddr <input-output> (ElementPtr *)
* This contains the address of the pointer to the element just above the
* one being created. It is used to speed the search and it is updated with
* address of the created element.
* Fillin <input> (BOOLEAN)
* Flag that indicates if created element is to be a fill-in.
*
* >>> Local variables:
* pElement (ElementPtr)
* Pointer to an element in the matrix. It is used to refer to the newly
* created element and to restring the pointers of the element's row and
* column.
* pLastElement (ElementPtr)
* Pointer to the element in the matrix that was just previously pointed
* to by pElement. It is used to restring the pointers of the element's
* row and column.
* pCreatedElement (ElementPtr)
* Pointer to the desired element, the one that was just created.
*
* >>> Possible errors:
* spNO_MEMORY
*/
ElementPtr
spcCreateElement( Matrix, Row, Col, LastAddr, Fillin )
MatrixPtr Matrix;
int Row;
register int Col;
register ElementPtr *LastAddr;
BOOLEAN Fillin;
{
register ElementPtr pElement, pLastElement;
ElementPtr pCreatedElement, spcGetElement(), spcGetFillin();
/* Begin `spcCreateElement'. */
if (Matrix->RowsLinked)
{
/* Row pointers cannot be ignored. */
if (Fillin)
{ pElement = spcGetFillin( Matrix );
Matrix->Fillins++;
}
else
{ pElement = spcGetElement( Matrix );
Matrix->NeedsOrdering = YES;
}
if (pElement == NULL) return NULL;
/* If element is on diagonal, store pointer in Diag. */
if (Row == Col) Matrix->Diag[Row] = pElement;
/* Initialize Element. */
pCreatedElement = pElement;
pElement->Row = Row;
pElement->Col = Col;
pElement->Real = 0.0;
#if spCOMPLEX
pElement->Imag = 0.0;
#endif
#if INITIALIZE
pElement->pInitInfo = NULL;
#endif
/* Splice element into column. */
pElement->NextInCol = *LastAddr;
*LastAddr = pElement;
/* Search row for proper element position. */
pElement = Matrix->FirstInRow[Row];
pLastElement = NULL;
while (pElement != NULL)
{
/* Search for element row position. */
if (pElement->Col < Col)
{
/* Have not reached desired element. */
pLastElement = pElement;
pElement = pElement->NextInRow;
}
else pElement = NULL;
}
/* Splice element into row. */
pElement = pCreatedElement;
if (pLastElement == NULL)
{
/* Element is first in row. */
pElement->NextInRow = Matrix->FirstInRow[Row];
Matrix->FirstInRow[Row] = pElement;
}
else
/* Element is not first in row. */
{
pElement->NextInRow = pLastElement->NextInRow;
pLastElement->NextInRow = pElement;
}
}
else
{
/*
* Matrix has not been factored yet. Thus get element rather than fill-in.
* Also, row pointers can be ignored.
*/
/* Allocate memory for Element. */
pElement = spcGetElement( Matrix );
if (pElement == NULL) return NULL;
/* If element is on diagonal, store pointer in Diag. */
if (Row == Col) Matrix->Diag[Row] = pElement;
/* Initialize Element. */
pCreatedElement = pElement;
pElement->Row = Row;
#if DEBUG
pElement->Col = Col;
#endif
pElement->Real = 0.0;
#if spCOMPLEX
pElement->Imag = 0.0;
#endif
#if INITIALIZE
pElement->pInitInfo = NULL;
#endif
/* Splice element into column. */
pElement->NextInCol = *LastAddr;
*LastAddr = pElement;
}
Matrix->Elements++;
return pCreatedElement;
}
/*
*
* LINK ROWS
*
* This routine is used to generate the row links. The spGetElement()
* routines do not create row links, which are needed by the spFactor()
* routines.
*
* >>> Arguments:
* Matrix <input> (MatrixPtr)
* Pointer to the matrix.
*
* >>> Local variables:
* pElement (ElementPtr)
* Pointer to an element in the matrix.
* FirstInRowEntry (ElementPtr *)
* A pointer into the FirstInRow array. Points to the FirstInRow entry
* currently being operated upon.
* FirstInRowArray (ArrayOfElementPtrs)
* A pointer to the FirstInRow array. Same as Matrix->FirstInRow but
* resides in a register and requires less indirection so is faster to
* use.
* Col (int)
* Column currently being operated upon.
*/
spcLinkRows( Matrix )
MatrixPtr Matrix;
{
register ElementPtr pElement, *FirstInRowEntry;
register ArrayOfElementPtrs FirstInRowArray;
register int Col;
/* Begin `spcLinkRows'. */
FirstInRowArray = Matrix->FirstInRow;
for (Col = Matrix->Size; Col >= 1; Col--)
{
/* Generate row links for the elements in the Col'th column. */
pElement = Matrix->FirstInCol[Col];
while (pElement != NULL)
{ pElement->Col = Col;
FirstInRowEntry = &FirstInRowArray[pElement->Row];
pElement->NextInRow = *FirstInRowEntry;
*FirstInRowEntry = pElement;
pElement = pElement->NextInCol;
}
}
Matrix->RowsLinked = YES;
return 0;
}
/*
* ENLARGE MATRIX
*
* Increases the size of the matrix.
*
* >>> Arguments:
* Matrix <input> (MatrixPtr)
* Pointer to the matrix.
* NewSize <input> (int)
* The new size of the matrix.
*
* >>> Local variables:
* OldAllocatedSize (int)
* The allocated size of the matrix before it is expanded.
*/
static
EnlargeMatrix( Matrix, NewSize )
MatrixPtr Matrix;
register int NewSize;
{
register int I, OldAllocatedSize = Matrix->AllocatedSize;
/* Begin `EnlargeMatrix'. */
Matrix->Size = NewSize;
if (NewSize <= OldAllocatedSize)
return 0;
/* Expand the matrix frame. */
NewSize = (int) MAX( NewSize, EXPANSION_FACTOR * OldAllocatedSize );
Matrix->AllocatedSize = NewSize;
if (( REALLOC(Matrix->IntToExtColMap, int, NewSize+1)) == NULL)
{ Matrix->Error = spNO_MEMORY;
return 0;
}
if (( REALLOC(Matrix->IntToExtRowMap, int, NewSize+1)) == NULL)
{ Matrix->Error = spNO_MEMORY;
return 0;
}
if (( REALLOC(Matrix->Diag, ElementPtr, NewSize+1)) == NULL)
{ Matrix->Error = spNO_MEMORY;
return 0;
}
if (( REALLOC(Matrix->FirstInCol, ElementPtr, NewSize+1)) == NULL)
{ Matrix->Error = spNO_MEMORY;
return 0;
}
if (( REALLOC(Matrix->FirstInRow, ElementPtr, NewSize+1)) == NULL)
{ Matrix->Error = spNO_MEMORY;
return 0;
}
/*
* Destroy the Markowitz and Intermediate vectors, they will be recreated
* in spOrderAndFactor().
*/
FREE( Matrix->MarkowitzRow );
FREE( Matrix->MarkowitzCol );
FREE( Matrix->MarkowitzProd );
FREE( Matrix->DoRealDirect );
FREE( Matrix->DoCmplxDirect );
FREE( Matrix->Intermediate );
Matrix->InternalVectorsAllocated = NO;
/* Initialize the new portion of the vectors. */
for (I = OldAllocatedSize+1; I <= NewSize; I++)
{ Matrix->IntToExtColMap[I] = I;
Matrix->IntToExtRowMap[I] = I;
Matrix->Diag[I] = NULL;
Matrix->FirstInRow[I] = NULL;
Matrix->FirstInCol[I] = NULL;
}
return 0;
}
#if TRANSLATE
/*
* EXPAND TRANSLATION ARRAYS
*
* Increases the size arrays that are used to translate external to internal
* row and column numbers.
*
* >>> Arguments:
* Matrix <input> (MatrixPtr)
* Pointer to the matrix.
* NewSize <input> (int)
* The new size of the translation arrays.
*
* >>> Local variables:
* OldAllocatedSize (int)
* The allocated size of the translation arrays before being expanded.
*/
static
ExpandTranslationArrays( Matrix, NewSize )
MatrixPtr Matrix;
register int NewSize;
{
register int I, OldAllocatedSize = Matrix->AllocatedExtSize;
/* Begin `ExpandTranslationArrays'. */
Matrix->ExtSize = NewSize;
if (NewSize <= OldAllocatedSize)
return 0;
/* Expand the translation arrays ExtToIntRowMap and ExtToIntColMap. */
NewSize = (int) MAX( NewSize, EXPANSION_FACTOR * OldAllocatedSize );
Matrix->AllocatedExtSize = NewSize;
if (( REALLOC(Matrix->ExtToIntRowMap, int, NewSize+1)) == NULL)
{ Matrix->Error = spNO_MEMORY;
return 0;
}
if (( REALLOC(Matrix->ExtToIntColMap, int, NewSize+1)) == NULL)
{ Matrix->Error = spNO_MEMORY;
return 0;
}
/* Initialize the new portion of the vectors. */
for (I = OldAllocatedSize+1; I <= NewSize; I++)
{ Matrix->ExtToIntRowMap[I] = -1;
Matrix->ExtToIntColMap[I] = -1;
}
return 0;
}
#endif
#if INITIALIZE
/*
* INITIALIZE MATRIX
*
* With the INITIALIZE compiler option (see spConfig.h) set true,
* Sparse allows the user to keep initialization information with each
* structurally nonzero matrix element. Each element has a pointer
* that is set and used by the user. The user can set this pointer
* using spInstallInitInfo and may be read using spGetInitInfo. Both
* may be used only after the element exists. The function
* spInitialize() is a user customizable way to initialize the matrix.
* Passed to this routine is a function pointer. spInitialize() sweeps
* through every element in the matrix and checks the pInitInfo
* pointer (the user supplied pointer). If the pInitInfo is NULL,
* which is true unless the user changes it (almost always true for
* fill-ins), then the element is zeroed. Otherwise, the function
* pointer is called and passed the pInitInfo pointer as well as the
* element pointer and the external row and column numbers. If the
* user sets the value of each element, then spInitialize() replaces
* spClear().
*
* The user function is expected to return a nonzero integer if there
* is a fatal error and zero otherwise. Upon encountering a nonzero
* return code, spInitialize() terminates and returns the error code.
*
* >>> Arguments:
* Matrix <input> (char *)
* Pointer to matrix.
*
* >>> Possible Errors:
* Returns nonzero if error, zero otherwise.
*/
void
spInstallInitInfo( pElement, pInitInfo )
RealNumber *pElement;
char *pInitInfo;
{
/* Begin `spInstallInitInfo'. */
ASSERT(pElement != NULL);
((ElementPtr)pElement)->pInitInfo = pInitInfo;
}
char *
spGetInitInfo( pElement )
RealNumber *pElement;
{
/* Begin `spGetInitInfo'. */
ASSERT(pElement != NULL);
return (char *)((ElementPtr)pElement)->pInitInfo;
}
int
spInitialize( eMatrix, pInit )
char *eMatrix;
int (*pInit)();
{
MatrixPtr Matrix = (MatrixPtr)eMatrix;
register ElementPtr pElement;
int J, Error, Col;
/* Begin `spInitialize'. */
ASSERT( IS_SPARSE( Matrix ) );
#if spCOMPLEX
/* Clear imaginary part of matrix if matrix is real but was complex. */
if (Matrix->PreviousMatrixWasComplex AND NOT Matrix->Complex)
{ for (J = Matrix->Size; J > 0; J--)
{ pElement = Matrix->FirstInCol[J];
while (pElement != NULL)
{ pElement->Imag = 0.0;
pElement = pElement->NextInCol;
}
}
}
#endif /* spCOMPLEX */
/* Initialize the matrix. */
for (J = Matrix->Size; J > 0; J--)
{ pElement = Matrix->FirstInCol[J];
Col = Matrix->IntToExtColMap[J];
while (pElement != NULL)
{ if (pElement->pInitInfo == NULL)
{ pElement->Real = 0.0;
# if spCOMPLEX
pElement->Imag = 0.0;
# endif
}
else
{ Error = (*pInit)((RealNumber *)pElement, pElement->pInitInfo,
Matrix->IntToExtRowMap[pElement->Row], Col);
if (Error)
{ Matrix->Error = spFATAL;
return Error;
}
}
pElement = pElement->NextInCol;
}
}
/* Empty the trash. */
Matrix->TrashCan.Real = 0.0;
#if spCOMPLEX
Matrix->TrashCan.Imag = 0.0;
#endif
Matrix->Error = spOKAY;
Matrix->Factored = NO;
Matrix->SingularCol = 0;
Matrix->SingularRow = 0;
Matrix->PreviousMatrixWasComplex = Matrix->Complex;
return 0;
}
#endif /* INITIALIZE */
|