internal DenseDoubleMatrixImplementor(
            int numberOfRows,
            int numberOfColumns,
            double[] data,
            StorageOrder storageOrder)
        {
            Debug.Assert(numberOfRows > 0);
            Debug.Assert(numberOfColumns > 0);

            Debug.Assert(data is not null);

            Debug.Assert(
                (StorageOrder.ColumnMajor == storageOrder) ||
                (StorageOrder.RowMajor == storageOrder));

            int matrixLength = numberOfRows * numberOfColumns;

            Debug.Assert(data.Length == matrixLength);

            this.storage = new double[matrixLength];
            data.CopyTo(this.storage, 0);
            if (StorageOrder.RowMajor == storageOrder)
            {
                ImplementationServices.ConvertStorageToColMajorOrdered(
                    numberOfRows, numberOfColumns, this.storage);
            }

            this.numberOfRows    = numberOfRows;
            this.numberOfColumns = numberOfColumns;
            this.storageOrder    = StorageOrder.ColumnMajor;
            this.storageScheme   = StorageScheme.Dense;
        }
        internal DenseDoubleMatrixImplementor(
            int numberOfRows,
            int numberOfColumns,
            double[] data,
            bool copyData)
        {
            Debug.Assert(numberOfRows > 0);
            Debug.Assert(numberOfColumns > 0);

            Debug.Assert(data is not null);

            int matrixLength = numberOfRows * numberOfColumns;

            Debug.Assert(data.Length == matrixLength);

            if (copyData)
            {
                this.storage = new double[matrixLength];
                data.CopyTo(this.storage, 0);
            }
            else
            {
                this.storage = data;
            }

            this.numberOfRows    = numberOfRows;
            this.numberOfColumns = numberOfColumns;
            this.storageOrder    = StorageOrder.ColumnMajor;
            this.storageScheme   = StorageScheme.Dense;
        }
        internal DenseDoubleMatrixImplementor(double[,] data)
        {
            Debug.Assert(data is not null);

            int numberOfRows    = data.GetLength(0);
            int numberOfColumns = data.GetLength(1);

            Debug.Assert(numberOfRows > 0);
            Debug.Assert(numberOfColumns > 0);

            this.numberOfRows    = numberOfRows;
            this.numberOfColumns = numberOfColumns;

            this.storageOrder  = StorageOrder.ColumnMajor;
            this.storageScheme = StorageScheme.Dense;

            this.storage = new double[data.Length];

            int offset;
            int firstDataRow = data.GetLowerBound(0);
            int dataColumn   = data.GetLowerBound(1);

            for (int j = 0; j < this.numberOfColumns; j++, dataColumn++)
            {
                offset = j * this.numberOfRows;
                int dataRow = firstDataRow;
                for (int i = 0; i < this.numberOfRows; i++, dataRow++)
                {
                    this.storage[i + offset] = (double)(data.GetValue(dataRow, dataColumn));
                }
            }
        }
        internal DenseDoubleMatrixImplementor(int numberOfRows, int numberOfColumns)
        {
            Debug.Assert(numberOfRows > 0);
            Debug.Assert(numberOfColumns > 0);

            this.storage         = new double[numberOfRows * numberOfColumns];
            this.numberOfRows    = numberOfRows;
            this.numberOfColumns = numberOfColumns;
            this.storageOrder    = StorageOrder.ColumnMajor;
            this.storageScheme   = StorageScheme.Dense;
        }
        internal DenseDoubleMatrixImplementor(
            int numberOfRows,
            int numberOfColumns,
            IEnumerable <double> data,
            StorageOrder storageOrder)
        {
            Debug.Assert(numberOfRows > 0);
            Debug.Assert(numberOfColumns > 0);
            Debug.Assert(data is not null);
            Debug.Assert(
                (StorageOrder.ColumnMajor == storageOrder) ||
                (StorageOrder.RowMajor == storageOrder));

            int matrixLength = numberOfRows * numberOfColumns;

            Debug.Assert(data.Count() == matrixLength);

            double[] dataStorage = new double[matrixLength];

            int l = 0;

            foreach (double d in data)
            {
                dataStorage[l] = d;
                l++;
            }

            if (StorageOrder.RowMajor == storageOrder)
            {
                ImplementationServices.ConvertStorageToColMajorOrdered(
                    numberOfRows, numberOfColumns, dataStorage);
            }
            this.storage = dataStorage;


            this.numberOfRows    = numberOfRows;
            this.numberOfColumns = numberOfColumns;
            this.storageOrder    = StorageOrder.ColumnMajor;
            this.storageScheme   = StorageScheme.Dense;
        }
