Skip to content

Commit bbe8803

Browse files
committed
Update docs.
Fix integration by parts with index-less derivatives.
1 parent b8bed82 commit bbe8803

File tree

15 files changed

+148
-46
lines changed

15 files changed

+148
-46
lines changed

client_server/Server.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ std::string Server::run_string(const std::string& blk, bool handle_output)
156156
std::string result;
157157

158158
// Preparse input block.
159-
auto newblk = cadabra::cdb2python(blk);
159+
auto newblk = cadabra::cdb2python(blk, true);
160160

161161
// std::cerr << "PREPARSED:\n" << newblk << std::endl;
162162
// snoop::log("preparsed") << newblk << snoop::flush;

config/Doxyfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ WARN_LOGFILE =
774774
INPUT = doc/main.md \
775775
c++lib \
776776
core \
777+
core/pythoncdb \
777778
core/cadabra2 \
778779
doc/modules.dox \
779780
core/cadabra2_defaults.py \

core/CdbPython.cc

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ std::string cadabra::escape_quotes(const std::string& line)
1616
}
1717

1818

19-
std::string cadabra::cdb2python(const std::string& blk)
19+
std::string cadabra::cdb2python(const std::string& blk, bool display)
2020
{
2121
std::stringstream str(blk);
2222
std::string line;
2323
std::string newblk;
2424
std::string lhs, rhs, op, indent;
2525
while(std::getline(str, line, '\n')) {
26-
std::string res=cadabra::convert_line(line, lhs, rhs, op, indent);
26+
std::string res=cadabra::convert_line(line, lhs, rhs, op, indent, display);
2727
// std::cerr << "preparsed : " + res << std::endl;
2828
if(res!="::empty")
2929
newblk += res+'\n';
3030
}
3131
return newblk;
3232
}
3333

34-
std::string cadabra::convert_line(const std::string& line, std::string& lhs, std::string& rhs, std::string& op, std::string& indent)
34+
std::string cadabra::convert_line(const std::string& line, std::string& lhs, std::string& rhs, std::string& op, std::string& indent, bool display)
3535
{
3636
std::string ret;
3737

@@ -58,7 +58,12 @@ std::string cadabra::convert_line(const std::string& line, std::string& lhs, std
5858
if(line_stripped[0]=='#') return line;
5959

6060
// Bare ';' gets replaced with 'display(_)'.
61-
if(line_stripped==";") return indent_line+"display(_)";
61+
if(line_stripped==";") {
62+
if(display)
63+
return indent_line+"display(_)";
64+
else
65+
return indent_line;
66+
}
6267

6368
// 'lastchar' is either a Cadabra termination character, or empty.
6469
// 'line_stripped' will have that character stripped, if present.
@@ -74,7 +79,8 @@ std::string cadabra::convert_line(const std::string& line, std::string& lhs, std
7479
ret+=" _="+lhs;
7580
}
7681
if(lastchar!=".")
77-
ret = ret + "; display("+lhs+")";
82+
if(display)
83+
ret = ret + "; display("+lhs+")";
7884
indent="";
7985
lhs="";
8086
rhs="";
@@ -124,7 +130,7 @@ std::string cadabra::convert_line(const std::string& line, std::string& lhs, std
124130
+ escape_quotes(line_stripped.substr(found+2)) + "')";
125131
std::string objname = line_stripped.substr(0,found);
126132
ret = ret + "; _="+objname;
127-
if(lastchar==";" && indent_line.size()==0)
133+
if(lastchar==";" && indent_line.size()==0 && display)
128134
ret = ret + "; display("+objname+")";
129135
}
130136
}
@@ -149,7 +155,7 @@ std::string cadabra::convert_line(const std::string& line, std::string& lhs, std
149155
ret = indent_line + "__cdbtmp__ = " + line_stripped.substr(found+2)
150156
+ "(Ex(r'"+escape_quotes(line_stripped.substr(0,found))+"'))";
151157
}
152-
if(lastchar==";")
158+
if(lastchar==";" && display)
153159
ret += "; display(__cdbtmp__)";
154160
}
155161
else {
@@ -158,7 +164,7 @@ std::string cadabra::convert_line(const std::string& line, std::string& lhs, std
158164
}
159165
}
160166
else {
161-
if(lastchar==";")
167+
if(lastchar==";" && display)
162168
ret = indent_line + "_ = " + line_stripped + " display(_)";
163169
else
164170
ret = indent_line + line_stripped;
@@ -167,7 +173,7 @@ std::string cadabra::convert_line(const std::string& line, std::string& lhs, std
167173
return ret+end_of_line;
168174
}
169175

170-
std::string cadabra::cnb2python(const std::string& in_name)
176+
std::string cadabra::cnb2python(const std::string& in_name, bool display)
171177
{
172178
// Read the file into a Json object and get the cells
173179
std::ifstream ifs(in_name);
@@ -200,8 +206,11 @@ std::string cadabra::cnb2python(const std::string& in_name)
200206
std::stringstream s, temp;
201207
s << cell["source"].asString();
202208
std::string line, lhs, rhs, op, indent;
203-
while (std::getline(s, line))
204-
ofs << convert_line(line, lhs, rhs, op, indent) << '\n';
209+
while (std::getline(s, line)) {
210+
auto res = convert_line(line, lhs, rhs, op, indent, display);
211+
if(res!="::empty")
212+
ofs << res << '\n';
213+
}
205214
}
206215
}
207216
// Ensure only symbols defined in this file get exported

