public void CanConvertArrayToSparseVector()
 {
     var array = new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1), new Complex(4, 1) };
     var vector = new SparseVector(array);
     Assert.IsInstanceOf(typeof(SparseVector), vector);
     CollectionAssert.AreEqual(array, array);
 }
 public void CanConvertArrayToSparseVector()
 {
     var array = new[] { 0.0, 1.0, 2.0, 3.0, 4.0 };
     var vector = new SparseVector(array);
     Assert.IsInstanceOf(typeof(SparseVector), vector);
     CollectionAssert.AreEqual(array, array);
 }
 public void CanConvertSparseVectorToArray()
 {
     var vector = new SparseVector(Data);
     var array = vector.ToArray();
     Assert.IsInstanceOf(typeof(double[]), array);
     CollectionAssert.AreEqual(vector, array);
 }
Beispiel #4
0
        public double Compute(SparseVector x, SparseVector z)
        {
            double temp;

            temp = ((SparseVector)(x - z)).Magnitude;
            return Math.Exp(-sigma * (temp * temp));
        }
Beispiel #5
0
        public ExampleSet GetProof(SparseVector x)
        {
            ExampleSet res = new ExampleSet();

            List<ExampleDistancePair> list = new List<ExampleDistancePair>();

            foreach (Example e in m_t_set.Examples)
            {
                list.Add(new ExampleDistancePair(e, SparseVector.Distance(x, e.X)));
            }

            list.Sort();

            int[] votes = new int[m_catnum];
            for (int i = 0; i < votes.Length; i++)
            {
                votes[i] = 0;
            }

            for (int i = 0; i < this.m_k; i++)
            {
                ExampleDistancePair pair = list[i];
                res.AddExample(pair.Example);
            }

            return res;
        }
        //-- VMP -------------------------------------------------------------------------------------------

		/// <summary>
		/// Evidence message for VMP
		/// </summary>
		/// <param name="sample">Incoming message from 'sample'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
		/// <param name="probsTrue">Incoming message from 'probsTrue'. Must be a proper distribution.  If any element is uniform, the result will be uniform.</param>
		/// <param name="MeanLog">Buffer 'MeanLog'.</param>
		/// <param name="MeanLogOneMinus">Buffer 'MeanLogOneMinus'.</param>
		/// <returns>Average of the factor's log-value across the given argument distributions</returns>
		/// <remarks><para>
		/// The formula for the result is <c>sum_(sample,probsTrue) p(sample,probsTrue) log(factor(sample,probsTrue))</c>.
		/// Adding up these values across all factors and variables gives the log-evidence estimate for VMP.
		/// </para></remarks>
		/// <exception cref="ImproperMessageException"><paramref name="sample"/> is not a proper distribution</exception>
		/// <exception cref="ImproperMessageException"><paramref name="probsTrue"/> is not a proper distribution</exception>
		public static double AverageLogFactor([Proper] SparseBernoulliListBase sample,
			[Proper] SparseBetaList probsTrue, SparseVector MeanLog, SparseVector MeanLogOneMinus)
        {
			//var MeanLogOneMinus = probsTrue.GetMeanLogOneMinus();
            var p = sample.GetProbTrueVector();
			var res = p * MeanLog + (1 - p) * MeanLogOneMinus;
            return res.Sum();
        }
Beispiel #7
0
 /// <summary>
 /// Adds each element of vec to corresponding element of addTo vector
 /// addTo vector is being changed in place
 /// </summary>
 public static void AddInPlace(SparseVector<double> addTo, SparseVector<double> vec)
 {
     foreach (var vecIdx in vec.InnerIdx)
     {
         var num = addTo.TryGet(vecIdx, 0.0);
         addTo[vecIdx] = num + vec[vecIdx];
     }
 }
 public void CanCallUnaryNegationOperatorOnSparseVector()
 {
     var vector = new SparseVector(_data);
     var other = -vector;
     for (var i = 0; i < _data.Length; i++)
     {
         Assert.AreEqual(-_data[i], other[i]);
     }
 }
Beispiel #9
0
 public static SparseVector<double> Multiply(double scalar, SparseVector<double> vector)
 {
     var newVector = vector.DeepClone();
     for (int i = 0; i < newVector.InnerDat.Count; i++ )
     {
         newVector.SetDirect(i, newVector.InnerDat[i] * scalar);
     }
     return newVector;
 }
        public void AddRowTestAddsRowWithGivenVector()
        {
            SparseVector v = new SparseVector(TestMatrix.Columns);
            v[2] = 5.1f;
            TestMatrix.AddRow(v);

            float expected = 5.1f;

            Assert.AreEqual(expected, TestMatrix[3, 2]);
        }
        /// <summary>
        /// Creates a new instance of the Vector class.
        /// </summary>
        /// <param name="data">The array to create this vector from.</param>
        /// <returns>The new <c>Vector</c>.</returns>
        protected override Vector<double> CreateVector(IList<double> data)
        {
            var vector = new SparseVector(data.Count);
            for (var index = 0; index < data.Count; index++)
            {
                vector[index] = data[index];
            }

            return vector;
        }
        public void ConstructorThrowsArgumentNullExceptionIfNullArgumentPassed()
        {
            // arrange

            // act
            var sv = new SparseVector<double>(null);
            
            // assert
            Assert.Fail("SparseVector ctor must throw ArgumentNullException if null argument passed.");
        }
Beispiel #13
0
        /// <summary>
        /// Divides each element of dividee with corresponding element of divisor in place
        /// Dividee is being changed
        /// </summary>
        public static void DivideInPlace(SparseVector<double> dividee, SparseVector<double> divisor)
        {
            foreach (var divIdx in divisor.InnerIdx)
            {
                var num = dividee.TryGet(divIdx, 0.0);
                if (num < Double.Epsilon)
                    continue;

                dividee[divIdx] = num/divisor[divIdx];
            }
        }
        public void CanCreateSparseVectorFromArray()
        {
            var data = new double[Data.Length];
            Array.Copy(Data, data, Data.Length);
            var vector = new SparseVector(data);

            for (var i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(data[i], vector[i]);
            }
        }
        public void AddColumnTestAddsColumnWithGivenValue()
        {
            SparseVector expected = new SparseVector(TestMatrix.Rows);
            expected[0] = 5.0f;
            expected[1] = 5.0f;
            expected[2] = 5.0f;

            TestMatrix.AddColumn(5.0f);

            Assert.IsTrue(expected.ApproximatelyEqual(
                TestMatrix.ColumnVector(TestMatrix.Columns - 1)));
        }
Beispiel #16
0
        public Binary_SVM_SMO(int n)
        {
            this.m_NonBound = new List<int>();
            this.m_rand = new Random();
            this.m_weight = new SparseVector(n);

            // foamliu, 2008/12/29, default values
            this.m_c = Constants.SVM_C;
            this.m_eta = Constants.SVM_Eta;
            this.m_tolerance = Constants.SVM_Tolerance;
            this.m_epsilon = Constants.SVM_Epsilon;
        }
        public void CanAddTwoSparseVectorsUsingOperator()
        {
            var vector = new SparseVector(Data);
            var other = new SparseVector(Data);
            var result = vector + other;

            for (var i = 0; i < Data.Length; i++)
            {
                AssertHelpers.AreEqual(Data[i], vector[i]);
                AssertHelpers.AreEqual(Data[i], other[i]);
                AssertHelpers.AreEqual(Data[i] * 2.0f, result[i]);
            }
        }
        public void CanAddTwoSparseVectorsUsingOperator()
        {
            var vector = new SparseVector(_data);
            var other = new SparseVector(_data);
            var result = vector + other;

            for (var i = 0; i < _data.Length; i++)
            {
                Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified.");
                Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified.");
                Assert.AreEqual(_data[i] * 2.0, result[i]);
            }
        }
        public void CanAddTwoSparseVectorsUsingOperator()
        {
            var vector = new SparseVector(Data);
            var other = new SparseVector(Data);
            var result = vector + other;
            CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified.");
            CollectionAssert.AreEqual(Data, other, "Making sure the original vector wasn't modified.");

            for (var i = 0; i < Data.Length; i++)
            {
                Assert.AreEqual(Data[i] * 2.0f, result[i]);
            }
        }
        public void IndexedPropertyGetAccessorReturnsWhatWasSavedThroughConstructor()
        {
            // arrange           
            var originalVector = SparseVectorHelper.GenerateRandomVector(1000, 0.7, () => SparseVectorHelper.RandomInInterval(-100, 100, 2));

            // act
            var sv = new SparseVector<double>(originalVector);

            // assert
            foreach (var element in originalVector)
            {
                Assert.AreEqual(element.Value, sv[element.Key]);
            }
        }
Beispiel #21
0
        public static SparseVector<double> Add(SparseVector<double> v1, SparseVector<double> v2)
        {
            var newVector = new SparseVector<double>();
            var v1LastI = v1.Count > 0 ? v1.Last.Idx : 0;
            var v2LastI = v2.Count > 0 ? v2.Last.Idx : 0;
            var limit = Math.Max(v1LastI, v2LastI);

            for (int i = 0; i < limit; i++)
            {
                var sum = v1.TryGet(i, 0.0) + v2.TryGet(i, 0.0);
                if (sum > Double.Epsilon)
                    newVector[i] = sum;
            }

            return newVector;
        }
        public void Test()
        {
            int l = 30;
            int k = 10;
            double ratioSeparable = 0;
            int numSeparable = 0;
            ExampleSet set = new ExampleSet();

            for (int d = 10; d < 50; d=d+10)
            {
                numSeparable = 0;

                for (int n = 0; n < k; n++)
                {
                    set.Examples.Clear();

                    for (int i = 0; i < l; i++)
                    {
                        SparseVector x = new SparseVector(d);

                        for (int j = 0; j < d; j++)
                        {
                            x[j] = m_rand.NextDouble();
                        }

                        Category c = GetRandCategory();
                        Example e = new Example(c);
                        e.X = x;
                        set.AddExample(e);
                    }

                    SimpleLLM llm = new SimpleLLM(set, d);
                    //Logging.Info(string.Format("IsLinearSeparable: {0}", llm.IsLinearSeparable()));
                    //System.Console.WriteLine(string.Format("IsLinearSeparable: {0}", llm.IsLinearSeparable()));
                    if (llm.IsLinearSeparable())
                    {
                        numSeparable++;
                    }

                }

                ratioSeparable = 1.0 * numSeparable / k;

                System.Console.WriteLine(string.Format("d: {0}, l: {1}, Separable ratio: {2}", d, l, ratioSeparable));
            }
        }
        public static SparseVector<double> GenerateSparseVector(int columns, double density, Func<double> randomGenerator)
        {
            if (density < 0 || density > 1)
            {
                throw new ArgumentOutOfRangeException("rowDensity");
            }

            var rowDensity = columns * density;
            var nonZeroElementsInRow = Poisson(rowDensity);
            var result = new SparseVector<double>();

            for (var j = 0; j < nonZeroElementsInRow; j++)
            {
                result[Random.Next(0, columns)] = randomGenerator();
            }

            return result;
        }
