siteIcon

Tech Novice Tools

PHP Source Code

burgerIcon

Simple Radical Formradical

PHP Source Code

We love OOP at TNT. Who knew that PHP utilizes it as well?!

Because PHP is a server-side language, however, we can't normally see the source code, so we thought we'd give you a peek at it here.

The Radical class can be used anytime you are needing to process radicals, say, like when using the quadratic formula?

SRF: Radical Class Source Code
                            
class Radical{
	//properties
	//original values
	private $coeff0;
	private $rad0;
	//simplified values
	private $coeff;
	private $rad;
	//approximate value
	private $approx;
	private $decPlaces = 5;
	//factors
	private $factorInfo;
	private $factors;
	private $duplicates;
	private $gps; //greatest perfect square
	
	function __construct($c, $r){
		$this->coeff0 = $c;
		$this->rad0 = $r;
		$this->coeff = $c;
		$this->rad = $r;
		$this->approx = round($c * sqrt(abs($r)), $this->decPlaces);
		if($r == 0) {
			$this->factorInfo = "not applicable";
			$this->factors = "not applicable";
		}
		else{
			if($r == 1) {
				$this->factorInfo = "1";
				$this->factors = "1";
			}
			else{
				if(isWholeNumber($r)) 
                    $this->factorInfo = factorInfo(abs($r));
				else $this->factorInfo = "1; ".$r;
				
				$this->factors = factor((abs($r)));
			}
		}
		//$this->factors = factor((abs($r)));
		$this->duplicates = $this->simplify();
	}//end function __construct
	
	function printOriginalRadical(){
		/*
			code below inspired by:
			http://www.scientificpsychic.com/etc/square-root.html
		*/
		echo number_format($this->coeff0)." ".number_format($this->rad0)." 
";
	}//end function printOriginalRadical
    
	function printSimplifiedRadical(){
		/*
			code below inspired by:
			http://www.scientificpsychic.com/etc/square-root.html
		*/
		$absRad = number_format(abs($this->rad));
		$simpRad = number_format($this->coeff)." ".$absRad." 
";
		if($this->rad0 <0) $simpRad .= " i";
		echo $simpRad;
	}//end function printSimplifiedRadical
	
	//1/20/10
	function printAdjustedRadicand(){
		/*
			code below inspired by:
			http://www.scientificpsychic.com/etc/square-root.html
		*/
		$absRadicand = number_format($this->gps). 
            " * ". number_format(abs($this->rad));
		$adjRad = number_format($this->coeff0)." ".$absRadicand." 
";
		if($this->rad0 <0) $adjRad .= " i";
		echo $adjRad;
	}//end function printAdjustedRadicand
	
	//accessors
	function get_approx(){return $this->approx;}
	function get_factors(){return $this->factorInfo;}
	function get_duplicates(){return $this->duplicates;}
	function get_rad(){return $this->rad;}
	function get_rad0(){return $this->rad0;}
	function get_coeff(){return $this->coeff;}
	function get_gps(){return $this->gps;}
	
	//modifiers 1/12/10 for quadratic function analysis
	//when dealing with a discrim that isnt a whole number
	function set_rad($v){
		$this->rad = $v;
	}
    
	function set_coeff($v){
		$this->coeff = $v;
	}
	
	function simplify(){
		if(isWholeNumber($this->rad0)){
			$temp = $this->factors;
			//had occassional warnings about reading only int or strings
			//suppressed the warning with the @ symbol
			$s = @array_count_values($temp);
			$p = 1;
			for($i = 0; $i < sizeof($s); $i++){
				$k = array_keys($s);
				$ftr = $k[$i];
				$times = $s[$ftr];
				if($times > 1){
					if($times % 2 == 0) //even
					{
						$e = $times/2;
					}
					else //odd
					{
						$e = ($times - 1)/2;
					}
					$inc = pow($ftr, $e);
					$p *= $inc;
				}						
			}
			$this->coeff = $this->coeff0*$p;
			$this->rad = $this->rad0/pow($p,2);
			$this->gps = pow($p, 2);
		}
		else{
			$s = array();
			array_push($s, $this->rad0);
		}
		return $s;		
	}//end function simplify	
    
}//end class Radical 
                        

Last update: 09/19/19