Skip to content

Commit 38057a1

Browse files
author
Kasper Peeters
committed
Make 'join' accept more than 2 arguments.
1 parent 2e84ffc commit 38057a1

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

core/algorithms/substitute.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ substitute::substitute(const Kernel& k, Ex& tr, Ex& args_, bool partial)
1919
if(args.is_empty())
2020
throw ArgumentException("substitute: Replacement rule is an empty expression.");
2121

22-
Stopwatch sw;
23-
sw.start();
22+
// Stopwatch sw;
23+
// sw.start();
2424
cadabra::do_list(args, args.begin(), [&](Ex::iterator arrow) {
2525
//args.print_recursive_treeform(std::cerr, arrow);
2626
if(*arrow->name!="\\arrow" && *arrow->name!="\\equals")
@@ -83,8 +83,8 @@ substitute::substitute(const Kernel& k, Ex& tr, Ex& args_, bool partial)
8383
}
8484
return true;
8585
});
86-
sw.stop();
87-
std::cerr << "preparation took " << sw << std::endl;
86+
// sw.stop();
87+
// std::cerr << "preparation took " << sw << std::endl;
8888
}
8989

9090
bool substitute::can_apply(iterator st)

core/pythoncdb/py_ex.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ namespace cadabra {
9797

9898
Ex_ptr Ex_join(const Ex_ptr ex1, const Ex_ptr ex2)
9999
{
100+
// DEPRECATED: no longer in use, can be removed
100101
if (ex1->size() == 0) return ex2;
101102
if (ex2->size() == 0) return ex1;
102103

@@ -130,6 +131,18 @@ namespace cadabra {
130131
return ret;
131132
}
132133
}
134+
135+
Ex_ptr Ex_join(std::vector<Ex_ptr> exs)
136+
{
137+
auto ret = std::make_shared<Ex>("\\comma");
138+
139+
for(const Ex_ptr& ex: exs) {
140+
auto loc = ret->append_child(ret->begin(), ex->begin());
141+
if(*ex->begin()->name=="\\comma")
142+
ret->flatten_and_erase(loc);
143+
}
144+
return ret;
145+
}
133146

134147
Ex_ptr Ex_mul(const Ex_ptr ex1, const Ex_ptr ex2)
135148
{
@@ -689,7 +702,14 @@ namespace cadabra {
689702
.def("from_sympy", &sympy::SympyBridge::import_ex)
690703
;
691704

692-
m.def("join", &Ex_join);
705+
m.def("join", [](const Ex_ptr ex1, const Ex_ptr ex2, py::args args) {
706+
std::vector<Ex_ptr> ex = {ex1, ex2};
707+
for (const auto& arg : args) {
708+
ex.push_back(arg.cast<Ex_ptr>());
709+
}
710+
return Ex_join(ex);
711+
});
712+
693713
m.def("tree", &print_tree);
694714

695715
m.def("map_sympy", &map_sympy_wrapper,

tests/programming.cdb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ def test14():
321321
ex3 = join(ex1, ex2)
322322
assert(ex3==${A,C}$)
323323
print("Test 14e passed")
324+
ex1:= {A,B};
325+
ex2:= C;
326+
ex3:= {D, E};
327+
ex4= join(ex1, ex2, ex3)
328+
assert(ex4==${A,B,C,D,E}$)
329+
print("Test 14f passed")
330+
ex1:= A;
331+
ex2:= C;
332+
ex3:= {D, E};
333+
ex4= join(ex1, ex2, ex3)
334+
assert(ex4==${A,C,D,E}$)
335+
print("Test 14g passed")
324336

325337
test14()
326338

0 commit comments

Comments
 (0)