-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Wey Gu <weyl.gu@gmail.com> Co-authored-by: Cheng Xuntao <7731943+xtcyclist@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
181 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* Copyright (c) 2020 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
#include "standard_deviation.h" | ||
|
||
#include <cmath> | ||
#include <vector> | ||
|
||
#include "../src/common/datatypes/List.h" | ||
|
||
extern "C" GraphFunction *create() { | ||
return new standard_deviation; | ||
} | ||
extern "C" void destroy(GraphFunction *function) { | ||
delete function; | ||
} | ||
|
||
char *standard_deviation::name() { | ||
const char *name = "standard_deviation"; | ||
return const_cast<char *>(name); | ||
} | ||
|
||
std::vector<std::vector<nebula::Value::Type>> standard_deviation::inputType() { | ||
std::vector<nebula::Value::Type> vtp = {nebula::Value::Type::LIST}; | ||
std::vector<std::vector<nebula::Value::Type>> vvtp = {vtp}; | ||
return vvtp; | ||
} | ||
|
||
nebula::Value::Type standard_deviation::returnType() { | ||
return nebula::Value::Type::FLOAT; | ||
} | ||
|
||
size_t standard_deviation::minArity() { | ||
return 1; | ||
} | ||
|
||
size_t standard_deviation::maxArity() { | ||
return 1; | ||
} | ||
|
||
bool standard_deviation::isPure() { | ||
return true; | ||
} | ||
|
||
double caculate_standard_deviation(const std::vector<double> &numbers) { | ||
double sum = 0; | ||
for (double number : numbers) { | ||
sum += number; | ||
} | ||
double average = sum / numbers.size(); | ||
|
||
double variance = 0; | ||
for (double number : numbers) { | ||
double difference = number - average; | ||
variance += difference * difference; | ||
} | ||
variance /= numbers.size(); | ||
|
||
return sqrt(variance); | ||
} | ||
|
||
nebula::Value standard_deviation::body( | ||
const std::vector<std::reference_wrapper<const nebula::Value>> &args) { | ||
switch (args[0].get().type()) { | ||
case nebula::Value::Type::NULLVALUE: { | ||
return nebula::Value::kNullValue; | ||
} | ||
case nebula::Value::Type::LIST: { | ||
std::vector<double> numbers; | ||
auto list = args[0].get().getList(); | ||
auto size = list.size(); | ||
|
||
for (int i = 0; i < size; i++) { | ||
auto &value = list[i]; | ||
if (value.isInt()) { | ||
numbers.push_back(value.getInt()); | ||
} else if (value.isFloat()) { | ||
numbers.push_back(value.getFloat()); | ||
} else { | ||
return nebula::Value::kNullValue; | ||
} | ||
} | ||
return nebula::Value(caculate_standard_deviation(numbers)); | ||
} | ||
default: { | ||
return nebula::Value::kNullValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* Copyright (c) 2020 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
#ifndef UDF_PROJECT_STANDARD_DEVIATION_H | ||
#define UDF_PROJECT_STANDARD_DEVIATION_H | ||
|
||
#include "../src/common/function/GraphFunction.h" | ||
|
||
// Example of a UDF function that calculates the standard deviation of a set of numbers. | ||
// > YIELD standard_deviation([1,2,3]) | ||
// +-----------------------------+ | ||
// | standard_deviation([1,2,3]) | | ||
// +-----------------------------+ | ||
// | 0.816496580927726 | | ||
// +-----------------------------+ | ||
|
||
// > YIELD standard_deviation([1,1,1]) | ||
// +-----------------------------+ | ||
// | standard_deviation([1,1,1]) | | ||
// +-----------------------------+ | ||
// | 0.0 | | ||
// +-----------------------------+ | ||
|
||
// > GO 1 TO 2 STEPS FROM "player100" OVER follow YIELD properties(edge).degree AS d | yield collect($-.d) | ||
// +--------------------------+ | ||
// | collect($-.d) | | ||
// +--------------------------+ | ||
// | [95, 95, 95, 90, 95, 90] | | ||
// +--------------------------+ | ||
|
||
|
||
class standard_deviation : public GraphFunction { | ||
public: | ||
char *name() override; | ||
|
||
std::vector<std::vector<nebula::Value::Type>> inputType() override; | ||
|
||
nebula::Value::Type returnType() override; | ||
|
||
size_t minArity() override; | ||
|
||
size_t maxArity() override; | ||
|
||
bool isPure() override; | ||
|
||
nebula::Value body(const std::vector<std::reference_wrapper<const nebula::Value>> &args) override; | ||
}; | ||
|
||
#endif // UDF_PROJECT_STANDARD_DEVIATION_H |