Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sparse matrices - vfp8
Message
 
To
04/10/2011 03:07:41
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01525329
Message ID:
01525557
Views:
47
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
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform