/*
 * call-seq:
 *   new(vsize, hsize)
 *   new(vsize, hsize, scalar)
 *   new(vsize, hsize) { |i, j| ... }
 *
 * Returns a new matrix with dimensions [+vsize+, +hsize+].
 *
 * In the first form, the matrix is filled with zero.  In the second
 * form, the matrix is filled with +scalar+.  In the third form, each
 * index pair is passed to the block and the respective matrix element
 * is assigned the block result.
 *
 *   DMatrix.new(4,4) { |i,j| i == j ? 1 : 0 }
 *
 * is the 4 x 4 identity matrix.
 *
 */
VALUE rb_dmatrix_initialize(int argc, VALUE* argv, VALUE self)
{
    DMatrix* a ;
    VALUE rvsize ;
    VALUE rhsize ;
    VALUE rscalar ;
    
    int scan = rb_scan_args(argc, argv, "21", &rvsize, &rhsize, &rscalar) ;

    get_dmatrix(a, self) ;
    DMatrix_init(a, num2size(rvsize), num2size(rhsize)) ;

    if( scan == 3 )
    {
        DMatrix_fill(a, NUM2DBL(rscalar)) ;
    }
    else if( rb_block_given_p() == Qtrue )
    {
        int i ;
        int j ;

        for( j = 0 ; j != a->hsize ; ++j )
        {
            for( i = 0 ; i != a->vsize ; ++i )
            {
                VALUE pair = rb_assoc_new(INT2FIX(i),
                                          INT2FIX(j)) ;
                a->data[i + j*a->vsize] = NUM2DBL(rb_yield(pair)) ;
            }
        }
    }
    else
    {
        DMatrix_fill(a, dmatrix_doublereal_0) ;
    }

    return self ;
}