#include "doubledouble.h"
#include "ddnrutil.h"
#include "malloc.h"
#include <stdio.h>

static void nrerror(const char error_text[])
{
        fprintf(stderr,"Numerical Recipes run-time error...\n");
        fprintf(stderr,"%s\n",error_text);
        fprintf(stderr,"...now exiting to system...\n");
        exit(1);
}

doubledouble *ddvector(int nl,int nh)
{
	doubledouble *v;

	v=(doubledouble *)malloc((unsigned) (nh-nl+1)*sizeof(doubledouble));
	if (!v) nrerror("allocation failure in dvector()");
	return v-nl;
}

doubledouble **ddmatrix(int nrl,int nrh,int ncl,int nch)
{
	int i;
	doubledouble **m;

	m=(doubledouble **) malloc((unsigned) (nrh-nrl+1)*sizeof(doubledouble*));
	if (!m) nrerror("allocation failure 1 in dmatrix()");
	m -= nrl;

	for(i=nrl;i<=nrh;i++) {
		m[i]=(doubledouble *) malloc((unsigned) (nch-ncl+1)*sizeof(doubledouble));
		if (!m[i]) nrerror("allocation failure 2 in dmatrix()");
		m[i] -= ncl;
	}
	return m;
}

void free_ddvector(doubledouble *v, int nl,int nh)
{
	free((char*) (v+nl));
}


void free_ddmatrix(doubledouble **m, int nrl,int nrh,int ncl,int nch)
{
	int i;

	for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
	free((char*) (m+nrl));
}

