complex inverse tangent function

Complex::arctan($z) computes and returns the principal branch of arctan($z)


@param mixed $z (Complex, or parsable to Complex)
@retval Complex
428644
public static function arctan($z) {
        if (!($z instanceof Complex)) $z = static::parse($z);

        $I = new Complex(0,1);
        $iz = static::mul($z, $I);
        $w = static::div( static::add(1, $iz), static::sub(1, $iz) );
        $logw = static::log($w);
        return static::div($logw, new Complex(0,2));
    }
892409
public static function log($z) {
        if (!($z instanceof Complex)) $z = static::parse($z);

        $r = $z->abs();
        $theta = $z->arg();

        if ($r == 0) return new Complex(NAN, NAN);

        return new Complex(log($r), $theta);
    }
656611
public static function add($z, $w) {
        if (!($z instanceof Complex)) $z = static::parse($z);
        if (!($w instanceof Complex)) $w = static::parse($w);

        return static::create($z->x + $w->x, $z->y + $w->y);
    }
1126897
public function inverse($p)
    {
        $x = ($p->x - $this->x0) / $this->k0;
        $y = ($this->rh - ($p->y - $this->y0) / $this->k0);

        if ($this->ns > 0) {
            $rh1 = sqrt($x * $x + $y * $y);
            $con = 1.0;
        } else {
            $rh1 = -sqrt($x * $x + $y * $y);
            $con = -1.0;
        }
        $theta = 0.0;

        if ($rh1 != 0) {
            $theta = atan2(($con * $x ), ($con * $y));
        }

        if (($rh1 != 0) || ($this->ns > 0.0)) {
            $con = 1.0 / $this->ns;
            $ts = pow(($rh1 / ($this->a * $this->f0)), $con);
            $lat = Common::phi2z($this->e, $ts);

            if ($lat == -9999) {
                return null;
            }
        } else {
            $lat = -Common::HALF_PI;
        }
        $lon = Common::adjust_lon($theta / $this->ns + $this->long0);

        $p->x = $lon;
        $p->y = $lat;

        return $p;
    }
923979
protected function calculateOffset($zone)
    {
        if ($this->validation()->isLocation($zone)) {
            return $this->getOffsetFromLocation($zone);
        }

        if ($this->validation()->isUtc($zone)) {
            return $this->getOffsetFromUtc($zone);
        }

        if ($this->validation()->isAbbr($zone)) {
            return $this->getOffsetFromAbbr($zone);
        }

        return 0;
    }
1003217
public static function exp($z) {
        if (!($z instanceof Complex)) $z = static::parse($z);

        $r = exp($z->x);
        return new Complex($r*cos($z->y), $r*sin($z->y));
    }
1095212
public function setInverse($inverse)
    {
        if (!is_bool($inverse)) {
            throw new \InvalidArgumentException('The attribute inverse on the class Attribute has to be boolean (' . gettype($inverse) . ('object' === gettype($inverse) ? ' ' . get_class($inverse) : '') . ' given).');
        }

        $this->inverse = $inverse;

        return $this;
    }
72189
public function complexMul(ComplexType $a, ComplexType $b)
    {
        $r = $this->rationalSub(
            $this->rationalMul($a->r(), $b->r()),
            $this->rationalMul($a->i(), $b->i())
        );
        $i = $this->rationalAdd(
            $this->rationalMul($a->i(), $b->r()),
            $this->rationalMul($a->r(), $b->i())
        );
        return ComplexTypeFactory::create($r, $i);
    }
