Skip to content

Commit

Permalink
Added new statistics class
Browse files Browse the repository at this point in the history
  • Loading branch information
ceticamarco committed Mar 26, 2024
1 parent 19ceac3 commit 89b7825
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 13 deletions.
41 changes: 39 additions & 2 deletions man.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: dc
section: 1
header: General Commands Manual
footer: Marco Cetica
date: March 25, 2024
date: March 26, 2024
---


Expand Down Expand Up @@ -179,7 +179,7 @@ Pops one value, computes its square root, and pushes that.

**!**

Pops one value, computes its factorial, and pushes that.
Pops one non-negative integer, computes its factorial, and pushes that.

**pi**

Expand Down Expand Up @@ -224,6 +224,43 @@ Pops one value, computes its `acos`, and pushes that.

Pops one value, computes its `atan`, and pushes that.

## Statistics Operations
**dc** supports various common statistics operations, such as permutations, combinations, mean, standard deviation
summation, sum of squares and linear regression. All statistics functions are limited to **non-negative integers**.
Accumulating functions use the `X` register.

**gP**

Pops two non-negative integers(that is, >=0) and computes `P_{y, x}`, that is the number of possible different arrangements of
`y` different items taken in quantities of `x` items at a time. No item shall occur more than once in an arrangmement and different orders of same `x`
items are counted separately. The `y` parameter correspond to the second one value popped while the `x` parameter correspond to the first one popped.

**gC**

Pops two non-negative integers(that is, >=0) and computes `C_{y, x}`, that is the number of possible sets of
`y` different items taken in quantities of `x` items at a time. No item shall occur more than once in a set and different orders of the same `x`
items are not counted separately. The `y` parameter correspond to the second one value popped while the `x` parameter correspond to the first one popped.

**gN**

Counts the number of accumulated elements of the `X` register's stack and pushes that.

**gs**

Computes Σx of the `X` register's stack and pushes that.

**gS**

Computes Σx^2 of the `X` register's stack and pushes that.

**gM**

Computes x̄(mean) of the `X` register's stack and pushes that.

**gD**

Computes σ(standard deviation) of the `X` register's stack and pushes that.

## Base Conversion

**pb**
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(HEADER_FILES
eval.h
macro.h
mathematics.h
statistics.h
bitwise.h
operation.h
stack.h
Expand All @@ -14,6 +15,7 @@ set(SOURCE_FILES
eval.cpp
macro.cpp
mathematics.cpp
statistics.cpp
bitwise.cpp
stack.cpp
adt.cpp
Expand Down
38 changes: 38 additions & 0 deletions src/adt.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <numeric>
#include "adt.h"

#define GET_X this->stack.back()
Expand Down Expand Up @@ -121,6 +122,43 @@ namespace dc {
return this->stack.size();
}

/*
* Returns the summation of all items
*/
template<typename T>
requires is_num_or_str<T>
double Stack<T>::summation() {
auto sum = std::accumulate(this->stack.begin(), this->stack.end(), 0.0,
[](auto accumulator, const T& val) -> double {
if constexpr(std::is_same_v<T, std::string>) {
return accumulator + std::stod(val);
} else {
return accumulator + val;
}
});

return sum;
}

/*
* Returns the summation of squares of all items
*/
template<typename T>
requires is_num_or_str<T>
double Stack<T>::summation_squared() {
auto sum = std::accumulate(this->stack.begin(), this->stack.end(), 0.0,
[](auto accumulator, const T& val) -> double {
if constexpr(std::is_same_v<T, std::string>) {
return accumulator + (std::stod(val) * std::stod(val));
} else {
return accumulator + (val * val);
}
});

return sum;
}


/**
* Returns true if stack is empty
* false otherwise
Expand Down
2 changes: 2 additions & 0 deletions src/adt.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace dc {
T& at(std::size_t index);
T& operator[](std::size_t index);
std::size_t size();
double summation();
double summation_squared();
bool empty();
[[nodiscard]] const std::vector<T>& get_ref() const;

Expand Down
8 changes: 7 additions & 1 deletion src/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "adt.cpp"
#include "eval.h"
#include "mathematics.h"
#include "statistics.h"
#include "bitwise.h"
#include "stack.h"
#include "macro.h"
Expand Down Expand Up @@ -45,7 +46,12 @@ void Evaluate::init_environment() {
this->op_factory.emplace("@", MAKE_UNIQUE_PTR(Mathematics, OPType::RND));
this->op_factory.emplace("$", MAKE_UNIQUE_PTR(Mathematics, OPType::INT));
// Statistical operations

this->op_factory.emplace("gP", MAKE_UNIQUE_PTR(Statistics, OPType::PERM));
this->op_factory.emplace("gC", MAKE_UNIQUE_PTR(Statistics, OPType::COMB));
this->op_factory.emplace("gs", MAKE_UNIQUE_PTR(Statistics, OPType::SUMX));
this->op_factory.emplace("gS", MAKE_UNIQUE_PTR(Statistics, OPType::SUMXX));
this->op_factory.emplace("gM", MAKE_UNIQUE_PTR(Statistics, OPType::MEAN));
this->op_factory.emplace("gD", MAKE_UNIQUE_PTR(Statistics, OPType::SDEV));
// Bitwise operations
this->op_factory.emplace("{", MAKE_UNIQUE_PTR(Bitwise, OPType::BAND));
this->op_factory.emplace("}", MAKE_UNIQUE_PTR(Bitwise, OPType::BOR));
Expand Down
20 changes: 10 additions & 10 deletions src/mathematics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,17 @@ std::optional<std::string> Mathematics::fn_mod_exp(dc::Stack<std::string> &stack
auto n = stack[len];
auto e = stack[len-1];
auto b = stack[len-2];
auto is_n_num = is_num<double>(n);
auto is_e_num = is_num<double>(e);
auto is_b_num = is_num<double>(b);
auto is_n_num = is_num<long long>(n);
auto is_e_num = is_num<long long>(e);
auto is_b_num = is_num<long long>(b);

// This functions computes
// c ≡ b^e (mod n)
if(is_n_num && is_e_num && is_b_num) {
stack.copy_xyz();
auto modulus = std::stol(stack.pop(true));
auto exponent = std::stol(stack.pop(true));
auto base = std::stol(stack.pop(true));
auto modulus = std::stoll(stack.pop(true));
auto exponent = std::stoll(stack.pop(true));
auto base = std::stoll(stack.pop(true));

if(modulus == 1) {
stack.push("0");
Expand Down Expand Up @@ -502,14 +502,14 @@ std::optional<std::string> Mathematics::fn_fact(dc::Stack<std::string> &stack, c
// Check whether the entry is a number
if(is_x_num) {
stack.copy_xyz();
unsigned long factorial = 1;
unsigned long long factorial = 1;
auto val = std::stod(stack.pop(true));

if(val == 0.0) {
factorial = 1;
if(val < 0.0) {
return "'!' is not defined for negative numbers";
}

for(std::size_t i = 2; i <= (std::size_t)val; i++) {
for(unsigned long long i = 2; i <= val; i++) {
factorial *= i;
}

Expand Down
Loading

0 comments on commit 89b7825

Please sign in to comment.