Beispiel #24
0
        private SparseVector m_weight; // weight vector

        #endregion Fields

        #region Constructors

        public Binary_SVM_SMO(ClassificationProblem problem)
        {
            this.m_problem = problem;
            this.m_t_set = this.m_problem.TrainingSet;
               // this.m_problem.RetrieveVocabulary(out this.m_voc);
            this.m_l = m_t_set.Examples.Count;
            this.m_alpha = new double[m_l];
            this.m_error = new double[m_l];

            this.m_kernel = new LinearKernel();
            this.m_NonBound = new List<int>();
            this.m_rand = new Random();
            this.m_weight = new SparseVector(problem.Dimension);

            // foamliu, 2009/01/12, default values
            this.m_c = Constants.SVM_C;
            this.m_eta = Constants.SVM_Eta;
            this.m_tolerance = Constants.SVM_Tolerance;
            this.m_epsilon = Constants.SVM_Epsilon;
        }
Beispiel #25
0
        public static void Main()
        {
            Vector vector = new SparseVector(10);
            for (int i = 0; i < vector.Count; i++)
            {
                vector[i] = i % 2 * i;
            }

            //The Vector enumerator also returns a KeyValuePair with the key being the
            //element's position in the Vector and the value being the element's value.
            //For sparse matrices, the enumerator only returns non-zero values.
            //The code below will return:
            //Position: 1, Value: 1
            //Position: 3, Value: 3
            //Position: 5, Value: 5
            //Position: 7, Value: 7
            //Position: 9, Value: 9
            foreach (KeyValuePair<int, double> element in vector.GetIndexedEnumerator())
            {
                Console.WriteLine("Position: {0}, Value: {1}", element.Key, element.Value);
            }
        }
        private static void BuildExample(TextExample example, Vocabulary voc, int exampleCount)
        {
            int dimension = voc.Count;
            SparseVector vector = new SparseVector(dimension);

            foreach (string word in example.Tokens.Keys)
            {
                int pos = voc.GetWordPosition(word);
                if (pos == Constants.KEY_NOT_FOUND)
                    continue;

                // phi i(x) = tfi log(idfi) /k
                // tfi:     number of occurences of the term i in the document x
                // idfi:    the ratio between the total number of documents and the
                //              number of documents containing the term
                // k:       normalisation constant ensuring that ||phi|| = 1
                double phi = example.Tokens[word] * Math.Log(exampleCount / voc.WordExampleOccurMap[word]);
                vector.Components.Add(pos, phi);

            }
            vector.Normalize();
            example.X = vector;
        }
        public void CanCreateSparseVectorFromAnotherSparseVector()
        {
            var vector = new SparseVector(Data);
            var other = new SparseVector(vector);

            Assert.AreNotSame(vector, other);
            for (var i = 0; i < Data.Length; i++)
            {
                Assert.AreEqual(vector[i], other[i]);
            }
        }
Beispiel #28
0
 /// <summary>
 /// Initialize a new MultiPathGenerator with constant drifts and variances.
 /// </summary>
 /// <summary>
 /// Initialize a new MultiPathGenerator which samples at explicitly specified times.
 /// </summary>
 /// <remarks>
 /// The initial time is assumed to be zero and must
 /// <b>not</b> be included in the passed SparseVector.
 /// </remarks>
 /// <param name="generator"></param>
 /// <param name="drifts">
 /// A <see cref="SparseVector"/> of constant drifts - one for each single asset.
 /// </param>
 /// <param name="covariance">
 /// A covariance <see cref="Matrix"/> which encapsulates the relations between
 /// the diffusion components of the single assets.
 /// </param>
 /// <param name="times">
 /// A <see cref="SparseVector"/> of explicitly specified times at which the
 /// path will be sampled.
 /// The initial time is assumed to be zero and must
 /// <b>not</b> be included in the passed SparseVector.
 /// </param>
 public MultiPathGenerator(IContinuousRng generator,
                           SparseVector drifts, Matrix covariance, SparseVector times)
     : base(drifts, times)
 {
     InitializeGenerator(generator, covariance);
 }
Beispiel #29
0
        /// <summary>
        /// foamliu, 2009/05/14, 补上这个方法.
        /// </summary>
        /// <param name="x">要分类的向量</param>
        /// <param name="iResult">分类结果: +1 或者 -1</param>
        public override int Predict(SparseVector x)
        {
            double confidence;

            return(this.Predict(x, out confidence));
        }
Beispiel #30
0
        static void Main(string[] args)
        {
            // Get the stop words and stemmer for English.

            IStemmer stemmer;

            Set <string> .ReadOnly stopWords;
            TextMiningUtils.GetLanguageTools(Language.English,
                                             out stopWords, out stemmer);

            // Create a tokenizer.

            UnicodeTokenizer tokenizer = new UnicodeTokenizer();

            tokenizer.MinTokenLen = 2;                      // Each token must be at least 2
            // characters long.
            tokenizer.Filter = TokenizerFilter.AlphaStrict; // Tokens
            // can consist of alphabetic characters only.

            // Load a document corpus from a file. Each line in the file
            // represents one document.

            string[] docs
                = File.ReadAllLines("..\\..\\Data\\YahooFinance.txt");

            // Create a bag-of-words space.

            BowSpace bowSpc = new BowSpace();

            bowSpc.Tokenizer   = tokenizer; // Assign the tokenizer.
            bowSpc.StopWords   = stopWords; // Assign the stop words.
            bowSpc.Stemmer     = stemmer;   // Assign the stemmer.
            bowSpc.MinWordFreq = 3;         // A term must appear at least 3
            // times in the corpus for it to be part of the
            // vocabulary.
            bowSpc.MaxNGramLen = 3;                       // Terms consisting of at most 3
            // consecutive words will be considered.
            bowSpc.WordWeightType = WordWeightType.TfIdf; // Set the
            // weighting scheme for the bag-of-words vectors to
            // TF-IDF.
            bowSpc.NormalizeVectors = true; // The TF-IDF vectors will
            // be normalized.
            bowSpc.CutLowWeightsPerc = 0.2; // The terms with the lowest
            // weights, summing up to 20% of the overall weight sum,
            // will be removed from each TF-IDF vector.

            bowSpc.Initialize(docs); // Initialize the BOW space.

            // Compute 100 clusters of documents.

            KMeansFast kMeans = new KMeansFast(100); // Set k to 100.

            kMeans.Trials = 3;                       // Perform 3 repetitions. Take the best
            // result.
            kMeans.Eps = 0.001;                      // Stop iterating when the partition
            // quality increases for less than 0.001.

            ClusteringResult cr = kMeans.Cluster(bowSpc); // Execute.

            // Extract the top 5 terms with the highest TF-IDF weights
            // from each of the clusters' centroids and output the
            // number of documents (companies) in each cluster.

            foreach (Cluster cl in cr.Roots)
            {
                SparseVector <double> .ReadOnly centroid
                    = cl.ComputeCentroid(bowSpc, CentroidType.NrmL2);
                Console.Write(bowSpc.GetKeywordsStr(centroid, 5));
                Console.WriteLine(" ({0} companies)", cl.Items.Count);
            }

            // Output the documents that are contained in the first
            // cluster.

            foreach (int docIdx in cr.Roots[0].Items)
            {
                Console.WriteLine(docs[docIdx]);
            }
        }
 /// <summary>
 /// Creates a vector from an array.
 /// </summary>
 /// <param name="data">The array to create this vector from.</param>
 /// <returns>The new vector. </returns>
 protected override Vector <float> CreateVector(float[] data)
 {
     return(SparseVector.OfEnumerable(data));
 }
Beispiel #32
0
 public double Compute(SparseVector x, SparseVector z)
 {
     return(Math.Pow(scale * SparseVector.DotProduct(x, z) + offset, degree));
 }
 public abstract int Predict(SparseVector x);
