Exemplo n.º 1
0
        public void TestSymmetricity()
        {
            var matrix = new SymmetricMatrix<double>(5);
            matrix[1, 3] = 1.0;

            Assert.AreEqual(1.0, matrix[3, 1]);
        }
        static void Main(string[] args)
        {
            int[,] array = new int[3, 3] { { 1, 4, 5 }, { 52, 34, 3 }, { 6, 734, 8 } };                                                                                    
            SquareMatrix<int> m = new SquareMatrix<int>(array);
            Console.WriteLine(m.ToString());
            m.Change += IsChange;
            m[1, 2] = 100;
            Thread.Sleep(1000);
            Console.WriteLine();
            Console.WriteLine(m.ToString());

            int[,] arrayM = new int[3, 3] { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };                                        
            Matrix<int> firstM = new SquareMatrix<int>(array);
            Console.WriteLine(firstM.ToString());
            Matrix<int> secondM = new SymmetricMatrix<int>(arrayM);
            Console.WriteLine(secondM.ToString());
            var result = WorkWithMatrix.Add( firstM,secondM, (a, b) => a + b);
            System.Console.WriteLine(result.ToString());

            
            int [,] arD1 = new int[3,3]{{1,0,0},{0,1,0},{0,0,1}};
            int[,] arD2 = new int[3, 3] { { 2, 0, 0 }, { 0, 2, 0 }, { 0, 0, 2 } };
            var fisrtDM = new DiagonalMatrix<int>(arD1);
            Console.WriteLine(fisrtDM.ToString());
            var secondDM = new DiagonalMatrix<int>(arD2);
            Console.WriteLine(secondDM.ToString());
            result = WorkWithMatrix.Add(fisrtDM, secondDM, (a, b) => a + b);
            System.Console.WriteLine(result.ToString());


            Console.ReadKey();
        }
Exemplo n.º 3
0
		/// <summary>Compute the overlap between the vectors in a binary matrix</summary>
		/// <returns>a sparse matrix with the overlaps</returns>
		/// <param name='entity_data'>the binary matrix</param>
		public static Tuple<IMatrix<float>, IList<float>> ComputeWeighted(IBooleanMatrix entity_data)
		{
			var transpose = (IBooleanMatrix) entity_data.Transpose();

			var other_entity_weights = new float[transpose.NumberOfRows];
			for (int row_id = 0; row_id < transpose.NumberOfRows; row_id++)
			{
				int freq = transpose.GetEntriesByRow(row_id).Count;
				other_entity_weights[row_id] = 1f / (float) Math.Log(3 + freq, 2); // TODO make configurable
			}

			IMatrix<float> weighted_overlap = new SymmetricMatrix<float>(entity_data.NumberOfRows);
			IList<float> entity_weights = new float[entity_data.NumberOfRows];

			// go over all (other) entities
			for (int row_id = 0; row_id < transpose.NumberOfRows; row_id++)
			{
				var row = transpose.GetEntriesByRow(row_id);
				for (int i = 0; i < row.Count; i++)
				{
					int x = row[i];
					entity_weights[x] += other_entity_weights[row_id];
					for (int j = i + 1; j < row.Count; j++)
					{
						int y = row[j];
						weighted_overlap[x, y] += other_entity_weights[row_id] * other_entity_weights[row_id];
					}
				}
			}

			return Tuple.Create(weighted_overlap, entity_weights);
		}
Exemplo n.º 4
0
		[Test()] public void TestSymmetricity()
		{
			var matrix = new SymmetricMatrix<float>(5);
			matrix[1, 3] = 1.0f;

			Assert.AreEqual(1.0, matrix[3, 1]);
		}
        static void Main(string[] args)
        {
            int[,] matr1 = { { 1, 2, 3 }, 
                            { 1, 2, 3 }, 
                            { 1, 2, 3 } };
            SquareMatrix<int> matrix1 = new SquareMatrix<int>(matr1);

            int[,] matr2 = { { 1, 0, 0 }, 
                            { 0, 2, 0 }, 
                            { 0, 0, 3 } };
            DiagonalMatrix<int> matrix2 = new DiagonalMatrix<int>(matr2);

            int[,] matr3 = { { 1, 5, 3 }, 
                            { 5, 2, 7 }, 
                            { 3, 7, 3 } };
            SymmetricMatrix<int> matrix3 = new SymmetricMatrix<int>(matr3);

            var squar = new Listeners<int>();
            squar.Register(matrix1);
            squar.Register(matrix2);
            squar.Register(matrix3);

             matrix1[1, 2] = 3;
             matrix2[2, 2] = 3;
             matrix3[1, 0] = 3;


             SquareMatrix<int> output = matrix1.Sum(matrix2, (x, y) => x + y);
             SquareMatrix<int> output2 = matrix1.Sum(matrix2);
             Console.WriteLine();
            }
        static void Main(string[] args)
        {
            int[,] first =
            {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
            };
            int[,] second =
            {
                {1, 2, 3},
                {2, 5, 6},
                {3, 6, 9}
            };
            int[,] third =
            {
                {1, 0, 0},
                {0, 5, 0},
                {0, 0, 9}
            };
            SquareMatrix<int> matrix = new SquareMatrix<int>(3);
            SquareMatrix<int> lol1 = new SquareMatrix<int>(first);
            SymmetricMatrix<int> lol2 = new SymmetricMatrix<int>(second);
            DiagonalMatrix<int> lol3 = new DiagonalMatrix<int>(third);
            matrix.MatrixChange += ShowChanges;
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    matrix[i, j] = i*j;

            var lol4 = lol3.Add(lol1);
            System.Console.ReadLine();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Computes four correlated deviates for Monte Carlo simulation.
        /// </summary>
        /// <param name="R">Correlation matrix (which is always symmetric).</param>
        /// <param name="dt">Time step.</param>
        /// <param name="z">Array to store correlated deviates.</param>
        /// <returns></returns>
        public double[] GenerateCorrelatedDeviates(SymmetricMatrix R, double dt, double[] z)
        {
            double sum =  0.0;
            // standard normal deviate
            double deviate = 0.0;
            // number of rows in correlation matrix.
            int m = R.GetNumberOfRows();
            // list of correlated deviates
            //List<double> dz;
            // list of eigenvalues
            List<double> eigenValues = new List<double>();
            // array of eigenvectors
            List<double>[] eigenVectors = new List<double>[4];
            // stores eigenvalues of correlation matrix R
            double[] lambda = new double[] { 0.0, 0.0, 0.0, 0.0 };
            // stores correlated deviates
            double[] dw = new double[] { 0.0, 0.0, 0.0, 0.0 };

            DiagonalMatrix D = R.GetEigenValues();
            Matrix V = GenerateEigenVectors(R);

            // store eigen values
            for (int i = 0; i < m; i++)
            {
                eigenValues.Add(D.GetElement(i, i));
                lambda[i] = D.GetElement(i, i);
            }

            // stores rows of eigenvectors so that we can compute
            // dz[i] = v[i][1]*sqrt(eigenvalue[1])*dw1 + v[i][2]*sqrt(eigenvalue[2])*dw2 + ...
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    eigenVectors[i].Add(V.GetElement(i, j));
                }
            }

            Random randomNumberGenerator = new Random();
            long seed = randomNumberGenerator.Next() % 100;

            // generate uncorrelated deviates
            for (int i = 0; i < m; i++)
            {
                deviate = NormalDeviate(ref seed);
                dw[i] = deviate * Math.Sqrt(dt);
            }

            // generate correlated deviates
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    sum += eigenVectors[i][j] * Math.Sqrt(eigenValues[j]) * dw[j];
                }
                z[i] = sum;
            }
            return z;
        }
