In C# you only need the implementation file, "vec.cs". It could be something like this,
public class vec{ public double x,y,z; /* the three components of a vector */ public vec(){ x=y=z=0; } // default constructor public vec(double x,double y,double z){ // parametrized constructor this.x=x; this.y=y; this.z=z; } /* ... more stuff goes here ... */ }In C++ you need both the "vec.h" header file and "vec.cc" implementation file. The header file "vec.h" could be something like this,
The file "vec.cc" should implement the things that have been decleared in the "vec.h" file.
//C# operators: public static vec operator*(vec v, double c){return new vec(c*v.x,c*v.y,c*v.z);} public static vec operator*(double c, vec v){return v*c;} public static vec operator/(vec u, double c){/*...*/} public static vec operator+(vec u, vec v){/*...*/} public static vec operator-(vec u){return new vec(-u.x,-u.y,-u.z);} public static vec operator-(vec u, vec v){/*...*/} /* ... */
C++: the implementation file "vec.cc" should implement all the operators defined in the "vec.h" file above.
public void print(string s=""){Write(s);WriteLine($"{x} {y} {z}");}
void vec::print(std::string s) const { std::cout << s << x << " " << y << " " << z << std::endl; }
Implement methods for dot-product, vector-product, and norm. Something like
public double dot(vec other) /* to be called as u.dot(v) */ {return this.x*other.x+this.y*other.y+this.z*other.z;} public static double dot(vec v,vec w) /* to be called as vec.dot(u,v) */ {return v.x*w.x+v.y*w.y+v.z*w.z;}You can also try this C# syntactic sugar,
public double dot(vec other) => this.x*other.x+this.y*other.y+this.z*other.z; public static double dot(vec v,vec w) => v.x*w.x+v.y*w.y+v.z*w.z;
Make an "approx" method to compare two vec's with absolute precision "acc" and relative precision "eps". Something like
static bool approx(double a,double b,double acc=1e-9,double eps=1e-9){ if(Abs(a-b)<acc)return true; if(Abs(a-b)<(Abs(a)+Abs(b))*eps)return true; return false; } public bool approx(vec other){ if(!approx(this.x,other.x))return false; if(!approx(this.y,other.y))return false; if(!approx(this.z,other.z))return false; return true; } public static bool approx(vec u, vec v) => u.approx(v);
public override string ToString(){ return $"{x} {y} {z}"; }C++: overload "operator<<", something like
std::ostream& operator<<(std::ostream& os, const vec& v){ os << "{ " << v.x << ", " << v.y << ", " << v.z << " } "; return os; }
vec.dll: vec.cs mcs -target:library -out:vec.dll vec.csCreate a "main.cs" file with the "Main" function that illustrates your implementation and link it with "vec.dll" like this,
main.exe: main.cs vec.dll mcs -target:exe -out:main.exe -reference:vec.dll main.cs