Skip to content

Commit a44114a

Browse files
committed
Change behaviour of '+' operator for lists.
1 parent ffd0fc1 commit a44114a

File tree

10 files changed

+112
-74
lines changed

10 files changed

+112
-74
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ webup:
1414
cd build; make -f web2/Makefile
1515
doxygen config/Doxyfile
1616
rsync -avz --chmod=+rx doxygen/ cadabra_web:/var/www/cadabra2/doxygen/
17-
cd web2/cadabra2; clay build; rsync -avz --chmod=+rx build/ cadabra_web:/var/www/cadabra2/; rsync -avz --chmod=+rx source/static/styles/ cadabra_web:/var/www/cadabra2/static/styles; rsync -avz --chmod=+rx source/static/images/ cadabra_web:/var/www/cadabra2/static/images/; rsync -avz --chmod=+rx source/static/icons/ cadabra_web:/var/www/cadabra2/static/icons/; rsync -avz --chmod=+rx source/static/pdf/ cadabra_web:/var/www/cadabra2/static/pdf/; rsync -avz --chmod=+rx source/static/scripts/ cadabra_web:/var/www/cadabra2/static/scripts/; rsync -avz --chmod=+r robots.txt cadabra_web:/var/www/cadabra2
17+
cd web2/cadabra2; clay build; rsync -avz --chmod=+rx build/ cadabra_web:/var/www/cadabra2/; rsync -avz --chmod=+rx source/static/styles/ cadabra_web:/var/www/cadabra2/static/styles; rsync -avz --chmod=+rx source/static/fonts/ cadabra_web:/var/www/cadabra2/static/fonts; rsync -avz --chmod=+rx source/static/images/ cadabra_web:/var/www/cadabra2/static/images/; rsync -avz --chmod=+rx source/static/icons/ cadabra_web:/var/www/cadabra2/static/icons/; rsync -avz --chmod=+rx source/static/pdf/ cadabra_web:/var/www/cadabra2/static/pdf/; rsync -avz --chmod=+rx source/static/scripts/ cadabra_web:/var/www/cadabra2/static/scripts/; rsync -avz --chmod=+r robots.txt cadabra_web:/var/www/cadabra2
1818

1919
format:
2020
astyle --style=k/r --indent=tab=3 --recursive --attach-classes --attach-namespaces --indent-classes --indent-namespaces --indent-switches --break-closing-braces '*.hh'

