Ejemplo n.º 1
0
        /// <summary>
        /// See <see cref="IMatrixView.MultiplyRight(IMatrixView, bool, bool)"/>.
        /// </summary>
        public Matrix MultiplyRight(IMatrixView other, bool transposeThis = false, bool transposeOther = false)
        {
            // TODO: Throwing exceptions when csc is on the left seems attractive.
            if (transposeOther)
            {
                if (transposeThis)
                {
                    Preconditions.CheckMultiplicationDimensions(this.NumRows, other.NumColumns);
                    var result = Matrix.CreateZero(this.NumColumns, other.NumRows);
                    CsrMultiplications.CsrTimesMatrixTrans(this.NumColumns, values, colOffsets, rowIndices, other, result);
                    return(result);
                }
                else
                {
                    Preconditions.CheckMultiplicationDimensions(this.NumColumns, other.NumColumns);
                    var result = Matrix.CreateZero(this.NumRows, other.NumRows);
                    CsrMultiplications.CsrTransTimesMatrixTrans(this.NumColumns, values, colOffsets, rowIndices, other, result);
                    return(result);
                }
            }
            else
            {
                //TODO: perhaps I can use the left multiplications if the other matrix is also transposed
                if (other is Matrix dense)
                {
                    return(MultiplyRight(dense, transposeThis));
                }

                if (transposeThis)
                {
                    Preconditions.CheckMultiplicationDimensions(this.NumRows, other.NumRows);
                    var result = Matrix.CreateZero(this.NumColumns, other.NumColumns);
                    CsrMultiplications.CsrTimesMatrix(this.NumColumns, values, colOffsets, rowIndices, other, result);
                    return(result);
                }
                else
                {
                    Preconditions.CheckMultiplicationDimensions(this.NumColumns, other.NumRows);
                    var result = Matrix.CreateZero(this.NumRows, other.NumColumns);
                    CsrMultiplications.CsrTransTimesMatrix(this.NumColumns, values, colOffsets, rowIndices, other, result);
                    return(result);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// See <see cref="IMatrixView.MultiplyIntoResult(IVectorView, IVector, bool)"/>.
        /// </summary>
        public void MultiplyIntoResult(IVectorView lhsVector, IVector rhsVector, bool transposeThis = false)
        {
            if ((lhsVector is Vector lhsDense) && (rhsVector is Vector rhsDense))
            {
                MultiplyIntoResult(lhsDense, rhsDense, transposeThis);
            }

            if (transposeThis)
            {
                Preconditions.CheckMultiplicationDimensions(NumRows, lhsVector.Length);
                Preconditions.CheckSystemSolutionDimensions(NumColumns, rhsVector.Length);
                CsrMultiplications.CsrTimesVector(NumColumns, values, colOffsets, rowIndices, lhsVector, rhsVector);
            }
            else
            {
                Preconditions.CheckMultiplicationDimensions(NumColumns, lhsVector.Length);
                Preconditions.CheckSystemSolutionDimensions(NumRows, rhsVector.Length);
                CsrMultiplications.CsrTransTimesVector(NumColumns, values, colOffsets, rowIndices, lhsVector, rhsVector);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// See <see cref="IMatrixView.Multiply(IVectorView, bool)"/>.
        /// </summary>
        public IVector Multiply(IVectorView vector, bool transposeThis = false)
        {
            if (vector is Vector dense)
            {
                return(Multiply(dense, transposeThis));
            }

            if (transposeThis)
            {
                var result = new double[NumColumns];
                Preconditions.CheckMultiplicationDimensions(NumRows, vector.Length);
                CsrMultiplications.CsrTimesVector(NumColumns, values, colOffsets, rowIndices, vector, result);
                return(Vector.CreateFromArray(result, false));
            }
            else
            {
                var result = new double[NumRows];
                Preconditions.CheckMultiplicationDimensions(NumColumns, vector.Length);
                CsrMultiplications.CsrTransTimesVector(NumColumns, values, colOffsets, rowIndices, vector, result);
                return(Vector.CreateFromArray(result, false));
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// See <see cref="IMatrixView.MultiplyLeft(IMatrixView, bool, bool)"/>.
 /// </summary>
 public Matrix MultiplyLeft(IMatrixView other, bool transposeThis = false, bool transposeOther = false)
 {
     //TODO: To use BLAS for this too, we must accept row major matrices as output.
     if (transposeOther)
     {
         if (transposeThis)
         {
             Preconditions.CheckMultiplicationDimensions(other.NumRows, this.NumColumns);
             var result = Matrix.CreateZero(other.NumColumns, this.NumRows);
             CsrMultiplications.MatrixTransTimesCsr(this.NumColumns, values, colOffsets, rowIndices, other, result);
             return(result);
         }
         else
         {
             Preconditions.CheckMultiplicationDimensions(other.NumRows, this.NumRows);
             var result = Matrix.CreateZero(other.NumColumns, this.NumColumns);
             CsrMultiplications.MatrixTransTimesCsrTrans(this.NumColumns, values, colOffsets, rowIndices, other, result);
             return(result);
         }
     }
     else
     {
         if (transposeThis)
         {
             Preconditions.CheckMultiplicationDimensions(other.NumColumns, this.NumColumns);
             var result = Matrix.CreateZero(other.NumRows, this.NumRows);
             CsrMultiplications.MatrixTimesCsr(this.NumColumns, values, colOffsets, rowIndices, other, result);
             return(result);
         }
         else
         {
             Preconditions.CheckMultiplicationDimensions(other.NumColumns, this.NumRows);
             var result = Matrix.CreateZero(other.NumRows, this.NumColumns);
             CsrMultiplications.MatrixTimesCsrTrans(this.NumColumns, values, colOffsets, rowIndices, other, result);
             return(result);
         }
     }
 }