예제 #1
0
        public void Drot(DoubleMatrix1D x, DoubleMatrix1D y, double c, double s)
        {
            x.CheckSize(y);
            DoubleMatrix1D tmp = x.Copy();

            x.Assign(F1.Mult(c));
            x.Assign(y, F2.PlusMult(s));

            y.Assign(F1.Mult(c));
            y.Assign(tmp, F2.MinusMult(s));
        }
        public DoubleMatrix2D Assign(Cern.Colt.Function.DoubleFunction function, Double multialpha = 1)
        {
            if (Cern.Jet.Math.Functions.EvaluateDoubleFunctionEquality(function, F1.Mult(multialpha)))
            {
                //if (function == F1.Mult()) { // x[i] = mult*x[i]
                //var mult = new Cern.Jet.Math.Mult();
                double alpha = multialpha; //((Cern.Jet.Math.Mult)function).multiplicator;
                if (alpha == 1)
                {
                    return(this);
                }
                if (alpha == 0)
                {
                    return(Assign(0));
                }
                if (double.IsNaN(alpha))
                {
                    return(Assign(alpha));                     // the funny definition of IsNaN()d This should better not happen.
                }
                double[] vals = Values.ToArray();
                for (int j = Values.Count; --j >= 0;)
                {
                    vals[j] *= alpha;
                }

                /*
                 * forEachNonZero(
                 *  new Cern.Colt.Function.IntIntDoubleFunction() {
                 *      public double apply(int i, int j, double value) {
                 *          return function(value);
                 *      }
                 *  }
                 * );
                 */
            }
            else
            {
                base.Assign(function);
            }
            return(this);
        }
        public override DoubleMatrix2D ZMult(DoubleMatrix2D B, DoubleMatrix2D C, double alpha, double beta, Boolean transposeA, Boolean transposeB)
        {
            if (transposeB)
            {
                B = B.ViewDice();
            }
            int m = Rows;
            int n = Columns;

            if (transposeA)
            {
                m = Columns;
                n = Rows;
            }
            int     p      = B.Columns;
            Boolean ignore = (C == null);

            if (C == null)
            {
                C = new DenseDoubleMatrix2D(m, p);
            }

            if (B.Rows != n)
            {
                throw new ArgumentException(String.Format(Cern.LocalizedResources.Instance().Exception_Matrix2DInnerDimensionMustAgree, ToStringShort(), (transposeB ? B.ViewDice() : B).ToStringShort()));
            }
            if (C.Rows != m || C.Columns != p)
            {
                throw new ArgumentException(String.Format(Cern.LocalizedResources.Instance().Exception_IncompatibleResultMatrix, ToStringShort(), (transposeB ? B.ViewDice() : B).ToStringShort(), C.ToStringShort()));
            }
            if (this == C || B == C)
            {
                throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_MatricesMustNotBeIdentical);
            }

            if (!ignore)
            {
                C.Assign(F1.Mult(beta));
            }

            // cache views
            DoubleMatrix1D[] Brows = new DoubleMatrix1D[n];
            for (int i = n; --i >= 0;)
            {
                Brows[i] = B.ViewRow(i);
            }
            DoubleMatrix1D[] Crows = new DoubleMatrix1D[m];
            for (int i = m; --i >= 0;)
            {
                Crows[i] = C.ViewRow(i);
            }


            ForEachNonZero(
                new Cern.Colt.Function.IntIntDoubleFunction((i, j, value) =>
            {
                var fun = F2.PlusMult(value * alpha);
                //fun.multiplicator = value * alpha;
                if (!transposeA)
                {
                    Crows[i].Assign(Brows[j], fun);
                }
                else
                {
                    Crows[j].Assign(Brows[i], fun);
                }
                return(value);
            }
                                                            ));

            return(C);
        }
        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);
        }
예제 #5
0
 public void Dscal(double alpha, DoubleMatrix2D A)
 {
     A.Assign(F1.Mult(alpha));
 }
예제 #6
0
 public void Dscal(double alpha, DoubleMatrix1D x)
 {
     x.Assign(F1.Mult(alpha));
 }
        public override DoubleMatrix2D ZMult(DoubleMatrix2D B, DoubleMatrix2D C, double alpha, double beta, Boolean transposeA, Boolean transposeB)
        {
            if (transposeB)
            {
                B = B.ViewDice();
            }
            int m = Rows;
            int n = Columns;

            if (transposeA)
            {
                m = Columns;
                n = Rows;
            }
            int     p      = B.Columns;
            Boolean ignore = (C == null);

            if (C == null)
            {
                C = new DenseDoubleMatrix2D(m, p);
            }

            if (B.Rows != n)
            {
                throw new ArgumentException("Matrix2D inner dimensions must agree:" + ToStringShort() + ", " + (transposeB ? B.ViewDice() : B).ToStringShort());
            }
            if (C.Rows != m || C.Columns != p)
            {
                throw new ArgumentException("Incompatibel result matrix: " + ToStringShort() + ", " + (transposeB ? B.ViewDice() : B).ToStringShort() + ", " + C.ToStringShort());
            }
            if (this == C || B == C)
            {
                throw new ArgumentException("Matrices must not be identical");
            }

            if (!ignore)
            {
                C.Assign(F1.Mult(beta));
            }

            // cache views
            DoubleMatrix1D[] Brows = new DoubleMatrix1D[n];
            for (int i = n; --i >= 0;)
            {
                Brows[i] = B.ViewRow(i);
            }
            DoubleMatrix1D[] Crows = new DoubleMatrix1D[m];
            for (int i = m; --i >= 0;)
            {
                Crows[i] = C.ViewRow(i);
            }


            ForEachNonZero(
                new Cern.Colt.Function.IntIntDoubleFunction((i, j, value) =>
            {
                var fun = F2.PlusMult(value * alpha);
                //fun.multiplicator = value * alpha;
                if (!transposeA)
                {
                    Crows[i].Assign(Brows[j], fun);
                }
                else
                {
                    Crows[j].Assign(Brows[i], fun);
                }
                return(value);
            }
                                                            ));

            return(C);
        }
        public override DoubleMatrix2D ZMult(DoubleMatrix2D B, DoubleMatrix2D C, double alpha, double beta, Boolean transposeA, Boolean transposeB)
        {
            if (transposeB)
            {
                B = B.ViewDice();
            }
            int m = Rows;
            int n = Columns;

            if (transposeA)
            {
                m = Columns;
                n = Rows;
            }
            int     p      = B.Columns;
            Boolean ignore = (C == null);

            if (C == null)
            {
                C = new DenseDoubleMatrix2D(m, p);
            }

            if (B.Rows != n)
            {
                throw new ArgumentException(String.Format(Cern.LocalizedResources.Instance().Exception_Matrix2DInnerDimensionMustAgree, ToStringShort(), (transposeB ? B.ViewDice() : B).ToStringShort()));
            }
            if (C.Rows != m || C.Columns != p)
            {
                throw new ArgumentException(String.Format(Cern.LocalizedResources.Instance().Exception_IncompatibleResultMatrix, ToStringShort(), (transposeB ? B.ViewDice() : B).ToStringShort(), C.ToStringShort()));
            }
            if (this == C || B == C)
            {
                throw new ArgumentException(Cern.LocalizedResources.Instance().Exception_MatricesMustNotBeIdentical);
            }

            if (!ignore)
            {
                C.Assign(F1.Mult(beta));
            }

            // cache views
            DoubleMatrix1D[] Brows = new DoubleMatrix1D[n];
            for (int i = n; --i >= 0;)
            {
                Brows[i] = B.ViewRow(i);
            }
            DoubleMatrix1D[] Crows = new DoubleMatrix1D[m];
            for (int i = m; --i >= 0;)
            {
                Crows[i] = C.ViewRow(i);
            }

            int[]    idx  = Indexes.ToArray();
            double[] vals = Values.ToArray();
            for (int i = Starts.Length - 1; --i >= 0;)
            {
                int low = Starts[i];
                for (int k = Starts[i + 1]; --k >= low;)
                {
                    int j   = idx[k];
                    var fun = F2.PlusMult(vals[k] * alpha);
                    //fun.Multiplicator = vals[k] * alpha;
                    if (!transposeA)
                    {
                        Crows[i].Assign(Brows[j], fun);
                    }
                    else
                    {
                        Crows[j].Assign(Brows[i], fun);
                    }
                }
            }

            return(C);
        }
        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);
        }