[Pntool-developers] SF.net SVN: pntool:[206] spnbox
Brought to you by:
compaqdrew,
miordache
From: <Ste...@us...> - 2009-07-25 00:47:02
|
Revision: 206 http://pntool.svn.sourceforge.net/pntool/?rev=206&view=rev Author: StephenCamp Date: 2009-07-25 00:46:51 +0000 (Sat, 25 Jul 2009) Log Message: ----------- Added fvpr.c, test routines, and makefile entries. Modified avpr.c. fvpr and avpr now can accept either matrices or integer arrays for the vectors. Modified Paths: -------------- spnbox/avpr.c spnbox/spnbox.h spnbox/tests/Makefile spnbox/tests/test-avpr.c spnbox/tests/test-avpr.txt Modified: spnbox/avpr.c =================================================================== --- spnbox/avpr.c 2009-07-24 19:54:56 UTC (rev 205) +++ spnbox/avpr.c 2009-07-25 00:46:51 UTC (rev 206) @@ -1,77 +1,163 @@ #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include "../pnheaders/general.h" +#include "../pnheaders/matrix.h" +#include "spnbox.h" -static int CheckParams(double* vector, int length, char** chr, char* destination); +/*The answer that avpr returns is a pointer to this buffer. It is reset with each +call to avpr.*/ +static char* buffer = 0; +static int bufferLength = 0; -void avpr(double *vector, int length, char* chr, int option, char* destination) +static int CheckParams(int* vector, int length, int mode, char** chr); +static int GetVectorEl(void* vector, int i, int mode); +static int GetBufferLength(void* vector, int length, int mode, char* chr, char* bl, char* el); + +char* avpr(void *vector, int length, int mode, char* chr, int option) { char element[64]; + if (mode == DISPLAY_MATRIX) length = MatrixLength(*((matrix*) vector)); + /*Fill in default parameters.*/ - if (! CheckParams(vector, length, &chr, destination)) - { - return; - } + if (! CheckParams(vector, length, mode, &chr)) return 0; + + /*If the vector is a matrix, get the length from it.*/ + /*Setup bl and el - some kind of beginning and terminating strings.*/ char *bl = option ? "_{" : ""; char *el = option ? "}" : ""; - /*Destination defaults to an empty string.*/ - destination[0] = '\0'; + /*Setup the destination buffer. Begin by determining the maximum length we + will need. It is, at most, the number of nonzero entries in the vector times + the sum of the lengths of the various string parts times the base-10 log of + the length of the vector.*/ + int i, newBufferLength = GetBufferLength(vector, length, mode, chr, bl, el); - /*Iterate through the elements in the vector, processing only nonzero - elements. The first nonzero element is processed differently than the others, - so use a flag to indicate whether the first nonzero element has been found or - not.*/ - int firstProcessed = 0; - int i; + //Allocate new space only if necessary. + if (newBufferLength > bufferLength) + { + free(buffer); + bufferLength = newBufferLength; + buffer = tcalloc(bufferLength, sizeof(char)); + } + //The buffer defaults to an empty string. + buffer[0] = '\0'; + /*If there are no nonzero entries, newBufferLength will be 1 and we should + return an empty string.*/ + if (newBufferLength == 1) + { + return buffer; + } + + int firstProcessed = 0; for (i = 0; i < length; i++) { - if (! vector[i]) continue; + int c = GetVectorEl(vector, i, mode); + if (! c) continue; if (! firstProcessed) { - if (vector[i] == 1) + if (c == 1) { sprintf(element, "%s%s%d%s", chr, bl, i + 1, el); } - else if (vector[i] == -1) + else if (c == -1) { sprintf(element, "-%s%s%d%s", chr, bl, i + 1, el); } else { - sprintf(element, "%g%s%s%d%s", vector[i], chr, bl, i + 1, el); + sprintf(element, "%d%s%s%d%s", c, chr, bl, i + 1, el); } firstProcessed = 1; } else { - if (vector[i] == 1) + if (c == 1) { sprintf(element, "+%s%s%d%s", chr, bl, i + 1, el); } - else if (vector[i] > 0) + else if (c > 0) { - sprintf(element, "+%g%s%s%d%s", vector[i], chr, bl, i + 1, el); + sprintf(element, "+%d%s%s%d%s", c, chr, bl, i + 1, el); } - else if (vector[i] == -1) + else if (c == -1) { sprintf(element, "-%s%s%d%s", chr, bl, i + 1, el); } else { - sprintf(element, "%g%s%s%d%s", vector[i], chr, bl, i + 1, el); + sprintf(element, "%d%s%s%d%s", c, chr, bl, i + 1, el); } } - strcat(destination, element); + strcat(buffer, element); } + return buffer; } -int CheckParams(double* vector, int length, char** chr, char* destination) +int GetBufferLength(void* vector, int length, int mode, char* chr, char* bl, char* el) { + /*The largest the new buffer will be will be related to the length required + for each nonzero element. Each element's length will max out at: the number of + digits required to display the largest element in the vector plus the number + of digits required to display the largest nonzero index in the vector plus the + number of characters required to display the strings chr, bl, and el.*/ + int indexMax = 0, elementMax = 0; + int indexDigits = 0, elementDigits = 1, i; + int result = 0; + for (i = 0; i < length; i++) + { + int c = GetVectorEl(vector, i, mode); + if (c) + { + indexMax = i; + result++; + if (c > elementMax) elementMax = c; + if (c < -elementMax) elementMax = -c; + } + } + /*If there were no nonzero vectors, the buffer length just needs to be 1.*/ + if (! result) return 1; + /*To find the number of digits required to display the maximum, do an + approximate base-10 logarithm.*/ + elementMax++; + while (elementMax) + { + elementMax /= 10; + elementDigits++; + } + indexMax++; + while (indexMax) + { + indexMax /= 10; + indexDigits++; + } + /*Now, assume each nonzero element displays chr, bl, el, an index, and the + element value:*/ + result *= (strlen(chr) + strlen(bl) + strlen(el) + indexDigits + elementDigits); + /*Add 1 for the null terminator and return.*/ + return ++result; +} + +int GetVectorEl(void* vector, int i, int mode) +{ + switch(mode) + { + case DISPLAY_MATRIX: + return GetMatrixEl((matrix*) vector, i % NumberOfRows(* ((matrix*) vector)), i / NumberOfRows(* ((matrix*) vector))); + break; + case DISPLAY_INTS: + return ((int*)vector)[i]; + break; + } +} + +int CheckParams(int* vector, int length, int mode, char** chr) +{ /*vector and destination are both required.*/ - if (! (vector && destination)) + if (! vector) { merror(0, "AVPR: A required pointer was null"); return 0; @@ -87,5 +173,11 @@ { *chr = "m"; } + if (mode != DISPLAY_MATRIX && mode !=DISPLAY_INTS) + { + merror(0, "AVPR: Invalid mode"); + return 0; + } return 1; -} \ No newline at end of file +} + \ No newline at end of file Modified: spnbox/spnbox.h =================================================================== --- spnbox/spnbox.h 2009-07-24 19:54:56 UTC (rev 205) +++ spnbox/spnbox.h 2009-07-25 00:46:51 UTC (rev 206) @@ -229,6 +229,10 @@ #define VRB_ASIPH 3 #define VRB_MAX 10 +/*Constaints used to change the behavior of the vector dispaly functions.*/ +#define DISPLAY_MATRIX 0 +#define DISPLAY_INTS 1 + msplit_r msplit(matrix* Dm, matrix* Dp, int* mask, matrix* M, matrix* L0, matrix* L, int* ipl, int ipCount, int* dpl, int dpCount, int** TD, int* TDCount); /*MSPLIT - split transitions to make a Petri net PT-ordinary. Original Matlab Usage: @@ -932,7 +936,7 @@ size must be appropriate (same number of rows as there are dependent places, same number of columns as there are independent places).*/ -void avpr(double *vector, int length, char* chr, int option, char* destination); +char* avpr(void *vector, int length, int mode, char* chr, int option); /* AVPR - Transforms a vector in a string to be used for displaying equations/inequalities. @@ -951,9 +955,30 @@ Written for Matlab by Marian V. Iordache, mar...@le.... Converted to C by Marian V. Iordache and Stephen Camp, ste...@le.... -C Usage: Parameters have the same meaning and names as in Matlab, with the -addition of the destination parameter, a pointer to a character array that will -be filled with the result. The array is not checked to make sure it has enough -space; this is left to the caller. -If chr is a null pointer it will default to m, as in Matlab.*/ +C Usage: Parameters have the same meaning and names as in Matlab. +If chr is a null pointer it will default to m, as in Matlab. +Use mode = DISPLAY_MATRIX for vector a pointer to a matrix. length is ignored. +Use mode = DISPLAY_INTS for vector a pointer to an array of integers. +The function returns a pointer to a static buffer that it owns. This buffer is +overwritten every time the function is called. The caller should use or copy the +buffer immediately after calling the function.*/ + +char* fvpr(void* vector, int length, int mode, int option); +/*FVPR - Transforms a vector in a string + +[str] = fvpr(x) + +Use the format below to print finite sets + +[str] = fvpr(x,2) + +Written for Matlab by Marian V. Iordache, mar...@le.... +Converted to C by Marian V. Iordache and Stephen Camp, ste...@le.... +C Usage: Parameters have the same meaning and names as in Matlab. +Use mode = DISPLAY_MATRIX for vector a pointer to a matrix. length is ignored. +Use mode = DISPLAY_INTS for vector a pointer to an array of integers. +The function returns a pointer to a static buffer that it owns. This buffer is +overwritten every time the function is called. The caller should use or copy the +buffer immediately after calling the function.*/ + #endif Modified: spnbox/tests/Makefile =================================================================== --- spnbox/tests/Makefile 2009-07-24 19:54:56 UTC (rev 205) +++ spnbox/tests/Makefile 2009-07-25 00:46:51 UTC (rev 206) @@ -12,6 +12,7 @@ #These symbols define the dependencies of various test executables. AVPR=avpr.o EXTENDEDMATRIX=extendedmatrix.o +FVPR=fvpr.o IPSLV=ipslv.o ../liblpsolve55.a IPSOLVE=ipsolve.o $(IPSLV) ISADM=isadm.o @@ -38,6 +39,8 @@ $(COMPILER) -o avpr.exe test-avpr.o $(AVPR) $(COMMON) extendedmatrix: test-extendedmatrix.o $(EXTENDEDMATRIX) $(COMMON) $(COMPILER) -o extendedmatrix.exe test-extendedmatrix.o $(EXTENDEDMATRIX) $(COMMON) +fvpr: test-fvpr.o $(FVPR) $(COMMON) + $(COMPILER) -o fvpr.exe test-fvpr.o $(FVPR) $(COMMON) ipslv: test-ipslv.o $(IPSLV) $(COMPILER) -o ipslv.exe test-ipslv.o $(IPSLV) ipsolve: test-ipsolve.o $(COMMON) $(IPSOLVE) @@ -90,6 +93,8 @@ $(COMPILER) -c ../deallocation.c extendedmatrix.o: ../extendedmatrix.c ../extendedmatrix.h ../../pnheaders/matrix.h ../../pnheaders/general.h $(COMPILER) -c ../extendedmatrix.c +fvpr.o: ../fvpr.c + $(COMPILER) -c ../fvpr.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 @@ -142,6 +147,8 @@ $(COMPILER) -c test-avpr.c test-extendedmatrix.o: test-extendedmatrix.c $(COMMONHEADER) $(COMPILER) -c test-extendedmatrix.c +test-fvpr.o: test-fvpr.c $(COMMONHEADER) + $(COMPILER) -c test-fvpr.c test-ilpadm.o: test-ilpadm.c $(COMMONHEADER) $(COMPILER) -c test-ilpadm.c test-ipslv.o: test-ipslv.c Modified: spnbox/tests/test-avpr.c =================================================================== --- spnbox/tests/test-avpr.c 2009-07-24 19:54:56 UTC (rev 205) +++ spnbox/tests/test-avpr.c 2009-07-25 00:46:51 UTC (rev 206) @@ -2,12 +2,13 @@ int main(int argc, char* argv[]) { - char iDesc[] = "arrayd vector, string chr, int option, int maxlength"; - double* vector = 0; - int vLength = 0, option = 0, maxlength = 256; - char *destination; + char iDesc[] = "arrayi V, matrix M, string chr, int option"; + char *result; + int* vector = 0; + int vLength = 0, option = 0; char chr[128] = ""; int *iFill; + matrix M; FILE* file; @@ -16,26 +17,34 @@ return 0; } - MemoryManager mem = CreateMemoryManager(3, 0, 0, 0); + MemoryManager mem = CreateMemoryManager(3, 1, 0, 0); - while (ParseStructure(file, iDesc, &iFill, &mem, &vector, &vLength, chr, &option, &maxlength)) + while (ParseStructure(file, iDesc, &iFill, &mem, &vector, &vLength, &M, chr, &option)) { - DisplayStructure(iDesc, iFill, vector, vLength, chr, option, maxlength); + if (iFill[0]) iFill[1] = 0; + DisplayStructure(iDesc, iFill, vector, vLength, &M, chr, option); - if (maxlength < 2 || maxlength > 5000) maxlength = 256; - destination = mcalloc(&mem, maxlength, sizeof(char)); + if (iFill[0]) + { + result = avpr(vector, vLength, DISPLAY_INTS, chr, option); + } + else if (iFill[1]) + { + result = avpr(&M, 0, DISPLAY_MATRIX, chr, option); + } + else + { + result = " No vector or matrix provided!"; + } - avpr(vector, vLength, chr, option, destination); + printf("Result:\n%s\n", result); - printf("Result:\n%s\n", destination); - FreeMemory(&mem); mem = CreateMemoryManager(3, 0, 0, 0); vector = 0; vLength = 0; option = 0; - maxlength = 256; chr[0] = '\0'; printf("-------------------------------------------------------------------------------\n"); } Modified: spnbox/tests/test-avpr.txt =================================================================== --- spnbox/tests/test-avpr.txt 2009-07-24 19:54:56 UTC (rev 205) +++ spnbox/tests/test-avpr.txt 2009-07-25 00:46:51 UTC (rev 206) @@ -1,59 +1,69 @@ echo Problem 1. echo Solution should be: echo <empty string> -vector 1 +V 1 0 done echo Problem 2. echo Solution should be: echo m1 -vector 1 +V 1 1 done echo Problem 3. echo Solution should be: echo -m1 -vector 1 +V 1 -1 done echo Problem 4. echo Solution should be: echo 2m1 -vector 1 +V 1 2 done echo Problem 5 echo Solution should be: echo -2m1 -vector 1 +V 1 -2 done echo Problem 6 echo Solution should be: echo -2m1-m2+m4+2m5 -vector 5 +V 5 -2 -1 0 1 2 done echo Problem 7 echo Solution should be: -echo -2.5z1-z2+z4+2.5z5 -vector 5 --2.5 -1 0 1 2.5 +echo -2z1-z2+z4+2z5 +V 5 +-2 -1 0 1 2 chr z done echo Problem 8 echo Solution should be: -echo -2.5m_{1}-m_{2}+m_{4}+2.5m_{5} -vector 5 --2.5 -1 0 1 2.5 +echo -2m_{1}-m_{2}+m_{4}+2m_{5} +V 5 +-2 -1 0 1 2 option 1 done + +echo Problem 9 +echo Solution should be: +echo -2m_{1}-m_{2}+m_{4}+2m_{5} +M 3 2 +-2 1 +-1 2 + 0 0 +option 1 +done quit This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |