Skip to content

Commit 7a0abe3

Browse files
Implemented div operator
1 parent 43507f1 commit 7a0abe3

File tree

6 files changed

+157
-1
lines changed

6 files changed

+157
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(SOURCES
1919
src/fields/addition.cpp
2020
src/fields/subtraction.cpp
2121
src/fields/multiplication.cpp
22+
src/fields/division.cpp
2223
src/fields/field_link_definition.cpp
2324
src/fields/obj.cpp
2425
src/fields/type.cpp

include/fields/division.h

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

install_manifest.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@
2525
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/flattened_type.h
2626
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/rel_obj_iterator.h
2727
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/queue_key.h
28-
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/queue.h
28+
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/queue.h
29+
/Users/lukasmolzberger/CLionProjects/aika-cpp/.venv/lib/python3.12/site-packages/aika/include/fields/division.h

python-tests/division-test.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import unittest
2+
import sys
3+
import os
4+
from parameterized import parameterized
5+
6+
# Add the project root to Python's module search path
7+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
8+
9+
import aika
10+
11+
class DivisionTestCase(unittest.TestCase):
12+
13+
@parameterized.expand([
14+
(0, "linking_pos_0"),
15+
(1, "linking_pos_1"),
16+
(2, "linking_pos_2")
17+
])
18+
def testDivision(self, linking_pos, test_name):
19+
print("Module 'aika' was loaded from:", aika.__file__)
20+
21+
TEST_RELATION_FROM = aika.RelationOne(1, "TEST_FROM")
22+
TEST_RELATION_TO = aika.RelationOne(2, "TEST_TO")
23+
TEST_RELATION_TO.setReversed(TEST_RELATION_FROM)
24+
TEST_RELATION_FROM.setReversed(TEST_RELATION_TO)
25+
26+
assert isinstance(TEST_RELATION_FROM, aika.Relation)
27+
assert isinstance(TEST_RELATION_TO, aika.Relation)
28+
29+
registry = aika.TypeRegistry()
30+
31+
typeA = aika.TestType(registry, "A")
32+
typeB = aika.TestType(registry, "B")
33+
34+
a = typeA.inputField("a")
35+
b = typeA.inputField("b")
36+
37+
c = typeB.div("c")
38+
39+
print("Type of c:", type(c))
40+
print("Type of TEST_RELATION_FROM:", type(TEST_RELATION_FROM))
41+
print("Type of a:", type(a))
42+
43+
assert isinstance(a, aika.FieldDefinition)
44+
assert isinstance(c, aika.FieldDefinition)
45+
46+
c.input(TEST_RELATION_FROM, a, 0)
47+
c.input(TEST_RELATION_FROM, b, 1)
48+
49+
registry.flattenTypeHierarchy()
50+
51+
oa = typeA.instantiate()
52+
ob = typeB.instantiate()
53+
54+
print("linking_pos:", linking_pos)
55+
56+
if linking_pos == 0:
57+
aika.TestObj.linkObjects(oa, ob)
58+
ob.initFields()
59+
60+
oa.setFieldValue(a, 50.0)
61+
print("oa.getFieldValue(a):", oa.getFieldValue(a))
62+
63+
if linking_pos == 1:
64+
aika.TestObj.linkObjects(oa, ob)
65+
ob.initFields()
66+
67+
oa.setFieldValue(b, 2.0)
68+
print("oa.getFieldValue(b):", oa.getFieldValue(b))
69+
70+
if linking_pos == 2:
71+
aika.TestObj.linkObjects(oa, ob)
72+
ob.initFields()
73+
74+
print("ob.getFieldValue(c):", ob.getFieldValue(c))
75+
76+
print("ob.getFieldAsString(c):", ob.getFieldAsString(c))
77+
78+
self.assertEqual(25.0, ob.getFieldValue(c))
79+
80+
if __name__ == '__main__':
81+
unittest.main()

src/fields/division.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "fields/division.h"
2+
3+
// Constructor
4+
Division::Division(Type* ref, const std::string& name)
5+
: AbstractFunctionDefinition(ref, name, 2, 0.0) {}
6+
7+
void Division::initializeField(Field* field) {
8+
Obj* toObj = field->getObject();
9+
FieldDefinition* fieldDef = field->getFieldDefinition();
10+
std::vector<FieldLinkDefinition*> inputs = fieldDef->getInputs();
11+
double dividend = inputs[0]->getInputValue(toObj);
12+
double divisor = inputs[1]->getInputValue(toObj);
13+
14+
if (divisor == 0.0) {
15+
// Handle division by zero if necessary
16+
return;
17+
}
18+
19+
field->setValue(dividend / divisor);
20+
}
21+
22+
// Overridden computeUpdate method
23+
// This method computes the update for the division operation
24+
// based on which argument is being updated.
25+
double Division::computeUpdate(Obj* obj, FieldLinkDefinition* fl, double u) {
26+
if (fl->getArgument() == 0) {
27+
double divisor = getInputs()[1]->getInputValue(obj);
28+
if (divisor == 0.0) {
29+
// Handle division by zero if necessary
30+
return 0.0;
31+
}
32+
return u / divisor;
33+
} else {
34+
double dividend = getInputs()[0]->getInputValue(obj);
35+
double divisor = getInputs()[1]->getUpdatedInputValue(obj);
36+
if (divisor == 0.0) {
37+
// Handle division by zero if necessary
38+
return 0.0;
39+
}
40+
double oldValue = obj->getFieldValue(this);
41+
return (dividend / divisor) - oldValue;
42+
}
43+
}

src/fields/python_bindings.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "fields/field.h"
1414
#include "fields/addition.h"
1515
#include "fields/multiplication.h"
16+
#include "fields/division.h"
1617

1718

1819
// ----------------
@@ -57,6 +58,9 @@ PYBIND11_MODULE(aika, m)
5758
// Bind Multiplication (inherits from AbstractFunctionDefinition)
5859
py::class_<Multiplication, AbstractFunctionDefinition>(m, "Multiplication");
5960

61+
// Bind Division (inherits from AbstractFunctionDefinition)
62+
py::class_<Division, AbstractFunctionDefinition>(m, "Division");
63+
6064
py::class_<InputField, FieldDefinition>(m, "InputField")
6165
.def(py::init<Type*, const std::string &>())
6266
.def("__str__", [](const InputField &f) {
@@ -91,6 +95,12 @@ PYBIND11_MODULE(aika, m)
9195
const_cast<Type*>(&ref),
9296
name
9397
);
98+
}, py::return_value_policy::reference_internal)
99+
.def("div", [](const Type &ref, const std::string &name) {
100+
return new Division(
101+
const_cast<Type*>(&ref),
102+
name
103+
);
94104
}, py::return_value_policy::reference_internal);
95105

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

0 commit comments

Comments
 (0)