400860
public function inverse($p)
    {
        /*
        $dlon;
        $xx;
        $yy;
        $xys;
        $c1;
        $c2;
        $c3;
        $al;
        $asq;
        $a1;
        $m1;
        $con;
        $th1;
        $d;
        */

        $p->x -= $this->x0;
        $p->y -= $this->y0;

        $con = Common::PI * $this->R;

        $xx = $p->x / $con;
        $yy = $p->y / $con;

        $xys = $xx * $xx + $yy * $yy;

        $c1 = -abs( $yy ) * (1.0 + $xys);
        $c2 = $c1 - 2.0 * $yy * $yy + $xx * $xx;
        $c3 = -2.0 * $c1 + 1.0 + 2.0 * $yy * $yy + $xys * $xys;

        $d = $yy * $yy / $c3 + (2.0 * $c2 * $c2 * $c2 / $c3 / $c3 / $c3 - 9.0 * $c1 * $c2 / $c3 / $c3) / 27.0;

        $a1 = ($c1 - $c2 * $c2 / 3.0 / $c3) / $c3;
        $m1 = 2.0 * sqrt( -$a1 / 3.0 );
        $con = ((3.0 * $d) / $a1) / $m1;

        if (abs($con) > 1.0) {
            if ($con >= 0.0) {
                $con = 1.0;
            } else {
                $con = -1.0;
            }
        }

        $th1 = acos($con) / 3.0;

        if ($p->$y >= 0) {
            $lat = (-$m1 * cos($th1 + Common::PI / 3.0) - $c2 / 3.0 / $c3) * Common::PI;
        } else {
            $lat = -(-m1 * cos($th1 + Common::PI / 3.0) - $c2 / 3.0 / $c3) * Common::PI;
        }

        if (abs($xx) < Common::EPSLN) {
            $lon = $this->$long0;
        }

        $lon = Common::adjust_lon(
            $this->long0 + Common::PI * ($xys - 1.0 + sqrt(1.0 + 2.0 * ($xx * $xx - $yy * $yy) + $xys * $xys)) / 2.0 / $xx
        );

        $p->x = $lon;
        $p->y = $lat;

        return $p;
    }
117226
public function inverse($p)
    {
        /*
        $delta_lon; /* Delta longitude (Given longitude - center 
        $theta;  /* angle
        $delta_theta; /* adjusted longitude
        $sin_phi;
        $cos_phi; /* sin and cos value	
        $b;  /* temporary values
        $c;
        $t;
        $tq; /* temporary values
        $con;
        $n;
        $ml; /* cone constant, small m	
        $vs;
        $us;
        $q;
        $s;
        $ts1;
        $vl;
        $ul;
        $bs;
        $dlon;
        $flag;
        */

        $p->x -= $this->x0;
        $p->y -= $this->y0;
        #$flag = 0;

        $vs = $p->x * $this->cosaz - $p->y * $this->sinaz;
        $us = $p->y * $this->cosaz + $p->x * $this->sinaz;
        $us = $us + $this->u;

        $q = exp( -$this->bl * $vs / $this->al );
        $s = .5 * ($q - 1.0 / $q);
        $t = .5 * ($q + 1.0 / $q);

        $vl = sin( $this->bl * $us / $this->al );
        $ul = ($vl * $this->cosgam + $s * $this->singam) / $t;

        if (abs(abs($ul) - 1.0) <= Common::EPSLN) {
            $lon = $this->longc;
            if (ul >= 0.0) {
                $lat = Common::HALF_PI;
            } else {
                $lat = -Common::HALF_PI;
            }
        } else {
            $con = 1.0 / $this->bl;
            $ts1 = pow( ($this->el / sqrt( (1.0 + $ul) / (1.0 - $ul) ) ), $con );
            $lat = Common::phi2z( $this->e, $ts1 );
            //if ($flag != 0)
            //return($flag);
            //~ con = cos($this->bl * us /al);
            $theta = $this->longc - atan2( ($s * $this->cosgam - $vl * $this->singam ), $con ) / $this->bl;
            $lon = Common::adjust_lon( $theta );
        }

        $p->x = $lon;
        $p->y = $lat;

        return $p;
    }
304598
public static function sin($z) {
        if (!($z instanceof Complex)) $z = static::parse($z);

        return static::create(sin($z->x)*cosh($z->y), cos($z->x)*sinh($z->y));
    }
