Something like
public class vec{
public double x,y,z;
/* your methods go here */
}
//constructors:
public vec(){ x=y=z=0; }
public vec(double a,double b,double c){ x=a;y=b;z=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, vec v){/*...*/}
public static vec operator-(vec u, vec v){/*...*/}
public static vec operator-(vec u){/*...*/}
//methods:
public void print(string s){Write(s);WriteLine($"{x} {y} {z}");}
public void print(){this.print("");}
vec.dll: vec.cs mcs -target:library -out:vec.dll vec.cs
main.exe: main.cs vec.dll mcs -target:exe -out:main.exe -reference:vec.dll vec.cs
(3 points) Implement methods for dot-product, vector-product, and norm; override ToString method. Something like
public double dot(vec other){return this.x*other.x+this.y*other.y+this.z*other.z;}
/* and/or */
public static double dot(vec v,vec w){return v.x*w.x+v.y*w.y+v.z*w.z;}
(1 point) Make an "approx" method to compare two vec's with absolute precision τ and relative precision ε. Something like
static bool approx(double a,double b,double tau=1e-9,double eps=1e-9){
if(Abs(a-b)<tau)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){ return u.approx(v); }
and run extensive tests of your implementation in the Main function (see
[../matlib/complex/main.cs] for
inspiration).