Example #1
0
        /// <summary>
        /// Construct a numerical array.
        /// </summary>
        /// <typeparam name="T">Element type.</typeparam>
        /// <param name="flags">Array flags.</param>
        /// <param name="dimensions">Array dimensions.</param>
        /// <param name="name">Array name.</param>
        /// <param name="realData">Real parts of the values.</param>
        /// <param name="imaginaryData">Imaginary parts of the values.</param>
        /// <returns>A constructed array.</returns>
        /// <remarks>
        /// Possible values for T: Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double,
        ///   ComplexOf&lt;TReal&gt; (where TReal is one of Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single),
        ///   Complex.
        /// </remarks>
        public static MatArray ConvertToMatNumericalArrayOf <T>(
            ArrayFlags flags,
            int[] dimensions,
            string name,
            DataElement realData,
            DataElement imaginaryData)
            where T : struct
        {
            if (flags.Variable.HasFlag(Variable.IsLogical))
            {
                var data = DataExtraction.GetDataAsUInt8(realData).ToArrayLazily().Select(x => x != 0).ToArray();
                return(new MatNumericalArrayOf <bool>(flags, dimensions, name, data));
            }
            switch (flags.Class)
            {
            case ArrayType.MxChar:
                switch (realData)
                {
                case MiNum <byte> dataByte:
                    return(ConvertToMatCharArray(flags, dimensions, name, dataByte));

                case MiNum <ushort> dataUshort:
                    return(ConvertToMatCharArray(flags, dimensions, name, dataUshort));

                default:
                    throw new NotSupportedException("Only utf8, utf16 or ushort char arrays are supported.");
                }

            case ArrayType.MxDouble:
            case ArrayType.MxSingle:
            case ArrayType.MxInt8:
            case ArrayType.MxUInt8:
            case ArrayType.MxInt16:
            case ArrayType.MxUInt16:
            case ArrayType.MxInt32:
            case ArrayType.MxUInt32:
            case ArrayType.MxInt64:
            case ArrayType.MxUInt64:
                var dataArray = ConvertDataToProperType <T>(realData, flags.Class);
                if (flags.Variable.HasFlag(Variable.IsComplex))
                {
                    var dataArray2 = ConvertDataToProperType <T>(imaginaryData, flags.Class);
                    if (flags.Class == ArrayType.MxDouble)
                    {
                        var complexArray =
                            (dataArray as double[])
                            .Zip(dataArray2 as double[], (x, y) => new Complex(x, y))
                            .ToArray();
                        return(new MatNumericalArrayOf <Complex>(flags, dimensions, name, complexArray));
                    }
                    var complexDataArray = dataArray.Zip(dataArray2, (x, y) => new ComplexOf <T>(x, y)).ToArray();
                    return(new MatNumericalArrayOf <ComplexOf <T> >(flags, dimensions, name, complexDataArray));
                }
                return(new MatNumericalArrayOf <T>(flags, dimensions, name, dataArray));

            default:
                throw new NotSupportedException();
            }
        }
Example #2
0
        private static T[]? ConvertDataToSparseProperType <T>(DataElement data, bool isLogical)
        {
            if (isLogical)
            {
                return(DataExtraction.GetDataAsUInt8(data).Select(x => x != 0).ToArray() as T[]);
            }
            switch (data)
            {
            case MiNum <double> _:
                return(DataExtraction.GetDataAsDouble(data) as T[]);

            default:
                throw new NotSupportedException();
            }
        }
Example #3
0
        private static T[] ConvertDataToProperType <T>(DataElement data, ArrayType arrayType)
        {
            switch (arrayType)
            {
            case ArrayType.MxDouble:
                return(DataExtraction.GetDataAsDouble(data).ToArrayLazily() as T[]);

            case ArrayType.MxSingle:
                return(DataExtraction.GetDataAsSingle(data).ToArrayLazily() as T[]);

            case ArrayType.MxInt8:
                return(DataExtraction.GetDataAsInt8(data).ToArrayLazily() as T[]);

            case ArrayType.MxUInt8:
                return(DataExtraction.GetDataAsUInt8(data).ToArrayLazily() as T[]);

            case ArrayType.MxInt16:
                return(DataExtraction.GetDataAsInt16(data).ToArrayLazily() as T[]);

            case ArrayType.MxUInt16:
                return(DataExtraction.GetDataAsUInt16(data).ToArrayLazily() as T[]);

            case ArrayType.MxInt32:
                return(DataExtraction.GetDataAsInt32(data).ToArrayLazily() as T[]);

            case ArrayType.MxUInt32:
                return(DataExtraction.GetDataAsUInt32(data).ToArrayLazily() as T[]);

            case ArrayType.MxInt64:
                return(DataExtraction.GetDataAsInt64(data).ToArrayLazily() as T[]);

            case ArrayType.MxUInt64:
                return(DataExtraction.GetDataAsUInt64(data).ToArrayLazily() as T[]);

            default:
                throw new NotSupportedException();
            }
        }