Stavo definendo uno struct per i numeri complessi (grazie Microsoft per non averli considerati!!!!).
Ho eseguito l'overloading degli operatori fondamentali, tuttavia ho dei problemi con l'overloading delle funzioni trigonometriche.
Questo è il file dove definisco lo struct.
Codice: Seleziona tutto
// Define Complex Class, that allows to create a complex number
// that allow us to work with complex numbers
using System;
using Mono.Math;
namespace MaxSim
{
struct Complex
{
double r;
double i;
public Complex(double real, double imaginary)
{
this.r = real;
this.i = imaginary;
}
public double Real
{
get
{
return(r);
}
set
{
r = value;
}
}
public double Imaginary
{
get
{
return(i);
}
set
{
i = value;
}
}
public override string ToString()
{
return(String.Format("({0}, {1}i)", r, i));
}
// Basic boolean operators
public static bool operator==(Complex c1, Complex c2)
{
if ((c1.r == c2.r) && (c1.i == c2.i))
return(true);
else
return(false);
}
public static bool operator!=(Complex c1, Complex c2)
{
return(!(c1 == c2));
}
public override bool Equals(object o2)
{
Complex c2 = (Complex) o2;
return(this == c2);
}
public override int GetHashCode()
{
return(r.GetHashCode() ^ i.GetHashCode());
}
// The Four Operations
public static Complex operator+(Complex c1, Complex c2)
{
return(new Complex(c1.r + c2.r, c1.i + c2.i));
}
public static Complex operator-(Complex c1, Complex c2)
{
return(new Complex(c1.r - c2.r, c1.i - c2.i));
}
public static Complex operator*(Complex c1, Complex c2)
{
return(new Complex(c1.r * c2.r - c1.i * c2.i, c1.r * c2.i + c2.r * c1.i));
}
public static Complex operator/(Complex c1, Complex c2)
{
if((c2.r == 0.0f) && (c2.i == 0.0f))
throw new DivideByZeroException("Divisione per zero di un numero complesso");
double newR = (c1.r * c2.r + c1.i * c2.i) / (c2.r * c2.r + c2.i * c2.i);
double newI = (c2.r * c1.i - c1.r * c2.i) / (c2.r * c2.r + c2.i * c2.i);
return (new Complex(newR, newI));
}
// Trigonometric Functions
public override Complex Mono.Math.Sin(Complex c1)
{
return (new Complex(Math.Cosh(c1.i) * Math.Sin(c1.r), Math.Cos(c1.r) * Math.Sinh(c1.i)));
}
}
}
Posso risolvere evitando l'overloading e introducendo Sin come funzione membro di complex, in questo modo
Codice: Seleziona tutto
// Trigonometric Functions
public Complex Sin(Complex c1)
{
return (new Complex(Math.Cosh(c1.i) * Math.Sin(c1.r), Math.Cos(c1.r) * Math.Sinh(c1.i)));
}
Voi cosa mi consigliate di fare?
Daniele

