1 /******************************************************************
2 This file is a part of eco: R Package for Fitting Bayesian Models
3 of Ecological Inference for 2x2 Tables
4 by Kosuke Imai and Ying Lu
5 Copyright: GPL version 2 or later.
6 *******************************************************************/
7
8 #include <stdlib.h>
9 #include <assert.h>
10 #include <stdio.h>
11 #include <R_ext/Utils.h>
12 #include <R_ext/PrtUtil.h>
13 #include <R.h>
14
intArray(int num)15 int* intArray(int num) {
16 int *iArray = (int *)malloc(num * sizeof(int));
17 if (iArray)
18 return iArray;
19 else {
20 error("Out of memory error in intArray\n");
21 return NULL;
22 }
23 }
24
intMatrix(int row,int col)25 int** intMatrix(int row, int col) {
26 int i;
27 int **iMatrix = (int **)malloc(row * sizeof(int *));
28 if (iMatrix) {
29 for (i = 0; i < row; i++) {
30 iMatrix[i] = (int *)malloc(col * sizeof(int));
31 if (!iMatrix[i])
32 error("Out of memory error in intMatrix\n");
33 }
34 return iMatrix;
35 }
36 else {
37 error("Out of memory error in intMatrix\n");
38 return NULL;
39 }
40 }
41
doubleArray(int num)42 double* doubleArray(int num) {
43 //double *dArray = (double *)malloc(num * sizeof(double));
44 double *dArray = Calloc(num,double);
45 if (dArray)
46 return dArray;
47 else {
48 error("Out of memory error in doubleArray\n");
49 return NULL;
50 }
51 }
52
doubleMatrix(int row,int col)53 double** doubleMatrix(int row, int col) {
54 int i;
55 //double **dMatrix = (double **)malloc((size_t)(row * sizeof(double *)));
56 double **dMatrix = Calloc(row,double*);
57 if (dMatrix) {
58 for (i = 0; i < row; i++) {
59 dMatrix[i] = Calloc(col,double);
60 if (!dMatrix[i]) {
61 error("Out of memory error in doubleMatrix\n");
62 return NULL;
63 }
64 }
65 return dMatrix;
66 }
67 else {
68 error("Out of memory error in doubleMatrix\n");
69 return NULL;
70 }
71 }
72
doubleMatrix3D(int x,int y,int z)73 double*** doubleMatrix3D(int x, int y, int z) {
74 int i;
75 double ***dM3 = (double ***)malloc(x * sizeof(double **));
76 if (dM3) {
77 for (i = 0; i < x; i++)
78 dM3[i] = doubleMatrix(y, z);
79 return dM3;
80 }
81 else {
82 error("Out of memory error in doubleMatrix3D\n");
83 return NULL;
84 }
85 }
86
longArray(int num)87 long* longArray(int num) {
88 long *lArray = (long *)malloc(num * sizeof(long));
89 if (lArray)
90 return lArray;
91 else {
92 error("Out of memory error in longArray\n");
93 return NULL;
94 }
95 }
96
FreeMatrix(double ** Matrix,int row)97 void FreeMatrix(double **Matrix, int row) {
98 int i;
99 for (i = 0; i < row; i++)
100 Free(Matrix[i]);
101 Free(Matrix);
102 }
103
FreeintMatrix(int ** Matrix,int row)104 void FreeintMatrix(int **Matrix, int row) {
105 int i;
106 for (i = 0; i < row; i++)
107 free(Matrix[i]);
108 free(Matrix);
109 }
110
Free3DMatrix(double *** Matrix,int index,int row)111 void Free3DMatrix(double ***Matrix, int index, int row) {
112 int i;
113 for (i = 0; i < index; i++)
114 FreeMatrix(Matrix[i], row);
115 free(Matrix);
116 }
117
118
119
120
121