RTOp_ROp_norms.c

Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00006 //                  Copyright (2003) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //  
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #include "RTOp_ROp_norms.h"
00032 #include "RTOp_obj_null_vtbl.h"
00033 #include "RTOp_obj_value_vtbl.h"
00034 #include "RTOp_reduct_sum_value.h"
00035 #include "RTOp_reduct_max_value.h"
00036 
00037 #define MY_MAX(a,b) a > b ? a : b
00038 
00039 /* One norm reduction operator class. */
00040 
00041 static int RTOp_ROp_norms_apply_op_norm_1(
00042   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00043   , const int num_vecs, const struct RTOp_SubVector vecs[]
00044   , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00045   , RTOp_ReductTarget targ_obj )
00046 {
00047   RTOp_index_type        sub_dim;
00048   const RTOp_value_type  *v0_val;
00049   ptrdiff_t              v0_val_s;
00050   register RTOp_index_type k;
00051   RTOp_value_type        *norm = NULL;
00052 
00053   /* */
00054   /* Validate the input */
00055   /* */
00056   if( num_vecs != 1 )
00057     return RTOp_ERR_INVALID_NUM_VECS;
00058   if( num_targ_vecs != 0 )
00059     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00060   assert(targ_obj);
00061   assert(vecs);
00062 
00063   /* */
00064   /* Get pointers to data */
00065   /* */
00066 
00067   /* v0 */
00068   sub_dim        = vecs[0].sub_dim;
00069   v0_val         = vecs[0].values;
00070   v0_val_s       = vecs[0].values_stride;
00071 
00072   /* */
00073   /* Perform the reduction */
00074   /* */
00075   norm = (RTOp_value_type*)targ_obj;
00076   for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s ) {
00077     *norm += fabs(*v0_val);  /* ||v[0]||_1 */
00078   }
00079 
00080   return 0; /* success? */
00081 }
00082 
00083 const struct RTOp_RTOp_vtbl_t RTOp_ROp_norm_1_vtbl =
00084 {
00085   &RTOp_obj_null_vtbl   /* use null type for instance data */
00086   ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */
00087   ,"RTOp_ROp_norm_1"
00088   ,NULL
00089   ,RTOp_ROp_norms_apply_op_norm_1
00090   ,RTOp_reduct_sum_value
00091   ,RTOp_get_reduct_sum_value_op
00092 };
00093 
00094 int RTOp_ROp_norm_1_construct( struct RTOp_RTOp* op )
00095 {
00096   op->vtbl     = &RTOp_ROp_norm_1_vtbl;
00097   op->obj_data = NULL;
00098   return 0;
00099 }
00100 
00101 RTOp_value_type RTOp_ROp_norm_1_val(RTOp_ReductTarget targ_obj)
00102 {
00103 #ifdef RTOp_DEBUG
00104   assert(targ_obj != RTOp_REDUCT_OBJ_NULL );
00105 #endif
00106   return *(RTOp_value_type*)targ_obj;
00107 }
00108 
00109 /* Two norm reduction operator class. */
00110 
00111 static int RTOp_ROp_norms_apply_op_norm_2(
00112   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00113   , const int num_vecs, const struct RTOp_SubVector vecs[]
00114   , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00115   , RTOp_ReductTarget targ_obj )
00116 {
00117   RTOp_index_type        sub_dim;
00118   const RTOp_value_type  *v0_val;
00119   ptrdiff_t              v0_val_s;
00120   register RTOp_index_type k;
00121   RTOp_value_type        *norm = NULL;
00122 
00123   /* */
00124   /* Validate the input */
00125   /* */
00126   if( num_vecs != 1 )
00127     return RTOp_ERR_INVALID_NUM_VECS;
00128   if( num_targ_vecs != 0 )
00129     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00130   assert(targ_obj);
00131   assert(vecs);
00132 
00133   /* */
00134   /* Get pointers to data */
00135   /* */
00136 
00137   /* v0 */
00138   sub_dim        = vecs[0].sub_dim;
00139   v0_val         = vecs[0].values;
00140   v0_val_s       = vecs[0].values_stride;
00141 
00142   /* */
00143   /* Perform the reduction */
00144   /* */
00145   norm = (RTOp_value_type*)targ_obj;
00146   for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s )
00147     *norm += (*v0_val)*(*v0_val);  /* (||v[0]||_2)^2 */
00148 
00149   return 0; /* success? */
00150 }
00151 
00152 const struct RTOp_RTOp_vtbl_t RTOp_ROp_norm_2_vtbl =
00153 {
00154   &RTOp_obj_null_vtbl   /* use null type for instance data */
00155   ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */
00156   ,"RTOp_ROp_norm_2"
00157   ,NULL
00158   ,RTOp_ROp_norms_apply_op_norm_2
00159   ,RTOp_reduct_sum_value
00160   ,RTOp_get_reduct_sum_value_op
00161 };
00162 
00163 int RTOp_ROp_norm_2_construct( struct RTOp_RTOp* op )
00164 {
00165   op->vtbl     = &RTOp_ROp_norm_2_vtbl;
00166   op->obj_data = NULL;
00167   return 0;
00168 }
00169 
00170 RTOp_value_type RTOp_ROp_norm_2_val(RTOp_ReductTarget targ_obj)
00171 {
00172 #ifdef RTOp_DEBUG
00173   assert(targ_obj != RTOp_REDUCT_OBJ_NULL );
00174 #endif
00175   return sqrt(*(RTOp_value_type*)targ_obj);
00176 }
00177 
00178 /* Infinity norm reduction operator class. */
00179 
00180 static int RTOp_ROp_norms_apply_op_norm_inf(
00181   const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
00182   , const int num_vecs, const struct RTOp_SubVector vecs[]
00183   , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
00184   , RTOp_ReductTarget targ_obj )
00185 {
00186   RTOp_index_type        sub_dim;
00187   const RTOp_value_type  *v0_val;
00188   ptrdiff_t              v0_val_s;
00189   register RTOp_index_type k;
00190   RTOp_value_type        *norm = NULL;
00191 
00192   /* */
00193   /* Validate the input */
00194   /* */
00195   if( num_vecs != 1 )
00196     return RTOp_ERR_INVALID_NUM_VECS;
00197   if( num_targ_vecs != 0 )
00198     return RTOp_ERR_INVALID_NUM_TARG_VECS;
00199   assert(targ_obj);
00200   assert(vecs);
00201 
00202   /* */
00203   /* Get pointers to data */
00204   /* */
00205 
00206   /* v0 */
00207   sub_dim        = vecs[0].sub_dim;
00208   v0_val         = vecs[0].values;
00209   v0_val_s       = vecs[0].values_stride;
00210 
00211   /* */
00212   /* Perform the reduction */
00213   /* */
00214   norm = (RTOp_value_type*)targ_obj;
00215   for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s )
00216     *norm = MY_MAX( fabs(*v0_val), (*(RTOp_value_type*)targ_obj) );  /* ||v[0]||_inf */
00217 
00218   return 0; /* success? */
00219 }
00220 
00221 const struct RTOp_RTOp_vtbl_t RTOp_ROp_norm_inf_vtbl =
00222 {
00223   &RTOp_obj_null_vtbl  /* use null type for instance data */
00224   ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */
00225   ,"RTOp_ROp_norm_inf"
00226   ,NULL
00227   ,RTOp_ROp_norms_apply_op_norm_inf
00228   ,RTOp_reduct_max_value
00229   ,RTOp_get_reduct_max_value_op
00230 };
00231 
00232 int RTOp_ROp_norm_inf_construct( struct RTOp_RTOp* op )
00233 {
00234   op->vtbl     = &RTOp_ROp_norm_inf_vtbl;
00235   op->obj_data = NULL;
00236   return 0;
00237 }
00238 
00239 RTOp_value_type RTOp_ROp_norm_inf_val(RTOp_ReductTarget targ_obj)
00240 {
00241 #ifdef RTOp_DEBUG
00242   assert(targ_obj != RTOp_REDUCT_OBJ_NULL );
00243 #endif
00244   return *(RTOp_value_type*)targ_obj;
00245 }
00246 
00247 /* Common functions */
00248 
00249 int RTOp_ROp_norm_destroy( struct RTOp_RTOp* op )
00250 {
00251   op->vtbl = NULL;
00252   return 0;
00253 }

Generated on Tue Jul 13 09:30:54 2010 for MOOCHO (Single Doxygen Collection) by  doxygen 1.4.7