public Rational GetNarrowParent(Rational r) { int lastLevel = r.GetHighPrimeIndex(); if (lastLevel >= _involvedPrimeCount) { return(default(Rational)); } if (lastLevel <= _basePrimeIndex) { return(default(Rational)); // We don't draw lines between base intervals (e.g. 1/2 - 1 - 2 - 4). } // Rational step = _narrows[lastLevel]; // last level step if (step.IsDefault()) { return(default(Rational)); //!!! exception ? } int lastPower = r.GetPrimePower(lastLevel); // last level coordinate if (lastPower > 0) { return(r / step); } else { return(r * step); } }
public HarmonicityNormalizer(IHarmonicity harmonicity, Rational sample = default(Rational)) { _harmonicity = harmonicity; if (sample.IsDefault()) { sample = _defaultSample; } _distanceFactor = 1.0 / _harmonicity.GetDistance(sample); }
// Returns per-row errors public static string[] GetErrors(Tempered[] ts, Subgroup subgroup) { if (ts == null) { return(null); } string[] errors = new string[ts.Length]; Rational[] indep = new Rational[ts.Length]; // independent intervals int indepSize = 0; for (int i = 0; i < ts.Length; ++i) { Rational r = ts[i].rational; string error = null; if (r.IsDefault()) { error = "Invalid rational"; } else if (r.Equals(Rational.One)) { error = "1/1 can't be tempered"; } else if (!subgroup.IsInRange(r)) { error = "Out of JI range"; } else { if (indepSize > 0) { var m = new Vectors.Matrix(indep, -1, indepSize, makeDiagonal: true); var coords = m.FindRationalCoordinates(r); if (coords != null) { error = ""; for (int j = 0; j < coords.Length; ++j) { if (coords[j].sign != 0) { error += String.Format(" * {0}^{1}", indep[j].FormatFraction(), coords[j].FormatFraction()); } } if (error.Length != 0) { error = "Dependend: " + error.Substring(2); } } } indep[indepSize++] = r; } errors[i] = error; } return(errors); }
public void UpdateNarrows(Rational[] userNarrows = null) { _narrows = new Rational[_involvedPrimeCount]; _error = null; // set default narrows foreach (Rational item in _items) { Rational narrow = NarrowUtils.MakeNarrow(item, _baseItem); SetNarrow(narrow); } // set custom user narrows if (userNarrows != null) { var invalidNarrows = new List <Rational>(); foreach (Rational narrow in userNarrows) { if (narrow.IsDefault()) { continue; } if (!IsInRange(narrow)) { invalidNarrows.Add(narrow); } else { SetNarrow(narrow); } } if (invalidNarrows.Count > 0) { _error = "Narrows out of subgroup: " + String.Join(", ", invalidNarrows); } } // !!! he we should check if resulting narrows can solve each generated rational; // currently we add all missing narrows (even if it's out of subgroup) instead. /* * for (int i = 0; i < _narrows.Length; ++i) { * if (_narrows[i].IsDefault()) { * _narrows[i] = NarrowUtils.MakeNarrow(Rational.Prime(i), _baseItem); // default narrow prime * } * } */ #if DEBUG Debug.WriteLine("Narrows set: " + Rational.FormatRationals(_narrows)); #endif }
public static Rational ValidateNarrow(Rational n) { if (n.IsDefault()) { return(n); // invalid } int h = n.GetHighPrimeIndex(); if (h == -1) { return(default(Rational)); // "1" can't be a narrow } if (n.GetPrimePower(h) < 0) // max prime should be in nominator { n = Rational.One / n; } return(n); }
private bool SetNarrow(Rational n) { // make high prime positive n = NarrowUtils.ValidateNarrow(n); if (n.IsDefault()) { return(false); } // set narrow to the array - by high prime index int h = n.GetHighPrimeIndex(); if (0 <= h && h < _narrows.Length) { _narrows[h] = n; return(true); } return(false); }
// Filters out invalid and dependent vectors public static Tempered[] Validate(Tempered[] ts, Subgroup subgroup) { if (ts == null) { return(null); } var result = new List <Tempered>(); Rational[] indep = new Rational[ts.Length]; // independent intervals int indepSize = 0; for (int i = 0; i < ts.Length; ++i) { Rational r = ts[i].rational; if (r.IsDefault()) { continue; } // skip if out of subgroup if (!subgroup.IsInRange(r)) { continue; } // skip if dependend if (indepSize > 0) { var m = new Matrix(indep, -1, indepSize, makeDiagonal: true); if (m.FindCoordinates(r) != null) { continue; } } indep[indepSize++] = r; // result.Add(ts[i]); } return(result.Count == 0 ? null : result.ToArray()); }
public bool IsRational() { return(!rational.IsDefault()); }