Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/lib/cpu-target/eigen.cxx
ViewVC logotype

View of /branches/vis15/src/lib/cpu-target/eigen.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3875 - (download) (as text) (annotate)
Wed May 18 17:06:02 2016 UTC (2 years, 10 months ago) by jhr
File size: 4549 byte(s)
  fix include filename
/*! \file eigen.cxx
 *
 * \author John Reppy
 */

/*
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 */

/*
  Teem: Tools to process and visualize scientific data and images
  Copyright (C) 2011, 2010, 2009, University of Chicago
  Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
  Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public License
  (LGPL) as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  The terms of redistributing and/or modifying this software also
  include exceptions to the LGPL that facilitate static linking.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this library; if not, write to Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <diderot/diderot.hxx>

namespace diderot {

#define ROOT_DOUBLE 1
#define ROOT_TWO    2
#define EPSILON	    REAL(1.0E-12)

template <typename REAL>
inline void _normalize2 (vec2<REAL> v)
{
    REAL s = std::sqrt (v[0]*v[0] + v[1]*v[1]);
    if (s > EPSILON) {
	s = REAL(1.0) / s;
	v[0] *= s;
	v[1] *= s;
    }
}

/*
** Eigensolver for symmetric 2x2 matrix:
**
**  M00  M01
**  M01  M11
**
** Return value indicates something about the eigenvalue solution to
** the quadratic characteristic equation; see ROOT_ #defines above
**
** HEY: the numerical precision issues here merit some more scrutiny.
*/
template <typename REAL>
int eigenvals (mat2x2<REAL>, REAL eval[2])
{
    REAL mean, Q, D, M00, M01, M11;
    int roots;

  /* copy the given matrix elements */
    M00 = mat[0];
    M01 = mat[1];
    M11 = mat[3];

  /*
  ** subtract out the eigenvalue mean (will add back to evals later);
  ** helps with numerical stability
  */
    mean = (M00 + M11)/REAL(2.0);
    M00 -= mean;
    M11 -= mean;

    Q = M00 - M11;
    D = REAL(4.0)*M01*M01 + Q*Q;
    if (D > EPSILON) {
      /* two distinct roots */
        REAL vv;
        vv = SQRT(D)/REAL(2.0);
        eval[0] = vv;
        eval[1] = -vv;
        roots = ROOT_TWO;
    }
    else {
      /* double root */
      eval[0] = eval[1] = REAL(0.0);
      roots = ROOT_DOUBLE;
    }

  /* add back in the eigenvalue mean */
    eval[0] += mean;
    eval[1] += mean;

    return roots;
}

template <typename REAL>
int eigenvecs (mat2x2<REAL> mat, REAL eval[2], vec2<REAL> evec[2])
{
    REAL mean, Q, D, M00, M01, M11;
    int roots;

  /* copy the given matrix elements */
    M00 = mat[0];
    M01 = mat[1];
    M11 = mat[3];

  /*
  ** subtract out the eigenvalue mean (will add back to evals later);
  ** helps with numerical stability
  */
    mean = (M00 + M11)/REAL(2.0);
    M00 -= mean;
    M11 -= mean;

    Q = M00 - M11;
    D = REAL(4.0)*M01*M01 + Q*Q;
    if (D > EPSILON) {
      /* two distinct roots */
        REAL vv;
        REAL r1[2], r2[2];
        REAL vv = std::sqrt(D) / REAL(2.0);
        eval[0] = vv;
        eval[1] = -vv;
      /* null space of T = M - evec[0]*I ==
         [M00 - vv      M01  ]
         [  M01      M11 - vv]
         is evec[0], but we know evec[0] and evec[1] are orthogonal,
         so row span of T is evec[1]
      */
        REAL r1[2] = { M00 - vv, = M01 };
        REAL r2[2] = { M01, M11 - vv };
        if ((r1[0]*r2[0] + r1[1]*r2[1]) > REAL(0.0)) {
            evec[1][0] = r1[0] + r2[0];
	    evec[1][1] = r1[1] + r2[1];
        }
        else {
            evec[1][0] = r1[0] - r2[0];
	    evec[1][1] = r1[1] - r2[1];
        }
        _normalize2 (evec[1]);
        evec[0][0] = evec[1][1];
	evec[0][1] = -evec[1][0];
        _normalize2 (evec[0]);
        roots = ROOT_TWO;
    }
    else {
      /* double root */
        eval[0] = eval[1] = REAL(0.0);
      /* use any basis for eigenvectors */
        evec[0][0] = REAL(1.0);
	evec[0][1] = REAL(0.0);
        evec[1][0] = REAL(0.0);
	evec[1][1] = REAL(1.0);
        roots = ROOT_DOUBLE;
    }

    /* add back in the eigenvalue mean */
    eval[0] += mean;
    eval[1] += mean;

    return roots;
}

} // namespace diderot

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0