Tensor (outer) product of vectors

For developers writing C++, Fortran, Java, code who have questions or comments to make.

Moderators: silvia, selimgunay, Moderators

Post Reply
maksins
Posts: 32
Joined: Sun Jun 19, 2011 10:28 am
Location: University at Buffalo

Tensor (outer) product of vectors

Post by maksins »

I have to calculate tensor product of two vectors a and b as c=a*b'
I checked in Matrix and Vector class but could not find any function to obtain transpose of a vector, or to calculate outer/tensor product of a vector. Is there any function that can perform this task?
I also found a class Tensor which further includes BJtensor.h. However, when I try to include Tensor or BJtensor to myelement.h file using #include <Tensor.h>, it gives error because core folder does not have Tensor class files. Should I copy paste Tensor and BJtensor class from OpenSees svn? Where would I get header and .cpp files of this class?
Manish Kumar
Department of Civil, Structural and Environmental Engineering
University at Buffalo, The State University of New York

http://www.manishkumar.org
fmk
Site Admin
Posts: 5884
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: Tensor (outer) product of vectors

Post by fmk »

there is no existing method in the Vector class, one could simply be added.

as for the Tensor classes. They have been removed as no classes currently in the framework were using them.
maksins
Posts: 32
Joined: Sun Jun 19, 2011 10:28 am
Location: University at Buffalo

Re: Tensor (outer) product of vectors

Post by maksins »

Thanks for the reply Frank.
I have added a function for a=b*c' for two-dimensional vectors in Vector class and working with that right now. In future would it be possible to add that function in Vector and Matrix classes? In fact a tensor operator(%) overloading to be added in Matrix and Vector classes to return a Matrix would be really helpful.
Manish Kumar
Department of Civil, Structural and Environmental Engineering
University at Buffalo, The State University of New York

http://www.manishkumar.org
fmk
Site Admin
Posts: 5884
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: Tensor (outer) product of vectors

Post by fmk »

sure .. why don't you send me what you have written and i will just add that.
maksins
Posts: 32
Joined: Sun Jun 19, 2011 10:28 am
Location: University at Buffalo

Re: Tensor (outer) product of vectors

Post by maksins »

Here it is, Frank. It's a very primitive function for two dimensional vector and matrices because that's what I need right now. May be in the future if I need it, I will write for n dimensional vectors and matrices.

Declaration to be added in Vector.h:

//methods added by Manish @ UB
Matrix operator%(const Vector &V) const;

Definition to be added in Vector.cpp:

Matrix Vector::operator%(const Vector &V) const
{
//returns M=A*B'
Matrix M(2,2);
M.Zero();
M(0,0)=theData[0]*V(0);
M(0,1)=theData[0]*V(1);
M(1,0)=theData[1]*V(0);
M(1,1)=theData[1]*V(1);

return M;

}
Manish Kumar
Department of Civil, Structural and Environmental Engineering
University at Buffalo, The State University of New York

http://www.manishkumar.org
fmk
Site Admin
Posts: 5884
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: Tensor (outer) product of vectors

Post by fmk »

i would need a more general routine if i was to include it, this is too specific and limited.
maksins
Posts: 32
Joined: Sun Jun 19, 2011 10:28 am
Location: University at Buffalo

Re: Tensor (outer) product of vectors

Post by maksins »

Hi Frank, here's a more generalized version:

Declaration to be added in Vector.h:

//methods added by Manish @ UB
Matrix operator%(const Vector &V) const;

Definition to be added in Vector.cpp:

Matrix Vector::operator%(const Vector &V) const
{
// if sizes are compatable add
#ifdef _G3DEBUG
if (sz != V.sz) {
// else sizes are incompatable, do nothing but warning
opserr << "WARNING Vector::tensor multiplication operator % - incompatable Vector sizes\n";
return -1;
}
#endif

//we want result=a*b'

Matrix result(sz,sz);
for (int i=0; i<sz; i++)
for (int j=0; j<sz; j++)
result(i,j)=theData[i]*V.theData[j];

return result;

}
Manish Kumar
Department of Civil, Structural and Environmental Engineering
University at Buffalo, The State University of New York

http://www.manishkumar.org
Post Reply