51 if (rows == 0 || cols == 0)
57 m->
data = calloc(rows * cols,
sizeof(
double));
67 m->
data = calloc(1,
sizeof(
double));
75 if (rows == 0 || cols == 0)
79 for (
int i = 0; i < rows * cols; i++)
87 if (rows == 0 || cols == 0)
91 for (
int i = 0; i < rows * cols; i++)
92 m->
data[i] = (
double)data[i];
103 for (
int i = 0; i < dim; i++)
114 assert(row < m->nrows);
115 assert(col < m->ncols);
130 assert(row < m->nrows);
131 assert(col < m->ncols);
169 assert(r0 < a->nrows);
170 assert(c0 < a->ncols);
172 int nrows = r1 - r0 + 1;
173 int ncols = c1 - c0 + 1;
177 for (
int row = r0; row <= r1; row++)
178 for (
int col = c0; col <= c1; col++)
193 for (
unsigned int i = 0; i < m->
nrows; i++) {
194 for (
unsigned int j = 0; j < m->
ncols; j++) {
211 for (
unsigned int j = 0; j < m->
ncols; j++) {
212 for (
unsigned int i = 0; i < m->
nrows; i++) {
225 assert(m->
data != NULL);
243 for (
unsigned int i = 0; i < m->
nrows; i++) {
244 for (
unsigned int j = 0; j < m->
ncols; j++) {
246 for (
unsigned int k = 0; k < a->
ncols; k++) {
265 for (
unsigned int i = 0; i < m->
nrows; i++) {
266 for (
unsigned int j = 0; j < m->
ncols; j++) {
283 for (
unsigned int i = 0; i < a->
nrows; i++) {
284 for (
unsigned int j = 0; j < a->
ncols; j++) {
302 for (
unsigned int i = 0; i < m->
nrows; i++) {
303 for (
unsigned int j = 0; j < m->
ncols; j++) {
323 for (
unsigned int i = 0; i < a->
nrows; i++) {
324 for (
unsigned int j = 0; j < a->
ncols; j++) {
343 for (
unsigned int i = 0; i < m->
nrows; i++) {
344 for (
unsigned int j = 0; j < m->
ncols; j++) {
364 for (
unsigned int i = 0; i < a->
nrows; i++) {
365 for (
unsigned int j = 0; j < a->
ncols; j++) {
381 for (
unsigned int i = 0; i < a->
nrows; i++) {
382 for (
unsigned int j = 0; j < a->
ncols; j++) {
399 double detL = 1;
double detU = 1;
400 for (
unsigned int i = 0; i < a->
nrows; i++) {
410 double det = mlu->
pivsign * detL * detU;
428 assert(a->
nrows > 0);
450 double m00 =
MATD_EL(a,0,0), m01 =
MATD_EL(a,0,1), m02 =
MATD_EL(a,0,2), m03 =
MATD_EL(a,0,3);
451 double m10 =
MATD_EL(a,1,0), m11 =
MATD_EL(a,1,1), m12 =
MATD_EL(a,1,2), m13 =
MATD_EL(a,1,3);
452 double m20 =
MATD_EL(a,2,0), m21 =
MATD_EL(a,2,1), m22 =
MATD_EL(a,2,2), m23 =
MATD_EL(a,2,3);
453 double m30 =
MATD_EL(a,3,0), m31 =
MATD_EL(a,3,1), m32 =
MATD_EL(a,3,2), m33 =
MATD_EL(a,3,3);
455 return m00 * m11 * m22 * m33 - m00 * m11 * m23 * m32 -
456 m00 * m21 * m12 * m33 + m00 * m21 * m13 * m32 + m00 * m31 * m12 * m23 -
457 m00 * m31 * m13 * m22 - m10 * m01 * m22 * m33 +
458 m10 * m01 * m23 * m32 + m10 * m21 * m02 * m33 -
459 m10 * m21 * m03 * m32 - m10 * m31 * m02 * m23 +
460 m10 * m31 * m03 * m22 + m20 * m01 * m12 * m33 -
461 m20 * m01 * m13 * m32 - m20 * m11 * m02 * m33 +
462 m20 * m11 * m03 * m32 + m20 * m31 * m02 * m13 -
463 m20 * m31 * m03 * m12 - m30 * m01 * m12 * m23 +
464 m30 * m01 * m13 * m22 + m30 * m11 * m02 * m23 -
465 m30 * m11 * m03 * m22 - m30 * m21 * m02 * m13 +
466 m30 * m21 * m03 * m12;
496 double det = x->
data[0];
500 double invdet = 1.0 / det;
503 MATD_EL(m, 0, 0) = 1.0 * invdet;
512 double invdet = 1.0 / det;
553 while (expr[*pos] != 0) {
555 switch (expr[*pos]) {
560 garb[*garbpos] = res;
571 assert(expr[*pos+1] ==
'-');
572 assert(expr[*pos+2] ==
'1');
575 garb[*garbpos] = res;
594 matd_t **garb,
int *garbpos,
int oneterm)
596 while (expr[*pos] != 0) {
598 switch (expr[*pos]) {
601 if (oneterm && acc != NULL)
611 garb[*garbpos] = res;
637 garb[*garbpos] = res;
646 matd_t *rhs = args[*argpos];
647 garb[*garbpos] = rhs;
659 garb[*garbpos] = res;
668 matd_t *rhs = args[*argpos];
679 garb[*garbpos] = res;
709 const char *start = &expr[*pos];
711 double s = strtod(start, &end);
712 (*pos) += (end - start);
714 garb[*garbpos] = rhs;
723 garb[*garbpos] = res;
732 if (oneterm && acc != NULL)
743 garb[*garbpos] = res;
750 if (oneterm && acc != NULL)
760 garb[*garbpos] = res;
770 garb[*garbpos] = res;
784 debug_print(
"Unknown character: '%c'\n", expr[*pos]);
785 assert(expr[*pos] != expr[*pos]);
798 assert(expr != NULL);
800 for (
const char *p = expr; *p != 0; p++) {
801 if (*p ==
'M' || *p ==
'F')
815 for (
int i = 0; i < nargs; i++) {
816 args[i] = va_arg(ap,
matd_t*);
837 for (
int i = 0; i < garbpos; i++) {
852 for (
int i = 0; i < len; i++)
877 assert(n <= lena && n <= lenb);
882 for (
int i = 0; i < n; i++)
893 for (
int i = 0; i < maxcol; i++) {
896 double v = fabs(
MATD_EL(A, row, i));
913 assert(adim == bdim);
917 for (
int i = 0; i < adim; i++) {
935 for(
int i = 0; i < len; i++)
963 for (
unsigned int i = 0; i < a->
nrows; i++) {
964 for (
unsigned int j = 0; j < a->
ncols; j++) {
968 TYPE err = fabs(av - bv);
969 maxf = fmax(maxf, err);
1008 for (
unsigned int hhidx = 0; hhidx < A->
nrows; hhidx++) {
1010 if (hhidx < A->ncols) {
1028 int vlen = A->
nrows - hhidx;
1030 double *v = malloc(
sizeof(
double)*vlen);
1033 for (
int i = 0; i < vlen; i++) {
1034 v[i] =
MATD_EL(B, hhidx+i, hhidx);
1038 double oldv0 = v[0];
1044 mag2 += -oldv0*oldv0 + v[0]*v[0];
1047 double mag = sqrt(mag2);
1055 for (
int i = 0; i < vlen; i++)
1068 for (
unsigned int i = 0; i < LS->
nrows; i++) {
1070 for (
int j = 0; j < vlen; j++)
1071 dot +=
MATD_EL(LS, i, hhidx+j) * v[j];
1072 for (
int j = 0; j < vlen; j++)
1073 MATD_EL(LS, i, hhidx+j) -= 2*dot*v[j];
1077 for (
unsigned int i = 0; i < B->
ncols; i++) {
1079 for (
int j = 0; j < vlen; j++)
1080 dot +=
MATD_EL(B, hhidx+j, i) * v[j];
1081 for (
int j = 0; j < vlen; j++)
1082 MATD_EL(B, hhidx+j, i) -= 2*dot*v[j];
1088 if (hhidx+2 < A->
ncols) {
1089 int vlen = A->
ncols - hhidx - 1;
1091 double *v = malloc(
sizeof(
double)*vlen);
1094 for (
int i = 0; i < vlen; i++) {
1095 v[i] =
MATD_EL(B, hhidx, hhidx+i+1);
1099 double oldv0 = v[0];
1105 mag2 += -oldv0*oldv0 + v[0]*v[0];
1108 double mag = sqrt(mag2);
1116 for (
int i = 0; i < vlen; i++)
1126 for (
unsigned int i = 0; i < RS->
nrows; i++) {
1128 for (
int j = 0; j < vlen; j++)
1129 dot +=
MATD_EL(RS, i, hhidx+1+j) * v[j];
1130 for (
int j = 0; j < vlen; j++)
1131 MATD_EL(RS, i, hhidx+1+j) -= 2*dot*v[j];
1135 for (
unsigned int i = 0; i < B->
nrows; i++) {
1137 for (
int j = 0; j < vlen; j++)
1138 dot +=
MATD_EL(B, i, hhidx+1+j) * v[j];
1139 for (
int j = 0; j < vlen; j++)
1140 MATD_EL(B, i, hhidx+1+j) -= 2*dot*v[j];
1150 int maxiters = 200 + 2 * A->
nrows * A->
ncols;
1151 assert(maxiters > 0);
1160 const int find_max_method = 1;
1164 unsigned int *maxrowidx = malloc(
sizeof(
int)*B->
ncols);
1165 unsigned int lastmaxi, lastmaxj;
1167 if (find_max_method == 1) {
1168 for (
unsigned int i = 2; i < B->
ncols; i++)
1174 lastmaxi = 0, lastmaxj = 1;
1177 for (iter = 0; iter < maxiters; iter++) {
1187 if (find_max_method == 1) {
1201 for (
unsigned int rowi = 0; rowi < B->
ncols; rowi++) {
1209 if (rowi == lastmaxi || rowi == lastmaxj) {
1211 thismaxv = fabs(
MATD_EL(B, rowi, maxrowidx[rowi]));
1219 if (maxrowidx[rowi] == lastmaxi || maxrowidx[rowi] == lastmaxj) {
1221 thismaxv = fabs(
MATD_EL(B, rowi, maxrowidx[rowi]));
1231 thismaxv = fabs(
MATD_EL(B, rowi, maxrowidx[rowi]));
1234 if (lastmaxi != rowi) {
1235 double v = fabs(
MATD_EL(B, rowi, lastmaxi));
1238 maxrowidx[rowi] = lastmaxi;
1243 if (lastmaxj != rowi) {
1244 double v = fabs(
MATD_EL(B, rowi, lastmaxj));
1247 maxrowidx[rowi] = lastmaxj;
1253 if (thismaxv > maxv) {
1260 maxj = maxrowidx[maxi];
1269 }
else if (find_max_method == 2) {
1274 for (
unsigned int i = 0; i < B->
ncols; i++) {
1275 for (
unsigned int j = 0; j < B->
ncols; j++) {
1279 double v = fabs(
MATD_EL(B, i, j));
1301 double A0 =
MATD_EL(B, maxi, maxi);
1302 double A1 =
MATD_EL(B, maxi, maxj);
1303 double A2 =
MATD_EL(B, maxj, maxi);
1304 double A3 =
MATD_EL(B, maxj, maxj);
1313 double U[4], S[2], V[4];
1340 for (
unsigned int i = 0; i < LS->
nrows; i++) {
1341 double vi =
MATD_EL(LS, i, maxi);
1342 double vj =
MATD_EL(LS, i, maxj);
1344 MATD_EL(LS, i, maxi) = U[0]*vi + U[2]*vj;
1345 MATD_EL(LS, i, maxj) = U[1]*vi + U[3]*vj;
1349 for (
unsigned int i = 0; i < RS->
nrows; i++) {
1350 double vi =
MATD_EL(RS, i, maxi);
1351 double vj =
MATD_EL(RS, i, maxj);
1353 MATD_EL(RS, i, maxi) = V[0]*vi + V[2]*vj;
1354 MATD_EL(RS, i, maxj) = V[1]*vi + V[3]*vj;
1359 for (
unsigned int i = 0; i < B->
ncols; i++) {
1360 double vi =
MATD_EL(B, maxi, i);
1361 double vj =
MATD_EL(B, maxj, i);
1363 MATD_EL(B, maxi, i) = U[0]*vi + U[2]*vj;
1364 MATD_EL(B, maxj, i) = U[1]*vi + U[3]*vj;
1368 for (
unsigned int i = 0; i < B->
nrows; i++) {
1369 double vi =
MATD_EL(B, i, maxi);
1370 double vj =
MATD_EL(B, i, maxj);
1372 MATD_EL(B, i, maxi) = V[0]*vi + V[2]*vj;
1373 MATD_EL(B, i, maxj) = V[1]*vi + V[3]*vj;
1381 debug_print(
"WARNING: maximum iters (maximum = %d, matrix %d x %d, max=%.15f)\n",
1389 int *idxs = malloc(
sizeof(
int)*A->
ncols);
1390 double *vals = malloc(
sizeof(
double)*A->
ncols);
1391 for (
unsigned int i = 0; i < A->
ncols; i++) {
1401 for (
unsigned int i = 0; i + 1 < A->
ncols; i++) {
1402 if (fabs(vals[i+1]) > fabs(vals[i])) {
1404 idxs[i] = idxs[i+1];
1407 double tmpv = vals[i];
1408 vals[i] = vals[i+1];
1419 for (
unsigned int i = 0; i < A->
ncols; i++) {
1420 MATD_EL(LP, idxs[i], idxs[i]) = 0;
1421 MATD_EL(RP, idxs[i], idxs[i]) = 0;
1423 MATD_EL(LP, idxs[i], i) = vals[i] < 0 ? -1 : 1;
1433 B =
matd_op(
"M'*F*M", LP, B, RP);
1443 memset(&res, 0,
sizeof(res));
1447 for (
unsigned int i = 0; i < B->
nrows; i++) {
1448 for (
unsigned int j = 0; j < B->
ncols; j++) {
1480 memset(&res, 0,
sizeof(res));
1516 unsigned int *piv = calloc(a->
nrows,
sizeof(
unsigned int));
1525 for (
unsigned int i = 0; i < a->
nrows; i++)
1528 for (
unsigned int j = 0; j < a->
ncols; j++) {
1529 for (
unsigned int i = 0; i < a->
nrows; i++) {
1530 int kmax = i < j ? i : j;
1534 for (
int k = 0; k < kmax; k++)
1543 for (
unsigned int i = j+1; i < lu->
nrows; i++) {
1563 double LUjj =
MATD_EL(lu, j, j);
1580 if (j < lu->ncols && j < lu->nrows && LUjj != 0) {
1582 for (
unsigned int i = j+1; i < lu->
nrows; i++)
1608 for (
unsigned int i = 0; i < lu->
ncols; i++)
1620 for (
unsigned int i = 0; i < lu->
nrows; i++) {
1632 for (
unsigned int i = 0; i < lu->
nrows; i++) {
1635 for (
unsigned int j = 0; j < i; j++) {
1648 for (
unsigned int i = 0; i < lu->
ncols; i++) {
1649 for (
unsigned int j = 0; j < lu->
ncols; j++) {
1667 for (
unsigned int i = 0; i < mlu->
lu->
nrows; i++)
1671 for (
unsigned int k = 0; k < mlu->
lu->
nrows; k++) {
1672 for (
unsigned int i = k+1; i < mlu->
lu->
nrows; i++) {
1674 for (
unsigned int t = 0; t < b->
ncols; t++)
1680 for (
int k = mlu->
lu->
ncols-1; k >= 0; k--) {
1681 double LUkk = 1.0 /
MATD_EL(mlu->
lu, k, k);
1682 for (
unsigned int t = 0; t < b->
ncols; t++)
1685 for (
int i = 0; i < k; i++) {
1687 for (
unsigned int t = 0; t < b->
ncols; t++)
1708 int v = random()&31;
1713 static double randf()
1715 double v = 1.0 *random() / RAND_MAX;
1719 int main(
int argc,
char *argv[])
1725 for (
int iter = 0; 1; iter++) {
1728 if (iter % 1000 == 0)
1729 printf(
"%d\n", iter);
1731 int m = 1 + (random()%(maxdim-1));
1732 int n = 1 + (random()%(maxdim-1));
1734 for (
int i = 0; i < m*n; i++)
1735 A->
data[i] = randi();
1773 for (
int iter = 0; 1; iter++) {
1774 if (iter % 100000 == 0)
1775 printf(
"%d\n", iter);
1782 matd_svd22_impl(A->
data, &s);
1784 memcpy(U->
data, s.U, 4*
sizeof(
double));
1785 memcpy(V->
data, s.V, 4*
sizeof(
double));
1789 assert(s.S[0] >= s.S[1]);
1790 assert(s.S[0] >= 0);
1791 assert(s.S[1] >= 0);
1803 for (
int i = 0; i < 4; i++)
1804 maxerr = fmax(maxerr, fabs(USV->
data[i] - A->
data[i]));
1807 printf(
"------------------------------------\n");
1810 printf(
"\nUSV':\n");
1812 printf(
"maxerr: %.15f\n", maxerr);
1818 assert(maxerr < 0.00001);
1872 for (
int i = 0; i < N; i++) {
1880 for (
int j = i; j < N; j++)
1883 for (
int j = i+1; j < N; j++) {
1889 for (
int k = j; k < N; k++) {
1911 memcpy(x, b, n*
sizeof(
TYPE));
1912 for (
int i = 0; i < n; i++) {
1915 for (
unsigned int j = i+1; j < u->
ncols; j++) {
1916 x[j] -= x[i] *
MATD_EL(u, i, j);
1926 for (
int i = 0; i < n; i++) {
1929 for (
int j = 0; j < i; j++) {
1933 x[i] = acc /
MATD_EL(L, i, i);
1940 for (
int i = u->
ncols-1; i >= 0; i--) {
1943 double diag =
MATD_EL(u, i, i);
1945 for (
unsigned int j = i+1; j < u->
ncols; j++)
1962 for (
unsigned int i = 0; i < u->
nrows; i++) {
1963 for (
unsigned int j = 0; j < i; j++) {
1967 for (
unsigned int k = 0; k < b->
ncols; k++) {
1972 for (
unsigned int k = 0; k < b->
ncols; k++) {
1978 for (
int k = u->
ncols-1; k >= 0; k--) {
1979 double LUkk = 1.0 /
MATD_EL(u, k, k);
1980 for (
unsigned int t = 0; t < b->
ncols; t++)
1983 for (
int i = 0; i < k; i++) {
1984 double LUik = -
MATD_EL(u, i, k);
1985 for (
unsigned int t = 0; t < b->
ncols; t++)
2020 double d = -DBL_MAX;
2021 for(
unsigned int x=0; x<m->
nrows; x++) {
2022 for(
unsigned int y=0; y<m->
ncols; y++) {