1050610
public function inverse($p)
    {
        $x = $p->x;
        $y = $p->y;

        $delta_x = $x - $this->x0;
        $delta_y = $y - $this->y0;

        // 1. Calculate z
        $z_re = $delta_y / $this->a;
        $z_im = $delta_x / $this->a;

        // 2a. Calculate theta - first approximation gives km accuracy
        $z_n_re = 1;
        $z_n_im = 0;  // z^0
        $z_n_re1;
        $z_n_im1;

        $th_re = 0;
        $th_im = 0;

        for ($n = 1; $n <= 6; $n++) {
            $z_n_re1 = $z_n_re * $z_re - $z_n_im * $z_im;
            $z_n_im1 = $z_n_im * $z_re + $z_n_re * $z_im;
            $z_n_re = $z_n_re1;
            $z_n_im = $z_n_im1;
            $th_re = $th_re + $this->C_re[$n] * $z_n_re - $this->C_im[$n] * $z_n_im;
            $th_im = $th_im + $this->C_im[$n] * $z_n_re + $this->C_re[$n] * $z_n_im;
        }

        // 2b. Iterate to refine the accuracy of the calculation
        //        0 iterations gives km accuracy
        //        1 iteration gives m accuracy -- good enough for most mapping applications
        //        2 iterations bives mm accuracy
        for ($i = 0; $i < $this->iterations; $i++) {
            $th_n_re = $th_re;
            $th_n_im = $th_im;
            $th_n_re1;
            $th_n_im1;

            $num_re = $z_re;
            $num_im = $z_im;

            for ($n = 2; $n <= 6; $n++) {
                $th_n_re1 = $th_n_re * $th_re - $th_n_im * $th_im;
                $th_n_im1 = $th_n_im * $th_re + $th_n_re * $th_im;
                $th_n_re = $th_n_re1;
                $th_n_im = $th_n_im1;
                $num_re = $num_re + ($n - 1) * ($this->B_re[$n] * $th_n_re - $this->B_im[$n] * $th_n_im);
                $num_im = $num_im + ($n - 1) * ($this->B_im[$n] * $th_n_re + $this->B_re[$n] * $th_n_im);
            }

            $th_n_re = 1;
            $th_n_im = 0;
            $den_re = $this->B_re[1];
            $den_im = $this->B_im[1];

            for ($n = 2; $n <= 6; $n++) {
                $th_n_re1 = $th_n_re * $th_re - $th_n_im * $th_im;
                $th_n_im1 = $th_n_im * $th_re + $th_n_re * $th_im;
                $th_n_re = $th_n_re1;
                $th_n_im = $th_n_im1;
                $den_re = $den_re + $n * ($this->B_re[$n] * $th_n_re - $this->B_im[$n] * $th_n_im);
                $den_im = $den_im + $n * ($this->B_im[$n] * $th_n_re + $this->B_re[$n] * $th_n_im);
            }

            // Complex division
            $den2 = $den_re * $den_re + $den_im * $den_im;
            $th_re = ($num_re * $den_re + $num_im * $den_im) / $den2;
            $th_im = ($num_im * $den_re - $num_re * $den_im) / $den2;
        }

        // 3. Calculate d_phi              ...                                    // and d_lambda
        $d_psi = $th_re;
        $d_lambda = $th_im;
        $d_psi_n = 1;  // d_psi^0

        $d_phi = 0;
        for ($n = 1; $n <= 9; $n++) {
            $d_psi_n = $d_psi_n * $d_psi;
            $d_phi = $d_phi + $this->D[$n] * $d_psi_n;
        }

        // 4. Calculate latitude and longitude
        // d_phi is calcuated in second of arc * 10^-5, so we need to scale back to radians. d_lambda is in radians.
        $lat = $this->lat0 + ($d_phi * Common::SEC_TO_RAD * 1E5);
        $lon = $this->long0 + $d_lambda;

        $p->x = $lon;
        $p->y = $lat;

        return $p;
    }
781145
public function getAst(Node\Node $b, $path = '')
    {
        $ret = array(
            'type' => str_replace('Zicht\Tool\Script\Node\\', '', get_class($b))
        );
        if ($b instanceof Node\Branch) {
            if (count($b->nodes)) {
                $ret['nodes'] = array();

                foreach ($b->nodes as $n) {
                    if (null === $n) {
                        $ret['nodes'][] = $n;
                    } else {
                        if (!$n instanceof Node\Node) {
                            throw new \InvalidArgumentException("Invalid child node in " . Util::toPhp($path));
                        }
                        $ret['nodes'][] = $this->getAst($n);
                    }
                }
            }
        }

        return $ret;
    }