Exemplo n.º 8
0
		[Test()] public void TestIsSymmetric()
		{
			var matrix = new SymmetricMatrix<float>(5);
			Assert.IsTrue(matrix.IsSymmetric);

			matrix[1, 3] = 1.0f;
			Assert.IsTrue(matrix.IsSymmetric);
		}
Exemplo n.º 9
0
        public void TestIsSymmetric()
        {
            var matrix = new SymmetricMatrix<double>(5);
            Assert.IsTrue(matrix.IsSymmetric);

            matrix[1, 3] = 1.0;
            Assert.IsTrue(matrix.IsSymmetric);
        }
 public void SymmetricMatrix_Create_Test()
 {
     int[,] matr = { { 1, 5, 3 }, 
                     { 5, 2, 7 }, 
                     { 3, 7, 3 } };
     SymmetricMatrix<int> matrix = new SymmetricMatrix<int>(matr);
     Assert.AreEqual(5, matrix[0, 1]);
 }
        public void Add_IntDiagonalAndSymmetricMatrixes_ReturnesSymmetricMatrix()
        {
            var m1 = new DiagonalMatrix<int>(new int[][] { new int[] { 1, 0, 0 }, new int[] { 0, 1, 0 }, new int[] { 0, 0, 1 } });
            var m2 = new SymmetricMatrix<int>(new int[][] { new int[] { 1, 4, 5 }, new int[] { 4, 2, 6 }, new int[] { 5, 6, 3 } });

            dynamic actual = m1.Add(m2);

            Assert.IsTrue(actual is SymmetricMatrix<int>);
        }
        public void CreatingSymmetricMatrix()
        {
            Matrix<int> m = new SymmetricMatrix<int>(new int[3][]
            {
                new int[3] {1, 2, 3},
                new int[3] {2, 3, 4}, new int[3] {3, 4, 5}
            });

        }
        public void Add_IntSquareAndSymmetricMatrixes_GivesCorrectValue()
        {
            var m1 = new SquareMartix<int>(new int[][] { new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 } });
            var m2 = new SymmetricMatrix<int>(new int[][] { new int[] { 1, 4, 5 }, new int[] { 4, 2, 6 }, new int[] { 5, 6, 3 } });

            dynamic actual = m1.Add(m2);
            var expected = new SquareMartix<int>(new int[][] { new int[] { 2, 5, 6 }, new int[] { 5, 3, 7 }, new int[] { 6, 7, 4 } });

            Assert.AreEqual(expected, actual);
        }
 public void Indexer_jLess0_ArgumentOutOfRangeException()
 {
     int[,] sourceArray = new int[2, 2] 
         {
             {1, 2},
             {2, 4}
         };
     SymmetricMatrix<int> matrix = new SymmetricMatrix<int>(sourceArray);
     int a = matrix[0, -1];
 }
        public void Add_IntDiagonalAndSymmetricMatrixes_GivesCorrectValue()
        {
            var m1 = new DiagonalMatrix<int>(new int[][] { new int[] { 1, 0, 0 }, new int[] { 0, 1, 0 }, new int[] { 0, 0, 1 } });
            var m2 = new SymmetricMatrix<int>(new int[][] { new int[] { 1, 4, 5 }, new int[] { 4, 2, 6 }, new int[] { 5, 6, 3 } });

            dynamic actual = m1.Add(m2);
            var expected = new SymmetricMatrix<int>(new int[][] { new int[] { 2, 4, 5 }, new int[] { 4, 3, 6 }, new int[] { 5, 6, 4 } });

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            int[,] iarray= new int[10,5];
            Random r = new Random(10);
            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 5; j++)
                    iarray[i,j]=r.Next(10);
            Matrix<int> m = new Matrix<int>(10,5,iarray);
            Console.WriteLine(m);
            m.Register();
            m.ChangeSingleCell(4, 2,777);
            Console.WriteLine();

            double[,] darray = new double[3, 3];
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    darray[i, j] =r.Next(10);
            SquereMatrix<double> sqm = new SquereMatrix<double>(3,darray);
            Console.WriteLine(sqm);
            sqm.ChangeSingleCell(2, 2, 77.7);
            Console.WriteLine();

            //конструктор отзеркалит нижнюю половину
            string[,] strarray = new string[3, 3];
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                    strarray[i, j] = r.Next(10).ToString();
            SymmetricMatrix<string> smm = new SymmetricMatrix<string>(3, strarray);
            smm.ChangeSingleCell(2, 2, "777");
            Console.WriteLine(smm);

            DateTime[] dtarray = new DateTime[3];
            for (int i = 0; i < 3; i++)
                    dtarray[i] = new DateTime((long)(i+1)*1000000000000000000);
            DiagonalMatrix<DateTime> dm = new DiagonalMatrix<DateTime>(3, dtarray);
            dm.ChangeSingleCell(2, 2, new DateTime((long)7770000000000000));
            Console.WriteLine(dm);

            for (int i = 0; i < 10; i++)
                for (int j = 0; j < 5; j++)
                    iarray[i, j] = r.Next(10);
            Matrix<int> m2 = new Matrix<int>(10, 5, iarray);
            Console.WriteLine(m2);
            Console.WriteLine();
            Console.WriteLine(MatrixExtentor.Addition<int>(m,m2));

            Console.WriteLine();

            Console.Read();
        }
        public void BuildEmptySymmetricMatrix(Int32 nodeCount)
        {
            var symmMatrix = new SymmetricMatrix<Boolean>(Guid.NewGuid(),
                nodeCount);

            Assert.NotNull(symmMatrix);

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

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

            var symmMatrix = new SymmetricMatrix<Int32>(Guid.NewGuid(),
                sglArray, 3);

            Assert.NotNull(symmMatrix);

            Assert.Equal(sglArray[0], symmMatrix[0, 0]);
            Assert.Equal(sglArray[2], symmMatrix[0, 2]);
            Assert.Equal(sglArray[6], symmMatrix[2, 0]);
        }
        public void BuildSymmetricMatrixFromDoubleArray()
        {
            Int32[,] dblArray = new Int32[,] {
                {1, 2, 3},
                {2, 1, 2},
                {3, 2, 1}
            };

            var symmMatrix = new SymmetricMatrix<Int32>(Guid.NewGuid(),
                dblArray);

            Assert.NotNull(symmMatrix);

            Assert.Equal(dblArray[0, 0], symmMatrix[0, 0]);
            Assert.Equal(dblArray[0, 1], symmMatrix[0, 1]);
            Assert.Equal(dblArray[2, 1], symmMatrix[1, 2]);
        }
