Hot answers tagged

34

General remarks In General Relativity we work in a 4-dimentional Lorentzian manifold i.e. there is a metric tensor $g$ of signature $(+,-,-,-)$ or $(-,+,+,+)$. Theses signatures are mathematically equivalent and we choose the latter because of certain quite formal aspects even though there are some physically relevant reasons for choosing the former one. In ...


31

I am somewhat reluctant to offer this as an answer since it is inherently difficult to comprehensively address questions on undocumented functionality. Nonetheless, the following observations do constitute partial answers to points raised in the question and are likely to be of value to anyone trying to write practical compiled code using Bags. However, ...


14

Mathematica 9 contains some functionality for working with symbolic tensors. Here's a list of packages in no particular order, that may have some functionality for working with symbolic tensors. TensoriaCalc - intended for basic calculations in general relativity, but not finished (calculates only Christoffel symbols, Riemann and Ricci tensor). Parallel ...


14

Since you're working with vectors, just let Mathematica know that these are vectors. Some other systems (MATLAB and its relatives in particular) have the limitation that they can only work with matrices, forcing you to distinguish between row vector and column vectors and keep transposing. This is not necessary nor convenient in Mathematica. In[1]:= $...


13

About your question regarding the definition of the type of local variables in Compile, Compile has an optional third argument that allows you do this in the same manner you specify arguments. It helps the compiler solve some type ambiguity issues sometimes as by default a local variable is considered a Real number. This can be the case if a local variable ...


13

You could do this: Outer[List, B, #] & /@ A


12

If we transpose the indices of $v$ and $w$ so that $v'_{abe} = v_{aeb}$ and $w'_{ecd} = w_{ced}$, then we can compute $u = v' \cdot w'$: u === Transpose[v, {1, 3, 2}] . Transpose[w, {2, 1, 3}] (* True *) We can use a similar trick to compute $q$ if we reorder $v_{dea} \to v''_{ade}$, except that this time the $d$ and $e$ indices in $v_{ade}''$ and $w_{deb}...


12

I stumbled upon this question via Google. Thanks for using my TensoriaCalc package! My response is probably too late, but I believe the problem you cited Symbol Tensor is Protected. Symbol TensorType is Protected. Symbol TensorName is Protected. is because you loaded TensoriaCalc more than once in the same kernel session. When writing the ...


11

On this wikipedia page you find a collection of Tensor software and Mathematica has the biggest section. The package Ricci, which username acl pointed out in his answer is there, and I personally have used xAct. It looks like this And yes, as you suggest in your question, for smaller computation in specific dimensions you can also work in components ...


11

Higher-order SVD (in sense of Tucker decomposition) of the matrix $M$ with dimensions $d_1\times d_2\times\cdots\times d_n$ is $$ M_{i_1,i_2,\dots,i_N} = \sum_{j_1} \sum_{j_2}\cdots \sum_{j_N} s_{j_1,j_2,\dots,j_N} u^{(1)}_{i_1,j_1} u^{(2)}_{i_2,j_2} \dots u^{(N)}_{i_N,j_N}, $$ where $s$ is the core tensor and $u^{(i)}$ is the orthogonal matrix. The matrix ...


11

Please compare Map[findpointsC[data, #] &, data]; // AbsoluteTiming (* {3.567939, Null} *) and findpointsC[data, data] ; // AbsoluteTiming (* {0.841954, Null} *) And of course is Map[findpointsC[data, #] &, data] == findpointsC[data, data] (* True *) The explanation is simple: You typed RuntimeAttributes->{Listable} and Parallelization ...


11

Did you know about the second argument of Total, which lets you sum up element at a certain level, which in practice means along a certain dimensions? For example, if you want to keep levels 1 and 2, and sum up along level 3, you can use Total[data, {3}] (* ==> {{10, 22, 34}, {50, 62, 74}} *) Or sum up along dimension 1: Total[data, {1}] (* ==> {...


11

Some errors corrected and a few tricks Using memoization, "Precomputing" the solutions by using Reduce once instead of NSolve each time, Rewriting the functions in a compact and more understandable way, Reducing the overall calcs needed, Using NonlinearModelFit with Method -> {"NMinimize", Method -> "NelderMead"} for efficiency Reduced the ...


11

The Pitsianis-Van Loan algorithm turns out to be surprisingly easy to implement in Mathematica: nearestKroneckerProduct[mat_?MatrixQ, dim1_?VectorQ, dim2_?VectorQ] /; TrueQ[Dimensions[mat] == dim1 dim2] := Module[{bv, cv, sig}, {bv, sig, cv} = SingularValueDecomposition[Flatten[ Map[Composition[Flatten, Transpose], ...


10

This one might be a starter to calculate the tensors. partialDer[T_, vars_] := D[T, #] & /@ vars // Simplify christoffelSymbols[metric_, coord_] := Module[{dg = partialDer[metric, coord], inverse = Simplify[Inverse[metric]]}, inverse.(Transpose[dg, {2, 1, 3}] + Transpose[dg, {3, 2, 1}] - dg)/ 2 // Simplify] curvTensor[christ_, var_] := ...


10

Thank you for your interest. I would strongly recommend against trying to modify SymbolicTensors`CoordinateChartDataDump`mappingInfo. It is a very low level function and any changes you make are unlikely to work. There are two sets of operations commonly needed with alternate coordinate systems. One is calculus in the coordinate system - Grad, Div, Curl ...


10

There is another option, using the relatively new tensor capabilities of Mathematica. This is pretty much copied from another answer by jose, but I don't need any assumptions here: TensorExpand[KroneckerProduct[X, X] + KroneckerProduct[-X, X]] (* ==> 0 *) TensorExpand[KroneckerProduct[2 X, 3 Y]] (* ==> 6 KroneckerProduct[X, Y] *) There is a ...


9

This limitation exists, and as far as I can see we have to live with it for now. It does indeed make it impractical to use Parallel operations inside functions---the functions won't work in parallel kernels. I recommend you parallelize at the top level unless you have a specific reason to do it at lower levels. The longer the a "unit calculation" that is ...


9

In Mathematica version 9, you can do these kinds of things much more naturally. Here are the two quantities that you wanted: u = TensorContract[TensorProduct[v, w], {2, 5}]; q = TensorContract[TensorProduct[v, w], {{1, 4}, {2, 5}}]; The contraction is performed on the tensor product in which the first three indices belong to the factor v and the last ...


9

Another possibility is Distribute soln = Distribute[{B, #}, List] & /@ A And soln == (Flatten[#, 1] & /@ (Outer[List, B, #] & /@ A)) == Flatten[Outer[List, B, A], {{2}, {1, 3}, {4}}] => True Edit Yet another possibility is Tuples (A little surprising to my mind that this one works) Tuples[{B, #}] & /@ A And (Tuples[{B, #}] &...


9

Depends on what dimension your final matrix is supposed to have. When I should make a guess, I would say you want this TensorProduct[IdentityMatrix[2], IdentityMatrix[2]] // ArrayFlatten


8

Here are a couple of possibilities. MapThread[Riffle[{#1}, {#2}] &, {aA, bB}, 2] (* Out[64]= {{{a[0, 0], b[0, 0]}, {a[1, 0], b[1, 0]}}, {{a[0, 1], b[0, 1]}, {a[1, 1], b[1, 1]}}} *) Transpose[ArrayFlatten[{aA, bB}], {3, 1, 2}] (* Out[72]= {{{a[0, 0], b[0, 0]}, {a[1, 0], b[1, 0]}}, {{a[0, 1], b[0, 1]}, {a[1, 1], b[1, 1]}}} *) Another that I ...


8

Is this faster? CoefficientArrays[identity, basis][[2]] // MatrixForm $\left( \begin{array}{cccccc} 1 & 0 & 1 & 0 & 0 & -1 \\ 0 & 1 & 1 & 0 & 0 & 0 \\ -1 & 0 & 0 & 1 & -1 & 0 \end{array} \right) $ Responding to Jens' elegant answer it should be noted that performance of CoefficientArrays is ...


8

The symbol $\epsilon$ is called the Levi-Civita tensor, it is usually written in terms of its components $\epsilon_{i j k}$. Having defined two vectors (i.e. first order contravariant tensors): a = { a1, a2, a3}; b = { b1, b2, b3}; we have to build up a tensor product of two vectors and Levi-Civita tensor, i.e. we get a new tensor of valence $(3,2)$ (i....


7

Mathematica does not yet support tensor calculus and notation natively, but there are numerous 3rd party packages which address this issue very well. This is a list of the packages I am more familiar with: xAct suite: free (GPL license), most powerful, created by relativists, actively supported. Disclosure: I contributed to the suite by creating the ...


7

Here's a fairly simple way of doing it (ignore the front end's syntax warning): Function[, {##}, Listable][A, B] (* {{{a[0, 0], b[0, 0]}, {a[1, 0], b[1, 0]}}, {{a[0, 1], b[0, 1]}, {a[1, 1], b[1, 1]}}} *)


7

Yes, you can simplify the trace this way. First, Tr[x.y.z] is invariant under cyclic permutations, so Tr[x.y.z] = Tr[y.z.x] = Tr[z.x.y], as described in Wikipedia's entry on trace of a product. For the case of interest, this means Tr[y.x.y] is equal to Tr[x.y.y]. Hence Tr[b x.y.y + b y.x.y] = Tr[b x.y.y] + Tr[b y.x.y] = b Tr[x.y.y] + ...


7

Well, you certainly can if you have version 9 and don't mind using the (quite verbose) tensor notation: Assuming[ {b ∈ Complexes, (x | y) ∈ Matrices[{n, n}]}, TensorReduce@ TensorContract[ b TensorContract[TensorProduct[x, y, y], {{2, 3}, {4, 5}}] + b TensorContract[TensorProduct[y, x, y], {{2, 3}, {4, 5}}], {{1, 2}} ] ] (* -> 2 b ...


7

Good question; the notion of a tensorial (covariant) derivative is something that is missing in Mathematica AFAIK. I can think of two ways to proceed: Option 1 One way is to overload the TensorRank, TensorDimensions, and TensorSymmetry functions for patterns that have head CD: CD /: TensorRank[CD[tensor_]] := TensorRank[tensor] + 1 CD /: TensorDimensions[...


7

You can combine the best of both worlds: symbolic tensors and vectors on one hand, and explicit vectors on the other. Explicit vectors are necessary in most vector algebra operations, unless you want to rely heavily on UpValues defined for all those operations and all the symbols you're using. It's cleaner to let Mathematica's matrix algebra take over ...



Only top voted, non community-wiki answers of a minimum length are eligible