Differences between matrix-java and php computing results

Source: Internet
Author: User
One method used to calculate the matrix weight the input matrices are {,-1,-7}, {-,-,-5, 1} {-,-, 7} {,-1,-} else this is the java computing code staticdouble [] weightsnewdoubleweights [5]; privatedoub... matrixphpjava functions

A method for calculating matrix weights
All 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 java computing code.
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 ;}then the result is

CI = 1, 0.097302165775079
Weights: 0.10850015438106028
Weights: 0.056125161448460546
Weights: 0.45493710163432743
Weights: 0.06991685010726335
Weights: 0.3105207324288884
Bytes ------------------------------------------------------------------------------------
This is the php computing 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 calculation result is different from the third digit after the decimal point.
Is this error normal?
Is the error related to 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.