-
Notifications
You must be signed in to change notification settings - Fork 11
/
matrixnvector.cpp
executable file
·110 lines (96 loc) · 3.19 KB
/
matrixnvector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//#include "nctest.h"
#include "uebpgdecls.h"
//create 3D array and allocate contiguous memory block this enbales a full block read of netcdf
float*** create3DArrayblock_Contiguous(int nt, int nr, int nc) //inputs: no. of rows and no. of colos , height/time (dimensions) of matrix
{
float*** myMatrix = new float**[nt];
float* ptrMemory = new float[nt*nr*nc];
for (int t = 0; t<nt; t++)
{
//this looks suspicious
//#??_TBC 6.18.13
myMatrix[t] = new float*[nr];
for (int r = 0; r<nr; r++)
{
myMatrix[t][r] = ptrMemory; //new float*[nr];
ptrMemory += nc;
}
}
return myMatrix;
}//float*** create3DArrayblock
//delets a 3D array (frees memory) allocated contiguously
void delete3DArrayblock_Contiguous(float*** myMatrix) // int nr, int nc)// input: 3D array
{
/*for (int t = 0; t< nt; t++)
{
/*for (int r=0; r<nr; r++)
delete [] myMatrix[t][r];
}*/
delete[] myMatrix[0];
delete[] myMatrix;
return;
}//double** CreateMatrix
//create 2D array and allocate contiguous memory block this enbales a full block read of netcdf
float** create2DArray_Contiguous(int nr, int nc) //inputs: no. of rows and no. of colos , height/time (dimensions) of matrix
{
float** myMatrix = new float*[nr];
float* ptrMemory = new float[nr*nc];
for (int r = 0; r<nr; r++)
{
myMatrix[r] = ptrMemory; //new float*[nr];
ptrMemory += nc;
}
return myMatrix;
}//float*** create3DArrayblock
//delets a 2D array (frees memory allocated) contiguously
void delete2DArray_Contiguous(float** myMatrix) // int nr, int nc)// input: 2D array
{
delete[] myMatrix[0];
delete[] myMatrix;
return;
}//double**
//Creates a matrix. The inputs are matrix dimensions. It allocates a memory block of size nrows*ncols* (size of float)
//and returns an array of pointers to the allocated memory block
float*** Create3DArray(int nt, int nr, int nc) //inputs: no. of rows and no. of colos (dimensions) of matrix
{
float*** myMatrix = new float**[nt];
for (int i = 0; i<nt; i++)
{
myMatrix[i] = new float*[nr];
for (int j = 0; j< nr; j++)
myMatrix[i][j] = new float[nc];
}
return myMatrix;
}//float** CreateMatrix
//The following program deletes a matrix passed to it (it frees up the memory block allocated to the matrix)
void Delete3DArray(float ***A, int nt, int nr, int nc) //input: A matrix
{
for (int i = 0; i< nt; i++)
{
for (int j = 0; j< nr; j++)
delete[] A[i][j];
delete[] A[i];
}
delete[] A;
return;
}//void DeleteMatrix
//for int
//create 2D array and allocate contiguous memory block this enbales a full block read of netcdf
int** create2DArray_Contiguous_int(int nr, int nc) //inputs: no. of rows and no. of colos , height/time (dimensions) of matrix
{
int** myMatrix = new int* [nr];
int* ptrMemory = new int[nr * nc];
for (int r = 0; r < nr; r++)
{
myMatrix[r] = ptrMemory; //new float*[nr];
ptrMemory += nc;
}
return myMatrix;
}//float*** create3DArrayblock
//delets a 2D array (frees memory allocated) contiguously
void delete2DArray_Contiguous_int(int** myMatrix) // int nr, int nc)// input: 2D array
{
delete[] myMatrix[0];
delete[] myMatrix;
return;
}//double**