Hi Neuling,
To create a symmetric tensor of orders 2, 3, and higher, adjustments are needed in your code to correctly handle the symmetry across all dimensions and orders. The primary challenges include:
- Generalizing Symmetry for Higher Orders: The operation posMatrix = posMatrix+posMatrix'-diag(diag(posMatrix)); is specific to matrices (2D arrays) and does not generalize to tensors of higher dimensions.
- Correct Cumulative Sum for Positioning: The line posMatrix = cumsum(A0(:)); computes the cumulative sum linearly, which doesn't account for the multidimensional structure properly when reshaped back for higher orders.
To address these issues, let's revise your approach. We need a method that:
- Constructs the lower triangular part correctly for any order of tensor.
- Mirrors this lower triangular part across all dimensions to achieve symmetry.
Here's an approach to achieve this:
function posTensor = createSymmetricTensor(n, Order)
posTensor = zeros(repelem(n, Order));
tempTensor(row(i), col(i)) = 1;
tempTensor = repmat(tempTensor, repelem(1, Order-2));
permsRow = perms([row(i)*ones(1,Order-2), row(i), col(i)]);
uniquePermsRow = unique(permsRow, 'rows');
for j = 1:size(uniquePermsRow, 1)
indices = num2cell(uniquePermsRow(j, :));
posTensor(indices{:}) = posCounter;
posCounter = posCounter + 1;
This function attempts to create a symmetric tensor by explicitly handling the symmetry for tensors of any order. However, note that the approach to generating all permutations and then finding unique rows can be computationally intensive for large tensors or higher orders. The approach needs to be optimized for specific needs or constraints.
To test this function for orders 2 and 3:
posTensor = createSymmetricTensor(n, Order);
Please note: The provided solution is a conceptual approach and might require adjustments based on the exact requirements for symmetry in higher dimensions. The complexity of generating symmetric tensors increases significantly with the order, and special attention is needed to ensure all symmetric positions are correctly identified and assigned.