-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* lab2 * Update test_zhatkin_v_levenshtein_distance.cpp * cpplint * cpplint * cpplint * cpplint * cpplint * cpplint * Update modules/mironov_i_stats/include/stats.h Co-authored-by: Kuznetsov-Artyom <[email protected]> * Update modules/mironov_i_stats/src/stats.cpp Co-authored-by: Kuznetsov-Artyom <[email protected]> --------- Co-authored-by: Kuznetsov-Artyom <[email protected]>
- Loading branch information
1 parent
4e54692
commit 8288f1e
Showing
10 changed files
with
158 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Declare variables for binaries' names | ||
get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) | ||
set(MODULE "${DIR_NAME}") | ||
set(LIBRARY "lib_${MODULE}") | ||
set(TESTS "test_${MODULE}") | ||
|
||
# Include directory with public headers | ||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
# Add all submodules | ||
add_subdirectory(src) | ||
add_subdirectory(test) |
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,12 @@ | ||
// Copyright 2024 Mironov Ilya | ||
|
||
#pragma once | ||
|
||
#include <vector> | ||
|
||
namespace stats { | ||
float moment(const std::vector<float>& data, size_t k, float mu); | ||
float mean(const std::vector<float>& data); | ||
float var_base(const std::vector<float>& data); | ||
float var(const std::vector<float>& data); | ||
} // namespace stats |
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,18 @@ | ||
set(target ${LIBRARY}) | ||
|
||
file(GLOB srcs "*.cpp") | ||
file(GLOB hdrs "../include/*.h") | ||
set_source_files_properties(${srcs} ${hdrs} PROPERTIES | ||
LABELS "${MODULE};Library") | ||
|
||
add_library(${target} STATIC ${srcs} ${hdrs}) | ||
set_target_properties(${target} PROPERTIES | ||
OUTPUT_NAME ${MODULE} | ||
LABELS "${MODULE};Library") | ||
|
||
if (UNIX) | ||
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT}) | ||
endif (UNIX) | ||
target_link_libraries(${target} ${LIBRARY_DEPS}) | ||
|
||
set(LIBRARY_DEPS "${LIBRARY_DEPS};${target}" PARENT_SCOPE) |
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,33 @@ | ||
// Copyright 2024 Mironov Ilya | ||
|
||
#include "include/stats.h" | ||
|
||
#include <cmath> | ||
#include <numeric> | ||
|
||
namespace stats { | ||
|
||
float moment(const std::vector<float>& data, size_t k, float mu) { | ||
float result = 0.0f; | ||
for (auto& x : data) { | ||
result += std::pow(x - mu, k); | ||
} | ||
return result / data.size(); | ||
} | ||
float mean(const std::vector<float>& data) { | ||
return moment(data, 1, 0.0f); | ||
} | ||
float var_base(const std::vector<float>& data) { | ||
return moment(data, 2, mean(data)); | ||
} | ||
float var(const std::vector<float>& data) { | ||
float result1 = 0.0f; | ||
float result2 = 0.0f; | ||
const auto size = data.size(); | ||
for (auto& x : data) { | ||
result1 += x * x; | ||
result2 += x; | ||
} | ||
return result1 / size - (result2 / size) * (result2 / size); | ||
} | ||
} // namespace stats |
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,27 @@ | ||
set(target ${TESTS}) | ||
|
||
file(GLOB srcs "*.cpp") | ||
set_source_files_properties(${srcs} PROPERTIES | ||
LABELS "${MODULE};Test") | ||
|
||
add_executable(${target} ${srcs} ${hdrs}) | ||
set_target_properties(${target} PROPERTIES | ||
LABELS "${MODULE};Test") | ||
|
||
if (UNIX) | ||
target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread) | ||
endif (UNIX) | ||
target_link_libraries(${target} gtest ${LIBRARY}) | ||
|
||
# VS2012 doesn't support correctly the tuples yet, | ||
# see http://code.google.com/p/googletest/issues/detail?id=412 | ||
if(MSVC) | ||
target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10) | ||
endif() | ||
|
||
add_test( | ||
NAME ${MODULE}_gtest | ||
COMMAND ${target} | ||
) | ||
set_tests_properties (${MODULE}_gtest PROPERTIES | ||
LABELS "${MODULE}") |
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,8 @@ | ||
// Copyright 2024 Mironov Ilya | ||
|
||
#include <gtest/gtest.h> | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
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,43 @@ | ||
// Copyright 2024 Mironov Ilya | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <numeric> | ||
|
||
#include "include/stats.h" | ||
|
||
TEST(TestStats, OneElementMean) { | ||
std::vector<float> data = {1.0f}; | ||
EXPECT_FLOAT_EQ(1.0f, stats::moment(data, 1, 0.0f)); | ||
} | ||
|
||
TEST(TestStats, OneElementVar) { | ||
std::vector<float> data = {1.0f}; | ||
const float mean = stats::moment(data, 1, 0.0f); | ||
EXPECT_FLOAT_EQ(0.0f, stats::moment(data, 2, mean)); | ||
} | ||
|
||
TEST(TestStats, CompareVarImplsSimple) { | ||
std::vector<float> data = {1.0f, 2.0f, 3.0f}; | ||
EXPECT_NEAR(0.6666f, stats::var(data), 1e-4); | ||
EXPECT_NEAR(0.6666f, stats::var_base(data), 1e-4); | ||
} | ||
|
||
TEST(TestStats, SimpleData) { | ||
std::vector<float> data = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f}; | ||
EXPECT_FLOAT_EQ(3.0f, stats::mean(data)); | ||
EXPECT_FLOAT_EQ(2.0f, stats::var(data)); | ||
EXPECT_FLOAT_EQ(2.0f, stats::var_base(data)); | ||
} | ||
|
||
TEST(TestStats, CompareVarImplsIota) { | ||
std::vector<float> data(100); | ||
std::iota(data.begin(), data.end(), 0.0f); | ||
ASSERT_FLOAT_EQ(stats::var_base(data), stats::var(data)); | ||
} | ||
|
||
TEST(TestStats, NegativeSimpleData) { | ||
std::vector<float> data = {-1.0f, -2.0f, -3.0f, -4.0f, -5.0f}; | ||
EXPECT_FLOAT_EQ(-3.0f, stats::mean(data)); | ||
EXPECT_FLOAT_EQ(2.0f, stats::var(data)); | ||
} |
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