The difference between Matrix-java and PHP calculation results

Source: Internet
Author: User
Keywords Matrix PHP Java functions
Tags pow
Matrixphpjava function

A method of calculating matrix weights
The input matrices are
{0, 2, 6, 1,-7},
{ -2,0,-5, 0,-5}
{ -6,5, 0, 8, 1}
{ -1,0,-8, 0, 7}
{7,5,-1,-7, 0}

This is the computational code for Java
Static double[] Weights=new double weights[5];
Private double computeciandweights (int[][] matrix) {
Double totalweight = 0;
for (int i = 0; i < weights.length; i++) {
Weights[i] = 1;
for (int j = 0; J < Weights.length; J + +)
Weights[i] *= decode (matrix[i][j]);
Weights[i] = Math.pow (Weights[i], (double) 1/weights.length);
Totalweight + = Weights[i];
}
for (int i = 0; i < weights.length; i++)
Weights[i]/= totalweight;
Double Eig = 0;
for (int i = 0; i < weights.length; i++) {
Double part = 0;
for (int j = 0; J < Weights.length; J + +)
Part + = decode (Matrix[i][j]) *weights[j];
Part/= Weights[i]*weights.length;
Eig + = part;
}
Return (eig-weights.length)/(WEIGHTS.LENGTH-1);
}

    private double decode(int num) {        if (num < 0)            return (double)1/(1-num);        else            return num+1;    }    然后结果是

ci=0.097302165775079
weights:0.10850015438106028
weights:0.056125161448460546
weights:0.45493710163432743
weights:0.06991685010726335
weights:0.3105207324288884
------------------------------------------------------------------------------------
This is the PHP calculation code.

function computeciandweights ($matrix,& $weights) {
$totalWeight = 0.0;
$len = 5;
for ($i =0; $i < $len; $i + +) {
$weights [$i]=1;
for ($j =0; $j < $len; $j + +) {
$weights [$i]*=decode ($matrix [$i] [$j]);
}
$weights [$i]=pow ($weights [$i],doubleval (1/$len));
$totalWeight +=doubleval ($weights [$i]);
}
for ($i =0; $i < $len; $i + +) {
$weights [$i]/= $totalWeight;
}
$eig = 0.0;
for ($i =0; $i < $len; $i + +) {
$part = 0.0;
for ($j =0; $j < $len; $j + +) {
$part +=decode ($matrix [$i] [$j]) * $weights [$j];
}
$part/= $weights [$i]* $len;
$eig + = $part;
}
Return ($eig-$len)/($len-1);
}
function decode ($num) {
if ($num <0) {
return Doubleval (1/(1-$num));
}else{
return $num +1;
}
}

$ci =computeciandweights ($inputMatrix, $weights);
Echo ' ci= '. $ci.
';
for ($i =0; $i echo ' weights= '. $weights [$i].
';
}

The result is
ci=0.0973021657751.
weights=0.106290974412.
weights=0.0549823927295.
weights=0.445674092399.
weights=0.0631581840822.
weights=0.329894356377.

The weight is calculated as the third digit after the decimal point is different.
Does this error belong to normal?
is the error caused by the calculation of floating-point numbers?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.