Esempio 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));
        }
Esempio 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));
        }
Esempio n. 3
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="otherCoefficient"/> * <paramref name="otherMatrix"/>[i, j] + this[i, j].
 /// The resulting matrix overwrites the entries of this <see cref="CscMatrix"/> instance.
 /// </summary>
 /// <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 AxpyIntoThis(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");
     }
     Blas.Daxpy(values.Length, otherCoefficient, otherMatrix.values, 0, 1, this.values, 0, 1);
 }
Esempio n. 4
0
 public SymmetricMatrix Axpy(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];
     Array.Copy(this.data, result, data.Length);
     Blas.Daxpy(data.Length, otherCoefficient, otherMatrix.data, 0, 1, result, 0, 1);
     return(new SymmetricMatrix(result, NumColumns, DefiniteProperty.Unknown));
 }
Esempio n. 5
0
 /// <summary>
 /// Performs the following operation for 0 &lt;= i &lt; this.<see cref="Length"/>:
 /// result[i] = <paramref name="otherCoefficient"/> * <paramref name="otherVector"/>[i] + this[i].
 /// The resulting vector is written to a new <see cref="Vector"/> and then returned.
 /// </summary>
 /// <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 Axpy(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];
     Array.Copy(data, result, data.Length);
     Blas.Daxpy(Length, otherCoefficient, otherVector.data, 0, 1, result, 0, 1);
     return(new Vector(result));
 }
 /// <summary>
 /// Performs the following operation for 0 &lt;= j &lt; <see cref="Order"/>, 0 &lt;= i &lt;= j:
 /// result[i, j] = <paramref name="otherCoefficient"/> * <paramref name="otherMatrix"/>[i, j] + this[i, j].
 /// The resulting matrix is written to a new <see cref="TriangularUpper"/> and then returned.
 /// </summary>
 /// <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 Axpy(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];
     Array.Copy(this.data, result, data.Length);
     Blas.Daxpy(data.Length, otherCoefficient, otherMatrix.data, 0, 1, result, 0, 1);
     return(new TriangularUpper(result, NumColumns));
 }
Esempio n. 7
0
        /// <summary>
        /// Performs the following operation for all i:
        /// this[i] = <paramref name="otherCoefficient"/> * <paramref name="otherVector"/>[i] + this[i].
        /// Optimized version of <see cref="IVector.DoEntrywise(IVectorView, Func{double, double, double})"/> and
        /// <see cref="IVector.LinearCombination(double, IVectorView, double)"/>. Named after BLAS axpy (y = a*x plus y).
        /// The resulting vector overwrites the entries of this.
        /// </summary>
        /// <param name="otherVector">
        /// A vector with the same <see cref="IIndexable1D.Length"/> as this.
        /// </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="IIndexable1D.Length"/> than this.
        /// </exception>
        /// <exception cref="SparsityPatternModifiedException">
        /// Thrown if an entry this[i] needs to be overwritten, but that is not permitted by the vector storage format.
        /// </exception>
        public void AxpyIntoThis(SparseVector otherVector, double otherCoefficient)
        {
            Preconditions.CheckVectorDimensions(this, otherVector);
            if (!HasSameIndexer(otherVector))
            {
                throw new SparsityPatternModifiedException(
                          "This operation is legal only if the other vector has the same sparsity pattern");
            }

            Blas.Daxpy(values.Length, otherCoefficient, otherVector.values, 0, 1, this.values, 0, 1);
        }
 /// <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);
     }
 }
Esempio n. 9
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);
     }
 }
Esempio n. 10
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;
 }
