Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sparse matrices - vfp8
Message
De
04/10/2011 15:43:59
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Versions des environnements
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Application:
Desktop
Divers
Thread ID:
01525329
Message ID:
01525573
Vues:
43
Mejor, maestro

>Hi Gregory.
>
>I think I got carried away with NxM dimensions after reading Al's post, a 2D matrix is even easier as you can code the access/assign directly, but I think you are missing the point that the request is about Sparse Matrix, which you usually do not use a Dense Matrix representation to store as they would eat your memory away just to store zeroes, that is why I think a cursor would be a better suitor, at the cost of performance.
>
>This would be the bare bone code for a 2D "sparse" matrix, using a compound index
>
>
>clear
>loMyClass=createobject('SparseMatrix2D', 'I')
>
>RAND(-1)
>lnStart		= seconds()
>for ln1 = 1 to 1000
>	for ln2 = 1 to 1000
>		lnValue			= IIF(RAND()> 0.8, rand() * 100000, 0) && Will end up with around 200,000 records
>		IF m.lnValue > 0
>			loMyClass.data[m.ln1, m.ln2] = lnValue
>		endif
>	next ln2
>next ln1
>
>? seconds() - lnStart	&& Takes 3 seconds, not the fastest kid in the block :(
>lnStart		= seconds()
>? loMyClass.data[2, 5]
>? seconds() - lnStart
>loMyClass.browse()
>
>
>define class SparseMatrix2D as session
>
>	dimension data(1, 1)
>
>	procedure browse()
>		browse last normal
>	endproc
>
>	procedure Data_assign(m.vNewVal, m.nIndex1, m.nIndex2)
>		if indexseek(bintoc(m.nIndex1) + bintoc(m.nIndex2), .t., 'cData', 'RowCol')
>			replace value with m.vNewVal in cData
>		else
>			insert into cData (row, column, value) values (m.nIndex1, m.nIndex2, m.vNewVal)
>		endif
>	endfunc
>
>	procedure Data_Access(m.nIndex1, m.nIndex2)
>		return iif(indexseek(bintoc(m.nIndex1) + bintoc(m.nIndex2), .t., 'cData', 'RowCol'), cData.value, 0)
>	endfunc
>
>	function init(tcValueType as string) as VOID
>		local lcCommand
>
>		lcCommand		= 'CREATE CURSOR cData (Row I, Column I, Value ' + tcValueType + ')'
>		execscript(lcCommand)
>		index on bintoc(row) + bintoc(column) tag rowcol
>		return .t.
>	endfunc
>enddefine
>
Gregory
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform