Beispiel #1
0
        public void SparseSample_ComplexFloat_Identity()
        {
            NumericsConfiguration.NativeProviderPath = @"C:\Program Files (x86)\Extreme Optimization\Numerical Libraries for .NET\bin\Net40";
            NumericsConfiguration.Providers.RegisterSinglePrecisionProvider();
            NumericsConfiguration.AutoLoadNativeProviders = true;
            CoreImplementations <float> .UseNative();

            Console.WriteLine(CoreImplementations <Complex <float> > .LinearAlgebra.Name);

            SparseCompressedColumnMatrix <Complex <float> > matrixA = Matrix.CreateSparse <Complex <float> >(3, 3);

            matrixA.SetValue(new Complex <float>(1, 0), 0, 0);
            matrixA.SetValue(new Complex <float>(1, 0), 1, 1);
            matrixA.SetValue(new Complex <float>(1, 0), 2, 2);
            Vector <Complex <float> > vectorB = Vector.Create(new Complex <float>(1.0f, 0), new Complex <float>(2.0f, 0), new Complex <float>(3.0f, 0));

            IterativeSparseSolver <Complex <float> > solver       = new BiConjugateGradientSolver <Complex <float> >(matrixA);
            DenseVector <Complex <float> >           resultVector = solver.Solve(vectorB);

            Console.WriteLine("Result: {0}", resultVector);
            Console.WriteLine("Solved in {0} iterations.", solver.IterationsNeeded);
            Console.WriteLine("Estimated error: {0}", solver.SolutionReport.Error);

            // With incomplete LU preconditioner
            solver.Preconditioner = new IncompleteLUPreconditioner <Complex <float> >(matrixA);
            resultVector          = solver.Solve(vectorB);

            Console.WriteLine("Result: {0}", resultVector);
            Console.WriteLine("Solved in {0} iterations.", solver.IterationsNeeded);
            Console.WriteLine("Estimated error: {0}", solver.EstimatedError);
        }
Beispiel #2
0
        public void SimpleSample_Double_Identity()
        {
            Console.WriteLine(CoreImplementations <Complex <float> > .LinearAlgebra.Name);
            // We load a sparse matrix and right-hand side from a data file:
            SparseCompressedColumnMatrix <double> matrixA = SparseCompressedColumnMatrix <double> .CreateIdentity(3);

            Vector <double> vectorB = Vector.Create(1.0, 2.0, 3.0);

            IterativeSparseSolver <double> solver = new BiConjugateGradientSolver <double>(matrixA);
            var resultVector = solver.Solve(vectorB);

            Console.WriteLine("Result: {0}", resultVector);
            Console.WriteLine("Solved in {0} iterations.", solver.IterationsNeeded);
            Console.WriteLine("Estimated error: {0}", solver.SolutionReport.Error);

            // With incomplete LU preconditioner
            solver.Preconditioner = new IncompleteLUPreconditioner <double>(matrixA);
            resultVector          = solver.Solve(vectorB);

            Console.WriteLine("Result: {0}", resultVector);
            Console.WriteLine("Solved in {0} iterations.", solver.IterationsNeeded);
            Console.WriteLine("Estimated error: {0}", solver.EstimatedError);
        }
Beispiel #3
0
        public void ClusterNmf(int numClusters, string path)
        {
            var data = _dataset.AllSamples.Select(ir => new
            {
                Row   = _userMapping.ToInternalID(ir.User.Id),
                Col   = _itemMapping.ToInternalID(ir.Item.Id),
                Value = Convert.ToDouble(ir.Rating)
            }).ToList();

            File.WriteAllLines(path + ".data", data.Select(d => string.Format("{0},{1},{2}", d.Row, d.Col, d.Value)).ToList());

            var rows   = data.Select(d => d.Row).ToArray();
            var colums = data.Select(d => d.Col).ToArray();
            var values = data.Select(d => d.Value).ToArray();

            int numRows = rows.Max();
            int numCols = colums.Max();

            Console.WriteLine("Creating sparse matrix...\nNum users: {0} Num items: {1} Num ratings: {2}",
                              numRows, numCols, values.Count());

            SparseCompressedColumnMatrix sparseMatrix = Matrix.CreateSparse(numRows + 1, numCols + 1, rows, colums, values);

            var nmf = new NonNegativeMatrixFactorization(sparseMatrix, numClusters);

            Console.WriteLine("Decomposing...");
            nmf.Decompose();

            Console.WriteLine("Writing decompose matrixes...");

            var lf = nmf.LeftFactor.ToArray(MatrixElementOrder.RowMajor);
            var rf = nmf.RightFactor.ToArray(MatrixElementOrder.RowMajor);

            var lfWriter = new StreamWriter(path + ".lf");
            var rfWriter = new StreamWriter(path + ".rf");



            for (int i = 0; i < lf.Length; i++)
            {
                if (i != 0 && i % numClusters == 0)
                {
                    lfWriter.WriteLine();
                }
                lfWriter.Write(lf[i] + " ");
            }

            lfWriter.Close();

            for (int i = 0; i < rf.Length; i++)
            {
                if (i != 0 && i % numClusters == 0)
                {
                    rfWriter.WriteLine();
                }
                rfWriter.Write(rf[i] + " ");
            }

            rfWriter.Close();

            Console.WriteLine("Finished writing.");
        }