public static int CompareValues(RationalInfo r0, RationalInfo r1) { return(Powers.Compare( r0.rational.GetPrimePowers(), r1.rational.GetPrimePowers() )); }
public Fraction ToFraction() { Fraction f; Powers.ToFraction(pows, out f.N, out f.D); return(f); }
public bool Equals(Long nominator, Long denominator) { if (this.IsDefault()) { return(false); } return(Powers.Equal(pows, Powers.FromFraction(nominator, denominator))); }
public bool Equals(Rational r) { if (r.IsDefault()) { return(this.IsDefault()); } return(Powers.Equal(pows, r.pows)); }
public int CompareTo(Rational other) { return(this.IsZero() ? (other.IsZero() ? 0 : -1) : (other.IsZero() ? 1 : this.IsInfinity() ? (other.IsInfinity() ? 0 : 1) : (other.IsInfinity() ? -1 : Powers.Compare(this.pows, other.pows)) )); }
public static string FormatNarrowPowers(this Rational r, Rational[] narrows = null) { if (narrows == null) { narrows = NarrowUtils.GetDefault(r.GetInvolvedPowerCount()); } Pow[] pows = GetNarrowPowers(r, narrows); if (pows == null) { return(null); //!!! where invalid narrowPrimes from? } return(Powers.ToString(pows, "|}")); }
// public RationalGenerator(IHarmonicity harmonicity, Limits limits, Rational[] customBasis = null) { _harmonicity = harmonicity; _limits = limits; if (customBasis != null) { _customBasis = new Pow[customBasis.Length][]; for (int i = 0; i < customBasis.Length; ++i) { _customBasis[i] = Powers.Clone(customBasis[i].GetPrimePowers()); } } }
private static void Solve(int a, int b, out int x, out int y) { // Solve ax + by = 0 bool sameSign = Math.Sign(a) == Math.Sign(b); int[] d = Powers.FromFraction(Math.Abs(b), Math.Abs(a)); int[] d0, d1; Powers.Split(d, out d0, out d1); x = (int)Powers.ToInt(d0); y = (int)Powers.ToInt(d1); if (sameSign) { y = -y; } }
private Rational MakeRational(int[] coordinates) { if (_customBasis == null) { return(new Rational(coordinates)); } else { Pow[] r = new Pow[] { }; int len = Math.Min(coordinates.Length, _customBasis.Length); for (int i = 0; i < len; ++i) { r = Powers.Mul(r, Powers.Power(_customBasis[i], coordinates[i])); } return(new Rational(r)); } }
//!!! standard basis added here public Matrix(Rational[] basis, int vectorLength = -1, int vectorCount = -1, bool makeDiagonal = false) { // if (basis == null) { throw new ArgumentException(); } if (vectorCount == -1) { vectorCount = basis.Length; // other vectors may be invalid } if (vectorLength == -1) { vectorLength = GetMaxLength(basis, vectorCount); } // basisSize = vectorCount; width = basisSize + vectorLength; // we add standard basis here height = vectorLength; // m = new int[width, height]; for (int j = 0; j < basisSize; ++j) { //!!! fail: C# can't copy memory from 1d to 2d - we might use 1d array for matrix! int[] pows = basis[j].GetPrimePowers(); //Array.Copy(basis[i], 0, m, i * height, vectorLength); for (int i = 0; i < vectorLength; ++i) { m[j, i] = Powers.SafeAt(pows, i); } } // add standard basis for (int j = 0; j < vectorLength; ++j) { m[basisSize + j, j] = 1; } Init(); if (makeDiagonal) { MakeEchelon(); ReduceRows(); } }
/* * public static int[] FindCoordinates(Rational[] basis, Rational vector, int vectorLength) * { * int basisSize = basis.Length; * * // get prime powers * int[][] b = new int[basisSize][]; * for (int i = 0; i < basisSize; ++i) { * b[i] = basis[i].GetPrimePowers(); * } * int[] v = vector.GetPrimePowers(); * * // * int[] coordinates = FindCoordinates(b, v, vectorLength); * #if DEBUG * // check result * if (coordinates != null) { * Rational r = Rational.One; * for (int i = 0; i < coordinates.Length; ++i) { * r *= basis[i].Pow(coordinates[i]); * } * if (!r.Equals(vector)) throw new Exception( * String.Format("FindCoordinates failed: {0} != {1}", r, vector) * ); * } #endif * * return coordinates; * } */ #region Tests #if DEBUG private static void CheckVector(Rational[] basis, Rational vector, int vectorLength, bool addStandard = false) { // add standard basis int basisSize = basis.Length; if (addStandard) { Array.Resize(ref basis, basisSize + vectorLength); for (int i = 0; i < vectorLength; ++i) { basis[basisSize + i] = Rational.Prime(i); } } // for (int i = 0; i < basis.Length; ++i) { Debug.WriteLine(String.Format("Basis {0}. {1,-15} {2}", i, basis[i].FormatFraction(), basis[i].FormatMonzo())); } Debug.WriteLine(String.Format("Vector {0,-15} {1}", vector.FormatFraction(), vector.FormatMonzo())); // int[] coords = FindCoordinates(basis, vector, vectorLength); if (coords == null) { Debug.WriteLine("Invalid Basis or Out of basis subspace"); } else { Debug.WriteLine("Coordinates: " + Powers.ToString(coords, "()")); // Debug.WriteLine(vector.FormatFraction() + " = "); for (int i = 0; i < basisSize; ++i) { int e = Powers.SafeAt(coords, i); Debug.Print(" * ({0})^{1}", basis[i].FormatFraction(), e); } if (addStandard) { Rational r = new Rational(coords.Skip(basisSize).ToArray()); Debug.Print(" * {0} {1} {2}", r.FormatFraction(), r.FormatMonzo(), r.FormatNarrowPowers()); } } }
public int[] leadCols; // column -> row // public Matrix(int[][] basis, int[] vector, int vectorLength) { basisSize = basis.Length; width = basisSize + 1; height = vectorLength; // m = new int[width, height]; for (int j = 0; j < basisSize; ++j) { //!!! fail: C# can't copy memory from 1d to 2d - we might use 1d array for matrix! //Array.Copy(basis[i], 0, m, i * height, vectorLength); for (int i = 0; i < vectorLength; ++i) { m[j, i] = Powers.SafeAt(basis[j], i); } } //Array.Copy(vector, 0, m, basisSize * height, vectorLength); for (int i = 0; i < vectorLength; ++i) { m[basisSize, i] = Powers.SafeAt(vector, i); } Init(); }
public Rational Clone() { return(new Rational(Powers.Clone(pows))); }
public Rational(Long integer) { this.pows = Powers.FromFraction(integer, 1); }
public Rational Power(int e) { return(new Rational(Powers.Power(pows, e))); }
public Rational(Long nominator, Long denominator) { this.pows = Powers.FromFraction(nominator, denominator); }
public static Rational operator /(Rational r0, Rational r1) { return(new Rational(Powers.Div(r0.pows, r1.pows))); }
public double ToDouble() { return(Powers.ToDouble(pows)); }
public Long ToInt() { return(Powers.ToInt(pows)); }
public string FormatMonzo() { return(Powers.ToString(pows)); }
public Rational(Rational r) { this.pows = Powers.Clone(r.pows); }
public bool IsInteger() { Pow[] ns, ds; Powers.Split(pows, out ns, out ds); return(Powers.GetLength(ds) == 0); }
public override int GetHashCode() { return(Powers.GetHash(pows)); }
public int GetInvolvedPowerCount() // HighPrimeIndex - 1 { return(Powers.GetLength(pows)); }
public int GetHighPrimeIndex() { return(Powers.GetLength(pows) - 1); }