How to transpose Tensor in Eigen - eigen

I'm trying to get matrix product of two tensors, where one of the tensor should be transposed before it multiplied (At*B).
So far what I've found in eigen documentation is matrix product without any transposed and with both matrix transposed.
I'm looking for a way to either directly contracting two tensor with one of the tensor is transposed, or either by transposing one tensor before contracting it.

I figured it out, transpose effect can be done using shuffle method.
Eigen::Tensor<int, 2> m(3, 5);
m.setValues(
{
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
});
Eigen::array<int, 2> shuffling({1, 0});
Eigen::Tensor<int, 2> transposed = m.shuffle(shuffling);
Eigen::Tensor<int, 2> original = transposed.shuffle(shuffling);

You can also use the contraction directly:
Eigen::Tensor<int, 2> A(3, 5);
Eigen::Tensor<int, 2> B(3, 5);
Eigen::array<int, 1> contraction_indices;
// This will contract the first dimension of A with the first dim of B,
// effectively computing At*B
contraction_indices[0] = {0, 0};
Eigen::Tensor<int, 2> Result = A.contract(B, contraction_indices);

Related

Largest rectangular sub matrix with the number difference k

Input: A 2-dimensional array NxN - Matrix - with numbers from 0 to 9.
Output: Largest rectangular area where absolute value of number's difference in area is k.
Possible input:
int k=3;
const int N=5;
int matrix[N][N] = {{9, 3, 8, 2, 0},
{2, 7, 6, 8, 5},
{8, 5, 7, 7, 6},
{3, 0, 4, 0, 9},
{7, 2, 0, 4, 0}};
Is it related to find largest area in histogram problem? If it does how I can transform this matrix two binary matrix? And how to approach this kind of problems?
Answer: largest area is 8 by following sub matrix {{7, 6, 8, 6}, {5, 7, 7, 6}}
What I think should be done:
Transform matrix to binary matrix
Create histogram from binary matrix
Calculate largest area using largest area in histogram.
What is unclear is how to transform input matrix to binary matrix.

How to find the location of the highest element in a tensor in mathematica

I have a very large array of numbers in the form of a third order tensor.I want to find the highest of all the values in that tensor. How can I do it in mathematica? The context is that a reaction is carried out by varying temperature pressure and vessel volume. I want to find the optimum combination of the three to maximize the product. Each element of the tensor represents a value of the product produced corresponding to a specific combination of temperature pressure and volume.
Given some matrix, tensor, or basically any list-of-lists of real numbers, you can simply use the Max function to determine the maximum value and then Position to say where it is. Assuming your data isn't enormous (requiring some conservative/careful approach to save time/memory), this should be fine.
For example, here is a random list of lists of of lists of reals:
data = Table[RandomReal[],
{i, 1, RandomInteger[{4, 8}]},
{j, 1, RandomInteger[{4, 8}]},
{k, 1, RandomInteger[{4, 8}]}
];
You can just do:
m = Max[data]
Position[data, m]
This will tell you the position of the maximum value. If you did random integers instead, you could have ties, in which case you might have repeats:
data = Table[RandomInteger[{1, 10}],
{i, 1, RandomInteger[{4, 8}]},
{j, 1, RandomInteger[{4, 8}]},
{k, 1, RandomInteger[{4, 8}]}
];
m = Max[data]
Position[data, m]
Table[RandomInteger[100, 3], 3]
Prepend[Ordering[%[[First[Ordering[Reverse#*Sort /# %, -1]]]], -1],
First[Ordering[Reverse#*Sort /# %, -1]]]
% stands for the tensor to sort, in this case it's a random tensor generated from Table[RandomInteger[100, 3], 3]
This gives the position and value in one shot.
(m = RandomReal[{-1, 1}, {4, 3, 2}]) // MatrixForm
First#MaximalBy[
Flatten[MapIndexed[ {##} &, #, {-1}], ArrayDepth[#] - 1],
First] &#m
{0.903213, {3, 2, 2}}
Here is an alternate that will work with ragged lists:
Module[{h},
First#MaximalBy[List ### Flatten[MapIndexed[h### &, #, {-1}]],
First]] &#{{1, 2, 3}, {4, 5, {2, 3}}}
{5, {2, 2}}

How to calculate a matrix formed by vector in Mathematica

I need to obtain a matrix vvT formed by a column vector v. i.e. the column vector v matrix times its transpose.
I found Mathematica doesn't support column vector. Please help.
Does this do what you want?
v = List /# Range#5;
vT = Transpose[v];
vvT = v.vT;
v // MatrixForm
vT // MatrixForm
vvT // MatrixForm
To get {1, 2, 3, 4, 5} into {{1}, {2}, {3}, {4}, {5}} you can use any of:
List /# {1, 2, 3, 4, 5}
{ {1, 2, 3, 4, 5} }\[Transpose]
Partition[{1, 2, 3, 4, 5}, 1]
You may find one of these more convenient than the others. Usually on long lists you will find Partition to be the fastest.
Also, your specific operation can be done in different ways:
x = {1, 2, 3, 4, 5};
Outer[Times, x, x]
Syntactically shortest:

using nested arrays and lists in mathematica

Absolute beginner question here.
I have two lists in mathematica.
The first one was generated by the Table command:
Table[QP[[i]], {i, 10}]
which generates the list:
{52.5, 45., 37.5, 30., 22.5, 15., 7.5, 0., -7.5, -15.}
the second is a Range
Range[0, 9, 1]
which generates {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
I need to get these into a list of lists. i.e. {{0,52.5},{1,45} ... } etc. But I can't seem to get it. Do you need to use loops? Because I think that what I want can be generated with the Table and Array commands.
Thanks
Transpose may be what you want:
list1 = {52.5, 45., 37.5, 30., 22.5, 15., 7.5, 0., -7.5, -15.}
list2 = Range[0, 9, 1]
Transpose[{list2, list1}]
gives
{{0, 52.5}, {1, 45.}, {2, 37.5}, {3, 30.}, {4, 22.5}, {5, 15.}, {6,
7.5}, {7, 0.}, {8, -7.5}, {9, -15.}}
The first parameter of Table can be any expression. You can have it output a list of lists, by specifying a list as the first parameter:
Table[{i-1, QP[[i]]}, {i, 10}]
(* {{0, QP[[1]]}, {1, QP[[2]]}, ... {8, QP[[9]]}, {9, QP[[10]]}} *)
Thread[List[Range[0, 9], QP[[;; 10]]]]
To complete the exposition of methods, you could use MapIndexed
MapIndexed[{First[#2] - 1,#1}&, data]
where
data = {52.5, 45., 37.5, 30., 22.5, 15., 7.5, 0., -7.5, -15.}
Or, you could use MapThread
MapThread[List, {Range[0,9], data}]
Although, MapIndexed is more appropriate since it does not require you to generate an extra list.
A last point I want to make is that your code Table[QP[[i]], {i, 10}] implies that QP itself is a list. (The double brackets, [[ ]], gave it away.) If that is correct, than Table isn't the best way to generate a subset, you can use Part ([[ ]]) along with Span directly
QP[[ 1 ;; 10 ]]
or
QP[[ ;; 10 ]]
Then, you can replace data in the first bits of code with either of those forms.

How to reshape matrices in Mathematica

When manipulating matrices it is often convenient to change their shape. For instance, to turn an N x M sized matrix into a vector of length N X M. In MATLAB a reshape function exists:
RESHAPE(X,M,N) returns the M-by-N matrix whose elements are taken columnwise from X. An error results if X does not have M*N elements.
In the case of converting between a matrix and vector I can use the Mathematica function Flatten which takes advantage of Mathematica's nested list representation for matrices. As a quick example, suppose I have a matrix X:
With Flatten[X] I can get the vector {1,2,3,...,16}. But what would be far more useful is something akin to applying Matlab's reshape(X,2,8) which would result in the following Matrix:
This would allow creation of arbitrary matrices as long as the dimensions equal N*M. As far as I can tell, there isn't anything built in which makes me wonder if someone hasn't coded up a Reshape function of their own.
Reshape[mtx_, _, n_] := Partition[Flatten[mtx], n]
ArrayReshape does exactly that.
Reshape[list_, dimensions_] :=
First[Fold[Partition[#1, #2] &, Flatten[list], Reverse[dimensions]]]
Example Usage:
In: Reshape[{1,2,3,4,5,6},{2,3}]
Out: {{1,2,3},{4,5,6}}
This works with arrays of arbitrary depth.
I know this is an old thread but for the sake of the archives and google searches I've got a more general way that allows a length m*n*... list to be turned into an m*n*... array:
Reshape[list_, shape__] := Module[{i = 1},
NestWhile[Partition[#, shape[[i]]] &, list, ++i <= Length[shape] &]
]
Eg:
In:= Reshape[Range[8], {2, 2, 2}]
Out:= {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}
There is now also a new function ArrayReshape[].
Example:
{{1, 2, 3}, {4, 5, 6}} // MatrixForm
ArrayReshape[{{1, 2, 3}, {4, 5, 6}}, {3, 2}] // MatrixForm

Resources