Beispiel #34
0
        // *** ISimilarity<SparseVector<double>> interface implementation ***

        public double GetSimilarity(SparseVector <double> a, SparseVector <double> b)
        {
            return(GetSimilarity(new SparseVector <double> .ReadOnly(a), new SparseVector <double> .ReadOnly(b)));
        }
        public double[] Solve(double[] x0, double[,] a, double[] b, double[] measurability, double[] tolerance,
                              double[] lower, double[] upper)
        {
            // Проверка аргументов на null
            _ = x0 ?? throw new ArgumentNullException(nameof(x0));
            _ = a ?? throw new ArgumentNullException(nameof(a));
            _ = b ?? throw new ArgumentNullException(nameof(b));
            _ = measurability ?? throw new ArgumentNullException(nameof(measurability));
            _ = tolerance ?? throw new ArgumentNullException(nameof(tolerance));
            _ = lower ?? throw new ArgumentNullException(nameof(lower));
            _ = upper ?? throw new ArgumentNullException(nameof(upper));

            //Проверка аргументов на размерности
            if (x0.Length == 0)
            {
                throw new ArgumentException(nameof(x0));
            }
            if (a.GetLength(1) != x0.Length)
            {
                throw new ArgumentException("Array length by dimension 1 is not equal to X0 length.", nameof(a));
            }
            if (b.Length != a.GetLength(0))
            {
                throw new ArgumentException("Array length is not equal to A length by 0 dimension.", nameof(b));
            }
            if (measurability.Length != x0.Length)
            {
                throw new ArgumentException("Array length is not equal to X0 length.", nameof(measurability));
            }
            if (tolerance.Length != x0.Length)
            {
                throw new ArgumentException("Array length is not equal to X0 length.", nameof(tolerance));
            }
            if (lower.Length != x0.Length)
            {
                throw new ArgumentException("Array length is not equal to X0 length.", nameof(lower));
            }
            if (upper.Length != x0.Length)
            {
                throw new ArgumentException("Array length is not equal to X0 length.", nameof(upper));
            }

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            var i = SparseMatrix.OfDiagonalArray(measurability);
            var w = SparseMatrix.OfDiagonalVector(1 / SparseVector.OfEnumerable(tolerance).PointwisePower(2));

            var h = i * w;
            var d = -(h * SparseVector.OfEnumerable(x0));

            var func        = new QuadraticObjectiveFunction(h.ToArray(), d.ToArray());
            var constraints = new List <LinearConstraint>();

            Time = stopWatch.Elapsed;

            //Нижние и верхние границы
            for (var j = 0; j < x0.Length; j++)
            {
                constraints.Add(new LinearConstraint(1)
                {
                    VariablesAtIndices = new[] { j },
                    ShouldBe           = ConstraintType.GreaterThanOrEqualTo,
                    Value = lower[j]
                });

                constraints.Add(new LinearConstraint(1)
                {
                    VariablesAtIndices = new[] { j },
                    ShouldBe           = ConstraintType.LesserThanOrEqualTo,
                    Value = upper[j]
                });
            }

            //Ограничения для решения задачи баланса
            for (var j = 0; j < b.Length; j++)
            {
                var notNullElements        = Array.FindAll(a.GetRow(j), x => Math.Abs(x) > 0.0000001);
                var notNullElementsIndexes = new List <int>();
                for (var k = 0; k < x0.Length; k++)
                {
                    if (Math.Abs(a[j, k]) > 0.0000001)
                    {
                        notNullElementsIndexes.Add(k);
                    }
                }

                constraints.Add(new LinearConstraint(notNullElements.Length)
                {
                    VariablesAtIndices = notNullElementsIndexes.ToArray(),
                    CombinedAs         = notNullElements,
                    ShouldBe           = ConstraintType.EqualTo,
                    Value = b[j]
                });
            }

            var solver = new GoldfarbIdnani(func, constraints);

            if (!solver.Minimize())
            {
                throw new ApplicationException("Failed to solve balance task.");
            }

            stopWatch.Stop();
            TimeAll = stopWatch.Elapsed;

            DisbalanceOriginal = a.Dot(x0).Subtract(b).Euclidean();
            Disbalance         = a.Dot(solver.Solution).Subtract(b).Euclidean();

            return(solver.Solution);
        }
Beispiel #36
0
        public ClusteringResult Cluster(IExampleCollection <LblT, SparseVector <double> .ReadOnly> dataset)
        {
            Utils.ThrowException(dataset == null ? new ArgumentNullException("dataset") : null);
            Utils.ThrowException(dataset.Count < m_k ? new ArgumentValueException("dataset") : null);
            ClusteringResult clustering             = null;
            ClusteringResult best_clustering        = null;
            double           global_best_clust_qual = 0;

            for (int trial = 1; trial <= m_trials; trial++)
            {
                Utils.VerboseLine("*** CLUSTERING TRIAL {0} OF {1} ***", trial, m_trials);
                ArrayList <SparseVector <double> .ReadOnly> centroids = null;
                clustering = new ClusteringResult();
                for (int i = 0; i < m_k; i++)
                {
                    clustering.Roots.Add(new Cluster());
                }
                // select seed items
                double          min_sim = double.MaxValue;
                ArrayList <int> tmp     = new ArrayList <int>(dataset.Count);
                for (int i = 0; i < dataset.Count; i++)
                {
                    tmp.Add(i);
                }
                for (int k = 0; k < 3; k++)
                {
                    ArrayList <SparseVector <double> .ReadOnly> seeds = new ArrayList <SparseVector <double> .ReadOnly>(m_k);
                    tmp.Shuffle(m_rnd);
                    for (int i = 0; i < m_k; i++)
                    {
                        seeds.Add(ModelUtils.ComputeCentroid(new SparseVector <double> .ReadOnly[] { dataset[tmp[i]].Example }, m_centroid_type));
                    }
                    // assess quality of seed items
                    double sim_avg = 0;
                    foreach (SparseVector <double> .ReadOnly seed_1 in seeds)
                    {
                        foreach (SparseVector <double> .ReadOnly seed_2 in seeds)
                        {
                            if (seed_1 != seed_2)
                            {
                                sim_avg += m_similarity.GetSimilarity(seed_1, seed_2);
                            }
                        }
                    }
                    sim_avg /= (double)(m_k * m_k - m_k);
                    //Console.WriteLine(sim_avg);
                    if (sim_avg < min_sim)
                    {
                        min_sim   = sim_avg;
                        centroids = seeds;
                    }
                }
                // main loop
                int    iter            = 0;
                double best_clust_qual = 0;
                double clust_qual;
                while (true)
                {
                    iter++;
                    clust_qual = 0;
                    // assign items to clusters
                    foreach (Cluster cluster in clustering.Roots)
                    {
                        cluster.Items.Clear();
                    }
                    for (int i = 0; i < dataset.Count; i++)
                    {
                        SparseVector <double> .ReadOnly example = dataset[i].Example;
                        double          max_sim    = double.MinValue;
                        ArrayList <int> candidates = new ArrayList <int>();
                        for (int j = 0; j < m_k; j++)
                        {
                            SparseVector <double> .ReadOnly centroid = centroids[j];
                            double sim = m_similarity.GetSimilarity(example, centroid);
                            if (sim > max_sim)
                            {
                                max_sim = sim;
                                candidates.Clear();
                                candidates.Add(j);
                            }
                            else if (sim == max_sim)
                            {
                                candidates.Add(j);
                            }
                        }
                        if (candidates.Count > 1)
                        {
                            candidates.Shuffle(m_rnd);
                        }
                        if (candidates.Count > 0) // *** is this always true?
                        {
                            clustering.Roots[candidates[0]].Items.Add(new Pair <double, int>(1, i));
                            clust_qual += max_sim;
                        }
                    }
                    clust_qual /= (double)dataset.Count;
                    Utils.VerboseLine("*** Iteration {0} ***", iter);
                    Utils.VerboseLine("Quality: {0:0.0000}", clust_qual);
                    // check if done
                    if (iter > 1 && clust_qual - best_clust_qual <= m_eps)
                    {
                        break;
                    }
                    best_clust_qual = clust_qual;
                    // compute new centroids
                    for (int i = 0; i < m_k; i++)
                    {
                        centroids[i] = clustering.Roots[i].ComputeCentroid(dataset, m_centroid_type);
                    }
                }
                if (trial == 1 || clust_qual > global_best_clust_qual)
                {
                    global_best_clust_qual = clust_qual;
                    best_clustering        = clustering;
                }
            }
            return(best_clustering);
        }
Beispiel #37
0
 /// <summary>
 /// foamliu, 2009/05/14, 补上这个方法.
 /// </summary>
 /// <param name="x"></param>
 /// <param name="confidence"></param>
 public override int Predict(SparseVector x, out double confidence)
 {
     confidence = 1.0;
     return(this.Predict(x));
 }
Beispiel #38
0
 public State()
 {
     SparseData = new SparseVector();
 }
Beispiel #39
0
 /// <summary>
 /// Creates a vector from an array.
 /// </summary>
 /// <param name="data">The array to create this vector from.</param>
 /// <returns>The new vector. </returns>
 protected override Vector <Complex> CreateVector(Complex[] data)
 {
     return(SparseVector.OfEnumerable(data));
 }
Beispiel #40
0
 public SimpleLLM(ExampleSet t_set, int d)
 {
     this.m_t_set  = t_set;
     this.m_l      = t_set.Examples.Count;
     this.m_weight = new SparseVector(d);
 }
Beispiel #41
0
 public static ulong GetSimHash64(SparseVector<double>.ReadOnly vec, ArrayList<Word>.ReadOnly vocabulary, double eps)
 {
     // TODO: check parameters
     double[] v = new double[64];
     Array.Clear(v, 0, 64);
     foreach (IdxDat<double> item in vec)
     {
         Word word = vocabulary[item.Idx];
         double weight = item.Dat;
         ulong hashCode = word.GetHashCode64();
         for (int i = 0; i < 64; i++)
         {
             if ((hashCode & (1UL << i)) > 0) { v[i] += weight; }
             else { v[i] -= weight; }
         }
     }
     ulong fp = 0;
     for (int i = 0; i < 64; i++)
     {
         if (v[i] >= eps) { fp |= (1UL << i); }
     }
     return fp;
 }
Beispiel #42
0
        private void Replicate(SparseVector <double> vector, out SparseVector <double> vector1, out SparseVector <double> vector2)
        {
            int featureIdx = BowSpace.Words.Count;

            vector1 = vector.Clone();
            vector1.InnerIdx.Add(featureIdx);
            vector1.InnerDat.Add(H1);

            vector2 = vector.Clone();
            vector2.InnerIdx.Add(featureIdx);
            vector2.InnerDat.Add(H2);

            ModelUtils.TryNrmVecL2(vector1);
            ModelUtils.TryNrmVecL2(vector2);
        }
 /// <summary>
 /// Gets a vector of P(true) values.
 /// </summary>
 /// <returns></returns>
 public void SetProbTrueVector(SparseVector probTrueVector)
 {
     LogOddsVector.SetToFunction(probTrueVector, MMath.Logit);
 }