Esempio n. 11
0
 /// <summary>
 /// Performs the following operation for 0 &lt;= i &lt; <see cref="NumRows"/>, 0 &lt;= j &lt; <see cref="NumColumns"/>:
 /// result[i, j] = <paramref name="otherCoefficient"/> * <paramref name="otherMatrix"/>[i, j] + this[i, j].
 /// The resulting matrix is written to a new <see cref="CscMatrix"/> and then returned.
 /// </summary>
 /// <param name="otherMatrix">A matrix with the same <see cref="NumRows"/> and <see cref="NumColumns"/> as this
 ///     <see cref="CscMatrix"/> 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="NumRows"/> or <see cref="NumColumns"/> than this instance.</exception>
 public CscMatrix Axpy(CscMatrix otherMatrix, double otherCoefficient)
 {
     // Conceptually it is not wrong to so this, even if the indexers are different, but how would I implement it.
     if (!HasSameIndexer(otherMatrix))
     {
         throw new SparsityPatternModifiedException("Only allowed if the indexing arrays are the same");
     }
     //TODO: Perhaps this should be done using mkl_malloc and BLAS copy.
     double[] resultValues = new double[values.Length];
     Array.Copy(this.values, resultValues, values.Length);
     Blas.Daxpy(values.Length, otherCoefficient, otherMatrix.values, 0, 1, resultValues, 0, 1);
     // Do not copy the index arrays, since they are already spread around. TODO: is this a good idea?
     return(new CscMatrix(NumRows, NumColumns, resultValues, this.rowIndices, this.colOffsets));
 }
Esempio n. 12
0
        /// <summary>
        /// See <see cref="IMatrixView.Axpy(IMatrixView, double)"/>.
        /// </summary>
        public IMatrix Axpy(IMatrixView otherMatrix, double otherCoefficient)
        {
            if ((otherMatrix is SymmetricCscMatrix otherCSC) && HaveSameIndexArrays(otherCSC))
            {
                // Unneeded if the indexers are identical, but worth it
                Preconditions.CheckSameMatrixDimensions(this, otherMatrix);

                //TODO: Perhaps this should be done using mkl_malloc and BLAS copy.
                double[] resultValues = new double[values.Length];
                Array.Copy(this.values, resultValues, values.Length);

                Blas.Daxpy(values.Length, otherCoefficient, otherCSC.values, 0, 1, resultValues, 0, 1);

                // Do not copy the index arrays, since they are already spread around. TODO: is this a good idea?
                return(new SymmetricCscMatrix(NumRows, NumNonZerosUpper, resultValues, this.rowIndices, this.colOffsets));
            }
Esempio n. 13
0
 /// <summary>
 /// Performs the operation: this[<paramref name="destinationIdx"/> + i] = this[<paramref name="destinationIdx"/> + i]
 /// + <paramref name="sourceVector"/>[<paramref name="sourceIdx"/> + j], for 0 &lt;= j &lt; <paramref name="length"/>.
 /// </summary>
 /// <param name="length">The number of entries to add.</param>
 /// <param name="destinationIdx">
 /// The index into this <see cref="Vector"/> instance, at which to start adding entries to. Constraints:
 /// 0 &lt;= <paramref name="destinationIdx"/>,
 /// <paramref name="destinationIdx"/> + <paramref name="Length"/> &lt;= this.<see cref="Length"/>.
 /// </param>
 /// <param name="sourceVector">The vector that will be added to a part of this <see cref="Vector"/> instance.</param>
 /// <param name="sourceIdx">
 /// The index into <paramref name="sourceVector"/>, at which to start adding entries from. Constraints:
 /// 0 &lt;= <paramref name="sourceIdx"/>,
 /// <paramref name="sourceIdx"/> + <paramref name="length"/> &lt;= <paramref name="sourceVector"/>.<see cref="Length"/>.
 /// </param>
 /// <exception cref="NonMatchingDimensionsException">
 /// Thrown if <paramref name="destinationIdx"/>, <paramref name="sourceVector"/> or <paramref name="sourceIdx"/>
 /// violate the described constraints.
 /// </exception>
 public void AddSubvectorIntoThis(int destinationIdx, IVectorView sourceVector, int sourceIdx, int length)
 {
     if (destinationIdx + sourceVector.Length > this.Length)
     {
         throw new NonMatchingDimensionsException(
                   "The entries to set exceed this vector's length");
     }
     if (sourceVector is Vector subvectorDense)
     {
         Blas.Daxpy(length, 1.0, subvectorDense.data, sourceIdx, 1, this.data, destinationIdx, 1);
     }
     else
     {
         this.AddSubvectorIntoThis(destinationIdx, sourceVector, 0, sourceVector.Length);
     }
 }
Esempio n. 14
0
        /// <summary>
        /// See <see cref="IVector.CopySubvectorFrom(int, IVectorView, int, int)"/>.
        /// </summary>
        public void AxpySubvectorIntoThis(int destinationIndex, IVectorView sourceVector, double sourceCoefficient,
                                          int sourceIndex, int length)
        {
            Preconditions.CheckSubvectorDimensions(this, destinationIndex, length);
            Preconditions.CheckSubvectorDimensions(sourceVector, sourceIndex, length);

            if (sourceVector is Vector casted)
            {
                Blas.Daxpy(Length, sourceCoefficient, casted.data, sourceIndex, 1, this.data, destinationIndex, 1);
            }
            else
            {
                for (int i = 0; i < length; ++i)
                {
                    data[i + destinationIndex] += sourceCoefficient * sourceVector[i + sourceIndex];
                }
            }
        }
Esempio n. 15
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");
 }
