/// <summary> /// See <see cref="IVectorView.LinearCombination(double, IVectorView, double)"/>. /// </summary> public IVector LinearCombination(double thisCoefficient, IVectorView otherVector, double otherCoefficient) { Preconditions.CheckVectorDimensions(this, otherVector); if (otherVector is SparseVector otherSparse) // In case both matrices have the exact same index arrays { if (HasSameIndexer(otherSparse)) { // Do not copy the index arrays, since they are already spread around. TODO: is this a good idea? double[] result = new double[this.values.Length]; if (thisCoefficient == 1.0) { Array.Copy(this.values, result, this.values.Length); Blas.Daxpy(values.Length, otherCoefficient, otherSparse.values, 0, 1, result, 0, 1); } else if (otherCoefficient == 1.0) { Array.Copy(otherSparse.values, result, values.Length); Blas.Daxpy(values.Length, thisCoefficient, this.values, 0, 1, result, 0, 1); } else { Array.Copy(this.values, result, this.values.Length); BlasExtensions.Daxpby(values.Length, otherCoefficient, otherSparse.values, 0, 1, thisCoefficient, result, 0, 1); } return(new SparseVector(Length, result, indices)); } } // All entries must be processed. TODO: optimizations may be possible (e.g. only access the nnz in this vector) return(DenseStrategies.LinearCombination(this, thisCoefficient, otherVector, otherCoefficient)); }
/// <summary> /// See <see cref="IMatrixView.LinearCombination(double, IMatrixView, double)"/>. /// </summary> public IMatrix LinearCombination(double thisCoefficient, IMatrixView otherMatrix, double otherCoefficient) { if (otherMatrix is CscMatrix otherCSC) // In case both matrices have the exact same index arrays { if (HasSameIndexer(otherCSC)) { // Do not copy the index arrays, since they are already spread around. TODO: is this a good idea? double[] resultValues = new double[values.Length]; if (thisCoefficient == 1.0) { Array.Copy(this.values, resultValues, values.Length); Blas.Daxpy(values.Length, otherCoefficient, otherCSC.values, 0, 1, this.values, 0, 1); } else if (otherCoefficient == 1.0) { Array.Copy(otherCSC.values, resultValues, values.Length); Blas.Daxpy(values.Length, thisCoefficient, this.values, 0, 1, resultValues, 0, 1); } else { Array.Copy(this.values, resultValues, values.Length); BlasExtensions.Daxpby(values.Length, otherCoefficient, otherCSC.values, 0, 1, thisCoefficient, resultValues, 0, 1); } return(new CscMatrix(NumRows, NumColumns, resultValues, this.rowIndices, this.colOffsets)); } } // All entries must be processed. TODO: optimizations may be possible (e.g. only access the nnz in this matrix) return(DenseStrategies.LinearCombination(this, thisCoefficient, otherMatrix, otherCoefficient)); }
/// <summary> /// See <see cref="IEntrywiseOperableView2D{TMatrixIn, TMatrixOut}.DoEntrywise(TMatrixIn, Func{double, double, double})"/>. /// </summary> public IMatrix DoEntrywise(IMatrixView other, Func <double, double, double> binaryOperation) { if (other is SymmetricMatrix casted) { return(DoEntrywise(casted, binaryOperation)); } else { return(DenseStrategies.DoEntrywise(this, other, binaryOperation)); //TODO: optimize this } }
private static void TestGetColumn() { var matrix = TriangularLower.CreateFromArray(LowerInvertible10by10.Matrix); for (int j = 0; j < LowerInvertible10by10.Order; ++j) { Vector colExpected = DenseStrategies.GetColumn(matrix, j); Vector colComputed = matrix.GetColumn(j); comparer.AssertEqual(colExpected, colComputed); } }
private static void TestGetRow() { var matrix = Matrix.CreateFromArray(RectangularFullRank10by5.Matrix); for (int i = 0; i < RectangularFullRank10by5.NumRows; ++i) { Vector rowExpected = DenseStrategies.GetRow(matrix, i); Vector rowComputed = matrix.GetRow(i); comparer.AssertEqual(rowExpected, rowComputed); } }
private static void TestGetColumn() { var matrix = Matrix.CreateFromArray(RectangularFullRank10by5.Matrix); for (int j = 0; j < RectangularFullRank10by5.NumCols; ++j) { Vector colExpected = DenseStrategies.GetColumn(matrix, j); Vector colComputed = matrix.GetColumn(j); comparer.AssertEqual(colExpected, colComputed); } }
private static void TestGetRow() { var matrix = SymmetricMatrix.CreateFromArray(SymmPosDef10by10.Matrix); for (int i = 0; i < SymmPosDef10by10.Order; ++i) { Vector rowExpected = DenseStrategies.GetRow(matrix, i); Vector rowComputed = matrix.GetRow(i); comparer.AssertEqual(rowExpected, rowComputed); } }
private static void TestGetColumn() { var matrix = SymmetricMatrix.CreateFromArray(SymmPosDef10by10.Matrix); for (int j = 0; j < SymmPosDef10by10.Order; ++j) { Vector colExpected = DenseStrategies.GetColumn(matrix, j); Vector colComputed = matrix.GetColumn(j); comparer.AssertEqual(colExpected, colComputed); } }
private static void TestGetRow() { var matrix = TriangularLower.CreateFromArray(LowerInvertible10by10.Matrix); for (int i = 0; i < LowerInvertible10by10.Order; ++i) { Vector rowExpected = DenseStrategies.GetRow(matrix, i); Vector rowComputed = matrix.GetRow(i); comparer.AssertEqual(rowExpected, rowComputed); } }
private static void TestGetColumn() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); for (int j = 0; j < SparsePosDef10by10.Order; ++j) { Vector colExpected = DenseStrategies.GetColumn(matrix, j); Vector colComputed = matrix.GetColumn(j); comparer.AssertEqual(colExpected, colComputed); } }
private static void TestGetRow() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); for (int i = 0; i < SparsePosDef10by10.Order; ++i) { Vector rowExpected = DenseStrategies.GetRow(matrix, i); Vector rowComputed = matrix.GetRow(i); comparer.AssertEqual(rowExpected, rowComputed); } }
private static void TestGetRow() { var matrix = CscMatrix.CreateFromArrays(SparseRectangular10by5.NumRows, SparseRectangular10by5.NumCols, SparseRectangular10by5.CscValues, SparseRectangular10by5.CscRowIndices, SparseRectangular10by5.CscColOffsets, true); for (int i = 0; i < SparseRectangular10by5.NumRows; ++i) { Vector rowExpected = DenseStrategies.GetRow(matrix, i); Vector rowComputed = matrix.GetRow(i); comparer.AssertEqual(rowExpected, rowComputed); } }
private static void TestGetColumn() { var matrix = CscMatrix.CreateFromArrays(SparseRectangular10by5.NumRows, SparseRectangular10by5.NumCols, SparseRectangular10by5.CscValues, SparseRectangular10by5.CscRowIndices, SparseRectangular10by5.CscColOffsets, true); for (int j = 0; j < SparseRectangular10by5.NumCols; ++j) { Vector colExpected = DenseStrategies.GetColumn(matrix, j); Vector colComputed = matrix.GetColumn(j); comparer.AssertEqual(colExpected, colComputed); } }
/// <summary> /// See <see cref="IEntrywiseOperableView2D{TMatrixIn, TMatrixOut}.DoEntrywise(TMatrixIn, Func{double, double, double})"/>. /// </summary> public IMatrix DoEntrywise(IMatrixView other, Func <double, double, double> binaryOperation) { if (other is CscMatrix otherCSC) // In case both matrices have the exact same index arrays { if (HasSameIndexer(otherCSC)) { // Do not copy the index arrays, since they are already spread around. TODO: is this a good idea? double[] resultValues = new double[values.Length]; for (int i = 0; i < values.Length; ++i) { resultValues[i] = binaryOperation(this.values[i], otherCSC.values[i]); } return(new CscMatrix(NumRows, NumColumns, resultValues, rowIndices, colOffsets)); } } // All entries must be processed. TODO: optimizations may be possible (e.g. only access the nnz in this matrix) return(DenseStrategies.DoEntrywise(this, other, binaryOperation)); }
/// <summary> /// See <see cref="IEntrywiseOperableView1D{TVectorIn, TVectorOut}.DoEntrywise(TVectorIn, Func{double, double, double})"/>. /// </summary> public IVector DoEntrywise(IVectorView otherVector, Func <double, double, double> binaryOperation) { Preconditions.CheckVectorDimensions(this, otherVector); if (otherVector is SparseVector otherSparse) // In case both matrices have the exact same index arrays { if (HasSameIndexer(otherSparse)) { // Do not copy the index arrays, since they are already spread around. TODO: is this a good idea? double[] resultValues = new double[values.Length]; for (int i = 0; i < values.Length; ++i) { resultValues[i] = binaryOperation(this.values[i], otherSparse.values[i]); } return(new SparseVector(Length, resultValues, indices)); } } // All entries must be processed. TODO: optimizations may be possible (e.g. only access the nnz in this vector) return(DenseStrategies.DoEntrywise(this, otherVector, binaryOperation)); }
/// <summary> /// See <see cref="IVectorView.Axpy(IVectorView, double)"/>. /// </summary> public IVector Axpy(IVectorView otherVector, double otherCoefficient) { Preconditions.CheckVectorDimensions(this, otherVector); if (otherVector is SparseVector otherSparse) // In case both matrices have the exact same index arrays { if (HasSameIndexer(otherSparse)) { // Do not copy the index arrays, since they are already spread around. TODO: is this a good idea? double[] result = new double[this.values.Length]; Array.Copy(this.values, result, this.values.Length); Blas.Daxpy(values.Length, otherCoefficient, otherSparse.values, 0, 1, result, 0, 1); return(new SparseVector(Length, result, indices)); } } else if (otherVector is Vector otherDense) { double[] result = otherDense.Scale(otherCoefficient).RawData; SparseBlas.Daxpyi(this.indices.Length, 1.0, this.values, this.indices, 0, result, 0); return(Vector.CreateFromArray(result, false)); } // All entries must be processed. TODO: optimizations may be possible (e.g. only access the nnz in this vector) return(DenseStrategies.LinearCombination(this, 1.0, otherVector, otherCoefficient)); }
/// <summary> /// Returns true if this[i] <= <paramref name="tolerance"/> for 0 <= i < this.<see cref="Length"/>. /// Otherwise false is returned. /// </summary> /// <param name="tolerance">The tolerance under which a vector entry is considered to be 0. It can be set to 0, to check /// if the entries are exactly 0.</param> public bool IsZero(double tolerance) => DenseStrategies.IsZero(data, tolerance);
/// <summary> /// Copies the entries of the matrix into a 2-dimensional array. The returned array has length(0) = <see cref="NumRows"/> /// and length(1) = <see cref="NumColumns"/>. /// </summary> public double[,] CopyToArray2D() => DenseStrategies.CopyToArray2D(this);
/// <summary> /// See <see cref="IIndexable2D.Equals(IIndexable2D, double)"/>. /// </summary> public bool Equals(IIndexable2D other, double tolerance = 1E-13) { return(DenseStrategies.AreEqual(this, other, tolerance)); }
/// <summary> /// See <see cref="IVector.AddIntoThisNonContiguouslyFrom(int[], IVectorView)"/> /// </summary> public void AddIntoThisNonContiguouslyFrom(int[] thisIndices, IVectorView otherVector) => DenseStrategies.AddNonContiguouslyFrom(this, thisIndices, otherVector);
/// <summary> /// See <see cref="IVector.CopyNonContiguouslyFrom(IVectorView, int[])"/> /// </summary> public void CopyNonContiguouslyFrom(IVectorView otherVector, int[] otherIndices) => DenseStrategies.CopyNonContiguouslyFrom(this, otherVector, otherIndices);
public Matrix CopyToFullMatrix() => DenseStrategies.CopyToFullMatrix(this);
public IMatrix LinearCombination(double thisCoefficient, IMatrixView otherMatrix, double otherCoefficient) => DenseStrategies.LinearCombination(this, thisCoefficient, otherMatrix, otherCoefficient);
/// <summary> /// See <see cref="IEntrywiseOperableView2D{TMatrixIn, TMatrixOut}.DoEntrywise(TMatrixIn, Func{double, double, double})"/>. /// </summary> public IMatrix DoEntrywise(IMatrixView matrix, Func <double, double, double> binaryOperation) { return(DenseStrategies.DoEntrywise(this, matrix, binaryOperation)); //TODO: this can be optimized. }
/// <summary> /// See <see cref="IEntrywiseOperableView2D{TMatrixIn, TMatrixOut}.DoEntrywise(TMatrixIn, Func{double, double, double})"/>. /// </summary> public IMatrix DoEntrywise(IMatrixView matrix, Func <double, double, double> binaryOperation) { return(DenseStrategies.DoEntrywise(this, matrix, binaryOperation)); }
public Matrix MultiplyRight(IMatrixView other, bool transposeThis = false, bool transposeOther = false) { return(DenseStrategies.Multiply(this, other, transposeThis, transposeOther)); }
/// <summary> /// See <see cref="ISliceable2D.GetSubmatrix(int, int, int, int)"/>. /// </summary> public IMatrix GetSubmatrix(int rowStartInclusive, int rowEndExclusive, int colStartInclusive, int colEndExclusive) => DenseStrategies.GetSubmatrix(this, rowStartInclusive, rowEndExclusive, colStartInclusive, colEndExclusive);
/// <summary> /// See <see cref="ISliceable2D.GetSubmatrix(int[], int[])"/>. /// </summary> public IMatrix GetSubmatrix(int[] rowIndices, int[] colIndices) => DenseStrategies.GetSubmatrix(this, rowIndices, colIndices);
/// <summary> /// See <see cref="IIndexable2D.Equals(IIndexable2D, double)"/>. /// </summary> public bool Equals(IIndexable2D other, double tolerance = 1e-13) => DenseStrategies.AreEqual(this, other, tolerance);
/// <summary> /// See <see cref="IMatrixView.MultiplyLeft(IMatrixView, bool, bool)"/>. /// </summary> public Matrix MultiplyLeft(IMatrixView matrix, bool transposeThis = false, bool transposeOther = false) { return(DenseStrategies.Multiply(matrix, this, transposeOther, transposeThis)); }