Difference between revisions of "Quadratic Solver"

From CodeCodex

(Ruby)
Line 1: Line 1:
<highlightsyntax language="PHP">
+
==Implementations==
 +
 
 +
===PHP===
 +
<pre class="php">
 
// Quadratic Solver for PHP by Gazza 2004-03-21
 
// Quadratic Solver for PHP by Gazza 2004-03-21
 
// Useage: for 3x^2 + 4x + 5 = 0, use quadratic(3, 4, 5, 'root1') and quadratic(3, 4, 5, 'root2'),
 
// Useage: for 3x^2 + 4x + 5 = 0, use quadratic(3, 4, 5, 'root1') and quadratic(3, 4, 5, 'root2'),
Line 41: Line 44:
 
if ($root == 'both') {return $lambdaone. ' and ' .$lambdatwo;}
 
if ($root == 'both') {return $lambdaone. ' and ' .$lambdatwo;}
 
} // End function
 
} // End function
</highlightsyntax>
+
</pre>
 +
 
 +
===Ruby===
 +
<pre class="ruby">
 +
require 'complex'      # It is unnecessary from version 1.9.
 +
 
 +
def quadratic(a, b, c)
 +
  d = b * b - 4 * a * c
 +
  a2 = a * 2.0
 +
  if d < 0
 +
    real = -b / a2
 +
    imag = Math::sqrt(-d) / a2
 +
    r1 = Complex(real, imag)
 +
    r2 = Complex(real,-imag)
 +
  elsif d == 0
 +
    r1 = r2 = -b / a2
 +
  else
 +
    r1 = ( -b + Math::sqrt(d) ) / a2
 +
    r2 = ( -b - Math::sqrt(d) ) / a2
 +
  end
 +
  return r1, r2
 +
end
 +
</pre>
  
 
=== Tcl ===
 
=== Tcl ===
Line 65: Line 90:
 
}
 
}
 
</pre>
 
</pre>
 +
 
[[Category:PHP]]
 
[[Category:PHP]]
 +
[[Category:Ruby]]
 
[[Category:Tcl]]
 
[[Category:Tcl]]
 
[[Category:Math]]
 
[[Category:Math]]

Revision as of 11:33, 7 March 2011

Implementations

PHP

// Quadratic Solver for PHP by Gazza 2004-03-21
// Useage: for 3x^2 + 4x + 5 = 0, use quadratic(3, 4, 5, 'root1') and quadratic(3, 4, 5, 'root2'),
// or simply just quadratic(3, 4, 5, 'both').
// The $root argument doesn't define which value it will be in relation to the other - I could
// easily have called them John and Jane, instead of 1st and 2nd.
//
function quadratic($a, $b, $c, $root)
{
	$precision = 3; // Change this value for a different decimal places rounding.
	
	$bsmfac = $b*$b-4*$a*$c;
	if ($bsmfac < 0) { // Accounts for complex roots.
		$plusminusone = " + "; $plusminustwo = " - ";
		$bsmfac *=-1;
		$complex=(sqrt($bsmfac)/(2*$a));
		if ($a < 0){ //if negative imaginary term, tidies appearance.
			$plusminustwo = " + ";
			$plusminusone = " - ";
			$complex *= -1;
		} // End if ($a < 0)
		$lambdaone = round(-$b/(2*$a), $precision).$plusminusone.round($complex, $precision).'i';
		$lambdatwo = round(-$b/(2*$a), $precision).$plusminustwo.round($complex, $precision).'i';
	} // End if ($bsmfac < 0)
	
	else if ($bsmfac == 0) { // Simplifies if b^2 = 4ac (real roots).
		$lambdaone = round(-$b/(2*$a), $precision);
		$lambdatwo = round(-$b/(2*$a), $precision);
	} // End else if (bsmfac == 0)
	
	else { // Finds real roots when b^2 != 4ac.
		$lambdaone = (-$b+sqrt($bsmfac))/(2*$a);
		$lambdaone = round($lambdaone, $precision);
		$lambdatwo = (-$b-sqrt($bsmfac))/(2*$a);
		$lambdatwo = round($lambdatwo, $precision);
	} // End else
	
	// Return what is asked for.
	if ($root == 'root1') {return $lambdaone;}
	if ($root == 'root2') {return $lambdatwo;}
	if ($root == 'both') {return $lambdaone. ' and ' .$lambdatwo;}
} // End function

Ruby

require 'complex'       # It is unnecessary from version 1.9.

def quadratic(a, b, c)
  d = b * b - 4 * a * c
  a2 = a * 2.0
  if d < 0
    real = -b / a2
    imag = Math::sqrt(-d) / a2
    r1 = Complex(real, imag)
    r2 = Complex(real,-imag)
  elsif d == 0
    r1 = r2 = -b / a2
  else
    r1 = ( -b + Math::sqrt(d) ) / a2
    r2 = ( -b - Math::sqrt(d) ) / a2
  end
  return r1, r2
end

Tcl

package require math::complexnumbers

proc quadratic {a b c} {
 set 2a [expr {$2*$a}]
 set delta [expr {$b**2 - 4*$a*$c}]
 set roots {}
 if {$delta < 0} {
   set x1 [expr {(-1.0*$b) / $2a}]
   set x2 [expr {sqrt(abs($delta)) / $2a}]
   lappend roots [tostring [complex $x1 $x2]] \
                 [tostring [complex $x1 [expr {-1 * $x2}]]]
 } elseif {$delta == 0} {
   lappend roots [expr {-1.0*$b / $2a)}]
 } else {
   lappend roots [expr {(-1*$b + sqrt($delta)) / $2a}] \
                 [expr {(-1*$b - sqrt($delta)) / $2a}]
 }
 return $roots
}