Esempio n. 16
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));
 }
Esempio n. 17
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));
 }
Esempio n. 18
0
        /// <summary>
        /// See <see cref="IVector.AxpySubvectorIntoThis(int, IVectorView, double, int, int)"/>.
        /// </summary>
        public void AxpySubvectorIntoThis(int destinationIndex, IVectorView sourceVector, double sourceCoefficient,
                                          int sourceIndex, int length)
        {
            //TODO: needs testing for off-by-1 bugs and extension to cases where source and destination indices are different.

            Preconditions.CheckSubvectorDimensions(this, destinationIndex, length);
            Preconditions.CheckSubvectorDimensions(sourceVector, sourceIndex, length);

            if ((sourceVector is SparseVector otherSparse) && HasSameIndexer(otherSparse))
            {
                if (destinationIndex != sourceIndex)
                {
                    throw new NotImplementedException();
                }
                int start        = Array.FindIndex(this.indices, x => x >= destinationIndex);
                int end          = Array.FindIndex(this.indices, x => x >= destinationIndex + length);
                int sparseLength = end - start;
                Blas.Daxpy(sparseLength, sourceCoefficient, otherSparse.values, start, 1, this.values, start, 1);
            }
            throw new SparsityPatternModifiedException(
                      "This operation is legal only if the other vector has the same sparsity pattern");
        }
 /// <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));
 }
Esempio n. 20
0
 /// <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));
 }
Esempio n. 21
0
        // markov operation
        public void DoBackward(double t, double[] x, int right, double[] prob)
        {
            //int right;
            //if (t > maxt)
            //{
            //	right = setMaxT(t);
            //}
            //else
            //{
            //	right = PoiDist.getRightBound(lambda * t, epsi);
            //}
            double weight = PoiDist.CompProb(Lambda * t, 0, right, prob);

            Blas.Dcopy(ndim, x, xi);
            Blas.Fill(ndim, x, 0.0);
            Blas.Daxpy(ndim, prob[0], xi, x);
            for (int l = 1; l <= right; l++)
            {
                Blas.Dcopy(ndim, xi, tmp);
                DgemvNoTrans(1.0, tmp, 0.0, xi);
                Blas.Daxpy(ndim, prob[l], xi, x);
            }
            Blas.Dscal(ndim, 1.0 / weight, x);
        }
