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

SCM Repository

[diderot] View of /branches/pure-cfg/src/lib/c-target/eigen2D.c
ViewVC logotype

View of /branches/pure-cfg/src/lib/c-target/eigen2D.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1540 - (download) (as text) (annotate)
Mon Oct 17 12:40:09 2011 UTC (8 years ago) by glk
File size: 5228 byte(s)
for symmetric 2x2 eigensolve
/*! \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

#ifdef DIDEROT_DOUBLE_PRECISION
# define SQRT(x)	sqrt(x)
# define VEC(a,b)       vec2d(a,b)
# define DOT(u,v)       dot2d(u,v)
# define SCALE(s,v)     scale2d(s,v)
# define NORMALIZE(v)   normalize2d(v)
#else
# define SQRT(x)	sqrtf(x)
# define VEC(a,b)       vec2f(a,b)
# define DOT(u,v)       dot2f(u,v)
# define SCALE(s,v)     scale2f(s,v)
# define NORMALIZE(v)   normalize2f(v)
#endif
#define SUB(v,i)       (((Diderot_union3_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_evals2D (
	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;
// FIXME: probably want a different value of epsilon for single-precision
    Diderot_real_t epsilon = 1.0E-12;
    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_evals_evecs2D (
	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;
// FIXME: probably want a different value of epsilon for single-precision
    Diderot_real_t epsilon = 1.0E-12;
    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 = VEC(M00 - vv, M01);
      r2 = VEC(M01, M11 - vv);
      if (DOT(r1,r2) > 0.0) {
        evec[1] = VEC(r1[0]+r2[0], r1[1]+r2[1]);
      } else {
        evec[1] = VEC(r1[0]-r2[0], r1[1]-r2[1]);
      }
      evec[1] = NORMALIZE(evec[1]);
      evec[0] = VEC(evec[1][1], -evec[1][0]);
      evec[0] = NORMALIZE(evec[0]);
      roots = ROOT_TWO;
    } else {
      /* double root */
      eval[0] = eval[1] = 0.0;
      /* use any basis for eigenvectors */
      evec[0] = VEC(1.0, 0.0);
      evec[1] = VEC(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