| 1 |
/* |
| 2 |
* Copyright (C) 1996-2011 Daniel Waggoner |
| 3 |
* |
| 4 |
* This free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* It is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* If you did not received a copy of the GNU General Public License |
| 15 |
* with this software, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "dw_matrix_array.h" |
| 19 |
#include "dw_error.h" |
| 20 |
#include "bmatrix.h" |
| 21 |
#include "dw_std.h" |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <stdarg.h> |
| 25 |
|
| 26 |
TElementSpecification dw_VectorSpecs = |
| 27 |
{ |
| 28 |
dw_ARRAY_POINTER, |
| 29 |
sizeof(TVector), |
| 30 |
sizeof(int)+sizeof(TElementSpecification*), |
| 31 |
(void (*)(void*))FreeVector, |
| 32 |
DefaultPointerConstructor, |
| 33 |
(void* (*)(void*,void*))EquateVector, |
| 34 |
NULL, |
| 35 |
(int (*)(FILE*,void*,char*))dw_PrintVector, |
| 36 |
(int (*)(FILE*,void*))dw_ReadVector |
| 37 |
}; |
| 38 |
|
| 39 |
TElementSpecification dw_MatrixSpecs = |
| 40 |
{ |
| 41 |
dw_ARRAY_POINTER, |
| 42 |
sizeof(TMatrix), |
| 43 |
sizeof(int)+sizeof(TElementSpecification*), |
| 44 |
(void (*)(void*))FreeMatrix, |
| 45 |
DefaultPointerConstructor, |
| 46 |
(void* (*)(void*,void*))EquateMatrix, |
| 47 |
NULL, |
| 48 |
(int (*)(FILE*,void*,char*))dw_PrintMatrix, |
| 49 |
(int (*)(FILE*,void*))dw_ReadMatrix |
| 50 |
}; |
| 51 |
|
| 52 |
/*******************************************************************************/ |
| 53 |
/*************************** Multidimensional Arrays ***************************/ |
| 54 |
/*******************************************************************************/ |
| 55 |
void* dw_CreateMultidimensionalArrayList_vector(int depth, ...) |
| 56 |
{ |
| 57 |
va_list ap; |
| 58 |
int i, *dim; |
| 59 |
void *a=(void*)NULL; |
| 60 |
if ((dim=(int*)dw_malloc(depth*sizeof(int)))) |
| 61 |
{ |
| 62 |
va_start(ap,depth); |
| 63 |
for (i=0; i < depth; i++) dim[i]=va_arg(ap,int); |
| 64 |
va_end(ap); |
| 65 |
a=dw_CreateMultidimensionalArray_vector(depth,dim); |
| 66 |
dw_free(dim); |
| 67 |
} |
| 68 |
return a; |
| 69 |
} |
| 70 |
|
| 71 |
void* dw_CreateMultidimensionalArrayList_matrix(int depth, ...) |
| 72 |
{ |
| 73 |
va_list ap; |
| 74 |
int i, *dim; |
| 75 |
void *a=(void*)NULL; |
| 76 |
if ((dim=(int*)dw_malloc(depth*sizeof(int)))) |
| 77 |
{ |
| 78 |
va_start(ap,depth); |
| 79 |
for (i=0; i < depth; i++) dim[i]=va_arg(ap,int); |
| 80 |
va_end(ap); |
| 81 |
a=dw_CreateMultidimensionalArray_matrix(depth,dim); |
| 82 |
dw_free(dim); |
| 83 |
} |
| 84 |
return a; |
| 85 |
} |
| 86 |
|
| 87 |
/******************************************************************************/ |
| 88 |
/******************************* Initializaton ********************************/ |
| 89 |
/******************************************************************************/ |
| 90 |
void* dw_InitializeArray_vector(void* x, PRECISION y) |
| 91 |
{ |
| 92 |
int i; |
| 93 |
if (!x) |
| 94 |
dw_Error(NULL_ERR); |
| 95 |
else |
| 96 |
if (dw_IsArrayA(x)) |
| 97 |
for (i=dw_DimA(x)-1; i >= 0; i--) dw_InitializeArray_vector(((void**)x)[i],y); |
| 98 |
else |
| 99 |
for (i=dw_DimA(x)-1; i >= 0; i--) InitializeVector(((TVector*)x)[i],y); |
| 100 |
return x; |
| 101 |
} |
| 102 |
|
| 103 |
void* dw_InitializeArray_matrix(void* X, PRECISION y) |
| 104 |
{ |
| 105 |
int i; |
| 106 |
if (!X) |
| 107 |
dw_Error(NULL_ERR); |
| 108 |
else |
| 109 |
if (dw_IsArrayA(X)) |
| 110 |
for (i=dw_DimA(X)-1; i >= 0; i--) dw_InitializeArray_matrix(((void**)X)[i],y); |
| 111 |
else |
| 112 |
for (i=dw_DimA(X)-1; i >= 0; i--) InitializeMatrix(((TMatrix*)X)[i],y); |
| 113 |
return X; |
| 114 |
} |
| 115 |
|
| 116 |
/******************************************************************************/ |
| 117 |
/****************************** Tensor Calculus *******************************/ |
| 118 |
/******************************************************************************/ |
| 119 |
/* |
| 120 |
Assumes: |
| 121 |
X - r x s matrix or null pointer |
| 122 |
Y - k dimensional array of matrices |
| 123 |
|
| 124 |
Returns: |
| 125 |
The the tensor product |
| 126 |
|
| 127 |
Y[0] x Y[1] x ... x Y[k-1] |
| 128 |
|
| 129 |
If X is null, then space for the tensor product is allocated. If X is not |
| 130 |
null, then the dimensions must match. |
| 131 |
|
| 132 |
r=RowM(Y[0]) x ... x RowM(Y[k-1]) |
| 133 |
c=ColM(Y[0]) x ... x ColM(Y[k-1]) |
| 134 |
|
| 135 |
Notes: |
| 136 |
Calls bMatrixTensor(). |
| 137 |
*/ |
| 138 |
TMatrix MatrixTensor(TMatrix X, TMatrix* Y) |
| 139 |
{ |
| 140 |
int i, r=1, c=1; |
| 141 |
PRECISION *Z, *U, *V, *W; |
| 142 |
TMatrix rtrn; |
| 143 |
if (!Y) |
| 144 |
{ |
| 145 |
dw_Error(NULL_ERR); |
| 146 |
return (TMatrix)NULL; |
| 147 |
} |
| 148 |
for (i=dw_DimA(Y)-1; i >= 0; i--) |
| 149 |
if (!Y[i]) |
| 150 |
{ |
| 151 |
dw_Error(NULL_ERR); |
| 152 |
return (TMatrix)NULL; |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
r*=RowM(Y[i]); |
| 157 |
c*=ColM(Y[i]); |
| 158 |
} |
| 159 |
if (!X) |
| 160 |
{ |
| 161 |
if (!(rtrn=CreateMatrix(r,c))) |
| 162 |
return (TMatrix)NULL; |
| 163 |
} |
| 164 |
else |
| 165 |
if ((r != RowM(X)) || (c != ColM(X))) |
| 166 |
{ |
| 167 |
dw_Error(SIZE_ERR); |
| 168 |
return (TMatrix)NULL; |
| 169 |
} |
| 170 |
else |
| 171 |
rtrn=X; |
| 172 |
if (dw_DimA(Y) > 2) |
| 173 |
{ |
| 174 |
if (!(Z=(PRECISION*)dw_malloc(r*c*sizeof(PRECISION)))) |
| 175 |
{ |
| 176 |
if (!X) FreeMatrix(rtrn); |
| 177 |
return (TMatrix)NULL; |
| 178 |
} |
| 179 |
if (dw_DimA(Y) % 2) |
| 180 |
{ |
| 181 |
U=Z; |
| 182 |
V=pElementM(rtrn); |
| 183 |
} |
| 184 |
else |
| 185 |
{ |
| 186 |
U=pElementM(rtrn); |
| 187 |
V=Z; |
| 188 |
} |
| 189 |
i=dw_DimA(Y)-2; |
| 190 |
bMatrixTensor(U,pElementM(Y[i]),pElementM(Y[i+1]),RowM(Y[i]),ColM(Y[i]),RowM(Y[i+1]), |
| 191 |
ColM(Y[i+1]),MajorForm(rtrn),MajorForm(Y[i]),MajorForm(Y[i+1])); |
| 192 |
r=RowM(Y[i])*RowM(Y[i+1]); |
| 193 |
c=ColM(Y[i])*ColM(Y[i+1]); |
| 194 |
while (--i >= 0) |
| 195 |
{ |
| 196 |
bMatrixTensor(V,pElementM(Y[i]),U,RowM(Y[i]),ColM(Y[i]),r,c,MajorForm(rtrn),MajorForm(Y[i]),MajorForm(rtrn)); |
| 197 |
r*=RowM(Y[i]); |
| 198 |
c*=ColM(Y[i]); |
| 199 |
W=U; |
| 200 |
U=V; |
| 201 |
V=W; |
| 202 |
} |
| 203 |
dw_free(Z); |
| 204 |
} |
| 205 |
else |
| 206 |
if (dw_DimA(Y) > 1) |
| 207 |
bMatrixTensor(pElementM(rtrn),pElementM(Y[0]),pElementM(Y[1]),RowM(Y[0]),ColM(Y[0]),RowM(Y[1]), |
| 208 |
ColM(Y[1]),MajorForm(rtrn),MajorForm(Y[0]),MajorForm(Y[1])); |
| 209 |
else |
| 210 |
EquateMatrix(rtrn,Y[0]); |
| 211 |
return rtrn; |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
Assumes: |
| 216 |
X - d dimensional vector or null pointer |
| 217 |
Y - k dimensional array of vectors |
| 218 |
|
| 219 |
Returns: |
| 220 |
The the tensor product |
| 221 |
|
| 222 |
y[0] x y[1] x ... x y[k-1] |
| 223 |
|
| 224 |
If x is null, then space for the tensor product is allocated. If x is not |
| 225 |
null, then the dimensions must match. |
| 226 |
|
| 227 |
d=DimV(Y[0]) x ... x DimV(Y[k-1]) |
| 228 |
|
| 229 |
Notes: |
| 230 |
Calls bVectorTensor(). |
| 231 |
*/ |
| 232 |
TVector VectorTensor(TVector x, TVector* y) |
| 233 |
{ |
| 234 |
int i, d=1; |
| 235 |
PRECISION *z, *u, *v, *w; |
| 236 |
TVector rtrn; |
| 237 |
if (!y) |
| 238 |
{ |
| 239 |
dw_Error(NULL_ERR); |
| 240 |
return (TVector)NULL; |
| 241 |
} |
| 242 |
for (i=dw_DimA(y)-1; i >= 0; i--) |
| 243 |
if (!y[i]) |
| 244 |
{ |
| 245 |
dw_Error(NULL_ERR); |
| 246 |
return (TVector)NULL; |
| 247 |
} |
| 248 |
else |
| 249 |
d*=DimV(y[i]); |
| 250 |
if (!x) |
| 251 |
{ |
| 252 |
if (!(rtrn=CreateVector(d))) |
| 253 |
return (TVector)NULL; |
| 254 |
} |
| 255 |
else |
| 256 |
if (d != DimV(x)) |
| 257 |
{ |
| 258 |
dw_Error(SIZE_ERR); |
| 259 |
return (TVector)NULL; |
| 260 |
} |
| 261 |
else |
| 262 |
rtrn=x; |
| 263 |
if (dw_DimA(y) > 2) |
| 264 |
{ |
| 265 |
if (!(z=(PRECISION*)dw_malloc(d*sizeof(PRECISION)))) |
| 266 |
{ |
| 267 |
if (!x) FreeVector(rtrn); |
| 268 |
return (TVector)NULL; |
| 269 |
} |
| 270 |
if (dw_DimA(y) % 2) |
| 271 |
{ |
| 272 |
u=z; |
| 273 |
v=pElementV(rtrn); |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 277 |
u=pElementV(rtrn); |
| 278 |
v=z; |
| 279 |
} |
| 280 |
i=dw_DimA(y)-2; |
| 281 |
bVectorTensor(u,pElementV(y[i]),pElementV(y[i+1]),DimV(y[i]),DimV(y[i+1])); |
| 282 |
d=DimV(y[i])*DimV(y[i+1]); |
| 283 |
while (--i >= 0) |
| 284 |
{ |
| 285 |
bVectorTensor(v,pElementV(y[i]),u,DimV(y[i]),d); |
| 286 |
d*=DimV(y[i]); |
| 287 |
w=u; |
| 288 |
u=v; |
| 289 |
v=w; |
| 290 |
} |
| 291 |
dw_free(z); |
| 292 |
} |
| 293 |
else |
| 294 |
if (dw_DimA(y) > 1) |
| 295 |
bVectorTensor(pElementV(rtrn),pElementV(y[0]),pElementV(y[1]),DimV(y[0]),DimV(y[1])); |
| 296 |
else |
| 297 |
EquateVector(rtrn,y[0]); |
| 298 |
return rtrn; |
| 299 |
} |
| 300 |
/******************************************************************************/ |
| 301 |
/******************************************************************************/ |
| 302 |
/******************************************************************************/ |
| 303 |
|
| 304 |
|