MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RTOp_ROp_norms.c
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
6 // Copyright (2003) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #include <math.h>
45 
46 #include "RTOp_ROp_norms.h"
47 #include "RTOp_obj_null_vtbl.h"
48 #include "RTOp_obj_value_vtbl.h"
49 #include "RTOp_reduct_sum_value.h"
50 #include "RTOp_reduct_max_value.h"
51 
52 #define MY_MAX(a,b) a > b ? a : b
53 
54 /* One norm reduction operator class. */
55 
57  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
58  , const int num_vecs, const struct RTOp_SubVector vecs[]
59  , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
60  , RTOp_ReductTarget targ_obj )
61 {
62  RTOp_index_type sub_dim;
63  const RTOp_value_type *v0_val;
64  ptrdiff_t v0_val_s;
65  register RTOp_index_type k;
66  RTOp_value_type *norm = NULL;
67 
68  /* */
69  /* Validate the input */
70  /* */
71  if( num_vecs != 1 )
73  if( num_targ_vecs != 0 )
75  assert(targ_obj);
76  assert(vecs);
77 
78  /* */
79  /* Get pointers to data */
80  /* */
81 
82  /* v0 */
83  sub_dim = vecs[0].sub_dim;
84  v0_val = vecs[0].values;
85  v0_val_s = vecs[0].values_stride;
86 
87  /* */
88  /* Perform the reduction */
89  /* */
90  norm = (RTOp_value_type*)targ_obj;
91  for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s ) {
92  *norm += fabs(*v0_val); /* ||v[0]||_1 */
93  }
94 
95  return 0; /* success? */
96 }
97 
99 {
100  &RTOp_obj_null_vtbl /* use null type for instance data */
101  ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */
102  ,"RTOp_ROp_norm_1"
103  ,NULL
107 };
108 
110 {
111  op->vtbl = &RTOp_ROp_norm_1_vtbl;
112  op->obj_data = NULL;
113  return 0;
114 }
115 
117 {
118 #ifdef RTOp_DEBUG
119  assert(targ_obj != RTOp_REDUCT_OBJ_NULL );
120 #endif
121  return *(RTOp_value_type*)targ_obj;
122 }
123 
124 /* Two norm reduction operator class. */
125 
127  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
128  , const int num_vecs, const struct RTOp_SubVector vecs[]
129  , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
130  , RTOp_ReductTarget targ_obj )
131 {
132  RTOp_index_type sub_dim;
133  const RTOp_value_type *v0_val;
134  ptrdiff_t v0_val_s;
135  register RTOp_index_type k;
136  RTOp_value_type *norm = NULL;
137 
138  /* */
139  /* Validate the input */
140  /* */
141  if( num_vecs != 1 )
143  if( num_targ_vecs != 0 )
145  assert(targ_obj);
146  assert(vecs);
147 
148  /* */
149  /* Get pointers to data */
150  /* */
151 
152  /* v0 */
153  sub_dim = vecs[0].sub_dim;
154  v0_val = vecs[0].values;
155  v0_val_s = vecs[0].values_stride;
156 
157  /* */
158  /* Perform the reduction */
159  /* */
160  norm = (RTOp_value_type*)targ_obj;
161  for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s )
162  *norm += (*v0_val)*(*v0_val); /* (||v[0]||_2)^2 */
163 
164  return 0; /* success? */
165 }
166 
168 {
169  &RTOp_obj_null_vtbl /* use null type for instance data */
170  ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */
171  ,"RTOp_ROp_norm_2"
172  ,NULL
176 };
177 
179 {
180  op->vtbl = &RTOp_ROp_norm_2_vtbl;
181  op->obj_data = NULL;
182  return 0;
183 }
184 
186 {
187 #ifdef RTOp_DEBUG
188  assert(targ_obj != RTOp_REDUCT_OBJ_NULL );
189 #endif
190  return sqrt(*(RTOp_value_type*)targ_obj);
191 }
192 
193 /* Infinity norm reduction operator class. */
194 
196  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
197  , const int num_vecs, const struct RTOp_SubVector vecs[]
198  , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
199  , RTOp_ReductTarget targ_obj )
200 {
201  RTOp_index_type sub_dim;
202  const RTOp_value_type *v0_val;
203  ptrdiff_t v0_val_s;
204  register RTOp_index_type k;
205  RTOp_value_type *norm = NULL;
206 
207  /* */
208  /* Validate the input */
209  /* */
210  if( num_vecs != 1 )
212  if( num_targ_vecs != 0 )
214  assert(targ_obj);
215  assert(vecs);
216 
217  /* */
218  /* Get pointers to data */
219  /* */
220 
221  /* v0 */
222  sub_dim = vecs[0].sub_dim;
223  v0_val = vecs[0].values;
224  v0_val_s = vecs[0].values_stride;
225 
226  /* */
227  /* Perform the reduction */
228  /* */
229  norm = (RTOp_value_type*)targ_obj;
230  for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s )
231  *norm = MY_MAX( fabs(*v0_val), (*(RTOp_value_type*)targ_obj) ); /* ||v[0]||_inf */
232 
233  return 0; /* success? */
234 }
235 
237 {
238  &RTOp_obj_null_vtbl /* use null type for instance data */
239  ,&RTOp_obj_value_vtbl /* use simple scalar type for target object */
240  ,"RTOp_ROp_norm_inf"
241  ,NULL
245 };
246 
248 {
250  op->obj_data = NULL;
251  return 0;
252 }
253 
255 {
256 #ifdef RTOp_DEBUG
257  assert(targ_obj != RTOp_REDUCT_OBJ_NULL );
258 #endif
259  return *(RTOp_value_type*)targ_obj;
260 }
261 
262 /* Common functions */
263 
265 {
266  op->vtbl = NULL;
267  return 0;
268 }
static int RTOp_ROp_norms_apply_op_norm_1(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, const int num_vecs, const struct RTOp_SubVector vecs[], const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[], RTOp_ReductTarget targ_obj)
int RTOp_ROp_norm_inf_construct(struct RTOp_RTOp *op)
ptrdiff_t values_stride
Definition: RTOp.h:305
double RTOp_value_type
Definition: RTOp.h:69
RTOp_value_type RTOp_ROp_norm_2_val(RTOp_ReductTarget targ_obj)
void * RTOp_ReductTarget
Definition: RTOp.h:191
#define MY_MAX(a, b)
int RTOp_reduct_sum_value(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, RTOp_ReductTarget in_targ_obj, RTOp_ReductTarget inout_targ_obj)
static int RTOp_ROp_norms_apply_op_norm_inf(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, const int num_vecs, const struct RTOp_SubVector vecs[], const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[], RTOp_ReductTarget targ_obj)
const RTOp_value_type * values
Definition: RTOp.h:303
int RTOp_ROp_norm_2_construct(struct RTOp_RTOp *op)
int RTOp_get_reduct_max_value_op(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, RTOp_reduct_op_func_ptr_t *reduct_op_func_ptr)
#define RTOp_ERR_INVALID_NUM_VECS
Definition: RTOp.h:266
static int RTOp_ROp_norms_apply_op_norm_2(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, const int num_vecs, const struct RTOp_SubVector vecs[], const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[], RTOp_ReductTarget targ_obj)
const struct RTOp_obj_type_vtbl_t RTOp_obj_null_vtbl
void sqrt(DVectorSlice *vs_lhs, const DVectorSlice &vs_rhs)
vs_lhs = sqrt(vs_rhs)
RTOp_value_type RTOp_ROp_norm_1_val(RTOp_ReductTarget targ_obj)
void * obj_data
Definition: RTOp.h:800
const struct RTOp_RTOp_vtbl_t * vtbl
Definition: RTOp.h:802
int RTOp_reduct_max_value(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, RTOp_ReductTarget in_targ_obj, RTOp_ReductTarget inout_targ_obj)
RTOp_value_type RTOp_ROp_norm_inf_val(RTOp_ReductTarget targ_obj)
const struct RTOp_RTOp_vtbl_t RTOp_ROp_norm_inf_vtbl
int RTOp_ROp_norm_1_construct(struct RTOp_RTOp *op)
int RTOp_ROp_norm_destroy(struct RTOp_RTOp *op)
RTOp_index_type sub_dim
Definition: RTOp.h:301
#define RTOp_ERR_INVALID_NUM_TARG_VECS
Definition: RTOp.h:268
const struct RTOp_obj_type_vtbl_t RTOp_obj_value_vtbl
const struct RTOp_RTOp_vtbl_t RTOp_ROp_norm_2_vtbl
int RTOp_get_reduct_sum_value_op(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, RTOp_reduct_op_func_ptr_t *reduct_op_func_ptr)
const struct RTOp_RTOp_vtbl_t RTOp_ROp_norm_1_vtbl
Teuchos_Ordinal RTOp_index_type
Definition: RTOp.h:68
#define RTOp_REDUCT_OBJ_NULL
Definition: RTOp.h:192