Tag Info

Hot answers tagged

26

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, ...


24

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 ...


13

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


12

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 ...


12

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 ...


12

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]:= ...


10

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}] (* ==> ...


10

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 ...


10

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 ...


9

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 ...


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

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 ...


8

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_] := ...


8

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


7

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 ...


7

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 ...


7

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, #}] ...


7

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)$ ...


6

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]}}} *)


6

Since the sum goes over the last index of $e$ and the first index of $A$, it is directly done by using Dot: dim = 5; e = Array[\[ScriptE], Table[dim, {dim}]]; a = Array[\[ScriptA], Table[dim, {2}]]; c = e.a; Here I defined the arrays with the appropriate dimensions but suppressed the output because it's too long for five dimensions. Another ...


6

Infix is only an output form. You most probably want the expression which you'd get when typing v1 ⊗ v2 into Mathematica, which is entered with either \[CircleTimes] or Escc*Esc. As you can see by typing v1 ⊗ v2 //FullForm, this is CircleTimes[v1, v2]. The same is true for longer chains of ⊗, i.e. v1 ⊗ v2 ⊗ … ⊗ vn has the internal form CircleTimes[v1, v2, ...


6

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 ...


6

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) $


6

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 ...


6

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 /: ...


6

Assuming $\mu$ and $\nu$ also run from 1 to 4 (which they have to, otherwise your expression doesn't make sense), you can simply take a cue from this Q&A and write TensorContract[ TensorProduct[LeviCivitaTensor[4], a, b, c], {{2, 5}, {3, 6}, {4, 7}} ] // Normal { -a4 b3 c2 + a3 b4 c2 + a4 b2 c3 - a2 b4 c3 - a3 b2 c4 + a2 b3 c4, a4 b3 c1 - a3 ...


5

I may be misunderstanding what you need, however consider this. If you have an structure of nested lists such as your data, then summing across the deepest list is easily accomplished using Map[Total,data,{-2}]. So, as long as you want to remove dimensions from "the back" you are good to go. And if we need to remove for example the second dimension, then you ...


5

First, get set t2 by removing those tensors that are permutations. Second, get set t3 by removing the tensors that are equivalent when the 0-1 substitutions are considered. t = Tuples[{1, 0}, {3, 3}]; t2 = DeleteDuplicates[t, Sort@#1 == Sort@#2 &]; t3 = DeleteDuplicates[t2, Sort@(#1 /. {0 -> 1, 1 -> 0}) == Sort@#2 &]; Length@t3 This gives a ...


5

Here's a rule that I think captures the spirit of what you're trying to do. (EDIT: Needed to shuffle some of the indices around to get the identity matrix into the right spot. SECOND EDIT: Treat the case of both left and right contraction.) $Assumptions = A ∈ Matrices[{n, n}] && Inverse[A] ∈ Matrices[{n, n}] && Det[A] != 0 && ...


5

This gives what you want! Adding the axiom of matrix power $A^0=I$ to Simplify gives us Simplify[ Assuming[Element[A, Matrices[{n, n}]] && Det[A] != 0,TensorExpand[Inverse[A].A]], ForAll[{A}, MatrixPower[A, 0] == IdentityMatrix[n]]] IdentityMatrix[n] Use n=3 to validate your case.



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