Ejemplo n.º 6
0
        ///<inheritdoc/>
        public override MatrixImplementor <double> Read(
            ref Utf8JsonReader reader,
            Type typeToConvert,
            JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new JsonException();
            }

            #region StorageScheme

            reader.Read();
            if (reader.TokenType != JsonTokenType.PropertyName)
            {
                throw new JsonException();
            }

            string propertyName = reader.GetString();
            if (propertyName != "StorageScheme")
            {
                throw new JsonException();
            }

            reader.Read();
            if (reader.TokenType != JsonTokenType.Number)
            {
                throw new JsonException();
            }

            StorageScheme storageScheme = (StorageScheme)reader.GetInt32();

            #endregion

            #region NumberOfRows

            reader.Read();
            if (reader.TokenType != JsonTokenType.PropertyName)
            {
                throw new JsonException();
            }

            propertyName = reader.GetString();
            if (propertyName != "NumberOfRows")
            {
                throw new JsonException();
            }

            reader.Read();
            if (reader.TokenType != JsonTokenType.Number)
            {
                throw new JsonException();
            }

            int numberOfRows = reader.GetInt32();

            #endregion

            #region NumberOfColumns

            reader.Read();
            if (reader.TokenType != JsonTokenType.PropertyName)
            {
                throw new JsonException();
            }

            propertyName = reader.GetString();
            if (propertyName != "NumberOfColumns")
            {
                throw new JsonException();
            }

            reader.Read();
            if (reader.TokenType != JsonTokenType.Number)
            {
                throw new JsonException();
            }

            int numberOfColumns = reader.GetInt32();

            #endregion

            MatrixImplementor <double> matrixImplementor;

            switch (storageScheme)
            {
            case StorageScheme.Dense:
            {
                matrixImplementor =
                    new DenseDoubleMatrixImplementor(
                        numberOfRows,
                        numberOfColumns);

                #region Storage

                reader.Read();
                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                propertyName = reader.GetString();
                if (propertyName != "Storage")
                {
                    throw new JsonException();
                }

                reader.Read();
                if (reader.TokenType != JsonTokenType.StartArray)
                {
                    throw new JsonException();
                }

                for (int i = 0; i < matrixImplementor.Count; i++)
                {
                    reader.Read();
                    if (reader.TokenType != JsonTokenType.Number)
                    {
                        throw new JsonException();
                    }

                    matrixImplementor[i] = reader.GetDouble();
                }

                reader.Read();
                if (reader.TokenType != JsonTokenType.EndArray)
                {
                    throw new JsonException();
                }

                #endregion
            }
            break;

            case StorageScheme.CompressedRow:
            {
                #region Capacity

                reader.Read();
                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                propertyName = reader.GetString();
                if (propertyName != "Capacity")
                {
                    throw new JsonException();
                }

                reader.Read();
                if (reader.TokenType != JsonTokenType.Number)
                {
                    throw new JsonException();
                }

                int capacity = reader.GetInt32();

                #endregion

                var sparseMatrixImplementor =
                    new SparseCsr3DoubleMatrixImplementor(
                        numberOfRows,
                        numberOfColumns,
                        capacity);

                #region Values

                reader.Read();
                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                propertyName = reader.GetString();
                if (propertyName != "Values")
                {
                    throw new JsonException();
                }

                reader.Read();
                if (reader.TokenType != JsonTokenType.StartArray)
                {
                    throw new JsonException();
                }

                for (int i = 0; i < capacity; i++)
                {
                    reader.Read();
                    if (reader.TokenType != JsonTokenType.Number)
                    {
                        throw new JsonException();
                    }

                    sparseMatrixImplementor.values[i] = reader.GetDouble();
                }

                reader.Read();
                if (reader.TokenType != JsonTokenType.EndArray)
                {
                    throw new JsonException();
                }

                #endregion

                #region Columns

                reader.Read();
                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                propertyName = reader.GetString();
                if (propertyName != "Columns")
                {
                    throw new JsonException();
                }

                reader.Read();
                if (reader.TokenType != JsonTokenType.StartArray)
                {
                    throw new JsonException();
                }

                for (int i = 0; i < capacity; i++)
                {
                    reader.Read();
                    if (reader.TokenType != JsonTokenType.Number)
                    {
                        throw new JsonException();
                    }

                    sparseMatrixImplementor.columns[i] = reader.GetInt32();
                }

                reader.Read();
                if (reader.TokenType != JsonTokenType.EndArray)
                {
                    throw new JsonException();
                }

                #endregion

                #region RowIndex

                reader.Read();
                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new JsonException();
                }

                propertyName = reader.GetString();
                if (propertyName != "RowIndex")
                {
                    throw new JsonException();
                }

                reader.Read();
                if (reader.TokenType != JsonTokenType.StartArray)
                {
                    throw new JsonException();
                }

                for (int i = 0; i < numberOfRows + 1; i++)
                {
                    reader.Read();
                    if (reader.TokenType != JsonTokenType.Number)
                    {
                        throw new JsonException();
                    }

                    sparseMatrixImplementor.rowIndex[i] = reader.GetInt32();
                }

                reader.Read();
                if (reader.TokenType != JsonTokenType.EndArray)
                {
                    throw new JsonException();
                }

                #endregion

                matrixImplementor = sparseMatrixImplementor;
            }
            break;

            default:
                throw new JsonException();
            }

            reader.Read();
            if (reader.TokenType == JsonTokenType.EndObject)
            {
                return(matrixImplementor);
            }

            throw new JsonException();
        }