Ejemplo n.º 1
0
        /// <returns>true if 'm' is symmetric up to a small epsilon constant</returns>
        public static bool IsSymmetric(DotNetMatrix.GeneralMatrix m)
        {
            const double EPS = 1e-6;

            DotNetMatrix.GeneralMatrix Z = m.Transpose();
            Z = Z.Subtract(m);
            return(Z.NormInf() < EPS);
        }
Ejemplo n.º 2
0
        public static Plane RecomputePlane(List <Vertex> vertices)
        {
            Vector3 center = Vector3.zero;

            foreach (var v in vertices)
            {
                center += v.position;
            }
            center /= vertices.Count;

            var A = new DotNetMatrix.GeneralMatrix(3, 3);

            double[] r = new double[3];

            foreach (var v in vertices)
            {
                Vector3 p = v.position - center;
                r[0] = p.x;
                r[1] = p.y;
                r[2] = p.z;
                for (int j = 0; j < 3; j++)
                {
                    for (int i = j; i < 3; i++)
                    {
                        A.Array[j][i] += r[i] * r[j];
                    }
                }
            }
            for (int j = 1; j < 3; j++)
            {
                for (int i = 0; i < j; i++)
                {
                    A.Array[j][i] = A.Array[i][j];
                }
            }

            var E = A.Eigen();
            var minimal_eigenvalue = E.RealEigenvalues[0];
            int pick = 0;

            for (int i = 1; i < 3; i++)
            {
                if (E.RealEigenvalues[i] < minimal_eigenvalue)
                {
                    minimal_eigenvalue = E.RealEigenvalues[i];
                    pick = i;
                }
            }
            var eigenvectors = E.GetV();
            var normal       = new Vector3((float)eigenvectors.Array[0][pick],
                                           (float)eigenvectors.Array[1][pick],
                                           (float)eigenvectors.Array[2][pick]);

            return(new Plane(normal, center));
        }
Ejemplo n.º 3
0
 /// <returns>true when exact 'value' is anywhere on the diagonal.</returns>
 public static bool HasValueOnDiagonal(DotNetMatrix.GeneralMatrix m, double value)
 {
     for (int i = 0; i < Math.Min(m.RowDimension, m.ColumnDimension); i++)
     {
         if (m.GetElement(i, i) == value)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 4
0
        public static String ToString(DotNetMatrix.GeneralMatrix m)
        {
            System.Text.StringBuilder result = new System.Text.StringBuilder();

            for (int i = 0; i < m.RowDimension; i++)
            {
                for (int j = 0; j < m.ColumnDimension; j++)
                {
                    result.Append(m.GetElement(i, j));
                    result.Append(" ");
                }
                result.Append("\n");
            }
            return(result.ToString());
        }
Ejemplo n.º 5
0
        public static bool IsDiagonal(DotNetMatrix.GeneralMatrix m)
        {
            const double EPS = 1e-6;

            for (int i = 0; i < m.RowDimension; i++)
            {
                for (int j = 0; j < m.ColumnDimension; j++)
                {
                    if ((i != j) && (Math.Abs(m.GetElement(i, j)) > EPS))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Returns true when the matrix is diagonal and has only exact 0, +1 and/or -1 entries on the diagonal.
 /// </summary>
 /// <param name="m">the (square) matrix</param>
 /// <returns></returns>
 public static bool IsSimpleDiagonal(DotNetMatrix.GeneralMatrix m)
 {
     for (int i = 0; i < m.RowDimension; i++)
     {
         for (int j = 0; j < m.ColumnDimension; j++)
         {
             double v = m.GetElement(i, j);
             if ((i != j) && (v != 0.0))
             {
                 return(false);
             }
             else if (i == j)
             {
                 if (!((v == 0.0) || (v == 1.0) || (v == -1.0)))
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
Ejemplo n.º 7
0
        public calibrationvalues sensorToTarget(int target, int chart, imagestats.colorvals[] meanvalues, int OBLevel)
        {
            // apply OB offset and do gain correction
            //int OBLevel = (int)num_OBLevel.Value;
            double OBComp = 4095.0 / (4095.0 - OBLevel);

            for (int i = 0; i < 24; i++)
            {
                meanvalues[i].R -= OBLevel >> 2;
                meanvalues[i].R *= OBComp;
                meanvalues[i].GR -= OBLevel >> 2;
                meanvalues[i].GR *= OBComp;
                meanvalues[i].GB -= OBLevel >> 2;
                meanvalues[i].GB *= OBComp;
                meanvalues[i].B -= OBLevel >> 2;
                meanvalues[i].B *= OBComp;
            }

            // reorder colors to B,G,G,R
            /*double temp;
            for (int i = 0; i < 24; i++)
            {
                //for (int j = 0; j < 4; j++) {
                if (rawBayerOrder == 2)
                {
                    temp = meanvalues[i, 0];
                    meanvalues[i, 0] = meanvalues[i, 3];
                    meanvalues[i, 3] = temp;
                }
                //}
            }*/

            // measure WB correction
            double[,] WBRatios = new double[4, 2]; // 4 patches, 2 ratios (R/G, B/G)
            //for (int i = 0; i < 24; i++) {
            for (int j = 0; j < 4; j++)
            {
                WBRatios[j, 0] = meanvalues[j + 19].GR / meanvalues[j + 19].R;
                WBRatios[j, 1] = meanvalues[j + 19].GB / meanvalues[j + 19].B;
            }
            //}

            // calculate WB correction
            double RGain = 0, BGain = 0;
            for (int i = 0; i < 4; i++)
            {
                BGain += WBRatios[i, 0];
                RGain += WBRatios[i, 1];
            }
            RGain /= 4;
            BGain /= 4;

            // apply WB correction
            for (int i = 0; i < 24; i++)
            {
                meanvalues[i].R = meanvalues[i].R * BGain;
                meanvalues[i].B = meanvalues[i].B * RGain;
            }
            // measure exposure compensation
            double[] GainRatios = new double[4]; // 4 patches, green channel
            double[] macBethGreens = new double[4] { 148.4 * 4, 90.6 * 4, 47.8 * 4, 22.2 * 4 };
            for (int j = 0; j < 4; j++)
            {
                GainRatios[j] = macBethGreens[j] / ((meanvalues[j + 19].GR + meanvalues[j + 19].GB)/2);
            }

            // calculate exposure compensation
            double ExposureComp = 0;
            for (int i = 0; i < 4; i++)
            {
                ExposureComp += GainRatios[i];
            }
            ExposureComp /= 4;

            // apply exposure compensation
            for (int i = 0; i < 24; i++)
            {
                meanvalues[i].R = meanvalues[i].R * ExposureComp;
                meanvalues[i].GR = meanvalues[i].GR * ExposureComp;
                meanvalues[i].GB = meanvalues[i].GB * ExposureComp;
                meanvalues[i].B = meanvalues[i].B * ExposureComp;
            }

            // target macbeth color values (linear RGB from sRGB)

            double[][] macBethColors =
            {new double[]{44,21,15},
            new double[]{140,76,55},
            new double[]{28,50,86},
            new double[]{27,38,13},
            new double[]{57,56,110},
            new double[]{31,132,103},
            new double[]{183,51,7},
            new double[]{17,27,100},
            new double[]{138,23,31},
            new double[]{27,11,36},
            new double[]{90,129,12},
            new double[]{199,90,6},
            new double[]{6,13,74},
            new double[]{17,77,16},
            new double[]{109,8,10},
            new double[]{219,147,2},
            new double[]{128,23,78},
            new double[]{0,63,98},
            new double[]{234,233,222},
            new double[]{148,151,149},
            new double[]{91,92,92},
            new double[]{48,49,49},
            new double[]{22,23,23},
            new double[]{8,8,8}};

            double[][] inputRGB = { new double[24], new double[24], new double[24] };
            for (int i = 0; i < 24; i++)
            {
                inputRGB[0][i] = meanvalues[i].B / 4;
                inputRGB[1][i] = (meanvalues[i].GB + meanvalues[i].GR) / 8;
                inputRGB[2][i] = meanvalues[i].R / 4;
            }

            DotNetMatrix.GeneralMatrix RGBin = new DotNetMatrix.GeneralMatrix(inputRGB);
            RGBin = RGBin.Transpose();

            double[][] RedOut = { new double[24] };
            double[][] GreenOut = { new double[24] };
            double[][] BlueOut = { new double[24] };

            for (int i = 0; i < 24; i++)
            {
                RedOut[0][i] = macBethColors[i][0];
                GreenOut[0][i] = macBethColors[i][1];
                BlueOut[0][i] = macBethColors[i][2];
            }

            DotNetMatrix.GeneralMatrix invA = RGBin.Inverse();

            DotNetMatrix.GeneralMatrix b = new DotNetMatrix.GeneralMatrix(RedOut);
            DotNetMatrix.GeneralMatrix bT = b.Transpose();
            DotNetMatrix.GeneralMatrix RGB2RGB_R = invA.Multiply(bT);

            b = new DotNetMatrix.GeneralMatrix(GreenOut);
            bT = b.Transpose();
            DotNetMatrix.GeneralMatrix RGB2RGB_G = invA.Multiply(bT);

            b = new DotNetMatrix.GeneralMatrix(BlueOut);
            bT = b.Transpose();
            DotNetMatrix.GeneralMatrix RGB2RGB_B = invA.Multiply(bT);

            calibrationvalues output = new calibrationvalues();

            //loadingControls = true;
            output.exposuregain = ExposureComp;
            output.RGain = RGain;
            output.BGain = BGain;
            output.RGB_RR = 1 - (double)RGB2RGB_R.Array[1][0] - (double)RGB2RGB_R.Array[2][0];
            output.RGB_RG = (double)RGB2RGB_R.Array[1][0];
            output.RGB_RB = (double)RGB2RGB_R.Array[2][0];
            output.RGB_GR = (double)RGB2RGB_G.Array[0][0];
            output.RGB_GG = 1 - (double)RGB2RGB_G.Array[0][0] - (double)RGB2RGB_G.Array[2][0];
            output.RGB_GB = (double)RGB2RGB_G.Array[2][0];
            output.RGB_BR = (double)RGB2RGB_B.Array[0][0];
            output.RGB_BG = (double)RGB2RGB_B.Array[1][0];
            output.RGB_BB = 1 - (double)RGB2RGB_B.Array[0][0] - (double)RGB2RGB_B.Array[1][0];
            //loadingControls = false;

            return output;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Index the documents
        /// </summary>
        private void IndexThings()
        {
            lsiReader oReader = new lsiReader();

            oReader.IndexDirectory(LSICommon.Instance.LSIConfig.document_directory);


            // The base part. Create the Term Document Matrix. It is also
            DotNetMatrix.GeneralMatrix oLocalWeights = new DotNetMatrix.GeneralMatrix(oReader.WordList.Count, oReader.DocList.Count);
            DocWordRelation            oDocWord;

            foreach (DictionaryEntry oRow in oReader.WordList)
            {
                foreach (DictionaryEntry oCol in oReader.DocList)
                {
                    oDocWord.DocID  = (int)oCol.Key;
                    oDocWord.WordID = (int)oRow.Value;

                    if (oReader.DocWordRelation.ContainsKey(oDocWord))
                    {
                        oLocalWeights.Array[(int)oRow.Value][(int)oCol.Key] = (int)oReader.DocWordRelation[oDocWord];
                    }
                    else
                    {
                        oLocalWeights.Array[(int)oRow.Value][(int)oCol.Key] = 0;
                    }
                }
            }

            // Term Document matrix calculated.

            // NOTE: You can save this TDM here to avoid recalculations
            //         OR save the decomposed & reduced matrices later (I did it later)


            //calculate term weights

            double[] oGlobalTermWeights = new double[oReader.WordList.Count];
            double[] oDocNormFactors    = new double[oReader.DocList.Count];

            //Calculating global weights -> Gi
            for (int i = 0; i < oLocalWeights.Array.Length; i++)
            {
                int    sum = 0;
                double Fi  = 0;
                for (int j = 0; j < oLocalWeights.Array[i].Length; j++)
                {
                    if (oLocalWeights.Array[i][j] > 0)
                    {
                        sum += 1;
                        Fi  += oLocalWeights.Array[i][j];
                    }
                }
                if (sum > 0)
                {
                    oGlobalTermWeights[i] = Math.Log(oReader.DocList.Count / (double)sum);
                }
                else
                {
                    oGlobalTermWeights[i] = 0;
                }

                // The following commented section is another way of weighting the terms
                //if (sum > 0) oGlobalTermWeights[i] = Math.Sqrt((Fi / sum) - 0.9);
                //else oGlobalTermWeights[i] = 0;
            }

            //COMMENTED: Only meant for testing use.
            //Forcing global weights to 1;
            //for (int i = 0; i < oGlobalTermWeights.Length; i++) oGlobalTermWeights[i] = 1;


            //Calculating normalization factors-> Nj. Cosine normalization factor
            for (int j = 0; j < oLocalWeights.Array[0].Length; j++)
            {
                double sum = 0;
                for (int i = 0; i < oLocalWeights.Array.Length; i++)
                {
                    sum += (oGlobalTermWeights[i] * oLocalWeights.Array[i][j])
                           * (oGlobalTermWeights[i] * oLocalWeights.Array[i][j]);
                }
                oDocNormFactors[j] = 1 / Math.Sqrt(sum);
            }


            //COMMENTED: Only meant for testing use.
            //Forcing normalization to 1;
            //for (int i = 0; i < oDocNormFactors.Length; i++) oDocNormFactors[i] = 1;


            //Finally weighting terms and creating a weighted term document matrix
            DotNetMatrix.GeneralMatrix oWTDM = new DotNetMatrix.GeneralMatrix(oReader.WordList.Count, oReader.DocList.Count);

            for (int i = 0; i < oWTDM.Array.Length; i++)
            {
                for (int j = 0; j < oWTDM.Array[i].Length; j++)
                {
                    oWTDM.Array[i][j] = oLocalWeights.Array[i][j] * oGlobalTermWeights[i] * oDocNormFactors[j];
                }
            }


            //end of calculate term weights

            // Calculate SVD
            DotNetMatrix.SingularValueDecomposition svd = new DotNetMatrix.SingularValueDecomposition(oWTDM);


            // Set the svd rank. This will reduce the dimensions in the term document matrix
            int svd_rank = (int)(oReader.DocList.Count * ((double)LSICommon.Instance.LSIConfig.k_percent_for_rank_approx / 100));

            if (svd_rank == 0 || svd_rank > oReader.DocList.Count)
            {
                svd_rank = oReader.DocList.Count;
            }

            // Reduce the vector holding singular values (Sk)
            DotNetMatrix.GeneralMatrix sk = new DotNetMatrix.GeneralMatrix(svd_rank, svd_rank);
            for (int i = 0; i < svd_rank; i++)
            {
                for (int j = 0; j < svd_rank; j++)
                {
                    sk.Array[i][j] = svd.S.Array[i][j];
                }
            }

            // Calculate Sk inverse
            DotNetMatrix.GeneralMatrix skinv = sk.Inverse();

            // Calculate U
            DotNetMatrix.GeneralMatrix u = svd.GetU(); // U

            // Calculate the reduced Uk
            DotNetMatrix.GeneralMatrix uk = new DotNetMatrix.GeneralMatrix(u.Array.Length, svd_rank);
            for (int i = 0; i < u.Array.Length; i++)
            {
                for (int j = 0; j < svd_rank; j++)
                {
                    uk.Array[i][j] = u.Array[i][j];
                }
            }

            #region [Save the index]
            // Save the index
            // Only the necessary values required by the query have been saved.
            // Can store the original TDM for hybrid querying (That is not the purpose in part 1)

            DocIndex oDocIndex = new DocIndex();
            oDocIndex.WordList = new HashObj[oReader.WordList.Count];
            oDocIndex.DocList  = new HashObj[oReader.DocList.Count];
            //o.DocWord = new HashObj[oReader.DocWordRelation.Count];

            foreach (DictionaryEntry oEntry in oReader.WordList)
            {
                oDocIndex.WordList[(int)oEntry.Value].key   = oEntry.Key;
                oDocIndex.WordList[(int)oEntry.Value].value = oEntry.Value;
            }

            foreach (DictionaryEntry oEntry in oReader.DocList)
            {
                oDocIndex.DocList[(int)oEntry.Key].key   = oEntry.Key;
                oDocIndex.DocList[(int)oEntry.Key].value = oEntry.Value;
            }

            oDocIndex.skinv = skinv.Array;
            oDocIndex.uk    = uk.Array;
            oDocIndex.WTDM  = oWTDM.Array;

            BinaryFormatter bf = new BinaryFormatter();
            FileStream      f  = File.Open(LSICommon.Instance.LSIAppPath + @"\index.bin", FileMode.Create);
            bf.Serialize(f, oDocIndex);
            f.Close();

            //End of saving index

            #endregion
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Find the documents related to the query
        /// </summary>
        private void FindThings()
        {
            // Declaring variables required for calculations
            Hashtable WordList = new Hashtable();
            Hashtable DocList  = new Hashtable();

            DotNetMatrix.GeneralMatrix uk    = null;
            DotNetMatrix.GeneralMatrix skinv = null;
            DotNetMatrix.GeneralMatrix oWTDM = null;

            DocIndex oDocIndex;

            oDocIndex.DocList  = null;
            oDocIndex.WordList = null;
            oDocIndex.uk       = null;
            oDocIndex.WTDM     = null;
            oDocIndex.skinv    = null;

            // Read and load the index document
            string          index_path = LSICommon.Instance.LSIAppPath + @"\index.bin";
            bool            bRead      = false;
            BinaryFormatter bf         = new BinaryFormatter();

            if (File.Exists(index_path))
            {
                try
                {
                    FileStream f = File.Open(LSICommon.Instance.LSIAppPath + @"\index.bin", FileMode.Open);
                    oDocIndex = (DocIndex)bf.Deserialize(f);
                    f.Close();
                    bRead = true;
                }
                catch
                {
                    bRead = false;
                }
            }
            else
            {
                bRead = false;
            }

            if (!bRead)
            {
                MessageBox.Show("Index not created. Please index the documents");
                return;
            }


            // Restore the wordlist
            for (int i = 0; i < oDocIndex.WordList.Length; i++)
            {
                WordList.Add(oDocIndex.WordList[i].key, oDocIndex.WordList[i].value);
            }

            // Restore the documentlist
            for (int i = 0; i < oDocIndex.DocList.Length; i++)
            {
                DocList.Add(oDocIndex.DocList[i].key, oDocIndex.DocList[i].value);
            }

            // Restore the other SVD derived vectors and weighted TDM
            uk    = new DotNetMatrix.GeneralMatrix(oDocIndex.uk);
            skinv = new DotNetMatrix.GeneralMatrix(oDocIndex.skinv);
            oWTDM = new DotNetMatrix.GeneralMatrix(oDocIndex.WTDM);


            // Get the query
            string newquery = txtQuery.Text;

            string[] newqwords = LSICommon.Instance.GetWords(newquery);

            // Create the query vector
            DotNetMatrix.GeneralMatrix qt = new DotNetMatrix.GeneralMatrix(1, WordList.Count);
            for (int i = 0; i < WordList.Count; i++)
            {
                qt.Array[0][i] = 0;
            }

            // Apply stemming to the query vector
            PorterStemmer oStemmer = new PorterStemmer();

            for (int i = 0; i < newqwords.Length; i++)
            {
                newqwords[i] = oStemmer.stemTerm(newqwords[i]);
                if (WordList.ContainsKey(newqwords[i]))
                {
                    qt.Array[0][(int)WordList[newqwords[i]]] += 1;
                }
            }

            // Normalizing the query vector
            double qtmax = 0;

            for (int i = 0; i < qt.Array[0].Length; i++)
            {
                if (qt.Array[0][i] > qtmax)
                {
                    qtmax = qt.Array[0][i];
                }
            }

            double[] simarray = new double[DocList.Count];


            if (qtmax != 0)
            {
                for (int i = 0; i < qt.Array[0].Length; i++)
                {
                    qt.Array[0][i] = (qt.Array[0][i] / qtmax);
                }

                // Calculate the resultant query vector
                DotNetMatrix.GeneralMatrix qt_uk = qt.Multiply(uk);       // qT * Uk
                DotNetMatrix.GeneralMatrix q     = qt_uk.Multiply(skinv); // Resultant q

                // Calculate similarities now ( with each derived document vector)
                for (int l = 0; l < DocList.Count; l++)
                {
                    DotNetMatrix.GeneralMatrix d_v = new DotNetMatrix.GeneralMatrix(WordList.Count, 1);
                    for (int k = 0; k < WordList.Count; k++)
                    {
                        d_v.Array[k][0] = oWTDM.Array[k][l];
                    }

                    DotNetMatrix.GeneralMatrix dt    = d_v.Transpose();       // dT
                    DotNetMatrix.GeneralMatrix dt_uk = dt.Multiply(uk);       // dT * uk
                    DotNetMatrix.GeneralMatrix d     = dt_uk.Multiply(skinv); // Resultant d

                    // Calculate cosine similarity:
                    simarray[l] = 0;
                    simarray[l] = calculate_cosine_sim(q.Array[0], d.Array[0]);
                }
            }
            else
            {
                MessageBox.Show("No results found");
                // Since qtmax is zero, the query vector is null. This implies that there can be no results
            }

            // Display the results in datagrid
            DataTable oResTable = new DataTable();
            DataRow   oResDR    = null;

            oResTable.Columns.Add("filename", typeof(string));
            oResTable.Columns.Add("val", typeof(double));
            for (int i = 0; i < simarray.Length; i++)
            {
                oResDR = oResTable.NewRow();

                oResDR["val"]      = (Math.Round(simarray[i] * 100, 6));
                oResDR["filename"] = DocList[i].ToString();

                oResTable.Rows.Add(oResDR);
            }
            dataGridView1.DataSource = oResTable;

            // Sort the results in descending order
            dataGridView1.Sort(dataGridView1.Columns["val"], ListSortDirection.Descending);

            // Formatting the results
            dataGridView1.Columns["val"].HeaderText        = "Rating/100";
            dataGridView1.Columns["filename"].HeaderText   = "Document path";
            dataGridView1.Columns["val"].AutoSizeMode      = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns["filename"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;


            dataGridView1.RowHeadersVisible = false;
            DataGridViewCellStyle oCellStyle = new DataGridViewCellStyle();

            oCellStyle.BackColor = Color.AliceBlue;
            dataGridView1.AlternatingRowsDefaultCellStyle = oCellStyle;


            try
            {
                foreach (DataGridViewRow oRow in dataGridView1.Rows)
                {
                    oRow.Cells["filename"].ToolTipText = ReadShortTextContent(oRow.Cells["filename"].Value.ToString().Trim());
                }
            }
            catch {
                //Supress any exception here
            }
        }