Beispiel #44
0
 public void ParseIfMissingClosingParenThrowsFormatException()
 {
     Assert.Throws <FormatException>(() => SparseVector.Parse("(1"));
     Assert.Throws <FormatException>(() => SparseVector.Parse("[1"));
 }
Beispiel #45
0
 public double Compute(SparseVector x, SparseVector z)
 {
     return(Math.Tanh(scale * SparseVector.DotProduct(x, z) + offset));
 }
Beispiel #46
0
        public Vector2D[] ComputeLayout(LayoutSettings settings)
        {
            UnlabeledDataset <SparseVector <double> > dataset = new UnlabeledDataset <SparseVector <double> >(mDataset);

            // clustering
            mLogger.Info("ComputeLayout", "Clustering ...");
            KMeansFast kMeans = new KMeansFast(mKClust);

            kMeans.Eps    = mKMeansEps;
            kMeans.Random = mRandom;
            kMeans.Trials = 1;
            ClusteringResult clustering = kMeans.Cluster(mDataset); // throws ArgumentValueException
            // determine reference instances
            UnlabeledDataset <SparseVector <double> > dsRefInst = new UnlabeledDataset <SparseVector <double> >();

            foreach (Cluster cluster in clustering.Roots)
            {
                SparseVector <double> centroid
                    = cluster.Items.Count > 0 ? cluster.ComputeCentroid(mDataset, CentroidType.NrmL2) : new SparseVector <double>();
                dsRefInst.Add(centroid); // dataset of reference instances
                dataset.Add(centroid);   // add centroids to the main dataset
            }
            // position reference instances
            mLogger.Info("ComputeLayout", "Positioning reference instances ...");
            SparseMatrix <double>    simMtx = ModelUtils.GetDotProductSimilarity(dsRefInst, mSimThresh, /*fullMatrix=*/ false);
            StressMajorizationLayout sm     = new StressMajorizationLayout(dsRefInst.Count, new DistFunc(simMtx));

            sm.Random = mRandom;
            Vector2D[] centrPos = sm.ComputeLayout();
            // k-NN
            mLogger.Info("ComputeLayout", "Computing similarities ...");
            simMtx = ModelUtils.GetDotProductSimilarity(dataset, mSimThresh, /*fullMatrix=*/ true);
            mLogger.Info("ComputeLayout", "Constructing system of linear equations ...");
            LabeledDataset <double, SparseVector <double> > lsqrDs = new LabeledDataset <double, SparseVector <double> >();

            foreach (IdxDat <SparseVector <double> > simMtxRow in simMtx)
            {
                if (simMtxRow.Dat.Count <= 1)
                {
                    mLogger.Warn("ComputeLayout", "Instance #{0} has no neighborhood.", simMtxRow.Idx);
                }
                ArrayList <KeyDat <double, int> > knn = new ArrayList <KeyDat <double, int> >(simMtxRow.Dat.Count);
                foreach (IdxDat <double> item in simMtxRow.Dat)
                {
                    if (item.Idx != simMtxRow.Idx)
                    {
                        knn.Add(new KeyDat <double, int>(item.Dat, item.Idx));
                    }
                }
                knn.Sort(DescSort <KeyDat <double, int> > .Instance);
                int count = Math.Min(knn.Count, mKNN);
                SparseVector <double> eq = new SparseVector <double>();
                double wgt = 1.0 / (double)count;
                for (int i = 0; i < count; i++)
                {
                    eq.InnerIdx.Add(knn[i].Dat);
                    eq.InnerDat.Add(-wgt);
                }
                eq.InnerIdx.Sort(); // *** sort only indices
                eq[simMtxRow.Idx] = 1;
                lsqrDs.Add(0, eq);
            }
            Vector2D[] layout = new Vector2D[dataset.Count - mKClust];
            for (int i = dataset.Count - mKClust, j = 0; i < dataset.Count; i++, j++)
            {
                SparseVector <double> eq = new SparseVector <double>(new IdxDat <double>[] { new IdxDat <double>(i, 1) });
                lsqrDs.Add(centrPos[j].X, eq);
            }
            LSqrModel lsqr = new LSqrModel();

            lsqr.Train(lsqrDs);
            for (int i = 0; i < layout.Length; i++)
            {
                layout[i].X = lsqr.Solution[i];
            }
            for (int i = lsqrDs.Count - mKClust, j = 0; i < lsqrDs.Count; i++, j++)
            {
                lsqrDs[i].Label = centrPos[j].Y;
            }
            lsqr.Train(lsqrDs);
            for (int i = 0; i < layout.Length; i++)
            {
                layout[i].Y = lsqr.Solution[i];
            }
            return(settings == null ? layout : settings.AdjustLayout(layout));
        }
 public abstract int Predict(SparseVector x, out double confidence);