core/CdbPython.hh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@ namespace cadabra {
1010
/// \ingroup files
1111
/// Convert a block of Cadabra notation into pure Python. Mimics
1212
/// the functionality in the python script 'cadabra2'
13+
/// If display is false, this will not make ';' characters
14+
/// generate 'display' statements (used in the conversion of
15+
/// notebooks to python packages).
1316

14-
std::string cdb2python(const std::string&);
17+
std::string cdb2python(const std::string&, bool display);
1518

1619
/// \ingroup files
1720
/// As above, but for a single line; for private use only.
21+
/// If display is false, this will not make ';' characters
22+
/// generate 'display' statements (used in the conversion of
23+
/// notebooks to python packages).
1824

19-
std::string convert_line(const std::string&, std::string& lhs, std::string& rhs, std::string& op, std::string& indent);
25+
std::string convert_line(const std::string&, std::string& lhs, std::string& rhs, std::string& op, std::string& indent, bool display);
2026

2127
/// \ingroup files
2228
/// Convert a Cadabra notebook file to pure Python. This gets
2329
/// called on-the-fly when importing Cadabra notebooks written by
2430
/// users, and at install time for all system-supplied packages.
31+
/// If display is false, this will not make ';' characters
32+
/// generate 'display' statements (used in the conversion of
33+
/// notebooks to python packages).
2534

26-
std::string cnb2python(const std::string&);
35+
std::string cnb2python(const std::string&, bool display);
2736
}

core/DisplayTeX.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ DisplayTeX::DisplayTeX(const Kernel& k, const Ex& e)
2929
};
3030

3131
curly_bracket_operators = {
32-
"\\sqrt"
32+
"\\sqrt",
33+
"\\dot"
3334
};
3435
}
3536

core/algorithms/integrate_by_parts.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,19 @@ Algorithm::result_t integrate_by_parts::handle_term(iterator int_it, iterator& i
150150
if(std::next(from)!=to)
151151
from = tr.wrap(from, to, str_node("\\prod"));
152152

153+
// Find the argument of the derivative. Needs to handle derivatives with
154+
// a single index, and derivatives with no index at all.
153155
auto der_arg = tr.begin(fac);
154-
++der_arg;
155-
// This _has_ to be the argument because we have peeled off a single derivative.
156-
assert(der_arg->is_index()==false);
157-
158156
if(der_arg==tr.end(fac))
159157
throw ConsistencyException("integrate_by_parts: Derivative without argument encountered.");
160158

159+
if(der_arg->is_index()) {
160+
++der_arg;
161+
// This _has_ to be the argument because we have peeled off a single derivative.
162+
if(der_arg==tr.end(fac))
163+
throw ConsistencyException("integrate_by_parts: Derivative without argument encountered.");
164+
}
165+
161166
tr.swap(der_arg, from);
162167
tr.swap(fac, der_arg);
163168
multiply(it->multiplier, -1);

core/cadabra2python.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
4141
while(std::getline(file, line))
4242
content+=line+"\n";
4343

44-
auto python = cadabra::cdb2python(content);
44+
auto python = cadabra::cdb2python(content, true);
4545

4646
if(python_file!="") {
4747
std::ofstream pythonfile(python_file);

core/packages/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ set(PACKAGEDIR ${CMAKE_BINARY_DIR}/core/packages/cdb)
1717
#---------------------------------------------------------------------------
1818

1919
set(OLDPACKAGES
20-
relativity/__init__
21-
relativity/schwarzschild
20+
# relativity/__init__
21+
# relativity/schwarzschild
2222
gauge_theory/__init__
2323
gauge_theory/instantons
2424
)
@@ -29,6 +29,7 @@ set(PACKAGES
2929
core/solve
3030
utils/node
3131
sympy/solvers
32+
relativity/schwarzschild
3233
)
3334

3435
set(PDIRS

core/packages/cdb/relativity/schwarzschild.cdb

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"cell_id" : 7004405290148921668,
3+
"cells" :
4+
[
5+
{
6+
"cell_id" : 9654385100491360099,
7+
"cell_origin" : "client",
8+
"cell_type" : "input",
9+
"cells" :
10+
[
11+
{
12+
"cell_id" : 9223372036854775809,
13+
"cell_origin" : "server",
14+
"cell_type" : "latex_view",
15+
"source" : "\\begin{dmath*}{}\\text{Attached property Coordinate to~}\\left[t,~\\discretionary{}{}{} r,~\\discretionary{}{}{} \\phi,~\\discretionary{}{}{} \\theta\\right].\\end{dmath*}"
16+
},
17+
{
18+
"cell_id" : 9223372036854775810,
19+
"cell_origin" : "server",
20+
"cell_type" : "latex_view",
21+
"source" : "\\begin{dmath*}{}\\text{Attached property Indices(position=free) to~}\\left[\\mu,~\\discretionary{}{}{} \\nu,~\\discretionary{}{}{} \\rho,~\\discretionary{}{}{} \\sigma,~\\discretionary{}{}{} \\kappa\\right].\\end{dmath*}"
22+
},
23+
{
24+
"cell_id" : 9223372036854775811,
25+
"cell_origin" : "server",
26+
"cell_type" : "latex_view",
27+
"source" : "\\begin{dmath*}{}\\text{Attached property Metric to~}g_{\\mu \\nu}.\\end{dmath*}"
28+
},
29+
{
30+
"cell_id" : 9223372036854775812,
31+
"cell_origin" : "server",
32+
"cell_type" : "latex_view",
33+
"source" : "\\begin{dmath*}{}\\text{Attached property TableauSymmetry to~}g^{\\mu \\nu}.\\end{dmath*}"
34+
}
35+
],
36+
"source" : "{t,r,\\phi,\\theta}::Coordinate;\n{\\mu,\\nu,\\rho,\\sigma,\\kappa}::Indices(position=fixed, values={t,r,\\phi,\\theta});\ng_{\\mu\\nu}::Metric;\ng^{\\mu\\nu}::InverseMetric;"
37+
},
38+
{
39+
"cell_id" : 12571913938303989352,
40+
"cell_origin" : "client",
41+
"cell_type" : "input",
42+
"source" : "def metric():\n ret := { g_{t t} = -(1-2 M/r), \n g_{r r} = 1/(1-2 M/r), \n g_{\\theta \\theta} = r**2,\n g_{\\phi \\phi} = r**2 \\sin(\\theta)**2 \n };\n return ret"
43+
},
44+
{
45+
"cell_id" : 6301629692701616073,
46+
"cell_origin" : "client",
47+
"cell_type" : "input",
48+
"source" : ""
49+
}
50+
],
51+
"description" : "Cadabra JSON notebook format",
52+
"version" : 1
53+
}

0 commit comments

Comments
 (0)