PHP写的求多项式导数的函数代码

2015-01-24信息快讯网

PHP写的求多项式导数的函数代码,需要的朋友可以参考下

 
<?php 
function getDerivativeByFormulaAndXDATA($formula, $x_data){ 
$xArray = explode("+", $formula); 
$Derivative = 0; 
foreach ($xArray as $x_record) { 
$tmpArray = explode("x^", $x_record); 
if(count($tmpArray) == 2){ 
$coefficient = $tmpArray[0]==""?1:$tmpArray[0]; 
$exp = $tmpArray[1]; 
} 
//constant 
else { 
$coefficient = $tmpArray[0]; 
$exp = 0; 
} 
$Derivative += $coefficient*$exp*pow($x_data,$exp-1); 
} 
return $Derivative; 
} 
function getValueByFormulaAndXDATA($formula, $x_data){ 
$xArray = explode("+", $formula); 
$y_data = 0; 
foreach ($xArray as $x_record) { 
$tmpArray = explode("x^", $x_record); 
if(count($tmpArray) == 2){ 
$coefficient = $tmpArray[0]==""?1:$tmpArray[0]; 
$exp = $tmpArray[1]; 
} 
//constant 
else { 
$coefficient = $tmpArray[0]; 
$exp = 0; 
} 
$y_data += $coefficient*pow($x_data,$exp); 
} 
return $y_data; 
} 
function getMaxDerivativeByFormulaAndXDATAS($formula, $x_datas, &$matchs){ 
$derivatives = array(); 
$max_derivative = 0; 
foreach ($x_datas as $x_data) { 
$derivative = getDerivativeByFormulaAndXDATA($formula, $x_data); 
$derivatives[$x_data] = $derivative; 
$max_derivative = $max_derivative>=abs($derivative)?$max_derivative:abs($derivative); 
//printf("x=%f, derivative=%f \n",$x_data, $derivative); 
} 
$matchs = array(); 
foreach ($derivatives as $x_data=>$derivative) { 
if(abs($derivative) == $max_derivative){ 
$matchs[] = $x_data; 
} 
} 
printf("max derivative=%f\n",$max_derivative); 
foreach ($matchs as $x_match) { 
printf(" derivative=%f when x=%f\n",$derivatives[$x_match], $x_match); 
} 
} 
//notice the format of formula: ax^b if b=0 could omit except coefficient a, if a=1 could omit coefficient 
$formula = "x^2+2x^1+1"; 
print "The formula is $formula \n"; 
//printf("Derivative of 2 is %f \n",getDerivativeByFormulaAndXDATA($formula, 3.2)); 
//print getValueByFormulaAndXDATA($formula, 3.2)."\n"; 
$sampleData = array(-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9,10,11,12); 
foreach ($sampleData as $x_data) { 
$str.=$x_data.", "; 
} 
print "sample x values: $str \n"; 
getMaxDerivativeByFormulaAndXDATAS($formula, $sampleData, $matchs)."\n"; 
?> 

the output will be:
sample x values: -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
max derivative=26.000000
derivative=26.000000 when x=12.000000
PHP 清空varnish 缓存的详解(包括指定站点下的)
PHP操作MongoDB GridFS 存储文件的详解
解析Linux下Varnish缓存的配置优化
解析用PHP实现var_export的详细介绍
解析smarty 截取字符串函数 truncate的用法介绍
用Json实现PHP与JavaScript间数据交换的方法详解
php echo, print, print_r, sprintf, var_dump, var_expor的使用区别
php class中public,private,protected的区别以及实例分析
浅析is_writable的php实现
解析php中static,const与define的使用区别
探讨:如何通过stats命令分析Memcached的内部状态
eAccelerator的安装与使用详解
深入理解:单一入口、MVC、ORM、CURD、ActiveRecord概念
Could not load type System.ServiceModel.Activation.HttpModule解决办法
PHP类与对象中的private访问控制的疑问
php类中private属性继承问题分析
php中根据变量的类型 选择echo或dump
php中禁止单个IP与ip段访问的代码小结
PHP sprintf() 函数的应用(定义和用法)
php中jQuery插件autocomplate的简单使用笔记
php文件打包 下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
php 函数中使用static的说明
PHP number_format() 函数定义和用法
php提示Call-time pass-by-reference has been deprecated in的解决方法[已测]
PHP filter_var() 函数 Filter 函数
PHP新手用的Insert和Update语句构造类
php提示无法加载或mcrypt没有找到 PHP 扩展 mbstring解决办法
PHP中将字符串转化为整数(int) intval() printf() 性能测试
PHP和JAVA中的重载(overload)和覆盖(override) 介绍
JS中encodeURIComponent函数用php解码的代码
PHP警告Cannot use a scalar value as an array的解决方法
php数组函数序列之array_values() 获取数组元素值的函数与方法
判断Keep-Alive模式的HTTP请求的结束的实现代码
php expects parameter 1 to be resource, array given 错误
©2014-2024 dbsqp.com