Example #1
0
        /// <summary>
        /// Assigns the result of a function to each cell; <i>x[row,col] = function(x[row,col],y[row,col])</i>.
        /// <p>
        /// <b>Example:</b>
        /// <pre>
        /// // assign x[row,col] = x[row,col]<sup>y[row,col]</sup>
        /// m1 = 2 x 2 matrix
        /// 0 1
        /// 2 3
        ///
        /// m2 = 2 x 2 matrix
        /// 0 2
        /// 4 6
        ///
        /// m1.assign(m2, Cern.Jet.Math.Functions.pow);
        /// -->
        /// m1 == 2 x 2 matrix
        ///          1   1
        /// 16 729
        /// </pre>
        /// For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>.
        /// </summary>
        /// <param name="y">the secondary matrix to operate on.</param>
        /// <param name="function">a function object taking as first argument the current cell's value of <i>this</i>, and as second argument the current cell's value of <i>y</i>,</param>
        /// <returns><i>this</i> (for convenience only).</returns>
        /// <exception cref="ArgumentException">if <i>columns() != other.columns() || rows() != other.rows()</i></exception>
        /// <see cref="Cern.Jet.Math.Functions"/>
        public override ObjectMatrix2D Assign(ObjectMatrix2D y, Cern.Colt.Function.ObjectObjectFunction <Object> function)
        {
            // overriden for performance only
            if (!(y is DenseObjectMatrix2D))
            {
                return(base.Assign(y, function));
            }
            DenseObjectMatrix2D other = (DenseObjectMatrix2D)y;

            CheckShape(y);

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

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

            // the general case x[i] = f(x[i],y[i])
            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);
        }