示例#1
0
        public void Dsymv(Boolean isUpperTriangular, double alpha, DoubleMatrix2D A, DoubleMatrix1D x, double beta, DoubleMatrix1D y)
        {
            if (isUpperTriangular)
            {
                A = A.ViewDice();
            }
            Property.DEFAULT.CheckSquare(A);
            int size = A.Rows;

            if (size != x.Size || size != y.Size)
            {
                throw new ArgumentException(A.ToStringShort() + ", " + x.ToStringShort() + ", " + y.ToStringShort());
            }
            DoubleMatrix1D tmp = x.Like();

            for (int i = 0; i < size; i++)
            {
                double sum = 0;
                for (int j = 0; j <= i; j++)
                {
                    sum += A[i, j] * x[j];
                }
                for (int j = i + 1; j < size; j++)
                {
                    sum += A[j, i] * x[j];
                }
                tmp[i] = alpha * sum + beta * y[i];
            }
            y.Assign(tmp);
        }
示例#2
0
        public void Dtrmv(Boolean isUpperTriangular, Boolean transposeA, Boolean isUnitTriangular, DoubleMatrix2D A, DoubleMatrix1D x)
        {
            if (transposeA)
            {
                A = A.ViewDice();
                isUpperTriangular = !isUpperTriangular;
            }

            Property.DEFAULT.CheckSquare(A);
            int size = A.Rows;

            if (size != x.Size)
            {
                throw new ArgumentException(A.ToStringShort() + ", " + x.ToStringShort());
            }

            DoubleMatrix1D b = x.Like();
            DoubleMatrix1D y = x.Like();

            if (isUnitTriangular)
            {
                y.Assign(1);
            }
            else
            {
                for (int i = 0; i < size; i++)
                {
                    y[i] = A[i, i];
                }
            }

            for (int i = 0; i < size; i++)
            {
                double sum = 0;
                if (!isUpperTriangular)
                {
                    for (int j = 0; j < i; j++)
                    {
                        sum += A[i, j] * x[j];
                    }
                    sum += y[i] * x[i];
                }
                else
                {
                    sum += y[i] * x[i];
                    for (int j = i + 1; j < size; j++)
                    {
                        sum += A[i, j] * x[j];
                    }
                }
                b[i] = sum;
            }
            x.Assign(b);
        }
        /// <summary>
        /// Linear algebraic matrix-vector multiplication; <i>z = A * y</i>.
        /// <i>z[i] = alpha*Sum(A[i,j] * y[j]) + beta*z[i], i=0..A.rows()-1, j=0..y.Count-1</i>.
        /// Where <i>A == this</i>.
        /// </summary>
        /// <param name="y">the source vector.</param>
        /// <param name="z">the vector where results are to be stored.</param>
        /// <param name="nonZeroIndexes"></param>
        /// <param name="allRows"></param>
        /// <param name="alpha"></param>
        /// <param name="beta"></param>
        /// <exception cref="ArgumentException">if <i>A.columns() != y.Count || A.rows() > z.Count)</i>.</exception>
        protected void ZMult(DoubleMatrix1D y, DoubleMatrix1D z, IntArrayList nonZeroIndexes, DoubleMatrix1D[] allRows, double alpha, double beta)
        {
            if (Columns != y.Size || Rows > z.Size)
            {
                throw new ArgumentException(String.Format(Cern.LocalizedResources.Instance().Exception_IncompatibleArgs, ToStringShort(), y.ToStringShort(), z.ToStringShort()));
            }

            z.Assign(Cern.Jet.Math.Functions.DoubleFunctions.Mult(beta / alpha));
            for (int i = indexes.Length; --i >= 0;)
            {
                if (indexes[i] != null)
                {
                    for (int k = indexes[i].Count; --k >= 0;)
                    {
                        int    j     = indexes[i][k];
                        double value = values[i][k];
                        z[i] = z[i] + value * y[j];
                    }
                }
            }

            z.Assign(Cern.Jet.Math.Functions.DoubleFunctions.Mult(alpha));
        }
        public override DoubleMatrix1D ZMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, Boolean transposeA)
        {
            int m = Rows;
            int n = Columns;

            if (transposeA)
            {
                m = Columns;
                n = Rows;
            }

            Boolean ignore = (z == null);

            if (z == null)
            {
                z = new DenseDoubleMatrix1D(m);
            }

            if (!(!this.IsView && y is DenseDoubleMatrix1D && z is DenseDoubleMatrix1D))
            {
                return(base.ZMult(y, z, alpha, beta, transposeA));
            }

            if (n != y.Size || m > z.Size)
            {
                throw new ArgumentException(String.Format(Cern.LocalizedResources.Instance().Exception_IncompatibleArgs, ((transposeA ? ViewDice() : this).ToStringShort()), y.ToStringShort(), z.ToStringShort()));
            }

            if (!ignore)
            {
                z.Assign(F1.Mult(beta / alpha));
            }

            DenseDoubleMatrix1D zz = (DenseDoubleMatrix1D)z;

            double[] zElements = zz.Elements;
            int      zStride   = zz.Stride;
            int      zi        = z.Index(0);

            DenseDoubleMatrix1D yy = (DenseDoubleMatrix1D)y;

            double[] yElements = yy.Elements;
            int      yStride   = yy.Stride;
            int      yi        = y.Index(0);

            if (yElements == null || zElements == null)
            {
                throw new NullReferenceException();
            }

            ForEachNonZero(
                new Cern.Colt.Function.IntIntDoubleFunction((i, j, value) =>
            {
                if (transposeA)
                {
                    int tmp = i; i = j; j = tmp;
                }
                zElements[zi + zStride * i] += value * yElements[yi + yStride * j];
                //z.setQuick(row,z.getQuick(row) + value * y.getQuick(column));
                //Console.WriteLine("["+i+","+j+"]-->"+value);
                return(value);
            }
                                                            ));

            if (alpha != 1)
            {
                z.Assign(F1.Mult(alpha));
            }
            return(z);
        }
        public override DoubleMatrix1D ZMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, Boolean transposeA)
        {
            int m = Rows;
            int n = Columns;

            if (transposeA)
            {
                m = Columns;
                n = Rows;
            }

            Boolean ignore = (z == null || !transposeA);

            if (z == null)
            {
                z = new DenseDoubleMatrix1D(m);
            }

            if (!(y is DenseDoubleMatrix1D && z is DenseDoubleMatrix1D))
            {
                return(base.ZMult(y, z, alpha, beta, transposeA));
            }

            if (n != y.Size || m > z.Size)
            {
                throw new ArgumentException("Incompatible args: " + ((transposeA ? ViewDice() : this).ToStringShort()) + ", " + y.ToStringShort() + ", " + z.ToStringShort());
            }

            DenseDoubleMatrix1D zz = (DenseDoubleMatrix1D)z;

            double[] zElements = zz.Elements;
            int      zStride   = zz.Stride;
            int      zi        = z.Index(0);

            DenseDoubleMatrix1D yy = (DenseDoubleMatrix1D)y;

            double[] yElements = yy.Elements;
            int      yStride   = yy.Stride;
            int      yi        = y.Index(0);

            if (yElements == null || zElements == null)
            {
                throw new NullReferenceException();
            }

            /*
             * forEachNonZero(
             *  new Cern.Colt.Function.IntIntDoubleFunction() {
             *      public double apply(int i, int j, double value) {
             *          zElements[zi + zStride*i] += value * yElements[yi + yStride*j];
             *          //z.SetQuick(row,z.getQuick(row) + value * y.getQuick(column));
             *          //Console.WriteLine("["+i+","+j+"]-->"+value);
             *          return value;
             *      }
             *  }
             * );
             */


            int[]    idx  = Indexes.ToArray();
            double[] vals = Values.ToArray();
            int      s    = Starts.Length - 1;

            if (!transposeA)
            {
                for (int i = 0; i < s; i++)
                {
                    int    high = Starts[i + 1];
                    double sum  = 0;
                    for (int k = Starts[i]; k < high; k++)
                    {
                        int j = idx[k];
                        sum += vals[k] * yElements[yi + yStride * j];
                    }
                    zElements[zi] = alpha * sum + beta * zElements[zi];
                    zi           += zStride;
                }
            }
            else
            {
                if (!ignore)
                {
                    z.Assign(F1.Mult(beta));
                }
                for (int i = 0; i < s; i++)
                {
                    int    high  = Starts[i + 1];
                    double yElem = alpha * yElements[yi + yStride * i];
                    for (int k = Starts[i]; k < high; k++)
                    {
                        int j = idx[k];
                        zElements[zi + zStride * j] += vals[k] * yElem;
                    }
                }
            }

            return(z);
        }