Esempio n. 22
0
        public void DoSojournForward(double t, double[] f, double[] b, double[] h, int right, double[] prob, double[][] vc)
        {
            //int right;
            //if (t > maxt)
            //{
            //	right = setMaxT(t);
            //}
            //else
            //{
            //	right = PoiDist.getRightBound(lambda * t, epsi);
            //}
            double weight = PoiDist.CompProb(Lambda * t, 0, right + 1, prob);

            // forward and backward
            Blas.Fill(ndim, vc[right + 1], 0.0);
            Blas.Daxpy(ndim, prob[right + 1], b, vc[right + 1]);
            for (int l = right; l >= 1; l--)
            {
                DgemvNoTrans(1.0, vc[l + 1], 0.0, vc[l]);
                Blas.Daxpy(ndim, prob[l], b, vc[l]);
            }
            Blas.Dcopy(ndim, f, xi);
            Blas.Fill(ndim, f, 0.0);
            Blas.Daxpy(ndim, prob[0], xi, f);
            Blas.Fill(ndim * 2, h, 0.0);
            Dger(1.0, xi, vc[1], h);
            for (int l = 1; l <= right; l++)
            {
                Blas.Dcopy(ndim, xi, tmp);
                DgemvTrans(1.0, tmp, 0.0, xi);
                Blas.Daxpy(ndim, prob[l], xi, f);
                Dger(1.0, xi, vc[l + 1], h);
            }
            Blas.Dscal(ndim, 1.0 / weight, f);
            Blas.Dscal(ndim * 2, 1.0 / Lambda / weight, h);
        }
Esempio n. 23
0
        }                             // private constructor for singleton pattern

        #region BLAS Level 1

        /// <summary>
        /// See https://software.intel.com/en-us/mkl-developer-reference-fortran-axpy#E25D8E10-0440-4827-BC58-BC71128EA6EE
        /// </summary>
        public void Daxpy(int n, double alpha, double[] x, int offsetX, int incX, double[] y, int offsetY, int incY)
        => Blas.Daxpy(ref n, ref alpha, ref x[offsetX], ref incX, ref y[offsetY], ref incY);
Esempio n. 24
0
        public double Emstep(SRMData data)
        {
            int dsize = data.Size;

            double[] time = data.Time;
            int[]    num  = data.Fault;
            int[]    type = data.Type;

            int    x;
            double t, llf, tmpv;

            int right = PoiDist.GetRightBound(param.Lambda * NMath.Max(time), epsi);

            double[]   prob = new double[right + 2];
            double[][] vc   = Array2.Create(right + 2, ndim);

            // initialize for estep
            eno = 0.0;
            Blas.Fill(ndim, eb, 0.0);
            Blas.Fill(ndim, eb2, 0.0);
            Blas.Fill(ndim * 2, en, 0.0);

            // backward: compute eb
            Blas.Fill(ndim, vb[0], 1.0);
            Blas.Fill(ndim, vb2[0], 0.0);
            vb2[0][ndim - 1] = param.Rate[ndim - 1];

            llf = 0.0;
            for (int k = 1; k <= dsize; k++)
            {
                t = time[k - 1];              // dat.getTime(k);
                x = num[k - 1];               // dat.getNumber(k);

                Blas.Dcopy(ndim, vb[k - 1], vb[k]);
                cuni.DoBackward(t, vb[k], right, prob);
                if (x != 0)
                {
                    Blas.Dcopy(ndim, vb[k - 1], tmp);
                    Blas.Daxpy(ndim, -1.0, vb[k], tmp);
                    blf[k] = Blas.Ddot(ndim, param.Alpha, tmp);
                    llf   += x * NMath.Log(param.Omega * blf[k]) - NMath.Lgamma(x + 1);

                    eno += x;
                    Blas.Daxpy(ndim, x / blf[k], tmp, eb);
                }
                else
                {
                    blf[k] = 1.0;                     // to avoid NaN
                }

                Blas.Dcopy(ndim, vb2[k - 1], vb2[k]);
                cuni.DoBackward(t, vb2[k], right, prob);
                if (type[k - 1] == 1)               // (dat.getType(k) == 1)
                {
                    blf2[k] = Blas.Ddot(ndim, param.Alpha, vb2[k]);
                    llf    += NMath.Log(param.Omega * blf2[k]);
                    eno    += 1.0;
                    Blas.Daxpy(ndim, 1.0 / blf2[k], vb2[k], eb2);
                }
            }
            barblf = Blas.Ddot(ndim, param.Alpha, vb[dsize]);
            llf   += -param.Omega * (1.0 - barblf);
            Blas.Daxpy(ndim, param.Omega, vb[dsize], eb);

            // compute pi2
            tmpv = 0.0;
            for (int i = 0; i < ndim - 1; i++)
            {
                tmpv  += param.Alpha[i];
                pi2[i] = tmpv / param.Rate[i];
            }
            pi2[ndim - 1] = 1.0 / param.Rate[ndim - 1];

            // sojourn:
            Blas.Fill(ndim, tmp, 0.0);
            Blas.Daxpy(ndim, -num[dsize - 1] / blf[dsize] + param.Omega, pi2, tmp);
            if (type[dsize - 1] == 1)
            {
                Blas.Daxpy(ndim, 1.0 / blf2[dsize], param.Alpha, tmp);
            }
            cuni.DoSojournForward(time[dsize - 1], tmp, vb2[dsize - 1], h0, right, prob, vc);
            Blas.Daxpy(ndim * 2, 1.0, h0, en);
            for (int k = dsize - 1; k >= 1; k--)
            {
                Blas.Daxpy(ndim, num[k] / blf[k + 1] - num[k - 1] / blf[k], pi2, tmp);
                if (type[k - 1] == 1)
                {
                    Blas.Daxpy(ndim, 1.0 / blf2[k], param.Alpha, tmp);
                }
                cuni.DoSojournForward(time[k - 1], tmp, vb2[k - 1], h0, right, prob, vc);
                Blas.Daxpy(ndim * 2, 1.0, h0, en);
            }

            /* concrete algorithm: M-step */
            for (int i = 0; i < ndim - 1; i++)
            {             // <-- not <=ndim!
                ey[i] = param.Rate[i]
                        * (en[2 * i + 1] + eb[i + 1] * pi2[i])
                        / (en[2 * i] + eb[i] * pi2[i]);
            }
            tmpv = en[2 * (ndim - 1)] + eb[ndim - 1] * pi2[ndim - 1];
            double sum = 0.0;

            for (int i = 0; i < ndim; i++)
            {
                eb[i] = param.Alpha[i] * (eb[i] + eb2[i]);
                sum  += eb[i];
            }
            ey[ndim - 1] = sum / tmpv;
            for (int i = 0; i < ndim; i++)
            {
                eb[i] /= sum;
            }
            param.Update(eno + param.Omega * barblf, eb, ey);
            return(llf);
        }
