Hello everyone, I got some code I wish to compute for some Mie data. However it has some errors relating to +,-,* and/ for complex numbers with int and doubles. I got code online for using operator with in a function with 2 parameters (for light scattering off skin molecules. But, the IDE (VS 2015) says operator only works with 1 parameter. When using both I cannot my equations invoving complex numbers. See below. There are some small errors I will mention too.
Functions of class VectorsMat1 (operator functions with 1 parameter):
// division of a complex number A by a complex number B
complex<double> operator /(complex<double> A){
complex<double>B;
return 1 / pow(Cabs(B), 2)*A*compConj(B);
}
// division of a real number a by a complex number B
complex<double> operator /(int a){
complex<double>B;
return a*U0 / B;
}
// division of a complex number A by a real number b
complex<double> operator /(complex<double> A){
double b;
return 1 / b*A;
}
// division of a real number a by a complex number B
complex<double> operator /(int a){
complex<double>B;
return double(a)*U0 / B;
}
// division of a complex number A by a complex number B
complex<double> operator /(complex<double> A){
complex<double>B;
return 1 / pow(Cabs(B), 2)*A*compConj(B);
}
//division of a real number with a complex number
complex<double> operator *(double a){
complex<double>B;
a*real(B);
a*imag(B);
return B;
}
Now functions of VectorsMat2 (takes 2 parameters but IDE says operator function has too many parameters- got this from STOKES scattering of light from grains adapted to skin molecules)
struct complex
{
double re;
double im;
};
complex C(double a, double b){
complex res;
res.re = a;
res.im = b;
return res;
}
// addition of two complex number A and B
complex operator +(complex A, complex B) {
double CAbs(complex A) {
return sqrt((A*CConj(A)).re);
}
// multiplication of a real with a complex number
complex operator *(double a, complex B) {
complex res;
res.re = a*B.re;
res.im = a*B.im;
return res;
}
// multiplication of a complex with a real number
complex operator *(complex A, double b) {
return b*A;
}
// division of a complex number A by a complex number B
complex operator /(complex A, complex B) {
return 1 / pow(CAbs(B), 2)*A*CConj(B);
}
// division of a real number a by a complex number B
complex operator /(double a, complex B) {
return a*U0 / B;
}
// division of a complex number A by a real number b
complex operator /(complex A, double b) {
return 1 / b*A;
}
Errors in IDE log after building:
1st: CAbs function (VectorsMat2)->return sqrt((A*CConj(A)).re); (It's underlined at * and error says no operator "*" matches these operands, operand types are VectorsMat2::complex*VectorsMat2::complex
2nd: This is in a function called MieCalc with declaration
void MieCalc(double sizeParam, complex compRelRefrac, int NAng, complex S1[],
complex S2[], double& Qext, double& Qsca);
y = sizeParam * compRelRefrac; (double x complex number)
D[nmx - (n + 1)] = rn / y - 1 / (D[nmx - n] + rn / y); (this is in a for loop)
(At the division signs after rn (int) and before y-1 (with y-complex<double> type) it says no operator "/" matches the operands, operands are int/VectorsMat2::complex
I have the same kinds of error with
S2[j] = S2[j] + fn * (an*tau[j] + bn * Pi[j]); (this is in a for loop)
but just with it says no operator "*" matches the operands (with an being complex and tau an array storing doubles)
I need to help to sort these errors- which version of the operator functions are right (I tried with both VectorsMat1 and VectorsMat2 and they did not fix the errors). Hope this is not confusing.
Can't you add a complex and a double together without a special function (etc)?? Most of these functions should not even need to exist. Complex<> type already supports all this stuff.
you can't multiply a complex and an array though. You can't multiply anything against an array! You need a loop or something to do that, or if tau is representing a complex in an array, you can promote it to a complex and then do it?
this is just a mess. Sorry, but it is.
I would rewrite it using complex and vectors. If you need your vectors to behave like mathmatical vectors, wrap them in a small class to give the math operator overloads. Then just write math statements, it should clean up nicely.
@jonnin tau is an array but it does not store complex numbers it stores double (like normal numbers-notice the tau[j]-j being the iteration). You said Complex<> already supports this-are you talking about the header:
include <complex> or something else. Sorry.. I will try to edit this an state it is in a loop.
I am saying that this
complex<double> result;
can do things like these:
// multiplication of a real with a complex number
without a hand-rolled function. Just do it.
result *= 3.14; //ok. Done.
Either you are showing the complex<> file (this is built into c++, no need to show it) or you have something someone hand-wrote to do the same thing (???) in which case its extra weird because its using <complex> ... ?