Referrence: Jim Blinn, "Optimizing C Vector Expression", in IEEE DatabasekenskenNeth I. Joy, "The Vector Data Type", http://graphics.cs.ucdavis.edu/cppnotes/homepage.html
PART I: on Constructor: "All classes should have a copy constructor and an assignment operator." That means, we should add two functions: "Vector (const Vector &); and Vecotr & operator = (const Vector &);" together with "Vector (const Double, const double = 0.0) "
The reason is safe and convenient (refer to Effective C Chapter 3, Rule 11) and consider That Vecotr & Operator = The Input and Output of Operator is The Same.
Part II: on output and serialize No matter what he
Part III Operator Calling Try strictly abide by "Regulations for Duplicate Codes"
Vector & Operator = (const vector & v2) {_x = v2._x; _y = v2._y; return * this;} int operator == (Const Vector & V1, Const Vector & V2) {IF (((v1) ._x == v2._x) && (v1._y == v2._y) && (v1._z == v2._z)) Return True; Else Return False;} Define VECTOR & OPERATOR (Const Vector & " V1, Const Vector & V2) {RETURN VECTOR (A) = B;} int operator! = (Const Vector & V1, Const Vector & V2) {Return (! (v1 == v2));} It looks good, but actually Operator is not good. Because this adds a VECTOR (a) to create the overhead of the object. I dare to affirm that matlab will not do this. I hope to study the early code of MATLAB next time.
PART IV calculation as you want
Part of the Vast Popular Of C Is ITS Class Structure and Operator Overloading Features That Allow The Definition of Mathematical Operations for MANOW We can Make The C Act As Matlab!
Then discuss Operator . If we don't get so feel whistle, we can define the return value of Operator is vector instead of Vector & Note Opertator and Operator = situation is complete! Vector Operator (Const Vector & V1, Const Vector & V2) {Vector VV; vv._x = v1._x v2._x; vv._y = v1._y v2._y; vv._z = v1._z v2._z Return (VV);} This can be defined in *, /, -, . *, MATLAB is there. *,. /, .- ,. . You can try additional, define the Vector Cross (Const Vector & V1, Const) Vector & V2); Double Dot (Const Vector & V1, Const Vector & V2); Void Vector :: Normalize (); Double Vector :: Length (); Final, consider Operator overload, Vector Operator (Float F1), etc.. Of course, we must imagine MATLAB as you want, there are three things 1. Try the template. Matlab can eat regardless of int Double. 2. Calculation of the matrix, interface 3.matlab can D = [A, B, C]. C seems to only D = VECTOR (A, B, C). Use these three, let's talk later.
Part V compile error
This is extremely annoying. # Is the source of evil. STD pushed this evil to the extreme. Use const.
1. WE SHOULD KNOW THE DIFCERENCE BETWEEN Const Char * P (* p = 'c' is not allowed) And char * const p (p is not allowed); 2. We Should Know The Reason Why Vector Dot (Const Vector &, Const) Vector &) Instead of Vector Dot (Vector &, Vector &) Yes, One Reason Is Is More Safe. But Another Reason, IF WE Define Vector Dot (Vector &, Vector &) WE WILL GETERRORS WHEN USING: A = DOT ( V1 V2, V3). The Compiler Will Not Allow You To change (v1 v2)! 3. Do Not Forget to Declare a const () Const Function: Double length () const; will not change the data members of the class.
Part VI Efficiency C is slow to create an overhead of the object. A strange phenomenon is that the C large-scale matrix calculation does not have to catch Matlab. As for other software, at least I feel that Photoshop is much more than the general image handler. Optimization is still very important. The above VECTOR implements a more time-consuming place is that every Operator is created, however, a tricky practice is to use "container" instead of data objects
Class sum {const vector & l; const vector & r; public: sum (const vector & linit, const vector): l (tinit), R (Rinit) {;} float operator [] const {return L [i) Const {Return L [i ] R [i];}} void vector :: evata (const sum & e) {v [0] = E [0]; v [1] = E [1]; v [2] = E [2]; } Sum Operator (Const Vector & A, Const Vector & B) {Return Sum (A, B);
Well, this thing is really difficult to maintain. :-P, use Visitor mode, define vid Vector :: EVALUATE (Const Function & E), then deliver various classes, sum, product ...., all classes implement Operator [] with Function. For example, Sum :: Operator [] is {Return L [i] R [i]} and product :: Operator [] (INT i) is {Return S * v [I]}. Pretty ---- Yes, But .... do you know The cost of virtual function? According to the results of Berlin, the efficiency of Visitor mode is not as good as the best. :-(
In Effective C , Terms 41 tell us, "If the type T does not affect the class's behavior T, you can use the template. If t affects behavior, you need a virtual function to use inheritance." Can you use Trick Expressed with Template? The efficiency of the template seems yet, think about STL knows OK, Let's Go
// Sum
// Product
// Class vectorclass vector {... template
Template
Template
It seems that it has reached the limit. But this is just for 3 * vector or Vector Vector. Think about the A 3 * (B C), actually SUM (Vector, Product (SUM (Vector, Vector)). Can we further simplify the intermediate steps? Yes. But you need to use Template. Look again later. You can see the blinn's article. Finally, copy BLINN
Every programming language has a gimmick. The gimmick of C is to put as much intelligence in the class library as possible, to make things as easy as possible for the user of the those class. In spite of the fact that there is a lot of complexity in the implementation of vector arithmetic, any user of the Vector class does not see that complexity They can create Vector class and performarithmetic on them with ease and with confidence that is the classical trade-off in C :.. the need of the many Out weigh the needs of the.
The last sentence is very exciting. But maybe I will allow me to have something different. Class is not only easy to use, but also for maintenance. I think I am willing to sacrifice speed unless I really have a speed. However, the readability of CPP is not necessarily multiple inheritance, interface, and template may be more important. I want to consider complex data structures. For example, a neighboring matrix from point is found, what data structure is better, what Class is clear?
Then go back to the vector discussion, if I want to easily maintain, at least two angles can be thought of first, if you want to extend the vector to 4D, that is, Homogeneous Coordinates, what should I do if the vector class is designed? Second, consider the interface between Vector and Matrix, what should I design?
The previous one can go to VXL, then one can go to the MATLAB design. But I have to think about myself. Next