178877
public function inverse($p)
    {
        $x = ($p->x - $this->x0) / $this->a;   /* descale and de-offset */
        $y = ($p->y - $this->y0) / $this->a;

        /*
        $lon;
        $lat;
        $cosphi;
        $sinphi;
        $rho;
        $tp = 0.0;
        $phi_l = 0.0;
        $i;
        */

        $halfe = 0.0;
        $pi2 = 0.0;

        if ($this->sphere) {
            /*
            $c;
            $rh;
            $sinc;
            $cosc;
            */

            $rh = sqrt( $x * $x + $y * $y );
            $c = 2. * atan( $rh / $this->akm1 );
            $sinc = sin( $c );
            $cosc = cos( $c );
            $lon = 0.;

            switch ($this->mode) {
                case $this->EQUIT:
                    if (abs( $rh ) <= Common::EPSLN) {
                        $lat = 0.;
                    } else {
                        $lat = asin( $y * $sinc / $rh );
                    }
                    if ($cosc != 0. || $x != 0.0)
                        $lon = atan2( $x * $sinc, $cosc * $rh );
                    break;

                case $this->OBLIQ:
                    if (abs($rh) <= Common::EPSLN) {
                        $lat = $this->phi0;
                    } else {
                        $lat = asin( $cosc * $this->sinph0 + $y * $sinc * $this->cosph0 / $rh );
                    }
                    $c = $cosc - $this->sinph0 * sin( $lat );
                    if ($c != 0. || $x != 0.0) {
                        $lon = atan2( $x * $sinc * $this->cosph0, $c * $rh );
                    }
                    break;

                case $this->N_POLE:
                    $y = -$y;

                case $this->S_POLE:
                    if (abs($rh) <= Common::EPSLN) {
                        $lat = $this->phi0;
                    } else {
                        $lat = asin( $this->mode == $this->S_POLE ? -$cosc : $cosc  );
                    }
                    $lon = ($x == 0. && $y == 0.0) ? 0. : atan2($x, $y);
                    break;
            }

            $p->x = Common::adjust_lon( $lon + $this->long0 );
            $p->y = $lat;
        } else {
            $rho = sqrt($x * $x + $y * $y);

            switch ($this->mode) {
                case $this->OBLIQ:
                case $this->EQUIT:
                    $tp = 2. * atan2( $rho * $this->cosX1, $this->akm1 );
                    $cosphi = cos( $tp );
                    $sinphi = sin( $tp );
                    if ($rho == 0.0) {
                        $phi_l = asin($cosphi * $this->sinX1);
                    } else {
                        $phi_l = asin($cosphi * $this->sinX1 + ($y * $sinphi * $this->cosX1 / $rho));
                    }

                    $tp = tan( .5 * (Common::HALF_PI + $phi_l) );
                    $x *= $sinphi;
                    $y = $rho * $this->cosX1 * $cosphi - $y * $this->sinX1 * $sinphi;
                    $pi2 = Common::HALF_PI;
                    $halfe = .5 * $this->e;
                    break;

                case $this->N_POLE:
                    $y = -$y;

                case $this->S_POLE:
                    $tp = - $rho / $this->akm1;
                    $phi_l = Common::HALF_PI - 2. * atan( $tp );
                    $pi2 = -Common::HALF_PI;
                    $halfe = -.5 * $this->e;
                    break;
            }

            for ($i = $this->NITER; $i--; $phi_l = $lat) { //check this
                $sinphi = $this->e * sin($phi_l);
                $lat = 2.0 * atan( $tp * pow((1.0 + $sinphi) / (1.0 - $sinphi), $halfe)) - $pi2;

                if (abs( phi_l - lat ) < $this->CONV) {
                    if ($this->mode == $this->S_POLE) {
                        $lat = -$lat;
                    }

                    $lon = ($x == 0. && $y == 0.0) ? 0. : atan2($x, $y);
                    $p->x = Common::adjust_lon( $lon + $this->long0);
                    $p->y = $lat;

                    return $p;
                }
            }
        }
    }
584561
public function inverse($p)
    {
        $x = $p->x;
        $y = $p->y;

        $p->x = Common::adjust_lon( $this->long0 + (($x - $this->x0) / ($this->a * $this->rc)) );
        $p->y = Common::adjust_lat( $this->lat0 + (($y - $this->y0) / ($this->a )) );

        return $p;
    }
1354886
public static function sqrt($z)
    {
        if (!($z instanceof Complex)) $z = static::parse($z);

        $r = sqrt($z->abs());
        $theta = $z->arg()/2;

        return new Complex($r*cos($theta), $r*sin($theta));
    }
413305
public function drawLine($xStart, $yStart, $zStart, $xEnd, $yEnd, $zEnd)
	{
		$this->setCode("(line)");
		$this->setCode("G0 X{$xStart} Y{$yStart} Z{$this->spindleAxisStartPosition}");
		$this->setCode("G1 Z{$zStart}");
		
		// testing tool setup tool metric M8 diameter 4mm
		$this->setCode("T1 M08");
		$this->setCode("G41 D4");
		
		$this->setCode("G1 X{$xStart} Y{$yStart} Z{$zStart}");
		$this->setCode("G1 X{$xEnd} Y{$yEnd} Z{$zEnd}");

		$this->setCode("G0 Z{$this->spindleAxisStartPosition}");
		$this->setCode("G0 X{$xStart} Y{$yStart}");
		$this->setCode("(/line)\n");
		
		return $this;
	}