Esempio n. 25
0
 /// <summary>
 /// Performs the following operation for 0 &lt;= i &lt; this.<see cref="Length"/>:
 /// this[i] = <paramref name="otherCoefficient"/> * <paramref name="otherVector"/>[i] + this[i].
 /// The resulting vector overwrites the entries of this <see cref="Vector"/> instance.
 /// </summary>
 /// <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 AxpyIntoThis(Vector otherVector, double otherCoefficient)
 {
     Preconditions.CheckVectorDimensions(this, otherVector);
     Blas.Daxpy(Length, otherCoefficient, otherVector.data, 0, 1, this.data, 0, 1);
 }
 /// <summary>
 /// Performs the following operation for 0 &lt;= j &lt; <see cref="Order"/>, 0 &lt;= i &lt;= j:
 /// this[i, j] = <paramref name="otherCoefficient"/> * <paramref name="otherMatrix"/>[i, j] + this[i, j].
 /// The resulting matrix overwrites the entries of this <see cref="TriangularUpper"/> instance.
 /// </summary>
 /// <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 AxpyIntoThis(TriangularUpper otherMatrix, double otherCoefficient)
 {
     Preconditions.CheckSameMatrixDimensions(this, otherMatrix);
     Blas.Daxpy(data.Length, otherCoefficient, otherMatrix.data, 0, 1, this.data, 0, 1);
 }
Esempio n. 27
0
 public void AxpyIntoThis(SymmetricMatrix otherMatrix, double otherCoefficient)
 {
     Preconditions.CheckSameMatrixDimensions(this, otherMatrix);
     Blas.Daxpy(data.Length, otherCoefficient, otherMatrix.data, 0, 1, this.data, 0, 1);
     this.Definiteness = DefiniteProperty.Unknown;
 }