Tensors and vectors
using MuesliMaterialsMUESLI carries its own small fixed-size types rather than using a general array library. This package wraps four of them, all in three dimensions:
| Type | Shape | Notes |
|---|---|---|
Ivector | 3 | a vector |
Itensor | 3 × 3 | a general second-order tensor |
Istensor | 3 × 3 | symmetric; stores only six components |
Itensor4 | 3 × 3 × 3 × 3 | a fourth-order tensor |
Construction
Each type builds from the corresponding Julia array:
v = Ivector([1.0, 2.0, 3.0])MuesliMaterials.IvectorAllocated with 3 entries
1.0
2.0
3.0m = [1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0]t = Itensor(m)MuesliMaterials.ItensorAllocated with 3x3 entries
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
Julia stores matrices column-major and MUESLI's constructors are row-major, so the conversion reorders the components. The round trip is faithful — in particular nothing is transposed:
t[1, 2], t[2, 1](2.0, 4.0)Istensor is symmetric and keeps only six components, so it reads the symmetric part of what you give it:
s = Istensor([1.0 0.2 0.3 0.2 2.0 0.4 0.3 0.4 3.0])MuesliMaterials.IstensorAllocated with 3x3 entries
1.0 0.2 0.3
0.2 2.0 0.4
0.3 0.4 3.0
A fourth-order tensor takes a 3×3×3×3 array:
c = Itensor4(reshape(collect(1.0:81.0), 3, 3, 3, 3))c[1, 2, 3, 1]22.0Sizes are checked, so a wrong shape is an error rather than a silent misread:
try Itensor(zeros(2, 2))catch err errendErrorException("Input has to be an array of size 3x3, got 4 element(s).")They behave like Julia arrays
Indexing is 1-based, and slicing, axes, length and iteration all work:
t[1, :], t[:, 2]([1.0, 2.0, 3.0], [2.0, 5.0, 8.0])axes(t), length(t), ndims(t)((Base.OneTo(3), Base.OneTo(3)), 9, 2)sum(t) # iteration45.0Assignment works too, including into slices:
t[1, 1] = 100.0t[2, 1:3] = [7.0, 8.0, 9.0]tMuesliMaterials.ItensorAllocated with 3x3 entries
100.0 2.0 3.0
7.0 8.0 9.0
7.0 8.0 9.0
Converting back
convert produces an ordinary Julia array:
convert(Matrix{Float64}, s)3×3 Matrix{Float64}:
1.0 0.2 0.3
0.2 2.0 0.4
0.3 0.4 3.0convert(Array{Float64}, c)[1, 2, 3, 1]22.0Interop with Tensors.jl
Loading Tensors.jl activates an extension that converts to its static tensor types, which is what you want inside an element routine:
using Tensorsconvert(Tensor{2, 3}, Itensor([1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 9.0]))3×3 Tensors.Tensor{2, 3, Float64, 9}:
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0convert(SymmetricTensor{2, 3}, s)3×3 Tensors.SymmetricTensor{2, 3, Float64, 6}:
1.0 0.2 0.3
0.2 2.0 0.4
0.3 0.4 3.0