Cadabra Q&A - Recent questions and answers in General questions
http://cadabra.science/qa/qa/general-questions
Powered by Question2AnswerAnswered: Correlation functions, Wick theorem and summation
http://cadabra.science/qa/1012/correlation-functions-wick-theorem-and-summation?show=1018#a1018
<p>I am not sure exactly which summation problem you are trying to solve; maybe you can give a bit more detail?</p>
<p>For the Wick story, here's a bit of code I put together a while ago which may help you get things implemented. It basically gives you a <code>contract</code> function which takes a product of fields and replaces all pairs with propagators. You'll need to expand this of course to do real world problems but the gist of the solution is in here.</p>
<p>First, you need something that takes a list of numbers and returns all possible ways in <br>
which you can pair numbers:</p>
<pre><code>def all_pairs(lst):
if len(lst) < 2:
yield []
return
else:
a = lst[0]
for i in range(1,len(lst)):
pair = (a,lst[i])
for rest in all_pairs(lst[1:i]+lst[i+1:]):
yield [pair] + rest
</code></pre>
<p>That's just a bit of standard Python, nothing Cadabra-specific. Then you need something that takes a cadabra expression and does the contractions in there:</p>
<pre><code>def contract(ex):
amp:=0.
L=list(range(len(ex)))
for c in all_pairs(L):
diag:=1;
for p in c:
a1=ex[p[0]][0]
a2=ex[p[1]][0]
diag *= $G( @(a1) - @(a2) )$
amp += diag
return amp
</code></pre>
<p>Hopefully this makes it clear how to take Cadabra expressions apart and build new ones from the pieces. Then the following works:</p>
<pre><code>ex:=\phi(x_1) \phi(x_2) \phi(x_3) \phi(x_4);
contract(ex);
</code></pre>
<p>gives</p>
<p>$$G\left(x_{1}-x_{2}\right) G\left(x_{3}-x_{4}\right)+G\left(x_{1}-x_{3}\right) G\left(x_{2}-x_{4}\right)+G\left(x_{1}-x_{4}\right) G\left(x_{2}-x_{3}\right)$$</p>
<p>For any serious work you'll probably have to rewrite the <code>all_pairs</code> function so that it is not recursive.</p>
General questionshttp://cadabra.science/qa/1012/correlation-functions-wick-theorem-and-summation?show=1018#a1018Sat, 12 Jan 2019 14:13:48 +0000Answered: whenever make Cadabra do lengthy calculations, I always get error on output
http://cadabra.science/qa/1009/whenever-cadabra-lengthy-calculations-always-error-output?show=1010#a1010
<p>You'll have to make this a bit more specific. Can you email me (info@cadabra.science) a notebook that fails in this way (preferably as short as possible)? </p>
General questionshttp://cadabra.science/qa/1009/whenever-cadabra-lengthy-calculations-always-error-output?show=1010#a1010Wed, 02 Jan 2019 17:42:33 +0000Answered: collect_factors(_) for Symbol indices
http://cadabra.science/qa/998/collect_factors-_-for-symbol-indices?show=999#a999
<p>It should, it's a bug introduced with tidying up some pattern matching functions. I have just pushed a fix for this to github. Thanks for reporting.</p>
General questionshttp://cadabra.science/qa/998/collect_factors-_-for-symbol-indices?show=999#a999Mon, 03 Dec 2018 15:02:11 +0000Answered: Calculating determinants and traces
http://cadabra.science/qa/951/calculating-determinants-and-traces?show=991#a991
<p>There's a <code>Determinant</code> property now which you can use together with <code>complete</code> to compute determinants, as in <a rel="nofollow" href="https://cadabra.science/manual/Determinant.html">https://cadabra.science/manual/Determinant.html</a> .</p>
General questionshttp://cadabra.science/qa/951/calculating-determinants-and-traces?show=991#a991Fri, 23 Nov 2018 21:47:11 +0000Answered: Simple example showing differences between position=free, fixed and independent
http://cadabra.science/qa/937/example-showing-differences-between-position-independent?show=990#a990
<p>See the updated chapter 1.4 of the reference guide, <a rel="nofollow" href="https://cadabra.science/notebooks/ref_indices.html">https://cadabra.science/notebooks/ref_indices.html</a> .</p>
General questionshttp://cadabra.science/qa/937/example-showing-differences-between-position-independent?show=990#a990Fri, 23 Nov 2018 16:14:47 +0000Answered: Multiplying \sqrt{2} by 1/\sqrt{2} crashes the kernel unexpectedly
http://cadabra.science/qa/983/multiplying-sqrt-by-sqrt-2-crashes-the-kernel-unexpectedly?show=984#a984
<p>No, this is a bug. You'd be surprised how many computations can be done without ever encountering this one... Will fix, thanks.</p>
General questionshttp://cadabra.science/qa/983/multiplying-sqrt-by-sqrt-2-crashes-the-kernel-unexpectedly?show=984#a984Mon, 19 Nov 2018 21:17:52 +0000Answered: AntiCommuting/SelfAntiCommuting only for different indices
http://cadabra.science/qa/977/anticommuting-selfanticommuting-only-different-indices?show=979#a979
<p>You would first declare the $a$ and $a^\dagger$ operators to be anti-commuting among themselves, so</p>
<pre><code>{n,m,p,q}::Indices(position=free);
a_{n}::SelfAntiCommuting;
ad_{n}::SelfAntiCommuting;
ad_{n}::LaTeXForm("a^\dagger").
</code></pre>
<p>where the last line is just to make the <code>ad_{n}</code> print nicely. Then ensure that the default is for daggered and non-daggered operators to be non-commuting,</p>
<pre><code>{a_{n}, ad_{m}}::NonCommuting;
</code></pre>
<p>Now the only thing you need to do is to decide what you want to do with expressions which involve $a$ and $a^\dagger$. In many cases, the logic is to 'move' all $a$ to the far right of an expression (so they can annihilate a vacuum state on which they act). You can do that with the rule</p>
<pre><code>\delta_{m n}::KroneckerDelta;
rl:= a_{m} ad_{n} -> - ad_{n} a_{m} + \delta_{n m};
</code></pre>
<p>You then apply this rule repeatedly to an expression, distributing products at every step and eliminating Kronecker deltas. If you want you can do this automatically at every step of a calculation, by sticking that logic into the <code>post_process</code> function. Example:</p>
<pre><code>def post_process(ex):
# move all a operators to the right
converge(ex):
substitute(ex, rl)
distribute(ex)
eliminate_kronecker(ex)
sort_product(ex)
collect_terms(ex)
</code></pre>
<p>If you now enter e.g. the expression</p>
<pre><code>ex:= a_{n} ad_{p} a_{m} ad_{m} a_{n};
</code></pre>
<p>you get the answer</p>
<p>$$ -a^\dagger_m a_m a_p + \delta_{m m} a_p .$$</p>
<p>Hope this helps.</p>
General questionshttp://cadabra.science/qa/977/anticommuting-selfanticommuting-only-different-indices?show=979#a979Mon, 12 Nov 2018 20:54:18 +0000Answered: drop_weight before distribute
http://cadabra.science/qa/954/drop_weight-before-distribute?show=955#a955
<p>The logic here is intentional: <code>drop_weight</code> only acts on terms with a well-defined weight, and the $\epsilon (\epsilon^3 + 5)$ term is not of that type. So at the moment the answer is 'no'.</p>
<p>However, if your problem is that doing <code>distribute</code> is going to mess up too many other terms, you can try zooming into the expression and only distributing terms which match a certain pattern. E.g.</p>
<pre><code>\epsilon::Weight(label=field, value=1);
Exp:=(A+B)*\epsilon**2+\epsilon*(\epsilon**3+5);
zoom(_, $\epsilon Q??$);
distribute(_);
unzoom(_);
drop_weight(_, $field=4$);
</code></pre>
<p>would produce $(A+B)\epsilon^2 + 5\epsilon$, with the brackets in the first term intact.</p>
General questionshttp://cadabra.science/qa/954/drop_weight-before-distribute?show=955#a955Mon, 15 Oct 2018 10:09:53 +0000Answered: Defining the Rarita-Schwinger action: Action outputs 0
http://cadabra.science/qa/898/defining-the-rarita-schwinger-action-action-outputs-0?show=899#a899
<p>It's mostly an issue with not putting brackets at the right places. While Cadabra uses TeX notation, you sometimes have to be a little more explicit than strictly necessary in TeX, in order to avoid ambiguity. So you have to write e.g.</p>
<pre><code>\bar{\psi_{\mu}}
</code></pre>
<p>so that it is clear to Cadabra that the <code>\psi_{\mu}</code> is an 'argument' of <code>\bar</code>. Ditto for <code>\partial</code>, which you need to write as</p>
<pre><code>\partial_{\nu}{ \psi_{\rho} }
</code></pre>
<p>so that the <code>\psi_{\rho}</code> explicitly becomes an 'argument' of <code>\partial</code>. In your example, the <code>\partial_{\nu}\psi_{\rho}</code> was interpreted as a derivative acting on nothing, multiplied with <code>\psi_{\rho}</code>, giving nothing.</p>
<p>Here's a tidied-up version which works:</p>
<pre><code>#Rarita-Scwhinger Action
def post_process(ex):
sort_product(ex)
eliminate_kronecker(ex)
canonicalise(ex)
collect_terms(ex)
{\mu,\nu,\rho}::Indices(position=independent);
x::Coordinate;
\Gamma{#}::GammaMatrix(metric=\eta);
\eta_{\mu\nu}::Metric;
\partial{#}::Derivative;
\psi_{\mu}::Spinor;
\bar{#}::DiracBar;
\psi_{\mu}::Depends(x);
S:=\int{\bar{\psi_{\mu}} \Gamma^{\mu\nu\rho} \partial_{\nu}{\psi_{\rho}} }{x};
</code></pre>
<p>I have fixed a few typos and also changed the index type to 'independent' to prevent Cadabra from raising/lowering index pairs (usually better for susy computations, as you can then keep all indices on the Gamma's upstairs and more easily convert them using vielbeine).</p>
General questionshttp://cadabra.science/qa/898/defining-the-rarita-schwinger-action-action-outputs-0?show=899#a899Mon, 20 Aug 2018 10:03:08 +0000Answered: Partial derivative of exp()
http://cadabra.science/qa/895/partial-derivative-of-exp?show=896#a896
<p>This is something somewhere in the middle between an abstract and a component computation, and I haven't yet settled completely on what is the best way to handle this. For the time being I would suggest you use a simple substitution rule, e.g.</p>
<pre><code>\partial{#}::PartialDerivative;
test:=\partial_{\mu}{\exp(i*x^{\lambda}*k_{\lambda})};
rl:= \partial_{\mu}{ \exp( A?? ) } -> \exp( A?? ) \partial_{\mu}{ A?? };
substitute(test, rl);
product_rule(test);
substitute(test, $\partial_{\mu}{x^{\lambda}} -> \delta_{\mu}^{\lambda}$ );
unwrap(test);
</code></pre>
<p>That gets you almost there, but <code>eliminate_kronecker</code> which you would now want to use cannot handle the duplicate dummy index pair. So you'll need another substitution to get rid of the Kronecker delta.</p>
<p>Good example though, will see if we can make this work more easily.</p>
General questionshttp://cadabra.science/qa/895/partial-derivative-of-exp?show=896#a896Fri, 17 Aug 2018 09:22:45 +0000Answered: Is number_of_terms deprecated?
http://cadabra.science/qa/892/is-number_of_terms-deprecated?show=893#a893
<p>Yes, use standard Python <code>len</code>:</p>
<pre><code>ex:=A+B+C;
len(ex);
</code></pre>
<p>shows '3'.</p>
General questionshttp://cadabra.science/qa/892/is-number_of_terms-deprecated?show=893#a893Thu, 16 Aug 2018 07:03:49 +0000Answered: How to evaluate components of covariant derivative
http://cadabra.science/qa/885/how-to-evaluate-components-of-covariant-derivative?show=887#a887
<p>Yes you will need to determine the components of the metric and Christoffel symbols first, following e.g. the Schwarzschild tutorial notebook. You then need to expand your covariant derivatives into partial derivatives and Christoffels. You can do that with a simple substitution rule if you only have vectors and co-vectors, or you can use a more generic routine to do arbitrary tensors; see the example at the bottom of</p>
<p><a rel="nofollow" href="https://cadabra.science/notebooks/ref_programming.html">https://cadabra.science/notebooks/ref_programming.html</a></p>
<p>for more on that. This is something that will become available in the form of a package in 2.2.2 or 2.2.4.</p>
General questionshttp://cadabra.science/qa/885/how-to-evaluate-components-of-covariant-derivative?show=887#a887Sun, 12 Aug 2018 09:00:34 +0000Answered: Operator acting on tensors
http://cadabra.science/qa/879/operator-acting-on-tensors?show=880#a880
<p>Adding indices to object wildcards is not supported because there are very few cases where that actually makes much sense. The problem is that, if your rule had worked, it would also match</p>
<pre><code> met{ A_{b} }{ B_{b} }
met{ A_{b c d} }{ B_{c} }
</code></pre>
<p>and so on. You then almost always want to have control over where the extra $a$ index would need to be added (at the beginning of the set? at the end? in the middle?) and the notation which you wanted to use does not leave any way to specify that location. </p>
<p>So instead, you will need to make a rule which does not take an arbitrary object, but something which has the index structure spelled out. If you had in mind just to act on symbols $V$ and $W$, the rule</p>
<pre><code>Expmet:= met{X?}{Y?} -> X?_{a} Y?_{a};
</code></pre>
<p>would work. This will apply to your <code>met{V}{W}</code> case, but will not apply to any of the cases at the top of this reply. You could, however, write e.g.</p>
<pre><code>Expmet2:= met{ X?_{b} }{ Y?_{b} } -> X?_{b a} Y?_{a b};
</code></pre>
<p>or similar. That rule would allow you to do</p>
<pre><code>{a,b,c,d}::Indices;
ex:= met{ A_{c} }{ B_{c} };
exmet:= met{ X?_{b} }{ Y?_{b} } -> X?_{b a} Y?_{a b} ;
substitute(ex, exmet);
</code></pre>
<p>Hope the logic is clear.</p>
<p>So <strong>TL;DR</strong>: object patterns (patterns with '<code>??</code>' attached) are very brute force hammers, not always suitable when name patterns (patterns with just '<code>?</code>') will do.</p>
<p>(In addition, your example triggered a parsing bug that led to the indices being put on the product, not on the individual tensors, but that's a different story).</p>
General questionshttp://cadabra.science/qa/879/operator-acting-on-tensors?show=880#a880Sat, 11 Aug 2018 10:15:16 +0000Answered: How to evaluate the tensor of energy momentum for provided metric anzatz and 4-velocity
http://cadabra.science/qa/873/evaluate-energy-momentum-provided-metric-anzatz-velocity?show=874#a874
<p>Combine the rules for <code>u</code> and <code>g</code> into one call to <code>evaluate</code>, as in</p>
<pre><code>evaluate(T, u+g, rhsonly=True);
</code></pre>
General questionshttp://cadabra.science/qa/873/evaluate-energy-momentum-provided-metric-anzatz-velocity?show=874#a874Thu, 09 Aug 2018 16:34:53 +0000Answered: Code for general Einstein field equations with diagonal metric anzatc
http://cadabra.science/qa/870/general-einstein-field-equations-with-diagonal-metric-anzatc?show=871#a871
<p>Thanks for reporting this. The main issue is a bug in computing dependencies of expressions like <code>exp(F(r))</code>, which fail to figure out that this depends on <code>r</code>. I have just pushed a fix to github which solves this (your notebook then reproduces the Ricci tensor/scalar results in that PDF file). </p>
<p>The last error (when computing the Einstein tensor) is something weird in the bridge to sympy. Will look into that.</p>
General questionshttp://cadabra.science/qa/870/general-einstein-field-equations-with-diagonal-metric-anzatc?show=871#a871Wed, 08 Aug 2018 10:25:13 +0000Answered: Unwrap applied on expresion with partial derivative and two single variable functions
http://cadabra.science/qa/854/applied-expresion-partial-derivative-variable-functions?show=855#a855
<p>It's a bug :-( That is to say, your code should have read</p>
<pre><code>{x,y}::Coordinate.
\partial{#}::PartialDerivative.
f::Depends(x).
g::Depends(y).
ex:=\partial_{x}{A*f*g};
unwrap(_);
ex:=\partial_{y}{A*f*g};
unwrap(_);
</code></pre>
<p>(changed the two <code>Depends</code> lines) but even then it would produce the wrong answer.</p>
<p>This got introduced in the 1.x -> 2.x rewrite and somehow escaped the tests. I have just pushed a fix to github now which fixes this. </p>
General questionshttp://cadabra.science/qa/854/applied-expresion-partial-derivative-variable-functions?show=855#a855Mon, 06 Aug 2018 10:40:54 +0000Answered: how can i tell cadabra that the partial derivatives commute upto linear order while calculating Riemann Tensor
http://cadabra.science/qa/850/cadabra-partial-derivatives-commute-calculating-riemann?show=851#a851
<p>That one had me puzzled for a minute... The solution is to match your brackets: both terms should have an additional <code>}</code> to close the argument of the outmost derivative. So</p>
<pre><code>\partial{#}::PartialDerivative;
c1:= \partial_{\alpha}{\partial_{\beta}{h_{\mu \nu}}} =
\partial_{\beta}{\partial_{\alpha}{h_{\mu \nu}}};
</code></pre>
<p>And to answer the question in the subject: you can then make these terms equal with a call to <code>canonicalise(c1)</code>.</p>
General questionshttp://cadabra.science/qa/850/cadabra-partial-derivatives-commute-calculating-riemann?show=851#a851Wed, 01 Aug 2018 19:32:20 +0000Answered: Exponential series (or string type to expression type)
http://cadabra.science/qa/835/exponential-series-or-string-type-to-expression-type?show=836#a836
<p>That's easy: you can create a Cadabra expression object from a string by creating an <code>Ex</code> object with the string as argument. So make your last line read</p>
<pre><code>return Ex(expOutput)
</code></pre>
<p>and the result will be a Cadabra expression. </p>
<p>Should probably go into the reference guide, thanks for reminding me ;-)</p>
General questionshttp://cadabra.science/qa/835/exponential-series-or-string-type-to-expression-type?show=836#a836Tue, 24 Jul 2018 17:22:10 +0000Answered: Edit the output
http://cadabra.science/qa/823/edit-the-output?show=824#a824
<p>No, you can't, but that's a deliberate decision, as cutting and pasting makes your<br>
notebook very unreproducible (you cannot change something at the top and have the<br>
effects of that change automatically propagate to the remainder). But there is a better<br>
(IMHO) way to do something with a similar effect. I have started writing something more about this a chapter of the new reference manual, see</p>
<p><a rel="nofollow" href="https://cadabra.science/notebooks/ref_selecting.html">https://cadabra.science/notebooks/ref_selecting.html</a></p>
General questionshttp://cadabra.science/qa/823/edit-the-output?show=824#a824Sat, 14 Jul 2018 16:22:30 +0000Answered: Variable can be handled like values?
http://cadabra.science/qa/813/variable-can-be-handled-like-values?show=814#a814
<p>Yes, just make sure that you declare <code>z</code> to be a Coordinate, so add as your first line</p>
<pre><code>z::Coordinate;
</code></pre>
<p>and then all works as you expected.</p>
General questionshttp://cadabra.science/qa/813/variable-can-be-handled-like-values?show=814#a814Tue, 10 Jul 2018 20:18:09 +0000Answered: How can I implement cyclicity of trace?
http://cadabra.science/qa/729/how-can-i-implement-cyclicity-of-trace?show=802#a802
<p>Not perfect... but helpful.</p>
<p>A time ago I was trying something related to your question. After a while of looking on the web (and asking... just like you!) I came out with the following: define a circular function.</p>
<p>Let $a$ and $b$ be noncommuting, but distributable</p>
<pre><code>{a,b}::NonCommuting.
{a,b}::Distributable.
def post_process(ex):
expand_power(ex)
distribute(ex)
</code></pre>
<p>I define the function</p>
<pre><code>def isCircular(arr1, arr2):
return arr1 in arr2+' '+arr2
</code></pre>
<p>and a expression to test</p>
<pre><code>expr := (a + b)**2;
</code></pre>
<p>The trick is now to convert this to a string, split it, and compare term by term if they are circular. <strong>NOTE:</strong> blank spaces before and after the plus sign inside the <code>split</code> method.</p>
<pre><code>list = str(expr)
list = str(expr).split(' + ')
list;
for i in range(len(list)):
for j in range(i+1,len(list)):
if isCircular( list[i], list[j]):
list[j] = list[i]
list;
</code></pre>
<p>Then, convert it back to a <em>cadabra</em> expression to finally simplify. <strong>NOTE:</strong> the <code>newexpr</code> is initalised as an empty expression, it is <em>not</em> a single double quote, but double single quotes. </p>
<pre><code>newexpr = '';
for word in list:
newexpr = newexpr + ' + ' + word
;
cab_expr = Ex(newexpr)
collect_terms(cab_expr);
</code></pre>
<p>I tried something like <code>expr := (a b + b a)**2;</code> and the result is almost there! It could be a starting point!!!</p>
<p>Cheers.</p>
General questionshttp://cadabra.science/qa/729/how-can-i-implement-cyclicity-of-trace?show=802#a802Wed, 04 Jul 2018 09:51:43 +0000Answered: Gamma_5 matrix
http://cadabra.science/qa/783/gamma_5-matrix?show=800#a800
<p>If you're working on four dimensions, $\gamma^5$ is proportional to the product of the four (elemental) gamma's. Therefore you can define something like</p>
<pre><code>{s,r,l,k,m,n,p}::Indices(vector);
{s,r,l,k,m,n,p}::Integer(0..3);
\Gamma_{#}::GammaMatrix(metric=\delta);
e_{\mu\nu\lambda\rho}::EpsilonTensor(delta=\delta);
\delta_{m n}::KroneckerDelta;
g5 := e_{l m n p} \Gamma^{l m n p};
</code></pre>
<p>You have to normalise this definition accordingly. </p>
<p>Remember that <em>Cadabra</em> has a definition for the imaginary number</p>
<pre><code>i::ImaginaryI;
</code></pre>
General questionshttp://cadabra.science/qa/783/gamma_5-matrix?show=800#a800Tue, 03 Jul 2018 15:30:11 +0000Answered: Simple commutator in quantum mechanics
http://cadabra.science/qa/798/simple-commutator-in-quantum-mechanics?show=799#a799
<p>The <code>simplify</code> algorithm invokes Sympy, but does not (yet) translate all Cadabra properties to Sympy ones (Cadabra's properties are more general, so the map isn't even always possible, but of course in this particular case that's not the problem). For the time being, avoid <code>simplify</code>, as it will give wrong answers with non-commuting objects.</p>
<p>To do computations with non-commuting objects, you may want to have a look at the tutorial on the Poincare algebra, <a rel="nofollow" href="https://cadabra.science/notebooks/poincare_algebra.html">https://cadabra.science/notebooks/poincare_algebra.html</a> . That's a bit more advanced than what you asked for, but perhaps it gets you in the right direction. If not, please ask again and tell in a bit more detail what you want to compute.</p>
General questionshttp://cadabra.science/qa/798/simple-commutator-in-quantum-mechanics?show=799#a799Mon, 02 Jul 2018 23:03:20 +0000Answered: Cadabra 2 syntax for "<<"
http://cadabra.science/qa/776/cadabra-2-syntax-for?show=778#a778
<p>Dominic has figured out how to do this with the standard Python import functionality. I have just pushed some experimental code to github which does this. With this you can import notebooks into other notebooks. For example, if you have a <code>library.cnb</code> notebook containing a cell</p>
<p><code>library.cnb</code>:</p>
<pre><code>{m, n}::Indices;
ex:=A_{m n};
</code></pre>
<p>you can import this expression and the property declaration into another notebook doing</p>
<pre><code>from library import *
</code></pre>
<p>This will auto-convert your <code>library.cnb</code> into a <code>library.py</code> file which is then imported.</p>
<p>As I said: experimental. Any feedback welcome.</p>
General questionshttp://cadabra.science/qa/776/cadabra-2-syntax-for?show=778#a778Mon, 11 Jun 2018 18:27:07 +0000Answered: Custom symbol name as index
http://cadabra.science/qa/774/custom-symbol-name-as-index?show=775#a775
<p>Ouch, that was a <em>very</em> trivial typo in the 2.x code, but sufficient to mess up all LaTeXForm'ed symbols in sub/superscripts. Now fixed in github. Thanks!</p>
General questionshttp://cadabra.science/qa/774/custom-symbol-name-as-index?show=775#a775Sun, 10 Jun 2018 09:49:17 +0000Answered: "g" not registered.
http://cadabra.science/qa/758/g-not-registered?show=759#a759
<p>Can you send me (info@cadabra.science) a minimal notebook which exhibits the problem?</p>
General questionshttp://cadabra.science/qa/758/g-not-registered?show=759#a759Tue, 05 Jun 2018 13:22:37 +0000Answered: Python and greek indices
http://cadabra.science/qa/736/python-and-greek-indices?show=737#a737
<p>Do you really need the Python form, or is the Cadabra input form perhaps better? The Python form gives greek indices in the form of Unicode characters, which the Cadabra notebook does not print because the LaTeX backend does not handle them correctly. But you can do</p>
<pre><code>ex:=\alpha;
ex.input_form();
</code></pre>
<p>which will produce</p>
<pre><code>\alpha
</code></pre>
<p>You can feed that back into Cadabra, e.g.</p>
<pre><code>ex2=Ex("2 * "+ex.input_form());
</code></pre>
<p>which prints $2\alpha$.</p>
General questionshttp://cadabra.science/qa/736/python-and-greek-indices?show=737#a737Mon, 14 May 2018 19:22:25 +0000Answered: Einsteinify does not work in a sumo terms?
http://cadabra.science/qa/723/einsteinify-does-not-work-in-a-sumo-terms?show=724#a724
<p><code>einsteinify</code> by default (incorrectly) does not go down the expression tree, so it only sees the sum, not the terms making up the sum. Try</p>
<p>einsteinify(ex, deep=True);</p>
<p>to force it to go lower and see the individual terms.</p>
General questionshttp://cadabra.science/qa/723/einsteinify-does-not-work-in-a-sumo-terms?show=724#a724Mon, 07 May 2018 15:55:45 +0000Answered: Wick contraction and derivatives
http://cadabra.science/qa/706/wick-contraction-and-derivatives?show=711#a711
<p>There is a bit of discussion on how to do Fourier transforms in <a rel="nofollow" href="https://cadabra.science/qa/605/fourier-transform,">https://cadabra.science/qa/605/fourier-transform,</a> but there are better ways now. I'll need a bit more time to write this up properly, but you can look in the <code>tests/programming.cdb</code> file if you are in a hurry (it's the <code>test04</code> function which contains the logic).</p>
General questionshttp://cadabra.science/qa/706/wick-contraction-and-derivatives?show=711#a711Wed, 02 May 2018 18:09:07 +0000Answered: young_project a single term in a product
http://cadabra.science/qa/708/young_project-a-single-term-in-a-product?show=709#a709
<p>There is something not quite right in the way <code>young_project_tensor</code> puts the indices in their position (you can do <code>display(ex1)</code> to see this). I would suggest that you write all tensors with either all lower or all upper indices. That will avoid this particular problem until it is fixed.</p>
General questionshttp://cadabra.science/qa/708/young_project-a-single-term-in-a-product?show=709#a709Wed, 02 May 2018 14:29:38 +0000Answered: The algorithm JOIN seems to be incorrect
http://cadabra.science/qa/701/the-algorithm-join-seems-to-be-incorrect?show=702#a702
<p>You need to separate index names by a space,</p>
<pre><code>{m,n,p,q}::Indices;
\Gamma_{#}::GammaMatrix(metric=g);
ex:= \Gamma_{m n} \Gamma_{p};
join_gamma(_);
</code></pre>
General questionshttp://cadabra.science/qa/701/the-algorithm-join-seems-to-be-incorrect?show=702#a702Sun, 29 Apr 2018 19:35:35 +0000Answered: Should substitute act on indices?
http://cadabra.science/qa/609/should-substitute-act-on-indices?show=696#a696
<p>This is now fixed on github, the above produces </p>
<p>$$y^{r} y^{s} A_{r s} .$$</p>
<p>If you ever wanted to replace the <code>s</code> index instead (not recommendable, you never know whether the next run still calls this dummy index <code>s</code>), you need to do</p>
<pre><code>substitute (abc,$^{s}->^{1}$);
</code></pre>
General questionshttp://cadabra.science/qa/609/should-substitute-act-on-indices?show=696#a696Fri, 27 Apr 2018 17:31:42 +0000Answered: combine algorith for Gamma Matrices
http://cadabra.science/qa/687/combine-algorith-for-gamma-matrices?show=692#a692
<p><code>combine</code> is in a bit of a weird state right now, it will need a bit of work to make it do the right thing in all cases. Will keep you posted.</p>
General questionshttp://cadabra.science/qa/687/combine-algorith-for-gamma-matrices?show=692#a692Mon, 23 Apr 2018 20:27:34 +0000Answered: Scalar parameters
http://cadabra.science/qa/688/scalar-parameters?show=691#a691
<p>Use the <code>factor_in</code> algorithm, as in</p>
<pre><code>{i,j,k}::Indices(vector);
_ := S^{i}_{j}^{k} S_{i}^{j}_{k} + 15*N*S^{i j k} S_{i j k};
canonicalise(_);
factor_in(_, $N$);
</code></pre>
<p>which produces the result you wanted. </p>
<p>(incidentally, Cadabra makes no distinction between 'parameters' and 'rank-0 tensors', at least not when it comes to this kind of problem).</p>
General questionshttp://cadabra.science/qa/688/scalar-parameters?show=691#a691Mon, 23 Apr 2018 08:08:19 +0000Answered: Contractions in derivatives
http://cadabra.science/qa/689/contractions-in-derivatives?show=690#a690
<p>Cadabra does not make any assumptions about the symbol you use for the derivative. You need to explicitly declare <code>\nabla</code> to be a derivative:</p>
<pre><code>{i,j,k}::Indices(vector);
\nabla{#}::Derivative;
_:= \nabla^{i}{v_{i}} - 2 \nabla_{i}{v^{i}} ;
canonicalise(_);
</code></pre>
<p>does the trick.</p>
General questionshttp://cadabra.science/qa/689/contractions-in-derivatives?show=690#a690Mon, 23 Apr 2018 08:04:19 +0000Answered: Using Pythons function to modify expressions
http://cadabra.science/qa/684/using-pythons-function-to-modify-expressions?show=685#a685
<p>That's standard Python behaviour. The <code>obj</code> parameter of <code>foo</code> is a 'handle' to the expression object which you pass in. Any changes that you make to the expression 'pointed to' by <code>obj</code> will be visible outside <code>foo</code>. But assigning <code>obj</code> to something else just changes the handle. After the <code>obj=ans</code> line, <code>obj</code> no longer points to the expression you wanted to modify, but instead it points to the expression which was created on the first line of <code>foo</code>. </p>
<p>This pure-Python example does the same thing:</p>
<pre><code>def foo(obj):
bar = [5,6,7]
obj = bar
print(obj)
x = [1,2,3]
foo(x)
print(x)
</code></pre>
<p>See e.g. <a rel="nofollow" href="https://stackoverflow.com/questions/22054698/python-modifying-list-inside-a-function">https://stackoverflow.com/questions/22054698/python-modifying-list-inside-a-function</a> for more on this.</p>
<p>So the question now is how to change the object 'pointed to' by <code>obj</code>. Just like with the list example, you have two options. Either do as in your <code>bah</code>. Or directly manipulate 'elements of the object pointed to by <code>obj</code>'. That is effectively what you do in <code>kludge</code>, but yes, it is kludgy.</p>
<p>There are some methods to manipulate the object pointed to by <code>obj</code> directly, but they are not ready for prime time yet (I'll update this answer later when I have more time). So for the time being, do as in <code>bah</code>.</p>
General questionshttp://cadabra.science/qa/684/using-pythons-function-to-modify-expressions?show=685#a685Thu, 19 Apr 2018 13:51:20 +0000Answered: Contraction of indices in equations
http://cadabra.science/qa/679/contraction-of-indices-in-equations?show=680#a680
<p>That's a bug, thanks for reporting it. Try</p>
<pre><code>_:= N = g_{i}^{i} + 1
eliminate_kronecker(_);
</code></pre>
<p>It's the edge-case of a single term with a single factor on the rhs which is not handled correctly. Will fix.</p>
General questionshttp://cadabra.science/qa/679/contraction-of-indices-in-equations?show=680#a680Wed, 18 Apr 2018 06:15:44 +0000Answered: Upper, lower, mixed indices - clarification needed
http://cadabra.science/qa/663/upper-lower-mixed-indices-clarification-needed?show=665#a665
<p>It behaves as expected; for historical reasons <code>M{#}</code> means 'M with any arguments or indices, regardless of position'. <code>M^{#}</code> or <code>M_{#}</code> is interpreted in the same way. It's used a lot to declare <code>\delta{#}::KroneckerDelta</code>, in which case you really need it to mean 'both upper and lower indices'. </p>
<p>Perhaps this historical choice was not a particularly good one, but I'm reluctant to change it now. I could potentially make <code>M^{#}</code> and <code>M_{#}</code> mean what you thought they would mean, because that should not clash with existing notebooks. Let me think this through a bit more.</p>
<p>For the time being, your only options are to either declare <code>M^{A B}::AntiSymmetric</code> explicitly (in which case you have to repeat it for any number of indices that you need) or accept that it behaves as in your example.</p>
General questionshttp://cadabra.science/qa/663/upper-lower-mixed-indices-clarification-needed?show=665#a665Tue, 10 Apr 2018 09:17:08 +0000Answered: How to use Mathematica backend (in Ubuntu 16.04)?
http://cadabra.science/qa/654/how-to-use-mathematica-backend-in-ubuntu-16-04?show=655#a655
<p>Brown paper bag situation. I simplified some of the source just before release of 2.2.0, which had the side effect that <code>simplify</code> does not see the Mathematica backend... I have just pushed changes to github which fix this (and your example works with those fixes).</p>
<p>If you don't want to recompile, try</p>
<pre><code>map_mma(_, "FullSimplify")
</code></pre>
<p>which does work with 2.2.0 (but is of course less clean because you can't switch back to Sympy easily).</p>
General questionshttp://cadabra.science/qa/654/how-to-use-mathematica-backend-in-ubuntu-16-04?show=655#a655Thu, 05 Apr 2018 08:28:21 +0000Answered: split a fraction
http://cadabra.science/qa/630/split-a-fraction?show=631#a631
<p>If you type</p>
<pre><code>ex:=(a+b+c+d+e)/e;
</code></pre>
<p>Cadabra will store this as $(a + b + c + d + e) * e^{-1}$, which you can then get to your form by doing</p>
<pre><code>distribute(_);
</code></pre>
General questionshttp://cadabra.science/qa/630/split-a-fraction?show=631#a631Wed, 04 Apr 2018 11:00:13 +0000Answered: join_gamma features
http://cadabra.science/qa/623/join_gamma-features?show=626#a626
<p>That's still missing in 2.x, you're the first one to notice, should put it a bit higher on the todo list.</p>
<p>For most practical applications, you can get around it by doing a full join and then using <code>take_match</code>, e.g. doing something like</p>
<pre><code>{a,b,c,d,e,f}::Indices;
\Gamma{#}::GammaMatrix(metric=\delta);
ex:= \Gamma_{a b c} \Gamma_{d e};
join_gamma(_);
take_match(_, $Q?? \Gamma_{a b c}$);
</code></pre>
<p>which selects the 3-gamma terms (drop the semicolon ';' on the <code>join_gamma</code> to prevent display of the full join). The <code>Q??</code> wildcard in the last line is needed to select all other factors.</p>
General questionshttp://cadabra.science/qa/623/join_gamma-features?show=626#a626Wed, 04 Apr 2018 09:19:19 +0000Answered: Error : No module named Sympy
http://cadabra.science/qa/622/error-no-module-named-sympy?show=624#a624
<p>You need to install the python 'sympy' module. On which system/distribution are you running this?</p>
General questionshttp://cadabra.science/qa/622/error-no-module-named-sympy?show=624#a624Wed, 04 Apr 2018 08:46:42 +0000Answered: Fourier Transform
http://cadabra.science/qa/605/fourier-transform?show=617#a617
<p>I have checked in some experimental functionality to modify the expression tree from Python. If you pull the latest from github, your Fourier transform can now be done with</p>
<pre><code>\partial{#}::PartialDerivative;
ex:=A_{m n} \partial_{r}{ B_{p q} } \partial_{s}{ C } ;
for prod in ex["\\prod"]:
num=1
for factor in prod.children():
if factor.name=="\\partial":
for index in factor.indices():
k = Ex("k"+str(num)+"_{"+index.name+"}")
factor.insert(k)
for arg in factor.args():
arg.append_child(Ex("k"+str(num)))
factor.insert(arg)
factor.erase()
else:
factor.append_child(Ex("k"+str(num)))
num+=1
</code></pre>
<p>It could use some additional functionality to work on single-factor terms and perhaps some other edge cases, but it's a start (and I'm out of time to work on this until 2.2.0 is out).</p>
<p>I hope this is sufficiently self-explanatory, but feel free to ask if it is not clear. Please keep in mind that this is subject to change slightly in future updates; I haven't yet settled on the interface completely yet. Feedback more than welcome!</p>
General questionshttp://cadabra.science/qa/605/fourier-transform?show=617#a617Tue, 27 Mar 2018 17:01:18 +0000Answered: Conditional Substitute command
http://cadabra.science/qa/597/conditional-substitute-command?show=602#a602
<p>I knew this was coming, now you are approaching the problem from the other end of the scale ;-) You can do some basic conditionals in <code>substitute</code>, but not what you want. I think this is something for a feature list post 2.2.0 release which we are preparing right now.</p>
General questionshttp://cadabra.science/qa/597/conditional-substitute-command?show=602#a602Thu, 15 Mar 2018 19:20:29 +0000Answered: Error reading imported dict of Cadabra2 objects
http://cadabra.science/qa/593/error-reading-imported-dict-of-cadabra2-objects?show=594#a594
<p>You analysed that correctly: the reason why there is a problem when <code>get_item</code> is in a separate module is that that module does not see your</p>
<pre><code>\partial{#}::PartialDerivative.
</code></pre>
<p>declaration. A quick hack around that is to stick such a declaration line in the module as well, but that does not scale very well because you may be overlooking other properties. Instead, to make the module have access to the property information of the global scope, put a line</p>
<pre><code>__cdbkernel__ = cadabra2.__cdbkernel__
</code></pre>
<p>in the module (after <code>import cadabra2</code> obviously). </p>
<p>Support for modules could definitely be improved; one thing that you already ran into is that you need to write proper Python in a module; you cannot use Cadabra notation except in strings. To help us design this, please do share your experience with this.</p>
General questionshttp://cadabra.science/qa/593/error-reading-imported-dict-of-cadabra2-objects?show=594#a594Tue, 13 Mar 2018 14:42:21 +0000Answered: Saving expression results in file
http://cadabra.science/qa/589/saving-expression-results-in-file?show=590#a590
<p>The key is to use <code>Ex.input_form()</code>. Here's an example. Writing an expression <code>ex1</code> and <code>ex2</code> to a file <code>ex.txt</code> (in the directory from which you started Cadabra; use a full path if necessary) is done as in:</p>
<pre><code>ex1:=A_{m n} \sin{x};
ex2:= B_{m n};
with open("ex.txt", "w") as file:
file.write( ex1.input_form()+"\n" )
file.write( ex2.input_form()+"\n" )
</code></pre>
<p>You can then read them back in later using</p>
<pre><code>with open("ex.txt", "r") as file:
ex1=Ex( file.readline() )
ex2=Ex( file.readline() )
</code></pre>
General questionshttp://cadabra.science/qa/589/saving-expression-results-in-file?show=590#a590Wed, 07 Mar 2018 09:50:15 +0000Answered: Factor out vector which appears with different indices
http://cadabra.science/qa/581/factor-out-vector-which-appears-with-different-indices?show=586#a586
<p>The key is to use <code>SortOrder</code>; try</p>
<pre><code>{v{#}, g{#}, \partial{#}}::SortOrder;
</code></pre>
<p>which should move the $v$ factors to the front of the terms. Then </p>
<pre><code>rename_dummies(_);
</code></pre>
<p>to give the $v$ vector the same index name in every term. Finally <code>factor_out</code> giving the exact index name on $v$.</p>
<p>I agree this is sub-optimal; the general problem is hard (for reasons which are not clear from simple expressions like the one above) and we have been thinking about the best solution for some time. </p>
General questionshttp://cadabra.science/qa/581/factor-out-vector-which-appears-with-different-indices?show=586#a586Fri, 23 Feb 2018 20:31:16 +0000Answered: Integrate by parts only certain terms
http://cadabra.science/qa/579/integrate-by-parts-only-certain-terms?show=583#a583
<p>I have pushed some changes to github to fix <a rel="nofollow" href="https://github.com/kpeeters/cadabra2/issues/71">https://github.com/kpeeters/cadabra2/issues/71</a> and also make <code>integrate_by_parts</code> more consistent. That is, it will now always integrate by parts only once (even if there is a double derivative), and you have to give it the full argument of the derivative which you want to move away. Example:</p>
<pre><code>\partial{#}::PartialDerivative;
ex:= \int{ a \partial_{0}{\partial_{0}{a} } }{x};
</code></pre>
<p>which represents</p>
<p>$$\int a \partial_{0 0}{a} {\rm d}x$$</p>
<p>The following</p>
<pre><code>integrate_by_parts(_, $a$);
</code></pre>
<p>will not do anything (because <code>\partial_{0}</code> acts on <code>\partial_{0}{a}</code>, not on <code>a</code>). But</p>
<pre><code>integrate_by_parts(_, $\partial_{0}{a}$);
</code></pre>
<p>will produce</p>
<p>$$-\int \partial_{0}{a} \partial_{0}{a} {\rm d}x$$</p>
<p>A few more examples of this are in <a rel="nofollow" href="https://github.com/kpeeters/cadabra2/tree/master/tests/integrals.cdb">tests/integrals.cdb</a></p>
<p>For your concrete case, a simple</p>
<pre><code>integrate_by_parts(_, $a$);
</code></pre>
<p>will now produce zero because it will only act on the first term.</p>
General questionshttp://cadabra.science/qa/579/integrate-by-parts-only-certain-terms?show=583#a583Fri, 23 Feb 2018 19:10:10 +0000Kronecker 0 0 form
http://cadabra.science/qa/571/kronecker-0-0-form
<p>Thanks, your explanation makes sense.</p>
<p>Now with all the substitution rules I have ended up with a term like</p>
<p>\begin{equation}{}Ricci = -6\partial_{0}{}^{0}{a} {a}^{-3}-16\partial_{0}{a} \partial^{0}{a} {a}^{-4}+16\delta_{0}{}^{0} \partial_{0}{a} \partial^{0}{a} {a}^{-4}\end{equation}</p>
<p>Notice the $\delta_{0}{}^{0}$.</p>
<p>I can't figure out how to substitute this away, it gets turned into one right away in the substitution rule and therefore the pattern can't match.</p>
General questionshttp://cadabra.science/qa/571/kronecker-0-0-formMon, 19 Feb 2018 18:15:11 +0000Only one component of derivative non zero
http://cadabra.science/qa/569/only-one-component-of-derivative-non-zero
<p>I have a variable "a" which only is a function of time, ie all space derivatives are zero.</p>
<p>I have been trying various ways of dealing with expressions like<br>
\begin{equation}{}\partial^{r}{{a}^{-2}} h_{r}\end{equation}</p>
<p>I have used the substitute command to convert it to:<br>
\begin{equation}{}\eta^{r}{}_{0} \partial^{0}{{a}^{-2}} h_{r}\end{equation}</p>
<p>Problem is that eliminate metric doesn't seem to push through to give an h_0 term</p>
<p>I am using fixed indices.</p>
<p>Even worse when instead of $h_r$, the second term is a partial derivative like <br>
\begin{equation}{}\partial^{r}{{a}^{-2}} \partial_{r}{h}\end{equation}</p>
<p>Then I get <br>
\begin{equation}{}\eta^{r}{}_{0} \partial^{0}{{a}^{-2}} \partial_{r}{h}\end{equation}</p>
<p>And I cannot get the r index pushed through via eliminate metric.</p>
<p>I don't want to use kronecker deltas because I will loose the sign structure of metric.</p>
<p>Also I have many versions of the above expression, so I have been creating long substitution rules (seems like I need all the combinations of fixed indices, plus different tensor and derivative forms).</p>
<p>I don't really want to use the split index function to separate out space and time indices at this point in my calculation since the number of terms will explode, and I already have very many. It is a perturbation problem to third order.</p>
<p>Also many times canonicalize seems to crash the program with all of these metric terms.</p>
<p>Final question -- since fixed indices seems to make it tough to simply things, when is it safe to switch back to free indices. My raising and lowering metric is Minkowski.</p>
<p>Can I do part of a calculation with fixed indices then switch to free later on with another Indices declaration. Same with post_process -- can I start with one (without canonicalize) and then use another later.</p>
<p>Thanks for your thoughts.</p>
<p>Any ideas?</p>
<p>So, is there a better way to do this</p>
General questionshttp://cadabra.science/qa/569/only-one-component-of-derivative-non-zeroMon, 19 Feb 2018 03:13:19 +0000