ViennaCL - The Vienna Computing Library: viennacl/linalg/maxmin.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
maxmin.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_MAXMIN_HPP_
2 #define VIENNACL_LINALG_MAXMIN_HPP_
3
4 /* =========================================================================
5 Copyright (c) 2010-2016, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8 Portions of this software are copyright by UChicago Argonne, LLC.
9
10 -----------------
11 ViennaCL - The Vienna Computing Library
12 -----------------
13
14 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15
16 (A list of authors and contributors can be found in the manual)
17
18 License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20
25 #include <cmath>
26 #include "viennacl/forwards.h"
27 #include "viennacl/tools/tools.hpp"
28 #include "viennacl/meta/enable_if.hpp"
29 #include "viennacl/meta/tag_of.hpp"
30 #include "viennacl/meta/result_of.hpp"
31
32 namespace viennacl
33 {
34 //
35 // generic norm_inf function
36 // uses tag dispatch to identify which algorithm
37 // should be called
38 //
39 namespace linalg
40 {
41
42
43 // ----------------------------------------------------
44 // STL
45 //
46 template< typename NumericT >
47 NumericT max(std::vector<NumericT> const & v1)
48 {
49 //std::cout << "stl .. " << std::endl;
50 NumericT result = v1[0];
51 for (vcl_size_t i=1; i<v1.size(); ++i)
52 {
53 if (v1[i] > result)
54 result = v1[i];
55 }
56
57 return result;
58 }
59
60 // ----------------------------------------------------
61 // VIENNACL
62 //
63 template< typename ScalarType>
64 viennacl::scalar_expression< const viennacl::vector_base<ScalarType>,
65 const viennacl::vector_base<ScalarType>,
66 viennacl::op_max >
67 max(viennacl::vector_base<ScalarType> const & v1)
68 {
69 //std::cout << "viennacl .. " << std::endl;
70 return viennacl::scalar_expression< const viennacl::vector_base<ScalarType>,
71 const viennacl::vector_base<ScalarType>,
72 viennacl::op_max >(v1, v1);
73 }
74
75 // with vector expression:
76 template<typename LHS, typename RHS, typename OP>
77 viennacl::scalar_expression<const viennacl::vector_expression<const LHS, const RHS, OP>,
78 const viennacl::vector_expression<const LHS, const RHS, OP>,
79 viennacl::op_max>
80 max(viennacl::vector_expression<const LHS, const RHS, OP> const & vector)
81 {
82 return viennacl::scalar_expression< const viennacl::vector_expression<const LHS, const RHS, OP>,
83 const viennacl::vector_expression<const LHS, const RHS, OP>,
84 viennacl::op_max >(vector, vector);
85 }
86
87 // ----------------------------------------------------
88 // STL
89 //
90 template< typename NumericT >
91 NumericT min(std::vector<NumericT> const & v1)
92 {
93 //std::cout << "stl .. " << std::endl;
94 NumericT result = v1[0];
95 for (vcl_size_t i=1; i<v1.size(); ++i)
96 {
97 if (v1[i] < result)
98 result = v1[i];
99 }
100
101 return result;
102 }
103
104 // ----------------------------------------------------
105 // VIENNACL
106 //
107 template< typename ScalarType>
108 viennacl::scalar_expression< const viennacl::vector_base<ScalarType>,
109 const viennacl::vector_base<ScalarType>,
110 viennacl::op_min >
111 min(viennacl::vector_base<ScalarType> const & v1)
112 {
113 //std::cout << "viennacl .. " << std::endl;
114 return viennacl::scalar_expression< const viennacl::vector_base<ScalarType>,
115 const viennacl::vector_base<ScalarType>,
116 viennacl::op_min >(v1, v1);
117 }
118
119 template< typename ScalarType>
120 viennacl::scalar_expression< const viennacl::vector_base<ScalarType>,
121 const viennacl::vector_base<ScalarType>,
122 viennacl::op_min >
123 min(viennacl::vector<ScalarType> const & v1)
124 {
125 //std::cout << "viennacl .. " << std::endl;
126 return viennacl::scalar_expression< const viennacl::vector_base<ScalarType>,
127 const viennacl::vector_base<ScalarType>,
128 viennacl::op_min >(v1, v1);
129 }
130
131 // with vector expression:
132 template<typename LHS, typename RHS, typename OP>
133 viennacl::scalar_expression<const viennacl::vector_expression<const LHS, const RHS, OP>,
134 const viennacl::vector_expression<const LHS, const RHS, OP>,
135 viennacl::op_min>
136 min(viennacl::vector_expression<const LHS, const RHS, OP> const & vector)
137 {
138 return viennacl::scalar_expression< const viennacl::vector_expression<const LHS, const RHS, OP>,
139 const viennacl::vector_expression<const LHS, const RHS, OP>,
140 viennacl::op_min >(vector, vector);
141 }
142
143
144
145 } // end namespace linalg
146 } // end namespace viennacl
147 #endif
148
149
150
151
152
tag_of.hpp
Dispatch facility for distinguishing between ublas, STL and ViennaCL types.
tools.hpp
Various little tools used here and there in ViennaCL.
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::scalar_expression
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
viennacl::vector_expression
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
viennacl::op_max
A tag class representing the maximum of a vector.
Definition: forwards.h:210
NumericT
float NumericT
Definition: bisect.cpp:40
v1
viennacl::vector< float > v1
Definition: global_variables.cpp:60
viennacl::vector_base
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::vector
Definition: forwards.h:266
viennacl::op_min
A tag class representing the minimum of a vector.
Definition: forwards.h:213
viennacl::linalg::max
NumericT max(std::vector< NumericT > const &v1)
Definition: maxmin.hpp:47
result_of.hpp
A collection of compile time type deductions.
enable_if.hpp
Simple enable-if variant that uses the SFINAE pattern.
viennacl::linalg::min
NumericT min(std::vector< NumericT > const &v1)
Definition: maxmin.hpp:91