Hot answers tagged

22

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


22

The Pitsianis-Van Loan algorithm turns out to be surprisingly easy to implement in Mathematica: nearestKroneckerProductSum[mat_?MatrixQ, dim1_?VectorQ, dim2_?VectorQ, k_Integer?Positive, opts___] /; TrueQ[Dimensions[mat] == dim1 dim2] := Module[{tmp}, Check[tmp = SingularValueDecomposition[Flatten[...


20

The definition used (motivated by exterior calculus) is as follows: Given a rectangular array $a$ of depth $n$, with dimensions $\{d, ..., d\}$ (so there are $n$ $d$'s) and a list $x = \{x_1, ..., x_d\}$ of variables, then Curl[a, x] == (-1)^n (n+1) HodgeDual[Grad[a, x], d] If $a$ has depth $n$, then Grad[a, x] has depth $n+1$, and therefore HodgeDual[...


19

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


17

You can implement most of your einsum functionality using TensorContract/TensorTranspose. Here is an implementation, but note that it will not work with indices that are repeated but not contracted, and index specifications that don't match the corresponding array's depth: einsum[in_List->out_, arrays__] := Module[{res = isum[in->out, {arrays}]}, ...


16

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


14

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


13

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


12

I recommend exploiting new and powerful capabilities of Mathematica 9. When we deal with symbolic tensors sometimes we would like to assume special properties of tensors like their specific symmetries, dimensions etc. because most of tensor equations of mathematical physics (Maxwell, Yang-Mills, Einstein etc.) involve special symmetries of underlying ...


11

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


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

You could use my TensorSimplify package help with this. Install the paclet with: PacletInstall[ "TensorSimplify", "Site" -> "http://raw.githubusercontent.com/carlwoll/TensorSimplify/master" ] Once installed, you can load the package with: <<TensorSimplify` This package defines the helper functions ToTensor and FromTensor. ToTensor ...


11

You seem to be coming from Matlab as you try to transpose a vector, a concept that is not that useful in Mathematica. We will see in a second why that is. Dense tensor example n = 40; m = 200; T = RandomReal[{-1, 1}, {n, n, n, m}]; x = RandomReal[{-1, 1}, {n, 1}]; y = RandomReal[{-1, 1}, {n, 1}]; I defined x and y as $n\times 1$-matrices because that is how ...


11

You can use TensorContract instead of Sum: r = Activate @ TensorContract[ Inactive[TensorProduct][VL, VL, VL, VL, θ4], {{2, 9}, {4, 10}, {6, 11}, {8, 12}} ]; //AbsoluteTiming {0.419766, Null} Using Activate with Inactive is a tip from @jose.


11

The variable i is a dummy one. The evaluated expression: Sum[f[i], {i, 1, 10}] f[1] + f[2] + f[3] + f[4] + f[5] + f[6] + f[7] + f[8] + f[9] + f[10] contains no explicit variable f[i], hence, the result is 0. Try to first Inactivate the sum, and only then to calculate the derivative: expr1 = D[Inactivate[Sum[f[i], {i, 1, 100}], Sum], f[i]] The result is ...


10

Since Version 9, functions to do this have been built into Mathematica but not documented. They live in the SymbolicTensors package which underlies CoordinateChartData, CoordinateTransform, and related functions. You could add the context to $ContextPath to save needing to type it everywhere, though I won't do that in the example below. They easiest way to ...


10

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


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


10

Ok, you've got yourself a bit confused here, but that's okay. Going through the three different errors: For a simple net like the one you gave, which has one input and one output, what NetTrain needs is a list of input/output pairs. You provided a list of lists (i.e. a table), where each row contained mixed together the components of the input (the first 3 ...


10

Perhaps something like the following will suffice? R /: D[R, R[α_], NonConstants->{R}] := R[α]/R R /: D[R[α_], R[β_], NonConstants->{R}] := KroneckerDelta[α, β] R /: MakeBoxes[R[α_], fmt_] := MakeBoxes[Subscript[R,α], fmt] Your examples: D[1/R, R[α], NonConstants->{R}] //TeXForm $-\frac{R_{\alpha }}{R^3}$ D[1/R, R[α], R[β], NonConstants->{...


10

Perhaps what you want is symbolic tensors: http://reference.wolfram.com/language/tutorial/SymbolicTensors.html $Assumptions = M ∈ Arrays[{3, 3}] && a ∈ Complexes; TensorContract[a*M, {{1, 2}}] (* a TensorContract[M, {{1, 2}}] *)


10

Aha, simpler than I thought. Assuming all I guessed in the comments is correct: BeginPackage["einstein`"] allowtensor; $tensordimension = 3; Begin["`private`"] expand[func_, {}] := # & expand[func_, var_] := Function[s, func[s, ##] &[Sequence @@ ({#, $tensordimension} & /@ var)], HoldAll] tensor[index_List] := Function[...


10

mat = {TensorProduct[{1, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}]/Sqrt[2]}; FixedPoint[ArrayFlatten, mat] // MatrixForm $\left( \begin{array}{cccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & \...


9

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


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

One way is to turn them into ordinary matrices, take the dot product, and then ArrayReshape them into the form you want. Af = ArrayFlatten[A]; Bf = Flatten[B]; ArrayReshape[Af.Bf, {3, 1, 2}] // MatrixForm Just to check: ArrayReshape[Af.Bf, {3, 1, 2}] == dotdot[A, B] (* True *)


9

You could just do: Sum[KroneckerDelta[μ, ν] KroneckerDelta[μ, ν], {ν, d}, {μ, d}, Assumptions->d>1] d Although it might make sense to use symbolic tensors instead, e.g., something like: Tr[IdentityMatrix[d] . IdentityMatrix[d]] although in this case you would need some extra code for simplification (as you can find in my TensorSimplify package). ...


9

Let me try to partially answer. Partially for the following reason: I know how to implement index vector and tensor notations and how to work with them. I also wanted to implement the Einstein convention and failed. However, even without it one can successfully use the index notations. Let us first introduce the Kronecker, \[Delta] and Levi-Civita, ee ...


9

ArrayReshape doesn't let you do this, but ReshapeLayer does: ReshapeLayer[{2, Automatic}] @ Range[6] ReshapeLayer[{Automatic, 2}] @ Range[6] {{1., 2., 3.}, {4., 5., 6.}} {{1., 2.}, {3., 4.}, {5., 6.}} Unfortunately, ReshapeLayer is a neural network function that only works on machine numbers.


8

I agree that it's a bit odd that Mathematica doesn't simplify these expressions with its built-in functions, especially in the symbolic tensor language (i.e. using TensorContract and TensorReduce). Nonetheless, we can teach it how to simplify the identity matrix ourselves. I've chosen to implement this for Dot, since that is what you initially asked. I do ...


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