-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tensor Class and tensorProduct between vectors #194
Comments
I believe this is already implemented as Vector::outerProduct() and it returns a Matrix. |
As @Beakerboy says, try out Vector outerProduct(). Also of interest may be Matrix kroneckerProduct(). Let us know if this works for you. |
the tensorProduct of two vectors is a vector, if the vectors are defined in R^2, the tensorProduct is defined in R^(2*2) (1,0) tensorProduct (1,0) = (1,0,0,0) I'll try with kroneckerProduct (with 1-col matrix as vectors) and let you know. :) |
ok kroneckerProduct works, but it returns a matrix. Now I need a vector. I tried the asVectors(), but if I try to print such value I have an error.
if I try an echo of such value, I have
I tried also to create a new Vector from that array with : |
Maybe outerProduct is not what you need. Is there Mathmatica or Wikipedia article that describes the operation? Wikipedia calls the outer product the tensor product of two vectors. In your code snippet above, $j is never incremented, so the inner for loop will never halt. Also, the use of '-1' in the array_fill function appears off. Using this with two vectors, each of length 2, $ra is initialized with only 3 elements. it looks like your saying: The double square brackets tell you it is an array of arrays. In this case the outer array contains only one array: |
I just looked at the kronecker product wikipedia article, and for two vectors, it looks like it should return what you want. The asVectors method returns an array of rows. If you need a Vector, you might want:
We should probably make the asVectors method allow for an array of columns as well as an array of rows. |
@Beakerboy I tried the function and it works. The array_fill creates the array starting from 0 to $lv1 * $lv2 - 1 ... so, for K0 $lv1 and $lv2 are the same and equals 2, so it's 2*2-1 = 3 |
@Beakerboy you can check this page, in the example section: |
array_fill creates $lv1 * $lv2 - 1 INSTANCES of the value provided. It's array_fill(from, num, value) not array_fill(from, to, value). Stupid me, yes, $j is incremented (been doing too much VBA lately where every for loop needs a "Next i" at the end). The array_fill is unnecessary though. If you added a print_r you would see that you initialized it to 3 elements, but the for loop adds a fourth one to it. Your link confuses me towards the bottom, (ϕ1,ϕ2)⊗(ψ1,ψ2) equals two different things. The kronecker product above, and outer product below. I guess it's a case of the ⊗ operator having many meanings. @markrogoyski it looks like Vector::directProduct and Vector::outerProduct are the same thing.... |
ok... because there's an equivalence in some cases :) sorry for this. http://hitoshi.berkeley.edu/221a/tensorproduct.pdf chapter 3 (page 6) defines the tensorproduct for two general vectors. |
@TheFausap As you use the library, if you have any suggestions on extending it to be more useful for Tensors, let me know. I began a class a while back, but I'm not very familiar on the subject. https://github.com/Beakerboy/math-php/blob/Tensor/src/LinearAlgebra/Tensor.php |
@Beakerboy Interesting... just an idea: you defined mixed tensor T^h_k (I'm sorry but I have no idea how to do indices with this markup language). Now with it, you could generalize everything, because:
About the tensor (2,0) changing it (metric tensor) you can change the scalar product (for example, could define easily a scalar product with a spherical metric). If there's a way to render some math inside the comment it will be easier. :) |
kroneckerProduct returns a matrix, which you can print as is: $matrix = new Matrix([
[1,2],
[3,4],
]);
$kroneckerProduct = $matrix->kroneckerProduct($matrix);
echo $kroneckerProduct;
// [1, 2, 2, 4]
// [3, 4, 6, 8]
// [3, 6, 4, 8]
// [9, 12, 12, 16] Matrix asVectors() returns an array of matrix columns each as vectors. They are not rows. Each vector can be printed individually. $matrix = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]);
echo $matrix;
// [1, 2, 3]
// [4, 5, 6]
// [7, 8, 9]
$vectors = $matrix->asVectors();
foreach ($vectors as $vector) {
echo $vector . \PHP_EOL;
}
// [1, 4, 7]
// [2, 5, 8]
// [3, 6, 9] However, the return value $vectors is itself an array, which you will need to either iterate, or use something like print_r or var_dump or var_export. print_r($vectors);
/*
Array
(
[0] => MathPHP\LinearAlgebra\Vector Object
(
[n:MathPHP\LinearAlgebra\Vector:private] => 3
[A:MathPHP\LinearAlgebra\Vector:private] => Array
(
[0] => 1
[1] => 4
[2] => 7
)
)
[1] => MathPHP\LinearAlgebra\Vector Object
(
[n:MathPHP\LinearAlgebra\Vector:private] => 3
[A:MathPHP\LinearAlgebra\Vector:private] => Array
(
[0] => 2
[1] => 5
[2] => 8
)
)
[2] => MathPHP\LinearAlgebra\Vector Object
(
[n:MathPHP\LinearAlgebra\Vector:private] => 3
[A:MathPHP\LinearAlgebra\Vector:private] => Array
(
[0] => 3
[1] => 6
[2] => 9
)
)
)
*/ I can see how asVectors() can be confusing. So perhaps I'll add asColumnVectors and asRowVectors and remove asVectors(). Also, as @Beakerboy said, if you have any feedback about improvements or feature requests, please don't hesitate to let us know. Thanks for your interest in MathPHP and the great discussion about functionality. |
@TheFausap Right now I don't think my Tensor class knows if a dimension is covariant or contravariant, so it needs to be improved, ie. tᵝᵞ vs tᵝᵧ vs tᵦᵧ. I need to read up more on the subject to figure out how to design the class to represent the object the best, and be able to accurately perform the operations. Any examples you could provide would be great. |
@markrogoyski thanks a lot! :) your package is full of very useful functions, but if I found some nice addition, I'll let you know. |
@Beakerboy Basically, each linear map is described by a matrix (you can think a vector like a 1 col matrix, if you want extend the idea). So from a programming point of view a tensor, IMHO, cannot be a simple matrix, but a dictionary (for example): Cνμ the tensor. The indices needs to be incremented separately, so fixing the A index to 1, multiply for all the elements of B, and we have the tensor elements C1,1, C1,2, C1,3 e C1,4.
the contraction is the opposite of multiplication, reduces the index number, a (2,0) tensor can be contracted to (1,0) tensor (i.e. a vector). Now if you tensorMultiply Cνμ with At you have Dνμt (3,0) or PS: the tensorProduct in my first request, is a different one: is a tensorProduct between vector spaces, not tensors, so it behaves differently, it transfor vectors in vectors (with bigger dimensions) |
I read some Tensor articles last night, and I think you verified what I came out thinking. The characteristics which make a specific tensor unique are; number of contravariant indicies (or dimensions), number of covariant indicies, the order of the indices (although conventions seems to idicate that contravariant indices should all come first), the "length" of each dimension, and the specific value at each position. I've updated my class to include a few options:
Now I need to find a general tensor multiplication that will determine how mixtures of covariant and contravariant indices combine into the new tensor. I have code to create an "empty" new Tensor given the two tensors which will be combined. The thing I need is a general process of, given Aᵝᵩ ⊗ Bᵪᵞ = Tᵝᵩᵪᵞ, what combination of elements from A and B are used to calculate, say T⁴₂₃⁶? You said above, that if we have two contravariant indicies, Aᵝ ⊗ Bᵞ = Tᵝᵞ, T⁴⁶ = A⁴ x B⁶. What about Aᵝ ⊗ Bᵪ, or other mixes? Can contraction only occur when the two specified indicies are dissimilar variance-types? Also, does the input Tensor need to be "square" along the contracted dimensions? For example, can you contract:
|
@Beakerboy Generally the product of two tensors is based on the KroneckerProduct. Rargb = (GagsGsbr + etc) as you can see the number of indices on the left side is the indices on the right side (removing only the index "s", because in the multiplication is contracted, and yes the contraction can occur only between covariant and controvariant indices). the size of the matrix (rows and cols) representing the tensor depends where the tensor is defined, for example the metric tensor (2,0) [rank 2 tensor] in general relativity (3+1 dimensions), is a 4x4 2d-matrix. to visualize something, I found a nice pdf. at http://www.ita.uni-heidelberg.de/~dullemond/lectures/tensor/tensor.pdf |
@Beakerboy if you have a rank 2 tensor (1,1): Ckl, if you want to contract this, you have a (1-1,1-1) tensor or a scalar: Ckk. |
@TheFausap so you are saying, yes, you CAN contract a rectangular Tensor that is 4 elements by 3, for example? I'll update my Tensor class to accommodate this. I think the class has a working constructor, and the contraction method is pretty close to correct. |
@Beakerboy when you setup the space dimension, the indices runs all the same range. so if we are handling tensors in three dimensional space, all the indices runs from 1 to 3. So the matrices are always square. for (1,1) tensor number of elements are 3^(1+1) = 9 -> 3x3 matrix |
From stack exchange:
Is this worth considering in a general Tensor class? Should there be a parent, and a SquareTensor? |
@Beakerboy I'm not sure what you mean by the what is missing part. I went by this definition: |
@markrogoyski what I meant was, we have two copies of the outer product, while the vector version of the Kronecker product is missing. |
Can you provide a link to something that shows the definition of Kronecker product for vectors? How would it be different from the outer product? The Kronecker product is generalizing the outer product (of Vectors) and applying it to matrices. |
@markrogoyski The outer product is the kronecker product of a column matrix and a row matrix. The kronecker product of two column vectors will also be a column vector. If you feel that the kronecker product is only truly defined for matrix math, then I guess that's fine, and we can keep it out of the Vector class. |
OK. I think understand what you mean.
|
@Beakerboy The difference between covariant and controvariant indices, is useful for the contraction, but from the component point of view (and matrix or whatever else associated in the tensor description) is not useful. |
Your example, in the code, is useful: so the tensor Tᵝᵩᵪᵞ is 4-rank tensor, (2,2) type. Contracting, it becomes a (1,1) tensor, 2-rank tensor (or a matrix), with components: |
@TheFausap I believe the code does exactly what you explain. |
@Beakerboy the contraction could also happens between two objects: Tij Aj = Ki, following the same rule. |
@TheFausap is this the same as performing a tensor product first, then contracting, or is there a clever way to discard the 'j' indicies first, and make it a first order tensor Ti times a scaler A? |
@Beakerboy In my opinion, is better to apply the definition : Ti1A1 + Ti2A2 + Ti3A3 = Ki. |
@TheFausap I just submitted code for the Kronecker Sum of two square matrices. Wikipedia says it "appears naturally in physics when considering ensembles of non-interacting systems." |
@TheFausap The document you linked to states:
When I asked if one can multiply first, then contract, I'm now thinking the answer is no, because multiplication necessitates contraction. If this is the case, a tensor product function should something like this:
I can fill in the rest and test it out. Could you give me a list of tensors and the expected result of their tensor product so I can test the function? |
@Beakerboy this could be an interesting approach, also to implement other operations between vectors and tensors, scalar and tensors, tensors and tensors. https://people.rit.edu/pnveme/personal/EMEM851n/constitutive/tensors_rect.html as test, you can use
this also can easily extend the cross product in higher dimensions, if the indices runs from 1 to 4, for example. |
@TheFausap Can the Kronecker delta be defined as a higher order tensor, or just second order? If so, is it 1 iff all indices match, or just two? Also, can it be defines with any assortment of covariant and contravariant indices, or is it always (0,2)? |
@Beakerboy kronecker tensor is always rank 2 tensor i.e. identity matrix, and it can be defined in higher dimensional spaces, and it has a property it's same even if you change coordinate system (cartesian to spherical). I'm not aware about a (0,3) tensor, for example, defined in the same way. |
Hello,
could be possible to implement a tensorProduct operation between two Vectors ?
I defined such function for my purpose, but i don't know how to add to math-php, and if for you is ok.
regards,
Fausto
The text was updated successfully, but these errors were encountered: