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 <developer.ignace@gmail.com>
* @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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.