Jump to content

[SOLVED] complex numbers


ignace

Recommended Posts

anyone can figure out what is wrong with this? when i try to multiply the result i get is wrong, i checked the documentation and my implementation is correct but php thinks otherwise :(

 

<?php

/**
* Class for calculating complex numbers
* 
* @author Ignace Knops <[email protected]>
* @version 1.0
*/
class Complex
{
    /**
     * Real number
     * 
     * @var integer
     */
    var $a;
    
    /**
     * Imaginary unit
     * 
     * @var integer
     */
    var $b;
    
    /**
     * Class constructor
     * 
     * @param integer
     * @param integer
     */
    function Complex($a = 1, $b = 1)
    {
        $this->a = (int) $a;
        $this->b = (int) $b;
    }

    /**
     * 
     * 
     * @param integer|Complex
     * @param integer
     * @return Complex
     */
    function add($a, $b = null)
    {
        $a = (int) $a;
        $b = (int) $b;
        if (!is_a($a, 'Complex')) {
            $a = $this->a + $a;
            $b = $this->b + $b;
        } else {
            $c = $this->a + $a->a;
            $b = $this->b + $a->b;
            $a = $c;
        }
        return new Complex($a, $b);
    }

    /**
     * 
     * 
     * @param integer|Complex
     * @param integer
     * @return Complex
     */
    function subtract($a, $b = null)
    {
        $a = (int) $a;
        $b = (int) $b;
        if (!is_a($a, 'Complex')) {
            $a = $this->a - $a;
            $b = $this->b - $b;
        } else {
            $c = $this->a - $a->a;
            $b = $this->b - $a->b;
            $a = $c;
        }
        return new Complex($a, $b);
    }

    /**
     * 
     * 
     * @param integer|Complex
     * @param integer
     * @return Complex
     */
    function multiply($a, $b = null)
    {
        $a = (int) $a;
        $b = (int) $b;
        if (!is_a($a, 'Complex')) {
            $a = (($this->a * $a) - ($this->b * $b));
            $b = (($this->a * $b) + ($this->b * $a));
        } else {
            $c = (($this->a * $a->a) - ($this->b * $a->b));
            $b = (($this->a * $a->b) + ($this->b * $a->a));
            $a = $c;
        }
        return new Complex($a, $b);
    }

    /**
     * Alias for __toString()
     * 
     * @return string
     */
    function toString()
    {
        return $this->__toString();
    }
    
    /**
     * Returns a string version of the complex number
     * 
     * @return string
     */
    function __toString()
    {
        return sprintf("%s + %si", $this->a, $this->b);
    }
}

 

for example

 

require_once 'Complex.php';

$c = new Complex(4, 7);
echo $c->multiply(5, -3)->toString(); // returns 41 + 275i while it should be: 41 + 23i

 

the formula:

 

(a + bi) * (c + di) = (ac - bd) + (ad + bc)i.

 

my multiply() method with adjusted params:

 

/**
     * 
     * 
     * @param integer|Complex
     * @param integer
     * @return Complex
     */
    function multiply($c, $d = null)
    {
        $a = (int) $a;
        $b = (int) $b;
        if (!is_a($a, 'Complex')) {
            $a = (($this->a * $c) - ($this->b * $d));
            $b = (($this->a * $d) + ($this->b * $c));
        } else {
            $e = (($this->a * $c->a) - ($this->b * $c->b));
            $b = (($this->a * $c->b) + ($this->b * $c->a));
            $a = $e;
        }
        return new Complex($a, $b);
    }

Link to comment
https://forums.phpfreaks.com/topic/119979-solved-complex-numbers/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.