Advertisement
Guest User

kommentti2.0

a guest
May 27th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Timers;
  6. using System.Diagnostics;
  7.  
  8.  
  9.  
  10. namespace CsharpTester
  11. {
  12.     //sparseArray on luokka, jolla käydään läpi pelkkiä rivejä tai kolumneja
  13.     class sparseArray<T> : IEnumerable<KeyValuePair<ulong, T>>
  14.     {
  15.        
  16.         public readonly ulong size;
  17.         private ulong trueSize;
  18.         Dictionary<ulong, T> d;
  19.         //Konstruktori
  20.         public sparseArray(ulong size)
  21.         {
  22.             this.d = new Dictionary<ulong, T>();
  23.             this.size = size;
  24.             this.trueSize = 0;
  25.         }
  26.         /*Palauttaa lisättyjen alkioiden määrän
  27.         returns: ulong trueSize
  28.         */
  29.         public ulong realSize()
  30.         {
  31.             return trueSize;
  32.         }
  33.         /*Vaihtoehto get() ja set() metodeille
  34.         @param ulong key
  35.         returns: ulong key
  36.         returns: ulong value
  37.         */
  38.         public T this[ulong key]
  39.         {
  40.             get
  41.             {
  42.                 return get(key);
  43.             }
  44.             set
  45.             {
  46.                 set(key, value);
  47.             }
  48.         }
  49.         /*Asettaa rivin tai kolumnin alkioon arvon
  50.         @param ulong i
  51.         @param T val
  52.         */
  53.         public void set(ulong i, T val)
  54.         {
  55.             if (d.ContainsKey(i))
  56.             {
  57.                 //if val is not initilized or as a value type equal to ""|0|0.0..etc there is no point in adding it as its value is already gettable
  58.                 if (EqualityComparer<T>.Default.Equals(val, default(T)))
  59.                 {
  60.                     d.Remove(i);
  61.                     trueSize--;
  62.                     return;
  63.                 }
  64.                 d[i] = val;
  65.                
  66.             }
  67.             else
  68.             {
  69.                 //if val is not initilized or as a value type equal to ""|0|0.0..etc there is no point in adding it as its value is already gettable
  70.                 if (EqualityComparer<T>.Default.Equals(val, default(T))) return;
  71.                 d.Add(i, val);
  72.                 trueSize++;
  73.             }
  74.  
  75.         }
  76.         /*Palauttaa rivin tai kolumnin alkion arvon
  77.         @param ulong i
  78.         returns: T
  79.         */
  80.         public T get(ulong i)
  81.         {
  82.             if (d.ContainsKey(i)) return d[i];
  83.             return default(T); // for value types this means a value equivalent to zero aka. int 0, string "",float 0.0f
  84.                                // for objects this means a null reference
  85.         }
  86.         /*Käydään läpi koko rivi tai kolumni
  87.         returns: KeyValuePair<ulong, T> item
  88.         */
  89.         public IEnumerator<KeyValuePair<ulong, T>> GetEnumerator()
  90.         {
  91.             d.OrderBy(x => x.Key); //sort O(n log n)
  92.             foreach (KeyValuePair<ulong, T> item in d)
  93.             {
  94.                 yield return item;
  95.             }
  96.         }
  97.  
  98.  
  99.          IEnumerator<KeyValuePair<ulong, T>> IEnumerable<KeyValuePair<ulong, T>>.GetEnumerator()
  100.         {
  101.             d.OrderBy(x => x.Key); //sort O(n log n)
  102.             foreach (KeyValuePair<ulong,T> item in d)
  103.             {
  104.                 yield return item;
  105.             }
  106.         }
  107.  
  108.  
  109.          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  110.          {
  111.              return GetEnumerator();
  112.          }
  113.     }
  114.     //sparseMatrix on luokka, jolla käydään läpi matriiseja
  115.     class sparseMatrix<T> : IEnumerable<KeyValuePair<ulong, sparseArray<T>>>
  116.     {
  117.    
  118.         Dictionary<ulong, sparseArray<T>> dY;
  119.         Dictionary<ulong, sparseArray<T>> dX;
  120.  
  121.         public readonly ulong sizeY;
  122.         public readonly ulong sizeX;
  123.         private ulong trueSizeY;
  124.         private ulong trueSizeX;
  125.        
  126.         //Konstruktori
  127.         public sparseMatrix(ulong y, ulong x)
  128.         {
  129.             this.dY = new Dictionary<ulong, sparseArray<T>>();
  130.             this.dX = new Dictionary<ulong, sparseArray<T>>();
  131.             this.sizeX = x;
  132.             this.sizeY = y;
  133.             trueSizeX = 0;
  134.             trueSizeY = 0;
  135.         }
  136.         /*Palauttaa y -akselille lisättyjen alkioiden määrän
  137.         returns: ulong trueSizeY
  138.         */
  139.         public ulong RealSizeY(){
  140.             return trueSizeY;
  141.         }
  142.         /*Palauttaa x -akselille lisättyjen alkioiden määrän
  143.         returns: ulong trueSizeX
  144.         */
  145.         public ulong RealSizeX()
  146.         {
  147.             return trueSizeX;
  148.         }
  149.         /*Vaihtoehto metodeille get() set()
  150.        
  151.         Esimerkki:
  152.         typeT variable = sparseMatrix[x, y];
  153.         sparseMatrix[x, y] = typeT variable;
  154.        
  155.         @param ulong y
  156.         @param ulong x
  157.         returns: ulong y, ulong x, ulong value
  158.         */
  159.         public T this[ulong y,ulong x]
  160.         {
  161.             get
  162.             {
  163.                 return get(y,x);
  164.             }
  165.             set
  166.             {
  167.                 set(y,x, value);
  168.             }
  169.         }
  170.         /*Asettaa arvon matriisin alkiolle
  171.         @param ulong y
  172.         @param ulong x
  173.         @param T val
  174.         */
  175.         public void set(ulong y, ulong x, T val)
  176.         {
  177.             //if val is not initilized or as a value type equal to ""|0|0.0..etc there is no point in adding it as its value is already gettable
  178.             if (EqualityComparer<T>.Default.Equals(val, default(T))) return;
  179.             if (dY.ContainsKey(y))
  180.             {
  181.                 trueSizeX -= dY[y].realSize();
  182.                 dY[y].set(x, val);
  183.                 trueSizeX += dY[y].realSize();
  184.                 if (dX.ContainsKey(x))
  185.                 {
  186.                     dX[x].set(y, val);
  187.                 }
  188.                 else
  189.                 {
  190.                     dX.Add(x, new sparseArray<T>(sizeY));
  191.                     dX[x].set(y, val);
  192.                 }
  193.             }
  194.             else
  195.             {
  196.                 dY.Add(y, new sparseArray<T>(sizeX));
  197.                 dY[y].set(x, val);
  198.                 if(dY[y].realSize() > 0)trueSizeY++;
  199.                 if (dX.ContainsKey(x))
  200.                 {
  201.                     dX[x].set(y, val);
  202.                 }
  203.                 else
  204.                 {
  205.                     dX.Add(x, new sparseArray<T>(sizeY));
  206.                     dX[x].set(y, val);
  207.                 }
  208.             }
  209.         }
  210.         /*Palauttaa alkion matriisista
  211.         @param ulong y
  212.         @param ulong x
  213.         returns: T
  214.         */
  215.         public T get(ulong y, ulong x)
  216.         {
  217.             if (y > sizeY | x > sizeX)
  218.                 throw new NullReferenceException("@LooseMatrix get");
  219.             if (dY.ContainsKey(y)) return dY[y].get(x);
  220.             return default(T);
  221.         }
  222.         /*Palauttaa rivin alkiot sanakirjana
  223.         @param ulong y
  224.         returns: Dictionary dY[ulong y]
  225.         */
  226.         public sparseArray<T> getRow(ulong y)
  227.         {
  228.             if (dY.ContainsKey(y))
  229.                 return dY[y];
  230.             else
  231.                 return new sparseArray<T>(sizeX);
  232.         }
  233.         /*Palauttaa kolumnin alkiot sanakirjana
  234.         @param ulong y
  235.         returns: Dictionary dY[ulong y]
  236.         */
  237.         public sparseArray<T> getColumn(ulong columnNumber)
  238.         {
  239.             if (dY.ContainsKey(columnNumber))
  240.                 return dX[columnNumber];
  241.             else
  242.                 return new sparseArray<T>(sizeX);
  243.         }
  244.         /*Käy läpi sparseMatrixin
  245.         returns: KeyValuePair<ulong, sparseArray<T>> item
  246.         */
  247.         public IEnumerator<KeyValuePair<ulong, sparseArray<T>>> GetEnumerator()
  248.         {
  249.             dY.OrderBy(x => x.Key);
  250.             foreach (KeyValuePair<ulong, sparseArray<T>> item in dY)
  251.             {
  252.                 yield return item;
  253.             }
  254.         }
  255.  
  256.         IEnumerator<KeyValuePair<ulong, sparseArray<T>>> IEnumerable<KeyValuePair<ulong, sparseArray<T>>>.GetEnumerator()
  257.         {
  258.             dY.OrderBy(x => x.Key);
  259.             foreach (KeyValuePair<ulong, sparseArray<T>> item in dY)
  260.             {
  261.                 yield return item;
  262.             }
  263.         }
  264.  
  265.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  266.         {
  267.             dY.OrderBy(x => x.Key);
  268.             foreach (KeyValuePair<ulong, sparseArray<T>> item in dY)
  269.             {
  270.                 yield return item;
  271.             }
  272.         }
  273.         /*Tulostaa matriisit oikeassa muodossa
  274.         returns String sb
  275.         */
  276.         public string printableString()
  277.         {
  278.             StringBuilder sb = new StringBuilder();
  279.             for (ulong y = 0; y < sizeY; y++)
  280.             {
  281.  
  282.                 sb.Append('[');
  283.                 for (ulong x = 0; x < sizeX; x++)
  284.                 {
  285.                     sb.Append(" " + get(y, x) + " ");
  286.                 }
  287.                 sb.Append("]\n");
  288.             }
  289.             return sb.ToString();
  290.         }
  291.     }
  292.  
  293.     class Program
  294.     {
  295.         static sparseMatrix<ushort> matrixProduct(sparseMatrix<ushort> a, sparseMatrix<ushort> b)
  296.         {
  297.             sparseMatrix<ushort> c = new sparseMatrix<ushort>(Math.Max(a.sizeY, b.sizeY), Math.Max(a.sizeY, b.sizeY));
  298.             for (ulong y = 0; y < c.sizeY; y++)
  299.             {
  300.                sparseArray<ushort> rowY = a.getRow(y);
  301.                 for (ulong x = 0; x < c.sizeX; x++)
  302.                 {
  303.                     sparseArray<ushort> rowX = b.getColumn(x);
  304.                    
  305.                     ushort res = 0;
  306.                     foreach (KeyValuePair<ulong,ushort> kvp in rowY)
  307.                     {
  308.                         res += (ushort) (kvp.Value * rowX[kvp.Key]);
  309.                     }
  310.                     c[y,x] = res;
  311.                 }
  312.             }
  313.  
  314.             return c;
  315.         }
  316.         static void Main(string[] args)
  317.                 {
  318.             Console.WriteLine("Anna matriisien koot (muodossa 3x3(valilyontivaliin)45x245  tai muun muotoinen vastaus jotta ajetaan vertailut normaaleihin matriiseihin");
  319.             String tmp = Console.ReadLine();
  320.             tmp = tmp.Trim();
  321.             string[] kootString = tmp.Split('x',' ');
  322.          
  323.             if (kootString.Length > 3) //on kaksi eri kokoa annettu
  324.             {
  325.                 UInt16[] koot = new ushort[kootString.Length];
  326.                 for (int i = 0; i < kootString.Length; i++)
  327.                 {
  328.                     UInt16.TryParse(kootString[i], out koot[i]);
  329.                 }
  330.                 sparseMatrix<ushort> a = new sparseMatrix<ushort>(koot[0], koot[1]);
  331.                 sparseMatrix<ushort> b = new sparseMatrix<ushort>(koot[2], koot[3]);
  332.                 sparseMatrix<ushort> c;
  333.                 Console.WriteLine("Haluatko syöttää itse numerot? Anna tyhjä vastaus jos et halua");
  334.                 string v = Console.ReadLine();
  335.                 v = v.Trim();
  336.                 if (v != "")
  337.                 {
  338.                     for (int o = 0; o < 2; o++)
  339.                     {
  340.                         sparseMatrix<ushort> current;
  341.                         if (o == 0) current = a;
  342.                         else current = b;
  343.                         Console.WriteLine("Syötä matriisin: " + o + " rivit:");
  344.                         for (ulong i = 0; i < a.sizeY; i++)
  345.                         {
  346.                             Console.WriteLine("Syötä rivi: " + i);
  347.                             string s = Console.ReadLine();
  348.                             s = s.Trim();
  349.                             string[] arvotString = s.Split(' ');
  350.                             UInt16[] arvot = new UInt16[arvotString.Length];
  351.                             for (uint j = 0; j < arvotString.Length; j++)
  352.                             {
  353.                                 UInt16.TryParse(arvotString[j], out koot[j]);
  354.                                 current[i, (ulong)j] = koot[j];
  355.                             }
  356.  
  357.                         }
  358.                     }
  359.                     Console.WriteLine("Matrix 1:");
  360.                     Console.WriteLine(a.printableString());
  361.                     Console.WriteLine("\nMatrix 2:");
  362.                     Console.WriteLine(b.printableString());
  363.                     c = matrixProduct(a, b);
  364.                     //tulostetaan tulos matriisi
  365.                     Console.WriteLine("\nResult Matrix:");
  366.                     Console.WriteLine(c.printableString());
  367.                     Console.ReadKey();
  368.                 }
  369.                 else
  370.                 {
  371.                     byte[] rngBuffer = new byte[sizeof(ushort)];
  372.                     for (int o = 0; o < 2; o++)
  373.                     {
  374.                         sparseMatrix<ushort> current;
  375.                         if (o == 0) current = a;
  376.                         else current = b;
  377.                         Random r = new Random();
  378.                         int rI = r.Next(1, 200);
  379.                         int rI2 = r.Next(1, 200);
  380.                         uint rIu = (uint)(Math.Abs(rI));
  381.                         uint rIu2 = (uint)(Math.Abs(rI));
  382.                         for (ulong y = 0; y < current.sizeY; y+= rIu)
  383.                         {
  384.                             for (ulong x = 0; x < current.sizeX; x += rIu2)
  385.                             {
  386.                                 //generoidaan
  387.                                 r.NextBytes(rngBuffer);
  388.                                 ushort random = BitConverter.ToUInt16(rngBuffer, 0);
  389.                                 current[y, x] = random;
  390.                             }
  391.                         }
  392.                     }
  393.                     //tulostetaan generoidut
  394.                     Console.WriteLine("Matrix 1:");
  395.                     Console.WriteLine(a.printableString());
  396.                     Console.WriteLine("\nMatrix 2:");
  397.                     Console.WriteLine(b.printableString());
  398.                     c = matrixProduct(a, b);
  399.                     //tulostetaan tulos matriisi
  400.                     Console.WriteLine("\nResult Matrix:");
  401.                     Console.WriteLine(c.printableString());
  402.                     Console.ReadKey();
  403.                 }
  404.             }
  405.             else //ajetaan vertailut normaaleihin matriiseihin
  406.             {
  407.                 ulong testSize = 126;
  408.                 ulong fillrate = 100;
  409.                 var asd = new sparseMatrix<ushort>(testSize, testSize);
  410.                 for (int i = 0; i < 7; i++)
  411.                 {
  412.                     testSize = testSize * 2;
  413.                     Random rng = new Random();
  414.                     byte[] rngBuffer = new byte[sizeof(ushort)];
  415.                     var a = new sparseMatrix<ushort>(testSize, testSize);
  416.                     var b = new sparseMatrix<ushort>(testSize, testSize);
  417.                     Stopwatch time = new Stopwatch();
  418.                     time.Restart();
  419.  
  420.                     for (ulong y = 0; y < a.sizeY; y++)
  421.                     {
  422.                         for (ulong x = 0; x < a.sizeX; x++)
  423.                         {
  424.                             if (x % fillrate == 0)
  425.                             {
  426.                                 rng.NextBytes(rngBuffer);
  427.  
  428.                                 ushort u = BitConverter.ToUInt16(rngBuffer, 0);
  429.                                 a.set(y, x, u);
  430.                                 rng.NextBytes(rngBuffer);
  431.                                 u = BitConverter.ToUInt16(rngBuffer, 0);
  432.                                 b.set(y - 1, x - 1, u);
  433.                             }
  434.                         }
  435.                     }
  436.  
  437.                     string populateString = "SPARSE " + time.Elapsed + " population:" + a.RealSizeX();
  438.                     time.Restart();
  439.                     var c = new sparseMatrix<ushort>(a.sizeY, b.sizeX);
  440.                     foreach (KeyValuePair<ulong, sparseArray<ushort>> y in a)
  441.                     {
  442.                         foreach (KeyValuePair<ulong, ushort> x in y.Value)
  443.                         {
  444.                             c.set(y.Key, x.Key, (ushort)(a.get(y.Key, x.Key) + b.get(y.Key, x.Key)));
  445.                         }
  446.                     }
  447.                     string addingString = "SPARSE " + time.Elapsed;
  448.                     time.Restart();
  449.  
  450.                     ushort[][] matA = new ushort[testSize][];
  451.                     ushort[][] matB = new ushort[testSize][];
  452.  
  453.                     for (ulong y = 0; y < testSize; y++)
  454.                     {
  455.                         matA[y] = new ushort[testSize];
  456.                         matB[y] = new ushort[testSize];
  457.                         for (ulong x = 0; x < a.sizeX; x++)
  458.                         {
  459.                             if (x % fillrate == 0)
  460.                             {
  461.                                 rng.NextBytes(rngBuffer);
  462.                                 ushort u = BitConverter.ToUInt16(rngBuffer, 0);
  463.                                 matA[y][x] = u;
  464.                                 rng.NextBytes(rngBuffer);
  465.                                 u = BitConverter.ToUInt16(rngBuffer, 0);
  466.                                 matB[y][x] = u;
  467.                             }
  468.                             else
  469.                             {
  470.                                 matA[y][x] = 0;
  471.                                 matB[y][x] = 0;
  472.                             }
  473.                         }
  474.                     }
  475.                     Console.WriteLine("Populating");
  476.                     Console.WriteLine(populateString);
  477.                     Console.WriteLine("MATRIX " + time.Elapsed + " Size: " + (a.sizeX * a.sizeY));
  478.  
  479.                     time.Restart();
  480.                     {
  481.                         ushort[,] matC = new ushort[testSize, testSize];
  482.                         for (ulong y = 0; y < a.sizeY; y++)
  483.                         {
  484.                             for (ulong x = 0; x < a.sizeX; x++)
  485.                             {
  486.                                 matB[y][x] = (ushort)(matB[y][x] + matA[y][x]);
  487.                             }
  488.                         }
  489.                     }
  490.                     Console.WriteLine("Adding:");
  491.                     Console.WriteLine(addingString);
  492.                     Console.WriteLine("MATRIX " + time.Elapsed);
  493.                     Console.WriteLine();
  494.                     time.Restart();
  495.                 }
  496.  
  497.                 Console.ReadLine();
  498.             }
  499.         }
  500.     }
  501. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement