View Raw SPL
/*****************************************************************************
*                                                                            *
*   ZEROS.SPL    Copyright (C) 1997-2006 DSP Development Corporation         *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Returns an array of all zeros                               *
*                                                                            *
*   Revisions:    9 Jun 1997  RRR  Creation                                  *
*                14 Feb 2006  RRR  use builtin gvalarray for speed           *
*                                                                            *
*****************************************************************************/

#include 

#if @HELP_ZEROS

    ZEROS

    Purpose: Creates an array of all zeros

    Syntax:  ZEROS(numrows, numcols, dx, dy)

              numrows - an integer specifying the number of rows
              numcols - an optional integer for the number columns (default 1)
                   dx - an optional real, the delta X value. Defaults to 1.0.
                   dy - an optional real, the delta Y value. Defaults to 1.0.

    Returns: A series or array

    Example:
             Zeros(3)

             returns the series {0, 0, 0}


             Zeros(3, 2)

             returns the 3x2 array {{0, 0},
                                    {0, 0},
                                    {0, 0}}

    Remarks:
             Zeros(size(A)) returns a array of all zeros with same dimension
             as A


    See Also
             Gline
             Ravel
             Size
             Zeros

#endif


/* generate a series or array of all zeros */
zeros(a, b, dx, dy)
{
        local nr, nc, out;

        if (argc < 4)
        {
                if (argc < 3) dx = 1.0;
                
                dy = 1.0;
        }

        if (argc > 1)
        {
                /* e.g. zeros(10, 3) */
                if (isscalar(a) && isscalar(b))
                {
                        /* convert to ints */
                        a = castint(a);
                        b = castint(b);
                        
                        if (a <= 0 || b <= 0)
                        {
                                /* empty series */
                                return({});
                        }
                        else
                        {
                                out = gvalarray(0.0, a, b);
                                setdeltax(out, dx);
                        }
                        
                        setdeltay(out, dy);

                        return(out);
                }
                else
                {
                        error("zeros - series or number required");
                }
        }
        else
        {
                if (argc < 1) return(0);

                if (isarray(a))
                {
                        /* series argument - get size */
                        (nr, nc) = size(a);
                        
                        if (nr == 2 && nc == 1)
                        {
                                /*
                                 *  assume series came from size function,
                                 *  e.g. zeros(size(a))
                                 */
                                
                                b = int(a[2]);
                                a = int(a[1]);
                        }
                        else
                        {
                                /* e.g. zeros(x) - obsolete */
                                dx = deltax(a);
                                dy = deltay(a);
                                a  = nr;
                                b  = nc;
                        }
                }
                else if (isscalar(a))
                {
                        /* convert to ints */
                        a = castint(a);
                        b = a;
                }
                else
                {
                        error("zeros - series or number required");
                }

                if (a * b == 0)
                {
                        /* empty series */
                        out = {};
                }
                else
                {
                        out = gvalarray(0.0, a, b);
                        setdeltax(out, dx);
                }

                setdeltay(out, dy);

                return(out);
        }
}