Ejemplo n.º 1
0
        public static SparseMatrixComplex operator -(SparseMatrixComplex left, SparseMatrixComplex right)
        {
            SparseMatrixComplex result = new SparseMatrixComplex(right.rowCount, right.columnCount);

            foreach (KeyValuePair <Pair, Complex> item in left.mapData)
            {
                Pair    pair  = item.Key;
                Complex value = item.Value;

                result.mapData.Add(pair, value);
            }

            foreach (KeyValuePair <Pair, Complex> item in right.mapData)
            {
                Pair    pair  = item.Key;
                Complex value = item.Value;

                if (result.mapData.ContainsKey(pair))
                {
                    Complex temp = result.mapData[pair] -= item.Value;
                    if (temp.Equals(Complex.Zero))
                    {
                        result.mapData.Remove(pair);
                    }
                }
                else
                {
                    result.mapData.Add(pair, value);
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public SparseMatrixComplex BuildEnergy()
        {
            //Build Laplace matrix
            SparseMatrixDouble d0    = DECDouble.Instance.BuildExteriorDerivative0Form(mesh);;
            SparseMatrixDouble star1 = DECDouble.Instance.BuildHodgeStar1Form(mesh);
            SparseMatrixDouble L     = d0.Transpose() * star1 * d0;

            SparseMatrixComplex A = SparseMatrixComplex.Copy(ref L);

            //Iterate faces
            foreach (TriMesh.Face face in mesh.Faces)
            {
                //Iterate each halfedge in face
                foreach (TriMesh.HalfEdge hf in face.Halfedges)
                {
                    int i = hf.ToVertex.Index;
                    int j = hf.FromVertex.Index;

                    Complex value = 0.5 * DDGConstant;

                    A[i, j] -= value;
                    A[j, i] += value;
                }
            }


            return(A);
        }
Ejemplo n.º 3
0
        public SparseMatrixComplex cBuildExteriorDerivative1Form(TriMesh mesh)
        {
            SparseMatrixComplex d1 = new SparseMatrixComplex(mesh.Faces.Count, mesh.Edges.Count);

            foreach (TriMesh.Face face in mesh.Faces)
            {
                foreach (TriMesh.HalfEdge hf in face.Halfedges)
                {
                    double s = 0;

                    if (hf.Edge.HalfEdge0 == hf)
                    {
                        s = -1;
                    }
                    else
                    {
                        s = 1;
                    }

                    d1[face.Index, hf.Edge.Index] = new Complex(s, 0);
                }
            }

            return d1;
        }
Ejemplo n.º 4
0
        public static CholmodInfo cConverter(ref SparseMatrixComplex A, CholmodInfo.CholmodMatrixStorage storage)
        {
            CholmodInfo info = new CholmodInfo();

            info.MatrixType          = CholmodInfo.CholmodMatrixType.Sparse;
            info.MatrixStorageType   = storage;
            info.MatrixStorageMethod = CholmodInfo.CholmodMatrixStoageMethod.Normal;
            info.MatrixItemType      = CholmodInfo.CholmodMatrixItemType.Complex;
            info.RowCount            = A.RowCount;
            info.ColumnCount         = A.ColumnCount;

            switch (storage)
            {
            case CholmodInfo.CholmodMatrixStorage.CRS:
                A.ToCRS(out info.colIndex, out info.rowIndex, out info.values, out info.nnz);
                break;

            case CholmodInfo.CholmodMatrixStorage.CCS:
                A.ToCCS(out info.colIndex, out info.rowIndex, out info.values, out info.nnz);
                break;

            case CholmodInfo.CholmodMatrixStorage.Triplet:
                A.ToTriplet(out info.colIndex, out info.rowIndex, out info.values, out info.z, out info.nnz);
                break;

            default:
                A.ToTriplet(out info.colIndex, out info.rowIndex, out info.values, out info.z, out info.nnz);

                break;
            }
            return(info);
        }
Ejemplo n.º 5
0
        /*
         * For Complex Calculation
         */
        public void FactorizationLU(ref SparseMatrixComplex A)
        {
            CholmodInfo cholmodA = CholmodConverter.cConverter(ref A,
                                                               CholmodInfo.CholmodMatrixStorage.CCS);

            m = A.RowCount;
            n = A.ColumnCount;

            fixed(int *Index = cholmodA.rowIndex, Pt = cholmodA.colIndex)
            fixed(double *val = cholmodA.values)
            {
                solver = CreateSolverLUUMFPACK_CCS_Complex(cholmodA.RowCount,
                                                           cholmodA.ColumnCount,
                                                           cholmodA.nnz,
                                                           Index,
                                                           Pt,
                                                           val
                                                           );
            }


            if (solver == null)
            {
                throw new Exception("Create Solver Fail");
            }
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public SparseMatrixComplex cBuildExteriorDerivative1Form(TriMesh mesh)
        {
            SparseMatrixComplex d1 = new SparseMatrixComplex(mesh.Faces.Count, mesh.Edges.Count);

            foreach (TriMesh.Face face in mesh.Faces)
            {
                foreach (TriMesh.HalfEdge hf in face.Halfedges)
                {
                    double s = 0;

                    if (hf.Edge.HalfEdge0 == hf)
                    {
                        s = -1;
                    }
                    else
                    {
                        s = 1;
                    }

                    d1[face.Index, hf.Edge.Index] = new Complex(s, 0);
                }
            }

            return(d1);
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
        public DenseMatrixComplex smallestEigPositiveDefinite(ref SparseMatrixComplex A, bool ignoreConstantVector)
        {
            ignoreConstantVector = true;

            DenseMatrixComplex x = new DenseMatrixComplex();

            return(x);
        }
Ejemplo n.º 12
0
        public static SparseMatrixComplex Copy(ref SparseMatrixComplex B)
        {
            Dictionary <Pair, Complex> newData = new Dictionary <Pair, Complex>(B.mapData);

            SparseMatrixComplex newMatrix = new SparseMatrixComplex(B.rowCount, B.columnCount);

            newMatrix.mapData = newData;

            return(newMatrix);
        }
Ejemplo n.º 13
0
        public SparseMatrixComplex SubMatrix(int rowStart, int rowEnd, int columnStart, int columnEnd)
        {
            if (rowEnd < rowStart || columnEnd < columnStart)
            {
                throw new ArgumentOutOfRangeException();
            }

            int rows    = rowEnd - rowStart + 1;
            int columns = columnEnd - columnStart + 1;

            SparseMatrixComplex subMatrix = new SparseMatrixComplex(rows, columns);

            int subMatrixEntiries = rows * columns;

            if (subMatrixEntiries < mapData.Count)
            {
                for (int i = 0; i < rows; i++)
                {
                    int rowInx = i + rowStart;
                    for (int j = 0; j < columns; j++)
                    {
                        int  columnInx = j + columnStart;
                        Pair pair      = new Pair(rowInx, columnInx);
                        if (!mapData.ContainsKey(pair))
                        {
                            continue;
                        }

                        Pair subPair = new Pair(i, j);
                        subMatrix.mapData[subPair] = mapData[pair];
                    }
                }
            }
            else if (subMatrixEntiries >= mapData.Count)
            {
                foreach (KeyValuePair <Pair, Complex> item in mapData)
                {
                    Pair    pair  = item.Key;
                    Complex value = item.Value;

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

                    if ((m >= rowStart && m <= rowEnd) && (n >= columnStart && m <= columnEnd))
                    {
                        int i = m - rowStart;
                        int j = n - columnStart;

                        subMatrix.mapData[new Pair(i, j)] = value;
                    }
                }
            }

            return(subMatrix);
        }
Ejemplo n.º 14
0
        public static SparseMatrixComplex Identity(int N)
        {
            SparseMatrixComplex identity = new SparseMatrixComplex(N, N);

            for (int i = 0; i < N; i++)
            {
                identity.mapData.Add(new Pair(i, i), Complex.Identity);
            }

            return(identity);
        }
Ejemplo n.º 15
0
        public SparseMatrixComplex cBuildHodgeStar0Form(TriMesh mesh)
        {
            SparseMatrixComplex star0 = new SparseMatrixComplex(mesh.Vertices.Count, mesh.Vertices.Count);

            foreach (TriMesh.Vertex vertex in mesh.Vertices)
            {
                Complex value = new Complex(ComputeVertexDualArea(vertex), 0);

                star0[vertex.Index, vertex.Index] = value;
            }

            return(star0);
        }
Ejemplo n.º 16
0
        public static SparseMatrixComplex operator *(Complex left, SparseMatrixComplex right)
        {
            SparseMatrixComplex result = new SparseMatrixComplex(right.rowCount, right.columnCount);

            foreach (KeyValuePair <Pair, Complex> item in right.mapData)
            {
                Pair    pair  = item.Key;
                Complex value = left * item.Value;

                result.mapData.Add(pair, value);
            }

            return(result);
        }
Ejemplo n.º 17
0
        public SparseMatrixComplex cBuildHodgeStar2Form(TriMesh mesh)
        {
            SparseMatrixComplex star2 = new SparseMatrixComplex(mesh.Faces.Count, mesh.Faces.Count);

            foreach (TriMesh.Face face in mesh.Faces)
            {
                Complex value = new Complex(1 / TriMeshUtil.ComputeAreaFace(face), 0);


                star2[face.Index, face.Index] = value;
            }

            return(star2);
        }
Ejemplo n.º 18
0
        public static SparseMatrixComplex operator *(Complex left, SparseMatrixComplex right)
        {
            SparseMatrixComplex result = new SparseMatrixComplex(right.rowCount, right.columnCount);

            foreach (KeyValuePair<Pair, Complex> item in right.mapData)
            {
                Pair pair = item.Key;
                Complex value = left * item.Value;

                result.mapData.Add(pair, value);
            }

            return result;
        }
Ejemplo n.º 19
0
        public SparseMatrixComplex Transpose()
        {
            SparseMatrixComplex trMatrix = new SparseMatrixComplex(this.columnCount, this.rowCount);

            foreach (KeyValuePair <Pair, Complex> item in mapData)
            {
                Pair    pair  = item.Key;
                Complex value = item.Value;

                trMatrix.mapData[new Pair(pair.Value, pair.Key)] = value;
            }

            return(trMatrix);
        }
Ejemplo n.º 20
0
        public SparseMatrixComplex cBuildExteriorDerivative0Form(TriMesh mesh)
        {
            SparseMatrixComplex d0 = new SparseMatrixComplex(mesh.Edges.Count, mesh.Vertices.Count);

            foreach (TriMesh.Edge edge in mesh.Edges)
            {
                int ci = edge.HalfEdge0.FromVertex.Index;
                int cj = edge.HalfEdge0.ToVertex.Index;

                d0[edge.Index, ci] = new Complex(1, 0);
                d0[edge.Index, cj] = new Complex(-1, 0);
            }

            return(d0);
        }
Ejemplo n.º 21
0
        public SparseMatrixComplex cBuildExteriorDerivative0Form(TriMesh mesh)
        {
            SparseMatrixComplex d0 = new SparseMatrixComplex(mesh.Edges.Count, mesh.Vertices.Count);

            foreach (TriMesh.Edge edge in mesh.Edges)
            {
                int ci = edge.HalfEdge0.FromVertex.Index;
                int cj = edge.HalfEdge0.ToVertex.Index;

                d0[edge.Index, ci] = new Complex(1, 0);
                d0[edge.Index, cj] = new Complex(-1, 0);

            }

            return d0;
        }
Ejemplo n.º 22
0
        public SparseMatrixComplex cBuildHodgeStar1Form(TriMesh mesh)
        {
            SparseMatrixComplex star1 = new SparseMatrixComplex(mesh.Edges.Count, mesh.Edges.Count);

            foreach (TriMesh.Edge edge in mesh.Edges)
            {
                double cotAlpha = ComputeTan(edge.HalfEdge0);

                double cotBeta = ComputeTan(edge.HalfEdge1);

                Complex value = new Complex((cotAlpha + cotBeta) / 2, 0);

                star1[edge.Index, edge.Index] = value;
            }


            return(star1);
        }
Ejemplo n.º 23
0
        public static SparseMatrixComplex Copy(ref SparseMatrixDouble B)
        {
            //We only copy of realpart
            Dictionary <Pair, Complex> newData = new Dictionary <Pair, Complex>();

            foreach (KeyValuePair <Pair, double> e in B.Datas)
            {
                Pair   pair  = e.Key;
                double value = e.Value;

                newData.Add(pair, new Complex(value, 0));
            }


            SparseMatrixComplex newMatrix = new SparseMatrixComplex(B.RowCount, B.ColumnCount);

            newMatrix.mapData = newData;

            return(newMatrix);
        }
Ejemplo n.º 24
0
        public SparseMatrixComplex cBuildHodgeStar2Form(TriMesh mesh)
        {
            SparseMatrixComplex star2 = new SparseMatrixComplex(mesh.Faces.Count, mesh.Faces.Count);

            foreach (TriMesh.Face face in mesh.Faces)
            {
                Complex value = new Complex(1 / TriMeshUtil.ComputeAreaFace(face), 0);


                star2[face.Index, face.Index] = value;
            }

            return star2;
        }
Ejemplo n.º 25
0
        public static CholmodInfo cConverter(ref SparseMatrixComplex A, CholmodInfo.CholmodMatrixStorage storage)
        {
            CholmodInfo info = new CholmodInfo();
            info.MatrixType = CholmodInfo.CholmodMatrixType.Sparse;
            info.MatrixStorageType = storage;
            info.MatrixStorageMethod = CholmodInfo.CholmodMatrixStoageMethod.Normal;
            info.MatrixItemType = CholmodInfo.CholmodMatrixItemType.Complex;
            info.RowCount = A.RowCount;
            info.ColumnCount = A.ColumnCount;

            switch (storage)
            {
                case CholmodInfo.CholmodMatrixStorage.CRS:
                    A.ToCRS(out info.colIndex, out info.rowIndex, out info.values, out info.nnz);
                    break;
                case CholmodInfo.CholmodMatrixStorage.CCS:
                    A.ToCCS(out info.colIndex, out info.rowIndex, out info.values, out info.nnz);
                    break;
                case CholmodInfo.CholmodMatrixStorage.Triplet:
                    A.ToTriplet(out info.colIndex, out info.rowIndex, out info.values, out info.z, out info.nnz);
                    break;
                default:
                    A.ToTriplet(out info.colIndex, out info.rowIndex, out info.values, out info.z, out info.nnz);

                    break;
            }
            return info;
        }
Ejemplo n.º 26
0
        public static CholmodInfo cConverter(ref SparseMatrixComplex A, CholmodInfo.CholmodMatrixStorage storage, CholmodInfo.CholmodMatrixStoageMethod method)
        {
            CholmodInfo info = new CholmodInfo();
            info.MatrixType = CholmodInfo.CholmodMatrixType.Sparse;
            info.MatrixStorageType = storage;
            info.MatrixStorageMethod = method;
            info.MatrixItemType = CholmodInfo.CholmodMatrixItemType.Complex;
            info.RowCount = A.RowCount;
            info.ColumnCount = A.ColumnCount;

            double[] values = null;

            switch (storage)
            {
                case CholmodInfo.CholmodMatrixStorage.CRS:
                    A.ToCRS(out info.colIndex, out info.rowIndex, out values, out info.nnz);
                    break;
                case CholmodInfo.CholmodMatrixStorage.CCS:
                    A.ToCCS(out info.colIndex, out info.rowIndex, out values, out info.nnz);
                    break;
                case CholmodInfo.CholmodMatrixStorage.Triplet:
                    A.ToTriplet(out info.colIndex, out info.rowIndex, out values, out info.z, out info.nnz);
                    break;
                default:
                    A.ToTriplet(out info.colIndex, out info.rowIndex, out values, out info.z, out info.nnz);

                    break;
            }

            switch (method)
            {
                case CholmodInfo.CholmodMatrixStoageMethod.Normal:
                    info.values = values;
                    break;
                case CholmodInfo.CholmodMatrixStoageMethod.Divided:
                    double[] realParts = new double[info.nnz];
                    double[] imgParts = new double[info.nnz];

                    for (int i = 0; i < info.nnz; i++)
                    {
                        realParts[i] = values[2 * i];
                        imgParts[i] = values[2 * i + 1];
                    }

                    info.values = realParts;
                    info.z = imgParts;

                    values = null;

                    break;
                default:
                    break;
            }

            GC.Collect();
            return info;

        }
Ejemplo n.º 27
0
        public static SparseMatrixComplex operator *(SparseMatrixComplex 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");
            }

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

            int leftNNZ  = left.mapData.Count;
            int rightNNZ = right.mapData.Count;

            #region Left < Right
            //We use right as stardand sight
            //if (leftNNZ < rightNNZ)
            //{
            //Connection nonezero for each row of matrix a
            List <KeyValuePair <int, Complex> >[] bRows = new List <KeyValuePair <int, Complex> > [right.rowCount];
            for (int i = 0; i < bRows.Length; i++)
            {
                bRows[i] = new List <KeyValuePair <int, Complex> >();
            }


            foreach (KeyValuePair <Pair, Complex> item in right.mapData)
            {
                Pair    pair  = item.Key;
                Complex value = item.Value;

                bRows[pair.Key].Add(new KeyValuePair <int, Complex>(pair.Value, value));
            }

            //Compute C = A*B
            foreach (KeyValuePair <Pair, Complex> item in left.mapData)
            {
                Pair    pair  = item.Key;
                int     mA    = pair.Key;
                int     nA    = pair.Value;
                Complex value = item.Value;

                List <KeyValuePair <int, Complex> > bRow = bRows[nA];

                for (int i = 0; i < bRow.Count; i++)
                {
                    int k = bRow[i].Key;

                    Pair pair2 = new Pair(mA, k);

                    if (result.mapData.ContainsKey(pair2))
                    {
                        result.mapData[pair2] += value * bRow[i].Value;
                    }
                    else
                    {
                        result.mapData.Add(pair2, value * bRow[i].Value);
                    }
                }
            }

            //}
            #endregion
            #region Right < Left

            //else if (leftNNZ > rightNNZ)
            //{
            //    //Connection nonezero for each row of matrix a
            //    List<KeyValuePair<int, double>>[] aCols = new List<KeyValuePair<int, double>>[left.columnCount];
            //    for (int i = 0; i < aCols.Length; i++)
            //    {
            //        aCols[i] = new List<KeyValuePair<int, double>>();
            //    }

            //    foreach (KeyValuePair<Pair, double> item in left.mapData)
            //    {
            //        Pair pair = item.Key;
            //        double value = item.Value;

            //        aCols[pair.Value].Add(new KeyValuePair<int, double>(pair.Key, value));
            //    }

            //    //Compute C = A*B
            //    foreach (KeyValuePair<Pair, double> item in right.mapData)
            //    {
            //        Pair pair = item.Key;
            //        int mA = pair.Key;
            //        int nA = pair.Value;
            //        double value = item.Value;

            //        List<KeyValuePair<int, double>> aCol = aCols[mA];

            //        for (int i = 0; i < aCol.Count; i++)
            //        {
            //            int k = aCol[i].Key;

            //            Pair pair2 = new Pair(k, nA);

            //            if (result.mapData.ContainsKey(pair2))
            //            {
            //                result.mapData[pair2] += value * aCol[i].Value;
            //            }
            //            else
            //            {
            //                result.mapData.Add(pair2, value * aCol[i].Value);
            //            }

            //        }
            //    }

            //}


            #endregion

            return(result);
        }
Ejemplo n.º 28
0
 public SparseMatrixComplex(SparseMatrixComplex matrix)
 {
     mapData          = new Dictionary <Pair, Complex>(matrix.mapData);
     this.rowCount    = matrix.rowCount;
     this.columnCount = matrix.columnCount;
 }
Ejemplo n.º 29
0
 public SparseMatrixComplex(SparseMatrixComplex matrix)
 {
     mapData = new Dictionary<Pair, Complex>(matrix.mapData);
     this.rowCount = matrix.rowCount;
     this.columnCount = matrix.columnCount;
 }
Ejemplo n.º 30
0
        public static SparseMatrixComplex operator *(SparseMatrixComplex 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");
            }

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

            int leftNNZ = left.mapData.Count;
            int rightNNZ = right.mapData.Count;

            #region Left < Right
            //We use right as stardand sight
            //if (leftNNZ < rightNNZ)
            //{
            //Connection nonezero for each row of matrix a 
            List<KeyValuePair<int, Complex>>[] bRows = new List<KeyValuePair<int, Complex>>[right.rowCount];
            for (int i = 0; i < bRows.Length; i++)
            {
                bRows[i] = new List<KeyValuePair<int, Complex>>();
            }


            foreach (KeyValuePair<Pair, Complex> item in right.mapData)
            {
                Pair pair = item.Key;
                Complex value = item.Value;

                bRows[pair.Key].Add(new KeyValuePair<int, Complex>(pair.Value, value));
            }

            //Compute C = A*B
            foreach (KeyValuePair<Pair, Complex> item in left.mapData)
            {
                Pair pair = item.Key;
                int mA = pair.Key;
                int nA = pair.Value;
                Complex value = item.Value;

                List<KeyValuePair<int, Complex>> bRow = bRows[nA];

                for (int i = 0; i < bRow.Count; i++)
                {
                    int k = bRow[i].Key;

                    Pair pair2 = new Pair(mA, k);

                    if (result.mapData.ContainsKey(pair2))
                    {
                        result.mapData[pair2] += value * bRow[i].Value;
                    }
                    else
                    {
                        result.mapData.Add(pair2, value * bRow[i].Value);
                    }


                }
            }

            //}
            #endregion
            #region Right < Left

            //else if (leftNNZ > rightNNZ)
            //{
            //    //Connection nonezero for each row of matrix a 
            //    List<KeyValuePair<int, double>>[] aCols = new List<KeyValuePair<int, double>>[left.columnCount];
            //    for (int i = 0; i < aCols.Length; i++)
            //    {
            //        aCols[i] = new List<KeyValuePair<int, double>>();
            //    }

            //    foreach (KeyValuePair<Pair, double> item in left.mapData)
            //    {
            //        Pair pair = item.Key;
            //        double value = item.Value;

            //        aCols[pair.Value].Add(new KeyValuePair<int, double>(pair.Key, value));
            //    }

            //    //Compute C = A*B
            //    foreach (KeyValuePair<Pair, double> item in right.mapData)
            //    {
            //        Pair pair = item.Key;
            //        int mA = pair.Key;
            //        int nA = pair.Value;
            //        double value = item.Value;

            //        List<KeyValuePair<int, double>> aCol = aCols[mA];

            //        for (int i = 0; i < aCol.Count; i++)
            //        {
            //            int k = aCol[i].Key;

            //            Pair pair2 = new Pair(k, nA);

            //            if (result.mapData.ContainsKey(pair2))
            //            {
            //                result.mapData[pair2] += value * aCol[i].Value;
            //            }
            //            else
            //            {
            //                result.mapData.Add(pair2, value * aCol[i].Value);
            //            }

            //        }
            //    }

            //}


            #endregion

            return result;
        }
Ejemplo n.º 31
0
        /*
         * For Complex Calculation
         */
        public void FactorizationLU(ref SparseMatrixComplex A)
        {
            CholmodInfo cholmodA = CholmodConverter.cConverter(ref A,
                                                              CholmodInfo.CholmodMatrixStorage.CCS);

            m = A.RowCount;
            n = A.ColumnCount;

            fixed (int* Index = cholmodA.rowIndex, Pt = cholmodA.colIndex)
            fixed (double* val = cholmodA.values)
            {
                solver = CreateSolverLUUMFPACK_CCS_Complex(cholmodA.RowCount,
                                                           cholmodA.ColumnCount,
                                                           cholmodA.nnz,
                                                           Index,
                                                           Pt,
                                                           val
                                                           );
            }


            if (solver == null) throw new Exception("Create Solver Fail");
        }
Ejemplo n.º 32
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;
        }
Ejemplo n.º 33
0
        public static CholmodInfo cConverter(ref SparseMatrixComplex A, CholmodInfo.CholmodMatrixStorage storage, CholmodInfo.CholmodMatrixStoageMethod method)
        {
            CholmodInfo info = new CholmodInfo();

            info.MatrixType          = CholmodInfo.CholmodMatrixType.Sparse;
            info.MatrixStorageType   = storage;
            info.MatrixStorageMethod = method;
            info.MatrixItemType      = CholmodInfo.CholmodMatrixItemType.Complex;
            info.RowCount            = A.RowCount;
            info.ColumnCount         = A.ColumnCount;

            double[] values = null;

            switch (storage)
            {
            case CholmodInfo.CholmodMatrixStorage.CRS:
                A.ToCRS(out info.colIndex, out info.rowIndex, out values, out info.nnz);
                break;

            case CholmodInfo.CholmodMatrixStorage.CCS:
                A.ToCCS(out info.colIndex, out info.rowIndex, out values, out info.nnz);
                break;

            case CholmodInfo.CholmodMatrixStorage.Triplet:
                A.ToTriplet(out info.colIndex, out info.rowIndex, out values, out info.z, out info.nnz);
                break;

            default:
                A.ToTriplet(out info.colIndex, out info.rowIndex, out values, out info.z, out info.nnz);

                break;
            }

            switch (method)
            {
            case CholmodInfo.CholmodMatrixStoageMethod.Normal:
                info.values = values;
                break;

            case CholmodInfo.CholmodMatrixStoageMethod.Divided:
                double[] realParts = new double[info.nnz];
                double[] imgParts  = new double[info.nnz];

                for (int i = 0; i < info.nnz; i++)
                {
                    realParts[i] = values[2 * i];
                    imgParts[i]  = values[2 * i + 1];
                }

                info.values = realParts;
                info.z      = imgParts;

                values = null;

                break;

            default:
                break;
            }

            GC.Collect();
            return(info);
        }
Ejemplo n.º 34
0
        public static SparseMatrixComplex Copy(ref SparseMatrixComplex B)
        {
            Dictionary<Pair, Complex> newData = new Dictionary<Pair, Complex>(B.mapData);

            SparseMatrixComplex newMatrix = new SparseMatrixComplex(B.rowCount, B.columnCount);
            newMatrix.mapData = newData;

            return newMatrix;
        }
Ejemplo n.º 35
0
        public static SparseMatrixComplex Copy(ref SparseMatrixDouble B)
        {
            //We only copy of realpart
            Dictionary<Pair, Complex> newData = new Dictionary<Pair, Complex>();

            foreach (KeyValuePair<Pair, double> e in B.Datas)
            {
                Pair pair = e.Key;
                double value = e.Value;

                newData.Add(pair, new Complex(value, 0));
            }


            SparseMatrixComplex newMatrix = new SparseMatrixComplex(B.RowCount, B.ColumnCount);
            newMatrix.mapData = newData;

            return newMatrix;
        }
Ejemplo n.º 36
0
        public static SparseMatrixComplex Identity(int N)
        {
            SparseMatrixComplex identity = new SparseMatrixComplex(N, N);

            for (int i = 0; i < N; i++)
            {
                identity.mapData.Add(new Pair(i, i), Complex.Identity);
            }

            return identity;
        }
Ejemplo n.º 37
0
        public SparseMatrixComplex cBuildHodgeStar0Form(TriMesh mesh)
        {
            SparseMatrixComplex star0 = new SparseMatrixComplex(mesh.Vertices.Count, mesh.Vertices.Count);

            foreach (TriMesh.Vertex vertex in mesh.Vertices)
            {
                Complex value = new Complex(ComputeVertexDualArea(vertex), 0);

                star0[vertex.Index, vertex.Index] = value;
            }

            return star0;
        }
Ejemplo n.º 38
0
        public static SparseMatrixComplex operator -(SparseMatrixComplex left, SparseMatrixComplex right)
        {
            SparseMatrixComplex result = new SparseMatrixComplex(right.rowCount, right.columnCount);

            foreach (KeyValuePair<Pair, Complex> item in left.mapData)
            {
                Pair pair = item.Key;
                Complex value = item.Value;

                result.mapData.Add(pair, value);
            }

            foreach (KeyValuePair<Pair, Complex> item in right.mapData)
            {
                Pair pair = item.Key;
                Complex value = item.Value;

                if (result.mapData.ContainsKey(pair))
                {
                    Complex temp = result.mapData[pair] -= item.Value;
                    if (temp.Equals(Complex.Zero))
                    {
                        result.mapData.Remove(pair);
                    }
                }
                else
                {
                    result.mapData.Add(pair, value);
                }
            }

            return result;
        }
Ejemplo n.º 39
0
        public SparseMatrixComplex SubMatrix(int rowStart, int rowEnd, int columnStart, int columnEnd)
        {
            if (rowEnd < rowStart || columnEnd < columnStart)
            {
                throw new ArgumentOutOfRangeException();
            }

            int rows = rowEnd - rowStart + 1;
            int columns = columnEnd - columnStart + 1;

            SparseMatrixComplex subMatrix = new SparseMatrixComplex(rows, columns);

            int subMatrixEntiries = rows * columns;

            if (subMatrixEntiries < mapData.Count)
            {
                for (int i = 0; i < rows; i++)
                {
                    int rowInx = i + rowStart;
                    for (int j = 0; j < columns; j++)
                    {
                        int columnInx = j + columnStart;
                        Pair pair = new Pair(rowInx, columnInx);
                        if (!mapData.ContainsKey(pair))
                        {
                            continue;
                        }

                        Pair subPair = new Pair(i, j);
                        subMatrix.mapData[subPair] = mapData[pair];

                    }
                }
            }
            else if (subMatrixEntiries >= mapData.Count)
            {
                foreach (KeyValuePair<Pair, Complex> item in mapData)
                {
                    Pair pair = item.Key;
                    Complex value = item.Value;

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

                    if ((m >= rowStart && m <= rowEnd) && (n >= columnStart && m <= columnEnd))
                    {
                        int i = m - rowStart;
                        int j = n - columnStart;

                        subMatrix.mapData[new Pair(i, j)] = value;
                    }

                }
            }

            return subMatrix;
        }
Ejemplo n.º 40
0
        public SparseMatrixComplex cBuildHodgeStar1Form(TriMesh mesh)
        {
            SparseMatrixComplex star1 = new SparseMatrixComplex(mesh.Edges.Count, mesh.Edges.Count);

            foreach (TriMesh.Edge edge in mesh.Edges)
            {
                double cotAlpha = ComputeTan(edge.HalfEdge0);

                double cotBeta = ComputeTan(edge.HalfEdge1);

                Complex value = new Complex((cotAlpha + cotBeta) / 2, 0);

                star1[edge.Index, edge.Index] = value;
            }


            return star1;
        }
Ejemplo n.º 41
0
        public SparseMatrixComplex Transpose()
        {

            SparseMatrixComplex trMatrix = new SparseMatrixComplex(this.columnCount, this.rowCount);

            foreach (KeyValuePair<Pair, Complex> item in mapData)
            {
                Pair pair = item.Key;
                Complex value = item.Value;

                trMatrix.mapData[new Pair(pair.Value, pair.Key)] = value;
            }

            return trMatrix;
        }