示例#1
0
 public static int CompareValues(RationalInfo r0, RationalInfo r1)
 {
     return(Powers.Compare(
                r0.rational.GetPrimePowers(),
                r1.rational.GetPrimePowers()
                ));
 }
示例#2
0
        public Fraction ToFraction()
        {
            Fraction f;

            Powers.ToFraction(pows, out f.N, out f.D);
            return(f);
        }
示例#3
0
 public bool Equals(Long nominator, Long denominator)
 {
     if (this.IsDefault())
     {
         return(false);
     }
     return(Powers.Equal(pows, Powers.FromFraction(nominator, denominator)));
 }
示例#4
0
 public bool Equals(Rational r)
 {
     if (r.IsDefault())
     {
         return(this.IsDefault());
     }
     return(Powers.Equal(pows, r.pows));
 }
示例#5
0
 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))
            ));
 }
示例#6
0
 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, "|}"));
 }
示例#7
0
 //
 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());
         }
     }
 }
示例#8
0
        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;
            }
        }
示例#9
0
 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));
     }
 }
示例#10
0
            //!!! 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();
                }
            }
示例#11
0
        /*
         * 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());
                }
            }
        }
示例#12
0
            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();
            }
示例#13
0
 public Rational Clone()
 {
     return(new Rational(Powers.Clone(pows)));
 }
示例#14
0
 public Rational(Long integer)
 {
     this.pows = Powers.FromFraction(integer, 1);
 }
示例#15
0
 public Rational Power(int e)
 {
     return(new Rational(Powers.Power(pows, e)));
 }
示例#16
0
 public Rational(Long nominator, Long denominator)
 {
     this.pows = Powers.FromFraction(nominator, denominator);
 }
示例#17
0
 public static Rational operator /(Rational r0, Rational r1)
 {
     return(new Rational(Powers.Div(r0.pows, r1.pows)));
 }
示例#18
0
 public double ToDouble()
 {
     return(Powers.ToDouble(pows));
 }
示例#19
0
 public Long ToInt()
 {
     return(Powers.ToInt(pows));
 }
示例#20
0
 public string FormatMonzo()
 {
     return(Powers.ToString(pows));
 }
示例#21
0
 public Rational(Rational r)
 {
     this.pows = Powers.Clone(r.pows);
 }
示例#22
0
 public bool IsInteger()
 {
     Pow[] ns, ds;
     Powers.Split(pows, out ns, out ds);
     return(Powers.GetLength(ds) == 0);
 }
示例#23
0
 public override int GetHashCode()
 {
     return(Powers.GetHash(pows));
 }
示例#24
0
 public int GetInvolvedPowerCount()   // HighPrimeIndex - 1
 {
     return(Powers.GetLength(pows));
 }
示例#25
0
 public int GetHighPrimeIndex()
 {
     return(Powers.GetLength(pows) - 1);
 }