ALP User Documentation  0.8.preview
Algebraic Programming User Documentation
norm.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2021 Huawei Technologies Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
34 #ifndef _H_GRB_ALGORITHMS_NORM
35 #define _H_GRB_ALGORITHMS_NORM
36 
37 #include <graphblas.hpp>
38 
39 #include <cmath> // for std::sqrt
40 
41 
42 namespace grb {
43 
44  namespace algorithms {
45 
64  template< typename OutputType, typename InputType >
65  OutputType std_sqrt( const InputType x ) {
66  return( static_cast< OutputType >( std::sqrt( x ) ) );
67  };
68 
88  template<
89  Descriptor descr = descriptors::no_operation, class Ring,
90  typename InputType, typename OutputType,
91  Backend backend, typename Coords
92  >
94  OutputType &x,
96  const Ring &ring = Ring(),
97  const std::function< OutputType( OutputType ) > sqrtX =
98  std_sqrt< OutputType, OutputType >,
99  const typename std::enable_if<
100  std::is_floating_point< OutputType >::value,
101  void >::type * = nullptr
102  ) {
103  InputType yyt = ring.template getZero< InputType >();
104  RC ret = grb::dot< descr >(
105  yyt, y, y, ring.getAdditiveMonoid(),
107  );
108  if( ret == SUCCESS ) {
110  ret = ret ? ret : grb::foldl(
111  x,
112  sqrtX( grb::utils::is_complex< InputType >::modulus( yyt ) ),
113  foldOp
114  );
115  }
116  return ret;
117  }
118  }
119 }
120 
121 #endif // end ``_H_GRB_ALGORITHMS_NORM''
122 
RC
Return codes of ALP primitives.
Definition: rc.hpp:47
A GraphBLAS vector.
Definition: vector.hpp:64
static constexpr Descriptor no_operation
Indicates no additional pre- or post-processing on any of the GraphBLAS function arguments.
Definition: descriptors.hpp:63
unsigned int Descriptor
Descriptors indicate pre- or post-processing for some or all of the arguments to an ALP/GraphBLAS cal...
Definition: descriptors.hpp:54
Conjugate-multiply operator that conjugates the right-hand operand before multiplication.
Definition: ops.hpp:918
RC foldl(IOType &x, const Vector< InputType, backend, Coords > &y, const Vector< MaskType, backend, Coords > &mask, const Monoid &monoid=Monoid(), const typename std::enable_if< !grb::is_object< IOType >::value &&!grb::is_object< InputType >::value &&!grb::is_object< MaskType >::value &&grb::is_monoid< Monoid >::value, void >::type *const =nullptr)
Reduces, or folds, a vector into a scalar.
Definition: blas1.hpp:3840
OutputType std_sqrt(const InputType x)
An alias of std::sqrt where the input and output types are templated separately.
Definition: norm.hpp:65
RC norm2(OutputType &x, const Vector< InputType, backend, Coords > &y, const Ring &ring=Ring(), const std::function< OutputType(OutputType) > sqrtX=std_sqrt< OutputType, OutputType >, const typename std::enable_if< std::is_floating_point< OutputType >::value, void >::type *=nullptr)
Provides a generic implementation of the 2-norm computation.
Definition: norm.hpp:93
Backend
A collection of all backends.
Definition: backends.hpp:49
This operator takes the sum of the two input parameters and writes it to the output variable.
Definition: ops.hpp:175
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:477
The main header to include in order to use the ALP/GraphBLAS API.
Indicates the primitive has executed successfully.
Definition: rc.hpp:54