Beispiel #48
0
        //gamma is second level parameter
        public static Cluster run(Cluster histClus, Network curNet, double alpha, double gamma, double lambda, Dictionary <int, double> betaVec, int typeNum)
        {
            Cluster curClus = new Cluster(typeNum); //clustering for each type

            double log_alpha = Math.Log(alpha);

            SparseVector logPriorPropVec = new SparseVector();

            if (histClus.clusterList.Count != 0)
            {
                foreach (KeyValuePair <int, double> kvp in histClus.clusterSizeVec.vector)
                {
                    logPriorPropVec[kvp.Key] = Math.Log(kvp.Value); //log (lambda* pi_k)
                }
            }

            /*****************************/
            //background 2: uniform background
            /*********************************************/
            Dictionary <int, HashSet <int> > attributeDict = new Dictionary <int, HashSet <int> >();

            for (int type = 0; type < typeNum; type++)
            {
                attributeDict[type] = new HashSet <int>();
                attributeDict[type].UnionWith(histClus.attriClusMatList[type].getKeys());
                attributeDict[type].UnionWith(curNet.entityList[type].IDHT.Keys);
            }

            /*********************************************/
            //foreach object in curNet, adjust their cluster membership, until converge
            double epsi     = double.MaxValue;
            double EPSI     = 0.0001;
            int    INITITER = 0;

            HashSet <int> candidateClus = new HashSet <int>();

            //initial clusters are historical clusters. It should be commented if do not need prior clusters
            if (histClus.clusterList.Count != 0)
            {
                candidateClus.UnionWith(histClus.clusterList);
            }

            int iter    = 0;
            int objSize = curNet.network.tensor.Count;

            /******* control types that will be used **************/
            HashSet <int> subTypeSet = new HashSet <int> ();

            subTypeSet.Add(0); //C
            subTypeSet.Add(1); //A
            subTypeSet.Add(2); //T


            /********* calculate the p(oi) under H, and under each possible priors beforehand*/
            //SparseMatrix obj_priorTable_Mat = new SparseMatrix();
            Dictionary <int, int> obj_priorDict = new Dictionary <int, int> ();

            if (histClus.clusterList.Count != 0)
            {
                foreach (int objID in curNet.network.tensor.Keys)
                {
                    SparseVector clusProbVec         = new SparseVector();
                    double       log_prob_in_newclus = 0;

                    SparseMatrix typeIDMat = curNet.network.tensor[objID];

                    //prior clusters
                    foreach (int clusID in histClus.clusterList)
                    {
                        //double clusSize = /*histClus.clusterSizeVec[clusID] + */curClus.clusterSizeVec[clusID];
                        double clusSize = histClus.clusterSizeVec[clusID];

                        double log_prob_in_clus = 0;

                        for (int type = 0; type < typeNum; type++)
                        {
                            if (!subTypeSet.Contains(type))
                            {
                                continue;
                            }

                            SparseVector IDCountVec = typeIDMat.getRow(type);
                            if (IDCountVec == null)
                            {
                                continue;
                            }

                            double typeProb         = 1;
                            double curObjTotalCount = IDCountVec.getSum();

                            double histClus_total_count = histClus.clusTypeCountMat[clusID, type];
                            //double curClus_total_Count = curClus.clusTypeCountMat[clusID, type];
                            double curClus_total_Count = 0;

                            int i = 1;// record total current count

                            foreach (KeyValuePair <int, double> kvp in IDCountVec.vector)
                            {
                                int    ID    = kvp.Key;
                                double count = kvp.Value;

                                //if (!attributeDict[type].ContainsKey(ID))
                                //{
                                //    Console.WriteLine("Type {0} ID {1}is not in dictionary", type, ID);
                                //}

                                double hist_item_count = histClus.clusAttriMatList[type][clusID, ID];
                                //double cur_item_count = curClus.clusAttriMatList[type][clusID, ID];
                                double cur_item_count = 0;

                                for (int itemCount = 1; itemCount <= count; itemCount++)
                                {
                                    //background 1:
                                    //typeProb *= /*Math.Log*/((attributeDict[type][ID] * betaVec[type] + hist_item_count + cur_item_count + count - itemCount)
                                    //    / (typeCount[type] * betaVec[type] + histClus_total_count + curClus_total_Count + curObjTotalCount - i));
                                    //logTypeProb -= Math.Log(attributeDict[type].Count * betaVec[type] + histClus_total_count + curClus_total_Count + curObjTotalCount - i);

                                    //background 2:
                                    typeProb *= /*Math.Log*/ ((1 * betaVec[type] + hist_item_count + cur_item_count + count - itemCount)
                                                              / (attributeDict[type].Count * betaVec[type] + histClus_total_count + curClus_total_Count + curObjTotalCount - i));

                                    i++;
                                }
                            }

                            log_prob_in_clus += Math.Log(typeProb);
                        }

                        clusProbVec[clusID] = log_prob_in_clus;
                    }

                    //unseen clusters
                    for (int type = 0; type < typeNum; type++)
                    {
                        if (!subTypeSet.Contains(type))
                        {
                            continue;
                        }
                        SparseVector IDCountVec = typeIDMat.getRow(type);
                        if (IDCountVec == null)
                        {
                            continue;
                        }
                        double typeProb         = 1;
                        double curObjTotalCount = IDCountVec.getSum();
                        int    i = 1;
                        foreach (KeyValuePair <int, double> kvp in IDCountVec.vector)
                        {
                            int    ID    = kvp.Key;
                            double count = kvp.Value;

                            for (int itemCount = 1; itemCount <= count; itemCount++)
                            {
                                //background 1:
                                //typeProb *= /*Math.Log*/((attributeDict[type][ID] * betaVec[type] + count - itemCount) / (typeCount[type] * betaVec[type] + curObjTotalCount - i));
                                //logTypeProb -= Math.Log(attributeDict[type].Count * betaVec[type] + curObjTotalCount - i);

                                //background 2:
                                typeProb *= /*Math.Log*/ ((1 * betaVec[type] + count - itemCount) / (attributeDict[type].Count * betaVec[type] + curObjTotalCount - i));

                                i++;
                            }
                        }
                        log_prob_in_newclus += Math.Log(typeProb);
                    }

                    clusProbVec[-1] = log_prob_in_newclus + log_alpha;

                    int curPrior = clusProbVec.getMax_Pos().Key;
                    obj_priorDict.Add(objID, curPrior);
                }
            }

            ////handle middle results of tables
            //int MAXTableID = 0; //start from 1
            //Dictionary<int, Cluster> groupTableDict = new Dictionary<int,Cluster> ();//record tables for each group
            //Dictionary<int, int> tableClusterDict = new Dictionary<int, int>();// record cluster for each table;
            //foreach (int priorID in obj_priorDict)
            //{
            //    Cluster TableList = new Cluster();
            //    groupTableDict.Add(priorID, TableList);
            //}

            SparseMatrix groupClusSizeMat = new SparseMatrix();

            foreach (int priorID in obj_priorDict.Keys)
            {
                SparseVector vec = new SparseVector();
                groupClusSizeMat.setRow(priorID, vec);
            }

            SparseVector betaCoeffVec = new SparseVector(); //first level component coefficient, non-normalized

            betaCoeffVec[-1] = alpha;
            //betaCoeffVec[-1] = 1; //alpha logn (every one has alpha)
            while (epsi > EPSI && iter < 1000)
            {
                int      handledObjNum  = 0;
                int      clusChangedNum = 0;
                DateTime curTime        = DateTime.Now;
                //randomize the network
                curNet.network.randomize(objSize);

                //if (histClus.clusterList.Count != 0)
                {
                    //foreach (int objID in curNet.network.tensor.Keys)
                    //{
                    //    int priorID = obj_priorDict[objID];
                    //    SparseMatrix typeIDMat = curNet.network.tensor[objID];
                    //    //1. assign tables
                    //    int tID = getTableID(typeIDMat, histClus, curClus, groupTableDict[priorGroup].clusterSizeVec, gamma);

                    //    //2. assign clusters to tables
                    //    if (tID == -1) // a new table
                    //    {
                    //        int kID = getClusterID_new(typeIDMat,candidateClus, histClus, curClus, curClus.clusterSizeVec, alpha);
                    //        // change the clusters for all the objects in the table if needed
                    //        //...
                    //    }
                    //    else // a existing table
                    //    {
                    //        SparseMatrix tableTypeIDMat = new SparseMatrix();
                    //        int kID = getClusterID(tableTypeIDMat, candidateClus, histClus, curClus, curClus.clusterSizeVec, alpha);
                    //        //change the clusters for all the objects in the table if needed
                    //        //...
                    //    }


                    //}

                    foreach (int objID in curNet.network.tensor.Keys)
                    {
                        //calculate the probability it belongs to each cluster
                        SparseVector clusProbVec = new SparseVector();
                        int          newClusID   = Cluster.MAXClusID;

                        SparseMatrix typeIDMat  = curNet.network.tensor[objID];
                        int          priorGroup = -1;
                        if (obj_priorDict.ContainsKey(objID))
                        {
                            priorGroup = obj_priorDict[objID];
                        }
                        //existing clusters
                        foreach (int clusID in candidateClus)
                        {
                            //double clusSize = /*histClus.clusterSizeVec[clusID] + */curClus.clusterSizeVec[clusID];
                            //double clusSize = histClus.clusterSizeVec[clusID] + curClus.clusterSizeVec[clusID];
                            double clusSize = histClus.clusterSizeVec[clusID] + groupClusSizeMat[priorGroup, clusID] + gamma * betaCoeffVec[clusID] / betaCoeffVec.getSum();//groupClusSize could be zero, that's why we need HDP, or priors for the size

                            double log_prob_in_clus = 0;

                            for (int type = 0; type < typeNum; type++)
                            {
                                if (!subTypeSet.Contains(type))
                                {
                                    continue;
                                }

                                SparseVector IDCountVec = typeIDMat.getRow(type);
                                if (IDCountVec == null)
                                {
                                    continue;
                                }

                                double typeProb         = 1;
                                double curObjTotalCount = IDCountVec.getSum();

                                double histClus_total_count = histClus.clusTypeCountMat[clusID, type];
                                double curClus_total_Count  = curClus.clusTypeCountMat[clusID, type];

                                int i = 1;// record total current count

                                foreach (KeyValuePair <int, double> kvp in IDCountVec.vector)
                                {
                                    int    ID    = kvp.Key;
                                    double count = kvp.Value;

                                    //if (!attributeDict[type].ContainsKey(ID))
                                    //{
                                    //    Console.WriteLine("Type {0} ID {1}is not in dictionary", type, ID);
                                    //}

                                    double hist_item_count = histClus.clusAttriMatList[type][clusID, ID];
                                    double cur_item_count  = curClus.clusAttriMatList[type][clusID, ID];

                                    for (int itemCount = 1; itemCount <= count; itemCount++)
                                    {
                                        if (curClus.objectClusMat[objID, clusID] == 1)//clusID contains objID, needs to remove it
                                        {
                                            //background 1:
                                            //typeProb *= /*Math.Log*/((attributeDict[type][ID] * betaVec[type] + hist_item_count + cur_item_count /*- count + count*/ - itemCount)
                                            //    / (typeCount[type] * betaVec[type] + histClus_total_count + curClus_total_Count /*- curObjTotalCount + curObjTotalCount*/ - i));
                                            //logTypeProb -= Math.Log(attributeDict[type].Count * betaVec[type] + histClus_total_count + curClus_total_Count - curClus_total_Count + curObjTotalCount - i);

                                            //background 2:
                                            typeProb *= /*Math.Log*/ ((1 * betaVec[type] + hist_item_count + cur_item_count /*- count + count*/ - itemCount)
                                                                      / (attributeDict[type].Count * betaVec[type] + histClus_total_count + curClus_total_Count /*+ curObjTotalCount - curObjTotalCount*/ - i));
                                        }
                                        else
                                        {
                                            //background 1:
                                            //typeProb *= /*Math.Log*/((attributeDict[type][ID] * betaVec[type] + hist_item_count + cur_item_count + count - itemCount)
                                            //    / (typeCount[type] * betaVec[type] + histClus_total_count + curClus_total_Count + curObjTotalCount - i));
                                            //logTypeProb -= Math.Log(attributeDict[type].Count * betaVec[type] + histClus_total_count + curClus_total_Count + curObjTotalCount - i);

                                            //background 2:
                                            typeProb *= /*Math.Log*/ ((1 * betaVec[type] + hist_item_count + cur_item_count + count - itemCount)
                                                                      / (attributeDict[type].Count * betaVec[type] + histClus_total_count + curClus_total_Count + curObjTotalCount - i));
                                        }
                                        i++;
                                    }
                                }

                                log_prob_in_clus += Math.Log(typeProb);
                            }
                            if (iter < INITITER)
                            {
                                clusProbVec[clusID] = log_prob_in_clus;
                            }
                            else
                            {
                                double tempClusSize = clusSize;
                                if (curClus.objectClusMat[objID, clusID] == 1) //clusID
                                {
                                    tempClusSize = tempClusSize - 1;
                                }
                                //if (tempClusSize == 0)
                                //{
                                //    clusProbVec[clusID] = log_prob_in_clus + log_alpha;
                                //}
                                //else
                                {
                                    clusProbVec[clusID] = log_prob_in_clus + Math.Log(tempClusSize);
                                }
                            }
                        }
                        //new cluster, only calculate if current objID is not the only object in its cluster and with no history
                        int          oldClusID       = -1;
                        bool         needsNewCluster = true;
                        SparseVector assignVec       = curClus.objectClusMat.getRow(objID);
                        if (assignVec != null)
                        {
                            if (assignVec.vector.Count != 0)
                            {
                                oldClusID = assignVec.getMax_Pos().Key;
                            }

                            if (oldClusID != -1 && curClus.clusterSizeVec[oldClusID] == 1)
                            {
                                needsNewCluster = false;
                            }

                            if (histClus.clusterList.Contains(oldClusID))
                            {
                                needsNewCluster = true;
                            }
                        }

                        if (needsNewCluster)
                        {
                            double log_prob_in_newclus = 0;

                            for (int type = 0; type < typeNum; type++)
                            {
                                if (!subTypeSet.Contains(type))
                                {
                                    continue;
                                }
                                SparseVector IDCountVec = typeIDMat.getRow(type);
                                if (IDCountVec == null)
                                {
                                    continue;
                                }
                                double typeProb         = 1;
                                double curObjTotalCount = IDCountVec.getSum();
                                int    i = 1;
                                foreach (KeyValuePair <int, double> kvp in IDCountVec.vector)
                                {
                                    int    ID    = kvp.Key;
                                    double count = kvp.Value;

                                    for (int itemCount = 1; itemCount <= count; itemCount++)
                                    {
                                        //background 2:
                                        typeProb *= /*Math.Log*/ ((1 * betaVec[type] + count - itemCount) / (attributeDict[type].Count * betaVec[type] + curObjTotalCount - i));

                                        i++;
                                    }
                                }
                                log_prob_in_newclus += Math.Log(typeProb);
                            }
                            if (iter < INITITER)
                            {
                                clusProbVec[newClusID] = log_prob_in_newclus;
                            }
                            else
                            {
                                clusProbVec[newClusID] = log_prob_in_newclus + Math.Log(gamma * (betaCoeffVec[-1] / betaCoeffVec.getSum()));
                            }
                        }

                        //pick the cluster with the largest logprob
                        KeyValuePair <int, double> maxClusLogP = clusProbVec.getMax_Pos();

                        if (oldClusID != maxClusLogP.Key)
                        {
                            clusChangedNum++;

                            //code for test
                            if (epsi < 0.0001)
                            {
                                Console.WriteLine("\tObj {0} from {1} to {2}", objID, oldClusID, maxClusLogP.Key);
                            }
                        }

                        curClus.assignCluster(objID, typeIDMat, oldClusID, maxClusLogP.Key);

                        handledObjNum++;

                        //update candidateClus
                        if (oldClusID != -1 && !curClus.clusterList.Contains(oldClusID))
                        {
                            //candidateClus.Remove(oldClusID);
                            // This sentence was originally here.
                            // groupClusSizeMat[priorGroup, oldClusID]--;
                            //betaCoeffVec[oldClusID] = 0;
                        }
                        if (maxClusLogP.Key == newClusID)//assign to a new cluster
                        {
                            candidateClus.Add(newClusID);
                            Console.WriteLine("maxClus = {0}, objects handled = {1}", newClusID, handledObjNum);
                            Cluster.MAXClusID++;
                        }

                        if (maxClusLogP.Key != oldClusID)
                        {
                            // We move the sentence here.
                            groupClusSizeMat[priorGroup, oldClusID]--;
                            groupClusSizeMat[priorGroup, maxClusLogP.Key]++;
                            betaCoeffVec[maxClusLogP.Key] = Math.Log(curClus.clusterSizeVec[maxClusLogP.Key] + 1);
                        }
                    }
                }

                TimeSpan timeSpan = DateTime.Now - curTime;
                epsi = (double)clusChangedNum / objSize;
                Console.WriteLine("Iteration {0}: cluster number = {1}, epsi = {2}, execution time = {3}", ++iter, curClus.clusterList.Count, epsi, timeSpan);
            }
            curClus.clusterPriorMat = groupClusSizeMat.getTranspose();
            return(curClus);
        }
 /// <summary>
 /// Creates a vector from an array.
 /// </summary>
 /// <param name="data">The array to create this vector from.</param>
 /// <returns>The new vector. </returns>
 protected override Vector <double> CreateVector(double[] data)
 {
     return(SparseVector.OfEnumerable(data));
 }
