Ejemplo n.º 1
0
Archivo: Misc.cs Proyecto: xyuan/BoSSS
        /// <summary>
        /// creates an identity transformation
        /// </summary>
        /// <param name="D">spatial dimension</param>
        /// <returns></returns>
        public static AffineTrafo ID(int D)
        {
            AffineTrafo tr = new AffineTrafo();

            tr.Matrix = FullMatrix.Eye(D);
            tr.Affine = new double[D];
            return(tr);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// entriesAsColMajor is assumed to be col major form
        /// </summary>
        public static IMatrix<double> GetMatrix(Guid id, int nodeCount, string entriesAsColMajor)
        {
            double[] components = null;
            if (!MatrixHelper.ParseEntryString(nodeCount, entriesAsColMajor, out components))
                throw new ArgumentException(string.Format("Could not parse the matrix entry string:  \"{0}\"", entriesAsColMajor), "entriesAsColMajor");

            IMatrix<double> target = new FullMatrix<Double>(id, components, nodeCount);
            return target;
        }
Ejemplo n.º 3
0
        void PlotCell0()
        {
            BoundingBox BB = new BoundingBox(2);

            GridDat.Cells.GetCellBoundingBox(0, BB);

            int Nx     = 50;
            int Ny     = 55;
            var xNodes = Grid1D.Linspace(BB.Min[0], BB.Max[0], Nx);
            var yNodes = Grid1D.Linspace(BB.Min[1], BB.Max[1], Ny);

            var GlobalNodes = MultidimensionalArray.Create(Nx, Ny, 2);

            for (int i = 0; i < Nx; i++)
            {
                for (int j = 0; j < Ny; j++)
                {
                    GlobalNodes[i, j, 0] = xNodes[i];
                    GlobalNodes[i, j, 1] = yNodes[j];
                }
            }

            var GlobalNodes_C = GlobalNodes.ResizeShallow(Nx * Ny, 2);
            var LocalNodes    = MultidimensionalArray.Create(1, Nx * Ny, 2);

            GridDat.TransformGlobal2Local(GlobalNodes_C, LocalNodes, 0, 1, 0);

            var Values  = MultidimensionalArray.Create(1, Nx, Ny);
            var Values_ = Values.ResizeShallow(1, Nx * Ny);

            var Gradients  = MultidimensionalArray.Create(1, Nx, Ny, 2);
            var Gradients_ = Gradients.ResizeShallow(1, Nx * Ny, 2);


            var NodeSet = GridDat.NSC.CreateContainer(LocalNodes.ExtractSubArrayShallow(0, -1, -1), 0);
            var lh      = GridDat.NSC.LockNodeSetFamily(NodeSet);

            f1.Evaluate(0, 1, 0, Values_);
            f1.EvaluateGradient(0, 1, 0, Gradients_);
            GridDat.NSC.UnlockNodeSetFamily(lh);


            xNodes.SaveToTextFile("C:\\tmp\\xNodes.txt");
            yNodes.SaveToTextFile("C:\\tmp\\yNodes.txt");

            FullMatrix output1 = new FullMatrix(Values.ExtractSubArrayShallow(0, -1, -1));

            output1.ToTxtFile("C:\\tmp\\f.txt");

            FullMatrix output2 = new FullMatrix(Gradients.ExtractSubArrayShallow(0, -1, -1, 0));

            output2.ToTxtFile("C:\\tmp\\df.txt");
        }
Ejemplo n.º 4
0
Archivo: Misc.cs Proyecto: xyuan/BoSSS
        /// <summary>
        /// returns a 2D transformation
        /// </summary>
        public static AffineTrafo Some2DRotation(double angle)
        {
            AffineTrafo Trafo = new AffineTrafo();


            FullMatrix dreh = new FullMatrix(2, 2);

            dreh[0, 0]   = Math.Cos(angle); dreh[0, 1] = -Math.Sin(angle);
            dreh[1, 0]   = Math.Sin(angle); dreh[1, 1] = Math.Cos(angle);
            Trafo.Affine = new double[2];
            Trafo.Matrix = dreh;

            return(Trafo);
        }
        public void BuildEmptyFullMatrix(Int32 nodeCount)
        {
            var fullMatrix = new FullMatrix<Boolean>(Guid.NewGuid(), nodeCount);

            Assert.NotNull(fullMatrix);

            Int32 count =
                Enumerable.Range(0, nodeCount).AsParallel()
                    .Select(i =>
                        Enumerable.Range(i, nodeCount - i).AsParallel()
                            .Count(j => fullMatrix[i, j] != false)
                    ).Count(v => v != 0);

            Assert.Equal(0, count);
        }
        public void BuildFullMatrixFromDoubleArray()
        {
            Int32[,] dblArray = new Int32[,] {
                {1, 2, 3},
                {2, 1, 2},
                {3, 2, 1}
            };

            var fullMatrix = new FullMatrix<Int32>(Guid.NewGuid(), dblArray);

            Assert.NotNull(fullMatrix);

            Assert.Equal(dblArray[0, 0], fullMatrix[0, 0]);
            Assert.Equal(dblArray[0, 1], fullMatrix[0, 1]);
            Assert.Equal(dblArray[2, 1], fullMatrix[2, 1]);
        }
Ejemplo n.º 7
0
        public static IMatrix<double> GetMatrix(int nodeCount, string entriesAsColMajor, bool symmetric)
        {
            double[] components = null;
            if (!MatrixHelper.ParseEntryString(nodeCount, entriesAsColMajor, out components))
                throw new ArgumentException(string.Format("Could not parse the matrix entry string:  \"{0}\"", entriesAsColMajor), "entriesAsColMajor");

            IMatrix<double> target = null;
            if (symmetric)
            {
                target = new SymmetricMatrix<double>(Guid.NewGuid(), components, nodeCount);
            }
            else
            {
                target = new FullMatrix<double>(Guid.NewGuid(), components, nodeCount);
            }
            return target;
        }
        public void BuildFullMatrixFromSingleArray()
        {
            Int32[] sglArray = new Int32[] {
                1, 2, 3,
                2, 1, 2,
                3, 2, 1
            };

            var fullMatrix = new FullMatrix<Int32>(Guid.NewGuid(), sglArray, 3);

            Assert.NotNull(fullMatrix);

            Assert.Equal(sglArray[0], fullMatrix[0, 0]);
            Assert.Equal(sglArray[2], fullMatrix[0, 2]);
            Assert.Equal(sglArray[6], fullMatrix[2, 0]);
            Assert.Equal(sglArray[7], fullMatrix[2, 1]);
        }
Ejemplo n.º 9
0
        void OrthonormalityTest()
        {
            Basis B   = f1.Basis;
            int   N   = B.Length;
            var   Mtx = new FullMatrix(N, N);

            double TotErrSum = 0.0;

            CellQuadrature.GetQuadrature(new int[] { N, N }, this.GridDat,
                                         (new CellQuadratureScheme(true)).Compile(GridDat, Math.Min(B.Degree * 2, 16)),
                                         delegate(MultidimensionalArray NodesUntransformed, int iKref) {
                var ret = new NodeSetController.NodeSetContainer[] {
                    GridDat.NSC.CreateContainer(NodesUntransformed, iKref)
                };
                return(ret);
            },
                                         delegate(int i0, int Length, int NoOfNodes, MultidimensionalArray EvalResult) { // void Del_Evaluate
                var BasisVal = B.CellEval(0, i0, Length);

                EvalResult.Multiply(1.0, BasisVal, BasisVal, 0.0, "jknm", "jkn", "jkm");
            },
                                         delegate(int i0, int Length, MultidimensionalArray ResultsOfIntegration) { // SaveIntegrationResults
                for (int i = 0; i < Length; i++)
                {
                    var MassMtx = ResultsOfIntegration.ExtractSubArrayShallow(i, -1, -1);

                    double errsum = 0;
                    for (int n = 0; n < N; n++)
                    {
                        for (int m = 0; m < N; m++)
                        {
                            double soll = (n == m) ? 1.0 : 0.0;
                            errsum     += Math.Abs(MassMtx[m, n] - soll);
                        }
                    }

                    TotErrSum += errsum;
                }
            }).Execute();


            Console.WriteLine("orthonormality error sum:" + TotErrSum);
        }
        public void IsBinaryReturnsTrueAndFalseForInt32Matrix()
        {
            Int32[,] dblArray = new Int32[,] {
                {0, 0, 1, 0},
                {1, 1, 0, 0},
                {0, 0, 0, 0},
                {1, 0, 0, 1}
            };

            var fullMatrix = new FullMatrix<Int32>(Guid.NewGuid(), dblArray);
            Assert.True(fullMatrix.IsBinary);

            fullMatrix.SetEdge(2, 2, 3);
            Assert.False(fullMatrix.IsBinary);
        }
        public void SuccessorsThrowsExceptionIfIndexIsOutOfRange()
        {
            Int32[,] dblArray = new Int32[,] {
                {1, 2, 3},
                {2, 1, 2},
                {3, 2, 1}
            };

            var fullMatrix = new FullMatrix<Int32>(Guid.NewGuid(), dblArray);
            Assert.Throws<IndexOutOfRangeException>(()
                => fullMatrix.Successors(4));
            Assert.Throws<IndexOutOfRangeException>(()
                => fullMatrix.Successors(-1));
        }
        public void SetUpdatesMatrixEntry(int i, int j, double newValue)
        {
            Double[,] dblArray = new Double[,] {
                {0, 0, 0},
                {0, 0, 0},
                {0, 0, 0},
            };

            var fullMatrix = new FullMatrix<Double>(Guid.NewGuid(), dblArray);
            fullMatrix.SetEdge(i, j, newValue);
            Assert.Equal(newValue, fullMatrix[i, j]);
        }
        public void SetEdgeThrowsExceptionIfIndexIsOutOfRange()
        {
            Int32[,] dblArray = new Int32[,] {
                {1, 2, 3},
                {2, 1, 2},
                {3, 2, 1}
            };

            var fullMatrix = new FullMatrix<Int32>(Guid.NewGuid(), dblArray);
            Assert.Throws<IndexOutOfRangeException>(()
                => fullMatrix.SetEdge(4, 0, 3));
            Assert.Throws<IndexOutOfRangeException>(()
                => fullMatrix.SetEdge(1, -1, -10));
        }
        public void IsBinaryReturnsTrueForBooleanMatrix()
        {
            Boolean[,] dblArray = new Boolean[,] {
                {false, false, true, false},
                {true, true, false, false},
                {false, false, false, false},
                {true, true, false, true}
            };

            var fullMatrix = new FullMatrix<Boolean>(Guid.NewGuid(), dblArray);
            Assert.True(fullMatrix.IsBinary);
        }
        public void IsBinaryReturnsTrueAndFalseForDoubleMatrix()
        {
            Double[,] dblArray = new Double[,] {
                {0, 0, 1, 0},
                {1, 1, 0, 0},
                {0, 0, 0, 0},
                {1, 0, 0, 1}
            };

            var fullMatrix = new FullMatrix<Double>(Guid.NewGuid(), dblArray);
            Assert.True(fullMatrix.IsBinary);

            fullMatrix.SetEdge(2, 2, 1.444);
            Assert.False(fullMatrix.IsBinary);
        }
        public void InDegreeThrowsExceptionIfIndexIsOutOfRange()
        {
            Int32[,] dblArray = new Int32[,] {
                {1, 2, 3},
                {2, 1, 2},
                {3, 2, 1}
            };

            var fullMatrix = new FullMatrix<Int32>(Guid.NewGuid(), dblArray);
            Assert.Throws<AggregateException>(()
                => fullMatrix.InDegree(4));
            Assert.Throws<AggregateException>(()
                => fullMatrix.InDegree(-1));
        }
Ejemplo n.º 17
0
Archivo: Misc.cs Proyecto: xyuan/BoSSS
        /// <summary>
        /// computes matrix and affine vector of the affine-linear transformation
        /// that maps the vectors in the <paramref name="preimage"/> to <paramref name="image"/>;
        /// </summary>
        /// <param name="preimage">
        /// The preimage: (D+1) vectors of dimension D;
        /// <list type="bullet">
        ///   <item>1st index: vector/vertex index, length is D+1</item>
        ///   <item>2nd index: spatial dimension, length is D</item>
        /// </list>
        /// Tip: use <see cref="ilPSP.Utils.ArrayTools.Transpose{t}(t[,],t[,])"/>
        /// if the sequence of indices is not appropriate;
        /// </param>
        /// <param name="image">
        /// The image: (D+1) vectors of dimension D;
        /// <list type="bullet">
        ///   <item>1st index: vector/vertex index, length is D+1</item>
        ///   <item>2nd index: spatial dimension, length is D</item>
        /// </list>
        /// </param>
        /// <returns></returns>
        public static AffineTrafo FromPoints(double[,] preimage, double[,] image)
        {
            int D = preimage.GetLength(1);

            if (image.GetLength(1) != D || image.GetLength(0) != D + 1)
            {
                throw new ArgumentException("wrong size; expecting " + D + "x" + (D + 1) + " - array;", "image");
            }
            if (preimage.GetLength(0) != D + 1)
            {
                throw new ArgumentException("wrong size; expecting " + D + "x" + (D + 1) + " - array;", "preimage");
            }


            FullMatrix M = new FullMatrix(D * (D + 1), D * (D + 1));

            double[] rhs = new double[M.NoOfCols];
            double[] x   = (double[])rhs.Clone();

            // gesucht: affine lineare Transformation: "xi -> x"
            // preimage = xi, image = x;
            //
            //          x = Tr*xi + o
            //
            // Sei z.B. D=2 (andere D's ergeben sich analog)
            // x1 = (x11,x12)^T, x2 = (x21,x22)^T, x3 seien die Bilder von
            // xi1 = (xi11,xi12), xi2, xi3.
            //
            // gesucht sind die Matrix Tr und der Vektor o = (o1,o2)^T,
            //
            //                   [ Tr11  Tr12 ]
            //              Tr = [            ],
            //                   [ Tr21  Tr22 ]
            //
            // welche mit einem Glsys. der folgenden Form berechnet werden:
            //
            // [ 1 0   xi11 xi12     0    0  ]   [ o1  ]   [ x11 ]
            // [ 0 1    0    0     xi11 xi12 ]   [ o2  ]   [ x12 ]
            // [                             ]   [  0  ]   [     ]
            // [ 1 0   xi21 xi22     0    0  ] * [ T11 ] = [ x21 ]
            // [ 0 1    0    0     xi21 xi22 ]   [ T12 ]   [ x22 ]
            // [                             ]   [     ]   [     ]
            // [ 1 0   xi31 xi32     0    0  ]   [ T21 ]   [ x31 ]
            // [ 0 1    0    0     xi31 xi32 ]   [ T22 ]   [ x31 ]
            //
            //


            // build rhs
            for (int d = 0; d < (D + 1); d++)
            {
                for (int dd = 0; dd < D; dd++)
                {
                    rhs[d * D + dd] = image[d, dd];
                }
            }


            // build matrix
            M.Clear();
            for (int d = 0; d < (D + 1); d++)
            {
                for (int dd = 0; dd < D; dd++)
                {
                    M[d * D + dd, dd] = 1.0;

                    for (int ddd = 0; ddd < D; ddd++)
                    {
                        M[d * D + dd, D + D * dd + ddd] = preimage[d, ddd];
                    }
                }
            }

            // solve Matrix
            M.Solve(x, rhs);

            // save results, return
            AffineTrafo Trafo = new AffineTrafo();

            Trafo.Affine = new double[D];
            Trafo.Matrix = new FullMatrix(D, D);
            for (int d = 0; d < D; d++)
            {
                Trafo.Affine[d] = x[d];

                for (int dd = 0; dd < D; dd++)
                {
                    Trafo.Matrix[d, dd] = x[D + d * D + dd];
                }
            }
            return(Trafo);
        }