473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Vector classes

During my coding Ive found two vector classes on the internet. Ive
modified them both a little but the do both have advantages and
disadvantages.

vector1: http://rafb.net/p/4FVdh699.html
vector2: http://rafb.net/p/0KShGu30.html

With 1, I can typ vec.x and vec.y, very nice. With 2, I need to do
vec.vals[0] and vec.vals[1], which makes my eyes bleed.

But with 2, I can create a vector by supplying a list of numbers as
arguments. And I can also do maths with numbers (ints/floats) and not
just vectors (something 1 forces me to do).

Is there any way to combine the two? The ultimate would be if I
somehow could take vector2 and hook the member self.vals[0] to self.x
or something.

Apr 22 '07 #1
4 1664
On Apr 22, 8:33 am, Mizipzor <mizip...@gmail.comwrote:
With 1, I can typ vec.x and vec.y, very nice. With 2, I need to do
vec.vals[0] and vec.vals[1], which makes my eyes bleed.
If you add a __getitem__ method to the second vector class:

def __getitem__(self, i):
return self.vals[i]

then you can use vecs[0] instead of vec.vals[0]. But this vector
class has some other problems that you probably want to fix. For
example, the __add__ method modifies the second argument:
>>a = Vector([1, 2, 3])
[1, 2, 3]
>>b = Vector([4, 5, 6])
[4, 5, 6]
>>c = a + b
print b.vals
[5, 7, 9]

Something like

def __add__(self, other):
return Vector(x + y for x, y in zip(self.vals,
other.vals))

might work better. Similarly for the __sub__ method.

Have you considered using numpy?

Mark

Apr 22 '07 #2
Mizipzor wrote:
During my coding Ive found two vector classes on the internet. Ive
modified them both a little but the do both have advantages and
disadvantages.
I'm working on a vector class at the moment, in my 'gameobjects'
library. It's not really ready for public consumption, but feel free to
take a look...

http://www.willmcgugan.com/game-objects/

--
blog: http://www.willmcgugan.com
Apr 22 '07 #3
Mizipzor wrote:
During my coding Ive found two vector classes on the internet. Ive
modified them both a little but the do both have advantages and
disadvantages.

vector1: http://rafb.net/p/4FVdh699.html
vector2: http://rafb.net/p/0KShGu30.html

With 1, I can typ vec.x and vec.y, very nice. With 2, I need to do
vec.vals[0] and vec.vals[1], which makes my eyes bleed.

But with 2, I can create a vector by supplying a list of numbers as
arguments. And I can also do maths with numbers (ints/floats) and not
just vectors (something 1 forces me to do).

Is there any way to combine the two? The ultimate would be if I
somehow could take vector2 and hook the member self.vals[0] to self.x
or something.
There are also full-featured Vector and Matrix classes in the la.py
module of ZOE:

http://www.alcyone.com/software/zoe/

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Stretch a bow to the very full, / And you will wish you had stopped
in time. -- Laotse, ca. 6th C. BC
Apr 22 '07 #4
En Sun, 22 Apr 2007 09:33:53 -0300, Mizipzor <mi******@gmail.comescribió:
During my coding Ive found two vector classes on the internet. Ive
modified them both a little but the do both have advantages and
disadvantages.

vector1: http://rafb.net/p/4FVdh699.html
vector2: http://rafb.net/p/0KShGu30.html

With 1, I can typ vec.x and vec.y, very nice. With 2, I need to do
vec.vals[0] and vec.vals[1], which makes my eyes bleed.
[2] modifies the 2nd argument to __add__ and __sub__, not a good thing...
The only advantage I can see over [1], is that it handles dimensions
higher than 2.
But with 2, I can create a vector by supplying a list of numbers as
arguments.
Same with the first one:

coords = (1.2, 3.4)
v1 = vector(*coords)

There is even the "list" static method (not a good name!):

coords = (1.2, 3.4)
v1 = vector.list(coords)

Anyway I'd remove the __ne__ and __lt__ methods (if you don't require it)
and replace __hash__ with hash((self.x,self.y))
Is there any way to combine the two? The ultimate would be if I
somehow could take vector2 and hook the member self.vals[0] to self.x
or something.
You should try the other suggested classes, but if I had to choose among
these two, I'd use the first.

--
Gabriel Genellina
Apr 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Venn Syii | last post by:
I've searched all the forums but cannot find an answer to this question. I do the following: vector<MyClass*> myClassList; Later in the program I try to add to myClassList with a...
3
by: Chad E. Dollins | last post by:
Hello someone said that I should check this forum out for help to my c++ problems. I will admit at this point I may have really ran myself into the ground with this code that I am writing, but...
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
14
by: markww | last post by:
Hi, I want to use the vector container class to store pixel data. Currently I have some memory allocated using c++'s new operator. I allocate the memory differently based on if the pixel type is...
28
by: VK | last post by:
A while ago I wrote a "Vector data type" script using DOM interface to select.options. That was a (beautiful) mind game :-) rather than a practical thing to use. Here is another attempt open...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
7
by: Thomas | last post by:
I am compiling with g++ the fol. class: template<typename E> class C_vector_ : public std::vector<E> { private:
4
by: helge | last post by:
What is the best way to implement a vector in space R3, i.e a vector holding three floats, supporting arithmetic operations, dot and cross product etc in c++? is there a standard library class for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.