Beschreibung:
Eine kleine Bibliothek zur Bruch- und Vektorrechnung und zum rechnen mit komplexen Zahlen.
Für Verbesserungsvorschläge bin ich immer offen
namespace csMathLib
{
/// <summary>
/// This class calculates with fractions
/// </summary>
public class Fraction
{
#region members
private int Numerator;
private uint Divisor;
#endregion
#region ctors
public Fraction()
{
this.Numerator = 1;
this.Divisor = 1;
}
public Fraction(double a)
{
int counter = 0;
do
{
counter++;
a *= 10;
} while (a % 1 > 0);
Fraction Temp = new Fraction((int)a, (uint)(Math.Pow(10, counter)));
Temp.Reduce();
this.Numerator = Temp.Numerator;
this.Divisor = Temp.Divisor;
}
public Fraction(int a)
{
this.Numerator = a;
this.Divisor = 1;
}
public Fraction(int a, uint b)
{
this.Numerator = a;
this.Divisor = b;
}
#endregion
#region operators
public static Fraction operator *(Fraction a, Fraction b)
{
Fraction RETURN = new Fraction(a.Numerator * b.Numerator, a.Divisor * b.Divisor);
RETURN.Reduce();
return RETURN;
}
public static Fraction operator +(Fraction a, Fraction b)
{
Fraction RETURN = new Fraction(a.Numerator * (int)b.Divisor + b.Numerator * (int)a.Divisor, a.Divisor * b.Divisor);
RETURN.Reduce();
return RETURN;
}
public static Fraction operator -(Fraction a, Fraction b)
{
Fraction RETURN = new Fraction(a.Numerator * (int)b.Divisor - b.Numerator * (int)a.Divisor, a.Divisor * b.Divisor);
RETURN.Reduce();
return RETURN;
}
public static Fraction operator /(Fraction a, Fraction b)
{
Fraction RETURN = new Fraction(a.Numerator * (int)b.Divisor, a.Divisor * (uint)b.Numerator);
RETURN.Reduce();
return RETURN;
}
public static bool operator >(Fraction a, Fraction b)
{
return a.ToDouble() > b.ToDouble();
}
public static bool operator >=(Fraction a, Fraction b)
{
return a.ToDouble() >= b.ToDouble();
}
public static bool operator ==(Fraction a, Fraction b)
{
return a.ToDouble() == b.ToDouble();
}
public static bool operator !=(Fraction a, Fraction b)
{
return a.ToDouble() != b.ToDouble();
}
public static bool operator <=(Fraction a, Fraction b)
{
return a.ToDouble() <= b.ToDouble();
}
public static bool operator <(Fraction a, Fraction b)
{
return a.ToDouble() < b.ToDouble();
}
#endregion
#region public functions
public double ToDouble()
{
return this.Numerator / this.Divisor;
}
public void Reduce()
{
int GGT = ggT(this.Numerator, (int)this.Divisor);
this.Numerator = this.Numerator / GGT;
this.Divisor = this.Divisor / (uint)GGT;
}
public string PrintFraction(char seperator)
{
return this.Numerator.ToString() + seperator.ToString() + this.Divisor.ToString();
}
public override bool Equals(object o)
{
return true;
}
public override int GetHashCode()
{
return 0;
}
#endregion
#region private functions
private int ggT(int a, int b)
{
if (b == 0)
{
return a;
}
else
{
return ggT(b, a % b);
}
}
#endregion
}
}
namespace csMathLib
{
/// <summary>
/// This class calculates with 3 dimensional vectors
/// </summary>
public class Vector3d
{
#region Members
public bool ShowAsFraction { get; set; }
private double d_x;
private double d_y;
private double d_z;
private Fraction f_x;
private Fraction f_y;
private Fraction f_z;
#endregion
#region ctors
public Vector3d()
{
d_x = 0;
d_y = 0;
d_z = 0;
f_x = new Fraction();
f_y = new Fraction();
f_z = new Fraction();
}
public Vector3d(double x, double y, double z, bool asFraction)
{
this.d_x = x;
this.d_y = y;
this.d_z = z;
this.f_x = new Fraction(x);
this.f_y = new Fraction(y);
this.f_z = new Fraction(z);
this.ShowAsFraction = asFraction;
}
public Vector3d(double x, double y, Fraction z, bool asFraction)
{
this.d_x = x;
this.d_y = y;
this.d_z = z.ToDouble();
this.f_x = new Fraction(x);
this.f_y = new Fraction(y);
this.f_z = z;
this.ShowAsFraction = asFraction;
}
public Vector3d(double x, Fraction y, double z, bool asFraction)
{
this.d_x = x;
this.d_y = y.ToDouble();
this.d_z = z;
this.f_x = new Fraction(x);
this.f_y = y;
this.f_z = new Fraction(z);
this.ShowAsFraction = asFraction;
}
public Vector3d(double x, Fraction y, Fraction z, bool asFraction)
{
this.d_x = x;
this.d_y = y.ToDouble();
this.d_z = z.ToDouble();
this.f_x = new Fraction(x);
this.f_y = y;
this.f_z = z;
this.ShowAsFraction = asFraction;
}
public Vector3d(Fraction x, double y, double z, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y;
this.d_z = z;
this.f_x = x;
this.f_y = new Fraction(y);
this.f_z = new Fraction(z);
this.ShowAsFraction = asFraction;
}
public Vector3d(Fraction x, double y, Fraction z, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y;
this.d_z = z.ToDouble();
this.f_x = x;
this.f_y = new Fraction(y);
this.f_z = z;
this.ShowAsFraction = asFraction;
}
public Vector3d(Fraction x, Fraction y, double z, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y.ToDouble();
this.d_z = z;
this.f_x = x;
this.f_y = y;
this.f_z = new Fraction(z);
this.ShowAsFraction = asFraction;
}
public Vector3d(Fraction x, Fraction y, Fraction z, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y.ToDouble();
this.d_z = z.ToDouble();
this.f_x = x;
this.f_y = y;
this.f_z = z;
this.ShowAsFraction = asFraction;
}
#endregion
#region setValues
public void setValues(double x, double y, double z, bool asFraction)
{
this.d_x = x;
this.d_y = y;
this.d_z = z;
this.f_x = new Fraction(x);
this.f_y = new Fraction(y);
this.f_z = new Fraction(z);
this.ShowAsFraction = asFraction;
}
public void setValues(double x, double y, Fraction z, bool asFraction)
{
this.d_x = x;
this.d_y = y;
this.d_z = z.ToDouble();
this.f_x = new Fraction(x);
this.f_y = new Fraction(y);
this.f_z = z;
this.ShowAsFraction = asFraction;
}
public void setValues(double x, Fraction y, double z, bool asFraction)
{
this.d_x = x;
this.d_y = y.ToDouble();
this.d_z = z;
this.f_x = new Fraction(x);
this.f_y = y;
this.f_z = new Fraction(z);
this.ShowAsFraction = asFraction;
}
public void setValues(Fraction x, double y, double z, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y;
this.d_z = z;
this.f_x = x;
this.f_y = new Fraction(y);
this.f_z = new Fraction(z);
this.ShowAsFraction = asFraction;
}
public void setValues(double x, Fraction y, Fraction z, bool asFraction)
{
this.d_x = x;
this.d_y = y.ToDouble();
this.d_z = z.ToDouble();
this.f_x = new Fraction(x);
this.f_y = y;
this.f_z = z;
this.ShowAsFraction = asFraction;
}
public void setValues(Fraction x, double y, Fraction z, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y;
this.d_z = z.ToDouble();
this.f_x = x;
this.f_y = new Fraction(y);
this.f_z = z;
this.ShowAsFraction = asFraction;
}
public void setValues(Fraction x, Fraction y, double z, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y.ToDouble();
this.d_z = z;
this.f_x = x;
this.f_y = y;
this.f_z = new Fraction(z);
this.ShowAsFraction = asFraction;
}
public void setValues(Fraction x, Fraction y, Fraction z, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y.ToDouble();
this.d_z = z.ToDouble();
this.f_x = x;
this.f_y = y;
this.f_z = z;
this.ShowAsFraction = asFraction;
}
#endregion
#region operators
public static Vector3d operator +(Vector3d a, Vector3d b)
{
Vector3d c = new Vector3d(a.f_x + b.f_x, a.f_y + b.f_y, a.f_z + b.f_z, a.ShowAsFraction);
return c;
}
public static Vector3d operator -(Vector3d a, Vector3d b)
{
Vector3d c = new Vector3d(a.f_x - b.f_x, a.f_y - b.f_y, a.f_z - b.f_z, a.ShowAsFraction);
return c;
}
public static Vector3d operator *(Vector3d a, double b)
{
Vector3d c = new Vector3d(a.d_x * b, a.d_y * b, a.d_z * b, a.ShowAsFraction);
return c;
}
public static Vector3d operator *(Vector3d a, Fraction b)
{
return new Vector3d(a.f_x * b, a.f_y * b, a.f_z * b, a.ShowAsFraction);
}
public static double operator *(Vector3d a, Vector3d b)
{
return a.d_x * b.d_x + a.d_y * b.d_y + a.d_z * b.d_z;
}
public static bool operator >(Vector3d a, Vector3d b)
{
return a.getLength() > b.getLength();
}
public static bool operator >=(Vector3d a, Vector3d b)
{
return a.getLength() >= b.getLength();
}
public static bool operator ==(Vector3d a, Vector3d b)
{
return a.getLength() == b.getLength();
}
public static bool operator !=(Vector3d a, Vector3d b)
{
return a.getLength() != b.getLength();
}
public static bool operator <=(Vector3d a, Vector3d b)
{
return a.getLength() <= b.getLength();
}
public static bool operator <(Vector3d a, Vector3d b)
{
return a.getLength() < b.getLength();
}
public Vector3d Cross(Vector3d b)
{
Vector3d c = new Vector3d(this.f_y * b.f_z - this.f_z * b.f_y, this.f_z * b.f_x - this.f_x * b.f_z, this.f_x * b.f_z - this.f_z * b.f_x, this.ShowAsFraction);
return c;
}
/// <summary>
/// this function is a combination of a dot and a cross multiplication of three vectors. only two other vectors need to be provided. the third one is the current vector
/// </summary>
/// <param name="b"></param>
/// <param name="c"></param>
/// <returns></returns>
public double Spade(Vector3d b, Vector3d c)
{
return this.Cross(b) * c;
}
#endregion
#region public functions
/// <summary>
/// This function returns the length of the current vector
/// </summary>
/// <returns></returns>
public double getLength()
{
return Math.Sqrt(Math.Pow(this.d_x, 2) + Math.Pow(this.d_y, 2) + Math.Pow(this.d_z, 2));
}
/// <summary>
/// This function returns the angle between the current vector and another vector
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public double Angle(Vector3d a)
{
return (180 / Math.PI) * Math.Acos(this.getLength() * a.getLength());
}
public override bool Equals(object o)
{
return true;
}
public override int GetHashCode()
{
return 0;
}
public string PrintVector3d(char seperator)
{
if (this.ShowAsFraction)
{
return "(" + this.f_x.PrintFraction(seperator) + ")" + seperator.ToString() + "(" + this.f_y.PrintFraction(seperator) + ")" + seperator.ToString() + "(" + this.f_z.PrintFraction(seperator) + ")";
}
else
{
return this.d_x.ToString() + seperator.ToString() + this.d_y.ToString() + seperator.ToString() + this.d_z.ToString();
}
}
#endregion
}
}
namespace csMathLib
{
/// <summary>
/// This class calculates with 2 dimensional vectors
/// </summary>
public class Vector2d
{
#region members
public bool ShowAsFraction { get; set; }
private double d_x;
private double d_y;
private Fraction f_x;
private Fraction f_y;
#endregion
#region ctors
public Vector2d()
{
this.d_x = 0;
this.d_y = 0;
this.f_x = new Fraction();
this.f_y = new Fraction();
}
public Vector2d(double x, double y, bool asFraction)
{
this.d_x = x;
this.d_y = y;
this.f_x = new Fraction(x);
this.f_y = new Fraction(y);
this.ShowAsFraction = asFraction;
}
public Vector2d(double x, Fraction y, bool asFraction)
{
this.d_x = x;
this.d_y = y.ToDouble();
this.f_x = new Fraction(x);
this.f_y = y;
this.ShowAsFraction = asFraction;
}
public Vector2d(Fraction x, double y, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y;
this.f_x = x;
this.f_y = new Fraction(y);
this.ShowAsFraction = asFraction;
}
public Vector2d(Fraction x, Fraction y, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y.ToDouble();
this.f_x = x;
this.f_y = y;
this.ShowAsFraction = asFraction;
}
#endregion
#region SetValues
public void setValues(double x, double y, bool asFraction)
{
this.d_x = x;
this.d_y = y;
this.f_x = new Fraction(x);
this.f_y = new Fraction(y);
this.ShowAsFraction = asFraction;
}
public void setValues(double x, Fraction y, bool asFraction)
{
this.d_x = x;
this.d_y = y.ToDouble();
this.f_x = new Fraction(x);
this.f_y = y;
this.ShowAsFraction = asFraction;
}
public void setValues(Fraction x, double y, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y;
this.f_x = x;
this.f_y = new Fraction(y);
this.ShowAsFraction = asFraction;
}
public void setValues(Fraction x, Fraction y, bool asFraction)
{
this.d_x = x.ToDouble();
this.d_y = y.ToDouble();
this.f_x = x;
this.f_y = y;
this.ShowAsFraction = asFraction;
}
#endregion
#region operators
public static Vector2d operator +(Vector2d a, Vector2d b)
{
return new Vector2d(a.f_x + b.f_x, a.f_y + b.f_y, a.ShowAsFraction);
}
public static Vector2d operator -(Vector2d a, Vector2d b)
{
return new Vector2d(a.f_x - b.f_x, a.f_y - b.f_y, a.ShowAsFraction);
}
public static Vector2d operator *(Vector2d a, double b)
{
Fraction f_b = new Fraction(b);
return new Vector2d(a.f_x * f_b, a.f_y * f_b, a.ShowAsFraction);
}
public static bool operator >(Vector2d a, Vector2d b)
{
return a.getLength() > b.getLength();
}
public static bool operator >=(Vector2d a, Vector2d b)
{
return a.getLength() >= b.getLength();
}
public static bool operator ==(Vector2d a, Vector2d b)
{
return a.getLength() == b.getLength();
}
public static bool operator !=(Vector2d a, Vector2d b)
{
return a.getLength() != b.getLength();
}
public static bool operator <=(Vector2d a, Vector2d b)
{
return a.getLength() <= b.getLength();
}
public static bool operator <(Vector2d a, Vector2d b)
{
return a.getLength() < b.getLength();
}
#endregion
#region public functions
public override bool Equals(object o)
{
return true;
}
public override int GetHashCode()
{
return 0;
}
/// <summary>
/// This function returns the length of the current vector
/// </summary>
/// <returns></returns>
double getLength()
{
return Math.Sqrt(Math.Pow(this.d_x, 2) + Math.Pow(this.d_y, 2));
}
/// <summary>
/// This function returns the angle between the current vector and another vector
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public double Angle(Vector2d a)
{
return (180 / Math.PI) * Math.Acos(this.getLength() * a.getLength());
}
#endregion
}
}
namespace csMathLib
{
/// <summary>
/// This class calculates with complex numbers
/// </summary>
public class Complex
{
#region Members
public bool ShowAllAsFraction
{
get
{
return ShowAllAsFraction;
}
set
{
ShowAllAsFraction = value;
}
}
private double d_Real = 0;
private double d_Imaginary = 0;
private Fraction F_Real;
private Fraction F_Imaginary;
#endregion
#region ctors
public Complex()
{
ShowAllAsFraction = false;
d_Real = 0;
d_Imaginary = 0;
F_Real = new Fraction();
F_Imaginary = new Fraction();
}
public Complex(double a, double b, bool ShowFraction)
{
this.F_Real = new Fraction(a);
this.F_Imaginary = new Fraction(b);
this.d_Real = a;
this.d_Imaginary = b;
this.ShowAllAsFraction = ShowFraction;
}
public Complex(double a, Fraction b, bool ShowFraction)
{
this.F_Real = new Fraction(a);
this.F_Imaginary = b;
this.d_Real = a;
this.d_Imaginary = b.ToDouble();
this.ShowAllAsFraction = ShowFraction;
}
public Complex(Fraction a, double b, bool ShowFraction)
{
this.F_Real = a;
this.F_Imaginary = new Fraction(b);
this.d_Real = a.ToDouble();
this.d_Imaginary = b;
this.ShowAllAsFraction = ShowFraction;
}
public Complex(Fraction a, Fraction b, bool ShowFraction)
{
this.F_Real = a;
this.F_Imaginary = b;
this.d_Real = a.ToDouble();
this.d_Imaginary = b.ToDouble();
this.ShowAllAsFraction = ShowFraction;
}
#endregion
#region setValues
public void setValues(double a, double b, bool toFraction)
{
this.F_Real = new Fraction(a);
this.F_Imaginary = new Fraction(b);
this.d_Real = a;
this.d_Imaginary = b;
this.ShowAllAsFraction = toFraction;
}
public void setValue(double a, Fraction b, bool toFraction)
{
this.F_Real = new Fraction(a);
this.d_Real = a;
this.d_Imaginary = b.ToDouble();
this.F_Imaginary = b;
this.ShowAllAsFraction = toFraction;
}
public void setValue(Fraction a, double b, bool toFraction)
{
this.d_Real = 0;
this.F_Real = a;
this.F_Imaginary = new Fraction(b);
this.d_Imaginary = b;
this.ShowAllAsFraction = toFraction;
}
public void setValue(Fraction a, Fraction b, bool toFraction)
{
this.d_Real = a.ToDouble();
this.d_Imaginary = b.ToDouble();
this.F_Real = a;
this.F_Imaginary = b;
this.ShowAllAsFraction = toFraction;
}
#endregion
#region public functions
public override bool Equals(object o)
{
return true;
}
public override int GetHashCode()
{
return 0;
}
/// <summary>
/// This function returns a double value representing the length of a vector in a complex number space
/// </summary>
/// <returns></returns>
public double Getlength()
{
if (ShowAllAsFraction)
return Math.Sqrt(Math.Pow(this.F_Real.ToDouble(), 2) + Math.Pow(this.F_Imaginary.ToDouble(), 2));
else
return Math.Sqrt(Math.Pow(this.d_Real, 2) + Math.Pow(this.d_Imaginary, 2));
}
public string PrintComplex(char Seperator)
{
if (this.ShowAllAsFraction)
{
return this.F_Real.PrintFraction(Seperator) + " " + this.F_Imaginary.PrintFraction(Seperator) + " i";
}
else
{
return this.d_Real.ToString() + this.d_Imaginary.ToString() + " i";
}
}
#endregion
#region operators
public static bool operator <(Complex a, Complex b)
{
return a.Getlength() < b.Getlength();
}
public static bool operator <=(Complex a, Complex b)
{
return a.Getlength() <= b.Getlength();
}
public static bool operator ==(Complex a, Complex b)
{
return a.Getlength() == b.Getlength();
}
public static bool operator !=(Complex a, Complex b)
{
return a.Getlength() != b.Getlength();
}
public static bool operator >=(Complex a, Complex b)
{
return a.Getlength() >= b.Getlength();
}
public static bool operator >(Complex a, Complex b)
{
return a.Getlength() > b.Getlength();
}
public static Complex operator +(Complex a, Complex b)
{
return new Complex(a.F_Real + b.F_Real, a.F_Imaginary + b.F_Imaginary, a.ShowAllAsFraction);
}
public static Complex operator -(Complex a, Complex b)
{
return new Complex(a.F_Real - b.F_Real, a.F_Imaginary - b.F_Imaginary, a.ShowAllAsFraction);
}
public static Complex operator *(Complex a, Complex b)
{
return new Complex(a.F_Real * b.F_Real - a.F_Imaginary * b.F_Imaginary, a.F_Real * b.F_Imaginary + a.F_Imaginary * b.F_Real, a.ShowAllAsFraction);
}
public static Complex operator /(Complex a, Complex b)
{
return new Complex((a.F_Real * b.F_Real + a.F_Imaginary * b.F_Imaginary) / (b.F_Real * b.F_Real + b.F_Imaginary * b.F_Imaginary), (a.F_Real * b.F_Imaginary - a.F_Imaginary * b.F_Real) / (b.F_Real * b.F_Real + b.F_Imaginary * b.F_Imaginary), a.ShowAllAsFraction);
}
#endregion
}
}
Schlagwörter: Klassenbibliothek, Mathematik
Aaalso..
..deine Klassen,,, Ich finde diese recht gut implementierbar, auch wenn es das ein oder andere Problemchen damit gibt.
Hier fehlt dir z.B. der private member:
[B]private bool _ShowAllAsFraction;[/B]
public bool ShowAllAsFraction
{
get
{
return _ShowAllAsFraction;
}
set
{
_ShowAllAsFraction = value;
}
}
..dann würde ich noch anregen, bei einer mathematischen Klasse wie der Complex-Klasse auch die Eulerform und z.B. das Konjugieren zu implementieren.. wie z.B.:
public Complex Conjugate()
{
return new Complex(this.d_Real,- this.d_Imaginary, false);
}
public Trigonometrical ToTrigonometrical()
{
Trigonometrical retVal = new Trigonometrical();
if (Math.Sqrt(this.d_Real * this.d_Real + this.d_Imaginary * this.d_Imaginary) == 0)
retVal = new Trigonometrical(0, 0);
else
{
double r = Math.Sqrt(this.d_Real * this.d_Real + this.d_Imaginary * this.d_Imaginary),
phi;
if (d_Imaginary >= 0)
phi = Math.Acos(this.d_Real / r);
else
phi = Math.Acos(-this.d_Real / r) - Math.PI;
retVal = new Trigonometrical(r, phi);
}
return retVal;
}
public class Trigonometrical
{
public double R, Phi;
public Complex ToComplex()
{
return (new Complex(R * Math.Cos(Phi),
R * Math.Sin(Phi),
false));
}
public Trigonometrical()
{
R = 0;
Phi = 0;
}
public Trigonometrical(double r, double phi)
{
R = r;
Phi = phi;
}
}
}
...denn kann kann man recht einfach zwischen der karthesichen Form und der polaren Form hin und her switchen 😄...z.B.:
Complex c1 = new Complex(1, 2, false),
c2 = new Complex(1, 3, false);
Complex c3 = c1 + c2.Conjugate();
Complex.Trigonometrical a = c3.ToTrigonometrical();
Complex c4 = a.ToComplex();