Beispiel #1
0
    public static void Bench()
    {
        int n = 100;

        foreach (var iteration in Benchmark.Iterations)
        {
            double a = 0;

            using (iteration.StartMeasurement())
            {
                for (int i = 0; i < Iterations; i++)
                {
                    SpectralNorm s = new SpectralNorm();
                    a += s.Approximate(n);
                }
            }

            double norm     = a / Iterations;
            double expected = 1.274219991;
            bool   valid    = Math.Abs(norm - expected) < 1e-4;
            if (!valid)
            {
                throw new Exception("Benchmark failed to validate");
            }
        }
    }
Beispiel #2
0
    public static void Bench()
    {
        int n = 100;
        foreach (var iteration in Benchmark.Iterations)
        {
            double a = 0;

            using (iteration.StartMeasurement())
            {
                for (int i = 0; i < Iterations; i++)
                {
                    SpectralNorm s = new SpectralNorm();
                    a += s.Approximate(n);
                }
            }

            double norm = a / Iterations;
            double expected = 1.274219991;
            bool valid = Math.Abs(norm - expected) < 1e-4;
            if (!valid)
            {
                throw new Exception("Benchmark failed to validate");
            }
        }
    }
Beispiel #3
0
 public static int Main(String[] args)
 {
     int n = 100;
     if (args.Length > 0) n = Int32.Parse(args[0]);
     double norm = new SpectralNorm().Approximate(n);
     Console.WriteLine("Norm={0:f9}", norm);
     double expected = 1.274219991;
     bool result = Math.Abs(norm - expected) < 1e-4;
     return (result ? 100 : -1);
 }
Beispiel #4
0
    public static int Main(String[] args)
    {
        int n = 100;

        if (args.Length > 0)
        {
            n = Int32.Parse(args[0]);
        }
        double norm = new SpectralNorm().Approximate(n);

        Console.WriteLine("Norm={0:f9}", norm);
        double expected = 1.274219991;
        bool   result   = Math.Abs(norm - expected) < 1e-4;

        return(result ? 100 : -1);
    }
Beispiel #5
0
        public static void Main(string[] args)
        {
            var target = "C#";

            if (args.Length > 0)
            {
                target = args[0];
            }
            Console.WriteLine($"============ {target} ============");
            {
                Console.WriteLine("##### N-Body #####");
                NBodySystem dummy = new NBodySystem();
                dummy.Advance(0.01);

                Func <Int32, TimeSpan> test = (ti) =>
                {
                    NBodySystem bodies = new NBodySystem();
                    var         e      = new Elapsed("11");
                    for (int i = 0; i < 50000000; i++)
                    {
                        bodies.Advance(0.01);
                    }
                    var d = e.Done();
                    Console.WriteLine($"Try #{ti} : {d}, {bodies.Energy()}");
                    return(d);
                };

                var times0 = new List <TimeSpan>(10);
                for (var i = 0; i < 10; i++)
                {
                    times0.Add(test(i));
                }

                var times   = times0.Where(x => x != times0.Min() && x != times0.Max()).ToList();
                var average = new TimeSpan((long)times.Select(ts => ts.Ticks).Average());
                //Console.WriteLine($"{target} [N-Body] : {average}\n");
                using (StreamWriter sw = File.AppendText("averages.txt"))
                {
                    sw.WriteLine($"{target} [N-Body] : {average}");
                }
            }
            {
                Console.WriteLine("##### Spectral-Norm #####");
                SpectralNorm.RunGame(0);

                Func <Int32, TimeSpan> test = (ti) =>
                {
                    var e = new Elapsed("11");
                    var r = SpectralNorm.RunGame(5500);
                    var d = e.Done();
                    //Console.WriteLine("{0:f9}", r);
                    Console.WriteLine($"Try #{ti} : {d}, {r}");
                    return(d);
                };

                var times0 = new List <TimeSpan>(10);
                for (var i = 0; i < 10; i++)
                {
                    times0.Add(test(i));
                }

                var times   = times0.Where(x => x != times0.Min() && x != times0.Max()).ToList();
                var average = new TimeSpan((long)times.Select(ts => ts.Ticks).Average());
                //Console.WriteLine($"{target} [Spectral-Norm] : {average}");
                using (StreamWriter sw = File.AppendText("averages.txt"))
                {
                    sw.WriteLine($"{target} [S-Norm] : {average}");
                }
            }
            {
                Console.WriteLine("##### Quicksort #####");
                Quicksort.DoSort(new Int32[3] {
                    0, 1, 2
                }, 0, 2);
                Func <Int32, TimeSpan> test = (ti) =>
                {
                    var n     = 50000000;
                    var r     = new Random();
                    var array = new Int32[n];
                    for (var i = 0; i < n; i++)
                    {
                        array[i] = r.Next(0, n);
                    }
                    var e = new Elapsed("");
                    Quicksort.DoSort(array, 0, n - 1);
                    var d = e.Done();
                    Console.WriteLine($"Try #{ti} : {d}, {array[1000]}");
                    return(d);
                };

                var times0 = new List <TimeSpan>(10);
                for (var i = 0; i < 10; i++)
                {
                    times0.Add(test(i));
                }

                var times   = times0.Where(x => x != times0.Min() && x != times0.Max()).ToList();
                var average = new TimeSpan((long)times.Select(ts => ts.Ticks).Average());
                //Console.WriteLine($"{target} [Spectral-Norm] : {average}");
                using (StreamWriter sw = File.AppendText("averages.txt"))
                {
                    sw.WriteLine($"{target} [Q-Sort] : {average}");
                }
            }
        }
