/// <summary>
 /// Add coefficients from another group.
 /// </summary>
 /// <param name="otherGroup">Other group of coefficients</param>
 public void addCoeff(CoeffGroup otherGroup)
 {
     if (otherGroup.CoeffCoords.Count > 0) {
         addCoeff(otherGroup.CoeffCoords[0],
             otherGroup.MaxNoiseAmplitude);
     }
 }
        /// <summary>
        /// Distbute coefficients from the OriginalMatrix to perturbation
        /// groups.
        /// </summary>
        private void preparePerturbationGroups()
        {
            // Note: this must be called everytime the original matrix changes!

            List<CoeffGroup> coeffs = new List<CoeffGroup>();
            // make a temporary list of coeffs (groups contain single coeffs)
            OriginalMatrix.apply((int y, int x, double coeff) =>
                {
                    CoeffGroup group = new CoeffGroup();
                    group.addCoeff(new Coordinate<int>(x + OriginalMatrix.SourceOffsetX, y), coeff);
                    coeffs.Add(group);
                }
            );
            // sort the coeffs by their value
            coeffs.Sort((CoeffGroup g1, CoeffGroup g2) =>
                    { return g1.MaxNoiseAmplitude.CompareTo(
                        g2.MaxNoiseAmplitude); } );
            //// or this way (which is only a little bit slower)
            //coeffs.OrderBy((group) => group.MaxNoiseAmplitude);

            // Group the coefficients to pairs, make a group of three if
            // there's an odd coefficient.
            // Each group will carry its minimum value.
            List<CoeffGroup>.Enumerator coeffIter = coeffs.GetEnumerator();
            while (coeffIter.MoveNext()) {
                CoeffGroup group1 = coeffIter.Current;
                if (coeffIter.MoveNext()) {
                    // bind two coeffs together
                    CoeffGroup group2 = coeffIter.Current;
                    group1.addCoeff(group2);
                    _coeffGroups.Add(group1);
                } else if (coeffs.Count > 0) {
                    // a space odd coeff, bind to to the last group
                    // (it will contain 3 coeffs)
                    if (_coeffGroups.Count > 0) {
                        _coeffGroups[_coeffGroups.Count - 1].addCoeff(group1);
                    }
                    // else: single coeff, don't perturb at all
                }
            }
            //// ???
            //foreach (CoeffGroup group in _coeffGroups) {
            //    foreach(Coordinate<int> coords in group.CoeffCoords) {
            //    }
            //}
        }