示例#1
0
        /// <summary>
        /// Combines matrix and vector column-wise. Vector is added to the end of the matrix
        /// </summary>
        /// <param name="m"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static StringMatrix CombineCols(this StringMatrix m, string[] v)
        {
            if (m.RowCount != v.Length)
            {
                throw new ArgumentException("matrix must have same number of rows as vector");
            }

            var rows = v.Length;
            var cols = m.ColumnCount + 1;

            var features    = new string[rows * cols];
            var matrixArray = m.Data();

            var combineIndex = 0;

            for (int i = 0; i < rows; i++)
            {
                var matrixIndex = i * m.ColumnCount;
                Array.Copy(matrixArray, matrixIndex, features, combineIndex, m.ColumnCount);

                var otherIndex = i;
                combineIndex += m.ColumnCount;

                Array.Copy(v, otherIndex, features, combineIndex, 1);
                combineIndex += 1;
            }


            return(new StringMatrix(features, rows, cols));
        }
示例#2
0
        /// <summary>
        /// Converts StringMatrix to F64Matrix.
        /// </summary>
        /// <param name="stringMatrix"></param>
        /// <param name="converter"></param>
        /// <returns></returns>
        public static F64Matrix ToF64Matrix(this StringMatrix stringMatrix,
                                            Converter <string, double> converter)
        {
            var features = Array.ConvertAll(stringMatrix.Data(), converter);

            return(new F64Matrix(features, stringMatrix.RowCount, stringMatrix.ColumnCount));
        }
示例#3
0
        /// <summary>
        /// Combines matrix1 and matrix2 column-wise. Matrix2 is added to the end of matrix1
        /// </summary>
        /// <param name="m1"></param>
        /// <param name="m2"></param>
        /// <returns></returns>
        public static StringMatrix CombineCols(this StringMatrix m1, StringMatrix m2)
        {
            if (m1.RowCount != m2.RowCount)
            {
                throw new ArgumentException("matrices must have same number of rows in order to be combined");
            }

            var rows    = m1.RowCount;
            var columns = m1.ColumnCount + m2.ColumnCount;

            var matrixArray = m1.Data();
            var otherArray  = m2.Data();

            var features = new string[matrixArray.Length + otherArray.Length];

            var combineIndex = 0;

            for (int i = 0; i < rows; i++)
            {
                var matrixIndex = i * m1.ColumnCount;
                Array.Copy(matrixArray, matrixIndex, features, combineIndex, m1.ColumnCount);

                var otherIndex = i * m2.ColumnCount;
                combineIndex += m1.ColumnCount;

                Array.Copy(otherArray, otherIndex, features, combineIndex, m2.ColumnCount);
                combineIndex += m2.ColumnCount;
            }

            return(new StringMatrix(features, rows, columns));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            StringMatrix other = obj as StringMatrix;

            if (other != null && Equals(other))
            {
                return(true);
            }

            return(false);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(StringMatrix other)
        {
            if (RowCount != other.RowCount)
            {
                return(false);
            }
            if (ColumnCount != other.ColumnCount)
            {
                return(false);
            }
            if (!Data().SequenceEqual(other.Data()))
            {
                return(false);
            }

            return(true);
        }
示例#6
0
        /// <summary>
        /// Combines matrix1 and matrix2 row-wise. Matrix2 is added to the end of matrix1
        /// </summary>
        /// <param name="m1"></param>
        /// <param name="m2"></param>
        /// <returns></returns>
        public static StringMatrix CombineRows(this StringMatrix m1, StringMatrix m2)
        {
            if (m1.ColumnCount != m2.ColumnCount)
            {
                throw new ArgumentException("matrices must have same number of rows in order to be combined");
            }

            var rows    = m1.RowCount + m2.RowCount;
            var columns = m1.ColumnCount;

            var matrixArray = m1.Data();
            var otherArray  = m2.Data();

            var features = new string[matrixArray.Length + otherArray.Length];

            Array.Copy(matrixArray, features, matrixArray.Length);
            Array.Copy(otherArray, 0, features, matrixArray.Length, otherArray.Length);

            return(new StringMatrix(features, rows, columns));
        }
示例#7
0
 /// <summary>
 /// Iterates over all elements in the matrix and applies the function to the elements.
 /// The values are updated directly in the Matrix.
 /// </summary>
 /// <param name="matrix"></param>
 /// <param name="func"></param>
 public static void Map(this StringMatrix matrix, Func <string, string> func)
 {
     matrix.Data().Map(func);
 }
示例#8
0
 /// <summary>
 /// Converts StringMatrix to F64Matrix. Default converter will throw if there are values not parsable as a double
 /// </summary>
 /// <param name="stringMatrix"></param>
 /// <returns></returns>
 public static F64Matrix ToF64Matrix(this StringMatrix stringMatrix)
 {
     return(ToF64Matrix(stringMatrix, DefaultConverter));
 }
        /// <summary>
        /// Converts StringMatrix to F64Matrix. This will fail if there are values not parsable as a double
        /// </summary>
        /// <param name="stringMatrix"></param>
        /// <returns></returns>
        public static F64Matrix ToF64Matrix(this StringMatrix stringMatrix)
        {
            var features = Array.ConvertAll(stringMatrix.Data(), FloatingPointConversion.ToF64);

            return(new F64Matrix(features, stringMatrix.RowCount, stringMatrix.ColumnCount));
        }