How can I improve the speed of this code? It runs okay but is quite slow for large datasets, for example, 5 million elements.
ClearAll[filterData];
filterData[dataList_] :=
Module[{totalEntries, removedEntries, remainingEntries,
determinantTest},
determinantTest[{x1_, x2_, x3_, x4_, x5_, x6_, x7_, x8_, x9_, x10_,
x11_, x12_, x13_, x14_, x15_, x16_}] :=
Return[Det[{{1, x1, x2, x5, x4, x8}, {x2, x1, x10, x3, x11,
0}, {x6, x1, x12, x14, x16, x15}, {x13, x2, x4, x6, x5,
x6}, {x2, x1, x3, x14, x10, x15}, {x13, x5, x8, x6, x2,
x6}}] == 0];
totalEntries = Length[dataList];
removedEntries = DeleteCases[dataList, _?determinantTest];
remainingEntries = Complement[dataList, removedEntries];
{totalEntries, removedEntries, remainingEntries}]
(*Example usage:*)
sampleData = RandomInteger[{-1, 1}, {5*10^6, 16}];
result = filterData[sampleData]
DeleteCases
/Complement
thing seems unnecessary. Just useSelect[dataList, determinantTest]
. Or if you really need the removed/remaining thing, then there's alsoGroupBy[dataList, determinantTest]
which gets you an Association instead of a list. $\endgroup$GatherBy
can actually be slower). $\endgroup$RandomInteger
would be a better test case. $\endgroup$