示例#1
0
        public static Boolean EvaluateDoubleDoubleFunctionEquality(DoubleDoubleFunction a, DoubleDoubleFunction b)
        {
            String FullNameA = a.Method.DeclaringType.FullName;
            String FullNameB = b.Method.DeclaringType.FullName;

            return(FullNameA.Equals(FullNameB));
        }
示例#2
0
 /// <summary>
 /// Constructs the function <i>g( h(a,b) )</i>.
 /// </summary>
 /// <param name="g">a unary function.</param>
 /// <param name="h">a binary function.</param>
 /// <returns>the unary function <i>g( h(a,b) )</i>.</returns>
 public static DoubleDoubleFunction Chain(DoubleFunction g, DoubleDoubleFunction h)
 {
     return(new DoubleDoubleFunction((a, b) =>
     {
         return g(h(a, b));
     }));
 }
        /// <summary>
        /// Assigns the result of a function to each cell; <tt>x[i] = function(x[i],y[i])</tt>.
        /// (Iterates downwards from <tt>[size()-1]</tt> to <tt>[0]</tt>).
        /// </summary>
        /// <param name="y">
        /// The secondary matrix to operate on.
        /// </param>
        /// <param name="function">
        /// The function taking as first argument the current cell's value of <tt>this</tt>,
        /// and as second argument the current cell's value of <tt>y</tt>.
        /// </param>
        /// <returns>
        /// <tt>this</tt> (for convenience only).
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <tt>size() != y.size()</tt>.
        /// </exception>
        public override DoubleMatrix1D Assign(DoubleMatrix1D y, DoubleDoubleFunction function)
        {
            // overriden for performance only
            if (!(y is DenseDoubleMatrix1D))
            {
                return(base.Assign(y, function));
            }

            var other = (DenseDoubleMatrix1D)y;

            CheckSize(y);
            double[] elems      = Elements;
            double[] otherElems = other.Elements;
            if (elems == null || otherElems == null)
            {
                throw new ApplicationException();
            }
            int s  = Stride;
            int ys = other.Stride;

            int index      = this.Index(0);
            int otherIndex = other.Index(0);

            // specialized for speed
            for (int k = Size; --k >= 0;)
            {
                elems[index] = function(elems[index], otherElems[otherIndex]);
                index       += s;
                otherIndex  += ys;
            }

            return(this);
        }
示例#4
0
 public void Assign(DoubleMatrix2D x, DoubleMatrix2D y, DoubleDoubleFunction function)
 {
     run(x, y, false, new Matrix2DMatrix2DFunction((AA, BB) =>
     {
         seqBlas.Assign(AA, BB, function);
         return(0);
     }));
 }
示例#5
0
 /// <summary>
 /// Assigns the result of a function to each cell; <tt>x[row,col] = function(x[row,col],y[row,col])</tt>.
 /// </summary>
 /// <param name="y">
 /// The secondary matrix to operate on.
 /// </param>
 /// <param name="function">
 /// The function taking as first argument the current cell's value of <tt>this</tt>,
 /// and as second argument the current cell's value of <tt>y</tt>.
 /// </param>
 /// <returns>
 /// <tt>this</tt> (for convenience only).
 /// </returns>
 /// <exception cref="ArgumentException">
 /// If <tt>columns() != other.columns() || rows() != other.rows()</tt>
 /// </exception>
 public override DoubleMatrix2D Assign(DoubleMatrix2D y, DoubleDoubleFunction function)
 {
     if (!IsView)
     {
         CheckShape(y);
     }
     return(base.Assign(y, function));
 }
 /// <summary>
 /// Assigns the result of a function to each cell; <tt>x[i] = function(x[i],y[i])</tt>.
 /// </summary>
 /// <param name="y">
 /// The secondary matrix to operate on.
 /// </param>
 /// <param name="function">
 /// A function taking as first argument the current cell's value of <tt>this</tt>,
 /// and as second argument the current cell's value of <tt>y</tt>.
 /// </param>
 /// <returns>
 /// <tt>this</tt> (for convenience only).
 /// </returns>
 /// <exception cref="ArgumentOutOfRangeException">
 /// If <tt>size() != y.size()</tt>.
 /// </exception>
 public virtual DoubleMatrix1D Assign(DoubleMatrix1D y, DoubleDoubleFunction function)
 {
     CheckSize(y);
     for (int i = Size; --i >= 0;)
     {
         this[i] = function(this[i], y[i]);
     }
     return(this);
 }
示例#7
0
 /// <summary>
 /// Assigns the result of a function to each cell; <tt>x[row,col] = function(x[row,col],y[row,col])</tt>.
 /// </summary>
 /// <param name="y">
 /// The secondary matrix to operate on.
 /// </param>
 /// <param name="function">
 /// The function taking as first argument the current cell's value of <tt>this</tt>,
 /// and as second argument the current cell's value of <tt>y</tt>.
 /// </param>
 /// <returns>
 /// <tt>this</tt> (for convenience only).
 /// </returns>
 /// <exception cref="ArgumentException">
 /// If <tt>columns() != other.columns() || rows() != other.rows()</tt>
 /// </exception>
 public virtual DoubleMatrix2D Assign(DoubleMatrix2D y, DoubleDoubleFunction function)
 {
     CheckShape(y);
     for (int row = Rows; --row >= 0;)
     {
         for (int column = Columns; --column >= 0;)
         {
             this[row, column] = function(this[row, column], y[row, column]);
         }
     }
     return(this);
 }
        /// <summary>
        /// Applies a function to each cell and aggregates the results.
        /// </summary>
        /// <param name="aggr">
        /// An aggregation function taking as first argument the current aggregation and as second argument the transformed current cell value.
        /// </param>
        /// <param name="f">
        /// A function transforming the current cell value.
        /// </param>
        /// <returns>
        /// The aggregated measure.
        /// </returns>
        public virtual double Aggregate(DoubleDoubleFunction aggr, DoubleFunction f)
        {
            if (Size == 0)
            {
                return(double.NaN);
            }
            double a = f(this[Size - 1]);

            for (int i = Size - 1; --i >= 0;)
            {
                a = aggr(a, f(this[i]));
            }
            return(a);
        }
        /// <summary>
        /// Applies a function to each corresponding cell of two matrices and aggregates the results.
        /// </summary>
        /// <param name="other">
        /// The other matrix.
        /// </param>
        /// <param name="aggr">
        /// An aggregation function taking as first argument the current aggregation and as second argument the transformed current cell values.
        /// </param>
        /// <param name="f">
        /// A  function transforming the current cell values.
        /// </param>
        /// <returns>
        /// The aggregated measure.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <tt>size() != other.size()</tt>.
        /// </exception>
        public double Aggregate(DoubleMatrix1D other, DoubleDoubleFunction aggr, DoubleDoubleFunction f)
        {
            CheckSize(other);
            if (Size == 0)
            {
                return(double.NaN);
            }
            double a = f(this[Size - 1], other[Size - 1]);

            for (int i = Size - 1; --i >= 0;)
            {
                a = aggr(a, f(this[i], other[i]));
            }
            return(a);
        }
        /// <summary>
        /// Assigns the result of a function to each cell; <tt>x[row,col] = function(x[row,col],y[row,col])</tt>.
        /// </summary>
        /// <param name="y">
        /// The secondary matrix to operate on.
        /// </param>
        /// <param name="function">
        /// A function taking as first argument the current cell's value of <tt>this</tt>, and as second argument the current cell's value of <tt>y</tt>.
        /// </param>
        /// <returns>
        /// <tt>this</tt> (for convenience only).
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <tt>columns() != other.columns() || rows() != other.rows()</tt>
        /// </exception>
        public override DoubleMatrix2D Assign(DoubleMatrix2D y, DoubleDoubleFunction function)
        {
            // overriden for performance only
            if (!(y is DenseDoubleMatrix2D))
            {
                return(base.Assign(y, function));
            }

            var other = (DenseDoubleMatrix2D)y;

            CheckShape(y);

            double[] elems      = Elements;
            double[] otherElems = other.Elements;
            if (elems == null || otherElems == null)
            {
                throw new ApplicationException();
            }
            int cs  = ColumnStride;
            int ocs = ColumnStride;
            int rs  = RowStride;
            int ors = other.RowStride;

            int otherIndex = other.Index(0, 0);
            int index      = this.Index(0, 0);

            // specialized for speed
            for (int row = Rows; --row >= 0;)
            {
                for (int i = index, j = otherIndex, column = Columns; --column >= 0;)
                {
                    elems[i] = function(elems[i], otherElems[j]);
                    i       += cs;
                    j       += ocs;
                }

                index      += rs;
                otherIndex += ors;
            }

            return(this);
        }
示例#11
0
        /// <summary>
        /// Applies a function to each cell and aggregates the results.
        /// </summary>
        /// <param name="aggr">
        /// An aggregation function taking as first argument the current aggregation and as second argument the transformed current cell value.
        /// </param>
        /// <param name="f">
        /// A function transforming the current cell value.
        /// </param>
        /// <returns>
        /// The aggregated measure.
        /// </returns>
        public override double Aggregate(DoubleDoubleFunction aggr, DoubleFunction f)
        {
            double result = double.NaN;
            bool   first  = true;

            foreach (var e in Elements.Values)
            {
                if (first)
                {
                    first  = false;
                    result = f(e);
                }
                else
                {
                    result = aggr(result, f(e));
                }
            }

            return(result);
        }
示例#12
0
        /// <summary>
        /// Applies a function to each cell and aggregates the results.
        /// </summary>
        /// <param name="aggr">
        /// An aggregation function taking as first argument the current aggregation and as second argument the transformed current cell value.
        /// </param>
        /// <param name="f">
        /// A function transforming the current cell value.
        /// </param>
        /// <returns>
        /// The aggregated measure.
        /// </returns>
        public double Aggregate(DoubleDoubleFunction aggr, DoubleFunction f)
        {
            if (Size == 0)
            {
                return(double.NaN);
            }
            double a = f(this[Rows - 1, Columns - 1]);
            int    d = 1; // last cell already done

            for (int row = Rows; --row >= 0;)
            {
                for (int column = Columns - d; --column >= 0;)
                {
                    a = aggr(a, f(this[row, column]));
                }
                d = 0;
            }

            return(a);
        }
示例#13
0
        /// <summary>
        /// Applies a function to each cell and aggregates the results.
        /// </summary>
        /// <param name="aggr">
        /// An aggregation function taking as first argument the current aggregation and as second argument the transformed current cell value.
        /// </param>
        /// <param name="f">
        /// A function transforming the current cell value.
        /// </param>
        /// <returns>
        /// The aggregated measure.
        /// </returns>
        public override double Aggregate(DoubleDoubleFunction aggr, DoubleFunction f)
        {
            double result = double.NaN;
            bool   first  = true;

            AutoParallel.AutoParallelForEach(Elements.Values, (e) =>
            {
                if (first)
                {
                    first  = false;
                    result = f(e);
                }
                else
                {
                    result = aggr(result, f(e));
                }
            });

            return(result);
        }
示例#14
0
 /// <summary>
 /// Constructs the function <i>f( g(a), h(b) )</i>.
 /// </summary>
 /// <param name="f">a binary function.</param>
 /// <param name="g">a binary function.</param>
 /// <param name="h">a binary function.</param>
 /// <returns>the binary function <i>f( g(a), h(b) )</i>.</returns>
 public static DoubleDoubleFunction Chain(DoubleDoubleFunction f, DoubleFunction g, DoubleFunction h)
 {
     return(new DoubleDoubleFunction((a, b) => { return f(g(a), h(b)); }));
 }
示例#15
0
        /// <summary>
        /// Constructs and returns a new QR decomposition object;  computed by Householder reflections;
        /// The decomposed matrices can be retrieved via instance methods of the returned decomposition object.
        /// Return a decomposition object to access <i>R</i> and the Householder vectors <i>H</i>, and to compute <i>Q</i>.
        /// </summary>
        /// <param name="A">A rectangular matrix.</param>
        /// <exception cref="ArgumentException">if <i>A.Rows &lt; A.Columns</i>.</exception>
        public QRDecomposition(DoubleMatrix2D A)
        {
            Property.DEFAULT.CheckRectangular(A);

            Functions F = Functions.functions;

            // Initialize.
            QR    = A.Copy();
            m     = A.Rows;
            n     = A.Columns;
            Rdiag = A.Like1D(n);
            //Rdiag = new double[n];
            DoubleDoubleFunction hypot = Algebra.HypotFunction();

            // precompute and cache some views to avoid regenerating them time and again
            DoubleMatrix1D[] QRcolumns     = new DoubleMatrix1D[n];
            DoubleMatrix1D[] QRcolumnsPart = new DoubleMatrix1D[n];
            for (int k = 0; k < n; k++)
            {
                QRcolumns[k]     = QR.ViewColumn(k);
                QRcolumnsPart[k] = QR.ViewColumn(k).ViewPart(k, m - k);
            }

            // Main loop.
            for (int k = 0; k < n; k++)
            {
                //DoubleMatrix1D QRcolk = QR.ViewColumn(k).ViewPart(k,m-k);
                // Compute 2-norm of k-th column without under/overflow.
                double nrm = 0;
                //if (k<m) nrm = QRcolumnsPart[k].aggregate(hypot,F.identity);

                for (int i = k; i < m; i++)
                { // fixes bug reported by [email protected]
                    nrm = Algebra.Hypot(nrm, QR[i, k]);
                }


                if (nrm != 0.0)
                {
                    // Form k-th Householder vector.
                    if (QR[k, k] < 0)
                    {
                        nrm = -nrm;
                    }
                    QRcolumnsPart[k].Assign(F2.Div(nrm));

                    /*
                     * for (int i = k; i < m; i++) {
                     * QR[i][k] /= nrm;
                     * }
                     */

                    QR[k, k] = QR[k, k] + 1;

                    // Apply transformation to remaining columns.
                    for (int j = k + 1; j < n; j++)
                    {
                        DoubleMatrix1D QRcolj = QR.ViewColumn(j).ViewPart(k, m - k);
                        double         s      = QRcolumnsPart[k].ZDotProduct(QRcolj);

                        /*
                         * // fixes bug reported by John Chambers
                         * DoubleMatrix1D QRcolj = QR.ViewColumn(j).ViewPart(k,m-k);
                         * double s = QRcolumnsPart[k].ZDotProduct(QRcolumns[j]);
                         * double s = 0.0;
                         * for (int i = k; i < m; i++) {
                         * s += QR[i][k]*QR[i][j];
                         * }
                         */
                        s = -s / QR[k, k];
                        //QRcolumnsPart[j].Assign(QRcolumns[k], F.PlusMult(s));

                        for (int i = k; i < m; i++)
                        {
                            QR[i, j] = QR[i, j] + s * QR[i, k];
                        }
                    }
                }
                Rdiag[k] = -nrm;
            }
        }
示例#16
0
 /// <summary>
 /// Constructs a unary function from a binary function with the second operand (argument) fixed to the given constant <i>c</i>.
 /// The first operand is variable (free).
 /// </summary>
 /// <param name="function">a binary function taking operands in the form <i>function.apply(var,c)</i>.</param>
 /// <param name="c"></param>
 /// <returns>the unary function <i>function(var,c)</i>.</returns>
 public static DoubleFunction BindArg2(DoubleDoubleFunction function, double c)
 {
     return(new DoubleFunction((var) => { return function(var, c); }));
 }
示例#17
0
 /// <summary>
 /// Constructs a function that returns <i>function.apply(b,a)</i>, i.ed applies the function with the first operand as second operand and the second operand as first operand.
 /// </summary>
 /// <param name="function">a function taking operands in the form <i>function.apply(a,b)</i>.</param>
 /// <returns>the binary function <i>function(b, a)</i>.</returns>
 public static DoubleDoubleFunction SwapArgs(DoubleDoubleFunction function)
 {
     return(new DoubleDoubleFunction((a, b) => { return function(b, a); }));
 }
示例#18
0
 public PlusMult(double multiplicator)
 {
     this.Multiplicator = multiplicator;
     Apply = new DoubleDoubleFunction((a, b) => { return(a + b * Multiplicator); });
 }