Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2022
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.43 KB | None | 0 0
  1. /**
  2.  * Author: Kacper Blachut
  3.  * 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.
  4.  *          Counts the number of unique states of nodes in a 3-dimensional cube with side length N and 2 colors using high effort formulas.
  5.  *
  6.  * Program used in "O pewnym problemie przeliczania unikalnych przeksztalcen na zbiorach skonczonych" for Malopolski Konkurs Prac Matematycznych 2022.
  7.  * Doesn't have a UI
  8.  */
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using System.Diagnostics;
  14. using System.Numerics;
  15. using System.IO;
  16.  
  17. namespace RotatingHypercubes
  18. {
  19.     class Equations
  20.     {
  21.        
  22.         public class Version5
  23.         {
  24.             public void PrintValues(int[] Values)
  25.             {
  26.                 Console.Write(Values[0]);
  27.                 for (int i = 1; i < Values.Length; i++)
  28.                 {
  29.                     Console.Write(", " + Values[i]);
  30.                 }
  31.                 Console.WriteLine();
  32.             }
  33.             public void PrintValuesRaw(int[] Values)
  34.             {
  35.                 for (int i = 0; i < Values.Length; i++)
  36.                 {
  37.                     Console.Write(Values[i]);
  38.                 }
  39.             }
  40.             public void PrintValuesRaw(bool[] Values)
  41.             {
  42.                 for (int i = 0; i < Values.Length; i++)
  43.                 {
  44.                     if (Values[i])
  45.                     {
  46.                         Console.Write("1");
  47.                     }
  48.                     else
  49.                     {
  50.                         Console.Write("0");
  51.                     }
  52.                 }
  53.             }
  54.             public void PrintValuesRaw(BoolMatrix Values)
  55.             {
  56.                 for(int i = 0; i < Values.Size; i++)
  57.                 {
  58.                     for (int j = 0; j < Values.Size; j++)
  59.                     {
  60.                         if (Values.Values[i, j])
  61.                         {
  62.                             Console.Write("1");
  63.                         }
  64.                         else
  65.                         {
  66.                             Console.Write("0");
  67.                         }
  68.                     }
  69.                     Console.WriteLine();
  70.                 }
  71.                 Console.WriteLine();
  72.             }
  73.  
  74.             public class RawCube
  75.             {
  76.                 public readonly int Dimension;
  77.                 public int Size;
  78.  
  79.                 public readonly int[] Axes;
  80.                 public readonly bool[] StartingPoint;
  81.  
  82.                 public readonly int TotalPoints;
  83.                 public int[] Values;
  84.  
  85.                 public RawCube(int Dimension, int Size, bool ClearValues = true)
  86.                 {
  87.                     this.Dimension = Dimension;
  88.                     this.Size = Size;
  89.                     this.Axes = new int[this.Dimension];
  90.                     this.StartingPoint = new bool[this.Dimension];
  91.                     this.TotalPoints = (int)MathF.Pow(Size, Dimension);
  92.  
  93.                     for (int i = 0; i < this.Dimension; i++)
  94.                     {
  95.                         Axes[i] = i + 1;
  96.                         StartingPoint[i] = false;
  97.                     }
  98.  
  99.                     this.Values = new int[TotalPoints];
  100.  
  101.                     if (ClearValues)
  102.                     {
  103.                         for (int i = 0; i < this.TotalPoints; i++)
  104.                         {
  105.                             Values[i] = 0;
  106.                         }
  107.                     }
  108.                 }
  109.  
  110.                 public int[] Rotate(int[] NewAxes, bool[] NewStartingPoint)
  111.                 {
  112.                     int[] StepSize = new int[Dimension];
  113.  
  114.                     for (int i = 0; i < Dimension; i++)
  115.                     {
  116.                         StepSize[i] = (int)MathF.Pow(Size, NewAxes[i] - 1);
  117.                     }
  118.  
  119.  
  120.                     int StartingPointIndex = 0;
  121.  
  122.                     for (int i = 0; i < Dimension; i++)
  123.                     {
  124.                         if (NewStartingPoint[i])
  125.                         {
  126.                             StartingPointIndex += (Size - 1) * (int)MathF.Pow(Size, NewAxes[i] - 1);
  127.                         }
  128.                     }
  129.  
  130.  
  131.                     int[] NewValues = new int[TotalPoints];
  132.  
  133.                     for (int i = 0; i < TotalPoints; i++)
  134.                     {
  135.                         int TotalStep = 0;
  136.                         int[] StepValues = new int[Dimension];
  137.                         int PointsRemaining = i;
  138.  
  139.                         for (int j = 0; j < Dimension; j++)
  140.                         {
  141.                             StepValues[j] = PointsRemaining % Size;
  142.                             PointsRemaining -= StepValues[j];
  143.                             PointsRemaining /= Size;
  144.  
  145.                             if (NewStartingPoint[j])
  146.                             {
  147.                                 TotalStep -= StepValues[j] * StepSize[j];
  148.                             }
  149.                             else
  150.                             {
  151.                                 TotalStep += StepValues[j] * StepSize[j];
  152.                             }
  153.                         }
  154.  
  155.                         NewValues[i] = Values[StartingPointIndex + TotalStep];
  156.                     }
  157.  
  158.  
  159.                     return NewValues;
  160.                 }
  161.             }
  162.             public class BoolMatrix
  163.             {
  164.                 public bool[,] Values;
  165.                 public readonly int Size;
  166.  
  167.                 public BoolMatrix(int[] NewValues)
  168.                 {
  169.                     this.Values = new bool[NewValues.Length, NewValues.Length];
  170.                     this.Size = NewValues.Length;
  171.                     for(int i = 0; i < Size; i++)
  172.                     {
  173.                         this.Values[i, NewValues[i]] = true;
  174.                     }
  175.                 }
  176.                 public BoolMatrix(bool[,] NewValues)
  177.                 {
  178.                     Values = NewValues;
  179.                     Size = (int) MathF.Sqrt(NewValues.Length);
  180.                 }
  181.                 public BoolMatrix(int Size)
  182.                 {
  183.                     this.Size = Size;
  184.                     this.Values = new bool[Size, Size];
  185.                 }
  186.  
  187.                 public int Trace()
  188.                 {
  189.                     int Total = 0;
  190.                     for(int i = 0; i < Size; i++)
  191.                     {
  192.                         if(Values[i, i])
  193.                         {
  194.                             Total++;
  195.                         }
  196.                     }
  197.                     return Total;
  198.                 }
  199.                 public BoolMatrix Multiply(BoolMatrix B)
  200.                 {
  201.                     BoolMatrix A = new BoolMatrix(this.Values);
  202.                     BoolMatrix Result = new BoolMatrix(A.Size);
  203.                     if(A.Size == B.Size)
  204.                     {
  205.                         for(int i = 0; i < A.Size; i++)
  206.                         {
  207.                             for (int j = 0; j < A.Size; j++)
  208.                             {
  209.                                 for (int k = 0; k < A.Size; k++)
  210.                                 {
  211.                                     if(A.Values[i,k])
  212.                                     {
  213.                                         for(int l = 0; l < A.Size; l++)
  214.                                         {
  215.                                             if(B.Values[k,j])
  216.                                             {
  217.                                                 Result.Values[i, j] = true;
  218.                                                 l = A.Size; k = A.Size;
  219.                                             }
  220.                                         }
  221.                                     }
  222.                                 }
  223.                             }
  224.                         }
  225.                     }
  226.  
  227.                     return Result;
  228.                 }
  229.                 public BoolMatrix Power(int Exponent)
  230.                 {
  231.                     BoolMatrix Result = this;
  232.                     if(Exponent > 0)
  233.                     {
  234.                         for(int i = 1; i < Exponent; i++)
  235.                         {
  236.                             Result = Result.Multiply(this);
  237.                         }
  238.                     }
  239.                     return Result;
  240.                 }
  241.  
  242.             }
  243.  
  244.             public BigInteger Pow(BigInteger a, BigInteger b)
  245.             {
  246.                 BigInteger Total = a;
  247.                
  248.                 for(BigInteger i = 1; i < b; i++)
  249.                 {
  250.                     Total *= a;
  251.                 }
  252.  
  253.                 return Total;
  254.             }
  255.  
  256.             public void Count(int Dimension, int Size, int Colors)
  257.             {
  258.                 int[] UnitValues = new int[(int)MathF.Pow(Size, Dimension)];
  259.                 for (int i = 0; i < UnitValues.Length; i++)
  260.                 {
  261.                     UnitValues[i] = i;
  262.                 }
  263.  
  264.  
  265.                 int StartPointSymmetryNum = (int)MathF.Pow(2, Dimension);
  266.                 int AxesSymmetryNum = 1;
  267.                 for (int i = 2; i <= Dimension; i++)
  268.                 {
  269.                     AxesSymmetryNum *= i;
  270.                 }
  271.  
  272.  
  273.                 BoolMatrix[,] AllSymmetriesMatrices = new BoolMatrix[StartPointSymmetryNum, AxesSymmetryNum];
  274.                 RawCube Cube = new RawCube(Dimension, Size);
  275.                 Cube.Values = UnitValues;
  276.                 int[] CurrentAxes = Cube.Axes;
  277.                 bool[] CurrentStartingPoint = Cube.StartingPoint;
  278.                 for (int i = 0; i < StartPointSymmetryNum; i++)
  279.                 {
  280.                     for (int j = 0; j < AxesSymmetryNum; j++)
  281.                     {
  282.                         int[] Temp = Cube.Rotate(CurrentAxes, CurrentStartingPoint);
  283.                         AllSymmetriesMatrices[i, j] = new BoolMatrix(Temp);
  284.  
  285.                         CurrentAxes = IncrementAxes(CurrentAxes, Dimension, Size);
  286.                     }
  287.  
  288.                     CurrentStartingPoint = IncrementStartingPoint(CurrentStartingPoint);
  289.                 }
  290.  
  291.  
  292.                 BigInteger Total = 0;
  293.                 for(int i = 0; i < StartPointSymmetryNum; i++)
  294.                 {
  295.                     for(int j = 0; j < AxesSymmetryNum; j++)
  296.                     {
  297.                         BigInteger Power = 0;
  298.                         for (int l = 1; l <= 2 * Dimension; l++)
  299.                         {
  300.                             Power += FixedPoints(AllSymmetriesMatrices[i, j], l);
  301.                         }
  302.                         Total += Pow(Colors, Power);
  303.  
  304.                         Console.WriteLine(i + ", " + j + " / " + StartPointSymmetryNum + ", " + AxesSymmetryNum);
  305.                     }
  306.                    
  307.                 }
  308.  
  309.                 Total /= AllSymmetriesMatrices.Length;
  310.                 Console.WriteLine("Total: " + Total);
  311.             }
  312.  
  313.             public bool[] IncrementStartingPoint(bool[] StartingPoint)
  314.             {
  315.                 for (int i = 0; i < StartingPoint.Length; i++)
  316.                 {
  317.                     if (!StartingPoint[i])
  318.                     {
  319.                         StartingPoint[i] = true;
  320.  
  321.                         return StartingPoint;
  322.                     }
  323.                     else
  324.                     {
  325.                         StartingPoint[i] = false;
  326.                     }
  327.                 }
  328.  
  329.                 return StartingPoint;
  330.             }
  331.             public int[] IncrementAxes(int[] Axes, int Dimension, int Size)
  332.             {
  333.                 int A = Axes[Axes.Length - 1];
  334.                 int IndexA;
  335.                 int IndexB;
  336.                 int Temp;
  337.  
  338.                 for (int i = Axes.Length - 2; i >= 0; i--)
  339.                 {
  340.                     if (Axes[i] < A)
  341.                     {
  342.                         A = Axes[i];
  343.                         IndexA = i;
  344.  
  345.                         for (int j = Axes.Length - 1; j >= 0; j--)
  346.                         {
  347.                             if (Axes[j] > A)
  348.                             {
  349.                                 IndexB = j;
  350.                                 Temp = Axes[IndexA];
  351.                                 Axes[IndexA] = Axes[IndexB];
  352.                                 Axes[IndexB] = Temp;
  353.  
  354.                                 int l = Axes.Length - 1;
  355.  
  356.                                 for (int k = IndexA + 1; k < l; k++, l--)
  357.                                 {
  358.                                     Temp = Axes[k];
  359.                                     Axes[k] = Axes[l];
  360.                                     Axes[l] = Temp;
  361.                                 }
  362.                                 return Axes;
  363.                             }
  364.                         }
  365.                     }
  366.                     else
  367.                     {
  368.                         A = Axes[i];
  369.                     }
  370.                 }
  371.  
  372.                 RawCube Identity = new RawCube(Dimension, Size);
  373.                 return Identity.Axes;
  374.             }
  375.  
  376.             public BigInteger FixedPoints(BoolMatrix Matrix, int Period)
  377.             {
  378.                 BigInteger Total = Matrix.Power(Period).Trace();
  379.  
  380.                 for(int d = 1; d < Period; d++)
  381.                 {
  382.                     if(Period % d == 0)
  383.                     {
  384.                         Total -= d * FixedPoints(Matrix, d);
  385.                     }
  386.                 }
  387.                 Total /= Period;
  388.  
  389.                 return Total;
  390.             }
  391.  
  392.         }
  393.  
  394.         public class Version6
  395.         {
  396.             //D = 3, C = 2
  397.  
  398.             public BigInteger Pow(BigInteger a, BigInteger b)
  399.             {
  400.                 BigInteger Total = a;
  401.  
  402.                 for (BigInteger i = 1; i < b; i++)
  403.                 {
  404.                     Total *= a;
  405.                 }
  406.  
  407.                 return Total;
  408.             }
  409.  
  410.             public BigInteger Count(int n)
  411.             {
  412.                 BigInteger Result;
  413.  
  414.                 if (n % 2 == 0)
  415.                 {
  416.                     //Even case
  417.                     BigInteger a1 = Pow(2, Pow(n, 3));
  418.                     BigInteger a2 = 24 * Pow(2, Pow(n, 3) / 4);
  419.                     BigInteger a3 = 26 * Pow(2, Pow(n, 3) / 2);
  420.                     BigInteger a4 = 8 * Pow(2, (Pow(n, 3) + 2 * n) / 3);
  421.                     BigInteger a5 = 6 * Pow(2, (Pow(n, 3) + Pow(n, 2)) / 2);
  422.                     BigInteger a6 = 16 * Pow(2, (Pow(n, 3) + 2 * n) / 6);
  423.  
  424.                     Result = (a1 + a2 + a3 + a4 + a5 + a6) / 96;
  425.  
  426.                     // 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)))
  427.                     // 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));
  428.                 }
  429.                 else
  430.                 {
  431.                     //Odd case
  432.                     BigInteger b1 = Pow(2, Pow(n, 3));
  433.                     BigInteger b2 = 6 * Pow(2, (Pow(n, 3) + 3 * n) / 4);
  434.                     BigInteger b3 = 9 * Pow(2, (Pow(n, 3) + n) / 2);
  435.                     BigInteger b4 = 8 * Pow(2, (Pow(n, 3) + 2 * n) / 3);
  436.                     BigInteger b5 = 9 * Pow(2, (Pow(n, 3) + Pow(n, 2)) / 2);
  437.                     BigInteger b6 = 6 * Pow(2, (Pow(n, 3) + n + 2) / 4);
  438.                     BigInteger b7 = Pow(2, (Pow(n, 3) + 1) / 2);
  439.                     BigInteger b8 = 8 * Pow(2, (Pow(n, 3) + 2 * n + 3) / 6);
  440.  
  441.                     Result = (b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8) / 96;
  442.  
  443.                     // (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
  444.                    
  445.                 }
  446.  
  447.                 return Result;
  448.  
  449.                 //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
  450.                 //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
  451.             }
  452.  
  453.             public void CountUntil(int n, string TermSeparator)
  454.             {
  455.                 for(int i = 1; i < n; i++)
  456.                 {
  457.                     Console.WriteLine(Count(i) + TermSeparator);
  458.                 }
  459.                 Console.WriteLine(Count(n));
  460.             }
  461.  
  462.             public void CountWrite(int n)
  463.             {
  464.                 string fileName = @"D:\C#\b350891.txt";
  465.  
  466.                 try
  467.                 {
  468.                     // Check if file already exists. If yes, delete it.    
  469.                     if (File.Exists(fileName))
  470.                     {
  471.                         File.Delete(fileName);
  472.                     }
  473.  
  474.                     // Create a new file    
  475.                     using (FileStream fs = File.Create(fileName))
  476.                     {
  477.                         // Add some text to file    
  478.                         for(int i = 1; i <= n; i++)
  479.                         {
  480.                             string Text = i + " " + Count(i) + "\n";
  481.                             Byte[] text = new UTF8Encoding(true).GetBytes(Text);
  482.                             Console.WriteLine(Text);
  483.                             fs.Write(text, 0, text.Length);
  484.                         }
  485.                        
  486.                     }
  487.  
  488.                     // Open the stream and read it back.    
  489.                     using (StreamReader sr = File.OpenText(fileName))
  490.                     {
  491.                         string s = "";
  492.                         while ((s = sr.ReadLine()) != null)
  493.                         {
  494.                             Console.WriteLine(s);
  495.                         }
  496.                     }
  497.                 }
  498.                 catch (Exception Ex)
  499.                 {
  500.                     Console.WriteLine(Ex.ToString());
  501.                 }
  502.             }
  503.         }
  504.         static void Main(string[] args)
  505.         {
  506.             //DOESNT DO COLOR SYMMETRY
  507.  
  508.             Version5 Equation = new Version5();
  509.             Stopwatch Timer = new Stopwatch();
  510.             Timer.Start();
  511.  
  512.             Equation.Count(2, 4, 2);
  513.  
  514.             Timer.Stop();
  515.             Console.WriteLine("\n\nOperation Took: " + Timer.ElapsedMilliseconds / 1000f + "s");
  516.         }
  517.     }
  518. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement