Esempio n. 1
0
        /// <summary>
        /// Extract a diagonal or construct a diagonal array.
        /// </summary>
        /// <param name="v">input array</param>
        /// <param name="k">Diagonal in question. The default is 0. Use `k>0` for diagonals above the main diagonal, and `k<0` for diagonals below the main diagonal.</param>
        /// <returns></returns>
        public static ndarray diag(ndarray v, int k = 0)
        {
            /*
             * Extract a diagonal or construct a diagonal array.
             *
             * See the more detailed documentation for ``numpy.diagonal`` if you use this
             * function to extract a diagonal and wish to write to the resulting array;
             * whether it returns a copy or a view depends on what version of numpy you
             * are using.
             *
             * Parameters
             * ----------
             * v : array_like
             *  If `v` is a 2-D array, return a copy of its `k`-th diagonal.
             *  If `v` is a 1-D array, return a 2-D array with `v` on the `k`-th
             *  diagonal.
             * k : int, optional
             *  Diagonal in question. The default is 0. Use `k>0` for diagonals
             *  above the main diagonal, and `k<0` for diagonals below the main
             *  diagonal.
             *
             * Returns
             * -------
             * out : ndarray
             *  The extracted diagonal or constructed diagonal array.
             *
             * See Also
             * --------
             * diagonal : Return specified diagonals.
             * diagflat : Create a 2-D array with the flattened input as a diagonal.
             * trace : Sum along diagonals.
             * triu : Upper triangle of an array.
             * tril : Lower triangle of an array.
             *
             * Examples
             * --------
             * >>> x = np.arange(9).reshape((3,3))
             * >>> x
             * array([[0, 1, 2],
             *     [3, 4, 5],
             *     [6, 7, 8]])
             *
             * >>> np.diag(x)
             * array([0, 4, 8])
             * >>> np.diag(x, k=1)
             * array([1, 5])
             * >>> np.diag(x, k=-1)
             * array([3, 7])
             *
             * >>> np.diag(np.diag(x))
             * array([[0, 0, 0],
             *     [0, 4, 0],
             *     [0, 0, 8]])
             */

            int i;

            v = asanyarray(v);
            var s = v.dims;

            if (len(s) == 1)
            {
                int n   = (int)(s[0] + Math.Abs(k));
                var res = zeros(new shape(n, n), dtype: v.Dtype);
                if (k >= 0)
                {
                    i = k;
                }
                else
                {
                    i = (-k) * n;
                }

                res.A(":" + (n - k).ToString()).Flat[i.ToString() + "::" + (n + 1).ToString()] = v;
                return(res);
            }
            else if (len(s) == 2)
            {
                return(v.diagonal(k));
            }
            else
            {
                throw new ValueError("Input must be 1- or 2-d.");
            }
        }