kepler Posted October 5, 2018 Share Posted October 5, 2018 Hi I have a C++ code which I'm trying to convert to a php routine. Some parts seemed easy...but others are not. The main function is (in C++): function solve(const double al, const complex<double> &z, const double u, complex<double> &zto, double &r) { const double ex = abs(z); const double ex2 = ex*ex; const double ex3 = ex2*ex; double e = (al + (ex - ex3/8)*sin(al) + 0.5 * ex2 * sin(2*al) + 0.375 * ex3 * sin(3*al)); complex<double> z1 = conj(z); for (int k = 0; k < 10; k++) { const complex<double> z2(0, e); const complex<double> zteta = exp(z2); const complex<double> z3 = z1*zteta; const double dl = al - e + z3.imag(); r = 1 - z3.real(); if (fabs(dl) < 1e-12) { z1 = u * z * z3.imag(); const complex<double> z2(z1.imag(), -z1.real()); zto = (-z + zteta + z2)/r; return; } e += dl/r; } xpWarn("can't converge\n", __FILE__, __LINE__); } The function is called like this: complex<double> z(xk, xh); complex<double> zto; double r; solve(xl, z, u, zto, r); //xl, u and r are doubles const double xcw = zto.real(); const double xsw = zto.imag(); const double xm = xp*xcw - xq*xsw; const double xxx = xa*r; So, function Solve returns the complex number zto and the double r which are necessary to the calculations to proceed. Can someone help me out here? Kind regards Kepler Quote Link to comment Share on other sites More sharing options...
requinix Posted October 5, 2018 Share Posted October 5, 2018 Probably the first thing you need to do is find a PHP library for handling complex numbers. I'll bet PEAR has something... Quote Link to comment Share on other sites More sharing options...
kepler Posted October 5, 2018 Author Share Posted October 5, 2018 3 hours ago, requinix said: Probably the first thing you need to do is find a PHP library for handling complex numbers. I'll bet PEAR has something... Hi Thanks for the reply. I found this small class. Maybe you can help me understand some things?... class Complex { private $real = 0; private $imag = 0; public function __construct($real, $imag = null) { if ($imag === null) { } else { $this->real = (float)$real; $this->imag = (float)$imag; } } public function __toString() { $str = ""; if (round($this->imag, 12) != 0) $str .= sprintf("%.12f", $this->imag) . "i"; if ($str) $str .= "+"; if (round($this->real, 12) != 0) $str .= sprintf("%.12f", $this->real); if (!$str) $str = "0"; return $str; } public function abs() { return sqrt($this->real * $this->real + $this->imag * $this->imag); } public function add(Complex $c) { $this->real += $c->real; $this->imag += $c->imag; } } Tell me if this is right please: $z1 = new Complex(2,1); $z2 = new Complex(7,5); $z3 = new Complex; $x1 = $z1->abs(); //$x1 has now the absolute value of the complex var. $z1 = sqrt(2*2+1*1)?... $z3 = $z1->add($z2); //$z3 is now equal to (2+7,1+5)?... Cheers Kepler Quote Link to comment Share on other sites More sharing options...
requinix Posted October 5, 2018 Share Posted October 5, 2018 That's right. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.