cmake/version.cmake

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
set(CADABRA_VERSION_MAJOR 2)
2-
set(CADABRA_VERSION_MINOR 3)
3-
set(CADABRA_VERSION_PATCH 9)
4-
set(CADABRA_VERSION_TWEAK 1)
5-
set(COPYRIGHT_YEARS "2001-2021")
6-
math(EXPR SYSTEM_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
7-
find_program(GIT git PATHS ${GIT_DIR})
8-
if(GIT)
9-
message("-- Git found: ${GIT}")
10-
execute_process(COMMAND git rev-parse --short HEAD OUTPUT_VARIABLE GIT_SHORT_SHA OUTPUT_STRIP_TRAILING_WHITESPACE)
11-
execute_process(COMMAND git rev-list --count HEAD OUTPUT_VARIABLE GIT_COMMIT_SERIAL OUTPUT_STRIP_TRAILING_WHITESPACE)
12-
execute_process(COMMAND git log -1 --date=short --pretty=format:%cd OUTPUT_VARIABLE GIT_COMMIT_DATE OUTPUT_STRIP_TRAILING_WHITESPACE)
13-
else()
14-
message("-- Git not found, not including commit SHA")
15-
endif()
16-
if(GIT_SHORT_SHA)
17-
set(CADABRA_VERSION_BUILD "${GIT_COMMIT_SERIAL}.${GIT_SHORT_SHA}")
18-
else()
19-
set(CADABRA_VERSION_BUILD "private")
20-
endif()
21-
if(GIT_COMMIT_DATE)
22-
set(CADABRA_VERSION_DATE "${GIT_COMMIT_DATE}")
23-
else()
24-
string(TIMESTAMP THE_DATE "%Y-%m-%d" UTC)
25-
set(CADABRA_VERSION_DATE "${THE_DATE}")
26-
endif()
1+
set(CADABRA_VERSION_MAJOR 2)
2+
set(CADABRA_VERSION_MINOR 3)
3+
set(CADABRA_VERSION_PATCH 9)
4+
set(CADABRA_VERSION_TWEAK 2)
5+
set(COPYRIGHT_YEARS "2001-2022")
6+
math(EXPR SYSTEM_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
7+
find_program(GIT git PATHS ${GIT_DIR})
8+
if(GIT)
9+
message("-- Git found: ${GIT}")
10+
execute_process(COMMAND git rev-parse --short HEAD OUTPUT_VARIABLE GIT_SHORT_SHA OUTPUT_STRIP_TRAILING_WHITESPACE)
11+
execute_process(COMMAND git rev-list --count HEAD OUTPUT_VARIABLE GIT_COMMIT_SERIAL OUTPUT_STRIP_TRAILING_WHITESPACE)
12+
execute_process(COMMAND git log -1 --date=short --pretty=format:%cd OUTPUT_VARIABLE GIT_COMMIT_DATE OUTPUT_STRIP_TRAILING_WHITESPACE)
13+
else()
14+
message("-- Git not found, not including commit SHA")
15+
endif()
16+
if(GIT_SHORT_SHA)
17+
set(CADABRA_VERSION_BUILD "${GIT_COMMIT_SERIAL}.${GIT_SHORT_SHA}")
18+
else()
19+
set(CADABRA_VERSION_BUILD "private")
20+
endif()
21+
if(GIT_COMMIT_DATE)
22+
set(CADABRA_VERSION_DATE "${GIT_COMMIT_DATE}")
23+
else()
24+
string(TIMESTAMP THE_DATE "%Y-%m-%d" UTC)
25+
set(CADABRA_VERSION_DATE "${THE_DATE}")
26+
endif()

core/Cleanup.cc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace cadabra {
4646
changed = changed || res;
4747
if(*it->name=="\\sum") res = cleanup_sumlike(kernel, tr, it);
4848
changed = changed || res;
49+
if(*it->name=="\\comma") res = cleanup_comma(kernel, tr, it);
50+
changed = changed || res;
4951
if(*it->name=="\\components") res = cleanup_components(kernel, tr, it);
5052
changed = changed || res;
5153

@@ -124,7 +126,7 @@ namespace cadabra {
124126
tr.append_child(rhsfrac, div);
125127

126128
it=tr.flatten_and_erase(it);
127-
129+
128130
return true;
129131
}
130132

@@ -413,7 +415,7 @@ namespace cadabra {
413415
while(sib!=tr.end(it)) {
414416
if(*sib->name=="\\equals") ++equalities;
415417
else ++nonequalities;
416-
if(equalities!=0 && nonequalities!=0)
418+
if(equalities!=0 && nonequalities!=0)
417419
throw ConsistencyException("Encountered an equality and a normal term in the same sum; not allowed.");
418420
++sib;
419421
}
@@ -448,9 +450,9 @@ namespace cadabra {
448450
#endif
449451
Ex::iterator tmp1=lhs, tmp2=rhs;
450452
cleanup_sumlike(k, tr, tmp1);
451-
cleanup_sumlike(k, tr, tmp2);
453+
cleanup_sumlike(k, tr, tmp2);
452454
}
453-
455+
454456
// Flatten sums which are supposed to be flat.
455457
long num=tr.number_of_children(it);
456458
if(num==0) {
@@ -855,6 +857,19 @@ namespace cadabra {
855857
return false;
856858
}
857859

860+
bool cleanup_comma(const Kernel& k, Ex& tr, Ex::iterator& it)
861+
{
862+
if(*it->multiplier!=1) {
863+
Ex::sibling_iterator sib = tr.begin(it);
864+
while(sib!=tr.end(it)) {
865+
multiply(sib->multiplier, *it->multiplier);
866+
++sib;
867+
}
868+
one(it->multiplier);
869+
return true;
870+
}
871+
else return false;
872+
}
858873

859874
void cleanup_dispatch_deep(const Kernel& k, Ex& tr, dispatcher_t dispatch)
860875
{

core/Cleanup.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ namespace cadabra {
7676
bool cleanup_diagonal(const Kernel& k, Ex&, Ex::iterator& it);
7777
bool cleanup_kronecker(const Kernel& k, Ex&, Ex::iterator& it);
7878
bool cleanup_exterior_derivative(const Kernel& k, Ex&, Ex::iterator& it);
79+
bool cleanup_comma(const Kernel&k, Ex&, Ex::iterator& it);
7980

8081
/// Given a node with a non-unit multiplier, push this multiplier
8182
/// down the tree if the node is not allowed to have a non-unit

core/PreClean.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/// does not involve the property system.
1111
///
1212
/// - All numerical multipliers in a product on the product node, no
13-
/// multiplier on a sum node.
13+
/// multiplier on a sum node or on the comma node.
1414
///
1515
/// - Any '\\frac' nodes with a purely numerical denominator should be
1616
/// rewritten as a rational multiplier for the numerator node.

core/pythoncdb/py_ex.cc

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,28 @@ namespace cadabra {
6262
if (ex1->size() == 0) return ex2;
6363
if (ex2->size() == 0) return ex1;
6464

65-
bool comma1 = (*ex1->begin()->name == "\\comma");
66-
bool comma2 = (*ex2->begin()->name == "\\comma");
67-
68-
if (comma1 || comma2) {
69-
if (comma1) {
70-
auto ret = std::make_shared<Ex>(*ex1);
71-
auto loc = ret->append_child(ret->begin(), top2);
72-
if (comma2)
73-
ret->flatten_and_erase(loc);
74-
return ret;
75-
}
76-
else {
77-
auto ret = std::make_shared<Ex>(top2);
78-
auto loc = ret->prepend_child(ret->begin(), ex1->begin());
79-
if (comma1)
80-
ret->flatten_and_erase(loc);
81-
return ret;
82-
}
83-
}
84-
else {
65+
// Using '+' for list addition is very illogical; it should add
66+
// componentwise.
67+
// bool comma1 = (*ex1->begin()->name == "\\comma");
68+
// bool comma2 = (*ex2->begin()->name == "\\comma");
69+
70+
// if (comma1 || comma2) {
71+
// if (comma1) {
72+
// auto ret = std::make_shared<Ex>(*ex1);
73+
// auto loc = ret->append_child(ret->begin(), top2);
74+
// if (comma2)
75+
// ret->flatten_and_erase(loc);
76+
// return ret;
77+
// }
78+
// else {
79+
// auto ret = std::make_shared<Ex>(top2);
80+
// auto loc = ret->prepend_child(ret->begin(), ex1->begin());
81+
// if (comma1)
82+
// ret->flatten_and_erase(loc);
83+
// return ret;
84+
// }
85+
// }
86+
// else {
8587
auto ret = std::make_shared<Ex>(*ex1);
8688
if (*ret->begin()->name != "\\sum")
8789
ret->wrap(ret->begin(), str_node("\\sum"));
@@ -90,7 +92,7 @@ namespace cadabra {
9092
auto it = ret->begin();
9193
cleanup_dispatch(*get_kernel_from_scope(), *ret, it);
9294
return ret;
93-
}
95+
// }
9496
}
9597

9698
Ex_ptr Ex_mul(const Ex_ptr ex1, const Ex_ptr ex2)

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ endforeach()
5656
# The main test suite.
5757
#
5858
set(RTESTS
59+
nevaluate
5960
index_positions
6061
forms
6162
packages

tests/algebra.cdb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,16 @@ test33()
660660
# B::Depends(d{#}).
661661
# ex:= d_{B}{A B C};
662662
# unwrap(ex)
663+
664+
# {A,B,C,D,E,F}::NonCommuting;
665+
# ex:= A * (B + C) * D * (E + F);
666+
# distribute(_);
667+
668+
def test34():
669+
__cdbkernel__=create_scope()
670+
ex:= [1,2,3,4];
671+
tst:= -3/2 @(ex);
672+
assert(tst==$[-3/2, -3, -9/2, -6]$)
673+
print("Test 34 passed")
674+
675+
test34()

tests/selecting.cdb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,27 @@ def test01():
3838

3939
test01()
4040

41-
def test02():
42-
__cdbkernel__=create_scope()
43-
a:= A,B,C;
44-
b:= D,E,F;
45-
c:= Q;
46-
d:= R+S;
47-
ex1=a+b
48-
ex2=a+c
49-
ex3=c+a
50-
ex4=a+d
51-
tst1:= {A,B,C,D,E,F} - @(ex1);
52-
tst2:= {A,B,C,Q} - @(ex2);
53-
tst3:= {Q,A,B,C} - @(ex3);
54-
tst4:= {A,B,C,R+S} - @(ex4);
55-
assert(tst1==0)
56-
assert(tst2==0)
57-
assert(tst3==0)
58-
assert(tst4==0)
59-
print("Test 02 passed")
60-
61-
test02()
41+
# def test02():
42+
# __cdbkernel__=create_scope()
43+
# a:= A,B,C;
44+
# b:= D,E,F;
45+
# c:= Q;
46+
# d:= R+S;
47+
# ex1=a+b
48+
# ex2=a+c
49+
# ex3=c+a
50+
# ex4=a+d
51+
# tst1:= {A,B,C,D,E,F};
52+
# tst2:= {A,B,C,Q};
53+
# tst3:= {Q,A,B,C};
54+
# tst4:= {A,B,C,R+S};
55+
# assert(tst1==ex1)
56+
# assert(tst2==ex2)
57+
# assert(tst3==ex3)
58+
# assert(tst4==ex4)
59+
# print("Test 02 passed")
60+
#
61+
# test02()
6262

6363
def test03():
6464
# zoom should also work to select terms in an integral

web2/cadabra2/source/changelog.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ <h1>Change log</h1>
1919
<h3>github master (2.3.9)</h3>
2020
<ul>
2121
<li>Several fixes to <tt>meld</tt> (Dominic).</li>
22+
<li>Make typesetting run in a separate thread to prevent the UI from
23+
feeling sluggish.</li>
24+
<li>Some changes in the behaviour of list addition/multiplication,
25+
to match Mathematica's behaviour.</li>
26+
<li>New <tt>nevaluate</tt> algorithm for fast numerical evaluation
27+
of expressions (work-in-progress).</li>
2228
<li>Fix for Python 3.10.x.</li>
2329
<li>Fix for automatic cleanup of <tt>Diagonal</tt> objects.</li>
2430
<li>Fix crash with tab-completion.</li>

0 commit comments

Comments
 (0)