Exemplo n.º 20
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 Add_SymmetricMatrices_DoubleSourceArray()
        {
            int[,] sourceArray = new int[,]
            {
                {1, 5},
                {5, 4}
            };
            SymmetricMatrix<int> lhs = new SymmetricMatrix<int>(sourceArray);
            SymmetricMatrix<int> rhs = new SymmetricMatrix<int>(sourceArray);
            lhs.Add(rhs);

            int[,] actual = lhs.Add(rhs).ToArray();
            bool areDouble = true;
            for (int i = 0; i < sourceArray.GetLength(0); i++)
                for (int j = 0; j < sourceArray.GetLength(1); j++)
                    areDouble &= (sourceArray[i, j] * 2) == actual[i, j];
            Assert.IsTrue(areDouble);
        }
 public void AddingSymmetricMatrix()
 {
     Matrix<int> m = new SymmetricMatrix<int>(new int[3][]
     {
         new int[3] {1, 2, 3},
         new int[3] {2, 3, 4}, new int[3] {3, 4, 5}
     });
     Matrix<int> m2 = new SymmetricMatrix<int>(new int[3][]
     {
         new int[3] {1, 2, 3},
         new int[3] {2, 3, 4}, new int[3] {3, 4, 5}
     });
     Matrix<int> m1 = m.Add(m2);
     Assert.IsTrue(new SquareMatrix<int>(new int[3][]
     {
         new int[3] {2, 4, 6},
         new int[3] {4, 6, 8}, new int[3] {6, 8, 10}
     }).Equals(m1));
 }
Exemplo n.º 23
0
        static void Main(string[] args)
        {
            Console.WriteLine("Диагональная матрица");
            DiagonalMatrix<int> diagonalInt = new DiagonalMatrix<int>(new int[] { 1, 2, 3, 4 });
            diagonalInt.Print();
            diagonalInt.amendEvent.amendIMatrixItem += (s, e) => Console.WriteLine(e.massage);
            diagonalInt[2, 2] = 5;
            diagonalInt.Print();

            Console.WriteLine("Симметрическая матрица");
            SymmetricMatrix<int> symmetricInt = new SymmetricMatrix<int>(new int[][]
                {
                   new int[] {1,2,3,4},
                   new int[] {  2,3,4},
                   new int[] {    3,4},
                   new int[] {      4}
                });
            symmetricInt.Print();
            symmetricInt.amendEvent.amendIMatrixItem += (s, e) => Console.WriteLine(e.massage);
            symmetricInt[1, 3] = 9;
            symmetricInt.Print();

            Console.WriteLine("Квадратная матрица");
            SquareMatrix<int> square = new SquareMatrix<int>(new int[,]
                {
                    {1,1,1,1},
                    {2,2,2,2},
                    {3,3,3,3},
                    {4,4,4,4}
                });
            square.Print();
            square.amendEvent.amendIMatrixItem += (s, e) => Console.WriteLine(e.massage);
            square[2, 2] = 10;
            square.Print();

            Console.WriteLine("Квадратная + Симметрическая");
            square.Add(symmetricInt);
            square.Print();

            Console.ReadKey();
        }