Beispiel #50
0
 public VCInfo(int total_varcount)
 {
     total_varcount_ = total_varcount;
     tempmultstorage = new ThreadLocal <Vector <double> >(() => SparseVector.Create(total_varcount, 0.0));
 }
Beispiel #51
0
        static void Main(string[] args)
        {
            // create SparseVector
            Console.WriteLine("Create SparseVector ...");
            SparseVector <string> vec = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(1, "a"),
                new IdxDat <string>(3, "b"),
                new IdxDat <string>(4, "c"),
                new IdxDat <string>(6, "d")
            });

            Console.WriteLine(vec);
            // add more items
            Console.WriteLine("Add more items ...");
            // ... at the end
            vec.Add("E");
            vec.AddRange(new string[] { "F", "G" });
            // ... at specific places
            vec.AddRange(new IdxDat <string>[] {
                new IdxDat <string>(2, "AB"),
                new IdxDat <string>(10, "H")
            });
            vec[11] = "i";
            Console.WriteLine(vec);
            // get items
            Console.WriteLine("Get items ...");
            Console.WriteLine(vec[1]);
            Console.WriteLine(vec.TryGet(2, "missing"));
            Console.WriteLine(vec.TryGet(5, "missing"));
            // set items
            Console.WriteLine("Set items ...");
            vec[2]  = "ab";
            vec[10] = "h";
            Console.WriteLine(vec);
            // check for items
            Console.WriteLine("Check for items ...");
            Console.WriteLine(vec.ContainsAt(2));
            Console.WriteLine(vec.ContainsAt(5));
            // get first and last non-empty index
            Console.WriteLine("Get first and last non-empty index ...");
            Console.WriteLine(vec.FirstNonEmptyIndex);
            Console.WriteLine(vec.LastNonEmptyIndex);
            // get first and last item
            Console.WriteLine("Get first and last item ...");
            Console.WriteLine(vec.First);
            Console.WriteLine(vec.Last);
            // create another SparseVector
            Console.WriteLine("Create another SparseVector ...");
            SparseVector <string> vec2 = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(0, "!"),
                new IdxDat <string>(2, "@"),
                new IdxDat <string>(3, "#"),
                new IdxDat <string>(5, "$")
            });

            Console.WriteLine(vec2);
            // concatenate
            Console.WriteLine("Concatenate ...");
            vec.Append(vec2, vec.LastNonEmptyIndex + 1);
            Console.WriteLine(vec);
            vec2.Append(vec, vec2.LastNonEmptyIndex + 1);
            Console.WriteLine(vec2);
            // get number of items
            Console.WriteLine("Get number of items ...");
            Console.WriteLine(vec.Count);
            // remove item
            Console.WriteLine("Remove item ...");
            vec.RemoveAt(2);
            Console.WriteLine(vec);
            // directly access to items
            Console.WriteLine("Directly access to items ...");
            int idx = vec.GetDirectIdx(3);

            Console.WriteLine(idx);
            vec.SetDirect(idx, "bbb");
            Console.WriteLine(vec);
            Console.WriteLine(vec.GetIdxDirect(idx));
            Console.WriteLine(vec.GetDatDirect(idx));
            Console.WriteLine(vec.GetDirect(idx));
            vec.RemoveDirect(idx);
            Console.WriteLine(vec);
            // perform unary operation
            Console.WriteLine("Perform unary operation ...");
            vec.PerformUnaryOperation(delegate(string item) { return(item.ToUpper()); });
            Console.WriteLine(vec);
            // merge
            Console.WriteLine("Merge ...");
            vec.Merge(vec2, delegate(string a, string b) { return(string.Format("{0}+{1}", a, b)); });
            Console.WriteLine(vec);
            // purge
            Console.WriteLine("Purge items ...");
            vec.PurgeAt(1);
            Console.WriteLine(vec);
            vec.PurgeAt(1);
            Console.WriteLine(vec);
            Console.WriteLine();

            // *** SparseMatrix ***
            Console.WriteLine("*** SparseMatrix ***");
            Console.WriteLine();
            // create SparseMatrix
            Console.WriteLine("Create SparseMatrix ...");
            SparseMatrix <string> matrix = new SparseMatrix <string>();

            matrix[0] = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(1, "a"),
                new IdxDat <string>(3, "b"),
                new IdxDat <string>(4, "c")
            });
            matrix[2] = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(2, "d"),
                new IdxDat <string>(4, "e"),
                new IdxDat <string>(5, "f")
            });
            matrix[3] = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(0, "g"),
                new IdxDat <string>(3, "h"),
                new IdxDat <string>(5, "i")
            });
            matrix[4] = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(1, "j"),
                new IdxDat <string>(2, "k"),
                new IdxDat <string>(4, "l")
            });
            Console.WriteLine(matrix.ToString("E"));
            // get rows
            Console.WriteLine("Get rows ...");
            Console.WriteLine(matrix[0]);
            Console.WriteLine(matrix[3]);
            // set rows
            Console.WriteLine("Set rows ...");
            matrix[1] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(0, "j"), new IdxDat <string>(3, "k") });
            matrix[2] = null;
            matrix[4] = null;
            Console.WriteLine(matrix.ToString("E"));
            // count rows
            Console.WriteLine("Count rows ...");
            Console.WriteLine("{0} != {1}", matrix.GetRowCount(), matrix.GetLastNonEmptyRowIdx() + 1);
            // trim rows
            Console.WriteLine("Trim rows ...");
            matrix.TrimRows();
            Console.WriteLine(matrix.ToString("E"));
            // add more items
            Console.WriteLine("Add more items ...");
            matrix[0].Add("*");
            matrix[3].AddRange(new IdxDat <string>[] {
                new IdxDat <string>(1, "!"),
                new IdxDat <string>(2, "?"),
                new IdxDat <string>(4, "&")
            });
            matrix[2] = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(2, "d"),
                new IdxDat <string>(4, "e"),
                new IdxDat <string>(5, "f")
            });
            Console.WriteLine(matrix.ToString("E"));
            // get items
            Console.WriteLine("Get items ...");
            Console.WriteLine(matrix[0, 1]);
            Console.WriteLine(matrix[2, 2]);
            Console.WriteLine(matrix[2][4]);
            Console.WriteLine(matrix.TryGet(2, 4, "missing"));
            Console.WriteLine(matrix.TryGet(2, 6, "missing"));
            // set items
            Console.WriteLine("Set items ...");
            matrix[0, 1] = "l";
            matrix[2, 3] = "m";
            matrix[3][4] = "n";
            Console.WriteLine(matrix.ToString("E"));
            // check for items
            Console.WriteLine("Check for items ...");
            Console.WriteLine(matrix.ContainsAt(0, 1));
            Console.WriteLine(matrix.ContainsAt(1, 1));
            Console.WriteLine(matrix.Contains("c"));
            Console.WriteLine(matrix.Contains("C"));
            int rowIdx = -1, colIdx = -1;

            matrix.IndexOf("c", ref rowIdx, ref colIdx);
            Console.WriteLine("{0}, {1}", rowIdx, colIdx);
            // check for rows and columns
            Console.WriteLine("Check for rows and columns ...");
            Console.WriteLine(matrix.ContainsColAt(0));
            Console.WriteLine(matrix.ContainsColAt(100));
            Console.WriteLine(matrix.ContainsRowAt(0));
            Console.WriteLine(matrix.ContainsRowAt(100));
            // get first and last non-empty row and column index
            Console.WriteLine("Get first and last non-empty row and column index ...");
            Console.WriteLine(matrix.GetFirstNonEmptyRowIdx());
            Console.WriteLine(matrix.GetLastNonEmptyRowIdx());
            Console.WriteLine(matrix.GetFirstNonEmptyColIdx());
            Console.WriteLine(matrix.GetLastNonEmptyColIdx());
            // get first and last item in row
            Console.WriteLine("Get first and last item in row ...");
            Console.WriteLine(matrix[0].First);
            Console.WriteLine(matrix[3].Last);
            // create another SparseMatrix
            Console.WriteLine("Create another SparseMatrix ...");
            SparseMatrix <string> matrix2 = new SparseMatrix <string>();

            matrix2[0] = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(0, "A"),
                new IdxDat <string>(2, "B"),
                new IdxDat <string>(3, "C")
            });
            matrix2[2] = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(1, "D"),
                new IdxDat <string>(3, "E")
            });
            matrix2[3] = new SparseVector <string>(new IdxDat <string>[] {
                new IdxDat <string>(0, "G"),
                new IdxDat <string>(1, "H"),
                new IdxDat <string>(2, "I")
            });
            Console.WriteLine(matrix2.ToString("E"));
            // concatenate
            Console.WriteLine("Concatenate ...");
            matrix.AppendCols(matrix2, matrix.GetLastNonEmptyColIdx() + 1);
            Console.WriteLine(matrix.ToString("E"));
            // remove items
            Console.WriteLine("Remove items ...");
            matrix.RemoveAt(0, 1);
            matrix.RemoveAt(3, 5);
            Console.WriteLine(matrix.ToString("E"));
            // directly access to items
            Console.WriteLine("Directly access to items ...");
            idx = matrix[0].GetDirectIdx(4);
            Console.WriteLine(idx);
            Console.WriteLine(matrix[0].GetDirect(idx));
            matrix[0].SetDirect(idx, "C");
            Console.WriteLine(matrix[1].GetDirect(0));
            matrix[1].RemoveDirect(0);
            Console.WriteLine(matrix.ToString("E"));
            // get properties
            Console.WriteLine("Get properties ...");
            Console.WriteLine("{0:0.00}%", matrix.GetSparseness(matrix.GetLastNonEmptyRowIdx() + 1, matrix.GetLastNonEmptyColIdx() + 1) * 100.0);
            Console.WriteLine(matrix.IsSymmetric());
            Console.WriteLine(matrix.ContainsDiagonalElement());
            Console.WriteLine(matrix.CountValues());
            // perform unary operation
            Console.WriteLine("Perform unary operation ...");
            matrix.PerformUnaryOperation(delegate(string item) { return(item.ToUpper()); });
            Console.WriteLine(matrix.ToString("E"));
            // merge
            Console.WriteLine("Merge ...");
            matrix.Merge(matrix2, delegate(string a, string b) { return(string.Format("{0}+{1}", a, b)); });
            Console.WriteLine(matrix.ToString("E"));
            // clear row and column
            Console.WriteLine("Clear row and column ...");
            matrix.RemoveRowAt(2);
            matrix.RemoveColAt(1);
            Console.WriteLine(matrix.ToString("E"));
            // purge row and column
            Console.WriteLine("Purge row and column ...");
            matrix.PurgeRowAt(2);
            matrix.PurgeColAt(1);
            Console.WriteLine(matrix.ToString("E"));
            // get column copy
            Console.WriteLine("Get column copy ...");
            Console.WriteLine(matrix.GetColCopy(0));
            // transpose
            Console.WriteLine("Transpose ...");
            Console.WriteLine(matrix.GetTransposedCopy().ToString("E"));
            // set diagonal
            Console.WriteLine("Set diagonal ...");
            matrix.SetDiagonal(matrix.GetLastNonEmptyColIdx() + 1, "X");
            Console.WriteLine(matrix.ToString("E"));
            // make symmetric
            Console.WriteLine("Make symmetric ...");
            matrix.Symmetrize(delegate(string a, string b) { return(string.Format("{0}+{1}", a, b)); });
            Console.WriteLine(matrix.ToString("E"));
        }
Beispiel #52
0
 private LPSTerm()
 {
     coefficients_ = SparseVector.Create(vcinfo_.total_varcount_, 0.0);
     intercept_    = 0.0;
 }
        public void CanPointwiseMultiplySparseVector()
        {
            var zeroArray = new[] { 0.0, 1.0, 0.0, 1.0, 0.0 };
            var vector1 = new SparseVector(Data);
            var vector2 = new SparseVector(zeroArray);
            var result = new SparseVector(vector1.Count);

            vector1.PointwiseMultiply(vector2, result);

            for (var i = 0; i < vector1.Count; i++)
            {
                Assert.AreEqual(Data[i] * zeroArray[i], result[i]);
            }

            Assert.AreEqual(2, result.NonZerosCount);
        }
Beispiel #54
0
 public void Sparsify()
 {
     coefficients_ = SparseVector.OfVector(coefficients_);
 }
Beispiel #55
0
 public double SVMOutput(SparseVector x)
 {
     return(Calculate_F(x));
 }
Beispiel #56
0
 public Vector <double> CreateVector(int capacity)
 {
     return(SparseVector.Create(capacity, 0.0));
 }
Beispiel #57
0
 public State()
 {
     SparseData = new SparseVector();
 }
Beispiel #58
0
 public double Compute(SparseVector x, SparseVector z)
 {
     return(SparseVector.DotProduct(x, z));
 }
Beispiel #59
0
        private double[][] GetKernel(int rmvFeatIdx)
        {
            int numSv = SvmLightLib.GetSupportVectorCount(mModelId);

            // initialize matrix
            double[][] kernel = new double[numSv][];
            // compute linear kernel
            SparseMatrix <double> m = new SparseMatrix <double>();

            for (int i = 0; i < numSv; i++)
            {
                SparseVector <double> sv = GetSupportVector(i);
                m[i] = sv;
            }
            if (rmvFeatIdx >= 0)
            {
                m.RemoveColAt(rmvFeatIdx);
            }
            SparseMatrix <double> mTr = m.GetTransposedCopy();

            for (int i = 0; i < numSv; i++)
            {
                double[] innerProd = ModelUtils.GetDotProductSimilarity(mTr, numSv, m[i]);
                kernel[i] = innerProd;
            }
            // compute non-linear kernel
            switch (mKernelType)
            {
            case SvmLightKernelType.Polynomial:
                for (int row = 0; row < kernel.Length; row++)
                {
                    for (int col = 0; col < kernel.Length; col++)
                    {
                        kernel[row][col] = Math.Pow(mKernelParamS * kernel[row][col] + mKernelParamC, mKernelParamD);
                    }
                }
                break;

            case SvmLightKernelType.RadialBasisFunction:
                double[] diag = new double[kernel.Length];
                for (int i = 0; i < kernel.Length; i++)
                {
                    diag[i] = kernel[i][i];
                }                                                                       // save diagonal
                for (int row = 0; row < kernel.Length; row++)
                {
                    for (int col = 0; col < kernel.Length; col++)
                    {
                        kernel[row][col] = Math.Exp(-mKernelParamGamma * (diag[row] + diag[col] - 2.0 * kernel[row][col]));
                    }
                }
                break;

            case SvmLightKernelType.Sigmoid:
                for (int row = 0; row < kernel.Length; row++)
                {
                    for (int col = 0; col < kernel.Length; col++)
                    {
                        kernel[row][col] = Math.Tanh(mKernelParamS * kernel[row][col] + mKernelParamC);
                    }
                }
                break;
            }
            return(kernel);
        }
Beispiel #60
0
        public double[] Simulate(double t, double[] values)
        {
            SimulationState st = new SimulationState
            {
                VX       = values[3],
                VY       = values[4],
                VZ       = values[5],
                Phi      = values[6],
                Theta    = values[7],
                PhiDot   = values[8],
                ThetaDot = values[9],
                GammaDot = values[10],
                Gamma    = values[11],
            };

            //double CLo, CLa, CDo, CDa, CMo, CMa;
            //double CL_data, CD_data, CM_data;

            double[] CRr_rad = new[] { -0.0873, -0.0698, -0.0524, -0.0349, -0.0175, 0.0000, 0.0175, 0.0349, 0.0524, 0.0698, 0.0873, 0.1047, 0.1222, 0.1396, 0.1571, 0.1745, 0.1920, 0.2094, 0.2269, 0.2443, 0.2618, 0.5236 };

            double[] CRr_AdvR = new[] { 2, 1.04, 0.69, 0.35, 0.17, 0 };

            double[,] CRr_data = new [, ]
            {
                { -0.0172, -0.0192, -0.018, -0.0192, -0.018, -0.0172, -0.0172, -0.0168, -0.0188, -0.0164, -0.0136, -0.01, -0.0104, -0.0108, -0.0084, -0.008, -0.008, -0.006, -0.0048, -0.0064, -0.008, -0.003 },
                { -0.0112, -0.0132, -0.012, -0.0132, -0.012, -0.0112, -0.0112, -0.0108, -0.0128, -0.0104, -0.0096, -0.0068, -0.0072, -0.0076, -0.0052, -0.0048, -0.0048, -0.0028, -0.0032, -0.0048, -0.0064, -0.003 },
                { -0.0056, -0.0064, -0.0064, -0.0068, -0.0064, -0.0064, -0.0052, -0.0064, -0.0028, -0.0028, -0.004, -0.002, -0.004, -0.002, -0.0016, 0, 0, 0, 0, -0.002, -0.0048, -0.003 },
                { -0.0012, -0.0016, -0.0004, -0.0028, -0.0016, -0.0016, -0.0004, 0.0004, 0.0004, 0.0008, 0.0004, 0.0008, 0.0012, 0.0008, 0.002, 0.0028, 0.0032, 0.0024, 0.0028, 0.0004, -0.0012, -0.003 },
                { -0.0012, -0.0012, -0.0016, -0.0016, -0.0012, -0.0004, 0.0004, 0.0008, 0.0008, 0.0016, 0.0004, 0.002, 0.0004, 0.0016, 0.002, 0.002, 0.002, 0.0012, 0.0012, 0, -0.0012, -0.003 },
                { -0.0012, -0.0012, -0.0004, -0.0008, -0.0008, -0.0008, 0.0004, 0.0004, 0.0004, 0.0008, 0.0004, 0.0008, -0.0004, 0, 0, 0.0004, 0, 0, 0.0004, -0.002, -0.0012, -0.003 }
            };

            const double CMq = -0.005;
            const double CRp = -0.0055;
            const double CNr = 0.0000071;

            double diameter = 2 * Math.Sqrt(Area / Math.PI);

            //  Rotation matrix: http://s-mat-pcs.oulu.fi/~mpa/matreng/eem1_3-7.htm
            //                           y
            //  ------------------> x                        ^
            //  |\                       |
            //  | \                      |
            //  |  \                     |
            //  |   \ theta = pitch      |   gamma = yaw
            //  |                        |
            //  v                        --------------------> x
            //  z
            //  z
            //  ^
            //  |
            //  |
            //  |
            //  |  phi = roll
            //  |
            //  ------------------> y
            //
            // 3D homogenous transformation matrix
            //
            // g = gamma = yaw
            // t = theta = pitch
            // p = phi   = roll
            //
            // http://en.wikipedia.org/wiki/Rotation_matrix
            // http://www.gregslabaugh.name/publications/euler.pdf
            //       --                                                                                   --
            //      | cos(g)*cos(t), cos(g)*sin(t)*sin(p)-sin(g)*cos(p), cos(g)*sin(t)*cos(p)+sin(g)*sin(p) |
            //      |                                                                                       |
            //  T = | sin(g)*cos(t), sin(g)*sin(t)*sin(p)-cos(g)*cos(p), sin(g)*sin(t)*cos(p)+cos(g)*sin(p) |
            //      |                                                                                       |
            //      |    -sin(t)   ,          cos(t)*sin(p)            ,          cos(t)*cos(p)             |
            //       --                                                                                   --
            //
            // With g = yaw = 0 and sin(t) = -sin(t) since z is positive downward
            //
            //       --                                      --
            //      | cos(t)  , sin(t)*sin(p)  , sin(t)*cos(p) |
            //      |                                          |
            //  T = |   0     ,     cos(p)     ,     sin(p)    |
            //      |                                          |
            //      | -sin(t) , cos(t)*sin(p)  , cos(t)*cos(p) |
            //       --                                      --



            Matrix <double> transformation = new SparseMatrix(new [, ]
            {
                { st.CosTheta, st.SinTheta * st.SinPhi, -st.SinTheta * st.CosPhi },
                { 0, st.CosPhi, st.SinPhi },
                { st.SinTheta, -st.CosTheta * st.SinPhi, st.CosTheta * st.CosPhi }
            });

            // Eigenvector & eigenvalue
            //       --
            //      | x1
            //  X = | x2
            //      | x3
            //       --
            //
            //       --
            //      | a11, a12, a13
            //  A = | a21, a22, a23
            //      | a31, a32, a33
            //       --
            //
            //
            // Usually, the multiplication of a vector x by a square matrix A changes both the magnitude and the direction
            // of the vector upon which it acts; but in the special case where it changes only the scale (magnitude) of the
            // vector and leaves the direction unchanged, or switches the vector to the opposite direction, then that vector
            // is called an eigenvector of that matrix (the term "eigenvector" is meaningless except in relation to some
            // particular matrix). When multiplied by a matrix, each eigenvector of that matrix changes its magnitude by a
            // factor, called the eigenvalue corresponding to that eigenvector.
            //
            //
            // See local frame vs global frame:
            //

            Evd evd = new UserEvd(transformation);
            //Matrix<double> eigenVectors = evd.EigenVectors();
            Vector <Complex> temp        = evd.EigenValues();
            Vector <double>  eigenValues = new SparseVector(3);

            eigenValues[0] = temp[0].Real;
            eigenValues[1] = temp[1].Real;
            eigenValues[1] = temp[1].Real;

            //eigenValues.Norm

            //
            // If you have Theta and Phi = 0 you have a transformation matrix like this:
            //
            // | 1, 0, 0 |
            // | 0, 1, 0 |
            // | 0, 0, 1 |
            //
            // So each row represents the rotated X, Y or Z axis expressed as N-Frame coordinates. In this case,
            // there is no rotation so you have the axis (1,0,0), (0,1,0), (0,0,1).
            // For example, the first row represents the X Axis after the rotation. Since the rotation is 0,
            // the X axis is a vector (1,0,0) in the N-Frame.
            //
            //
            //
            //
            //SparseVector c1 = new SparseVector(transformation.Row(0));
            //SparseVector c2 = new SparseVector(transformation.Row(1));
            SparseVector c3 = new SparseVector(transformation.Row(2));

            SparseVector velocity          = new SparseVector(new [] { st.VX, st.VY, st.VZ });
            double       velocityMagnitude = velocity.Norm(2);

            double velocityC3 = velocity.DotProduct(c3);

            Vector <double> vp          = velocity.Subtract(c3.Multiply(velocityC3));
            double          vpMagnitude = vp.Norm(2);


            double alpha = Math.Atan(velocityC3 / vp.Norm(2));
            double adp   = Area * Rho * velocityMagnitude * velocityMagnitude / 2;

            Vector <double> unitVelocity = velocity.Divide(velocityMagnitude);
            Vector <double> unitVp       = vp.Divide(vpMagnitude);

            //c3.
            Vector <double> unitLat = ConvertVector(Vector3D.CrossProduct(ConvertVector(c3), ConvertVector(unitVp)));

            Matrix <double> omegaD_N_inC = new SparseMatrix(new [, ] {
                { st.PhiDot *st.CosTheta, st.ThetaDot, st.PhiDot *st.SinTheta + st.GammaDot }
            });                                                                                                     // expressed in c1,c2,c3
            Vector <double> omegaD_N_inN = transformation.Transpose().Multiply(omegaD_N_inC.Transpose()).Column(0); // expressed in c1,c2,c3
            double          omegaVp      = omegaD_N_inN.DotProduct(unitVp);
            double          omegaLat     = omegaD_N_inN.DotProduct(unitLat);
            double          omegaSpin    = omegaD_N_inN.DotProduct(c3);

            double aDvR = diameter * omegaSpin / 2 / vpMagnitude;

            LinearSplineInterpolation interpolation = new LinearSplineInterpolation(m_xCL, m_yCL);
            double CL = interpolation.Interpolate(alpha);

            interpolation = new LinearSplineInterpolation(m_xCD, m_yCD);
            double CD = interpolation.Interpolate(alpha);

            interpolation = new LinearSplineInterpolation(m_xCM, m_yCM);
            double CM = interpolation.Interpolate(alpha);


            alglib.spline2d.spline2dinterpolant interpolant = new alglib.spline2d.spline2dinterpolant();
            alglib.spline2d.spline2dbuildbilinear(CRr_rad, CRr_AdvR, CRr_data, 6, 22, interpolant);
            double CRr = alglib.spline2d.spline2dcalc(interpolant, alpha, aDvR);

            Vector <double> mvp = unitVp.Multiply(adp * diameter * (CRr * diameter * omegaSpin / 2 / velocityMagnitude + CRp * omegaVp)); // Roll moment, expressed in N

            double lift = CL * adp;
            double drag = CD * adp;

            Vector <double> unitLift = -ConvertVector(Vector3D.CrossProduct(ConvertVector(unitVelocity), ConvertVector(unitLat)));
            Vector <double> unitDrag = -unitVelocity;

            Vector <double> forceAerodynamic = unitLift.Multiply(lift).Add(unitDrag.Multiply(drag));
            Vector <double> gravityForceN    = new SparseVector(new[] { 0, 0, m * g });

            Vector <double> force = forceAerodynamic.Add(gravityForceN);

            Vector <double> mLat    = unitLat.Multiply(adp * diameter * (CM + CMq * omegaLat));
            Vector <double> mSpin   = new SparseVector(new [] { 0, 0, CNr * omegaSpin }); // TODO: Check if missing element
            Vector <double> mvpInC  = transformation.Multiply(mvp);
            Vector <double> mLatInC = transformation.Multiply(mLat);

            Vector <double> moment = mvpInC.Add(mLatInC).Add(mSpin);

            Vector <double> acceleration = force.Divide(m);


            double[] result = new double[12];

            result[0] = velocity[0];
            result[1] = velocity[1];
            result[2] = velocity[2];
            result[3] = acceleration[0];
            result[4] = acceleration[1];
            result[5] = acceleration[2];
            result[6] = -st.PhiDot;
            result[7] = st.ThetaDot;
            result[8] = (moment[0] + Id * st.ThetaDot * st.PhiDot * st.SinTheta -
                         Ia * st.ThetaDot * (st.PhiDot * st.SinTheta + st.GammaDot) + Id * st.ThetaDot * st.PhiDot * st.SinTheta) / Id /
                        st.CosTheta;
            result[9]  = (moment[1] + Ia * st.PhiDot * st.CosTheta * (st.PhiDot * st.SinTheta + st.GammaDot) - Id * st.PhiDot * st.PhiDot * st.CosTheta * st.SinTheta) / Id;
            result[10] = (moment[2] - Ia * (result[9] * st.SinTheta + st.ThetaDot * st.PhiDot * st.CosTheta)) / Ia;
            result[11] = result[10];
            return(result);
        }