Skip to content

Commit 31f6aa3

Browse files
implemented summation
1 parent 9dc50c7 commit 31f6aa3

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(SOURCES
2121
src/fields/multiplication.cpp
2222
src/fields/division.cpp
2323
src/fields/exponential_function.cpp
24+
src/fields/summation.cpp
2425
src/fields/field_link_definition.cpp
2526
src/fields/obj.cpp
2627
src/fields/type.cpp

include/fields/summation.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef SUM_H
2+
#define SUM_H
3+
4+
#include "fields/abstract_function_definition.h"
5+
#include "fields/type.h"
6+
#include "fields/obj.h"
7+
8+
class Summation : public AbstractFunctionDefinition {
9+
public:
10+
11+
// Constructor
12+
Summation(Type* ref, const std::string& name);
13+
14+
// Overridden method from AbstractFunctionDefinition
15+
double computeUpdate(Obj* obj, FieldLinkDefinition* fl, double u) override;
16+
};
17+
18+
#endif // SUM_H

install_manifest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/test_object.h
1111
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/test_type.h
1212
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/field.h
13+
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/summation.h
1314
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/step.h
1415
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/input_field.h
1516
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/field_link_definition.h

python-tests/summation-test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 SumTestCase(unittest.TestCase):
11+
12+
def testSum(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+
b = typeA.inputField("b")
27+
28+
c = typeB.add("c")
29+
30+
c.input(TEST_RELATION_FROM, a, 0)
31+
c.input(TEST_RELATION_FROM, b, 1)
32+
33+
registry.flattenTypeHierarchy()
34+
35+
oa = typeA.instantiate()
36+
ob = typeB.instantiate()
37+
38+
aika.TestObj.linkObjects(oa, ob)
39+
ob.initFields()
40+
41+
oa.setFieldValue(a, 50.0)
42+
oa.setFieldValue(b, 20.0)
43+
44+
print("ob.getFieldValue(c):", ob.getFieldValue(c))
45+
46+
self.assertEqual(70.0, ob.getFieldValue(c))
47+
48+
if __name__ == '__main__':
49+
unittest.main()

src/fields/python_bindings.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "fields/multiplication.h"
1616
#include "fields/division.h"
1717
#include "fields/exponential_function.h"
18+
#include "fields/summation.h"
1819

1920

2021
// ----------------
@@ -64,6 +65,9 @@ PYBIND11_MODULE(aika, m)
6465

6566
py::class_<ExponentialFunction, AbstractFunctionDefinition>(m, "ExponentialFunction");
6667

68+
// Bind Summation (inherits from AbstractFunctionDefinition)
69+
py::class_<Summation, AbstractFunctionDefinition>(m, "Summation");
70+
6771
py::class_<InputField, FieldDefinition>(m, "InputField")
6872
.def(py::init<Type*, const std::string &>())
6973
.def("__str__", [](const InputField &f) {
@@ -110,6 +114,12 @@ PYBIND11_MODULE(aika, m)
110114
const_cast<Type*>(&ref),
111115
name
112116
);
117+
}, py::return_value_policy::reference_internal)
118+
.def("sum", [](const Type &ref, const std::string &name) {
119+
return new Summation(
120+
const_cast<Type*>(&ref),
121+
name
122+
);
113123
}, py::return_value_policy::reference_internal);
114124

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

src/fields/summation.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "fields/summation.h"
2+
3+
// Constructor
4+
Summation::Summation(Type* ref, const std::string& name)
5+
: AbstractFunctionDefinition(ref, name, 2, 0.0) {}
6+
7+
// Overridden computeUpdate method
8+
double Summation::computeUpdate(Obj* obj, FieldLinkDefinition* fl, double u) {
9+
return u;
10+
}

0 commit comments

Comments
 (0)