Beispiel #6
0
        public static List <Cluster> SpectralClustering(List <Column> cols, int Nc, SpectralNorm norm, double sigma = 1e3)
        {
            // data
            List <ClusterLoad> dataN = cols.SelectMany(c => c.Loads.Select(l => new ClusterLoad(new MWPoint3D(l.MEdx, l.MEdy, l.P), c, l.Name))).ToList();

            int N = dataN.Count;

            // Similarity matrix construction
            Matrix <double> S = Matrix <double> .Build.Dense(N, N);

            for (int i = 0; i < N; i++)
            {
                for (int j = i; j < N; j++)
                {
                    double dist = Points.Distance3D(dataN[i].Load, dataN[j].Load);
                    S[i, j] = Math.Exp(-Math.Pow(dist, 2) / (2 * Math.Pow(sigma, 2)));
                    //S[i, j] = dist;
                    S[j, i] = S[i, j];
                }
            }

            // Diagonal matrix of degrees D
            Matrix <double> D = Matrix <double> .Build.Dense(N, N);

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    D[i, i] += S[i, j];
                }
            }

            // Laplacian matrix
            Matrix <double> L = Matrix <double> .Build.Dense(N, N);

            Matrix <double> I = Matrix <double> .Build.DenseIdentity(N);

            switch (norm)
            {
            case (SpectralNorm.None):
                L = D.Subtract(S);
                break;

            case (SpectralNorm.Division):
                Matrix <double> Dinv = Matrix <double> .Build.Dense(N, N, (i, j) => i == j? 1 / D[i, j] : 0);

                L = I.Subtract(Dinv.Multiply(S));
                break;

            case (SpectralNorm.SymmetricDivision):
                Matrix <double> Dhalfinv = Matrix <double> .Build.Dense(N, N, (i, j) => i == j? 1 / Math.Sqrt(D[i, i]) : 0);

                L = I.Subtract(Dhalfinv.Multiply(S.Multiply(Dhalfinv)));
                break;

            case (SpectralNorm.Additive):
                double dmax = D.Enumerate().Max();
                L = I.Subtract(S.Add(I.Multiply(dmax).Subtract(D)).Divide(dmax));
                break;
            }


            //L = D.Subtract(S);

            // Diagonalisation of L
            Evd <double>    eigen        = L.Evd(Symmetricity.Symmetric);
            Matrix <double> eigenVectors = eigen.EigenVectors;
            Vector <double> eigenValues  = eigen.EigenValues.Real();

            // Construction of matrix X
            Matrix <double> X = eigenVectors.SubMatrix(0, N, 0, Nc);

            // K means applied to the lines of X
            List <List <Vector <double> > > clusters = KMeans(X);

            // Assignment of column loads to their cluster
            List <Cluster>          res  = new List <Cluster>();
            List <Vector <double> > rows = X.EnumerateRows().ToList();

            if (norm == SpectralNorm.SymmetricDivision)
            {
                X = X.NormalizeRows(2);
            }

            for (int i = 0; i < Nc; i++)
            {
                res.Add(new Cluster());
                List <Vector <double> > cluster = clusters[i];
                for (int j = 0; j < clusters[i].Count; j++)
                {
                    int ind = rows.IndexOf(cluster[j]);
                    res[i].Loads.Add(dataN[ind]);
                }
            }

            return(res);
        }