Skip to content

Commit 9dc50c7

Browse files
implemented exponential function operator
1 parent 7a0abe3 commit 9dc50c7

File tree

8 files changed

+100
-5
lines changed

8 files changed

+100
-5
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(SOURCES
2020
src/fields/subtraction.cpp
2121
src/fields/multiplication.cpp
2222
src/fields/division.cpp
23+
src/fields/exponential_function.cpp
2324
src/fields/field_link_definition.cpp
2425
src/fields/obj.cpp
2526
src/fields/type.cpp
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef EXPONENTIAL_FUNCTION_H
2+
#define EXPONENTIAL_FUNCTION_H
3+
4+
#include "fields/abstract_function_definition.h"
5+
#include "fields/type.h"
6+
#include "fields/obj.h"
7+
8+
class ExponentialFunction : public AbstractFunctionDefinition {
9+
public:
10+
// Constructor
11+
ExponentialFunction(Type* ref, const std::string& name);
12+
13+
void initializeField(Field* field) override;
14+
15+
// Overridden method from AbstractFunctionDefinition
16+
double computeUpdate(Obj* obj, FieldLinkDefinition* fl, double u) override;
17+
};
18+
19+
#endif // EXPONENTIAL_FUNCTION_H

install_manifest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/null_terminated_array.h
66
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/relation.h
77
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/field_definition.h
8+
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/exponential_function.h
89
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/flattened_type_relation.h
910
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/test_object.h
1011
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/test_type.h

python-tests/exponential-test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import unittest
2+
import sys
3+
import os
4+
5+
# Add the project root to Python's module search path
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
7+
8+
import aika
9+
10+
class ExponentialFunctionTestCase(unittest.TestCase):
11+
12+
def testExponentialFunction(self):
13+
print("Module 'aika' was loaded from:", aika.__file__)
14+
15+
TEST_RELATION_FROM = aika.RelationOne(1, "TEST_FROM")
16+
TEST_RELATION_TO = aika.RelationOne(2, "TEST_TO")
17+
TEST_RELATION_TO.setReversed(TEST_RELATION_FROM)
18+
TEST_RELATION_FROM.setReversed(TEST_RELATION_TO)
19+
20+
registry = aika.TypeRegistry()
21+
22+
typeA = aika.TestType(registry, "A")
23+
typeB = aika.TestType(registry, "B")
24+
25+
a = typeA.inputField("a")
26+
27+
exp_func = typeB.exp("exp_func")
28+
29+
exp_func.input(TEST_RELATION_FROM, a, 0)
30+
31+
registry.flattenTypeHierarchy()
32+
33+
oa = typeA.instantiate()
34+
ob = typeB.instantiate()
35+
36+
aika.TestObj.linkObjects(oa, ob)
37+
ob.initFields()
38+
39+
oa.setFieldValue(a, 5.0)
40+
41+
expected_value = 148.413159102576603
42+
actual_value = ob.getFieldValue(exp_func)
43+
print("ob.getFieldValue(exp_func):", actual_value)
44+
45+
self.assertAlmostEqual(expected_value, actual_value, places=5)
46+
47+
if __name__ == '__main__':
48+
unittest.main()

src/fields/division.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ void Division::initializeField(Field* field) {
2424
// based on which argument is being updated.
2525
double Division::computeUpdate(Obj* obj, FieldLinkDefinition* fl, double u) {
2626
if (fl->getArgument() == 0) {
27-
double divisor = getInputs()[1]->getInputValue(obj);
27+
double divisor = inputs[1]->getInputValue(obj);
2828
if (divisor == 0.0) {
2929
// Handle division by zero if necessary
3030
return 0.0;
3131
}
3232
return u / divisor;
3333
} else {
34-
double dividend = getInputs()[0]->getInputValue(obj);
35-
double divisor = getInputs()[1]->getUpdatedInputValue(obj);
34+
double dividend = inputs[0]->getInputValue(obj);
35+
double divisor = inputs[1]->getUpdatedInputValue(obj);
3636
if (divisor == 0.0) {
3737
// Handle division by zero if necessary
3838
return 0.0;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "fields/exponential_function.h"
2+
#include <cmath>
3+
4+
// Constructor
5+
ExponentialFunction::ExponentialFunction(Type* ref, const std::string& name)
6+
: AbstractFunctionDefinition(ref, name, 1, 0.0) {}
7+
8+
void ExponentialFunction::initializeField(Field* field) {
9+
Obj* toObj = field->getObject();
10+
double valueArg0 = inputs[0]->getInputValue(toObj);
11+
12+
field->setValue(std::exp(valueArg0));
13+
}
14+
15+
// Overridden computeUpdate method
16+
double ExponentialFunction::computeUpdate(Obj* obj, FieldLinkDefinition* fl, double u) {
17+
double valueArg0 = inputs[0]->getUpdatedInputValue(obj);
18+
return std::exp(valueArg0) - obj->getFieldValue(this);
19+
}

src/fields/multiplication.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Multiplication::Multiplication(Type* ref, const std::string& name)
66

77
void Multiplication::initializeField(Field* field) {
88
Obj* toObj = field->getObject();
9-
FieldDefinition* fieldDef = field->getFieldDefinition();
10-
std::vector<FieldLinkDefinition*> inputs = fieldDef->getInputs();
119
double valueArg0 = inputs[0]->getInputValue(toObj);
1210
double valueArg1 = inputs[1]->getInputValue(toObj);
1311

src/fields/python_bindings.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "fields/addition.h"
1515
#include "fields/multiplication.h"
1616
#include "fields/division.h"
17+
#include "fields/exponential_function.h"
1718

1819

1920
// ----------------
@@ -61,6 +62,8 @@ PYBIND11_MODULE(aika, m)
6162
// Bind Division (inherits from AbstractFunctionDefinition)
6263
py::class_<Division, AbstractFunctionDefinition>(m, "Division");
6364

65+
py::class_<ExponentialFunction, AbstractFunctionDefinition>(m, "ExponentialFunction");
66+
6467
py::class_<InputField, FieldDefinition>(m, "InputField")
6568
.def(py::init<Type*, const std::string &>())
6669
.def("__str__", [](const InputField &f) {
@@ -101,6 +104,12 @@ PYBIND11_MODULE(aika, m)
101104
const_cast<Type*>(&ref),
102105
name
103106
);
107+
}, py::return_value_policy::reference_internal)
108+
.def("exp", [](const Type &ref, const std::string &name) {
109+
return new ExponentialFunction(
110+
const_cast<Type*>(&ref),
111+
name
112+
);
104113
}, py::return_value_policy::reference_internal);
105114

106115
py::class_<Obj>(m, "Obj")

0 commit comments

Comments
 (0)