Exemplo n.º 24
0
        static void Main(string[] args)
        {
            int[,] symm =
            {
                {11, 2, -8},
                {2, 2, 10},
                {-8, 10, 5}
            };

            int[,] diag = 
            {
                {1, 0, 0},
                {0, 1, 0},
                {0, 0, 1}
            };

            try
            {
                DiagonalMatrix<int> d = new DiagonalMatrix<int>(diag);
                Message<int> m = new Message<int>(d);
                d[0, 0] = 10;
                SymmetricMatrix<int> s = new SymmetricMatrix<int>(diag);
                SquareMatrix<int> sq = new SquareMatrix<int>(symm);
                for (int i = 0; i < s.Size; i++)
                {
                    for (int j = 0; j < s.Size; j++)
                    {
                        Console.Write(s[i, j] + " ");
                    }
                    Console.WriteLine();
                }
                d = sq.Sum(sq);

            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
 static void Main(string[] args)
 {
     var v = new int[3][];
     v[0] = new[] { 1, 2, 3 };
     v[1] = new[] { 1, 2, 3 };
     v[2] = new[] { 1, 2, 3 };
     var a = new DiagonalMatrix<int>(v);
     var b = new DiagonalMatrix<int>(v);
     var c = MatrixExtension<int>.Add(a, b);
     Console.WriteLine(c);
 
     c.Change = OnChange;
     c[1, 1] = 17;
     var array = new int[2][];
     array[0]= new[]{2,2};
     array[1] = new[]{2,1};
     var sm = new SymmetricMatrix<int>(array);
     sm.Change = OnChange;
     Console.WriteLine(sm);
     sm[0, 1] = 17;
     Console.ReadKey();
 }
        static void Main(string[] args)
        {
            int[,] squareArray = { { 1, 2, 3 }, { 1, 2, 9 }, { 2, 4, 1 } };
            int[,] diagonalArray = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
            int[,] semetricalArray = { { 1, 3, 5 }, { 3, 2, 4 }, { 5, 4, 6 } };
            try
            {
                var squareMatrix = new SquareMatrix<int>(squareArray);
                var diagonalMatrix = new DiagonalMatrix<int>(diagonalArray);
                var semetricalMatrix = new SymmetricMatrix<int>(semetricalArray);
                Console.WriteLine(diagonalMatrix.ToString());
                Console.WriteLine(semetricalMatrix.ToString());
                var resultSum=semetricalMatrix.SumMatrix(diagonalMatrix, (a, b) => a + b);
                Console.WriteLine(resultSum.ToString());
                diagonalMatrix.Changes+= Change;
                diagonalMatrix[2, 2] = 123;
                diagonalMatrix[2, 2] = 0;

            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
               Console.ReadKey();
            }

        }
 public void SymmetricMatrix_SetValue_Test()
 {
     SymmetricMatrix<int> matrix = new SymmetricMatrix<int>(3);
     matrix[1, 0] = 3;
     Assert.AreEqual(3, matrix[0, 1]);
 }
Exemplo n.º 28
0
        ///
        public override void ComputeCorrelations(IBooleanMatrix entity_data)
        {
            var transpose = (IBooleanMatrix) entity_data.Transpose();

            var other_entity_weights = new float[transpose.NumberOfRows];
            for (int row_id = 0; row_id < transpose.NumberOfRows; row_id++)
            {
                int freq = transpose.GetEntriesByRow(row_id).Count;
                other_entity_weights[row_id] = 1f / (float) Math.Log(3 + freq, 2); // TODO make configurable
            }

            var weighted_overlap = new SymmetricMatrix<float>(entity_data.NumberOfRows);
            var entity_weights = new float[entity_data.NumberOfRows];

            // go over all (other) entities
            for (int row_id = 0; row_id < transpose.NumberOfRows; row_id++)
            {
                var row = transpose.GetEntriesByRow(row_id);
                for (int i = 0; i < row.Count; i++)
                {
                    int x = row[i];
                    entity_weights[x] += other_entity_weights[row_id];
                    for (int j = i + 1; j < row.Count; j++)
                    {
                        int y = row[j];
                        weighted_overlap[x, y] += other_entity_weights[row_id] * other_entity_weights[row_id];
                    }
                }
            }

            // the diagonal of the correlation matrix
            for (int i = 0; i < num_entities; i++)
                this[i, i] = 1;

            // compute cosine
            for (int x = 0; x < num_entities; x++)
                for (int y = 0; y < x; y++)
                    this[x, y] = (float) (weighted_overlap[x, y] / Math.Sqrt(entity_weights[x] * entity_weights[y] ));
        }
Exemplo n.º 29
0
 private double VertexError(ref SymmetricMatrix q, double x, double y, double z)
 {
     return(q.M11 * x * x + 2 * q.M12 * x * y + 2 * q.M13 * x * z + 2 * q.M14 * x + q.M22 * y * y
            + 2 * q.M23 * y * z + 2 * q.M24 * y + q.M33 * z * z + 2 * q.M34 * z + q.M44);
 }
Exemplo n.º 30
0
        /**
         * Calculates the constitutive stiffness of the element.
         *
         * @return The constitutive stiffness
         */
        private Matrix CalculateConstitutiveStiffness()
        {
            var    constitutiveStiffness = SymmetricMatrix.CreateZero(FREEDOM_DEGREE_COUNT);
            double E         = this.material.YoungModulus;
            double G         = E / (2d * (1d + this.material.PoissonRatio));
            double Iy        = this.beamSection.InertiaY;
            double Iz        = this.beamSection.InertiaZ;
            double J         = this.beamSection.TorsionalInertia;
            double A         = this.beamSection.Area;
            double Ay        = this.beamSection.EffectiveAreaY;
            double Az        = this.beamSection.EffectiveAreaZ;
            double l         = this.currentLength;
            double lSqruared = l * l;
            double lCubed    = l * l * l;
            double phiY      = (12.0 * E * Iy) / (l * l * G * Az);
            double phiZ      = (12.0 * E * Iz) / (l * l * G * Ay);
            double psiY      = 1.0 / (1.0 + phiY);
            double psiZ      = 1.0 / (1.0 + phiZ);
            double EAOverL   = (E * A) / l;
            double GJOverL   = (G * J) / l;
            double psiZ_E_Iz_12_OverlCubed   = (12.0 * psiZ * E * Iz) / lCubed;
            double psiZ_E_Iz_6_OverlSquared  = (6.0 * psiZ * E * Iz) / lSqruared;
            double psiY_E_12_Iy_OverlCubed   = (12.0 * psiY * E * Iy) / lCubed;
            double psiY_E_6_Iy_OverlSquared  = (6.0 * psiY * E * Iy) / lSqruared;
            double psiZ_3_Plus_1_E_Iz_Overl  = (((3.0 * psiZ) + 1.0) * E * Iz) / l;
            double psiZ_3_Minus_1_E_Iz_Overl = (((3.0 * psiZ) - 1.0) * E * Iz) / l;
            double psiY_3_Plus_1_E_Iy_Overl  = (((3.0 * psiY) + 1.0) * E * Iy) / l;
            double psiY_3_Minus_1_E_Iy_Overl = (((3.0 * psiY) - 1.0) * E * Iy) / l;

            constitutiveStiffness[0, 0] = EAOverL;
            constitutiveStiffness[0, 6] = -EAOverL;

            constitutiveStiffness[1, 1]  = psiZ_E_Iz_12_OverlCubed;
            constitutiveStiffness[1, 5]  = psiZ_E_Iz_6_OverlSquared;
            constitutiveStiffness[1, 7]  = -psiZ_E_Iz_12_OverlCubed;
            constitutiveStiffness[1, 11] = psiZ_E_Iz_6_OverlSquared;

            constitutiveStiffness[2, 2]  = psiY_E_12_Iy_OverlCubed;
            constitutiveStiffness[2, 4]  = -psiY_E_6_Iy_OverlSquared;
            constitutiveStiffness[2, 8]  = -psiY_E_12_Iy_OverlCubed;
            constitutiveStiffness[2, 10] = -psiY_E_6_Iy_OverlSquared;

            constitutiveStiffness[3, 3] = GJOverL;
            constitutiveStiffness[3, 9] = -GJOverL;

            constitutiveStiffness[4, 4]  = psiY_3_Plus_1_E_Iy_Overl;
            constitutiveStiffness[4, 8]  = psiZ_E_Iz_6_OverlSquared;
            constitutiveStiffness[4, 10] = psiY_3_Minus_1_E_Iy_Overl;

            constitutiveStiffness[5, 5]  = psiZ_3_Plus_1_E_Iz_Overl;
            constitutiveStiffness[5, 7]  = -psiY_E_6_Iy_OverlSquared;
            constitutiveStiffness[5, 11] = psiZ_3_Minus_1_E_Iz_Overl;

            constitutiveStiffness[6, 6] = EAOverL;

            constitutiveStiffness[7, 7]  = psiZ_E_Iz_12_OverlCubed;
            constitutiveStiffness[7, 11] = -psiZ_E_Iz_6_OverlSquared;

            constitutiveStiffness[8, 8]  = psiY_E_12_Iy_OverlCubed;
            constitutiveStiffness[8, 10] = psiY_E_6_Iy_OverlSquared;

            constitutiveStiffness[9, 9] = GJOverL;

            constitutiveStiffness[10, 10] = psiY_3_Plus_1_E_Iy_Overl;

            constitutiveStiffness[11, 11] = psiZ_3_Plus_1_E_Iz_Overl;

            return(constitutiveStiffness.CopyToFullMatrix());
        }
Exemplo n.º 31
0
        /**
         * Calculates the geometric stiffness of the element.
         *
         * @return The geometric stiffness
         */
        private Matrix CalculateGeometricStiffness()
        {
            var    geometricStiffness    = SymmetricMatrix.CreateZero(FREEDOM_DEGREE_COUNT);
            var    forcesInNaturalSystem = this.CalculateForcesInNaturalSystem();
            var    forcesInLocalSystem   = this.CalculateForcesInLocalSystem();
            double torsionalMoment       = forcesInNaturalSystem[NaturalDeformationMode3D.TORSION];
            double axialForce            = forcesInNaturalSystem[NaturalDeformationMode3D.EXTENSION];
            double momentY_A             = forcesInLocalSystem[4];
            double momentZ_A             = forcesInLocalSystem[5];
            double momentY_B             = forcesInLocalSystem[10];
            double momentZ_B             = forcesInLocalSystem[11];
            double length        = this.currentLength;
            double Qy            = -(momentZ_A + momentZ_B) / length;
            double Qz            = (momentY_A + momentY_B) / length;
            double Qy_Over_L     = Qy / length;
            double Qz_Over_L     = Qz / length;
            double Qy_L_Over_6   = (Qy * length) / 6.0;
            double Qz_L_Over_6   = (Qz * length) / 6.0;
            double N_6_Over_5_L  = (6.0 * axialForce) / (5.0 * length);
            double N_Over_10     = axialForce / 10.0;
            double N_L_4_Over_30 = (4.0 * axialForce * length) / 30.0;
            double N_L_Over_30   = (axialForce * length) / 30.0;
            double MyA_Over_L    = momentY_A / length;
            double MyB_Over_L    = momentY_B / length;
            double MzA_Over_L    = momentZ_A / length;
            double MzB_Over_L    = momentZ_B / length;
            double M_Over_L      = torsionalMoment / length;
            double M_3_Over_6    = (3.0 * torsionalMoment) / 6.0;

            geometricStiffness[0, 1] = -Qy_Over_L;
            geometricStiffness[0, 2] = -Qz_Over_L;
            geometricStiffness[0, 7] = Qy_Over_L;
            geometricStiffness[0, 8] = Qz_Over_L;

            geometricStiffness[1, 1]  = N_6_Over_5_L;
            geometricStiffness[1, 3]  = MyA_Over_L;
            geometricStiffness[1, 4]  = M_Over_L;
            geometricStiffness[1, 5]  = N_Over_10;
            geometricStiffness[1, 6]  = Qy_Over_L;
            geometricStiffness[1, 7]  = -N_6_Over_5_L;
            geometricStiffness[1, 9]  = MyB_Over_L;
            geometricStiffness[1, 10] = -M_Over_L;
            geometricStiffness[1, 11] = N_Over_10;

            geometricStiffness[2, 2]  = N_6_Over_5_L;
            geometricStiffness[2, 3]  = MzA_Over_L;
            geometricStiffness[2, 4]  = -N_Over_10;
            geometricStiffness[2, 5]  = M_Over_L;
            geometricStiffness[2, 6]  = Qz_Over_L;
            geometricStiffness[2, 8]  = -N_6_Over_5_L;
            geometricStiffness[2, 9]  = MzB_Over_L;
            geometricStiffness[2, 10] = -N_Over_10;
            geometricStiffness[2, 11] = -M_Over_L;

            geometricStiffness[3, 4]  = ((-2.0 * momentZ_A) + momentZ_B) / 6.0;
            geometricStiffness[3, 5]  = ((2.0 * momentY_A) - momentY_B) / 6.0;
            geometricStiffness[3, 7]  = -MyA_Over_L;
            geometricStiffness[3, 8]  = -MzA_Over_L;
            geometricStiffness[3, 10] = Qy_L_Over_6;
            geometricStiffness[3, 11] = Qz_L_Over_6;

            geometricStiffness[4, 4]  = N_L_4_Over_30;
            geometricStiffness[4, 7]  = -M_Over_L;
            geometricStiffness[4, 8]  = +N_Over_10;
            geometricStiffness[4, 9]  = Qy_L_Over_6;
            geometricStiffness[4, 10] = -N_L_Over_30;
            geometricStiffness[4, 11] = M_3_Over_6;

            geometricStiffness[5, 5]  = N_L_4_Over_30;
            geometricStiffness[5, 7]  = -N_Over_10;
            geometricStiffness[5, 8]  = -M_Over_L;
            geometricStiffness[5, 9]  = Qz_L_Over_6;
            geometricStiffness[5, 10] = -M_3_Over_6;
            geometricStiffness[5, 11] = -N_L_Over_30;

            geometricStiffness[6, 7] = -Qy_Over_L;
            geometricStiffness[6, 8] = -Qz_Over_L;

            geometricStiffness[7, 7]  = N_6_Over_5_L;
            geometricStiffness[7, 9]  = -MyB_Over_L;
            geometricStiffness[7, 10] = M_Over_L;
            geometricStiffness[7, 11] = -N_Over_10;

            geometricStiffness[8, 8]  = N_6_Over_5_L;
            geometricStiffness[8, 9]  = -MzB_Over_L;
            geometricStiffness[8, 10] = N_Over_10;
            geometricStiffness[8, 11] = M_Over_L;

            geometricStiffness[9, 10] = ((-2.0 * momentZ_B) + momentZ_A) / 6.0;
            geometricStiffness[9, 11] = ((+2.0 * momentY_B) - momentY_A) / 6.0;

            geometricStiffness[10, 10] = N_L_4_Over_30;

            geometricStiffness[11, 11] = N_L_4_Over_30;

            return(geometricStiffness.CopyToFullMatrix());
        }
Exemplo n.º 32
0
        ///
        public void ComputeCorrelations(IRatings ratings, EntityType entity_type)
        {
            int num_entities = (entity_type == EntityType.USER) ? ratings.MaxUserID + 1 : ratings.MaxItemID + 1;
            Resize(num_entities);

            if (entity_type != EntityType.USER && entity_type != EntityType.ITEM)
                throw new ArgumentException("entity type must be either USER or ITEM, not " + entity_type);

            IList<IList<int>> ratings_by_other_entity = (entity_type == EntityType.USER) ? ratings.ByItem : ratings.ByUser;

            var freqs   = new SymmetricMatrix<int>(num_entities);
            var i_sums  = new SymmetricMatrix<float>(num_entities);
            var j_sums  = new SymmetricMatrix<float>(num_entities);
            var ij_sums = new SymmetricMatrix<float>(num_entities);
            var ii_sums = new SymmetricMatrix<float>(num_entities);
            var jj_sums = new SymmetricMatrix<float>(num_entities);

            foreach (IList<int> other_entity_ratings in ratings_by_other_entity)
                for (int i = 0; i < other_entity_ratings.Count; i++)
                {
                    var index1 = other_entity_ratings[i];
                    int x = (entity_type == EntityType.USER) ? ratings.Users[index1] : ratings.Items[index1];

                    // update pairwise scalar product and frequency
                    for (int j = i + 1; j < other_entity_ratings.Count; j++)
                    {
                        var index2 = other_entity_ratings[j];
                        int y = (entity_type == EntityType.USER) ? ratings.Users[index2] : ratings.Items[index2];

                        float rating1 = (float) ratings[index1];
                        float rating2 = (float) ratings[index2];

                        // update sums
                        freqs[x, y]   += 1;
                        i_sums[x, y]  += rating1;
                        j_sums[x, y]  += rating2;
                        ij_sums[x, y] += rating1 * rating2;
                        ii_sums[x, y] += rating1 * rating1;
                        jj_sums[x, y] += rating2 * rating2;
                    }
                }

            // the diagonal of the correlation matrix
            for (int i = 0; i < num_entities; i++)
                this[i, i] = 1;

            for (int i = 0; i < num_entities; i++)
                for (int j = i + 1; j < num_entities; j++)
                    if (freqs[i, j] < 2)
                        this[i, j] = 0;
                    else
                        this[i, j] = ComputeCorrelation (i_sums[i, j], j_sums[i, j], ii_sums[i, j], jj_sums[i, j], ij_sums[i, j], freqs[i, j]);
        }
Exemplo n.º 33
0
        void ComputeCorrelationsUShortOverlap(IBooleanMatrix entity_data)
        {
            var transpose = entity_data.Transpose() as IBooleanMatrix;

            var overlap = new SymmetricMatrix<ushort>(entity_data.NumberOfRows);

            // go over all (other) entities
            for (int row_id = 0; row_id < transpose.NumberOfRows; row_id++)
            {
                var row = transpose.GetEntriesByRow(row_id);
                for (int i = 0; i < row.Count; i++)
                {
                    int x = row[i];
                    for (int j = i + 1; j < row.Count; j++)
                        overlap[x, row[j]]++;
                }
            }

            // the diagonal of the correlation matrix
            for (int i = 0; i < num_entities; i++)
                this[i, i] = 1;

            // compute cosine
            for (int x = 0; x < num_entities; x++)
                for (int y = 0; y < x; y++)
                {
                    long size_product = entity_data.NumEntriesByRow(x) * entity_data.NumEntriesByRow(y);
                    if (size_product > 0)
                        this[x, y] = (float) (overlap[x, y] / Math.Sqrt(size_product));
                }
        }
Exemplo n.º 34
0
        public static double[] PWHalfSpace(double mu_a, double mu_s, double[] mu, double[] wt, SquareMatrix L, int N)
        {
            var zeros = Zeros(N);
            var eye   = Eye(N);

            // construct the eigenvalue problem

            var A = -mu_a * eye + mu_s * L;

            var invWtMatrix = new SymmetricMatrix(N);

            0.To(N - 1).ForEach(i => invWtMatrix[i, i] = 1 / mu[i]); // diag(wt)

            A = invWtMatrix * A;

            Console.WriteLine("A:\n");
            PrintMatrix(A);

            // solve the eigenvalue problems
            var e = A.Eigendecomposition();

            //0.To(N - 1).ForEach(i => PrintVector(e.Eigenvector(i).ToArray()));

            // sort the eigenvalues
            var indices = from i in 0.To(N - 1)
                          orderby e.Eigenpairs[i].Eigenvalue.Re
                          select i;
            // compute the expansion coefficients for the solution

            // eval           = eval(N/2+1:N);
            // evec           = V(:,indx(N/2+1:N));
            var eval = indices.Select(i => e.Eigenpairs[i].Eigenvalue.Re).Skip(N / 2);
            var evec = indices.Select(i => e.Eigenpairs[i].Eigenvector).Skip(N / 2);

            Console.WriteLine("eval:\n");
            PrintVector(eval.ToArray());

            var temp1 = evec.Select(t => t.Take(N / 2).Reverse());

            var M = new SquareMatrix(N / 2);

            temp1.ForEach((eveci, i) =>
            {
                eveci.ForEach((evecj, j) =>
                {
                    M[i, j] = evecj.Re; // ??
                });
            });

            Console.WriteLine("M:\n");
            PrintMatrix(M);

            // solve the linear system for the expansion coefficients

            var vectorValues = new double[N / 2];

            vectorValues[N / 2 - 1] = 1.0 / wt[N - 1];

            var mInverse = M.Inverse();

            Console.WriteLine("mInverse:\n");
            PrintMatrix(mInverse);

            var c = mInverse.Transpose * new ColumnVector(vectorValues); // why Transpose?
            //var c = mInverse * new ColumnVector(vectorValues);

            // compute the reflectance

            var temp2 = new RectangularMatrix(N, N / 2);

            evec.ForEach((eveci, i) =>
            {
                eveci.ForEach((evecj, j) =>
                {
                    temp2[j, i] = evecj.Re;
                });
            });
            Console.WriteLine("temp2 (evec):\n");
            PrintMatrix(temp2);

            var R = temp2 * new ColumnVector(c.Take(N / 2).ToArray());

            Console.WriteLine("R:\n");
            PrintMatrix(R);

            //return R.ToArray();
            return(R.Reverse().ToArray()); // why backwards?
        }
        ///
        public void ComputeCorrelations(IRatings ratings, EntityType entity_type)
        {
            int num_entities = (entity_type == EntityType.USER) ? ratings.MaxUserID + 1 : ratings.MaxItemID + 1;

            Resize(num_entities);

            if (entity_type != EntityType.USER && entity_type != EntityType.ITEM)
            {
                throw new ArgumentException("entity type must be either USER or ITEM, not " + entity_type);
            }

            IList <IList <int> > ratings_by_other_entity = (entity_type == EntityType.USER) ? ratings.ByItem : ratings.ByUser;

            var freqs   = new SymmetricMatrix <int>(num_entities);
            var i_sums  = new SymmetricMatrix <float>(num_entities);
            var j_sums  = new SymmetricMatrix <float>(num_entities);
            var ij_sums = new SymmetricMatrix <float>(num_entities);
            var ii_sums = new SymmetricMatrix <float>(num_entities);
            var jj_sums = new SymmetricMatrix <float>(num_entities);

            foreach (IList <int> other_entity_ratings in ratings_by_other_entity)
            {
                for (int i = 0; i < other_entity_ratings.Count; i++)
                {
                    var index1 = other_entity_ratings[i];
                    int x      = (entity_type == EntityType.USER) ? ratings.Users[index1] : ratings.Items[index1];

                    // update pairwise scalar product and frequency
                    for (int j = i + 1; j < other_entity_ratings.Count; j++)
                    {
                        var index2 = other_entity_ratings[j];
                        int y      = (entity_type == EntityType.USER) ? ratings.Users[index2] : ratings.Items[index2];

                        float rating1 = (float)ratings[index1];
                        float rating2 = (float)ratings[index2];

                        // update sums
                        freqs[x, y]   += 1;
                        i_sums[x, y]  += rating1;
                        j_sums[x, y]  += rating2;
                        ij_sums[x, y] += rating1 * rating2;
                        ii_sums[x, y] += rating1 * rating1;
                        jj_sums[x, y] += rating2 * rating2;
                    }
                }
            }

            // the diagonal of the correlation matrix
            for (int i = 0; i < num_entities; i++)
            {
                this[i, i] = 1;
            }

            for (int i = 0; i < num_entities; i++)
            {
                for (int j = i + 1; j < num_entities; j++)
                {
                    int n = freqs[i, j];

                    if (n < 2)
                    {
                        this[i, j] = 0;
                        continue;
                    }

                    double numerator = ij_sums[i, j] * n - i_sums[i, j] * j_sums[i, j];

                    double denominator = Math.Sqrt((n * ii_sums[i, j] - i_sums[i, j] * i_sums[i, j]) * (n * jj_sums[i, j] - j_sums[i, j] * j_sums[i, j]));
                    if (denominator == 0)
                    {
                        this[i, j] = 0;
                        continue;
                    }

                    double pmcc = numerator / denominator;
                    this[i, j] = (float)(pmcc * ((n - 1) / (n - 1 + Shrinkage)));
                }
            }
        }
Exemplo n.º 36
0
        public void block(int arow, int colum)
        {
            //int CoreNum = Environment.ProcessorCount;//获取本机处理器个数
            blockMatrix  = new IMatrix[arow][];
            blockMatrix1 = new IMatrix[arow * colum];
            // this.arows = CoreNum;
            //this.columns = CoreNum;//按核分行、列
            int rows    = OriginA.RowCount; //.Rows;
            int columns = OriginA.ColCount; //.Columns;

            arowBlock        = averageArowBlock(rows);
            columnBlock      = averageClowBlock(columns);
            firstarowBlock   = firstArowBlock(rows);
            firstcolumnBlock = firstClowBlock(columns);
            DateTime sss = DateTime.Now;

            var span = DateTime.Now - sss;

            //Console.WriteLine(span.TotalMilliseconds + "msfenkuai00");
            ////////////////并行分块
            //Parallel.For(0, arow, (int i) =>
            //{
            //    blockMatrix[i] = new IMatrix[colum];
            //    for (int j = 0; j < colum; j++)
            //    {
            //        int countarow = arowBlock[i * colum + j];
            //        int countcolumn = columnBlock[i * colum + j];
            //        int firstarow = firstarowBlock[i * colum + j];
            //        int firstcolumn = firstcolumnBlock[i * colum + j];
            //        double[][] data = new double[countarow][];
            //        for (int ii = 0; ii < countarow; ii++)
            //        {
            //            data[ii]=GetVector(countcolumn, firstarow, firstcolumn, ii);
            //        }
            //        blockMatrix[i][j] = new Matrix(data);
            //    }
            //});

            sss = DateTime.Now;
            for (int i = 0; i < 4; i++)
            {
                if (i == 0 || i == 3)
                {
                    //sss = DateTime.Now;
                    int      countOfarowBlock   = arowBlock[i];
                    int      countOfcolumnBlock = columnBlock[i];
                    int      count = countOfarowBlock * (countOfarowBlock + 1) / 2;
                    double[] aa    = new double[count];
                    int      indexOffirstarowBlock   = firstarowBlock[i];
                    int      indexOffirstcolumnBlock = firstcolumnBlock[i];
                    //for (int j = 0; j < countOfarowBlock; j++)
                    //    GetSymmetricArray(i, aa, j, indexOffirstarowBlock, indexOffirstcolumnBlock);
                    //span = DateTime.Now - sss;
                    //Console.WriteLine(span.TotalMilliseconds + "msfenkuai11---0zhiqian");
                    //blockMatrix1[i] = new SymmetricMatrix(aa);
                    Parallel.For(0, countOfarowBlock, new Action <int>(delegate(int j)
                    {
                        GetSymmetricArray(i, aa, j, indexOffirstarowBlock, indexOffirstcolumnBlock);
                    }));
                    //span = DateTime.Now - sss;
                    //Console.WriteLine(span.TotalMilliseconds + "msfenkuai11---0");
                    ////for (int j = 0; j < arowBlock[i]; j++)
                    ////    GetSymmetricArray(i, aa, j);
                    blockMatrix1[i] = new SymmetricMatrix(aa);
                }
                if (i == 1)
                {
                    //sss = DateTime.Now;
                    double[][] aa = new double[arowBlock[i]][];
                    int        indexOffirstarowBlock   = firstarowBlock[i];
                    int        indexOffirstcolumnBlock = firstcolumnBlock[i];
                    int        countOfcolumnBlock      = columnBlock[i];
                    Parallel.For(0, arowBlock[i], new Action <int>(delegate(int j)
                    {
                        aa[j] = GetMatrixArrayRow(i, j, countOfcolumnBlock, indexOffirstarowBlock, indexOffirstcolumnBlock);
                    }));
                    //span = DateTime.Now - sss;
                    //Console.WriteLine(span.TotalMilliseconds + "msfenkuai11---1");
                    blockMatrix1[i] = new ArrayMatrix(aa);
                }
            }
            //span = DateTime.Now - sss;
            //Console.WriteLine(span.TotalMilliseconds + "msfenkuai11");
        }