Skip to content

Commit

Permalink
Added more mathematics-related functionality to math library.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Oct 22, 2024
1 parent 77684ed commit 645b499
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
117 changes: 114 additions & 3 deletions lib/zhvlib/Math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@

#include <cmath>
#include <exception>
#include <random>

ZHIVO_FUNC(math_cos) {
if(args.size() != 1)
throw std::runtime_error("Expecting only 1 argument.");
throw std::runtime_error(
"Expecting 1 argument, got " +
std::to_string(args.size()) +
"."
);

DynamicObject value = args.at(0);
if(!value.isNumber())
Expand All @@ -32,9 +37,28 @@ ZHIVO_FUNC(math_cos) {
return DynamicObject(cos(value.getNumber()));
}

ZHIVO_FUNC(math_cosh) {
if(args.size() != 1)
throw std::runtime_error(
"Expecting 1 argument, got " +
std::to_string(args.size()) +
"."
);

DynamicObject value = args.at(0);
if(!value.isNumber())
throw std::runtime_error("Argument type is not of number.");

return DynamicObject(cosh(value.getNumber()));
}

ZHIVO_FUNC(math_sin) {
if(args.size() != 1)
throw std::runtime_error("Expecting only 1 argument.");
throw std::runtime_error(
"Expecting 1 argument, got " +
std::to_string(args.size()) +
"."
);

DynamicObject value = args.at(0);
if(!value.isNumber())
Expand All @@ -43,13 +67,100 @@ ZHIVO_FUNC(math_sin) {
return DynamicObject(sin(value.getNumber()));
}

ZHIVO_FUNC(math_sinh) {
if(args.size() != 1)
throw std::runtime_error(
"Expecting 1 argument, got " +
std::to_string(args.size()) +
"."
);

DynamicObject value = args.at(0);
if(!value.isNumber())
throw std::runtime_error("Argument type is not of number.");

return DynamicObject(sinh(value.getNumber()));
}

ZHIVO_FUNC(math_tan) {
if(args.size() != 1)
throw std::runtime_error("Expecting only 1 argument.");
throw std::runtime_error(
"Expecting 1 argument, got " +
std::to_string(args.size()) +
"."
);

DynamicObject value = args.at(0);
if(!value.isNumber())
throw std::runtime_error("Argument type is not of number.");

return DynamicObject(tan(value.getNumber()));
}

ZHIVO_FUNC(math_tanh) {
if(args.size() != 1)
throw std::runtime_error(
"Expecting 1 argument, got " +
std::to_string(args.size()) +
"."
);

DynamicObject value = args.at(0);
if(!value.isNumber())
throw std::runtime_error("Argument type is not of number.");

return DynamicObject(tanh(value.getNumber()));
}

ZHIVO_FUNC(math_rand) {
std::uniform_real_distribution<> dis(0.0, 1.0);
double value = 0.0f;

if(args.size() == 1) {
DynamicObject arg = args.at(0);
if(!arg.isNumber())
throw std::runtime_error("Expecting a number argument.");

std::mt19937 gen(arg.getNumber());
value = dis(gen);
}
else {
std::random_device rd;
std::mt19937 gen(rd());

value = dis(gen);
}

return DynamicObject(value);
}

ZHIVO_FUNC(math_sigmoid) {
if(args.size() != 1)
throw std::runtime_error(
"Expecting 1 argument, got " +
std::to_string(args.size()) +
"."
);

DynamicObject value = args.at(0);
if(!value.isNumber())
throw std::runtime_error("Argument type is not of number.");

return DynamicObject(1 / (1 + exp(-value.getNumber())));
}

ZHIVO_FUNC(math_sigmoidDerivative) {
if(args.size() != 1)
throw std::runtime_error(
"Expecting 1 argument, got " +
std::to_string(args.size()) +
"."
);

DynamicObject value = args.at(0);
if(!value.isNumber())
throw std::runtime_error("Argument type is not of number.");

double num = value.getNumber();
return DynamicObject(num * (1 - num));
}
8 changes: 8 additions & 0 deletions lib/zhvlib/Math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@
ZHIVO_LIB_START

ZHIVO_FUNC(math_cos);
ZHIVO_FUNC(math_cosh);
ZHIVO_FUNC(math_sin);
ZHIVO_FUNC(math_sinh);
ZHIVO_FUNC(math_tan);
ZHIVO_FUNC(math_tanh);

ZHIVO_FUNC(math_rand);

ZHIVO_FUNC(math_sigmoid);
ZHIVO_FUNC(math_sigmoidDerivative);

ZHIVO_LIB_END

Expand Down

0 comments on commit 645b499

Please sign in to comment.