IMHO, Galik's answer is the closest to what you are trying to accomplish.
Maybe something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
class OneSetOfInputs
{
private:
double m_inputs[100]; // or whatever you want your sample size to be
};
class OneChiSquareTest
{
public:
// arguments need to be between 0 and 7 - I leave it up to you to restrict their ranges
void calculate( unsigned set1Idx, unsigned set2Idx )
{
if ( !alreadyCalculated ) {
const OneSetOfInputs& set1 = myInputs[ set1Idx ];
const OneSetOfInputs& set2 = myInputs[ set2Idx ];
// do work here ----------------------------------------------
// work
// work
alreadyCalculated = true;
}
}
private:
bool alreadyCalculated; // true if we can just use the results
bool test, double chi2_value, double P_value;
};
OneSetOfInputs myInputs[8];
OneChiSquareTest myCache[8][8];
|
With respect to whether you need to cache or not, you should ask yourself:
1. Q: Is the calculation expensive?
A: It could be, in this case. From a theoretical POV, your big O is not small. From a practical standpoint, if your sample size is big and if you have many sets, it could take some time. Best thing is to try some runs before caching.
2. Q: What is the chance you will hit the cache?
A: The probability, could, in fact, be very low. It depends on how you intend to randomize your input sample sets. If the chance of a hit is too low, it's not worth caching. If you intend to use a map, you need to think along the lines of Disch:
Using a double/float as the key for a map seems like a bad idea. It would have all sorts of problems due to floating point error.
|
In fact, your chance of getting a hit on such a cache is practically nil, if it weren't for pseudo-random number generators that depend on non-random seed values. If you don't like the 8x8 and want to do a larger set of inputs, you could do something like this:
1 2 3 4 5 6
|
class InputIdxPair {
private:
unsigned idx1, idx2; // 0 to 499 in this case
};
OneSetOfInputs myInputs[500];
map<InputIdxPair,OneChiSquareTest> myCache;
|
But if you choose to use a large map (maybe sparse), you need to think carefully about your hit probabilities. If you only have 5 or 10 sets of inputs, then you may go for an array. If nSetsOfInputs gets large, maybe you should consider a map.
Most importantly, pay heed to backprop's comments. You need to concern yourself with the Chi squared algorithm first. Implement and test your basic algorithm. What inputs are you trying to randomize? In other words, define your sample sets very carefully.
If you still find it to be too slow, then think about caching.