Low-level SVD functionality. Useful for applications that do not want to construct a Matrix but need to perform the SVD operation.
1506 const int MAX_ITERATIONS = 30;
1508 int flag, i, its, j, jj, k, l = 0, nm = 0;
1509 double c, f, h, s,
x,
y,
z;
1510 double anorm = 0.0, g = 0.0, scale = 0.0;
1521 for (i = 0; i <
cols; ++i) {
1526 g = s = scale = 0.0;
1530 for (k = i; k <
rows; ++k) {
1531 scale += fabs((
double)U[k][i]);
1535 for (k = i; k <
rows; ++k) {
1536 U[k][i] = (float)((
double)U[k][i]/scale);
1537 s += ((double)U[k][i] * (
double)U[k][i]);
1540 f = (double)U[i][i];
1542 g = -
sign(f)*(sqrt(s));
1544 U[i][i] = (float)(f - g);
1546 if (i != cols - 1) {
1547 for (j = l; j <
cols; ++j) {
1549 for (s = 0.0, k = i; k <
rows; ++k) {
1550 s += ((double)U[k][i] * (
double)U[k][j]);
1554 for (k = i; k <
rows; ++k) {
1555 U[k][j] += (float)(f * (
double)U[k][i]);
1559 for (k = i; k <
rows; ++k) {
1560 U[k][i] = (float)((
double)U[k][i]*scale);
1564 D[i] = (float)(scale * g);
1567 g = s = scale = 0.0;
1568 if (i < rows && i != cols - 1) {
1569 for (k = l; k <
cols; ++k) {
1570 scale += fabs((
double)U[i][k]);
1574 for (k = l; k <
cols; ++k) {
1575 U[i][k] = (float)((
double)U[i][k]/scale);
1576 s += ((double)U[i][k] * (
double)U[i][k]);
1579 f = (double)U[i][l];
1580 g = -
SIGN(sqrt(s), f);
1582 U[i][l] = (float)(f - g);
1584 for (k = l; k <
cols; ++k) {
1585 rv1[k] = (double)U[i][k] / h;
1588 if (i != rows - 1) {
1590 for (j = l; j <
rows; ++j) {
1591 for (s = 0.0, k = l; k <
cols; ++k) {
1592 s += ((double)U[j][k] * (
double)U[i][k]);
1595 for (k = l; k <
cols; ++k) {
1596 U[j][k] += (float)(s * rv1[k]);
1601 for (k = l; k <
cols; ++k) {
1602 U[i][k] = (float)((
double)U[i][k]*scale);
1607 anorm =
max(anorm, fabs((
double)D[i]) + fabs(rv1[i]));
1611 for (i = cols - 1; i >= 0; --i) {
1614 for (j = l; j <
cols; ++j) {
1615 V[j][i] = (float)(((
double)U[i][j] / (
double)U[i][l]) / g);
1619 for (j = l; j <
cols; ++j) {
1620 for (s = 0.0, k = l; k <
cols; ++k) {
1621 s += ((double)U[i][k] * (
double)V[k][j]);
1624 for (k = l; k <
cols; ++k) {
1625 V[k][j] += (float)(s * (
double)V[k][i]);
1630 for (j = l; j <
cols; ++j) {
1631 V[i][j] = V[j][i] = 0.0;
1641 for (i = cols - 1; i >= 0; --i) {
1645 for (j = l; j <
cols; ++j) {
1652 if (i != cols - 1) {
1653 for (j = l; j <
cols; ++j) {
1654 for (s = 0.0, k = l; k <
rows; ++k) {
1655 s += ((double)U[k][i] * (
double)U[k][j]);
1658 f = (s / (double)U[i][i]) * g;
1660 for (k = i; k <
rows; ++k) {
1661 U[k][j] += (float)(f * (
double)U[k][i]);
1666 for (j = i; j <
rows; ++j) {
1667 U[j][i] = (float)((
double)U[j][i]*g);
1671 for (j = i; j <
rows; ++j) {
1679 for (k = cols - 1; k >= 0; --k) {
1681 for (its = 0; its < MAX_ITERATIONS; ++its) {
1685 for (l = k; l >= 0; --l) {
1688 if (fabs(rv1[l]) + anorm == anorm) {
1693 if (fabs((
double)D[nm]) + anorm == anorm) {
1701 for (i = l; i <= k; ++i) {
1703 if (fabs(f) + anorm != anorm) {
1710 for (j = 0; j <
rows; ++j) {
1711 y = (double)U[j][nm];
1712 z = (double)U[j][i];
1713 U[j][nm] = (float)(y * c + z * s);
1714 U[j][i] = (float)(z * c - y * s);
1727 for (j = 0; j <
cols; ++j) {
1728 V[j][k] = (-V[j][k]);
1734 if (its >= MAX_ITERATIONS) {
1737 return "Failed to converge.";
1746 f = ((y -
z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
1748 f = ((x -
z) * (x + z) + h * ((y / (f +
SIGN(g, f))) - h)) /
x;
1752 for (j = l; j <= nm; ++j) {
1767 for (jj = 0; jj <
cols; ++jj) {
1768 x = (double)V[jj][j];
1769 z = (double)V[jj][i];
1770 V[jj][j] = (float)(x * c + z * s);
1771 V[jj][i] = (float)(z * c - x * s);
1780 f = (c * g) + (s * y);
1781 x = (c *
y) - (s * g);
1782 for (jj = 0; jj <
rows; ++jj) {
1783 y = (double)U[jj][j];
1784 z = (double)U[jj][i];
1785 U[jj][j] = (float)(y * c + z * s);
1786 U[jj][i] = (float)(z * c - y * s);
int rows() const
Definition: Matrix.h:324
arena_t NULL
Definition: jemalloc_internal.h:624
static void alignedFree(void *ptr)
Definition: System.cpp:1546
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
T max(const T &x, const T &y)
Definition: g3dmath.h:320
#define debugAssert(exp)
Definition: debugAssert.h:160
G3D::int16 z
Definition: Vector3int16.h:46
G3D::int16 y
Definition: Vector2int16.h:38
static void * alignedMalloc(size_t bytes, size_t alignment)
Definition: System.cpp:1482
int cols() const
Definition: Matrix.h:329
#define SIGN(a, b)
Definition: Matrix.cpp:1503
G3D::int16 x
Definition: Vector2int16.h:37
double sign(double fValue)
Definition: g3dmath.h:669
static double pythag(double a, double b)
Definition: Matrix.cpp:1486