Exemplo n.º 1
0
        /// <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));
        }
Exemplo n.º 2
0
        /// <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));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Performs the following operation for 0 &lt;= i &lt; this.<see cref="Length"/>:
 /// this[i] = <paramref name="thisCoefficient"/> * this[i] + <paramref name="otherCoefficient"/> *
 /// <paramref name="otherVector"/>[i]. The resulting vector overwrites the entries of this <see cref="Vector"/> instance.
 /// </summary>
 /// <param name="thisCoefficient">A scalar that multiplies each entry of this <see cref="Vector"/>.</param>
 /// <param name="otherVector">A vector with the same <see cref="Length"/> as this <see cref="Vector"/> instance.</param>
 /// <param name="otherCoefficient">A scalar that multiplies each entry of <paramref name="otherVector"/>.</param>
 /// <exception cref="NonMatchingDimensionsException">Thrown if <paramref name="otherVector"/> has different
 ///     <see cref="Length"/> than this.</exception>
 public void LinearCombinationIntoThis(double thisCoefficient, Vector otherVector, double otherCoefficient)
 {
     Preconditions.CheckVectorDimensions(this, otherVector);
     if (thisCoefficient == 1.0)
     {
         Blas.Daxpy(Length, otherCoefficient, otherVector.data, 0, 1, this.data, 0, 1);
     }
     else
     {
         BlasExtensions.Daxpby(Length, otherCoefficient, otherVector.data, 0, 1, thisCoefficient, this.data, 0, 1);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Performs the following operation for 0 &lt;= j &lt; <see cref="Order"/>, 0 &lt;= i &lt;= j:
 /// this[i, j] = <paramref name="thisCoefficient"/> * this[i, j]
 ///     + <paramref name="otherCoefficient"/> * <paramref name="otherMatrix"/>[i, j].
 /// The resulting matrix overwrites the entries of this <see cref="TriangularUpper"/> instance.
 /// </summary>
 /// <param name="thisCoefficient">A scalar that multiplies each entry of this <see cref="TriangularUpper"/>.</param>
 /// <param name="otherMatrix">A matrix with the same <see cref="Order"/> as this <see cref="TriangularUpper"/>
 ///     instance.</param>
 /// <param name="otherCoefficient">A scalar that multiplies each entry of <paramref name="otherMatrix"/>.</param>
 /// <exception cref="NonMatchingDimensionsException">Thrown if <paramref name="otherMatrix"/> has different
 ///     <see cref="Order"/> than this instance.</exception>
 public void LinearCombinationIntoThis(double thisCoefficient, TriangularUpper otherMatrix, double otherCoefficient)
 {
     Preconditions.CheckSameMatrixDimensions(this, otherMatrix);
     if (thisCoefficient == 1.0)
     {
         Blas.Daxpy(data.Length, otherCoefficient, otherMatrix.data, 0, 1, this.data, 0, 1);
     }
     else
     {
         BlasExtensions.Daxpby(data.Length, otherCoefficient, otherMatrix.data, 0, 1, thisCoefficient, this.data, 0, 1);
     }
 }
Exemplo n.º 5
0
 public void LinearCombinationIntoThis(double thisCoefficient, SymmetricMatrix otherMatrix, double otherCoefficient)
 {
     Preconditions.CheckSameMatrixDimensions(this, otherMatrix);
     if (thisCoefficient == 1.0)
     {
         Blas.Daxpy(data.Length, otherCoefficient, otherMatrix.data, 0, 1, this.data, 0, 1);
     }
     else
     {
         BlasExtensions.Daxpby(data.Length, otherCoefficient, otherMatrix.data, 0, 1, thisCoefficient, this.data, 0, 1);
     }
     this.Definiteness = DefiniteProperty.Unknown;
 }
Exemplo n.º 6
0
 /// <summary>
 /// Performs the following operation for 0 &lt;= i &lt; <see cref="NumRows"/>, 0 &lt;= j &lt; <see cref="NumColumns"/>:
 /// this[i, j] = <paramref name="thisCoefficient"/> * this[i, j]
 ///     + <paramref name="otherCoefficient"/> * <paramref name="otherMatrix"/>[i, j].
 /// The resulting matrix overwrites the entries of this <see cref="CscMatrix"/> instance.
 /// </summary>
 /// <param name="thisCoefficient">A scalar that multiplies each entry of this <see cref="Matrix"/>.</param>
 /// <param name="otherMatrix">A matrix with the same indexing arrays as this <see cref="CscMatrix"/> instance.</param>
 /// <param name="otherCoefficient">A scalar that multiplies each entry of <paramref name="otherMatrix"/>.</param>
 /// <exception cref="SparsityPatternModifiedException">Thrown if <paramref name="otherMatrix"/> has different
 ///     indexing arrays than this instance.</exception>
 public void LinearCombinationIntoThis(double thisCoefficient, CscMatrix otherMatrix, double otherCoefficient)
 {
     //Preconditions.CheckSameMatrixDimensions(this, other); // no need if the indexing arrays are the same
     if (!HasSameIndexer(otherMatrix))
     {
         throw new SparsityPatternModifiedException("Only allowed if the indexing arrays are the same");
     }
     if (thisCoefficient == 1.0)
     {
         Blas.Daxpy(values.Length, otherCoefficient, otherMatrix.values, 0, 1, this.values, 0, 1);
     }
     else
     {
         BlasExtensions.Daxpby(values.Length, otherCoefficient, otherMatrix.values, 0, 1, thisCoefficient, this.values, 0, 1);
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// See <see cref="IVector.LinearCombinationIntoThis(double, IVectorView, double)"/>
 /// </summary>
 public void LinearCombinationIntoThis(double thisCoefficient, IVectorView otherVector, double otherCoefficient)
 {
     Preconditions.CheckVectorDimensions(this, otherVector);
     if ((otherVector is SparseVector otherSparse) && HasSameIndexer(otherSparse))
     {
         if (thisCoefficient == 1.0)
         {
             Blas.Daxpy(values.Length, otherCoefficient, otherSparse.values, 0, 1, this.values, 0, 1);
         }
         else
         {
             BlasExtensions.Daxpby(values.Length, otherCoefficient, otherSparse.values, 0, 1,
                                   thisCoefficient, this.values, 0, 1);
         }
     }
     throw new SparsityPatternModifiedException(
               "This operation is legal only if the other vector has the same sparsity pattern");
 }
Exemplo n.º 8
0
 /// <summary>
 /// Performs the following operation for 0 &lt;= i &lt; this.<see cref="Length"/>:
 /// result[i] = <paramref name="thisCoefficient"/> * this[i] + <paramref name="otherCoefficient"/> *
 /// <paramref name="otherVector"/>[i]. The resulting vector is written to a new <see cref="Vector"/> and then returned.
 /// </summary>
 /// <param name="thisCoefficient">A scalar that multiplies each entry of this <see cref="Vector"/>.</param>
 /// <param name="otherVector">A vector with the same <see cref="Length"/> as this <see cref="Vector"/> instance.</param>
 /// <param name="otherCoefficient">A scalar that multiplies each entry of <paramref name="otherVector"/>.</param>
 /// <exception cref="NonMatchingDimensionsException">Thrown if <paramref name="otherVector"/> has different
 ///     <see cref="Length"/> than this.</exception>
 public Vector LinearCombination(double thisCoefficient, Vector otherVector, double otherCoefficient)
 {
     Preconditions.CheckVectorDimensions(this, otherVector);
     //TODO: Perhaps this should be done using mkl_malloc and BLAS copy.
     double[] result = new double[data.Length];
     if (thisCoefficient == 1.0)
     {
         Array.Copy(data, result, data.Length);
         Blas.Daxpy(Length, otherCoefficient, otherVector.data, 0, 1, result, 0, 1);
     }
     else if (otherCoefficient == 1.0)
     {
         Array.Copy(otherVector.data, result, data.Length);
         Blas.Daxpy(data.Length, thisCoefficient, this.data, 0, 1, result, 0, 1);
     }
     else
     {
         Array.Copy(data, result, data.Length);
         BlasExtensions.Daxpby(Length, otherCoefficient, otherVector.data, 0, 1, thisCoefficient, result, 0, 1);
     }
     return(new Vector(result));
 }
Exemplo n.º 9
0
 public SymmetricMatrix LinearCombination(double thisCoefficient, SymmetricMatrix otherMatrix, double otherCoefficient)
 {
     Preconditions.CheckSameMatrixDimensions(this, otherMatrix);
     //TODO: Perhaps this should be done using mkl_malloc and BLAS copy.
     double[] result = new double[data.Length];
     if (thisCoefficient == 1.0)
     {
         Array.Copy(this.data, result, data.Length);
         Blas.Daxpy(data.Length, otherCoefficient, otherMatrix.data, 0, 1, result, 0, 1);
     }
     else if (otherCoefficient == 1.0)
     {
         Array.Copy(otherMatrix.data, result, data.Length);
         Blas.Daxpy(data.Length, thisCoefficient, this.data, 0, 1, result, 0, 1);
     }
     else
     {
         Array.Copy(this.data, result, data.Length);
         BlasExtensions.Daxpby(data.Length, otherCoefficient, otherMatrix.data, 0, 1, thisCoefficient, result, 0, 1);
     }
     return(new SymmetricMatrix(result, NumColumns, DefiniteProperty.Unknown));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Performs the following operation for 0 &lt;= j &lt; <see cref="Order"/>, 0 &lt;= i &lt;= j:
 /// result[i, j] = <paramref name="thisCoefficient"/> * this[i, j]
 ///     + <paramref name="otherCoefficient"/> * <paramref name="otherMatrix"/>[i, j].
 /// The resulting matrix is written to a new <see cref="TriangularUpper"/> and then returned.
 /// </summary>
 /// <param name="thisCoefficient">A scalar that multiplies each entry of this <see cref="TriangularUpper"/>.</param>
 /// <param name="otherMatrix">A matrix with the same <see cref="Order"/> as this <see cref="TriangularUpper"/>
 ///     instance.</param>
 /// <param name="otherCoefficient">A scalar that multiplies each entry of <paramref name="otherMatrix"/>.</param>
 /// <exception cref="NonMatchingDimensionsException">Thrown if <paramref name="otherMatrix"/> has different
 ///     <see cref="Order"/> than this instance.</exception>
 public TriangularUpper LinearCombination(double thisCoefficient, TriangularUpper otherMatrix, double otherCoefficient)
 {
     Preconditions.CheckSameMatrixDimensions(this, otherMatrix);
     //TODO: Perhaps this should be done using mkl_malloc and BLAS copy.
     double[] result = new double[data.Length];
     if (thisCoefficient == 1.0)
     {
         Array.Copy(this.data, result, data.Length);
         Blas.Daxpy(data.Length, otherCoefficient, otherMatrix.data, 0, 1, result, 0, 1);
     }
     else if (otherCoefficient == 1.0)
     {
         Array.Copy(otherMatrix.data, result, data.Length);
         Blas.Daxpy(data.Length, thisCoefficient, this.data, 0, 1, result, 0, 1);
     }
     else
     {
         Array.Copy(this.data, result, data.Length);
         BlasExtensions.Daxpby(data.Length, otherCoefficient, otherMatrix.data, 0, 1, thisCoefficient, result, 0, 1);
     }
     return(new TriangularUpper(result, NumColumns));
 }