Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Author: Kacper Blachut
- * Purpose: Counts the number of unique states of nodes in a D-dimensional cube with side length N and C colors using low effort formulas.
- * Counts the number of unique states of nodes in a 3-dimensional cube with side length N and 2 colors using high effort formulas.
- *
- * Program used in "O pewnym problemie przeliczania unikalnych przeksztalcen na zbiorach skonczonych" for Malopolski Konkurs Prac Matematycznych 2022.
- * Doesn't have a UI
- */
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- using System.Numerics;
- using System.IO;
- namespace RotatingHypercubes
- {
- class Equations
- {
- public class Version5
- {
- public void PrintValues(int[] Values)
- {
- Console.Write(Values[0]);
- for (int i = 1; i < Values.Length; i++)
- {
- Console.Write(", " + Values[i]);
- }
- Console.WriteLine();
- }
- public void PrintValuesRaw(int[] Values)
- {
- for (int i = 0; i < Values.Length; i++)
- {
- Console.Write(Values[i]);
- }
- }
- public void PrintValuesRaw(bool[] Values)
- {
- for (int i = 0; i < Values.Length; i++)
- {
- if (Values[i])
- {
- Console.Write("1");
- }
- else
- {
- Console.Write("0");
- }
- }
- }
- public void PrintValuesRaw(BoolMatrix Values)
- {
- for(int i = 0; i < Values.Size; i++)
- {
- for (int j = 0; j < Values.Size; j++)
- {
- if (Values.Values[i, j])
- {
- Console.Write("1");
- }
- else
- {
- Console.Write("0");
- }
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- public class RawCube
- {
- public readonly int Dimension;
- public int Size;
- public readonly int[] Axes;
- public readonly bool[] StartingPoint;
- public readonly int TotalPoints;
- public int[] Values;
- public RawCube(int Dimension, int Size, bool ClearValues = true)
- {
- this.Dimension = Dimension;
- this.Size = Size;
- this.Axes = new int[this.Dimension];
- this.StartingPoint = new bool[this.Dimension];
- this.TotalPoints = (int)MathF.Pow(Size, Dimension);
- for (int i = 0; i < this.Dimension; i++)
- {
- Axes[i] = i + 1;
- StartingPoint[i] = false;
- }
- this.Values = new int[TotalPoints];
- if (ClearValues)
- {
- for (int i = 0; i < this.TotalPoints; i++)
- {
- Values[i] = 0;
- }
- }
- }
- public int[] Rotate(int[] NewAxes, bool[] NewStartingPoint)
- {
- int[] StepSize = new int[Dimension];
- for (int i = 0; i < Dimension; i++)
- {
- StepSize[i] = (int)MathF.Pow(Size, NewAxes[i] - 1);
- }
- int StartingPointIndex = 0;
- for (int i = 0; i < Dimension; i++)
- {
- if (NewStartingPoint[i])
- {
- StartingPointIndex += (Size - 1) * (int)MathF.Pow(Size, NewAxes[i] - 1);
- }
- }
- int[] NewValues = new int[TotalPoints];
- for (int i = 0; i < TotalPoints; i++)
- {
- int TotalStep = 0;
- int[] StepValues = new int[Dimension];
- int PointsRemaining = i;
- for (int j = 0; j < Dimension; j++)
- {
- StepValues[j] = PointsRemaining % Size;
- PointsRemaining -= StepValues[j];
- PointsRemaining /= Size;
- if (NewStartingPoint[j])
- {
- TotalStep -= StepValues[j] * StepSize[j];
- }
- else
- {
- TotalStep += StepValues[j] * StepSize[j];
- }
- }
- NewValues[i] = Values[StartingPointIndex + TotalStep];
- }
- return NewValues;
- }
- }
- public class BoolMatrix
- {
- public bool[,] Values;
- public readonly int Size;
- public BoolMatrix(int[] NewValues)
- {
- this.Values = new bool[NewValues.Length, NewValues.Length];
- this.Size = NewValues.Length;
- for(int i = 0; i < Size; i++)
- {
- this.Values[i, NewValues[i]] = true;
- }
- }
- public BoolMatrix(bool[,] NewValues)
- {
- Values = NewValues;
- Size = (int) MathF.Sqrt(NewValues.Length);
- }
- public BoolMatrix(int Size)
- {
- this.Size = Size;
- this.Values = new bool[Size, Size];
- }
- public int Trace()
- {
- int Total = 0;
- for(int i = 0; i < Size; i++)
- {
- if(Values[i, i])
- {
- Total++;
- }
- }
- return Total;
- }
- public BoolMatrix Multiply(BoolMatrix B)
- {
- BoolMatrix A = new BoolMatrix(this.Values);
- BoolMatrix Result = new BoolMatrix(A.Size);
- if(A.Size == B.Size)
- {
- for(int i = 0; i < A.Size; i++)
- {
- for (int j = 0; j < A.Size; j++)
- {
- for (int k = 0; k < A.Size; k++)
- {
- if(A.Values[i,k])
- {
- for(int l = 0; l < A.Size; l++)
- {
- if(B.Values[k,j])
- {
- Result.Values[i, j] = true;
- l = A.Size; k = A.Size;
- }
- }
- }
- }
- }
- }
- }
- return Result;
- }
- public BoolMatrix Power(int Exponent)
- {
- BoolMatrix Result = this;
- if(Exponent > 0)
- {
- for(int i = 1; i < Exponent; i++)
- {
- Result = Result.Multiply(this);
- }
- }
- return Result;
- }
- }
- public BigInteger Pow(BigInteger a, BigInteger b)
- {
- BigInteger Total = a;
- for(BigInteger i = 1; i < b; i++)
- {
- Total *= a;
- }
- return Total;
- }
- public void Count(int Dimension, int Size, int Colors)
- {
- int[] UnitValues = new int[(int)MathF.Pow(Size, Dimension)];
- for (int i = 0; i < UnitValues.Length; i++)
- {
- UnitValues[i] = i;
- }
- int StartPointSymmetryNum = (int)MathF.Pow(2, Dimension);
- int AxesSymmetryNum = 1;
- for (int i = 2; i <= Dimension; i++)
- {
- AxesSymmetryNum *= i;
- }
- BoolMatrix[,] AllSymmetriesMatrices = new BoolMatrix[StartPointSymmetryNum, AxesSymmetryNum];
- RawCube Cube = new RawCube(Dimension, Size);
- Cube.Values = UnitValues;
- int[] CurrentAxes = Cube.Axes;
- bool[] CurrentStartingPoint = Cube.StartingPoint;
- for (int i = 0; i < StartPointSymmetryNum; i++)
- {
- for (int j = 0; j < AxesSymmetryNum; j++)
- {
- int[] Temp = Cube.Rotate(CurrentAxes, CurrentStartingPoint);
- AllSymmetriesMatrices[i, j] = new BoolMatrix(Temp);
- CurrentAxes = IncrementAxes(CurrentAxes, Dimension, Size);
- }
- CurrentStartingPoint = IncrementStartingPoint(CurrentStartingPoint);
- }
- BigInteger Total = 0;
- for(int i = 0; i < StartPointSymmetryNum; i++)
- {
- for(int j = 0; j < AxesSymmetryNum; j++)
- {
- BigInteger Power = 0;
- for (int l = 1; l <= 2 * Dimension; l++)
- {
- Power += FixedPoints(AllSymmetriesMatrices[i, j], l);
- }
- Total += Pow(Colors, Power);
- Console.WriteLine(i + ", " + j + " / " + StartPointSymmetryNum + ", " + AxesSymmetryNum);
- }
- }
- Total /= AllSymmetriesMatrices.Length;
- Console.WriteLine("Total: " + Total);
- }
- public bool[] IncrementStartingPoint(bool[] StartingPoint)
- {
- for (int i = 0; i < StartingPoint.Length; i++)
- {
- if (!StartingPoint[i])
- {
- StartingPoint[i] = true;
- return StartingPoint;
- }
- else
- {
- StartingPoint[i] = false;
- }
- }
- return StartingPoint;
- }
- public int[] IncrementAxes(int[] Axes, int Dimension, int Size)
- {
- int A = Axes[Axes.Length - 1];
- int IndexA;
- int IndexB;
- int Temp;
- for (int i = Axes.Length - 2; i >= 0; i--)
- {
- if (Axes[i] < A)
- {
- A = Axes[i];
- IndexA = i;
- for (int j = Axes.Length - 1; j >= 0; j--)
- {
- if (Axes[j] > A)
- {
- IndexB = j;
- Temp = Axes[IndexA];
- Axes[IndexA] = Axes[IndexB];
- Axes[IndexB] = Temp;
- int l = Axes.Length - 1;
- for (int k = IndexA + 1; k < l; k++, l--)
- {
- Temp = Axes[k];
- Axes[k] = Axes[l];
- Axes[l] = Temp;
- }
- return Axes;
- }
- }
- }
- else
- {
- A = Axes[i];
- }
- }
- RawCube Identity = new RawCube(Dimension, Size);
- return Identity.Axes;
- }
- public BigInteger FixedPoints(BoolMatrix Matrix, int Period)
- {
- BigInteger Total = Matrix.Power(Period).Trace();
- for(int d = 1; d < Period; d++)
- {
- if(Period % d == 0)
- {
- Total -= d * FixedPoints(Matrix, d);
- }
- }
- Total /= Period;
- return Total;
- }
- }
- public class Version6
- {
- //D = 3, C = 2
- public BigInteger Pow(BigInteger a, BigInteger b)
- {
- BigInteger Total = a;
- for (BigInteger i = 1; i < b; i++)
- {
- Total *= a;
- }
- return Total;
- }
- public BigInteger Count(int n)
- {
- BigInteger Result;
- if (n % 2 == 0)
- {
- //Even case
- BigInteger a1 = Pow(2, Pow(n, 3));
- BigInteger a2 = 24 * Pow(2, Pow(n, 3) / 4);
- BigInteger a3 = 26 * Pow(2, Pow(n, 3) / 2);
- BigInteger a4 = 8 * Pow(2, (Pow(n, 3) + 2 * n) / 3);
- BigInteger a5 = 6 * Pow(2, (Pow(n, 3) + Pow(n, 2)) / 2);
- BigInteger a6 = 16 * Pow(2, (Pow(n, 3) + 2 * n) / 6);
- Result = (a1 + a2 + a3 + a4 + a5 + a6) / 96;
- // 1/96 (2^(k^3) + 24×2^(k^3/4) + 26×2^(k^3/2) + 8×2^(1/3 (k^3 + 2 k)) + 6×2^(1/2 (k^3 + k^2)) + 16×2^(1/6 (k^3 + 2 k)))
- // 1/96 (2^(8 n^3) + 3 2^(2 n^3 + 3) + 13 2^(4 n^3 + 1) + 2^(1/6 (8 n^3 + 4 n) + 4) + 2^(1/3 (8 n^3 + 4 n) + 3) + 3 2^(1/2 (8 n^3 + 4 n^2) + 1));
- }
- else
- {
- //Odd case
- BigInteger b1 = Pow(2, Pow(n, 3));
- BigInteger b2 = 6 * Pow(2, (Pow(n, 3) + 3 * n) / 4);
- BigInteger b3 = 9 * Pow(2, (Pow(n, 3) + n) / 2);
- BigInteger b4 = 8 * Pow(2, (Pow(n, 3) + 2 * n) / 3);
- BigInteger b5 = 9 * Pow(2, (Pow(n, 3) + Pow(n, 2)) / 2);
- BigInteger b6 = 6 * Pow(2, (Pow(n, 3) + n + 2) / 4);
- BigInteger b7 = Pow(2, (Pow(n, 3) + 1) / 2);
- BigInteger b8 = 8 * Pow(2, (Pow(n, 3) + 2 * n + 3) / 6);
- Result = (b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8) / 96;
- // (2^(8n^3+12n^2+6n+1)+6×2^(2n^3+3n^2+4n+1)+9×2^(4n^3+6n^2+4n+1)+8×2^((8n^3+12n^2+10n+3)/3)+9×2^(4n^3+8n^2+5n+1)+6×2^(2n^3+3n^2+2n+1)+2^(4n^3+6n^2+3n+1)+8×2^((4n^3+6n^2+5n+3)/3))/96
- }
- return Result;
- //a(2n) = (2^(8*n^3) + 3*2^(2*n^3 + 3) + 13*2^(4*n^3 + 1) + 2^((4*n^3 + 2*n + 12)/3) + 2^((8*n^3 + 4*n + 9)/3) + 3*2^(4*n^3 + 2*n^2 + 1))/96
- //a(2n+1) = (2 ^ (8*n ^ 3 + 12*n ^ 2 + 6*n + 1) + 6*2 ^ (2*n ^ 3 + 3*n ^ 2 + 4*n + 1) + 9*2 ^ (4*n ^ 3 + 6*n ^ 2 + 4*n + 1) + 8*2 ^ ((8*n ^ 3 + 12*n ^ 2 + 10*n + 3) / 3) + 9*2 ^ (4*n ^ 3 + 8*n ^ 2 + 5*n + 1) + 6*2 ^ (2*n ^ 3 + 3*n ^ 2 + 2*n + 1) + 2 ^ (4*n ^ 3 + 6*n ^ 2 + 3*n + 1) + 8*2 ^ ((4*n ^ 3 + 6*n ^ 2 + 5*n + 3) / 3)) / 96
- }
- public void CountUntil(int n, string TermSeparator)
- {
- for(int i = 1; i < n; i++)
- {
- Console.WriteLine(Count(i) + TermSeparator);
- }
- Console.WriteLine(Count(n));
- }
- public void CountWrite(int n)
- {
- string fileName = @"D:\C#\b350891.txt";
- try
- {
- // Check if file already exists. If yes, delete it.
- if (File.Exists(fileName))
- {
- File.Delete(fileName);
- }
- // Create a new file
- using (FileStream fs = File.Create(fileName))
- {
- // Add some text to file
- for(int i = 1; i <= n; i++)
- {
- string Text = i + " " + Count(i) + "\n";
- Byte[] text = new UTF8Encoding(true).GetBytes(Text);
- Console.WriteLine(Text);
- fs.Write(text, 0, text.Length);
- }
- }
- // Open the stream and read it back.
- using (StreamReader sr = File.OpenText(fileName))
- {
- string s = "";
- while ((s = sr.ReadLine()) != null)
- {
- Console.WriteLine(s);
- }
- }
- }
- catch (Exception Ex)
- {
- Console.WriteLine(Ex.ToString());
- }
- }
- }
- static void Main(string[] args)
- {
- //DOESNT DO COLOR SYMMETRY
- Version5 Equation = new Version5();
- Stopwatch Timer = new Stopwatch();
- Timer.Start();
- Equation.Count(2, 4, 2);
- Timer.Stop();
- Console.WriteLine("\n\nOperation Took: " + Timer.ElapsedMilliseconds / 1000f + "s");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement