Exemple #1
0
        public DenseMatrixComplex smallestEigPositiveDefinite(ref SparseMatrixComplex A, ref SparseMatrixComplex B, ref DenseMatrixComplex x)
        {
            LinearSystemGenericByLib.Instance.FactorizationLU(ref A);
            int n = A.Length();

            DenseMatrixComplex e = new DenseMatrixComplex(n, 1);

            e.Fill(new Complex(1, 0));

            Complex dot  = e.Dot(B * e);
            double  norm = Math.Sqrt(dot.Norm());

            e /= new Complex(norm, 0);

            //Iteratation
            for (int i = 0; i < MaxEigIter; i++)
            {
                x  = B * x;
                x  = LinearSystemGenericByLib.Instance.SolveByFactorizedLU(ref x);
                x -= x.Dot(B * e) * e;

                double newNorm = Math.Sqrt(x.Dot(B * x).Norm());
                x /= new Complex(newNorm, 0);
            }

            LinearSystemGenericByLib.Instance.FreeSolverLUComplex();
            return(x);
        }
Exemple #2
0
        public static DenseMatrixComplex operator *(DenseMatrixComplex left, SparseMatrixComplex right)
        {
            //Make sure matrix dimensions are equal
            if (left.columnCount != right.RowCount)
            {
                throw new Exception("The dimension of two matrix must be equal");
            }
            DenseMatrixComplex resultMatrix = new DenseMatrixComplex(left.RowCount, right.ColumnCount);

            Complex[,] result = resultMatrix.datas;

            for (int i = 0; i < left.rowCount; i++)
            {
                foreach (KeyValuePair <Pair, Complex> item in right.Datas)
                {
                    Pair    pair  = item.Key;
                    Complex value = item.Value;

                    int m = pair.Key;
                    int n = pair.Value;

                    //M: mutiply index N: vector store index
                    result[i, n] += left[i, m] * value;
                }
            }

            return(resultMatrix);
        }
Exemple #3
0
        public void FlattenProcess()
        {
            SparseMatrixComplex Lc = BuildEnergy();

            SparseMatrixComplex star0 = DECComplex.Instance.cBuildHodgeStar0Form(mesh);

            Lc += new Complex(1.0e-8, 0) * star0; //[Reconsider: Complex(1.0e-8)]

            //Compute parameterization
            DenseMatrixComplex x = new DenseMatrixComplex(Lc.RowCount, 1);

            x.Randomize();  //Initial guesses

            DenseMatrixComplex result = LinearSystemDEC.Instance.smallestEigPositiveDefinite(ref Lc, ref star0, ref x);

            //Assign sultion
            foreach (TriMesh.Vertex v in mesh.Vertices)
            {
                Complex value = result[v.Index, 0];

                v.Traits.Position.x = value.RealPart;
                v.Traits.Position.y = value.ImagePart;
                v.Traits.Position.z = 0;
            }

            TriMeshUtil.ScaleToUnit(mesh, 1.0);
            TriMeshUtil.MoveToCenter(mesh);
        }
Exemple #4
0
        public Complex Dot(DenseMatrixComplex right)
        {
            DenseMatrixComplex x      = this.Transpose() * right;
            Complex            result = x[0, 0];

            return(result);
        }
Exemple #5
0
        public DenseMatrixComplex SolveLinerSystem(ref SparseMatrixComplex A, ref DenseMatrixComplex b)
        {
            if (A.RowCount != b.RowCount)
            {
                throw new Exception("The dimension of A and b must be agree");
            }


            CholmodInfo cholmodb = CholmodConverter.cConverter(ref b);
            CholmodInfo cholmodA = CholmodConverter.cConverter(ref A, CholmodInfo.CholmodMatrixStorage.CCS);

            double[] x = new double[2 * A.ColumnCount];

            fixed(int *Index = cholmodA.rowIndex, Pt = cholmodA.colIndex)
            fixed(double *val = cholmodA.values, bp = cholmodb.values, xx = x)
            {
                SolveRealByQR_CCS_Complex(cholmodA.RowCount,
                                          cholmodA.ColumnCount,
                                          cholmodA.nnz,
                                          Pt,    //Column Pointer
                                          Index, //Row Index
                                          val,
                                          xx,
                                          bp);
            }

            DenseMatrixComplex unknown = CholmodConverter.cConvertArrayToDenseMatrix(ref x, x.Length, 1);


            cholmodA = null;
            cholmodb = null;
            GC.Collect();
            return(unknown);
        }
Exemple #6
0
        public void FlattenProcess()
        {

            SparseMatrixComplex Lc = BuildEnergy();

            SparseMatrixComplex star0 = DECComplex.Instance.cBuildHodgeStar0Form(mesh);
            Lc += new Complex(1.0e-8, 0) * star0; //[Reconsider: Complex(1.0e-8)]

            //Compute parameterization
            DenseMatrixComplex x = new DenseMatrixComplex(Lc.RowCount, 1);
            x.Randomize();  //Initial guesses

            DenseMatrixComplex result = LinearSystemDEC.Instance.smallestEigPositiveDefinite(ref Lc, ref star0, ref x);

            //Assign sultion
            foreach (TriMesh.Vertex v in mesh.Vertices)
            {
                Complex value = result[v.Index, 0];

                v.Traits.Position.x = value.RealPart;
                v.Traits.Position.y = value.ImagePart;
                v.Traits.Position.z = 0;
            }

            TriMeshUtil.ScaleToUnit(mesh,1.0);
            TriMeshUtil.MoveToCenter(mesh);

        }
Exemple #7
0
        public double cResidual(ref SparseMatrixComplex A, ref DenseMatrixComplex b, ref DenseMatrixComplex x)
        {
            DenseMatrixComplex errors = (A * x - b);
            double             norm   = errors.InifnityNorm() / x.InifnityNorm();

            return(norm);
        }
Exemple #8
0
        public DenseMatrixComplex smallestEigPositiveDefinite(ref SparseMatrixComplex A, bool ignoreConstantVector)
        {
            ignoreConstantVector = true;

            DenseMatrixComplex x = new DenseMatrixComplex();

            return(x);
        }
Exemple #9
0
        public static DenseMatrixComplex Copy(ref DenseMatrixComplex B)
        {
            DenseMatrixComplex newMatrix = new DenseMatrixComplex();

            newMatrix.rowCount    = B.rowCount;
            newMatrix.columnCount = B.columnCount;
            Array.Copy(B.datas, newMatrix.datas, B.datas.Length);
            return(newMatrix);
        }
Exemple #10
0
        public static CholmodInfo cConverter(ref DenseMatrixComplex b)
        {
            CholmodInfo info = new CholmodInfo();

            info.MatrixType = CholmodInfo.CholmodMatrixType.Dense;

            b.ToArray(out info.RowCount, out info.ColumnCount, out info.values);    //Normal

            return(info);
        }
Exemple #11
0
        public static DenseMatrixComplex Identity(int N)
        {
            DenseMatrixComplex identity = new DenseMatrixComplex(N, N);

            for (int i = 0; i < N; i++)
            {
                identity.datas[i, i] = Complex.Identity;
            }

            return identity;
        }
Exemple #12
0
        public static DenseMatrixComplex Identity(int N)
        {
            DenseMatrixComplex identity = new DenseMatrixComplex(N, N);

            for (int i = 0; i < N; i++)
            {
                identity.datas[i, i] = Complex.Identity;
            }

            return(identity);
        }
Exemple #13
0
        public double F_Norm()
        {
            DenseMatrixComplex tr = this.Transpose() * this;

            double sum = 0;

            for (int i = 0; i < tr.RowCount; i++)
            {
                sum += tr[i, i].Norm();
            }

            return(Math.Sqrt(sum));
        }
Exemple #14
0
        public static DenseMatrixComplex operator *(Complex left, DenseMatrixComplex right)
        {
            DenseMatrixComplex result = new DenseMatrixComplex(right.rowCount, right.columnCount);

            for (int i = 0; i < right.rowCount; i++)
            {
                for (int j = 0; j < right.columnCount; j++)
                {
                    result.datas[i, j] = right.datas[i, j] * left;
                }
            }

            return(result);
        }
Exemple #15
0
        public DenseMatrixComplex Transpose()
        {
            DenseMatrixComplex tr = new DenseMatrixComplex(this.columnCount, this.rowCount);

            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    tr.datas[j, i] = datas[i, j];
                }
            }

            return tr;
        }
Exemple #16
0
        public DenseMatrixComplex Transpose()
        {
            DenseMatrixComplex tr = new DenseMatrixComplex(this.columnCount, this.rowCount);

            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    tr.datas[j, i] = datas[i, j];
                }
            }

            return(tr);
        }
Exemple #17
0
        public static DenseMatrixComplex operator /(DenseMatrixComplex left, Complex right)
        {
            //Make sure matrix dimensions are equal
            DenseMatrixComplex matrix = new DenseMatrixComplex(left.RowCount, left.ColumnCount);

            Complex rightInv = right.Inv();

            for (int i = 0; i < left.RowCount; i++)
            {
                for (int j = 0; j < left.columnCount; j++)
                {
                    matrix.datas[i, j] = left.datas[i, j] * rightInv;
                }
            }

            return(matrix);
        }
Exemple #18
0
        public static DenseMatrixComplex cConvertArrayToDenseMatrix(ref double[] x, int m, int n)
        {
            DenseMatrixComplex unknown = new DenseMatrixComplex(m, n);

            int count = 0;

            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Complex value = new Complex(x[2 * count], x[2 * count + 1]);
                    unknown[i, j] = value;
                    count++;
                }
            }

            return(unknown);
        }
Exemple #19
0
        public DenseMatrixComplex SolveByFactorizedLU(ref DenseMatrixComplex b)
        {
            CholmodInfo cholmodb = CholmodConverter.cConverter(ref b);

            DenseMatrixComplex result = new DenseMatrixComplex(n, 1);

            double[] x = new double[2 * n];

            SolveLUComplex(ref cholmodb.values, ref x);

            for (int i = 0; i < n; i++)
            {
                result[i, 0] = new Complex(x[2 * i], x[2 * i + 1]);
            }

            x = null;
            GC.Collect();

            return(result);
        }
Exemple #20
0
        public static DenseMatrixComplex operator +(DenseMatrixComplex left, DenseMatrixComplex right)
        {
            //Make sure matrix dimensions are equal
            if (left.columnCount != right.columnCount)
            {
                throw new Exception("The dimension of two matrix must be equal");
            }

            DenseMatrixComplex matrix = new DenseMatrixComplex(left.RowCount, left.ColumnCount);

            for (int i = 0; i < left.RowCount; i++)
            {
                for (int j = 0; j < left.columnCount; j++)
                {
                    matrix.datas[i, j] = left.datas[i, j] + right.datas[i, j];
                }
            }

            return(matrix);
        }
Exemple #21
0
        public static DenseMatrixComplex operator *(DenseMatrixComplex left, DenseMatrixComplex right)
        {
            //Make sure matrix dimensions are equal
            if (left.columnCount != right.rowCount)
            {
                throw new Exception("The dimension of two matrix must be equal");
            }

            DenseMatrixComplex result = new DenseMatrixComplex(left.rowCount, right.columnCount);

            for (int i = 0; i < left.rowCount; i++)
            {
                for (int j = 0; j < right.columnCount; j++)
                {
                    for (int z = 0; z < left.columnCount; z++)
                    {
                        result.datas[i, j] += left.datas[i, z] * right.datas[z, j];
                    }
                }
            }

            return(result);
        }
Exemple #22
0
        public static DenseMatrixComplex operator *(Complex left, DenseMatrixComplex right)
        {
            DenseMatrixComplex result = new DenseMatrixComplex(right.rowCount, right.columnCount);

            for (int i = 0; i < right.rowCount; i++)
            {
                for (int j = 0; j < right.columnCount; j++)
                {
                    result.datas[i, j] = right.datas[i, j] * left;
                }
            }

            return result;
        }
Exemple #23
0
 public Complex Dot(DenseMatrixComplex right)
 {
     DenseMatrixComplex x = this.Transpose() * right;
     Complex result = x[0, 0];
     return result;
 }
Exemple #24
0
        public static DenseMatrixComplex operator *(DenseMatrixComplex left, DenseMatrixComplex right)
        {
            //Make sure matrix dimensions are equal
            if (left.columnCount != right.rowCount)
            {
                throw new Exception("The dimension of two matrix must be equal");
            }

            DenseMatrixComplex result = new DenseMatrixComplex(left.rowCount, right.columnCount);

            for (int i = 0; i < left.rowCount; i++)
            {
                for (int j = 0; j < right.columnCount; j++)
                {
                    for (int z = 0; z < left.columnCount; z++)
                    {
                        result.datas[i, j] += left.datas[i, z] * right.datas[z, j];
                    }
                }
            }

            return result;
        }
        public DenseMatrixComplex SolveLinerSystem(ref SparseMatrixComplex A, ref DenseMatrixComplex b)
        {
            if (A.RowCount != b.RowCount)
            {
                throw new Exception("The dimension of A and b must be agree");
            }


            CholmodInfo cholmodb = CholmodConverter.cConverter(ref b);
            CholmodInfo cholmodA = CholmodConverter.cConverter(ref A, CholmodInfo.CholmodMatrixStorage.CCS);

            double[] x = new double[2 * A.ColumnCount];

            fixed (int* Index = cholmodA.rowIndex, Pt = cholmodA.colIndex)
            fixed (double* val = cholmodA.values, bp = cholmodb.values, xx = x)
            {
                SolveRealByQR_CCS_Complex(cholmodA.RowCount,
                                  cholmodA.ColumnCount,
                                  cholmodA.nnz,
                                  Pt,   //Column Pointer
                                  Index,    //Row Index
                                  val,
                                  xx,
                                  bp);
            }

            DenseMatrixComplex unknown = CholmodConverter.cConvertArrayToDenseMatrix(ref x, x.Length, 1);


            cholmodA = null;
            cholmodb = null;
            GC.Collect();
            return unknown;
        }
Exemple #26
0
        public static CholmodInfo cConverter(ref DenseMatrixComplex b)
        {
            CholmodInfo info = new CholmodInfo();
            info.MatrixType = CholmodInfo.CholmodMatrixType.Dense;

            b.ToArray(out info.RowCount, out info.ColumnCount, out info.values);    //Normal

            return info;
        }
Exemple #27
0
        public static DenseMatrixComplex operator /(DenseMatrixComplex left, Complex right)
        {
            //Make sure matrix dimensions are equal
            DenseMatrixComplex matrix = new DenseMatrixComplex(left.RowCount, left.ColumnCount);

            Complex rightInv = right.Inv();

            for (int i = 0; i < left.RowCount; i++)
            {
                for (int j = 0; j < left.columnCount; j++)
                {
                    matrix.datas[i, j] = left.datas[i, j] * rightInv;
                }
            }

            return matrix;
        }
Exemple #28
0
        public static DenseMatrixComplex operator +(DenseMatrixComplex left, DenseMatrixComplex right)
        {
            //Make sure matrix dimensions are equal
            if (left.columnCount != right.columnCount)
            {
                throw new Exception("The dimension of two matrix must be equal");
            }

            DenseMatrixComplex matrix = new DenseMatrixComplex(left.RowCount, left.ColumnCount);

            for (int i = 0; i < left.RowCount; i++)
            {
                for (int j = 0; j < left.columnCount; j++)
                {
                    matrix.datas[i, j] = left.datas[i, j] + right.datas[i, j];
                }
            }

            return matrix;
        }
Exemple #29
0
 public static DenseMatrixComplex Copy(ref DenseMatrixComplex B)
 {
     DenseMatrixComplex newMatrix = new DenseMatrixComplex();
     newMatrix.rowCount = B.rowCount;
     newMatrix.columnCount = B.columnCount;
     Array.Copy(B.datas, newMatrix.datas, B.datas.Length);
     return newMatrix;
 }
        public DenseMatrixComplex SolveByFactorizedLU(ref DenseMatrixComplex b)
        {
            CholmodInfo cholmodb = CholmodConverter.cConverter(ref b);

            DenseMatrixComplex result = new DenseMatrixComplex(n, 1);

            double[] x = new double[2 * n];

            SolveLUComplex(ref cholmodb.values, ref x);

            for (int i = 0; i < n; i++)
            {
                result[i, 0] = new Complex(x[2 * i], x[2 * i + 1]);
            }

            x = null;
            GC.Collect();

            return result;
        }
Exemple #31
0
        public static DenseMatrixComplex cConvertArrayToDenseMatrix(ref double[] x, int m, int n)
        {
            DenseMatrixComplex unknown = new DenseMatrixComplex(m, n);

            int count = 0;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Complex value = new Complex(x[2 * count], x[2 * count + 1]);
                    unknown[i, j] = value;
                    count++;
                }
            }

            return unknown;
        }
Exemple #32
0
        public static DenseMatrixComplex operator *(DenseMatrixComplex left, SparseMatrixComplex right)
        {
            //Make sure matrix dimensions are equal
            if (left.columnCount != right.RowCount)
            {
                throw new Exception("The dimension of two matrix must be equal");
            }
            DenseMatrixComplex resultMatrix = new DenseMatrixComplex(left.RowCount, right.ColumnCount);
            Complex[,] result = resultMatrix.datas;

            for (int i = 0; i < left.rowCount; i++)
            {
                foreach (KeyValuePair<Pair, Complex> item in right.Datas)
                {
                    Pair pair = item.Key;
                    Complex value = item.Value;

                    int m = pair.Key;
                    int n = pair.Value;

                    //M: mutiply index N: vector store index
                    result[i, n] += left[i, m] * value;
                }
            }

            return resultMatrix;
        }