toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Toon-members] std::vector overload to use with TooN ( Dynamic Matri


From: Gerhard Reitmayr
Subject: Re: [Toon-members] std::vector overload to use with TooN ( Dynamic Matrix assembling)
Date: Sat, 22 May 2010 19:03:35 +0200

On 22 May 2010, at 15:22, Gabriel Nützi wrote:

> Thx for the fast answer was very helpful!!
> 
> I think WrapMatrix is the best for my issue :-)
> 
> I assemble a container  (rows....)
> vector<Vector<7> >  A
> and then I reinterprete it with:
> Matrix<> B = wrapMatrix(&A[0][0], ?? , 7);
> 
> That works fine... is this correct and save? I mean it works, and because 
> vector stores in one data block it should be save.... whats your 
> recommendation?
> 

yes, that looks ok. its as save as it can be :) the matrix returned by 
wrapMatrix only stores a pointer to the data, so as long as you are not 
modifying the vector (add an element, swap it etc.) it should be fine. 

Also don't assign it to a Matrix<> . This will create a copy and store it in B 
(because Matrix<> takes care of its own data). either use it just in an 
expression

Vector<> x = wrapMatrix(...) * v;
or in the variable of the type returned by wrapMatrix:

Matrix<Dynamic, Dynamic, double, Reference::RowMajor> B = wrapMatrix;

This type of matrix will only store the pointer to the data. The last argument 
in the template is the key.

> How can I cope with std::deque< Vector<7> >  A    or something like that 
> which is not in one data block?
> 

There isn't anything. TooN will only supports layouts where rows (or columns 
for column major) are consecutive _and_ the offset from row to row is the same 
between all rows. for a vector that is fine with offset = 0. if every row is at 
a different location, its not possible.

for a dequeue, you can choose the underlying container (I believe). so you 
could use a vector and then reference the data from std::deque<>::begin() or 
so. 

> It would be very cool (because TooN almost supports everything)  to have this 
> as a special feature, that we can wrap matrices to other containers... :-)
> That would make the thing very handy....
> 

well, for the reasons outlined above, it would require some major development. 
Its probably feasible with the current framework of Layouts, but you would have 
to specify the address of every row. that seems to be almost as much work as 
just copying the data in to a Matrix object. 

In general, why not just reserve a large dynamic matrix instead of the 
vector<Vector<> >. I sort of assume you have some information about the size of 
your data ? Even if its not used, its just memory sitting around and can be 
freed afterwards.

cheers,
  Gerhard

> 
> Regards Gabriel!!
> 
> 
> 
> Am 22.05.2010 um 12:53 schrieb Gerhard Reitmayr:
> 
>> how about a Resizable matrix and a slice after it has been assembled ? you 
>> would have to keep it around between iterations to avoid memory 
>> reallocation. but it would be as efficient as a std::vector which also will 
>> reallocate and copy the contents, if new elements are added and it exceeds 
>> the reserved size.
>> 
>> another way would be to take the std::vector contents and wrap with a matrix 
>> using wrapMatrix (see internal/reference.hh). std::vector stores data in one 
>> memory block so that should work fine.
>> 
>> cheers,
>> Gerhard
>> 
>> 
>> On 22 May 2010, at 12:24, Gabriel Nützi wrote:
>> 
>>> Hello all
>>> 
>>> 
>>> I have a question , and I don't know how to solve this as good as possible:
>>> 
>>> I have an iteration in which I always assemble new Matrices (m x 7)  
>>> dynamically, so I do not know how big they become in each iteration, the 
>>> size m varies, 
>>> 
>>> Is there a way of realizing this either with TooN or with overloads in 
>>> std::vector< Vector<7> >   efficiently
>>> The only way I can push rows in my Matrix dynamically is with std::vector 
>>> ....
>>> 
>>> I was searching for a way to make this compatible with TooN and efficient 
>>> (I dont want to copy heaps of data around) ? But I ve come up so far only 
>>> with a overload in std::vectors
>>> 
>>> wrote the  fallowing overload:
>>> 
>>> 
>>> //Specialized Template vor Vector<>
>>> template <int N1, int N2, typename P, typename B>
>>> Vector<Dynamic,P,B> operator*(const std::vector<Vector<N1,P,B> >& x, 
>>> Vector<N2,P,B> &y)
>>> {
>>>   Vector<Dynamic,P,B> v(x.size());
>>>     v= Zeros;
>>>     for (int m=0; m< x.size();m++) {
>>>             for (int n=0; n<y.size(); n++) {
>>>                     v[m] += x[m][n] * y[n];
>>>             }
>>>     }
>>>     return v;
>>> }
>>> 
>>> 
>>> 
>>> Does anyone know why the error happens here: 
>>> 
>>>     vector<Vector<3> > A;
>>>     A.assign(3,makeVector(1,2,3));
>>>     Vector<> a = makeVector(2,2,2);
>>>     
>>>     Vector<> c = A * a ;
>>>     Vector<> b = A * ( A * a );   // ERRRRROOOOOORRRRRR  (but why))
>>> 
>>> 
>>> Would be very helpful if anyone could tell me how I should do this to make 
>>> it efficient , and why this ERROR happens?
>>> 
>>> 
>>> Thanks alot for your support!
>>> 
>>> Gabriel

--
Gerhard Reitmayr
Institute for Computer Graphics and Vision
http://www.icg.tugraz.at/Members/gerhard
tel: ++43 316 873 5082






reply via email to

[Prev in Thread] Current Thread [Next in Thread]