haojin2 commented on a change in pull request #17014: [NumPy] Add NumPy support 
for norm
URL: https://github.com/apache/incubator-mxnet/pull/17014#discussion_r364562677
 
 

 ##########
 File path: src/operator/numpy/linalg/np_norm-inl.h
 ##########
 @@ -0,0 +1,830 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_norm-inl.h
+ * \brief norm
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_NORM_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_NORM_INL_H_
+
+#include <mxnet/operator_util.h>
+#include <vector>
+#include <limits>
+#include <cmath>
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+#include "../../tensor/init_op.h"
+#include "./broadcast_reduce_op_customized.h"
+#include "./np_gesvd-inl.h"
+#include "../np_matrix_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+namespace mshadow_op {
+/*! \brief Lp-norm power reducer */
+
+struct nrmlp {
+  double lp;
+  MSHADOW_XINLINE nrmlp(): lp(2) {}
+  MSHADOW_XINLINE nrmlp(double l): lp(l) {}
+
+  /* \brief power for Lp norm */
+  MSHADOW_XINLINE static double lp_power(volatile double src, volatile double 
p) {
+    if (p != 0.0) {
+      if (src == 0.0) {
+        return src;
+      } else {
+        return power::Map(src, p);
+      }
+    } else {  // 0-norm, sparsity
+      return static_cast<double>(src != 0);
+    }
+  }
+
+  /*! \brief do reduction into dst */
+  template<typename AType, typename DType>
+  MSHADOW_XINLINE void Reduce(volatile AType& sum_of_powers, volatile DType 
src) { // NOLINT(*)
+    if (src != 0) {
+      sum_of_powers += AType(lp_power(static_cast<double>(src), lp));
+    }
+  }
+
+  /*! \brief do stable reduction into dst */
+  template<typename AType, typename DType>
+  MSHADOW_XINLINE void Reduce(volatile AType& sum_of_powers,  volatile DType 
src, volatile DType& scale) { // NOLINT(*)
+    if (src != 0) {
+      DType abs = abs::Map(src);
 
 Review comment:
   Better use `src_abs` as the variable name instead.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to