[Pntool-developers] SF.net SVN: pntool:[182] spnbox
Brought to you by:
compaqdrew,
miordache
From: <Ste...@us...> - 2009-07-07 19:11:33
|
Revision: 182 http://pntool.svn.sourceforge.net/pntool/?rev=182&view=rev Author: StephenCamp Date: 2009-07-07 19:11:31 +0000 (Tue, 07 Jul 2009) Log Message: ----------- Added issiph.c to implement the is_siph function, along with test routines. It is debugged and passes all tests. Added the function definition and the deallocation functions for issiph. Added ipslv's test program to the test makefile and deleted independent ipslv test makefile. Deleted matrixmath's test routines. New routines may be added later. Modified Paths: -------------- spnbox/deallocation.c spnbox/spnbox.h spnbox/tests/Makefile Added Paths: ----------- spnbox/issiph.c spnbox/tests/test-issiph.c spnbox/tests/test-issiph.txt Removed Paths: ------------- spnbox/tests/test-ipslv.mak spnbox/tests/test-matrixmath.c spnbox/tests/test-matrixmath.mak Modified: spnbox/deallocation.c =================================================================== --- spnbox/deallocation.c 2009-07-07 15:29:42 UTC (rev 181) +++ spnbox/deallocation.c 2009-07-07 19:11:31 UTC (rev 182) @@ -145,3 +145,8 @@ { DeallocateTactn(data); } + +void DeallocateIssiph(issiph_r *data) +{ + FreeMemorySafe(data->flag); +} Added: spnbox/issiph.c =================================================================== --- spnbox/issiph.c (rev 0) +++ spnbox/issiph.c 2009-07-07 19:11:31 UTC (rev 182) @@ -0,0 +1,127 @@ +#include "../pnheaders/general.h" +#include "../pnheaders/matrix.h" +#include "spnbox.h" + +static int CheckParams(matrix *Dm, matrix *Dp, void *PlaceSet, int MultiplePlaceSets); +static inline int PlaceSets(void* PlaceSet, int MultiplePlaceSets); +static inline int IsPlaceFlagged(int Place, int SetIndex, void* PlaceSet, int MultiplePlaceSets); + +issiph_r issiph(matrix *Dm, matrix *Dp, void *PlaceSet, int MultiplePlaceSets) +{ + issiph_r result; + int i, j, k, in, out; + + memset(&result, 0, sizeof(issiph_r)); + /*Check parameters and fail if they are bad.*/ + if (! CheckParams(Dm, Dp, PlaceSet, MultiplePlaceSets)) + { + return result; + } + + /*Allocate space and initialize the result set (no flags set by default). The + overall flag is cleared by default.*/ + result.flag = tcalloc(PlaceSets(PlaceSet, MultiplePlaceSets), sizeof(int)); + + /*Iterate through all the place sets to be checked.*/ + for (i = 0; i < PlaceSets(PlaceSet, MultiplePlaceSets); i++) + { + /*The flag for the place set should be set by default.*/ + result.flag[i] = 1; + /*Iterate through each transition and examine it.*/ + for (j = 0; j < NumberOfColumns(*Dm); j++) + { + /*For each transition, find out whether there are + (a) any input arcs coming from places in the current set. + (b) any output arcs coming from places in the current set.*/ + in = 0; + out = 0; + for (k = 0; k < NumberOfRows(*Dm); k++) + { + if (IsPlaceFlagged(k, i, PlaceSet, MultiplePlaceSets)) + { + if (GetMatrixEl(Dp, k, j)) in = 1; + if (GetMatrixEl(Dm, k, j)) out = 1; + } + } + /*If in is set and out is cleared, the current transition is such that + it feeds at least one of the current set of places without being fed by + any of them. If this is the case the current place set is not a siphon. + Clear the flag and break out of the transition loop. Also, set the flx + flag to indicate that there was at least one place set that was not a + siphone.*/ + if (in && ! out) + { + result.flag[i] = 0; + result.flx = 1; + break; + } + } + } + return result; +} + +/******************************************************************************* +IsPlaceFlagged returns nonzero if the given place is a member of (is flagged in) +the current place set. It automatically determines how to treat the PlaceSet +pointer.*/ +inline int IsPlaceFlagged(int Place, int SetIndex, void* PlaceSet, int MultiplePlaceSets) +{ + if (MultiplePlaceSets) + { + return GetMatrixEl((matrix*) PlaceSet, Place, SetIndex); + } + else + { + return ((int*) PlaceSet)[Place]; + } +} + +/******************************************************************************* +PlaceSets returns the number of place sets to be iterated through by examining +the MultiplePlaceSets flag and the PlaceSet pointer.*/ +inline int PlaceSets(void* PlaceSet, int MultiplePlaceSets) +{ + /*If MultiplePlaceSets is set, then PlaceSet points to a matrix such that each + column is a place set (a set of flags, one for each place). The number of sets + is the number of columns.*/ + if (MultiplePlaceSets) + { + return NumberOfColumns(*((matrix*) PlaceSet)); + } + else + { + return 1; + } +} + +/******************************************************************************* +Check the parameters. Dm and Dp must be provided and of the same size. PlaceSet +must not be a null pointer. If MultiplePlaceSets is nonzero, PlaceSet must point +to a matrix with the same number of rows as Dm/Dp.*/ +int CheckParams(matrix *Dm, matrix *Dp, void *PlaceSet, int MultiplePlaceSets) +{ + if (! (Dm && Dp)) + { + merror(0, "ISSIPH: The input or the output matrix pointer was null"); + return 0; + } + if (NumberOfRows(*Dm) != NumberOfRows(*Dp) || NumberOfColumns(*Dm) != NumberOfColumns(*Dp)) + { + merror(0, "ISSIPH: The input and output matrices are not the same size"); + return 0; + } + if (! PlaceSet) + { + merror(0, "ISSIPH: The place set pointer was null"); + return 0; + } + if (MultiplePlaceSets) + { + if (NumberOfRows(*((matrix*) PlaceSet)) != NumberOfRows(*Dm)) + { + merror(0, "ISSIPH: The place set matrix did not have the same number of rows as there were places"); + return 0; + } + } + return 1; +} Modified: spnbox/spnbox.h =================================================================== --- spnbox/spnbox.h 2009-07-07 15:29:42 UTC (rev 181) +++ spnbox/spnbox.h 2009-07-07 19:11:31 UTC (rev 182) @@ -126,6 +126,12 @@ type just for consistency's sake.*/ typedef actn_r tactn_r; +typedef struct issiph_r +{ + int* flag; + int flx; +} issiph_r; + /*Functions to free return structures. Implemented in deallocation.c*/ void DeallocateSupervise(supervis_r *data); void DeallocateIpslv(ipslv_r *data); @@ -140,6 +146,7 @@ void DeallocateReduce(reduce_r *data); void DeallocateActn(actn_r *data); void DeallocateTactn(tactn_r *data); +void DeallocateIssiph(issiph_r *data); /*Constants returned in by functions to indicate the nature of the result.*/ #define HOW_ERROR "error" @@ -603,4 +610,24 @@ Converted to C by Marian V. Iordache and Stephen Camp, ste...@le.... */ + +issiph_r issiph(matrix *Dm, matrix *Dp, void *PlaceSet, int MultiplePlaceSets); +/* +IS_SIPH - checks whether a set of places is a siphon + +[flag, flx] = is_siph(Dm, Dp, S) + +S - a set of places represented as a column of zeros and ones + S is allowed to be a matrix, in which case each of the columns + is verified + +Dm, Dp - the incidence matrices + +flag = (S == siphon) +flx = sum(flag == 0) (i.e. flx is nonzero if a column of S is not a siphon) + +Written for Matlab by Marian V. Iordache, mar...@le.... +Converted to C by Marian V. Iordache and Stephen Camp, ste...@le.... +*/ + #endif Modified: spnbox/tests/Makefile =================================================================== --- spnbox/tests/Makefile 2009-07-07 15:29:42 UTC (rev 181) +++ spnbox/tests/Makefile 2009-07-07 19:11:31 UTC (rev 182) @@ -7,11 +7,13 @@ COMPILER=gcc -g #The dependencies common to all test executables. -COMMON=StructuredIO.o test.o MemoryManager.o general.o pns.o matrix.o matrixmath.o +COMMON=StructuredIO.o test.o MemoryManager.o general.o pns.o matrix.o matrixmath.o deallocation.o #These symbols define the dependencies of various test executables. -IPSOLVE=ipsolve.o ipslv.o ../liblpsolve55.a +IPSLV=ipslv.o ../liblpsolve55.a +IPSOLVE=ipsolve.o $(IPSLV) ISADM=isadm.o +ISSIPH=issiph.o MSPLIT=msplit.o PN2ACPN=pn2acpn.o SUPERVIS=supervis.o @@ -23,19 +25,19 @@ REDUCE=reduce.o chkcons.o $(IPSOLVE) TACTN=tactn.o $(IPSOLVE) -#All test dependencies. -ALL=$(COMMON) $(IPSOLVE) isadm.o msplit.o pn2acpn.o actn.o ilpadm.o linenf.o nltrans.o pn2eacpn.o reduce.o chkcons.o tactn.o -ALLSRC=isolve.c ipslv.c isadm.c msplit.c pn2acpn.c supervis.c actn.c ilpadm.c linenf.c nltrans.c pn2eacpn.c reduce.c chkcons.c tactn.c - #Common test header dependencies. COMMONHEADER=spnbox.h test.h ../../pnheaders/pns.h ../../pnheaders/matrix.h ../matrixmath.h ../MemoryManager.h StructuredIO.h #Targets -all: ipsolve isadm msplit pn2acpn supervis actn ilpadm linenf nltrans pn2eacpn reduce tactn +all: ipsolve isadm issiph msplit pn2acpn supervis actn ilpadm linenf nltrans pn2eacpn reduce tactn +ipslv: test-ipslv.o $(IPSLV) + $(COMPILER) -o ipslv.exe test-ipslv.o $(IPSLV) ipsolve: test-ipsolve.o $(COMMON) $(IPSOLVE) $(COMPILER) -o ipsolve.exe test-ipsolve.o $(COMMON) $(IPSOLVE) isadm: test-isadm.o $(ISADM) $(COMPILER) -o isadm.exe test-isadm.o $(COMMON) $(ISADM) +issiph: test-issiph.o $(COMMON) $(ISSIPH) + $(COMPILER) -o issiph.exe test-issiph.o $(COMMON) $(ISSIPH) msplit: test-msplit.o $(MSPLIT) $(COMPILER) -o msplit.exe test-msplit.o $(COMMON) $(MSPLIT) pn2acpn: test-pn2acpn.o $(COMMON) $(PN2ACPN) @@ -66,12 +68,16 @@ $(COMPILER) -c ../actn.c chkcons.o: ../spnbox.h ../../pnheaders/matrix.h ../../pnheaders/pns.h ../chkcons.c $(COMPILER) -c ../chkcons.c +deallocation.o: ../spnbox.h + $(COMPILER) -c ../deallocation.c general.o: ../../pnheaders/general.c ../../pnheaders/general.h $(COMPILER) -c ../../pnheaders/general.c ilpadm.o: ../spnbox.h ../matrixmath.h ../../pnheaders/general.h ../../pnheaders/matrix.h ../MemoryManager.h ../ilpadm.c $(COMPILER) -c ../ilpadm.c isadm.o: ../isadm.c ../spnbox.h ../../pnheaders/general.h ../MemoryManager.h ../../pnheaders/pns.h ../matrixmath.h $(COMPILER) -c ../isadm.c +issiph.o: ../issiph.c ../spnbox.h ../../pnheaders/general.h ../../pnheaders/matrix.h + $(COMPILER) -c ../issiph.c ipslv.o: ../spnbox.h ../ipslv.c $(COMPILER) -c ../ipslv.c ipsolve.o: ../spnbox.h ../matrixmath.h ../../pnheaders/general.h ../../pnheaders/matrix.h ../MemoryManager.h ../ipsolve.c @@ -106,29 +112,31 @@ $(COMPILER) -c test.c #Rules for making the test executable object files. -test-actn.o: test-actn.c $(COMMONHEADERS) +test-actn.o: test-actn.c $(COMMONHEADER) $(COMPILER) -c test-actn.c -test-ilpadm.o: test-ilpadm.c $(COMMONHEADERS) +test-ilpadm.o: test-ilpadm.c $(COMMONHEADER) $(COMPILER) -c test-ilpadm.c -test-ipsolve.o: test-ipsolve.c $(COMMONHEADERS) +test-ipslv.o: test-ipslv.c + $(COMPILER) -c test-ipslv.c +test-ipsolve.o: test-ipsolve.c $(COMMONHEADER) $(COMPILER) -c test-ipsolve.c -test-isadm.o: test-isadm.c $(COMMONHEADERS) +test-isadm.o: test-isadm.c $(COMMONHEADER) $(COMPILER) -c test-isadm.c -test-linenf.o: test-linenf.c $(COMMONHEADERS) +test-linenf.o: test-linenf.c $(COMMONHEADER) $(COMPILER) -c test-linenf.c -test-msplit.o: test-msplit.c $(COMMONHEADERS) +test-msplit.o: test-msplit.c $(COMMONHEADER) $(COMPILER) -c test-msplit.c -test-nltrans.o: test-nltrans.c $(COMMONHEADERS) +test-nltrans.o: test-nltrans.c $(COMMONHEADER) $(COMPILER) -c test-nltrans.c -test-pn2acpn.o: test-pn2acpn.c $(COMMONHEADERS) +test-pn2acpn.o: test-pn2acpn.c $(COMMONHEADER) $(COMPILER) -c test-pn2acpn.c -test-pn2eacpn.o: test-pn2eacpn.c $(COMMONHEADERS) +test-pn2eacpn.o: test-pn2eacpn.c $(COMMONHEADER) $(COMPILER) -c test-pn2eacpn.c -test-supervis.o: test-supervis.c $(COMMONHEADERS) +test-supervis.o: test-supervis.c $(COMMONHEADER) $(COMPILER) -c test-supervis.c -test-reduce.o: test-reduce.c $(COMMONHEADERS) +test-reduce.o: test-reduce.c $(COMMONHEADER) $(COMPILER) -c test-reduce.c -test-tactn.o: test-tactn.c $(COMMONHEADERS) +test-tactn.o: test-tactn.c $(COMMONHEADER) $(COMPILER) -c test-tactn.c #Rules for making the liblpsolve55.a static library for use with ipsolve. Deleted: spnbox/tests/test-ipslv.mak =================================================================== --- spnbox/tests/test-ipslv.mak 2009-07-07 15:29:42 UTC (rev 181) +++ spnbox/tests/test-ipslv.mak 2009-07-07 19:11:31 UTC (rev 182) @@ -1,9 +0,0 @@ - -test-ipslv.exe: test-ipslv.o ipslv.o - gcc -g -o test-ipslv.exe ipslv.o test-ipslv.o ../liblpsolve55.a - -test-ipslv.o: test-ipslv.c ../spnbox.h - gcc -g -c test-ipslv.c - -ipslv.o: ../ipslv.c ../spnbox.h - gcc -g -c ../ipslv.c Added: spnbox/tests/test-issiph.c =================================================================== --- spnbox/tests/test-issiph.c (rev 0) +++ spnbox/tests/test-issiph.c 2009-07-07 19:11:31 UTC (rev 182) @@ -0,0 +1,57 @@ +#include "test.h" + +int main(int argc, char *argv[]) +{ + MemoryManager memory; + FILE* input; + int *Filled, *SArray; + int Constraints, Transitions, i, OFilled[2]; + issiph_r result; + + char IDesc[] = "matrix D, matrix Dm, matrix Dp, matrix PlaceSet"; + char ODesc[] = "arrayi IsSiphon, int NonSiphonPresent"; + + matrix D, Dm, Dp, PlaceSet; + + /*Get a pointer to the input file.*/ + if (! (input = GetInput(argc, argv))) + { + return 1; + } + + memory = CreateMemoryManager(5, 6, 0, 0); + + while (ParseStructure(input, IDesc, &Filled, &memory, &D, &Dm, &Dp, &PlaceSet)) + { + /*Display the problem.*/ + DisplayStructure(IDesc, Filled, &D, &Dm, &Dp, &PlaceSet); + + /*If an incidence matrix was supplied fill in the i/o matrices.*/ + FillDmDp(Filled, &D, &Dm, &Dp, &memory); + + /*If there is only one column in the PlaceSet matrix, then convert it to an + array.*/ + if (Filled[3] && NumberOfColumns(PlaceSet) == 1) + { + SArray = mcalloc(&memory, NumberOfRows(PlaceSet), sizeof(int)); + for (i = 0; i < NumberOfRows(PlaceSet); i++) + { + SArray[i] = GetMatrixEl(&PlaceSet, i, 0); + } + } + + /*Solve the problem.*/ + result = issiph(Filled[1] ? &Dm : 0, Filled[2] ? &Dp : 0, Filled[3] ? (NumberOfColumns(PlaceSet) > 1 ? (void*) &PlaceSet : (void*) SArray) : 0, Filled[3] ? NumberOfColumns(PlaceSet) > 1 : 0); + + /*Display the solution.*/ + OFilled[0] = result.flag ? 1 : 0; + OFilled[1] = 1; + DisplayStructure(ODesc, OFilled, result.flag, NumberOfColumns(PlaceSet), result.flx); + DeallocateIssiph(&result); + printf("---------------------------------------------------------------\n"); + FreeMemory(&memory); + memory = CreateMemoryManager(5, 6, 0, 0); + } + FreeMemory(&memory); + return 0; +} Added: spnbox/tests/test-issiph.txt =================================================================== --- spnbox/tests/test-issiph.txt (rev 0) +++ spnbox/tests/test-issiph.txt 2009-07-07 19:11:31 UTC (rev 182) @@ -0,0 +1,66 @@ +rem Keywords: +rem matrix D, matrix Dm, matrix Dp, matrix PlaceSet + +echo A set of places is a siphon if there exists no transition such that it has +echo at least one input arc feeding any of the places in the set and has no +echo output arcs fed by any of the places in the set. +echo That is, all transitions are such that they either have no input arcs +echo feeding the places in the set or have at least one output arc fed by one +echo of the places in the set. +echo Problem 1. +echo flag should be: +echo flag: 1 0 0 + +D 6 4 +0 -1 1 -1 +0 0 -1 2 +0 -1 1 -1 +1 0 -1 2 +0 -1 1 -1 +0 0 0 2 + +PlaceSet 6 3 +1 0 0 +1 0 0 +0 1 0 +0 1 0 +0 0 1 +0 0 1 + +done + +echo Problem 2 +echo This problem tests issiph with an array as the place set instead of a +echo matrix. The place set should be a siphon. + +D 4 3 +0 -1 1 +1 0 0 +0 0 -1 +0 1 1 + +PlaceSet 4 1 +1 +0 +1 +1 + +done + +echo Problem 3 +echo Final verification - Petri net of problem 2 with a different place set. +echo This time the set should not be a siphon. +D 4 3 +0 -1 1 +1 0 0 +0 0 -1 +0 1 1 + +PlaceSet 4 1 +1 +1 +1 +1 +done + +quit Deleted: spnbox/tests/test-matrixmath.c =================================================================== --- spnbox/tests/test-matrixmath.c 2009-07-07 15:29:42 UTC (rev 181) +++ spnbox/tests/test-matrixmath.c 2009-07-07 19:11:31 UTC (rev 182) @@ -1,340 +0,0 @@ -#include "../../pnheaders/pns.h" -#include "../matrixmath.h" - -/*This routine tests the add, subtract, and multiply functions in standard usage (pre-allocated result matrices).*/ -void TestAddSubtractMultiply(); - -/*This routine tests the add, subtract, and multiply routines' ability to allocate their own storage space.*/ -void TestAddSubtractMultiplySelfAllocation(); - -/*This routine tests the Concatenate functions.*/ -void TestConcatenations(); - -/*These functions test to make sure that the matrix math functions correctly identify when they are passed an initialized matrix of the wrong dimensions to fill with the result.*/ -void TestAddBadDestinationError(); -void TestSubtractBadDestinationError(); -void TestMultiplyBadDestinationError(); -void TestConcatenateRowsBadDestinationError(); -void TestConcatenateColsBadDestinationError(); - - -int main() -{ - printf("Test menu:\n"); - printf("0. Quit.\n"); - printf("1. Test Addition, Subtraction, and Multiplication with pre-allocated storage.\n"); - printf("2. Test Addition, Subtraction, and Multiplication with self-allocated storage.\n"); - printf("3. Test Concatenation of Rows and Columns, both storage allocation types.\n"); - printf("4. Test bad destination error on Addition.\n"); - printf("5. Test bad destination error on Subtraction.\n"); - printf("6. Test bad destination error on Multiplication.\n"); - printf("7. Test bad destination error on Concatenation of Rows\n"); - printf("8. Test bad destination error on Concatenation of Columns\n"); - printf("Enter your choice: "); - char c = getchar(); - - switch(c) - { - case '1': - TestAddSubtractMultiply(); - break; - case '2': - TestAddSubtractMultiplySelfAllocation(); - break; - case '3': - TestConcatenations(); - break; - case '4': - TestAddBadDestinationError(); - break; - case '5': - TestSubtractBadDestinationError(); - break; - case '6': - TestMultiplyBadDestinationError(); - break; - case '7': - TestConcatenateRowsBadDestinationError(); - break; - case '8': - TestConcatenateColsBadDestinationError(); - break; - } - return 0; -} - -void TestAddBadDestinationError() -{ - int Data[] = { 1, 2, 3, 4,\ - 2, 3, 4, 5,\ - 3, 4, 5, 6,\ - 4, 5, 6, 7}; - matrix DataMatrix, Destination; - //Prepare the data to be added to itself. - DataMatrix = vector2matrix(Data, 4, 4); - - //Allocate a result matrix of the wrong size. - AllocateMatrix(&Destination, 3, 3); - - //Attempt an addition and verify that it fails. - printf("Testing addition of a 4x4 with itself, a 3x3 as the preallocated destination...\n"); - printf("(This should cause an error)\n"); - AddMatrix(&DataMatrix, &DataMatrix, &Destination); - printf("If this point is reached the error was not detected.\n"); -} - -void TestSubtractBadDestinationError() -{ - int Data[] = { 1, 2, 3, 4,\ - 2, 3, 4, 5,\ - 3, 4, 5, 6,\ - 4, 5, 6, 7}; - matrix DataMatrix, Destination; - //Prepare the data to be added to itself. - DataMatrix = vector2matrix(Data, 4, 4); - - //Allocate a result matrix of the wrong size. - AllocateMatrix(&Destination, 3, 3); - - //Attempt an addition and verify that it fails. - printf("Testing subtraction of a 4x4 from itself, a 3x3 as the preallocated destination...\n"); - printf("(This should cause an error)\n"); - SubtractMatrix(&DataMatrix, &DataMatrix, &Destination); - printf("If this point is reached the error was not detected.\n"); -} - -void TestMultiplyBadDestinationError() -{ - int Data[] = { 1, 2, 3, 4,\ - 2, 3, 4, 5,\ - 3, 4, 5, 6,\ - 4, 5, 6, 7}; - matrix DataMatrix, Destination; - //Prepare the data to be added to itself. - DataMatrix = vector2matrix(Data, 4, 4); - - //Allocate a result matrix of the wrong size. - AllocateMatrix(&Destination, 3, 3); - - //Attempt an addition and verify that it fails. - printf("Testing multiplication of a 4x4 with itself, a 3x3 as the preallocated destination...\n"); - printf("(This should cause an error)\n"); - MultiplyMatrix(&DataMatrix, &DataMatrix, &Destination); - printf("If this point is reached the error was not detected.\n"); -} - -void TestConcatenateRowsBadDestinationError() -{ - int Data[] = { 1, 2, 3, 4,\ - 2, 3, 4, 5,\ - 3, 4, 5, 6,\ - 4, 5, 6, 7}; - matrix DataMatrix, Destination; - //Prepare the data to be added to itself. - DataMatrix = vector2matrix(Data, 4, 4); - - //Allocate a result matrix of the wrong size. - AllocateMatrix(&Destination, 3, 3); - - //Attempt an addition and verify that it fails. - printf("Testing concatenation of rows of a 4x4 with itself, a 3x3 as the preallocated destination...\n"); - printf("(This should cause an error)\n"); - ConcatenateMatrixRows(&DataMatrix, &DataMatrix, &Destination); - printf("If this point is reached the error was not detected.\n"); -} - -void TestConcatenateColsBadDestinationError() -{ - int Data[] = { 1, 2, 3, 4,\ - 2, 3, 4, 5,\ - 3, 4, 5, 6,\ - 4, 5, 6, 7}; - matrix DataMatrix, Destination; - //Prepare the data to be added to itself. - DataMatrix = vector2matrix(Data, 4, 4); - - //Allocate a result matrix of the wrong size. - AllocateMatrix(&Destination, 3, 3); - - //Attempt an addition and verify that it fails. - printf("Testing concatenation of cols of a 4x4 with itself, a 3x3 as the preallocated destination...\n"); - printf("(This should cause an error)\n"); - ConcatenateMatrixCols(&DataMatrix, &DataMatrix, &Destination); - printf("If this point is reached the error was not detected.\n"); -} - -void TestAddSubtractMultiplySelfAllocation() -{ - int DataA[] = { 1, 2, 3, 4,\ - 2, 3, 4, 5,\ - 3, 4, 5, 6,\ - 4, 5, 6, 7}; - int DataB[] = { 5, 6, 7, 8,\ - 6, 7, 8, 9,\ - 7, 8, 9, 10, - 8, 9, 10, 11}; - - int DataC[] = { 1, 0, 2,\ - -1, 3, 1}; - int DataD[] = { 3, 1,\ - 2, 1,\ - 1, 0}; - matrix A, B, C, D, SumAA, SumAB, DifferenceAA, DifferenceAB, ProductCD, ProductAB; - //Initialize A and B to matrices that will produce nice patterns for addition and subtraction. - A = vector2matrix(DataA, 4, 4); - B = vector2matrix(DataB, 4, 4); - //Initializae G and H to two matrices - C = vector2matrix(DataC, 2, 3); - D = vector2matrix(DataD, 3, 2); - - //Show the operands matrices for reference. - ShowMatrix(&A, "A"); - ShowMatrix(&B, "B"); - ShowMatrix(&C, "C"); - ShowMatrix(&D, "D"); - - //Take the sum of A and itself, default type. - printf("Testing matrix addition A + A, default type...\n"); - SumAA = AddMatrix(&A, &A, 0); - ShowMatrix(&SumAA, "A + A"); - - //Take the sum of A and B, type 2 - printf("Testing matrix addition A + B, type 2...\n"); - SumAB = AddMatrix(&A, &B, (matrix*) 2); - ShowMatrix(&SumAB, "A + B"); - - //Take A - A, default type. - printf("Testing matrix subtraction A - A, default type...\n"); - DifferenceAA = SubtractMatrix(&A, &A, 0); - ShowMatrix(&DifferenceAA, "A - A"); - - //Take A - B, type 2. - printf("Testing matrix subtraction A - B, type 2...\n"); - DifferenceAB = SubtractMatrix(&A, &B, (matrix*) 2); - ShowMatrix(&DifferenceAB, "A - A"); - - //Take C*D, default type. - printf("Testing matrix multiplication C*D, default type...\n"); - ProductCD = MultiplyMatrix(&C, &D, 0); - ShowMatrix(&ProductCD, "C*D"); - - //Take A*B, type 2 - printf("Testing matrix multiplication A*B, type 2...\n"); - ProductAB = MultiplyMatrix(&A, &B, (matrix*) 2); - ShowMatrix(&ProductAB, "A*B"); - - //Deallocate memory. - DeallocateMatrix(&A); - DeallocateMatrix(&B); - DeallocateMatrix(&C); - DeallocateMatrix(&D); - DeallocateMatrix(&SumAA); - DeallocateMatrix(&SumAB); - DeallocateMatrix(&DifferenceAA); - DeallocateMatrix(&DifferenceAB); - DeallocateMatrix(&ProductCD); - DeallocateMatrix(&ProductAB); -} - -void TestConcatenations() -{ - /*Use patterns for the operands that will yield patterns in the result.*/ - int DataA[] = { 1, 2, 3, 4,\ - 2, 3, 4, 5,\ - 3, 4, 5, 6,\ - 4, 5, 6, 7}; - int DataB[] = { 5, 6, 7, 8,\ - 6, 7, 8, 9,\ - 7, 8, 9, 10, - 8, 9, 10, 11}; - matrix A, B, AllocatedRowConcat, AllocatedColConcat, EmptyRowConcat, EmptyColConcat; - - /*Initialize the operand matrices.*/ - A = vector2matrix(DataA, 4, 4); - B = vector2matrix(DataB, 4, 4); - - /*Allocate storage space for the functions to fill in pre-allocated mode.*/ - AllocateMatrix(&AllocatedRowConcat, 4, 8); - AllocateMatrix(&AllocatedColConcat, 8, 4); - - /*Test the concatenations in pre-allocated mode.*/ - printf("Testing row concatenation in pre-allocated mode...\n"); - ConcatenateMatrixRows(&A, &B, &AllocatedRowConcat); - ShowMatrix(&AllocatedRowConcat, "rows(A) concat rows(B)"); - - printf("Testing column concatenation in pre-allocated mode...\n"); - ConcatenateMatrixCols(&A, &B, &AllocatedColConcat); - ShowMatrix(&AllocatedColConcat, "cols(A) concat cols(B)"); - - /*Testing the concenations with them allocating storage space.*/ - printf("Testing row concatenation with allocation (Default Type)...\n"); - EmptyRowConcat = ConcatenateMatrixRows(&A, &B, 0); - ShowMatrix(&EmptyRowConcat, "rows(A) concat rows(B)"); - printf("The type of matrix 'rows(A) concat rows(B)' is %d.\n", EmptyRowConcat.type); - - printf("Testing column concatenation with allocation (Type 2)...\n"); - EmptyColConcat = ConcatenateMatrixCols(&A, &B, (matrix*) 2); - ShowMatrix(&EmptyColConcat, "cols(A) concat cols(B)"); - printf("The type of matrix 'cols(A) concat cols(B)' is %d.\n", EmptyColConcat.type); -} - -void TestAddSubtractMultiply() -{ - //Declare three matrix objects. We'll use square matrices for the initial tests. To make things easy, we - //use a magic square matrix and its transpose from the Matlab documentation. - int DataA[] = {16, 3, 2, 13,\ - 5, 10, 11, 8,\ - 9, 6, 7, 12,\ - 4, 15, 14, 1}; - int DataB[] = {16, 5, 9, 4,\ - 3, 10, 6, 15,\ - 2, 11, 7, 14,\ - 13, 8, 12, 1}; - int DataG[] = { 1, 0, 2,\ - -1, 3, 1}; - int DataH[] = { 3, 1,\ - 2, 1,\ - 1, 0}; - matrix A, B, C, D, E, F, G, H, I; - //Initialize A and B to the magic square and its transpose. - A = vector2matrix(DataA, 4, 4); - B = vector2matrix(DataB, 4, 4); - //Initializae G and H to two matrices - G = vector2matrix(DataG, 2, 3); - H = vector2matrix(DataH, 3, 2); - - //Allocate memory for the other matrices. - AllocateMatrix(&C, 4, 4); - AllocateMatrix(&D, 4, 4); - AllocateMatrix(&E, 4, 4); - AllocateMatrix(&F, 4, 4); - AllocateMatrix(&I, 2, 2); - - //Display the matrices - ShowMatrix(&A, "A"); - ShowMatrix(&B, "B"); - - //Add A to itself and display the result. - AddMatrix(&A, &A, &C); - ShowMatrix(&C, "A + A"); - - //Add A to B and display the result. - AddMatrix(&A, &B, &D); - ShowMatrix(&D, "A + B"); - - //Subtract A - B and display the result. - SubtractMatrix(&A, &B, &E); - ShowMatrix(&E, "A - B"); - - //Multiply B * A and display the result. - MultiplyMatrix(&B, &A, &F); - ShowMatrix(&F, "B*A"); - - //Show the non-square matrices. - ShowMatrix(&G, "G"); - ShowMatrix(&H, "H"); - - //Multiply the non-square matrices. - MultiplyMatrix(&G, &H, &I); - ShowMatrix(&I, "G*H"); -} Deleted: spnbox/tests/test-matrixmath.mak =================================================================== --- spnbox/tests/test-matrixmath.mak 2009-07-07 15:29:42 UTC (rev 181) +++ spnbox/tests/test-matrixmath.mak 2009-07-07 19:11:31 UTC (rev 182) @@ -1,19 +0,0 @@ -COMPILER=gcc -g -ggdb - -test-matrixmath: test-matrixmath.o pns.o matrix.o general.o matrixmath.o - $(COMPILER) -o test-matrixmath test-matrixmath.o pns.o matrix.o general.o matrixmath.o - -general.o: ../../pnheaders/general.c ../../pnheaders/general.h - $(COMPILER) -c ../../pnheaders/general.c - -matrix.o: ../../pnheaders/matrix.c ../../pnheaders/matrix.h ../../pnheaders/general.h - $(COMPILER) -c ../../pnheaders/matrix.c - -pns.o: ../../pnheaders/pns.c ../../pnheaders/pns.h ../../pnheaders/general.h ../../pnheaders/matrix.h - $(COMPILER) -c ../../pnheaders/pns.c - -matrixmath.o: ../matrixmath.c ../matrixmath.h ../../pnheaders/general.h ../../pnheaders/matrix.h - $(COMPILER) -c ../matrixmath.c - -test-matrixmath.o: test-matrixmath.c ../../pnheaders/pns.h ../matrixmath.h - $(COMPILER) -c test-matrixmath.c This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |