Ejemplo n.º 1
0
        /// <summary>
        /// Construct a double sparse array or a logical sparse array.
        /// </summary>
        /// <typeparam name="T">Element type (Double or Boolean).</typeparam>
        /// <param name="flags">Array flags.</param>
        /// <param name="dimensions">Array dimensions.</param>
        /// <param name="name">Array name.</param>
        /// <param name="rowIndex">Row indices.</param>
        /// <param name="columnIndex">Denotes index ranges for each column.</param>
        /// <param name="data">The values.</param>
        /// <returns>A constructed array.</returns>
        public static MatArray ConvertToMatSparseArrayOf <T>(
            SparseArrayFlags flags,
            int[] dimensions,
            string name,
            int[] rowIndex,
            int[] columnIndex,
            DataElement data)
            where T : struct
        {
            if (dimensions.Length != 2)
            {
                throw new NotSupportedException("Only 2-dimensional sparse arrays are supported");
            }
            if (data == null)
            {
                throw new ArgumentException("Null data found.", "data");
            }
            var elements =
                ConvertDataToSparseProperType <T>(data, flags.ArrayFlags.Variable.HasFlag(Variable.IsLogical));

            if (elements == null)
            {
                throw new HandlerException("Couldn't read sparse array.");
            }
            var dataDictionary =
                ConvertMatlabSparseToDictionary(rowIndex, columnIndex, j => elements[j]);

            return(new MatSparseArrayOf <T>(flags, dimensions, name, dataDictionary));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Construct a complex sparse array.
        /// </summary>
        /// <param name="flags">Array flags.</param>
        /// <param name="dimensions">Array dimensions.</param>
        /// <param name="name">Array name.</param>
        /// <param name="rowIndex">Row indices.</param>
        /// <param name="columnIndex">Denotes index ranges for each column.</param>
        /// <param name="data">Real parts of the values.</param>
        /// <param name="imaginaryData">Imaginary parts of the values.</param>
        /// <returns>A constructed array.</returns>
        public static MatArray ConvertToMatSparseArrayOfComplex(
            SparseArrayFlags flags,
            int[] dimensions,
            string name,
            int[] rowIndex,
            int[] columnIndex,
            DataElement data,
            DataElement imaginaryData)
        {
            var realParts      = DataExtraction.GetDataAsDouble(data);
            var imaginaryParts = DataExtraction.GetDataAsDouble(imaginaryData);

            if (realParts == null)
            {
                throw new HandlerException("Couldn't read sparse array.");
            }
            var dataDictionary =
                ConvertMatlabSparseToDictionary(
                    rowIndex,
                    columnIndex,
                    j => new Complex(realParts[j], imaginaryParts[j]));

            return(new MatSparseArrayOf <Complex>(flags, dimensions, name, dataDictionary));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MatSparseArrayOf{T}"/> class.
 /// </summary>
 /// <param name="flags">Array properties.</param>
 /// <param name="dimensions">Dimensions of the array.</param>
 /// <param name="name">Array name.</param>
 /// <param name="data">Array contents.</param>
 public MatSparseArrayOf(
     SparseArrayFlags flags,
     int[] dimensions,
     string name,
     Dictionary <(int row, int column), T> data)