Example #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 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 DoubleMatrix2D Assign(DoubleMatrix2D matrixY, Cern.Colt.Function.DoubleDoubleFunction function, Double x = 0, Double y = 0)
        {
            CheckShape(matrixY);

            if (Cern.Jet.Math.Functions.EvaluateDoubleDoubleFunctionEquality(function, F2.PlusMult(x)))
            {                     // x[i] = x[i] + alpha*y[i]
                double alpha = x; // ((Cern.Jet.Math.PlusMult)function).multiplicator;
                if (alpha == 0)
                {
                    return(this);            // nothing to do
                }
                matrixY.ForEachNonZero(
                    new Cern.Colt.Function.IntIntDoubleFunction((i, j, value) =>
                {
                    this[i, j] = this[i, j] + alpha * value;
                    return(value);
                }
                                                                ));
                return(this);
            }

            if (Cern.Jet.Math.Functions.EvaluateFunctionEquality(function.Method, F2.Mult.Method))
            { // x[i] = x[i] * y[i]
                ForEachNonZero(

                    new Cern.Colt.Function.IntIntDoubleFunction((i, j, value) =>
                {
                    this[i, j] = this[i, j] * matrixY[i, j];
                    return(value);
                }
                                                                ));
                return(this);
            }

            if (Cern.Jet.Math.Functions.EvaluateFunctionEquality(function.Method, F2.Div.Method))
            { // x[i] = x[i] / y[i]
                ForEachNonZero(

                    new Cern.Colt.Function.IntIntDoubleFunction((i, j, value) =>
                {
                    this[i, j] = this[i, j] / matrixY[i, j];
                    return(value);
                }
                                                                ));
                return(this);
            }

            return(base.Assign(matrixY, function));
        }
Example #4
0
 public void Daxpy(double alpha, DoubleMatrix2D A, DoubleMatrix2D B)
 {
     B.Assign(A, F2.PlusMult(alpha));
 }
Example #5
0
 public void Daxpy(double alpha, DoubleMatrix1D x, DoubleMatrix1D y)
 {
     y.Assign(x, F2.PlusMult(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 DoubleMatrix2D Assign(DoubleMatrix2D matrixY, Cern.Colt.Function.DoubleDoubleFunction function, Double x = 0, Double y = 0)
        {
            CheckShape(matrixY);

            if (Cern.Jet.Math.Functions.EvaluateDoubleDoubleFunctionEquality(function, F2.PlusMult(x)))
            {
                double alpha = x;

                //if (function is Cern.Jet.Math.PlusMult) { // x[i] = x[i] + alpha*y[i]
                //double alpha = ((Cern.Jet.Math.PlusMult)function).multiplicator;
                if (alpha == 0)
                {
                    return(this);            // nothing to do
                }
                matrixY.ForEachNonZero(
                    new Cern.Colt.Function.IntIntDoubleFunction((i, j, value) =>
                {
                    this[i, j] = this[i, j] + alpha * value;
                    return(value);
                }
                                                                ));
                return(this);
            }

            //if (function==Cern.Jet.Math.Functions.mult) { // x[i] = x[i] * y[i]
            var mult = F2.Mult(x, y);

            if (Cern.Jet.Math.Functions.EvaluateFunctionEquality(function.Method, F2.Mult.Method))
            {
                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];
                        vals[k] *= matrixY[i, j];
                        if (vals[k] == 0)
                        {
                            Remove(i, j);
                        }
                    }
                }
                return(this);
            }

            if (Cern.Jet.Math.Functions.EvaluateFunctionEquality(function.Method, F2.Div.Method))
            {
                //	if (function==Cern.Jet.Math.Functions.Div) { // x[i] = x[i] / y[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];
                        vals[k] /= matrixY[i, j];
                        if (vals[k] == 0)
                        {
                            Remove(i, j);
                        }
                    }
                }
                return(this);
            }

            return(base.Assign(matrixY, function));
        }