Polinom Fonksiyonlarının Türevlerini Alma

<?php
class turev {
	var $ds = array(
		'log(ax+b)' => 'd(ax+b)/(ax+b)',
		'cos(ax+b)' => '-1*sin(ax+b)*d(ax+b)',
		'sin(ax+b)' => '1*cos(ax+b)*d(ax+b)');
	function coklu_turev($fonksiyon) {
		// İşlev formatı: k(coklu)^n + k(coklu)^n vb ...
		$q = explode(" ",$fonksiyon);
		foreach ($q as $r => $s) {
			if (strlen($s) > 1) {
				$t = preg_match("/([0-9]+)\((.+)\)\^([0-9-])/",$s,$w);
				if (is_array($w)) {
					if ($w[3] == 1) {
						$c1 = $w[1];
						$c2 = $this->tekli_turev(str_replace("+"," + ",str_replace("-"," - ",$w[2])));
						$df[] = $c1*$c2."x^0";
					}
					elseif ($w[3] == 0) $df[] = 0;
					else {
						$c1 = $w[1]*$w[3];
						$pfin = $w[3]-1;
						$c2 = $this->tekli_turev(str_replace("+"," + ",str_replace("-"," - ",$w[2])));
						if (is_int($c2)) {
							$c3 = $c1*$c2;
							$df[] = $c3."(".$w[2].")^".$pfin;
						}
						else {
							$r = $c1."(".$w[2].")^".$pfin;
							$df[] = $r;							
						}
					}
				}
				else{
					$df[] = $s;
				} 
			}
			else{
				if($t==1){
					$df[] = " + ";
				}else{
					$df[] = $t;
				}
			}
		}
		return implode($df);
	}
	function tekli_turev($fonksiyon) {
		// İşlev formatı: kx^n + kx^n + kx^n
		$a = explode(" ",$fonksiyon);
		foreach ($a as $b => $c) {
			if (strlen($c) > 1) {
				$r = preg_match("/([0-9]+)x\^([0-9-]+)/",$c,$d);
				if (is_array($d)) {
					if ($d[2] > 1 or $d[2] < -1) $turev[] = ($d[1]*$d[2])."x^".($d[2]-1);
					elseif (abs($d[2]) == 1) $turev[] = $d[1];
					else array_pop($turev);
					unset($d);
				}
				else $turev[] = $c;
			}
			else $turev[] = $c;
		}
		return implode(" ",$turev);
	}
}
$t= new turev;
echo $t->coklu_turev("3(x+b)^3 + 3(x+b)^3");
echo "<br>";
echo $t->tekli_turev("3x^2 + 3x^2 + 3x^2");

Çıktısı

9(x+b)^2 + 9(x+b)^2
6x^1 + 6x^1 + 6x^1

 

Sizin Değerli Görüşlerinize İhtiyacımız Var.