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

SCM Repository

[diderot] View of /branches/vis12/src/lib/common/eigen2x2.c
ViewVC logotype

View of /branches/vis12/src/lib/common/eigen2x2.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1685 - (download) (as text) (annotate)
Sun Jan 22 15:23:36 2012 UTC (9 years, 6 months ago) by jhr
File size: 4736 byte(s)
  Create a branch to implement things that we need for the Vis 2012 paper
/*! \file eigen2D.c
 *
 * \author Gordon Kindlmann
 */

/*
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * 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.h>

#define ROOT_DOUBLE 1
#define ROOT_TWO    2

#define SUB(v,i)       (((Diderot_union2_t)(v)).r[i])

// OpenCL 1.0 does not specify the C representation of the host types
// for OpenCL vector types (e.g., cl_float4), so we have to handle this
// mechanism with a macro.
#if defined(CL_HOST_VECTORS_ARE_STRUCTS)
#  define VSUB(v,i)        (v).s[i]
#elif defined(CL_HOST_VECTORS_ARE_ARRAYS)
#  define VSUB(v,i)        (v)[i]
#else
# error unable to access elements of host vectors
#endif

/*
** 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.
*/
int Diderot_evals2x2 (
    Diderot_real_t eval[2],
    const Diderot_real_t _M00, const Diderot_real_t _M01,
    const Diderot_real_t _M11)
{
    Diderot_real_t mean, Q, D, M00, M01, M11;
    int roots;

  /* copy the given matrix elements */
    M00 = _M00;
    M01 = _M01;
    M11 = _M11;

  /*
  ** subtract out the eigenvalue mean (will add back to evals later);
  ** helps with numerical stability
  */
    mean = (M00 + M11)/2.0;
    M00 -= mean;
    M11 -= mean;
  
    Q = M00 - M11;
    D = 4.0*M01*M01 + Q*Q;
    if (D > EPSILON) {
      /* two distinct roots */
        Diderot_real_t vv;
        vv = SQRT(D)/2.0;
        eval[0] = vv;
        eval[1] = -vv;
        roots = ROOT_TWO;
    }
    else {
      /* double root */
      eval[0] = eval[1] = 0.0;
      roots = ROOT_DOUBLE;
    }

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

    return roots;
}

int Diderot_evecs2x2 (
    Diderot_real_t eval[2], Diderot_vec2_t evec[2],
    const Diderot_real_t _M00, const Diderot_real_t _M01,
    const Diderot_real_t _M11)
{
    Diderot_real_t mean, Q, D, M00, M01, M11;
    int roots;

  /* copy the given matrix elements */
    M00 = _M00;
    M01 = _M01;
    M11 = _M11;

  /*
  ** subtract out the eigenvalue mean (will add back to evals later);
  ** helps with numerical stability
  */
    mean = (M00 + M11)/2.0;
    M00 -= mean;
    M11 -= mean;
  
    Q = M00 - M11;
    D = 4.0*M01*M01 + Q*Q;
    if (D > EPSILON) {
      /* two distinct roots */
        Diderot_real_t vv;
        Diderot_vec2_t r1, r2;
        vv = SQRT(D)/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]
      */
        r1 = vec2(M00 - vv, M01);
        r2 = vec2(M01, M11 - vv);
        if (dot2(r1,r2) > 0.0) {
            evec[1] = vec2(SUB(r1,0)+SUB(r2,0), SUB(r1,1)+SUB(r2,1));
        }
        else {
            evec[1] = vec2(SUB(r1,0)-SUB(r2,0), SUB(r1,1)-SUB(r2,1));
        }
        evec[1] = normalize2(evec[1]);
        evec[0] = vec2(SUB(evec[1],1), -SUB(evec[1],0));
        evec[0] = normalize2(evec[0]);
        roots = ROOT_TWO;
    }
    else {
      /* double root */
        eval[0] = eval[1] = 0.0;
      /* use any basis for eigenvectors */
        evec[0] = vec2(1.0, 0.0);
        evec[1] = vec2(0.0, 1.0);
        roots = ROOT_DOUBLE;
    }

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

    return roots;
}

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