将数学方程从PHP转换为Javascript(Jquery)

时间:2011-08-04 21:45:41

标签: php javascript jquery math jquery-mobile

我一直致力于将一系列变异系数从PHP移植到Javascript,但似乎无法使其正常工作。

原始PHP脚本:

// define variables, strip spaces
$weights = $_POST['weights'];
// define coefficient of variation function
function cv($array){
    $n = 0;
    $mean = 0;
    $M2 = 0;
    foreach($array as $x){
        if ($x != NULL AND $x != '') {
            $n++;
            $delta = $x - $mean;
            $mean = $mean + $delta/$n;
            $M2 = $M2 + $delta*($x - $mean);
            $total = $total + $x;
        }
    }
    return (((sqrt($M2/($n - 1))) / ($total/$n))*100);
}
$cv = (cv($weights));

这基本上是一个数组,并计算出它的变异系数。现在我尝试通过一些Jquery函数将其转换为Javascript:

var fields = $('#cvform').serializeArray();
        var count = 0;
        var num = 0;
        var mean = 0;
        var m2 = 0;
        var total = 0;
        var delta = 0;
        jQuery.each(fields, function(i, field){
        if (field.value > 0) {
            num++;
            delta=(field.value-mean);
            mean=(mean+delta/num);
            m2=(m2+delta*(field.value-mean));
            total=(total+field.value);
        };
        });
        var cov=(((Math.sqrt(m2/(num-1)))/(total/num))*100);
        $("<span>Coefficient of Variation: " + cov +  "</span>").appendTo('#cvdisplay');

虽然javascript函数输出答案,但它不正确。如果我输入值“3,3,2,3,3,4”,PHP脚本给出的输出为21.08,这是正确的。 javascript函数给我的值为0.0011418432035849642。

有人能指出方程式的不同之处吗?

5 个答案:

答案 0 :(得分:1)

问题是javascript行 total=(total+field.value);导致0332334而不是预期的18。正在应用字符串连接而不是数字加法。

您可以通过解析整数值来解决此问题:total += parseInt(field.value);

答案 1 :(得分:1)

您需要通过parseFloat()(或整数,parseInt(),无论适合您的方式)将数组值转换为浮点值:

var fields = $('#cvform').serializeArray();
var count = 0;
var num = 0;
var mean = 0;
var m2 = 0;
var total = 0;
var delta = 0;
$.each(fields, function(i, field) {
    alert(field.value);
    if (parseFloat(field.value) > 0) {
        num++;
        delta = (parseFloat(field.value) - mean);
        mean = (mean + delta / num);
        m2 = (m2 + delta * (parseFloat(field.value) - mean));
        total = (total + parseFloat(field.value));
    }
});
var cov = (((Math.sqrt(m2 / (num - 1))) / (total / num)) * 100);
$("<span>Coefficient of Variation: " + cov +  "</span>").appendTo('#cvdisplay');

答案 2 :(得分:0)

function stDeviation(array){
    var L= array.length,
    mean= array.reduce(function(a, b){
        return a+b;
    })/L,
    dev= array.map(function(itm){
        var tem= itm-mean;
        return tem*tem;
    });
    return Math.sqrt(dev.reduce(function(a, b){
        return a+b;
    })/L);
}

Math.mean= function(array){
    return array.reduce(function(a, b){ return a+b; })/array.length;
}
Math.stDeviation=function(array){
    var mean= Math.mean(array);
    dev= array.map(function(itm){return (itm-mean)*(itm-mean); });
    return Math.sqrt(dev.reduce(function(a, b){ return a+b; })/array.length);
}


var A2= [6.2, 5, 4.5, 6, 6, 6.9, 6.4, 7.5];
alert ('mean: '+Math.mean(A2)+'; deviation: '+Math.stDeviation(A2))

答案 3 :(得分:0)

在这里,我稍微更改了代码并使其工作。我基本上把这些段分开了一点。使它更直接。

<?php
$weights = Array(3,3,2,3,3,4);

// define coefficient of variation function
function cv($array) {
  $n = 0;
  $mean = 0;
  $M2 = 0;
  $total = 0;

  foreach($array as $x) {
    if ( !empty($x) ) {
      $n++;
      $delta = $x - $mean;
      $mean = $mean + $delta / $n;
      $M2 = $M2 + $delta * ($x - $mean);
      $total = $total + $x;
    }
  }

  $sqrt = sqrt( $M2 / ($n - 1) );
  $tn = $total / $n;
  echo "Sqrt is $sqrt tn is $tn";

  return ( $sqrt / $tn ) * 100;
}

$cv = cv($weights);
?>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  var fields = Array(3,3,2,3,3,4);
  var count = 0;
  var n = 0;
  var mean = 0;
  var m2 = 0;
  var total = 0;
  var delta = 0;

  jQuery.each(fields, function(i, field) {
    //var x = field.value;
    var x = 1 * field;

    if (x > 0) {
      n++;
      delta = (x - mean);
      mean = (mean + (delta / n));
      m2 = (m2 + delta * (x - mean));
      total = (total + x);
    };
  });

  var sqrt = Math.sqrt(m2 / (n - 1));
  var tn = total / n;
  var cov = ((sqrt / tn) * 100);
  console.log("Total is: "+ total);
  console.log("Sqrt is " + sqrt + " tn is " + tn + " cov is " + cov);

  $('#js').text("JS Output is: " + cov);
});
</script>
</head>
<body>
  <div>
    <div>PHP Output: <?=$cv;?></div>
    <div id="js"></div>
  </div>
</body>
</html>

答案 4 :(得分:-1)

这是您提供的功能的直接翻译。你必须传入一个javascript数组,它会产生你正在寻找的结果(至少根据Node.js编写的单元测试)。您应该在另一个函数中进行类型转换(字符串到数组)以分离您的关注点并使代码更容易推理;

var CoV = function(ary) {
    var mean = 0,
        n = 0,
        m2 = 0,
        total = 0,
        delta;

    for(var i = 0, l = ary.length; i < l; i += 1) {
        n += 1;

        delta = ary[i] - mean;
        mean = mean + delta / n;
        m2 = m2 + delta * (ary[i] - mean)
        total = total + ary[i]
    }

    console.log(mean);
    console.log(m2);
    console.log(total);

    return ((Math.sqrt(m2/(i - 1))) / (total / i)) * 100;
};