ÿþ<HTML> <HEAD> <TITLE>Given two sequences, find doubly stochastic matrix</TITLE> <!-- <TITLE>majorization matrix</TITLE> 9906212315 change title --> <META http-equiv=Content-Type content="text/html"> <style type="text/css"> v\:* { behavior: url(#default#VML); } </style> <!-- The Cauchy-Schwarz Master Class author --> <meta name="author" content="J. Michael Steele" /> <!--matMVecf.htm author --> <meta name="author" content="Liu, HsinHan ‰Rk”"o &#21129;&#37995;&#28450;" /> <script type="text/javascript"> <!-- var docA='<pre>' +'<a name=docA001>&lt;a name=docA001&gt;</a>\n' +'The following is document for\n' +'function permutef()\n' +'2010-06-27-13-24 document start\n' +'Assume input data \n' +'± sequence and ß sequence be\n' +'0.2 0.2 0.2 0.2 0.2 \n' +'1 0 0 0 0 \n' +'<a name=docA002>&lt;a name=docA002&gt;</a>\n' +'All calculation never change\n' +'± seq. Because ± seq. is the\n' +'goal of ß seq. transformation.\n' +'<a name=docA003>&lt;a name=docA003&gt;</a>\n' +'Each iteration do two ß seq.\n' +'modification.\n' +'First is RobinHood average \n' +' transformation\n' +'Second is sort to descending\n' +' re-order transformation\n' +' by a permutation matrix.\n' +'If function jkCheck() is coded\n' +'correctly, RobinHood average \n' +'from sequence center to two \n' +'ends. Permutation matrix \n' +'is always identity matrix.\n' +'<a name=docA004>&lt;a name=docA004&gt;</a>\n' +'Both MUST be carry out by \n' +'matrix multiplication. because\n' +'final answer stochastic matrix\n' +'is the over-all product of\n' +'RobinHood matrix and permutation\n' +'matrix. If permute by hand,\n' +'missing one permutation matrix,\n' +'final answer will be incomplete.\n' +'<a name=docA005>&lt;a name=docA005&gt;</a>\n' +'Early version of jsmajor2.htm\n' +'average ß seq. from\n' +'1 0 0 0 0 \n' +'to\n' +'0.8 0 0.2 0 0 \n' +'<a name=docA006>&lt;a name=docA006&gt;</a>\n' +'In this case need permutation\n' +'matrix change from\n' +'0.8 0 0.2 0 0 \n' +'to\n' +'0.8 0.2 0 0 0 \n' +'<a name=docA007>&lt;a name=docA007&gt;</a>\n' +'Program send \n' +'inp0=[0.8 0 0.2 0 0 ]\n' +'to \n' +'inp1=sortNumb(inp0)\n' +'receive index\n' +'0 2 1 3 4\n' +'// NOT receive new-ordered\n' +'// 0.8 0.2 0 0 0 \n' +'<a name=docA008>&lt;a name=docA008&gt;</a>\n' +' index\n' +'0 2 1 3 4\n' +'tell program that\n' +'inp0[0]g"inp0[2]g"inp0[1]g"inp0[3]g"inp0[4]\n' +'that is \n' +' 0.8 g" 0.2 g" 0 g" 0 g" 0 \n' +'<a name=docA009>&lt;a name=docA009&gt;</a>\n' +'Returned index 0 2 1 3 4 //y coordinate\n' +' current index 0 1 2 3 4 //x coordinate\n' +'give us the permutation matrix \n' +'building block.\n' +'<a name=docA010>&lt;a name=docA010&gt;</a>\n' +'First build a all-zero 5x5 matrix\n' +' 0 0 0 0 0\n' +' 0 0 0 0 0\n' +' 0 0 0 0 0\n' +' 0 0 0 0 0\n' +' 0 0 0 0 0\n' +'<a name=docA011>&lt;a name=docA011&gt;</a>\n' +'then fill "1" at [x,y] = [0,0] \n' +'[1,2] [2,1] [3,3] [4,4] to form\n' +'permutation matrix \n' +'1 0 0 0 0 \n' +'0 0 1 0 0 ÿÿ this "1" is [2,1]\n' +'0 1 0 0 0 ÿÿ this "1" is [1,2]\n' +'0 0 0 1 0 \n' +'0 0 0 0 1 \n' +'<a name=docA012>&lt;a name=docA012&gt;</a>\n' +'carry out matrix multiplication\n' +'[1 0 0 0 0] [0.8]\n' +'[0 0 1 0 0] [ 0 ]\n' +'[0 1 0 0 0] * [0.2]\n' +'[0 0 0 1 0] [ 0 ]\n' +'[0 0 0 0 1] [ 0 ]\n' +'<a name=docA013>&lt;a name=docA013&gt;</a>\n' +'we get sorted new sequence\n' +' 0.8 0.2 0 0 0 \n' +'\n' +'2010-06-27-13-49 here\n' +'<a name=docA014>&lt;a name=docA014&gt;</a>\n' +'Assume \n' +'first iteration get\n' +'RobinHood matrix R1\n' +'first iteration get\n' +'permutation matrix P1\n' +'<a name=docA015>&lt;a name=docA015&gt;</a>\n' +'second iteration get\n' +'RobinHood matrix R2\n' +'second iteration get\n' +'permutation matrix P2\n' +'etc.\n' +'<a name=docA016>&lt;a name=docA016&gt;</a>\n' +'the final answer is matrix\n' +'multiplication of\n' +'P2*R2*P1*R1*ß seq. ---eqn.maj01\n' +'Because\n' +' R1 first operate on ß seq.\n' +' P1 then operate on R1*ß seq.\n' +' R2 then operate on P1*R1*ß seq.\n' +'etc. Code line is next two lines\n' +'stochM0=matMVecf(perMat,RobinHood);\n' +'stochMat=matMVecf(stochM0,stochMat); //9906191747\n' +'<a name=docA017>&lt;a name=docA017&gt;</a>\n' +'If carry out more iteration,\n' +'pile up Pj*Rj to the left of eqn.maj01\n' +'Matrix order must not be altered.\n' +'P3*R3*P2*R2*P1*R1*ß seq. is correct\n' +'R3*P3*R2*P2*R1*P1*ß seq. is wrong\n' +'P1*R1*P2*R2*P3*R3*ß seq. is wrong\n' +'\n' +'2010-06-27-13-57 stop\n' +'\n' +'Above is document for\n' +'function permutef()\n' +'</pre>' //9906271615 convert to docA string function permutef(arg1,arg2) { //9906180035 start var inp0=readdata(arg1); var inp1=sortNumb(inp0); var n=inp0.length; var i,j,k; if(arg2==1) for(i=0;i<n;i++) { if(i>n/2)break; j=inp1[i]; inp1[i]=inp1[n-1-i]; inp1[n-1-i]=j; } var perMat=new Array(n); for(i=0;i<n;i++) perMat[i]=new Array(n); for(i=0;i<n;i++) for(j=0;j<n;j++) perMat[i][j]=0; //9906180040 for(i=0;i<n;i++) { perMat[i][inp1[i]]=1; //9906180041 } return perMat; } //9906180046 function permutef(arg1,arg2) var docB='<pre>' +'<a name=docB001>&lt;a name=docB001&gt;</a><br>' +'The following is document for\n' +'function stochastic0()\n' +'It is the most important part\n' +'of this program. 9906271827\n' +'<a name=docB002>&lt;a name=docB002&gt;</a><br>' +'2010-06-17-18-07 start\n' +'<a href=#textbook>CSMC</a> page 198\n' +'alpha=(alpha1,alpha2)\n' +'alpha=(rho+sigma,rho-sigma)\n' +'and\n' +'beta=(beta1,beta2)\n' +'beta=(rho+tau,rho-tau)\n' +'<a name=docB003>&lt;a name=docB003&gt;</a><br>' +'alpha(&lt;)beta if and only if\n' +' sigma&lt;=tau\n' +'(<a href=#textbook>CSMC</a> page 198, line -4)\n' +'alpha1&gt;=alpha2\n' +' beta1&gt;=beta2\n' +'<a name=docB004>&lt;a name=docB004&gt;</a><br>' +'<a href=#textbook>CSMC</a> page 199\n' +'if we take alpha1&gt;=alpha2&gt;=alpha3&gt;=...\n' +'and beta1&gt;=beta2&gt;=beta3&gt;=...\n' +'where alpha (&lt;) beta\n' +'proof by induction on the number N\n' +'of coordinates j such that \n' +'alpha_j != bata_j\n' +'<a name=docB005>&lt;a name=docB005&gt;</a><br>' +'we assume that N&gt;=1 or else\n' +'we can simply take conversion \n' +'matrix D to be identity matrix.\n' +'Now given N&gt;=1, the definition \n' +'of majorization implies that\n' +'<a name=docB006>&lt;a name=docB006&gt;</a><br>' +'there must exist a pair of integer\n' +'1&lt;=j&lt;k&lt;=n for which we have the\n' +'bounds\n' +' alpha_j &lt; beta_j ---eqn.13.15 bgn\n' +' beta_k &lt; alpha_k \n' +'and\n' +' beta_s = alpha_s\n' +' for all j&lt;s&lt;k ---eqn.13.15 end\n' +'<a name=docB007>&lt;a name=docB007&gt;</a><br>' +'figure 13.1 gives a useful \n' +'representation of this situation, \n' +'the interval [alpha_k, alpha_j] \n' +'is properly contained in the\n' +'interval [beta_k, beta_j] \n' +'The intervening values\n' +' beta_s = alpha_s for all j&lt;s&lt;k \n' +'are omitted.\n' +'<a name=docB008>&lt;a name=docB008&gt;</a><br>' +'Figure 13.1 marks out\n' +' rho=(beta_k + beta_j)/2\n' +'and tau&gt;=0 which we choose so\n' +'that beta_j=rho+tau\n' +'and beta_k=rho-tau\n' +'tau is defined to be\n' +' max(|alpha_k-rho|, |alpha_j-rho|)\n' +'<a name=docB009>&lt;a name=docB009&gt;</a><br>' +'We now take T to be n*n doubly\n' +'stochastic matrix which takes\n' +'beta=(beta1, beta2, ... betan)\n' +'to //<a href=#textbook>CSMC</a> page 199 line -7\n' +'beta\'=(beta1\',beta2\',... betan\')\n' +'//<font color=red><b>beta\'=stochastic matrix * beta</b></font>\n' +'2010-06-17-18-32 stop\n' +'<a name=docB010>&lt;a name=docB010&gt;</a><br>' +'2010-06-17-19-11 start\n' +'where\n' +' betak\'=betak+sigma\n' +' betaj\'=betaj-sigma\n' +'and betat\'=betat for t!=j, t!=k\n' +'<a name=docB011>&lt;a name=docB011&gt;</a><br>' +'put four numbers\n' +'(tau+sigma)/(2tau)\n' +'(tau-sigma)/(2tau)\n' +'(tau-sigma)/(2tau)\n' +'(tau+sigma)/(2tau)\n' +'at T matrix j,k rows and j,k columns\n' +'the rest of diagonal T is n-2 ones\n' +'the rest of off-diagonal T is n*n-n-2 zeros\n' +'<a name=docB012>&lt;a name=docB012&gt;</a><br>' +'<a href=#textbook>CSMC</a> page 200\n' +'we still need to check\n' +' alpha (&lt;) beta\'=T*beta\n' +'If we use //st was s sub t, simplify to st\n' +' st(gamma)=gamma1+gamma2+...+gammat\n' +'to simplify the writing of partial sum,\n' +'<a name=docB013>&lt;a name=docB013&gt;</a><br>' +'then we have three basic observations\n' +' st(alpha)&lt;=st(beta)=st(beta\') for 1&lt;=t&lt;j (a)\n' +' st(alpha)&lt;=st(beta\') for j&lt;=t&lt;k (b)\n' +' st(alpha)&lt;=st(beta)=st(beta\') for k&lt;=t&lt;=n (c)\n' +'<a name=docB014>&lt;a name=docB014&gt;</a><br>' +'observation (a) and (c) are immediately\n' +'to justify (b) we only need to note that\n' +' alphaj &lt;= betaj\'\n' +'and recall that \n' +' alphat = betat\' = betat for j&lt;t&lt;k\n' +'three bounds confirm that alpha (&lt;) beta\'\n' +'by the design of matrix T, we know that\n' +'<a name=docB015>&lt;a name=docB015&gt;</a><br>' +'the n-tuples alpha and beta\' agree in\n' +'all but at most N-1 coordinates. Hence\n' +'by induction, there is a doubly\n' +'stochastic matrix D\' such that \n' +'alpha = D\'*beta\'. Since beta\'=T*beta, \n' +'<a name=docB016>&lt;a name=docB016&gt;</a><br>' +'we therefore have\n' +' alpha=D\'(T*beta)=(D\'T)*beta\n' +'since the product of two doubly\n' +'stochastic matrix is doubly stochastic.\n' +'we see that the matrix D=D\'*T provides us \n' +'with the solution to our challenge problem.\n' +'2010-06-17-19-57. stop\n' +'Above is document for function stochastic0()\n' +'Above copied from page 198 to page 200 of\n' +'<a href=http://www.amazon.com/review/product/052154677X/ref=cm_cr_dp_all_helpful?%5Fencoding=UTF8&coliid=&showViewpoints=1&colid=&sortBy=bySubmissionDateDescending>The Cauchy-Schwarz Master Class</a>\n' +'Above is document for\n' +'function stochastic0()\n' +'</pre>' ; function stochastic0( arg1, //define alpha sequence, more average arg2, //define beta sequence, more diverse arg3, //define tolerance arg4, //define maximum iteration outSeq, //±,²,transformed ² ; box 3 debug outRHm, //Robin Hood Matrix ; box 4 debug outERc, //ERror Code ; box 5 debug outPRm, //PeRmute Matrix ; box 6 debug spanAl //<span> for alert ; QGspanAlert1 ) { //9906172025 start var alfa=readdata(arg1); var QGbx18=0; //9906271900 if(typeof QGboxb18=='object' &&typeof QGchkbxcC1=='object' &&QGchkbxcC1.checked )QGbx18=1; //9906272327 if(QGbx18) QGboxb18.value=''; //9906230648 var beta=readdata(arg2); //9906221604 var dbg0=''; var i,j; var tol0=parseFloat(arg3);//9906191045 if(isNaN(tol0)||tol0<1.e-8||tol0>0.01) tol0=1.e-8; //9906201707 var maxIt=parseInt(arg4); //9906191046 if(isNaN(maxIt)||maxIt<1)maxIt=10; //9906191058 var iter0=0; //9906191301 var eqStr='='; //9906191422 if((typeof QGchkbxcB1)=='object' &&(typeof QGchkbxcB2)=='object' && QGchkbxcB1.checked) eqStr='+="===0==="+'; //9906191436 add "====="; //9906211020 use "===0===" var i,j,k; var j0,j1,j2,j3,j4,j5,j6,j7,j8,j9; var i0,i1,i2,i3,i4,i5,i6,i7,i8,i9; var majstatus; var tota=alfa.length; var totb=beta.length; var perMat; var rho=(beta[j]+beta[k])/2; var tau= beta[j]-rho; var sigma=Math.abs(alfa[k]-rho); var RobinHood=identity(tota); var bet2; var bet3; var bx3=bx4=bx5=bx6=''; //9906191427 var stochMat=identity(tota); //9906191741 var stochM0; var whyErr=''; //9906191811 //9906230816 var aj,ak,bj,bk; //9906201207 move majCheck() //to out side of while() majstatus=majCheck(alfa,beta,tol0) //9906201220 //call majCheck() //return majorization status //majorization status need check //just once. Check before while loop // //call jkCheck() inside while loop //return j,k update status // //if majorization status wrong //set go01=0 , not go in to //while(iter0<maxIt) loop //9906201225 record // var go01=1; //goto while(iter0<maxIt) loop if(majstatus[0]!=0) //9906191209 eval(spanAl+'="Input has negative data."'); //Input has negative is allowed. //just give alert, still proceed. if(majstatus[1]!=0) { eval(spanAl+'="two seq. different number of elements. "+alfa.length+" not= "+beta.length'); whyErr='two seq. different number of elements ' +alfa.length+' not= '+beta.length ; //NOT goto while(iter0<maxIt) loop go01=0; //9906201226 } else if(majstatus[2]!=0) { var suma=0; var sumb=0; for(i=0;i<tota;i++)suma+=alfa[i]; for(i=0;i<totb;i++)sumb+=beta[i]; eval(spanAl+'="two seq. different total sum. "+suma+"!="+sumb'); whyErr='two seq. different total sum. '+suma+'!='+sumb; go01=0; //9906201226 } else if(majstatus[3]!=0) { eval(spanAl+'="two seq. no majorization relation. More at <a href=#atQGboxb17>box 7</a>"'); whyErr='two seq. no majorization relation. ' +'Up to ' +majstatus[3]+stndrdth(majstatus[3]) +' partial sum, found ² partial sum' +' is less than ± partial sum.' +' An error condition.' ; //break; //9906191814 go01=0; //9906201227 } else if(majstatus[4]!=0) //9906182131 { //'identical, DONE' // no need go to while loop //break; //9906191447 go01=0; //9906201227 } var jkstatus; iter0=0; if(go01==1) //9906201227 while(iter0<maxIt) { iter0++; /** comment move to top of function jkCheck() from 2010-06-21-17-55 start to 2010-06-21-18-07 stop /**/ jkstatus=jkCheck(alfa,beta,tol0); i5=mat2str(majstatus).replace(/\n/g,'\\n'); eval('bx5'+eqStr.replace('===0===','===15===')+'" iter="+iter0+'+'"\\n'+i5+'\\n"'); //9906211025 start, 9906211047 ok i5=mat2str(jkstatus).replace(/\n/g,'\\n'); eval('bx5'+eqStr.replace('===0===','')+'"'+i5+'\\n"'); //9906201214 j=jkstatus[0]; //9906201228 k=jkstatus[1]; if(j==-1)break; //9906201736 job is done if(j==k) { eval(spanAl+'="Smooth two elements have SAME index. Error."'); whyErr='Smooth two elements have SAME index. Error.'; break; //9906192122 } /** document from 9906211822 to 9906211831 moved to top of function robinMatrixf() which explain abs(beta[j]-beta[k])<=tol0 /**/ if(Math.abs(beta[j]-beta[k])<=tol0) { i2=i3=0 for(i0=j;i0<=k;i0++) { i2+=alfa[i0]; i3+=beta[i0]; } eval(spanAl+'="code ERR beta[j]==beta[k] j,k,beta[j]="+j+","+k+","+beta[j]'); whyErr="code ERR beta[j]==beta[k] j,k,beta[j]="+j+","+k+","+beta[j]; break; //9906192239 } //if(Math.abs(beta[j]-beta[k])<=tol0) if(QGbx18) QGboxb18.value+='\n===stochastic_1===\n' //9906230724 +'before RobinHood=robinMatrixf()\n' +'tota=['+tota+']\n' +'j='+j+', '+'k='+k+'\n' +'alfa[j]=['+alfa[j]+']\n' +'alfa[k]=['+alfa[k]+']\n' +'beta[j]=['+beta[j]+']\n' +'beta[k]=['+beta[k]+']\n' +'9906230728\n' //var aj,ak,bj,bk; if((typeof beta[j])=='number') {bj=beta[j]; bk=beta[k]; } else //9906230831 if((typeof beta[j])=='object') {bj=beta[j][0]; bk=beta[k][0]; } // RobinHood=robinMatrixf(tota,j,k ,bj,bk,alfa[j],alfa[k]); if(QGbx18) QGboxb18.value+='\n===stochastic_2===\n' //9906230730 +'after RobinHood=robinMatrixf()\n' +'RobinHood='+mat2str(RobinHood)+'\n' +'9906230731\n' j6=mat2str(RobinHood).replace(/\n/g,'\\n'); //9906191701 eval('bx4'+eqStr.replace('===0===','===14===')+'" iter="+iter0+'+'"\\n'+j6+'\\n"'); //9906211048 add iter if((typeof beta)=='object') //9906192027 j3=readdata(beta); else { //receive an error message. eval(spanAl+'="(typeof beta)!=object) Error."'); whyErr='(typeof beta)!=object) Error.'; break; //9906192028 } j4=''; for(i=0;i<j3.length;i++) { // j4 is used below at 9906172256 // if rename j4 must do completely. // 9906191640 record j4+=j3[i]+'\n'; } if(QGbx18) QGboxb18.value+='\n===stochastic_3===\n' //9906230650 +'before bet2=matMVecf(RobinHood,s2a(j4,..)\n' +'j4=['+j4+']\n'; //9906221747 change from [,j4] //to [,s2a(j4,'column')] bet2=matMVecf(RobinHood,s2a(j4,'column')); //9906172256 //error message 'rows as one string' //9906221638 /**9906212008 //bet2 is RobinHood averaged j4=beta if given problem is ± seq: 0.2 0.2 0.2 0.2 0.2 ² seq: 1 0 0 0 0 index: 0 1 2 3 4 after RobinHood first transformation ² seq: 1 0 0 0 0 become bet2 next ² seq: 0.8 0 0.2 0 0 following permutation reorder to ² seq: 0.8 0.2 0 0 0 9906212016 /**/ //create permute matrix //bet2 is target sequence to //be re-order. for example //bet2=[3,2,6,4,5] //'0' in permutef(mat2str(bet2),0) // indicate decending order //'1' in permutef(mat2str(bet2),1) // indicate ascending order //bet2 is 2-dimensional matrix //i9 is a string of numbers //to create permutation matrix //need a string of numbers. //9906231848 perMat=permutef(mat2str(bet2),0);//9906231840 //9906181251 before call matMVecf() //right data need change to column //that is insert '\n' // j7=readdata(bet2); j8=''; for(i=0;i<j7.length;i++) { j8+=j7[i]+'\n'; //9906181241 } //9906221621 change j8 to //2-dimensional matrix. //because matMVecf() require //both input be 2-dimensional matrix //otherwise get "Error. (typeof bet3)!" // if(QGbx18) QGboxb18.value+='\n===stochastic_4===\n' //9906230658 +'before j8=s2a(j8); //9906221622\n' +'(typeof j8)='+(typeof j8)+'\n' +'(j8)=['+(j8)+']\n'+'9906230659' //j8=s2a(j8); //9906221622 j8=s2a(j8,'column'); //9906231312 if(QGbx18) QGboxb18.value+='\n===stochastic_5===\n' //9906230652 +'before bet3=matMVecf(perMat,j8);\n' +'(typeof j8)='+(typeof j8)+'\n' +'(typeof j8[0])='+(typeof j8[0])+'\n' +'(typeof j8[0][0])='+(typeof j8[0][0])+'\n' +'(j8[0][0])='+(j8[0][0])+'\n' +'9906230653' bet3=matMVecf(perMat,j8); //9906181246 j0=mat2str([alfa,beta,bet2,bet3]).replace(/\n/g,'\\n') //9906191713 //eval('bx3'+eqStr.replace('===0===','===13===')+'"'+j0+'"'); //9906191716 OK eval('bx3'+eqStr.replace('===0===','===13===')+'" iter="+iter0+'+'"\\n'+j0+'\\n"'); //9906211050 add iter j9=mat2str(perMat).replace(/\n/g,'\\n') //9906191717 eval('bx6'+eqStr.replace('===0===','===16===')+'" iter="+iter0+'+'"\\n'+j9+'\\n"'); //9906211051 add iter if((typeof bet3)=='object') //9906192020 beta=bet3; //9906191311 else { eval(spanAl+'="Error. (typeof bet3)!=object Box4 may have infinity, 1/0"'); whyErr='Error. (typeof bet3)!=object\nBox4 may have infinity, 1/0'; break; //9906192021 } if(QGbx18) QGboxb18.value+='\n===stochastic_6===\n' //9906230656 +'before stochM0=matMVecf(perMat,RobinHood);\n' //next two line accumulate transformation //matrix toward final answer stochastic matrix stochM0=matMVecf(perMat,RobinHood); stochMat=matMVecf(stochM0,stochMat); //9906191747 } //while(iter0<maxIt) if(whyErr=='') //9906191815 { if(iter0<maxIt) eval(spanAl+'="Calculation is done. Output to box 3 to 7. Main result at <a href=#atQGboxb17>box 7</a>"'); else eval(spanAl+'="Stop, reached maximum iteration="+maxIt+" You can set maxIt."'); } //Box 5 error code eval((outERc+'=\''+bx5+'\'').replace(/\n/g,'\\n')); //9906191451 OK //Box 4 RobinHood matrix eval((outRHm+'=\''+bx4+'\'').replace(/\n/g,'\\n')); //9906191647 //Box 3 ² history eval((outSeq+'=\''+bx3+'\'').replace(/\n/g,'\\n')); //9906191715 //Box 6 permutation eval((outPRm+'=\''+bx6+'\'').replace(/\n/g,'\\n')); //9906191718 if(whyErr=='') //success return return stochMat; //9906191748 //error return return whyErr; //9906191816 } //function stochastic0() //9906191734 //9906212022 done stochastic0() document var docC='<pre>' +'<a name=docC001>&lt;a name=docC001&gt;</a><br>' +'The following is document for\n' +'function majCheck(arg1,arg2,arg3)\n\n' +'2010-06-18-18-40 start\n' +'<a name=docC002>&lt;a name=docC002&gt;</a><br>' +'input arg1,arg2 are two sequences. \n' +'arg1 is more average data.\n' +'arg2 is more diverse data.\n' +'arg3 is error tolerance.\n' +'tiny0=1.e-8\n' +'arg1 total sum and\n' +'arg2 total sum must be equal.\n' +'<a name=docC003>&lt;a name=docC003&gt;</a><br>' +'Example 1 //OK\n' +'extreme average data and\n' +'extreme diverse data are\n' +'majCheck( \'1 1 1 1 1\', \'5 0 0 0 0\')\n' +'<a name=docC004>&lt;a name=docC004&gt;</a><br>' +'Example 2 //OK\n' +'majCheck( \'5 3 3 2 2\', \'5 4 3 2 1\')\n' +'<a name=docC005>&lt;a name=docC005&gt;</a><br>' +'Example 3/4 //error\n' +'majCheck( \'4 1 1\', \'3 3 0\')\n' +'majCheck( \'3 3 0\', \'4 1 1\')\n' +'\'3 3 0\' and \'4 1 1\' do not have \n' +'majorization relation. \n' +'Both Example 3/4 are error\n' +'<a name=docC006>&lt;a name=docC006&gt;</a><br>' +'Output check code\n' +'return chkList\n' +'chkList[0]=0 arg1,arg2 two seq. no negative\n' +'chkList[1]=0 two seq. same number of elements\n' +'chkList[2]=0 two seq. same total sum.\n' +'chkList[3]=0 two seq. partial sum inequality\n' +' always arg1_p_sum <= arg2_p_sum\n' +'value zero is for OK status.\n' +'<a name=docC007>&lt;a name=docC007&gt;</a><br>' +'chkList[0]=1 arg1,arg2 two seq. has negative\n' +'chkList[1]=1 two seq. different number of elements\n' +'chkList[2]=1 two seq. different total sum\n' +'chkList[3]=2 two seq. partial sum inequality\n' +' wrong direction at 2nd element\n' +' \'2nd\' count start from 1 not from 0.\n' +'chkList[4]=\'identical, DONE\'; but overwritten \n' +' by other message.\n' +'<a name=docC008>&lt;a name=docC008&gt;</a><br>' +'Example 5 //error at 2nd element\n' +'majCheck( \'3 3 1 1 1\', \'3 2 2 1 1\')\n' +'\n' +'value &gt; zero is for error status.\n' +'2010-06-18-19-52 stop\n' +'\n' +'<a name=docC009>&lt;a name=docC009&gt;</a><br>' +'2010-06-21-21-05\n' +'to compare whether ±[i] equal ²[i] \n' +'can not use if(±[i]==²[i]), since\n' +'when ±[i]=1.00000000000000\n' +'and ²[i]=1.00000000000001\n' +'if(±[i]==²[i]) evaluate not equal.\n' +'but to us they are equal. To\n' +'avoid rounding/truncation error\n' +'use if(abs(±[i]-²[i])&lt;1.e-8)\n' +'then need tiny0=1.e-8\n' +'<a name=docC010>&lt;a name=docC010&gt;</a><br>' +'user can specify at arg3\n' +'if 0.0&lt;arg3&lt;0.01 program use\n' +'user specified tolerance\n' +'otherwise use default 1.e-8\n' +'2010-06-21-21-11\n' +'\n' +'Above is document for \n' +'function majCheck(arg1,arg2,arg3)\n' +'</pre>' function majCheck(arg1,arg2,arg3) { var i,j,k; var i0,i1,i2,i3; var N=0; //N=number of elements //that alfa_j != beta_j var tiny0=1.e-8; //this one user can not control var tolerance0=parseFloat(arg3); //9906191048 use arg3 if(isNaN(tolerance0) //if user did not define ||tolerance0<=0 //if user define tol<=0 ||tolerance0>0.01 //if user define tol>0.01 )tolerance0=tiny0; //use default. 9906191053 var chkList=new Array(5); for(i=0;i<5;i++)chkList[i]=0; if(typeof arg1=='string') alfa=readdata(arg1) else if(typeof arg1=='object' &&typeof arg1[0]=='number' ) alfa=arg1; else return [-1,-1,-1,-1,-1]; if(typeof arg2=='string') beta=readdata(arg2) else if(typeof arg2=='object' &&typeof arg2[0]=='number' ) beta=arg2; else return [-2,-2,-2,-2,-2]; var tota=alfa.length; var totb=beta.length; for(i=0;i<tota;i++) { if(alfa[i]<0) { //alfa sequence contains negative chkList[0]=1; //9906181332 break; } } //two for() loops //it could be tota!=totb if(chkList[0]==0) for(i=0;i<totb;i++) { if(beta[i]<0) { //beta sequence contains negative chkList[0]=1; //9906181338 break; } } if(tota!=totb) { //alfa sequence and beta sequence //have different number of elements. chkList[1]=1; //9906172047 } var suma=0; var sumb=0; for(i=0;i<tota;i++)suma+=alfa[i]; for(i=0;i<totb;i++)sumb+=beta[i]; if(Math.abs(suma-sumb)>tolerance0) //9906192148 { chkList[2]=1; //9906172047 } //9906172055 var psum=new Array(Math.max(tota,totb)) for(i=0;i<psum.length;i++) psum[i]=new Array(2); psum[0][0]=alfa[0]; psum[0][1]=beta[0]; if(psum[0][0]>psum[0][1]) { chkList[3]=1; //9906181914 } for(i=1;i<psum.length;i++) { psum[i][0]=psum[i-1][0]+alfa[i]; psum[i][1]=psum[i-1][1]+beta[i]; if((psum[i][0]-psum[i][1])>tolerance0 &&chkList[3]==0) //9906192358 use tolerance0 { chkList[3]=i+1; //9906172100 } //9906181922 add 'i+' to form 'i+1' } return chkList; //9906201244 } //9906181340 function majCheck(alfa,beta) //Robin Hood transformation matrix //start from identity matrix. Here //provide a tool for Robin. //9906212120 document // function identity(arg1) { //9906171519 var i,j,n; n=parseInt(arg1); var id=new Array(n); for(i=0;i<n;i++) id[i]=new Array(n); for(i=0;i<n;i++) for(j=0;j<n;j++) { id[i][j]=0; if(i==j)id[i][j]=1; } return id; //9906171522 } // function identity(arg1) var docD='<pre>' +'<a name=docD001>&lt;a name=docD001&gt;</a>\n' +'The following is document for\n' +'function matMVecf()\n' +'2010-06-28-12-09 start\n' +'function matMVecf(arg1,arg2)\n' +'matrix multiply with vector.\n' +'Later modified to\n' +'matrix multiply with matrix.\n' +'still use same name matMVecf()\n' +'<a name=docD002>&lt;a name=docD002&gt;</a>\n' +'function matMVecf(arg1,arg2) and\n' +'function matMatStr(arg1,arg2,arg3)\n' +'both multiply leftMatrix (arg1) \n' +'with rightMatrix (arg2).\n' +'matMVecf() accept 2-D array input.\n' +'matMatStr() take string input.\n' +'<a name=docD003>&lt;a name=docD003&gt;</a>\n' +'For example 2-D matrix \n' +' 11 12 13 14 15 ---eqn.docD001\n' +' 16 17 18 19 20\n' +' 21 22 23 24 25\n' +'2-D array expression is\n' +'<a name=docD004>&lt;a name=docD004&gt;</a>\n' +'var leftMat=\n' +'[[11,12,13,14,15], ---eqn.docD002\n' +' [16,17,18,19,20],\n' +' [21,22,23,24,25]];\n' +'Same matrix string expression is\n' +'\'11 12 13 14 15 16 17 18 19 20 21 22 23 24 25\' ---eqn.docD003\n' +'<a name=docD005>&lt;a name=docD005&gt;</a>\n' +'User input matrix from box.\n' +'Box.value is string only.\n' +'The command s2a(QHboxb11.value)\n' +'convert eqn.docD001 to eqn.docD002\n' +'function s2a() consider change line\n' +'be same as change row. If input box\n' +'use eqn.docD003 style data, s2a()\n' +'can not work. If user change \n' +'<a name=docD006>&lt;a name=docD006&gt;</a>\n' +'eqn.docD003 to eqn.docD004 below\n' +'\'11 12 13 14 15;16 17 18 19 20;21 22 23 24 25\' ---eqn.docD004\n' +'Although it is one line string.\n' +'The byte \';\' let s2a() know where\n' +'to cut.\n' +'<a name=docD007>&lt;a name=docD007&gt;</a>\n' +'matMVecf() receive eqn.docD002\n' +'row dimension is arg1.length\n' +'column dimension is arg1[0].length\n' +'elements numbers are arg1[i][j]\n' +'<a name=docD008>&lt;a name=docD008&gt;</a>\n' +'Above is matMVecf() \n' +'Below is matMatStr()\n' +'If 2-row, 3-column leftMatrix is\n' +'1 2 3 ---eqn.docD005\n' +'4 5 6\n' +'If 3-row, 2-column rightMatrix is\n' +' 7 8 ---eqn.docD006\n' +' 9 10\n' +'11 12\n' +'<a name=docD009>&lt;a name=docD009&gt;</a>\n' +'matMatStr() read string\n' +'arg1= \'1 2 3 4 5 6\' and\n' +'arg2= \' 7 8 9 10 11 12\'\n' +'in this case need\n' +'arg3= 3\n' +'arg3 tell matMatStr() that the\n' +'common dimension is 3. Then\n' +'<a name=docD010>&lt;a name=docD010&gt;</a>\n' +'matMatStr() be able to create\n' +'eqn.docD005 and eqn.docD006\n' +'for correct calculation.\n' +'\n' +'matMVecf() and matMatStr() are\n' +'two very different functions\n' +'doing the same work.\n' +'2010-06-28-12-30 stop\n' +'\n' +'<a name=docD011>&lt;a name=docD011&gt;</a>\n' +'2010-06-16-18-01\n' +'arg1 define matrix\n' +'arg2 define vector\n' +'2010-06-16-21-25 add non-square property\n' +'If matrix is\n' +'[1 2 3 4] ---eqn.docD007\n' +'[5 6 7 8]\n' +'[9 10 11 12]\n' +'input as array\n' +'otherwise not compatible with program.\n' +'<a name=docD012>&lt;a name=docD012&gt;</a>\n' +'&lt;textarea&gt; input box release string\n' +'only. To convert from one string\n' +'\'1 2 3 4 5 6 7 8 9 10 11 12\'\n' +'to array, use \n' +'function s2a(arg1,arg2,arg3)\n' +'input string, output array.\n' +'<a name=docD013>&lt;a name=docD013&gt;</a>\n' +'if arg2 (vector) has n elements\n' +'arg1 (matrix) must have m*n elements\n' +'\n' +'function matMVecf() need \n' +'function readdata()\n' +'2010-06-16-21-16\n' +'\n' +'</pre>' function matMVecf(arg1,arg2) { //9906171052 start matMVecf_ver1() //9906221511 start matMVecf() //Matrix Multiply with Vector //input as Array. (not as String) //In between function calls, it //is better use Array // //Read input from <textarea> box //it is string only. //9906161902 start matMVecStr() //9906162151 done matMVecStr() // var QGbx18=0; //9906271909 if(typeof QGboxb18=='object' &&typeof QGchkbxcC1=='object' &&QGchkbxcC1.checked )QGbx18=1; //9906272327 if(QGbx18) QGboxb18.value+='\n===matMVecf_1===\n' //9906230713 +'enter function matMVecf(arg1,arg2)\n' +'arg1=['+arg1+']\n' +'arg1.length=['+arg1.length+']\n' +'arg1[0].length=['+arg1[0].length+']\n' +'arg1[0][0]=['+arg1[0][0]+']\n' +'arg2=['+arg2+']\n' +'arg2.length=['+arg2.length+']\n' +'arg2[0].length=['+arg2[0].length+']\n' +'arg2[0][0]=['+arg2[0][0]+']\n' +'9906230715\n' if((typeof arg1)!='object' ||(typeof arg1[0])!='object' ||(typeof arg1[0][0])!='number' ||(typeof arg2)!='object' ||(typeof arg2[0])!='object' ||(typeof arg2[0][0])!='number' ) { //9906221512 alert("RETURN from matMVecf()\n" +"matMVecf() need both\n" +"input be 2-dim array\n" +" typeof arg1="+(typeof arg1)+" !='object'\n" +"||typeof arg1[0]="+(typeof arg1[0])+" !='object'\n" +"||typeof arg1[0][0]="+(typeof arg1[0][0])+" !='number'\n" +"||typeof arg2="+(typeof arg2)+" !='object'\n" +"||typeof arg2[0]="+(typeof arg2[0])+" !='object'\n" +"||typeof arg2[0][0]="+(typeof arg2[0][0])+" !='number'\n" +"9906222222"); return 'type mis-match. 9906222217' } var inMat1=arg1; var inMat2=arg2; var rowDim1, colDim1; //9906162256 var rowDim2, colDim2; //9906162257 rowDim1=inMat1.length; colDim1=inMat1[0].length; rowDim2=inMat2.length; colDim2=inMat2[0].length; if(colDim1!=rowDim2) { return 'function matMVecf() error\n' +'Mat1,Mat2 dimension error.\n' +'Matrix1 columns MUST equal\n' +'Matrix2 rows. But now:\n' +'rowDim1='+rowDim1+'\n' +'colDim1='+colDim1+'\n' +'rowDim2='+rowDim2+'\n' +'colDim2='+colDim2+'\n' +'Above four numbers,\n' +'middle two not equal\n' +'It is an error.\n' +'9906171216\n' } var i,j,k; var sum2=0; var ans2=[]; /** //9906172338 get correct answer alpha seq.= 5 3 3 2 2 beta seq. = 5 4 3 2 1 transformation matrix T = [[ 1 0 0 0 0 0 0.6666666666666666 0 0 0.3333333333333333 0 0 1 0 0 0 0 0 1 0 0 0.3333333333333333 0 0 0.6666666666666666 ]] T*beta get alpha 5 3 3 2 2 9906172338 get correct answer 9906172343 try alpha seq.= 3 3 3 3 3 beta seq. = 5 4 3 2 1 output is 3 4 3 2 3 need permutation metrix to sort beta seq. before next iteration /**/ ans2=new Array(rowDim1); //9906171232 for(i=0;i<rowDim1;i++) { ans2[i]=new Array(colDim2); //9906171233 } for(i=0;i<rowDim1;i++) for(k=0;k<colDim2;k++) { ans2[i][k]=0; //9906171235 } //Next three loops do leftMatrix //and rightMatrix multiplication for(i=0;i<rowDim1;i++) { for(k=0;k<colDim2;k++) { sum2=0; //9906171223 for(j=0;j<rowDim2;j++) { sum2+=inMat1[i][j]*inMat2[j][k] } //ans2[i][k]=sum2; ans2[i][k]=bye09(sum2); //9906231612 use bye09() //bye09() change 1.99999999999 to 2 //bye09() change 1.00000000002 to 1 } } if(QGbx18) QGboxb18.value+='\n===matMVecf_2===\n' //9906230716 +'return ans2; function matMVecf()\n' +'ans2=['+mat2str(ans2)+']\n' +'ans2.length=['+ans2.length+']\n' +'ans2[0].length=['+ans2[0].length+']\n' +'ans2[0][0]=['+ans2[0][0]+']\n' +'9906230718\n' return ans2; } //9906171350 done function matMVecf_ver1(arg1,arg2) //9906231612 done function matMVecf(arg1,arg2) var docE='<pre>' +'<a name=docE001>&lt;a name=docE001&gt;</a>\n' +'The following is document for\n' +'function mat2str()\n' +'2010-06-28-18-24\n' +'if there is a 2-Dim array defined\n' +'var leftMat= //9906281819\n' +'[[11,12,13,14,15],\n' +' [16,17,18,19,20],\n' +' [21,22,23,24,25]];\n' +'<a name=docE002>&lt;a name=docE002&gt;</a>\n' +'to print leftMat directly\n' +'document.write(leftMat)\n' +'get\n' +'11,12,13,14,15,16,17,18,19,20,21,22,23,24,25\n' +'The 2-dim array structure disappear.\n' +'<a name=docE003>&lt;a name=docE003&gt;</a>\n' +'function mat2str(arg1) read \n' +'[[11,12,13,14,15],\n' +' [16,17,18,19,20],\n' +' [21,22,23,24,25]];\n' +'insert a newline when change row.\n' +'that is change "]," to newline\n' +'also insert " " between numbers\n' +'<a name=docE004>&lt;a name=docE004&gt;</a>\n' +'output to\n' +' 11 12 13 14 15\n' +' 16 17 18 19 20\n' +' 21 22 23 24 25\n' +'which is much better than\n' +'11,12,13,14,15,16,17,18,19,20,21,22,23,24,25\n' +'<a name=docE005>&lt;a name=docE005&gt;</a>\n' +'function mat2str(arg1) main purpose \n' +'is to display 2-dim array in right\n' +'form.\n' +'2010-06-28-18-29\n' +'\n' +'</pre>' //matrix data to string data //key point is to add newline //when change row. // //9906171542 write as in-button code. //9906181038 change to function function mat2str(arg1) { var i,j,o=''; if(typeof arg1=='string') return arg1; //input is a string, no change if(typeof arg1=='object' &&typeof arg1[0]=='number' ) //input is an array of data { //merge data to one string for(i=0;i<arg1.length;i++) o+=bye09(arg1[i])+' '; return o; } //9906202301 add bye09() //input is a two dimension matrix of data for(i=0;i<arg1.length;i++) { for(j=0;j<arg1[i].length;j++) { o+=bye09(arg1[i][j])+' ';//merge one row data } o+='\n'; //change row, change line } return o; } //9906181048 done function mat2str(arg1) var docF='<pre>' +'<a name=docF001>&lt;a name=docF001&gt;</a>\n' +'The following is document for\n' +'function s2a()\n' +'2010-06-28-19-02\n' +'function s2a(arg1,arg2) read string\n' +'data and change to array data.\n' +'function s2a(arg1,arg2) do just \n' +'opposite to function mat2str(arg1) \n' +'<a name=docF002>&lt;a name=docF002&gt;</a>\n' +'matMVecf() accept 2-D array input.\n' +'matMatStr() take string input. To \n' +'use function matMVecf() the command\n' +'matMVecf(boxData.value) cause error\n' +'Because input matrix from &lt;textarea&gt;\n' +'box it is string data only. but\n' +'matMVecf() require array input.\n' +'<a name=docF003>&lt;a name=docF003&gt;</a>\n' +'Assume &lt;textarea&gt; has input \n' +' 11 12 13 14 15 ---eqn.docF001\n' +' 16 17 18 19 20\n' +' 21 22 23 24 25\n' +'<a name=docF004>&lt;a name=docF004&gt;</a>\n' +'function s2a(arg1,arg2) change\n' +'data to\n' +'[[11,12,13,14,15], ---eqn.docF002\n' +' [16,17,18,19,20],\n' +' [21,22,23,24,25]];\n' +'<a name=docF005>&lt;a name=docF005&gt;</a>\n' +'matMVecf(boxData.value) is error\n' +'matMVecf(s2a(boxData.value)) is OK\n' +'2010-06-28-19-12\n' +'\n' +'</pre>' //9906171100 //String to Array. //Matrix Multiply with Vector or //Matrix Multiply with Matrix //input as Array. (not as String) //In between function calls, it //is better use Array. BUT //Read input from <textarea> box //it is string only. //function s2a(arg1) convert from //string to Array. //9906171101 // //9906181137 //arg1 is data in string form //need to convert to array form //arg2 is either 'left' or 'right' //if array is 1-D (not 2-D) and //if arg2 = 'left' //function output row vector. // //if arg2 = 'right' //function output column vector. // function s2a(arg1,arg2) { //9906221038 start s2a() version 2 var assignCol=0; //9906221749 var assignRow=0; if(arg2=='left'||arg2=='l' ||arg2=='LEFT'||arg2=='L' ||arg2=='row' ||arg2=='r' ||arg2=='ROW' ||arg2=='R' ) { assignRow=1; //9906221750 } if(arg2=='right'||arg2=='r' ||arg2=='RIGHT'||arg2=='R' ||arg2=='column' ||arg2=='col' ||arg2=='c' ||arg2=='COLUMN' ||arg2=='COL' ||arg2=='C' ) { assignCol=1; //9906221751 } if(typeof arg1!='string' &&assignCol==0 &&assignRow==0 //9906221752 two '==0' ) return arg1 var QGbx18=0; //9906271915 if(typeof QGboxb18=='object' &&typeof QGchkbxcC1=='object' &&QGchkbxcC1.checked )QGbx18=1; //9906272327 if(QGbx18) QGboxb18.value+='\n===s2a_1===\n' //9906230701 +'enter function s2a(arg1,arg2)\n' +'arg1=['+(arg1)+']\n' +'arg2=['+(arg2)+']\n' +'typeof arg1='+(typeof arg1)+'\n' +'9906230704\n' var QHbx14=0; //9906281259 if(typeof QHboxb14=='object' )QHbx14=1; var i,j,k; var w0,w1,w2,w3,w4,w5,w6; var inp1=''; var oup1=[]; //9906221041 w0=arg1.length-1; if(w0<=0) return []; //9906281113 while((arg1.charAt(w0)<'0' || arg1.charAt(w0)>'9' ) &&w0>=0 //9906281112 avoid infinity loop ) w0--; inp1=arg1.substring(0,w0+1); //9906171137 add +1 var semicolon; //9906220651 var newline; semicolon=inp1.indexOf(';') newline=inp1.indexOf('\n'); if(QHbx14) { var dbg0='s2a() debug\n'; dbg0+='' +'assignRow='+assignRow+'\n' +'assignCol='+assignCol+'\n' +'semicolon index='+semicolon+'\n' +'newline index='+newline+'\n' +'Above are index, not count.\n' } //row vector if(assignRow==1 //9906221754 ||(assignCol==0&&semicolon<0&&newline<0) ) { oup1[0]=readdata(inp1); if(QHbx14) { dbg0+='oup1.length='+oup1.length+'\n' +'oup1[0].length='+oup1[0].length+'\n' for(i=0;i<oup1.length;i++) { for(j=0;j<oup1[i].length;j++) { dbg0+='i='+i+', j='+j+', a[i][j]=' +(oup1[i][j]+0)+'\n'; //'+0' make sure it is number. //'+0' make sure it is not a string } dbg0+='9906221756 force row vector \n\n' } QHboxb14.value=dbg0 } if(QGbx18) QGboxb18.value+='\n===s2a_2===\n' //9906230706 +'before return from s2a() ; row vector\n' +'assignCol='+assignCol+'\n' +'assignRow='+assignRow+'\n' +'oup1=['+oup1+']\n' +'oup1.length=['+oup1.length+']\n' +'oup1[0].length=['+oup1[0].length+']\n' +'oup1[0][0]=['+oup1[0][0]+']\n' return oup1; } //row vector if(assignRow==1 ... //column vector and m*n matrix //use same code. 9906221742 //9906221839 //if column vector newline=-1 if(assignCol==1&&newline==-1) { w1=readdata(inp1) w2=(w1+'').replace(/,/g,';'); //9906221842 w3=w2.replace(/;/g,'\n'); if(QHbx14) dbg0+='w2=['+w2+']\nhope w2 be [;] separated\n' +'w3=['+w3+']\n' } else w3=inp1 //oup1[0]=readdata(inp1); w4=w3.split('\n'); //9906221057 if(QHbx14) dbg0+='after w4=w3.split(nl)\n' +'w4 become string number\n' //99061920 +'w4=['+w4+']\n' +'w4.length=['+w4.length+']\n' +'w4[0]=['+w4[0]+']\n' +'w4[0].length=['+w4[0].length+']\n' for(i=0;i<w4.length;i++) { w5=readdata(w4[i]) if(w5.length==1) { oup1[i]=new Array(1); //9906221835 oup1[i][0]=w5[0]; //9906221921 if(QHbx14) dbg0+='i='+i+', oup1[i][0]=' +oup1[i][0]+'\n' //9906221853 } else oup1[i]=w5; } //for(i=0;i<w4.length;i++) if(QHbx14) { dbg0+='oup1.length='+oup1.length+'\n' +'oup1[0].length='+oup1[0].length+'\n' +'oup1[0][0]='+oup1[0][0]+'\n' +'oup1[i-1][0]='+oup1[i-1][0]+'\n' for(i=0;i<oup1.length;i++) { for(j=0;j<oup1[i].length;j++) { dbg0+='i='+i+', j='+j+', a[i][j]=' +(oup1[i][j]+0)+'\n'; //'+0' make sure it is number. //'+0' make sure it is not a string } dbg0+='9906221045 force column vector \n\n' } dbg0+='oup1.length='+oup1.length+'\n' +'oup1[0].length='+oup1[0].length+'\n' +'oup1[0][0]='+oup1[0][0]+'\n' QHboxb14.value=dbg0 } //if(QHbx14) if(QGbx18) QGboxb18.value+='\n===s2a_3===\n' //9906230710 +'before return from s2a() ; End of function\n' +'assignCol='+assignCol+'\n' +'assignRow='+assignRow+'\n' +'oup1=['+oup1+']\n' +'oup1.length=['+oup1.length+']\n' +'oup1[0].length=['+oup1[0].length+']\n' +'oup1[0][0]=['+oup1[0][0]+']\n' +'9906230710\n' return oup1; //9906221102 OK } //function s2a(arg1,arg2) /**9906162235 matMatStr(QGboxb11.value,QGboxb12.value, commonDim(QGboxb11.value,QGboxb12.value)) /**/ function matMatStr(arg1,arg2,arg3) { //matMatStr() //Matrix multiply with Matrix //input as String. (not as Array) //Read input from <textarea> box //it is string only. var inMat1; var inMat2; var rowDim1, colDim1; //9906162256 var rowDim2, colDim2; //9906162257 inMat1=readdata(arg1); inMat2=readdata(arg2); colDim1=parseInt(arg3); rowDim1=inMat1.length/colDim1; if(parseInt(rowDim1)!=rowDim1) { return 'matMatStr() error01\n' +'Matrix1,Mat2 dimension error\n' +'Matrix1 elements number='+inMat1.length+'\n' +'Matrix2 elements number='+inMat2.length+'\n' +'Matrix1 column='+colDim1+'\n' +'Matrix1 row='+inMat1.length+'/'+colDim1 +'='+rowDim1+'\n' +'Matrix1 row must be integer.\n' } //9906162302 rowDim2=colDim1; colDim2=inMat2.length/rowDim2; if(parseInt(colDim2)!=colDim2) { return 'matMatStr() error02\n' +'Mat1,Matrix2 dimension error\n' +'Matrix1 elements number='+inMat1.length+'\n' +'Matrix2 elements number='+inMat2.length+'\n' +'Matrix2 row='+rowDim2+'\n' +'Matrix2 column='+inMat2.length+'/'+rowDim2 +'='+colDim2+'\n' +'Matrix2 column must be integer.\n' } //9906162311 var ans1=[]; //9906162312 var i,j,k; var mat1Str=(inMat1+''); var mat2Str=''; var colBgn=0; var QHbx14=0; //9906281437 if(typeof QHboxb14=='object' )QHbx14=1; if(QHbx14) var dbg2=''; for(i=0;i<colDim2;i++) { mat2Str=inMat2[colBgn]; //9906162335 for(j=1;j<rowDim2;j++) { mat2Str+='' +' '+inMat2[colBgn+j*colDim2] //9906162336 '*colDim2' } ans1[i]=matMVecStr(mat1Str,mat2Str); colBgn++; //9906162340 if(QHbx14) dbg2+=mat2Str+'\n'; //9906162329 } if(QHbx14) dbg2+='\n===4===\n' ans2=''; for(j=0;j<ans1[0].length;j++) { for(i=0;i<ans1.length;i++) { ans2+=ans1[i][j]+' '; if(QHbx14) dbg2+=i+', '+j+', '+ans1[i][j]+'\n' } ans2+='\n'; } if(QHbx14) QHboxb14.value=dbg2; //9906162330 return ans2; // function matMatStr(arg1,arg2,arg3) } //9906162348 done matMatStr() function commonDim(arg1,arg2) { var w0,w1,w2,w3; var inp1=''; var inp2=''; w0=arg1.length-1; if(w0<=0) return -1; //9906281441 while(arg1.charAt(w0)<'0' || arg1.charAt(w0)>'9' &&w0>=0 //9906281442 avoid infinity loop ) w0--; inp1=arg1.substring(0,w0+1); //9906171137 add +1 w0=arg2.length-1; if(w0<=0) return -2; //9906281443 while(arg2.charAt(w0)<'0' && arg2.charAt(w0)>'9' &&w0>=0 //9906281444 avoid infinity loop ) w0--; inp2=arg2.substring(0,w0); inp1=inp1.split('\n'); inp2=inp2.split('\n'); var aa=readdata(inp1[0]); if(aa.length==inp2.length) return aa.length; else return -3; } // 9906162252 /** 99,06,16,18,01 arg1 define matrix arg2 define vector 99,06,16,21,25 add non-square property If matrix is [1 2 3 4] [5 6 7 8] [9 10 11 12] input as one string '1 2 3 4 5 6 7 8 9 10 11 12' otherwise not compatible with program. If column vector is [12] [13] [14] [15] define column vector as string '12 13 14 15' if arg2 (vector) has n elements arg1 (matrix) must have m*n elements function matMVecStr() need function readdata() 99,06,16,21,16 /**/ function matMVecStr(arg1,arg2) { //9906161902 //matMVecStr() //Matrix Multiply with Vector //input as String. (not as Array) //Read input from <textarea> box //it is string only. // if((typeof arg1)!='string') { //9906162107 return 'Please input matrix rows as one string.' } var i,j,k; var inMat; var inVec; var rowDim, colDim; //9906162126 inMat=readdata(arg1); inVec=readdata(arg2); colDim=inVec.length; //9906162128 rowDim=inMat.length/inVec.length; if(parseInt(rowDim)!=rowDim) { return 'Matrix,vector dimension error\n' +'Matrix elements number='+inMat.length+'\n' +'Vector elements number='+inVec.length+'\n' +'Matrix column='+colDim+'\n' +'Matrix row='+inMat.length+'/'+inVec.length +'='+rowDim+'\n' +'Matrix row must be integer.\n' } //9906162135 var dim0=inVec.length; var ans0=[]; //9906162001 var rowBgn=0 //9906162004 for(i=0;i<rowDim;i++) { ans0[i]=0; for(j=0;j<colDim;j++) { ans0[i]+=inMat[rowBgn+j]*inVec[j]; } rowBgn+=colDim; } return ans0; //function matMVecStr(arg1,arg2) } //9906162151 done m*n matrix multiply n vector //9906272038 jkstatus //docG is a document in // function stochastic0() //before the command //jkstatus=jkCheck(alfa,beta,tol0); //now move docG to //function jkCheck() // var docG='<pre>' +'<a name=docG001>&lt;a name=docG001&gt;</a>\n' +'The following is document for\n' +'function jkCheck()\n' +'2010-06-21-17-55 start\n' +'function jkCheck() document\n' +'If given problem is\n' +'± seq: 4 3 3 3 2 \n' +'² seq: 6 4 3 1 1 \n' +'index: 0 1 2 3 4\n' +'<a name=docG002>&lt;a name=docG002&gt;</a>\n' +'<a href=#textbook>CSMC</a> use index j for j=0 to j=2\n' +' and use index k for k=2 to k=4\n' +'Require jÿk,²[j]ÿ±[j],±[k]ÿ²[k]\n' +' ²[j]`"²[k]; ²[j]+²[k]=±[j]+±[k]\n' +'j and k are important. update j,k\n' +'at each while loop. If j,k update\n' +'wrong, output negative matrix. ERR\n' +'<a name=docG003>&lt;a name=docG003&gt;</a>\n' +'initial j,k are 1,3 index of ends\n' +'of next middle shorter sections\n' +'for ± [3 3 3] and \n' +'for ² [4 3 1] \n' +'ignore middle like \'3\' After\n' +'first RobinHood transformation\n' +'± seq: 4 3 3 3 2 \n' +'² old: 6 4 3 1 1 \n' +'² new: 6 3 3 2 1 \n' +'<a name=docG004>&lt;a name=docG004&gt;</a>\n' +'second j,k are 0,3 index of ends\n' +'for ± [4 3 3 3] and \n' +'for ² [6 3 3 2] \n' +'ignore middle like \'3 3\'\n' +'after second iteration\n' +'± seq: 4 3 3 3 2 \n' +'² old: 6 3 3 2 1 \n' +'² new: 5 3 3 3 1 \n' +'<a name=docG005>&lt;a name=docG005&gt;</a>\n' +'third j,k are 0,4 index of ends\n' +'for ± [4 3 3 3 2] and \n' +'for ² [5 3 3 3 1] \n' +'ignore middle like \'3 3 3\'\n' +'after third iteration\n' +'± seq: 4 3 3 3 2 \n' +'² old: 5 3 3 3 1 \n' +'² new: 4 3 3 3 2 \n' +'now bring ² seq to ± seq. we are done.\n' +'<a name=docG006>&lt;a name=docG006&gt;</a>\n' +'This process, update j,k are important\n' +'Program update jkstatus at the\n' +'begin of each while loop in \n' +'function stochastic0()\n' +'2010-06-21-18-07 stop\n' +'\n\n' +'<a name=docG007>&lt;a name=docG007&gt;</a>\n' +'2010-06-30-07-16 start\n' +'function jkCheck() use \n' +'jl (j left bound)\n' +'jr (j right bound)\n' +'ml (m left bound)\n' +'mr (m right bound)\n' +'kl (k left bound)\n' +'kr (k right bound)\n' +'to determine j,k value.\n' +'<a name=docG008>&lt;a name=docG008&gt;</a>\n' +'jl is first ²[jl] &gt; ±[jl] \n' +'jr is last ²[jr] &gt; ±[jr] \n' +'kl is first ²[kl] &lt; ±[kl] \n' +'kr is last ²[kr] &lt; ±[kr] \n' +'jl&lt;=j&lt;=jr\n' +'kl&lt;=k&lt;=kr\n' +'ml is middle equality left bound\n' +'ml is middle equality right bound\n' +'<a name=docG009>&lt;a name=docG009&gt;</a>\n' +'Example 1\n' +'±1 sequence 3.5 2.5 2.5 2.4 1.6 1.5 1.5 0.5 \n' +'²1 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 \n' +'index 0 1 2 3 4 5 6 7\n' +'Left equality ±1[0]=²1[0]=3.5 are ignored.\n' +'Right equality ±1[7]=²1[7]=0.5 are ignored.\n' +'<a name=docG010>&lt;a name=docG010&gt;</a>\n' +'For ±1,²1 find\n' +'jl=1 first ²1[1] &gt; ±1[1] 3.0&gt;2.5\n' +'jr=3 last ²1[3] &gt; ±1[3] 2.5&gt;2.4\n' +'kl=4 first ²1[4] &lt; ±1[4] 1.5&lt;1.6\n' +'kr=6 last ²1[6] &lt; ±1[6] 1.0&lt;1.5\n' +'ml=-1 no middle equality\n' +'mr=-1\n' +'<a name=docG011>&lt;a name=docG011&gt;</a>\n' +'2010-06-30-07-30 here\n' +'±2 sequence 3.5 2.5 2.5 2.4 1.6 1.5 1.5 0.5\n' +'²2 sequence 3.5 3 2.8 2.4 1.6 1.2 1 0.5\n' +'index 0 1 2 3 4 5 6 7\n' +'<a name=docG012>&lt;a name=docG012&gt;</a>\n' +'For ±2,²2 find\n' +'jl=1 first ²2[1] &gt; ±2[1] 3.0&gt;2.5\n' +'jr=2 last ²2[2] &gt; ±2[2] 2.8&gt;2.5\n' +'kl=5 first ²2[5] &lt; ±2[5] 1.2&lt;1.5\n' +'kr=6 last ²2[6] &lt; ±2[6] 1.0&lt;1.5\n' +'ml=3 first ²2[3] = ±2[3] 2.4=2.4\n' +'mr=4 last ²2[4] = ±2[4] 1.6=1.6\n' +'<a name=docG013>&lt;a name=docG013&gt;</a>\n' +'With j,k,m bounds in hand,\n' +'Next goto j,k determination\n' +'section\n' +'//Set j=k=-1; as initial state\n' +'j=k=-1;\n' +'//if middle_left ml defined (greater than -1)\n' +'//if middle_rite mr defined (greater than -1)\n' +'if(ml&gt;-1&&mr&gt;-1)\n' +' {\n' +'<a name=docG014>&lt;a name=docG014&gt;</a>\n' +'//set index j to be ml-1. For above\n' +'//±2 ²2 example, ml=3, then j=2=3-1\n' +'j=ml-1;\n' +'//if ²[j]-±[j]&lt;1.e-8, they are equal\n' +'//if j is greater than zero\n' +'//move index j toward zero\n' +'<a name=docG015>&lt;a name=docG015&gt;</a>\n' +'while(Math.abs(beta[j]-alfa[j])&lt;=tolerance0\n' +' && j&gt;0\n' +' )\n' +'j--; //9906201722\n' +'\n' +'<a name=docG016>&lt;a name=docG016&gt;</a>\n' +'//set index k to be mr+1. For above\n' +'//±2 ²2 example, mr=4, then j=5=4+1\n' +'k=mr+1\n' +'//if ±[j]-²[j]&lt;1.e-8, they are equal\n' +'//if k is less than 8 (total 8 elements)\n' +'//move index k toward 8\n' +'<a name=docG017>&lt;a name=docG017&gt;</a>\n' +'while(Math.abs(beta[k]-alfa[k])&lt;=tolerance0\n' +' && k&lt;tota-1\n' +' )\n' +'k++; //9906201726\n' +' }\n' +'<a name=docG018>&lt;a name=docG018&gt;</a>\n' +'//if middle_left ml is not defined\n' +'//if middle_rite mr is not defined\n' +'else\n' +' {\n' +'//set j to j right bound\n' +'j=jr;\n' +'//set k to k left bound\n' +'k=kl; //9906201628\n' +'//±1 ²1 example, jr=3, then j=3\n' +'//±1 ²1 example, kl=4, then k=4\n' +' }\n' +'2010-06-30-08-02 here\n' +'<a name=docG019>&lt;a name=docG019&gt;</a>\n' +'2010-06-30-08-42 start\n' +'On 2010-06-23 re-write code for\n' +'jl,jr;kl,kr;ml,mr determination\n' +'from 9906232224 (please read)\n' +' to 9906232254 (source code)\n' +'<a name=docG020>&lt;a name=docG020&gt;</a>\n' +'It is in logic order, then \n' +' if(N&gt;0) //9906202342\n' +' {...}\n' +'is not necessary and deleted.\n' +'2010-06-30-08-46 stop\n' +'\n' +'<a name=docG021>&lt;a name=docG021&gt;</a>\n' +'2010-06-30-09-36 start\n' +'Example button 52 is\n' +'±52 sequence 5 3 2 1 1\n' +'²52 sequence 6 2 2 2 0\n' +'index 0 1 2 3 4\n' +'Comment is\n' +'[[\n' +'RobinHood refused \'average\' \n' +'[2 2 2] to [3 2 1], now OK\n' +']]\n' +'<a name=docG022>&lt;a name=docG022&gt;</a>\n' +'Early version had trouble here\n' +'\'average\' [2 2 2] to [3 2 1]\n' +'is an infinite loop. Stop at\n' +'iteration upper bound.\n' +'How new code persuade RobinHood\n' +'continue to work ?\n' +'±52 and ²52 has ±52[2]=²52[2]=2\n' +'middle section has one common\n' +'value, why ml=mr=-1=undefined?\n' +'<a name=docG023>&lt;a name=docG023&gt;</a>\n' +'The reason is the following.\n' +'ml and mr are defined only if\n' +'±[middle]=²[middle] or\n' +'abs(±[middle]-²[middle])&lt;1.e-8\n' +'AND kl,kr are undefined.\n' +'<a name=docG024>&lt;a name=docG024&gt;</a>\n' +'Now before ±[middle]=²[middle]=2\n' +'there is ±[1]=3&gt;2=²[1]\n' +'When ±[1]&gt;²[1], kl is defined.\n' +'Next come to i=2, although\n' +'±[2]=²[2]=2, but kl is defined\n' +'then ml=mr=-1=undefined.\n' +'All the way to the end i=4\n' +'defined jl=jr=0, kl=1, kr=4\n' +'<a name=docG025>&lt;a name=docG025&gt;</a>\n' +'These definition, let jkCheck()\n' +'define j=0=jr and k=1=kl, \n' +'invite RobinHood to average\n' +'index=j,k that is index=0,1\n' +'±52 sequence 5 3\n' +'²52 sequence 6 2\n' +'index 0 1\n' +'in\n' +'<a name=docG026>&lt;a name=docG026&gt;</a>\n' +'±52 sequence 5 3 2 1 1\n' +'²52 sequence 6 2 2 2 0\n' +'RobinHood happly do the job.\n' +'²52 new-seq. 5 3 2 2 0 \n' +'index 0 1 2 3 4\n' +'<a name=docG027>&lt;a name=docG027&gt;</a>\n' +'Better logic code\n' +'from 9906232224 (please read)\n' +' to 9906232254 (source code)\n' +'solved old code problem.\n' +'2010-06-30-10-02 stop\n' +'\n' +'Above is document for\n' +'function jkCheck()\n' +'</pre>' /** 9906212038 initially function majCheck(arg1,arg2,arg3) is in while loop. check j,k value, check majorization condition. But majorization condition need check just once. Later move code of check majorization condition to out side of while loop. Next time stamp 9906181840 start 9906181952 stop are common in function majCheck(arg1,arg2,arg3) and function jkCheck(arg1,arg2,arg3) because they were one function. 9906212048 //each iteration bring beta close alfa //each iteration need update information. //9906181323 9906181840 start Return second array is [j,k,N]] j,k indicate switch j-th and k-th element of arg2 data string to make diverse arg2 data become average arg1 data. N indicate there are total N elements in each sequence different from the other sequence Example 6 //ok majCheck( '3 2 2 1 1', '3 3 1 1 1') has [j,k,N] = 1 2 2 '1 2' say in second data '3 3 1 1 1' switch 1st element (right side '3') and 2nd element ( left side '1') (count start from zero, zero-th element is left '3') N=2 say two input sequences has total 2 elements miss match. 9906181952 stop 99,06,20,05,20 ± sequence 3.5 2.5 2.5 2.4 1.6 1.5 1.5 0.5 ² sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 index 0 1 2 3 4 5 6 7 99,06,20,06,02 need find jl = jLeft = 1 jr = jRight = 3 kl = kLeft = 4 kr = kRight = 6 jl is first ²[jl] > ±[jl] jr is last ²[jr] > ±[jr] kl is first ²[kl] < ±[kl] kr is last ²[kr] < ±[kr] 99,06,20,06,06 jl<=j<=jr kl<=k<=kr ²[j]+²[k] = ±[j]+±[k] 99,06,20,06,08 //9906201255 start function jkCheck() 99,06,23,14,02 function jkcheck() still can be improved because if ±[j]+±[k] NOT equal ²[j]+²[k] program still work, then I can choose a no negative result j,k set as answer. 99,06,23,14,06 [=][][] /**/ function jkCheck( arg1, //alpha sequence, more average arg2, //beta sequence, more diverse arg3 //tolerance, suggest 1.e-8 ) { var i; //i is index of for() loop var i0,i1,i2,i3; var tiny0=1.e-8; //this one user can not control var tolerance0=parseFloat(arg3); //9906191048 use arg3 if(isNaN(tolerance0) ||tolerance0<0 ||tolerance0>0.01 )tolerance0=tiny0; //9906191053 var QHbx13=0; //9906301237 if(typeof QHboxb13=='object' )QHbx13=1; var QHbx15=0; //9906301238 if(typeof QHboxb15=='object' )QHbx15=1; var dbg0=''; //9906192004 var dbg990623=''; //9906232105 if(QHbx13) dbg0+='9906192011\n' +'arg1=['+arg1+']\n' +'arg2=['+arg2+']\n' +'arg3=['+arg3+']\n===5===\n' //9906201258 use, 9906201221 del jkList[i] //because return [j,k,N] is better. //var jk3=3; //9906201257 //var jkList=new Array(jk3); //for(i=0;i<jk3;i++)jkList[i]=0; if(typeof arg1=='string') alfa=readdata(arg1) else { if(typeof arg1[0]=='number') alfa=arg1; else { alfa=new Array(arg1.length) for(i=0;i<alfa.length;i++) alfa[i]=arg1[0][i]; //9906222140 } } if(typeof arg2=='string') beta=readdata(arg2) else { if(typeof arg2[0]=='number') beta=arg2; else { beta=new Array(arg2.length) for(i=0;i<beta.length;i++) beta[i]=arg2[i][0]; //9906222142 } } if(QHbx13) dbg0+='9906192014\n' +'alfa=['+alfa+']\n' +'beta=['+beta+']\n' +'===6===\n' var tota=alfa.length; var totb=beta.length; if(tota!=totb) //return [-2,-2,-2]; //9906201327 return [-2,tota,totb]; //9906232032 var suma=0; var sumb=0; for(i=0;i<tota;i++)suma+=alfa[i]; for(i=0;i<totb;i++)sumb+=beta[i]; if(Math.abs(suma-sumb)>tolerance0) //return [-3,-3,-3]; //9906201328 return [-3,suma,sumb]; //9906232033 var aMinusb=0; //9906191945 var N=0; //N=number of elements //that alfa_j != beta_j var jl=jr=kl=kr=-1; //9906201258 var j=k=-1; //j,k is main number in jkCheck() var ml=mr=-1; //9906201505 //ml=Middle Left //mr=Middle Right //middle mean the middle //equal alfa_j == beta_j //section. for example //±2 sequence 3.5 2.5 2.5 2.5 1.5 1.5 1.5 0.5 //²2 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 //index 0 1 2 3 4 5 6 7 // ml=3, mr=4 //and //±1 sequence 3.5 2.5 2.5 2.4 1.6 1.5 1.5 0.5 //²1 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 //index 0 1 2 3 4 5 6 7 // ml=-1, mr=-1 // //next input // alfa=[5,3,2,1,1] // beta=[6,2,2,2,0] //need ml,mr to pull jr=0 to jr=1 //9906201509 for(i=0;i<tota;i++) { aMinusb=alfa[i]-beta[i]; //if(aMinusb>=0)j=i-1; //9906191946 //if use >=0, j run to the end of array aMinusb=Math.abs(aMinusb); //9906201401 //all comparison use abs() //if ±[0]=1.0 and ²[0]=1.000000000001 //they are EQUAL ! only if use abs() //can skip above ill condition. if(QHbx13) dbg0+='===7=== 9906192008\n' +'i='+i+' , begin for loop\n' +'alfa[i]='+alfa[i]+'\n' +'beta[i]='+beta[i]+'\n' +'aMinusb='+aMinusb+'\n' +'next is last iter data\n' +'jl='+jl+', jr='+jr+'\n' +'kl='+kl+', kr='+kr+'\n' +'ml='+ml+', mr='+mr+'\n' +'N='+N+'\n' //±1 sequence 3.5 2.5 2.5 2.4 1.6 1.5 1.5 0.5 //²1 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 //index 0 1 2 3 4 5 6 7 //above ±1,²1 jl=1 //now i=0, do nothing, //continue to next iteration. if( //9906232224 aMinusb<=tolerance0 //beta[i]==alfa[i] &&jl==-1 //jl not yet define ) { if(QHbx13) dbg0+='' +'continue11 i='+i+' ; '+'do nothing\n' continue; } //±1 sequence 3.5 2.5 2.5 2.4 1.6 1.5 1.5 0.5 //²1 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 //index 0 1 2 3 4 5 6 7 //above ±1,²1 jl=1 if( //9906232226 beta[i]>alfa[i] &&jl==-1 //jl not yet define ) { jl=i; //9906232226 N++; //9906232227 beta[i]>alfa[i] not equal, N++ if(QHbx13) dbg0+='' +'continue12 i='+i+' ; '+'jl='+jl+' ; '+'N='+N+' beta[i]>alfa[i]\n' continue; } //±1 sequence 3.5 2.5 2.5 2.4 1.6 1.5 1.5 0.5 //²1 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 //index 0 1 2 3 4 5 6 7 //above ±1,²1 jl=1 if( beta[i]>alfa[i] &&jl>-1 //jl already define &&kl==-1 //kl not yet define &&ml==-1 //ml not yet define ) { //jr move forward until ml or kl defined jr=i; //9906232231 N++; //9906232052 if(QHbx13) dbg0+='' +'continue13 i='+i+' ; '+'jr='+jr+' ; '+'N='+N+'\n' continue; } //±2 sequence 3.5 2.5 2.5 2.5 1.5 1.5 1.5 0.5 //²2 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 //index 0 1 2 3 4 5 6 7 //above ±2,²2 jr=2; //abs(aMinusb)<=tol0 say ±[3]=²[3]=2.5 if( //9906232236 aMinusb<=tolerance0 //beta[i]==alfa[i] &&jl>-1 //jl already define &&kl==-1 //kl not yet define &&ml==-1 //ml not yet define ) { ml=i; //9906232237 jr=i-1; //9906232309 if(QHbx13) dbg0+='' +'continue14 i='+i+' ; '+'jr='+jr+' ; '+'ml='+ml+'\n' continue; } //±1 sequence 3.5 2.5 2.5 2.4 1.6 1.5 1.5 0.5 //²1 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 //index 0 1 2 3 4 5 6 7 //above ±1,²1 jr=3; if( beta[i]<alfa[i] &&kl==-1 ) { kl=i; //9906232241 if(ml>-1)mr=i-1; //9906232242 else jr=i-1; //9906232311 N++; if(QHbx13) dbg0+='' +'continue15 i='+i+' ; '+'kl='+kl +' ; '+'jr='+jr+' ; '+'mr='+mr +' ; '+'N='+N+'\n' continue; } //±2 sequence 3.5 2.5 2.5 2.5 1.5 1.5 1.5 0.5 //²2 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 //index 0 1 2 3 4 5 6 7 //above ±2,²2 kl=5; if( //9906232244 beta[i]<alfa[i] &&kl>-1 ) { //kr move forward until i=tota-1 //or until beta[i]==alfa[i] kr=i; //9906232245 N++; if(QHbx13) dbg0+='' +'continue16 i='+i+' ; '+'kr='+kr+' ; '+'N='+N+'\n' continue; } //±2 sequence 3.5 2.5 2.5 2.5 1.5 1.5 1.5 0.5 //²2 sequence 3.5 3.0 2.8 2.5 1.5 1.2 1.0 0.5 //index 0 1 2 3 4 5 6 7 //above ±2,²2 kl=5; if( //9906232244 aMinusb<=tolerance0 //beta[i]==alfa[i] &&kr>-1 ) { //kr stop move forward if(QHbx13) dbg0+='' +'continue17 i='+i+' ; '+'kr='+kr+' ; '+'N='+N+'\n' break; //9906232254 } if(QHbx13) dbg0+='\n' +'bottom of for(i=0;i<tota;i++)\n' } //for(i=0;i<tota;i++) 9906201421 if(QHbx13) dbg0+='===67=== 9906232302\n' +'i='+i+' , done for loop\n' +'alfa[i]='+alfa[i]+'\n' +'beta[i]='+beta[i]+'\n' +'aMinusb='+aMinusb+'\n' +'next is final data\n' +'jl='+jl+', jr='+jr+'\n' +'kl='+kl+', kr='+kr+'\n' +'ml='+ml+', mr='+mr+'\n' +'N='+N+'\n' j=k=-1; if(ml>-1&&mr>-1) { j=ml-1; while(Math.abs(beta[j]-alfa[j])<=tolerance0 && j>0 ) j--; //9906201722 k=mr+1 while(Math.abs(beta[k]-alfa[k])<=tolerance0 && k<tota-1 ) k++; //9906201726 } else { j=jr; k=kl; //9906201628 } if(QHbx15) dbg990623= '9906232105 jkCheck()\n' +'alfa=['+alfa+']\n' +'beta=['+beta+']\n' +'if(ml>-1&&mr>-1) else\n' +'ml='+ml+', mr='+mr+'\n' //9906300824 +'j='+j+', k='+k+'\n' +'jl='+jl+', kl='+kl+'\n' +'jr='+jr+', kr='+kr+'\n' /**9906300832 try remove //The code below is unnecessary 9906300833 // // //9906202331 // alfa=[0.5,0.5,0] // beta=[ 1, 0, 0] //has trouble. //add following code if(N>0) //9906202342 { //deleted } //The code above is unnecessary 9906300835 /**/ if(QHbx13) QHboxb13.value+=dbg0 var rho=(beta[j]+beta[k])/2; var tau= beta[j]-rho; //9906201636 //QHboxb12.value+='\n\n=====\n' if(QHbx15) //9906301240 QHboxb15.value+='' //9906271240 +dbg990623+'\n' //9906300907 change b15 +'9906201422\n' //from "=" to "+=" +'jl='+jl+', jr='+jr+'\n' +'kl='+kl+', kr='+kr+'\n' +'ml='+ml+', mr='+mr+'\n' +' j='+ j+', k='+ k+'\n' +'alfa[j]+alfa[k]='+(alfa[j]+alfa[k])+'\n' +'beta[j]+beta[k]='+(beta[j]+beta[k])+'\n' +'rho=(beta[j]+beta[k])/2; rho='+rho+'\n' +'tau= beta[j]-rho; tau='+tau+'\n' +'If tau==0, divide by zero, error.\n' +'9906201637\n\n\n' ; return [j,k,N]; //990623 } //function jkCheck() var docH='<pre>' +'<a name=docH001>&lt;a name=docH001&gt;</a>\n' +'The following is document for\n' +'function robinMatrixf()\n' +'2010-06-28-20-30\n' +'Robin Hood transformation is most\n' +'important material.\n' +'function robinMatrixf() document\n' +'is in study notes page <a href=http://freeman2.com/tute0047.htm>tute0047.htm</a>\n' +'The following discuss the error\n' +'caused by beta[j]=beta[k]. Please\n' +'visit study notes, please read<a href=http://www-stat.wharton.upenn.edu/~steele/Publications/Books/CSMC/CSMC_index.html>\n' +'The Cauchy-Schwarz Master Class</a>\n' +'chapter 13 for complete document.\n' +'2010-06-28-20-37\n' +'<a name=docH002>&lt;a name=docH002&gt;</a>\n' +'2010-06-21-18-22\n' +'If the condition\n' +'beta[j]=beta[k] //ERROR\n' +'is true, then next two lines\n' +'rho=(beta[j]+beta[k])/2;\n' +'tau= beta[j]-rho;\n' +'<a name=docH003>&lt;a name=docH003&gt;</a>\n' +'create tau=0. matrix elements\n' +'j1=(tau+sigma)/2/tau;\n' +'j2=(tau-sigma)/2/tau;\n' +'become infinity.\n' +'Next if() try to stop this case, \n' +'<a name=docH004>&lt;a name=docH004&gt;</a>\n' +'First method is copy from alpha\n' +'to beta\n' +'beta[i0]=alfa[i0];\n' +'result OK. trouble is by-pass \n' +'matrix transformation. Final\n' +'"Bx7 stochastic mat" is not \n' +'complete.\n' +'<a name=docH005>&lt;a name=docH005&gt;</a>\n' +'Second method is use Reverse \n' +'Robin Hood Matrix robinMatrixr()\n' +'result same disaster.\n' +'[ robinMatrixr() is deleted ]\n' +'Both solution fail.\n' +'2010-06-21-18-31\n' +'\n' +'<a name=docH006>&lt;a name=docH006&gt;</a>\n' +'Newer version do j,k update correct.\n' +'New version do not have above trouble.\n' +'2010-06-28-20-43\n' +'</pre>' //9906201930 function robinMatrixf( n,j,k,bj,bk,aj,ak ) { QGspanAlert2.innerHTML= //9906211538 'Regular Robin Hood Matrix robinMatrixf() called' /** 9906201921 change below to ** a function robinMatrix() /**/ var QGbx18=0; //9906271918 if(typeof QGboxb18=='object' &&typeof QGchkbxcC1=='object' &&QGchkbxcC1.checked )QGbx18=1; //9906272327 if(QGbx18) QGboxb18.value+='\n===robinMatrixf_1===\n' //9906230745 +'just enter function robinMatrixf()\n' +'n,j,k='+n+','+j+','+k+'\n' +'aj,ak='+aj+','+ak+'\n' +'bj,bk='+bj+','+bk+'\n' +'(aj+ak)/2='+((aj+ak)/2)+'\n' +'(bj+bk)/2='+((bj+bk)/2)+'\n' +'typeof aj='+(typeof aj)+'\n' +'typeof ak='+(typeof ak)+'\n' +'typeof bj='+(typeof bj)+'\n' +'typeof bk='+(typeof bk)+'\n' +'typeof bj[0]='+(typeof bj[0])+'\n' +'typeof bk[0]='+(typeof bk[0])+'\n' +'9906230748\n' ; n=parseInt(n); var robinMat0=identity(n) var rho=(bj+bk)/2; var tau= bj-rho; var sigma=Math.abs(ak-rho); var i0=Math.abs(aj-rho); if(i0>sigma)sigma=i0; //9906172233 var j1=(tau+sigma)/2/tau; var j2=(tau-sigma)/2/tau; robinMat0[j][j]=robinMat0[k][k]=j1; robinMat0[j][k]=robinMat0[k][j]=j2; if(QGbx18) QGboxb18.value+='\n===robinMatrixf_2===\n' //9906230749 +'end of function robinMatrixf()\n' +'rho=(bj+bk)/2='+rho+'\n' +'tau= bj-rho='+tau+'\n' +'sigma=Math.abs(ak-rho)='+sigma+'\n' +'if(|aj-rho}>sigma)sigma=|aj-rho|\n' +'j1=(tau+sigma)/2/tau='+j1+'\n' +'j2=(tau-sigma)/2/tau='+j2+'\n' +'robinMat0=[\n'+mat2str(robinMat0)+']\n' +'9906230753\n' return robinMat0; /** 9906201921 change above to ** a function robinMatrix() */ } //9906201938 function robinMatrixf() function verifyf() { //9906211103 if(QGboxb17.value=='') //9906250001 {alert('verifyf() alert01\n' +'Please click to run stochastic\n' +'and generate matrix to box 7\n' +'FIRST. Now box 7 is empty.'); return;} if(QGboxb12.value=='') //9906250004 {alert('verifyf() alert02\n' +'Please fill data to box 1 and 2,\n' +'click to run stochastic, generate matrix\n' +'to box 7 FIRST. Now box 2 is empty.'); return;} var inMat= QHboxb11.value=QGboxb17.value; QHboxb12.value=QGboxb12.value; //QHboxb14.value=QGboxb11.value; QHboxb13.value='' +'Result of Bx7 stochastic mat\n' +'product Box 2 ² seq. Should\n' +'be same as Box 1 ± seq.\n' +'Look at Box M4 second line.\n' /** +'Next is ± seq COLUMN vector.\n' //9906231325 + mat2str(matMVecf(s2a(QHboxb11.value), s2a(QHboxb12.value,'column'))) //9906231321 add 'column' //9906231605 change column output //to row output /**/ +('' //9906231606 +matMVecf(s2a(QHboxb11.value), s2a(QHboxb12.value,'column'))) .replace(/,/g,' ') var i,j,k; var w0=inMat.length-1; while(inMat.charAt(w0)<'0' || inMat.charAt(w0)>'9') w0--; inMat=inMat.substring(0,w0+1); //9906211127 //9906211124 inMat=inMat.split('\n'); for(i=0;i<inMat.length;i++) //9906211133 inMat[i]=readdata(inMat[i]); var vOut=''; //9906211138 var neg0=0; var pad0=''; var blank=' '; var o,ol; var secLen=6; for(i=0;i<inMat.length;i++) //9906211139 { neg0=0; for(j=0;j<inMat[i].length;j++) { o=(inMat[i][j]+''); ol=o.length; pad0=''; pad0=blank.substring(0,secLen-ol); vOut+=pad0+(inMat[i][j]+'').substring(0,6)+' ' if(inMat[i][j]<0.)neg0++; } vOut+=neg0>0?'NEG '+neg0+'\n':'\n'; } var colSum=''; var rowSum=''; //9906211203 var sum0=0; for(i=0;i<inMat.length;i++) //9906211205 { sum0=0; for(j=0;j<inMat[i].length;j++) { sum0+=inMat[i][j]; } rowSum+='row '+(i+1)+' sum='+bye09(sum0)+'\n'; } for(i=0;i<inMat.length;i++) //9906211208 { sum0=0; for(j=0;j<inMat[i].length;j++) { sum0+=inMat[j][i]; } colSum+='col '+(i+1)+' sum='+bye09(sum0)+'\n'; } var tmp=QGboxb11.value; k=tmp.length-1; while(tmp.charAt(k)<'0' || tmp.charAt(k)>'9') k--; tmp=tmp.substring(0,k+1); //9906211231 QHboxb14.value='' +'Next is original ± seq.\n' +tmp +'\n' +'It should = Box M3 value\n' +'(original ² seq see Box M2)\n' +'Next is matrix row/col sum\n' +rowSum +colSum +'If above are all one still\n' +'not enough. Look at below\n' +'if no negative number, OK.\n' +'Next is shortened stoch\n' +'matrix, Bx7 is complete\n' +'Next alert negative value\n' +vOut ; //9906211217 QHspanAlert1.innerHTML='Box M4=Box 1=± seq., Box M3=stochastic matrix*² seq.' } //9906291251 var link0='<br><a href=#intro001>intro</a> ' +'<a href=#atQGboxb11>±,ß in</a> ' +'<a href=#atQGboxb17>mat ou</a> ' +'<a href=#MMArray>verify</a> ' +'<a href=#how001>how to use</a>; ' +'<a href=#atQHboxb11>M*M</a>; ' +'<a href=#sortnumb>sort</a>;<br> ' +'<a href=#atQHboxb15>addQuote</a>; ' +'doc <a href=#docA000>A</a>, <a href=#docB000>B</a>, ' +'<a href=#docC000>C</a>, <a href=#docD000>D</a>, ' +'<a href=#docE000>E</a>, <a href=#docF000>F</a>, ' +'<a href=#docG000>G</a>, <a href=#docH000>H</a>; ' +'<a href=http://freeman2.com/powmean2.htm>P.M.</a> ' +'<a href=http://freeman2.com/curve3d2.htm>3D</a> ' +'<br>' +'<a href=#docU001>twoMinor</a>, ' +'<a href=#atQHboxb15>fraction to quotient</a>, ' +'<a href=#sortClick>permutation matrix</a> ' +'<br>' //9906301222 function clearBox() { QGboxb13.value= QGboxb14.value= QGboxb15.value= QGboxb16.value= QGboxb17.value= QGboxb18.value= QGspanAlert1.innerHTML= QHboxb13.value= QHboxb14.value= QHboxb15.value= //9906300909 clear box QHboxb16.value=''; } //9906301229 //--> </SCRIPT> </HEAD> <body link="#FF0000" vlink="#0000FF" alink="#50A000" bgcolor="#ccfcfc" > <!-- onload="javascript:initial0()" --> <Font size=+3> Majorization transformation <br> between two sequences. </font> <br> Given two sequences, find doubly stochastic matrix <br> <a href="#majorBgn"> program </a> &nbsp; <a href=#update011> Update 2010-07-15 </a> <script type="text/javascript"><!-- document.write(link0); //9906291259 //--></script> <br> <a href=http://www.structura.info/XYGraph/XYGraphDemo.htm> XYGraph v2.3 - web page graph </a> &nbsp; <!--9506231219 add link--> <a href=http://www.structura.info/XYGraph/Links.htm> && <!--##9756;##9758;--> </a> &nbsp; <!--9506231216 add link--> <a href=http://www.structura.info/XYGraph/Purchase.htm> donate<!--9801031950 use donate ##9829;##9835; e&k&--> </a> &nbsp; <a href="http://www.structura.info/XYGraph/XYGraph.zip"> get code</a> <!-- 2006-06-12-18-21 download http://www.structura.info/XYGraph/XYGraph.zip C:\$fm\js\xygraph\XYGraph.zip 2006-06-19-08-21 record --> <br> <a href=http://www-stat.wharton.upenn.edu/~steele/Publications/Books/CSMC/CSMC_index.html> The Cauchy-Schwarz Master Class </a> &nbsp; <a href=http://www-stat.wharton.upenn.edu/~steele/index.html> J. Michael</a> <a href=http://www.wharton.upenn.edu/faculty/steele.html> Steele</a> &nbsp; <a href=http://www.amazon.com/review/product/052154677X/ref=cm_cr_dp_all_helpful?%5Fencoding=UTF8&coliid=&showViewpoints=1&colid=&sortBy=bySubmissionDateDescending> &&&&& </a><!--9806081839 add link--> <br> <font color=red> This file is personal home work. No one <br> proofread. Cannot promise correctness. <br> If you suspect any view point wrong, <br> please ask a math expert near by. <br> Freeman 2009-06-19-10-46</font> <br> Please use MSIE browser to read this file. <br> Did not test other browser. This file is <br> <!--9806081846--> written under MSIE 6.0 <br> <script src="http://freeman2.com/rocsitee.js" language="javascript"></script> <br> <a name="textbook">&lt;a name="textbook"&gt;</a> (<a href=jsmajor2.htm>local</a>) <br> This program <a href=http://freeman2.com/jsmajor2.htm>http://freeman2.com/jsmajor2.htm</a> <br> use book written by <a href=http://www.wharton.upenn.edu/faculty/steele_michael.jpg>Professor</a> <a href=http://www-stat.wharton.upenn.edu/~steele/index.html>J. Michael</a> <a href=http://www.wharton.upenn.edu/faculty/steele.html>Steele</a> <br> <a href=http://www-stat.wharton.upenn.edu/~steele/Publications/Books/CSMC/CSMC_index.html>The Cauchy-Schwarz Master Class</a> 0<a href=http://www.amazon.com/review/product/052154677X/ref=cm_cr_dp_all_helpful?%5Fencoding=UTF8&coliid=&showViewpoints=1&colid=&sortBy=bySubmissionDateDescending>&&&&&</a> <br> as textbook. Program code is written base on <br> textbook page 198 to page 201. <br><br> <a name="intro001">&lt;a name="intro001"&gt;</a> If find error <a href="#intro019">what to do?</a> <script type="text/javascript"><!-- document.write(link0); //9906291259 //--></script> <pre><font size=+2>2010-06-28-21-59 start This page Majorization transformation between two sequences. Given two sequences, find doubly stochastic matrix<a href=http://freeman2.com/jsmajor2.htm> http://freeman2.com/jsmajor2.htm</a> is a result of study The <a href=#textbook>Cauchy-Schwarz Master Class</a> chapter 13 Majorization and Schur Convexity <a name="intro002">&lt;a name="intro002"&gt;</a> jsmajor2.htm has four programs (1) <a href="#majorBgn">Given</a> two sequences, find doubly stochastic matrix. <a href="#atQGboxb11">Input</a> at box 1 and box 2. Output at <a href="#atQGboxb17">box 7</a>. Example button from 11 to 18, 51 to 53 and 71 to 73. <a name="intro003">&lt;a name="intro003"&gt;</a> 11 to 18 had no trouble at early version. 51 to 53 got negative element matrix at early version. Now 51 to 53 are OK. Example 71 to 73 do not have majorization relation or not compatible. Run button is [run stoch]</font></pre> <script type="text/javascript"> <!-- document.write(link0); //9907041057 //--> </script> <pre><font size=+2><a name="intro004">&lt;a name="intro004"&gt;</a> (2) <a href="#atQHboxb11">Matrix</a> multiplication. Input at box M1 and box M2. Output at <a href="#atQHboxb13">box M3</a>. This is an auxiliary function Because Bx7 stochastic matrix need Matrix multiplication, <a name="intro005">&lt;a name="intro005"&gt;</a> please see P2*R2*P1*R1*ß seq. ---<a href=#docA016>eqn.maj01</a> Run button is [M*M Array] and [M*M Str]. Difference is <a href=#docD002>here</a> Example buttons are 01,02,03. <a name="intro006">&lt;a name="intro006"&gt;</a> (3) <a href="#atQHboxb15">Add</a> quote to comments. Allow one set comment be on top of related function and show up on web page. Serve two locations. This is an auxiliary function too. <a name="intro007">&lt;a name="intro007"&gt;</a> Inpur at Box M5, output at Box M6. Run button is [AddQuote pre] and [AddQuote br] This function for programmer use only. because output to javascript string code.</font></pre> <script type="text/javascript"> <!-- document.write(link0); //9907041057 //--> </script> <pre><font size=+2><a name="intro008">&lt;a name="intro008"&gt;</a> (4) <a href="#sortnumb">Sort</a> number Input at Box 71, output to Box 72. Run button is [Output new index] and [sorted sequence 321] and [sorted sequence 123] <a name="intro009">&lt;a name="intro009"&gt;</a> Another button [up side down] change from [[ aaa bbb ccc ]] to [[ ccc bbb aaa ]] <a name="intro010">&lt;a name="intro010"&gt;</a> <a href="#atQGboxb11">Box 1</a> top click button [s1] Box 2 top click button [s2] use sort function. Create permutation matrix, use sort [Output new index] function. 2010-06-28-22-42 stop</font></pre> <script type="text/javascript"> <!-- document.write(link0); //9907041057 //--> </script> <pre><font size=+2><a name="intro810">&lt;a name="intro810"&gt;</a> more <a href=#update007>01</a> <a href=#update010>02</a> 2010-07-04-11-05 start (5) Change fraction to <a href=#atQHboxb15>quotient</a> number. This function is added because in study notes <a href=tute0047.htm>page</a> <a href=http://freeman2.com/tute0047.htm>http://freeman2.com/tute0047.htm</a> change decimal number matrix to quotient number matrix. <a name="intro811">&lt;a name="intro811"&gt;</a> Change longer number to shorter number. <a href=tute0046.htm#ch13a203>First time</a> do it by hand, <a href=tute0047.htm#ch13b015>second time</a> decide to write a program to convert. Paste long decimal number into [x=:] box, fill [n2=] number default 3000. Click [fill M5] <a name="intro812">&lt;a name="intro812"&gt;</a> more <a href=#update007>01</a> <a href=#update010>02</a> Program generate sequence from 1 to 3000 at box M5. Next click [x*M5] button. Output at box M6. If the only sequence is positive integer, program need only [n2=] box, generate internal positive integer sequence. Actually, this <a name="intro813">&lt;a name="intro813"&gt;</a> program read from box M5. Such design allow any number sequence be used. For example, user can build a prime number sequence for other application. <a name="intro814">&lt;a name="intro814"&gt;</a> Left side of [x*M5] button has a [t] button, [t] is Test. Click t program generate random number x and indicate the quotient number Right side of [x*M5] button has a check box. If checked, output sequence of x*Box M5 value. <a name="intro815">&lt;a name="intro815"&gt;</a> Number x = 2.353/7 = 2353/7000 = 13*181/(2*2*2*5*5*5*7) = 0.3361428571428572 If fill M5 up to 3000 (no 7000) output x=NaN/undefined NaN = Not a Number. If fill M5 from 6800 to 7200 (has 7000) get normal answer. 2010-07-04-11-36 stop</font></pre> <script type="text/javascript"> <!-- document.write(link0); //9907041057 //--> </script> <pre><font size=+2><a name="intro910">&lt;a name="intro910"&gt;</a> 2010-07-04-12-34 start (6) Build permutation matrix. in/output is at <a href=#sortClick>Box 71</a> Paste random ordered data sequence to Box 71. Click [output new index] button. Box 72 has answer. <a name="intro911">&lt;a name="intro911"&gt;</a> 1st First is index sequence which correspond to descending data sequence. <font color=red><b> Be alert to index sequence and data sequence. They are different. data sequence is sorted. index sequence appear random.</b></font> <a name="intro912">&lt;a name="intro912"&gt;</a> 2nd Box 72 second answer is permutation matrix which transform Box 71 random ordered data sequence to descending data sequence. <a name="intro913">&lt;a name="intro913"&gt;</a> 3rd Box 72 third answer is permutation matrix which transform Box 71 random ordered data sequence to ascending data sequence. <a name="intro914">&lt;a name="intro914"&gt;</a> To verify, copy permutation Permut321 matrix to <a href=#atQHboxb11>Box M1</a> copy Box 71 random ordered sequence to Box M2, then click [M*M Array] button. Output to Box M3. It should be in descending order. If <a name="intro915">&lt;a name="intro915"&gt;</a> you do not want permutation matrix, just want sorted number. On top of Box 71, click [Sorted Seq 321] or click [Sorted Seq 123] . Output to Box 72 and no permutation matrix. 2010-07-04-13-00 stop</font></pre> <script type="text/javascript"> <!-- document.write(link0); //9907041057 //--> </script> <pre><font size=+2><a name="intro011">&lt;a name="intro011"&gt;</a> 2010-06-29-11-31 start Other click buttons are <a href="#MMArray">Build</a> n=[5] Identity matrix. This is programming debug button. In case you need identity matrix it can help. Output to Box M3. '5' in n=[5] can be replaced. <a name="intro012">&lt;a name="intro012"&gt;</a> <a href="#MMArray">Verify</a> Bx7 stochastic matrix. stoch. matrix (SM) must be all elements non-negative, each column sum to one, each row sum to one. <a name="intro013">&lt;a name="intro013"&gt;</a> You can do it by hand. But Verify button do the job for you. Output to Box M3 for ±_seq. = SM*²_seq Box M4 for sum to one and non-negative check. <a name="intro014">&lt;a name="intro014"&gt;</a> After find stochastic matrix (<a href="#atQGboxb17">Bx7</a>) then <a href=#how001> How to use stochastic matrix?</a> Please click above link. It bring you to "How to" section. <a name="intro015">&lt;a name="intro015"&gt;</a> You need click either one of 11 to 53 example button, then click "run stoch" button, then click "How to use?" link. Result display right below. <a name="intro016">&lt;a name="intro016"&gt;</a> How to use stochastic matrix main point is under ÿa name=how007ÿ where you can verify that individual alfa[i] = one row product main point is also under ÿa name=how009ÿ where tell you which study page to read.</font></pre> <script type="text/javascript"> <!-- document.write(link0); //9907041057 //--> </script> <pre><font size=+2><a name="intro017">&lt;a name="intro017"&gt;</a> This file jsmajor2.htm has many javascript functions. Main functions have document displayed from docA handle to docH handle It may help you if you want to know programming. <a name="intro018">&lt;a name="intro018"&gt;</a> <font color=red size=+3><b>This file jsmajor2.htm may still contain error.</b></font> If Liu,Hsinhan find error and if LiuHH correct error, the update notice is at <a href=http://freeman2.com/freeman2.htm#whatsnew>whatsnew</a> section. Please visit whatsnew regularly. Thank you for visiting Freeman's page.0 Freeman 02010-06-29-11-59. <a name="intro019">&lt;a name="intro019"&gt;</a> 2010-06-29-15-40 start The important javascript function in this program are <a href=#docG000>function</a> jkCheck(arg1,arg2,arg3) <a href=#docH000>function</a> robinMatrixf(n,j,k,bj,bk,aj,ak) <a href=#docD000>function</a> matMVecf(arg1,arg2) If you find error output, above three functions are first to exam. <a name="intro020">&lt;a name="intro020"&gt;</a> Within these three functions. robinMatrixf() and matMVecf() are less likely to have error. LiuHH spend more time on the function jkCheck(). If j,k determination is wrong, output matrix has negative elements or infinite loop. 2010-06-29-15-50 stop </font></pre> <a name="majorBgn">&lt;a name="majorBgn"&gt;</a> <script type="text/javascript"><!-- document.write(link0); //9906291259 //--></script> <font color=red size=+2> Output may contain error, Please verify first. <br> Program environment is MSIE 6.0, please use MSIE </font> <br> Majorization transformation between two sequences. <br> Input ± sequence to box 1, input ² sequence to box 2. <br> (1) ±_seq. & ²_seq. both be non-negative. <a href="tute0046.htm#ch13a121"><font color=red><b>alert</b></font></a> <br> (2) ± & ² both have same number of elements. <br> (3) ± total sum and ² total sum are equal. <a href="tute0046.htm#ch13a125"><font color=blue><b>Blue alert</b></font></a> <br> (4) When arrange both ± and ² to descending order, <br> &nbsp; &nbsp; ² partial sum is always g" ± partial sum. <br> Example button 11,12,13+ are OK. 51+ had negative <br> matrix elements. Current version improved. (51+ OK) <br> Example button 71+ do not have majorization relation. <br> tolerance= <INPUT value="1.e-8" id=QGboxTol size=4 > <!--9906190947 --> compare elements need tolerance. <br> maximum iteration= <INPUT value="10" id=QGboxMit size=4 > <!--9906191038 --> done in reasonable steps. <br> Write (slow) all <INPUT id="QGchkbxcB1" type="checkbox" onclick="QGchkbxcB2.checked=0" checked > OR (faster) last <INPUT id="QGchkbxcB2" type="checkbox" onclick="QGchkbxcB1.checked=0" > set output. <br> Box 8 debug output <INPUT id="QGchkbxcC1" type="checkbox" onclick="QGchkbxcC2.checked=QGchkbxcC1.checked" > ; <a href=#how001 onclick="javascript: spanhowtouse.innerHTML=how2use(); " ><!--9906241240; 9906272250--> How to use stochastic matrix? </a> <br> <a name="atQGboxb11"></a> <font color=red><b> Input Box 1 & 2, output <a href="#atQGboxb17">Box 7</a>. All other are debug. </b></font> <table border=0> <tr> <td> Box 1, ± seq <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QGboxb11").value.toString())' type=button value="copy1"> <INPUT onclick='document.getElementById("QGboxb11").value=""' type=button value="del1"> <input type="button" value="s1" onmouseover="title='Sort Box 1 data'" onclick="javascript: //9906211429 var inArray=readdata(QGboxb11.value); bubble_1(inArray, inArray.length ) QGboxb11.value='' //9906211430 +(''+inArray).replace(/,/g,' ') "> <br> <TEXTAREA id=QGboxb11 name=QGboxb11 rows=6 cols=30 > 4 3 3 3 2 </TEXTAREA> <!-- 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 9906162304 retire above var t1a=0.8; var t1b=0.2; var t1=[[1, 0, 0, 0, 0], [0, t1a, 0, t1b, 0], [0, 0, 0, 0, 0], [0, t1b, 0, t1a, 0], [0, 0, 0, 0, 0] ] --> </td> <td> Box 2, ² seq <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QGboxb12").value.toString())' type=button value="copy2"> <INPUT onclick='document.getElementById("QGboxb12").value=""' type=button value="del2"> <input type="button" value="s2" onmouseover="title='Sort Box 2 data'" onclick="javascript: //9906211433 var inArray=readdata(QGboxb12.value); bubble_1(inArray, inArray.length ) QGboxb12.value='' //9906211434 +(''+inArray).replace(/,/g,' ') "> <br> <TEXTAREA id=QGboxb12 name=QGboxb12 rows=6 cols=30 > 6 4 3 1 1 </TEXTAREA> <!-- var beta=[9,7,4,2,1]; 9906172028 retire next 31 41 32 42 33 43 34 44 35 45 --> </td> </tr> </table> <font color=red> <span id=QGspanAlert1> ± sum=²sum; ± more average, ² more diverse; click [Run Stoch] </span><!--9906161952--> </font> <br> <input type="button" value="Run Stoch" onmouseover="title='Main run button'" onclick="javascript: clearBox(); var ansId=stochastic0( QGboxb11.value, QGboxb12.value, QGboxTol.value, QGboxMit.value, 'QGboxb13.value', 'QGboxb14.value', 'QGboxb15.value', 'QGboxb16.value', 'QGspanAlert1.innerHTML' ) QGboxb17.value=mat2str(ansId); " /> <!--9906172024 --> <input type="button" value="h1" onclick="QGspanAlert1.innerHTML='' +'For majorization, box 1 alpha seq. box 2 beta seq. <a href=#majorBgn>more</a>' "> <input type="button" value="d" onmouseover="title='Delete box 3 to 8'" onclick="QGspanAlert1.innerHTML= QGspanAlert2.innerHTML= QGboxb13.value= QGboxb14.value= QGboxb15.value= QGboxb16.value= QGboxb17.value= QGboxb18.value='' "> <!-- example 11 to 49 has regular output 9906211000--> <input type="button" value="11" onclick=" QGboxb11.value=' 5 3 3 2 2'; QGboxb12.value=' 5 4 3 2 1'; QGspanAlert1.innerHTML='Majorization. Box 2 majorize box 1 <input type=button value=why onclick=\'javascript:whyf(1); \'>' " /><!--9906171605--> <input type="button" value="12" onclick=" QGboxb11.value='0.5 0.5 0'; QGboxb12.value=' 2 0 -1'; QGspanAlert1.innerHTML='<a href=#textbook>CSMC</a> page 187 line 1, negative number OK, <a href=tute0044.htm#ch12c087>eqn.BL080</a>' " /><!--9906192156--> <input type="button" value="13" onclick=" QGboxb11.value=' 3 2 1 '; QGboxb12.value=' 4 1 1 '; QGspanAlert1.innerHTML='Example used in <a href=tute0044.htm#ch12c106>tute0044.htm#ch12c106</a>' " /><!--9906251806--> <input type="button" value="14" onclick=" QGboxb11.value=' 5 3 3 2 2 '; QGboxb12.value=' 5 4 3 2 1 '; QGspanAlert1.innerHTML='This set is used in <a href=tute0046.htm#ch13a058>tute0046.htm#ch13a058</a>.' " /><!--9906211109--> <input type="button" value="15" onclick=" QGboxb11.value=' -2 -3 -3 -3 -4 '; QGboxb12.value=' -1 -1 -3 -4 -6 '; QGspanAlert1.innerHTML='All negative number. Output OK. (only Bx7 not allow negative)' " /><!--9906211237 [4 3 3 3 2] and [6 4 3 1 1] doc=This set turn around more and still well. Box 1/2 default 9906220034 change to [-2 -3 -3 -3 -4 ] and [ -1 -1 -3 -4 -6 ] --> <input type="button" value="16" onclick=" QGboxb11.value=' 0.2 0.2 0.2 0.2 0.2 '; QGboxb12.value=' 1 0 0 0 0 '; QGspanAlert1.innerHTML='An AM-GM inequality problem. Box 1 is GM, Box 2 is AM' " /><!--9906211902--> <input type="button" value="17" onclick=" QGboxb11.value=' 4 3.2 3 2.8 2 '; QGboxb12.value=' 6 4.4 3 0.9 0.7 '; QGspanAlert1.innerHTML='3.2+2.8=6 NOT equal 5.3=4.4+0.9 , well ? ill ?' " /><!--9906222345--> <input type="button" value="18" onclick=" QGboxb11.value=' 18 14 12 6 '; QGboxb12.value=' 20 15 10 5 '; QGspanAlert1.innerHTML='Example used in document.' " /><!--9906242251--> <!-- example 51 to 69 has majorization relation but ill output 9906211001--> <input type="button" value="51" onclick=" QGboxb11.value=' 3 2 1 1 1'; QGboxb12.value=' 8 0 0 0 0'; QGspanAlert1.innerHTML='Has majorization relation. Matrix had negative. Now OK.' " /><!--9906192005--> <input type="button" value="52" onclick=" QGboxb11.value=' 5 3 2 1 1'; QGboxb12.value=' 6 2 2 2 0'; QGspanAlert1.innerHTML='RobinHood refused \'average\' [2 2 2] to [3 2 1], now OK.' " /><!--9906191800--> <input type="button" value="53" onclick=" QGboxb11.value=' 5 3 3 2 1 1'; QGboxb12.value=' 6 2.8 2.8 1.8 1.5 0.1'; QGspanAlert1.innerHTML='No common value element. Longest iteration=5 .' " /><!--9906192133--> ; <!-- example 71+ no majorization relation 9906211002--> <input type="button" value="71" onclick=" QGboxb11.value=' 3 3 1 1 1'; QGboxb12.value=' 3 2 2 1 1'; QGspanAlert1.innerHTML='2nd partial sum ±ÿ², 3+3ÿ3+2 NO majorization' " /><!--9906192007--> <!-- 9906271255--> <input type="button" value="72" onclick=" QGboxb11.value=' 3 3 1 1 '; QGboxb12.value=' 3 2 2 1 1'; QGspanAlert1.innerHTML='Error. Number of elements are different.' " /> <!-- 9906271256--> <input type="button" value="73" onclick=" QGboxb11.value=' 3 3 1 1 0'; QGboxb12.value=' 3 2 2 1 1'; QGspanAlert1.innerHTML='Error. Total sum are different.' " /> <br> <a name="atQGboxb13"></a> <table border=0> <tr> <td> Box 3 ² history <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QGboxb13").value.toString())' type=button value="copy3"> <INPUT onclick='document.getElementById("QGboxb13").value=""' type=button value="del3"> <input type="button" value="dm" onmouseover="title='Delete box M3 to M6'" onclick=" QHspanAlert1.innerHTML= QHboxb13.value= QHboxb14.value= QHboxb15.value= QHboxb16.value='' "><!--9906201236--> <br> <TEXTAREA id=QGboxb13 name=QGboxb13 rows=6 cols=30 > Box 3, record ± sequence , ² sequence after Robin Hood trans- formed ² sequence and after permutation re- ordered ² sequence. </TEXTAREA> </td> <td> Box 4 RobinHood <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QGboxb14").value.toString())' type=button value="copy4"> <INPUT onclick='document.getElementById("QGboxb14").value=""' type=button value="del4"> <br> <TEXTAREA id=QGboxb14 name=QGboxb14 rows=6 cols=30 > Box 4 is Robin Hood matrix. Convert more diverse ² seq. to more average ± sequence. Matrix structure refer to Cauchy-Schwarz Master Class page 198, 199, 200, 201 </TEXTAREA> </td> </tr> </table> <font color=red> <span id=QGspanAlert2> <a href=http://www-stat.wharton.upenn.edu/~steele/Publications/Books/CSMC/CSMC_index.html> The Cauchy-Schwarz Master Class</a> Liu,Hsinhan <a href=http://freeman2.com/tute0046.htm>study notes</a> </span><!--9906182201--> </font> <br> <a name="atQGboxb15"></a> <!--9906182000 use box 5/6--> <table border=0> <tr> <td> Box 5 error code <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QGboxb15").value.toString())' type=button value="copy5"> <INPUT onclick='document.getElementById("QGboxb15").value=""' type=button value="del5"> <br> <TEXTAREA id=QGboxb15 name=QGboxb15 rows=6 cols=30 > Box 5 store error code. First line all zero is OK Second line example 1 4 2 say ² sequence exchange 1st and 4th elements to get more average seq. There are total 2 element different betwen ± and ² </TEXTAREA> </td> <td> Box 6 permutation <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QGboxb16").value.toString())' type=button value="copy6"> <INPUT onclick='document.getElementById("QGboxb16").value=""' type=button value="del6"> <br> <TEXTAREA id=QGboxb16 name=QGboxb16 rows=6 cols=30 > Box 6 store permutation matrix. If inputs are ± seq.=[ 3 2 1 1 1] ² seq.=[ 8 0 0 0 0] After first Robin Hood transformation ² seq.=[ 7 0 0 0 1] permutation matrix to ² seq.=[ 7 1 0 0 0] Since check majorization must have descend order. </TEXTAREA> </td> <!-- 9906182200 start 9906182246 done box 3 to box 6 document --> </tr> </table> <a name="atQGboxb17"></a> <!--9906182249 use box 7/8 because final over-all transformation matrix need one box. --> <table border=0> <tr> <td> Bx7 stochastic mat <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QGboxb17").value.toString())' type=button value="copy7"> <INPUT onclick='document.getElementById("QGboxb17").value=""' type=button value="del7"> <br> <TEXTAREA id=QGboxb17 name=QGboxb17 rows=6 cols=30 > </TEXTAREA> </td> <td> Box 8, <INPUT id="QGchkbxcC2" type="checkbox" onclick="QGchkbxcC1.checked=QGchkbxcC2.checked" > debug <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QGboxb18").value.toString())' type=button value="copy8"> <INPUT onclick='document.getElementById("QGboxb18").value=""' type=button value="del8"> <br> <TEXTAREA id=QGboxb18 name=QGboxb18 rows=6 cols=30 > </TEXTAREA> </td> </tr> </table> <a name="VerifyButton"></a> <INPUT type=button value="Verify" onclick="javascript: if(QGboxb17.value==''){alert('Please click to run stochastic\nand generate matrix to box 7\nFIRST. Now box 7 is empty.'); return;} verifyf(); " > output to Box M3 & M4 below. <input type="button" value="run stoch" onclick="javascript: clearBox(); var ansId=stochastic0( QGboxb11.value, QGboxb12.value, QGboxTol.value, QGboxMit.value, 'QGboxb13.value', 'QGboxb14.value', 'QGboxb15.value', 'QGboxb16.value', 'QGspanAlert1.innerHTML' ) QGboxb17.value=mat2str(ansId); " /> <!--9906172024, 9906211134 --> <br> Box M3 = ± seq = stochastic matrix * ² seq <br> Box M4 do column sum & row sum. Check matrix +/0/-. <script type="text/javascript"><!-- document.write(link0); //9906291259 //--></script> <hr> <hr> <br> <a name="atQHboxb11"></a> <table border=0> <tr> <td> Box M1, input <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QHboxb11").value.toString())' type=button value="copyM1"> <INPUT onclick='document.getElementById("QHboxb11").value=""' type=button value="delM1"> <br> <TEXTAREA id=QHboxb11 name=QHboxb11 rows=6 cols=30 > 5 3 3 2 2 </TEXTAREA> <!-- 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 9906162304 retire above var t1a=0.8; var t1b=0.2; var t1=[[1, 0, 0, 0, 0], [0, t1a, 0, t1b, 0], [0, 0, 0, 0, 0], [0, t1b, 0, t1a, 0], [0, 0, 0, 0, 0] ] --> </td> <td> Box M2, input <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QHboxb12").value.toString())' type=button value="copyM2"> <INPUT onclick='document.getElementById("QHboxb12").value=""' type=button value="delM2"> <br> <TEXTAREA id=QHboxb12 name=QHboxb12 rows=6 cols=30 > 5 4 3 2 1 </TEXTAREA> <!-- var beta=[9,7,4,2,1]; 9906172028 retire next 31 41 32 42 33 43 34 44 35 45 --> </td> </tr> </table> <a name="MMArray"></a> <input type="button" value="M*M Array" onclick="javascript: QHboxb13.value= mat2str(matMVecf(s2a(QHboxb11.value),s2a(QHboxb12.value,'column'))) " /><!--9906171058--> <input type="button" value="?L" onclick="QHspanAlert1.innerHTML= 'Matrix * matrix in/output both use 2-Din array.' " /> <input type="button" value="?R" onclick="QHspanAlert1.innerHTML= 'Matrix * matrix in/output both use string.' " /> <input type="button" value="M*M Str" onclick="javascript: var cD=commonDim(QHboxb11.value,QHboxb12.value); //alert('typeof cD='+typeof cD) if(cD<=0)QHboxb13.value='input dimension error.\n' +'commonDim() return cD='+cD else //why if else not work ?? 9906281450 //if(cD>0) QHboxb13.value= '' //+'cD='+cD+'\n' //9906281457 dbg +matMatStr(QHboxb11.value,QHboxb12.value,cD) " /> ; <input type="button" value="53" onclick=" QGboxb11.value=' 5 3 3 2 1 1'; QGboxb12.value=' 6 2.8 2.8 1.8 1.5 0.1'; QHspanAlert1.innerHTML='No common value element. Longest iteration=5 .' " /><!--9906192133--> <input type="button" value="Run Stoch" onclick="javascript: clearBox(); var ansId=stochastic0( QGboxb11.value, QGboxb12.value, QGboxTol.value, QGboxMit.value, 'QGboxb13.value', 'QGboxb14.value', 'QGboxb15.value', 'QGboxb16.value', 'QGspanAlert1.innerHTML' ) QGboxb17.value=mat2str(ansId); " /> <!--9906172024, 9906211134 --> <INPUT type=button value="verify" onclick="javascript: if(QGboxb17.value==''){alert('Please click to run stochastic\nand generate matrix to box 7\nFIRST. Now box 7 is empty.'); return;} verifyf(); " > <!-- 9906211136 --> <br> <font color=red> <span id=QHspanAlert1> Please visit <a href=http://freeman2.com/jsindex2.htm>Javascript index page</a> for more programs. </span><!--9906161952--> </font> <br> <input type="button" value="Build" onclick="javascript: var ansId=identity(QHboxcA1.value) var i,j,o=''; for(i=0;i<ansId.length;i++){ for(j=0;j<ansId[i].length;j++)o+=ansId[i][j]+' '; o+='\n'}; QHboxb13.value=o; " /> <!--9906171530 --> n= <INPUT value="5" id=QHboxcA1 size=4 > <!--9906171527 --> Identity ; <input type="button" value="dm" onmouseover="title='Delete box M3 to M6'" onclick=" QHspanAlert1.innerHTML= QHboxb13.value= QHboxb14.value= QHboxb15.value= QHboxb16.value='' "><!--9906201236--> <input type="button" value="h2" onclick="QHspanAlert1.innerHTML='' +'Box M1, left matrix size=m*n ; Box M2 right matrix size=n*p' "> <input type="button" value="01" onclick=" QHboxb11.value=' 11 12 13 14 15\n 16 17 18 19 20\n 21 22 23 24 25\n 26 27 28 29 30\n 31 32 33 34 35' QHboxb12.value='3\n4\n5\n6\n7' " /><!--9906162148--> <input type="button" value="02" onclick=" QHboxb11.value=' 11 12 13 14 15\n 16 17 18 19 20\n 21 22 23 24 25\n' QHboxb12.value='31 41\n32 42\n33 43\n34 44\n35 45\n' " /><!--9906162306--> <input type="button" value="03" onclick=" QHboxb11.value=' 1.1 1.2 1.3 1.4 1.5\n 1.6 1.7 1.8 1.9 2.0\n 2.1 2.2 2.3 2.4 2.5\n' QHboxb12.value='3.1 4.1\n3.2 4.2\n3.3 4.3\n3.4 4.4\n3.5 4.5\n' " /><!--9906222206--> <br> <a name="atQHboxb13"></a> <table border=0> <tr> <td> Box M3, output <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QHboxb13").value.toString())' type=button value="copyM3"> <INPUT onclick='document.getElementById("QHboxb13").value=""' type=button value="delM3"> <br> <TEXTAREA id=QHboxb13 name=QHboxb13 rows=6 cols=30 > </TEXTAREA> </td> <td> Box M4, debug <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QHboxb14").value.toString())' type=button value="copyM4"> <INPUT onclick='document.getElementById("QHboxb14").value=""' type=button value="delM4"> <br> <TEXTAREA id=QHboxb14 name=QHboxb14 rows=6 cols=30 > </TEXTAREA> </td> </tr> </table> <font color=red> <span id=QHspanAlert2> Thank you for visiting Freeman's web site <a href=http://freeman2.com/freeman2.htm#whatsnew>freeman2.com</a><br> </span><!--9906280900--> </font> <a name="atQHboxb15"></a> <table border=0> <!--9906271236 use box M5, M6 need more debug box. Not send output to QHboxb12.value+=... send to QHboxb15.value= 9906271238 --> <tr> <td> Box M5, debug <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QHboxb15").value.toString())' type=button value="copyM5"> <INPUT onclick='document.getElementById("QHboxb15").value=""' type=button value="delM5"> <br> <TEXTAREA id=QHboxb15 name=QHboxb15 rows=6 cols=30 > </TEXTAREA> </td> <td> Box M6, debug <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("QHboxb16").value.toString())' type=button value="copyM6"> <INPUT onclick='document.getElementById("QHboxb16").value=""' type=button value="delM6"> <br> <TEXTAREA id=QHboxb16 name=QHboxb16 rows=6 cols=30 > Convert decimal number to quotient number. Please click "?" below for help (buttons [dx] [?] [t]) AddQuote click [help3] </TEXTAREA> </td> </tr> </table> <!--9906271553--> <input type="button" value="AddQuote pre" onclick="javascript: QHspanAlert2.innerHTML='' +'Output to box 6. Use [pre]. Not use [br]' +'<br>'; QHboxb16.value=addQuote( QHboxb15.value ) " /> <!--9906271658--> <input type="button" value="AddQuote br" onclick="javascript: QHspanAlert2.innerHTML='' +'Use [br]. Not use [pre]. Output to box 6.' +'<br>'; QHboxb16.value=addQuote( QHboxb15.value, 1 ) " /> <!--9906280655--> <input type="button" value="test" onclick="javascript: if(QHboxb15.value.length==0) QHboxb15.value='Please fill test string\n' +'to Box M5, click either of\n' +'[AddQuote pre], [AddQuote br]\n' +'Then click [test] Output to\n' +'page below [AddQuote pre].\n' if(QHboxb16.value.length>0 &&QHboxb16.value.indexOf('var renameDocString=')>=0 ) { eval(QHboxb16.value); //9906280712 QHspanTest.innerHTML=renameDocString ; } " /><!--9906280700--> <!--9906280718--> <input type="button" value="del" onclick="QHspanTest.innerHTML=QHspanAlert2.innerHTML='<br>'" /> AddQuote <input type="button" value="help3" onclick="javascript: QHspanAlert2.innerHTML='<br>'; QHboxb15.value='2010-06-27-17-22\n' +'[AddQuote pre], [AddQuote br]\n' +'two buttons convert document\n' +'to javascript code. Put doc.\n' +'string on top of related\n' +'function and be able to \n' +'display on web page too.\n' +'One document serve two\n' +'purpose. 2010-06-27-17-26\n' ; QHspanTest.innerHTML='Please see Box M5 for help3.<br>' " /> <br> <a href="#intro810" style="text-decoration: none;"> x: </a> <INPUT value="0.2826086956521739" id=QHboxcB1 size=15 > <!--9907011223 --> <input type="button" value="dx" onmouseover="title='delete x, del mesage.'" onclick="QHboxcB1.value='';QHspanTest.innerHTML='<br>'" > <!--9907011248 --> <input type="button" value="?" onclick="QHspanTest.innerHTML='' +'Change fraction to quotient number. Fill x, click [F] or <a href=#update010>[L]</a><br>' +'If box value b:1 e:3000 click [F] box M5 receive 1 to 3000.<br>' +'To run click [x*M5], output to Box M6. Box M5 can be<br>' +'any number sequence as divisor. M5 supplied by user. <br>' +'If x is irrational, no rational answer. 2010-07-11-14-26' " > <!--9907031400 --> b: <INPUT value="1" id=QHboxcB0 size=2 onmouseover="title='begin of Box M5'" > <!--9907111238 --> e: <INPUT value="3000" id=QHboxcB2 size=2 onmouseover="title='end of Box M5'" > <!--9907011324 --> <input type="button" value="F" onmouseover="title='Fill number to Box M5'" onclick="javascript:fillLeap(1)" > <!--9907011328 --> <input type="button" value="L" onmouseover="title='Leap number to Box M5'" onclick="javascript:fillLeap(2)" > <!-- 9907111320 start Leap 9907111356 done Leap --> <!--9907031415 --> <input type="button" value="t" onmouseover="title='build Test number x'" onclick="javascript: var numer=Math.random(); var denom=Math.random(); numer=numer*1000; numer=parseInt(numer)+1; denom=denom*1000; denom=parseInt(denom)+1; QHboxcB1.value=numer/denom; QHspanTest.innerHTML='' +'x box filled '+numer+'/'+denom frac2quo(); //9907152100 add " > <!--9907031420 --> <!--9907011225--> <input type="button" value="x*M5" onmouseover="title='RUN fraction to quotient'" onclick="javascript:frac2quo();" /><!--9907011233--> <INPUT id="QHchkbxcA1" type="checkbox" onmouseover="title='More output to box M6'" > <!--9907011346--> <br> <span id=QHspanTest> <br> </span><!--9906280659--> <script type="text/javascript"> <!-- //9906271556 function addQuote(arg1,arg2) { //9906271642 add wktype var inp0=arg1.replace(/</g,'&lt;').replace(/>/g,'&gt;') inp0=inp0.split('\n'); var oup0='' var i,j,k; var br0='\\n'; var pre0='\'<pre>\''; var pre1='+\'</pre>\''; //9906271652 if(arguments.length==2 &&arg2==1 ) { br0='<br>'; pre0='\'\''; pre1=''; //9906271653 } for(i=0;i<inp0.length;i++) { inp0[i]=inp0[i].replace(/\'/g,'\\\''); //9906271629 //oup0+=''+'+\''+inp0[i].replace('\\n', '<br>\"\n') //oup0+=''+'+\''+inp0[i].replace('\\n', '')+'<br>\"\\n' //oup0+=''+'+\''+inp0[i].substring(0, inp0[i].length-2)+'<br>\"\n' oup0+='' //9906271612 +'+\''+inp0[i].substring(0, inp0[i].length-1) //+'<br>\'\n' +br0+'\'\n' } //9906271815 //9906272017 del oup0=oup0.replace(/</g,'&lt;').replace(/>/g,'&gt;') //return oup0; //9906271612 return 'var renameDocString=' +pre0+'\n'+oup0+pre1; //9906271654 } //9906271613 //9907111510 change //from in button code //to function // function fillLeap(arg1) //9907111510 { var n2=parseInt(QHboxcB2.value) if(isNaN(n2)||n2<1){ QHspanTest.innerHTML='' +'Please input correct [e]nd box value,' +' now ['+QHboxcB2.value+']'; return;} var n0=parseInt(QHboxcB0.value) if(isNaN(n0)||n0<1) //9907111326 { QHspanTest.innerHTML='' +'Please input correct [b]egin box value,' +' now ['+QHboxcB0.value+']'; return;} var max0=n2-n0+1; //9907111328 max0 if(max0<0)max0=n0-n2+1; var step0=1; //9907111329 step0 if(n0>n2)step0=-1; var oup1=''; var iter0=0; if(arg1==2) { if(n2==n0){QHboxb15.value='begin=end='+n2;return} var leap0=0; //9907111345 if(n2>n0){leap0=n2-n0; QHboxcB0.value= n0=n0+leap0+1; QHboxcB2.value= n2=n2+leap0+1; } else {leap0=n0-n2; QHboxcB0.value= n0=n0-leap0-1; QHboxcB2.value= n2=n2-leap0-1; } //9907111350 } //if(arg1==2) //for(var i=n0;i<n2;i++) i=n0; while(iter0<max0){ //9907111333 while() oup1+=(i)+'\n';i+=step0;iter0++;} QHboxb15.value=oup1 //9907152041 //if(!isNaN(QHboxcB1.value))frac2quo(); //9907152053 if(!isNaN(QHboxcB1.value) &&QHboxcB1.value.length>0 ) frac2quo(); //alert('QHboxcB1.value=['+']\n'+'!isNaN(QHboxcB1.value)='+(!isNaN(QHboxcB1.value))+'\n9907152049') } //9907152046 function frac2quo(arg1) { if(QHboxb15.value.length==0) QHboxb15.value= 'Please fill a sequence of\n' +'numbers to Box M5 and one\n' +'number to box x. Click \n' +'[x*M5 seq] Output Box M6\n' +'If click [fill], program\n' +'fill integer number from\n' +'[b] box value to [e] box\n' +'value to box M5. If check\n' +'box at right end, program\n' +'add sequence output. \n' +'2010-07-01-13-37\n' ; else { var xMSeq=''; //9907011225begin code var y,min0=min1=90; var minAns=''; var minAtI=-1; var x=parseFloat(QHboxcB1.value); var tiny5=1.e-8 if(isNaN(x))x=0; var inNumb=readdata(QHboxb15.value); var find09=''; //9907041157 var i; for(i=0;i<inNumb.length;i++) { y=x*inNumb[i]; xMSeq+=y+'\n'; //min0=y-parseInt(y); find09=y-parseInt(y); //9907041202 if(find09<tiny5)min0=find09; //above for 0.0000000000002 //below for 0.9999999999992 if((1-find09)<tiny5)min0=1-find09; //9907041205 if(min0<min1) { minAtI=i; //9907011342 min1=min0; //9907011255 minAns='Min: i='+i+',inNumb[i]=' +inNumb[i]+'\nfrac='+min0+'\n' ; } if(min0<tiny5)break; //9907011707 } QHboxb16.value='Find minimum fraction.\n' +minAns+'x='+x+'\n' +'x='+bye09(x*inNumb[minAtI])+'/' +inNumb[minAtI] //9907041212 bye09() +((min0<tiny5)?'' //9907041355 dbg : '\niter='+i+',min0='+min0 +',min1='+min1+'\nminAtI='+minAtI +'; Please Leap b/e' ) +(QHchkbxcA1.checked //9907011349 ? '\n\n'+xMSeq : '' ) ; } } //--> </script> <hr> <hr> <a name=docA000></a> docA handle <script type="text/javascript"> <!-- document.write(link0+docA); //9906271616 //QHboxb16.value=docA; //9906271704 dbg //--> </script> <br> <a name=docB000></a> docB handle <script type="text/javascript"> <!-- document.write(link0+docB); //9906271830 //--> </script> <br> <a name=docC000></a> docC handle <script type="text/javascript"> <!-- document.write(link0+docC); //9906272213 //--> </script> <br> <a name=docD000></a> docD handle <script type="text/javascript"> <!-- document.write(link0+docD); //9906281236 //--> </script> <br> <a name=docE000></a> docE handle <script type="text/javascript"> <!-- document.write(link0+docE); //9906281832 //--> </script> <br> <a name=docF000></a> docF handle <script type="text/javascript"> <!-- document.write(link0+docF); //9906281915 //--> </script> <br> <a name=docG000></a> docG handle <script type="text/javascript"> <!-- document.write(link0+docG); //9906281947 //--> </script> <br> <a name=docH000></a> docH handle <script type="text/javascript"> <!-- document.write(link0+docH); //9906282006 //--> </script> <a name=docU001>&lt;a name=docU001&gt;</a> <font color=red><b> TwoMinor programs </b></font> <script type="text/javascript"> <!-- document.write(link0); //9907041057 //--> </script> <pre><font size=+2><a name=docU001>&lt;a name=docU001&gt;</a> 2010-07-04-13-02 start This file jsmajor2.htm has two small programs fraction to quotient converter build permutation matrix <a name=docU002>&lt;a name=docU002&gt;</a> fraction to quotient converter document is at <a href=#intro810>intro810</a> in/output is at <a href=#atQHboxb15>QHboxb15</a> <a name=docU003>&lt;a name=docU003&gt;</a> build permutation matrix document is at <a href=#intro910>intro910</a> in/output is at <a href=#sortClick>Box 71</a> 2010-07-04-13-06 stop <a name=update001>&lt;a name=update001&gt;</a> 2010-06-30-10-15 start Update 2010-06-30 add few comment for function jkCheck() Please see <a href=#docG007>docG007</a> 2010-06-30-10-16 stop <a name=update002>&lt;a name=update002&gt;</a> 2010-06-30-13-03 start To test negative matrix element alert. Please click [run stoch] build a matrix in <a href=#atQGboxb17>box 7</a>. Change numbers to negative value. (change box 7, NOT change box M1) <a name=update003>&lt;a name=update003&gt;</a> Then click [verify] button Box M4 will print " NEG 1". '1' indicate one negative in current row. <a name=update004>&lt;a name=update004&gt;</a> If click [How to use?] At ÿa name=how004ÿ there is big red "negative !" Both are negative matrix element alert. 2010-06-30-13-10 stop <a name=update005>&lt;a name=update005&gt;</a> 2010-06-30-16-43 start New version of jsmajor2.htm [Box 6 permutation] always get identity matrix. LiuHH added program function. In <a href="#sortnumb">sort</a> area, if click [output new index] Box 72 show permutation matrix. Document is at <a href=#intro910>intro910</a><!--2010-07-04-15-02--> 2010-06-30-16-48 stop <a name=update006>&lt;a name=update006&gt;</a> 2010-07-04-14-55 start Update 2010-07-04 add fraction to quotient code Please see document at <a href=#intro810>intro810</a> and in/output at <a href=#atQHboxb15>QHboxb15</a> 2010-07-04-14-58 stop <a name=update007>&lt;a name=update007&gt;</a> 2010-07-11-12-49 Update 2010-07-11 change <a href=#atQHboxb15>fraction to quotient</a> control box from b2 one box to b and e two boxes Because during work need to find <a name=update008>&lt;a name=update008&gt;</a> quotient of 0.02219711236660385 calculation get 884/39825 that is 2*2*13*17/(3*3*3*5*5*59) run integer list from 1 to 39825 wait for three minutes. Decide <a name=update009>&lt;a name=update009&gt;</a> change code structure. Allow begin number 'b', end number 'e' specified for better control. Now input b:39000 e:40000 and x:0.02219711236660385 Click [fill], click [x*M5] get x=884/39825 in one second. 2010-07-11-13-01 <a name=update010>&lt;a name=update010&gt;</a> 2010-07-11-14-01 Added [L] button for Leap function If begin 'b', end number 'e' are b:1 e:3000 and click [L], program fill box M5 from 3001 to 6000 and change b/e box value to b:3001 e:6000 For the number x:0.02219711236660385 =884/39825 user click [L], instead of [F] 2010-07-11-14-06 <a name=update011>&lt;a name=update011&gt;</a> Update 2010-07-15 add auto run function to [F], [L], [t] <a href=#atQHboxb15>buttons</a> 2010-07-15-21-05 </font></pre> <hr> <hr> <a name=how001>&lt;a name=how001&gt;</a> <input type="button" value="run stoch" onclick="javascript: clearBox(); var ansId=stochastic0( QGboxb11.value, QGboxb12.value, QGboxTol.value, QGboxMit.value, 'QGboxb13.value', 'QGboxb14.value', 'QGboxb15.value', 'QGboxb16.value', 'QGspanAlert1.innerHTML' ) QGboxb17.value=mat2str(ansId); " /> <!--9906172024, 9906211134, 9906241259 --> &nbsp; <a href=#how001 onclick="javascript: spanhowtouse.innerHTML=how2use(); " ><!--9906241240--> How to use? </a> &nbsp; <input type="button" value="delHow" onclick="javascript: spanhowtouse.innerHTML=''; " ><!--9906241521--> <br> <!-- example 11 to 49 has regular output 9906211000--> <input type="button" value="11" onclick=" QGboxb11.value=' 5 3 3 2 2'; QGboxb12.value=' 5 4 3 2 1'; QGspanAlert8.innerHTML='Majorization. Box 2 majorize box 1' " /><!--9906171605--> <input type="button" value="12" onclick=" QGboxb11.value='0.5 0.5 0'; QGboxb12.value=' 2 0 -1'; QGspanAlert8.innerHTML='<a href=#textbook>CSMC</a> page 187 line 1, negative number OK, <a href=tute0044.htm#ch12c087>eqn.BL080</a>' " /><!--9906192156--> <input type="button" value="13" onclick=" QGboxb11.value=' 3 2 1 '; QGboxb12.value=' 4 1 1 '; QGspanAlert8.innerHTML='Example used in <a href=tute0044.htm#ch12c106>tute0044.htm#ch12c106</a>' " /><!--9906251806--> <input type="button" value="14" onclick=" QGboxb11.value=' 5 3 3 2 2 '; QGboxb12.value=' 5 4 3 2 1 '; QGspanAlert8.innerHTML='This set is used in <a href=tute0046.htm#ch13a058>tute0046.htm#ch13a058</a>.' " /><!--9906211109--> <input type="button" value="15" onclick=" QGboxb11.value=' -2 -3 -3 -3 -4 '; QGboxb12.value=' -1 -1 -3 -4 -6 '; QGspanAlert8.innerHTML='All negative number. Output OK. (only Bx7 not allow negative)' " /><!--9906211237 [4 3 3 3 2] and [6 4 3 1 1] doc=This set turn around more and still well. Box 1/2 default 9906220034 change to [-2 -3 -3 -3 -4 ] and [ -1 -1 -3 -4 -6 ] --> <input type="button" value="16" onclick=" QGboxb11.value=' 0.2 0.2 0.2 0.2 0.2 '; QGboxb12.value=' 1 0 0 0 0 '; QGspanAlert8.innerHTML='An AM-GM inequality problem. Box 1 is GM, Box 2 is AM' " /><!--9906211902--> <input type="button" value="17" onclick=" QGboxb11.value=' 4 3.2 3 2.8 2 '; QGboxb12.value=' 6 4.4 3 0.9 0.7 '; QGspanAlert8.innerHTML='3.2+2.8=6 NOT equal 5.3=4.4+0.9 , well ? ill ?' " /><!--9906222345--> <input type="button" value="18" onclick=" QGboxb11.value=' 18 14 12 6 '; QGboxb12.value=' 20 15 10 5 '; QGspanAlert8.innerHTML='Example used in document.' " /><!--9906242251--> <!-- example 51 to 69 has majorization relation but ill output 9906211001--> <input type="button" value="51" onclick=" QGboxb11.value=' 3 2 1 1 1'; QGboxb12.value=' 8 0 0 0 0'; QGspanAlert8.innerHTML='Has majorization relation. Matrix had negative. Now OK.' " /><!--9906192005--> <input type="button" value="52" onclick=" QGboxb11.value=' 5 3 2 1 1'; QGboxb12.value=' 6 2 2 2 0'; QGspanAlert8.innerHTML='RobinHood refused \'average\' [2 2 2] to [3 2 1], now OK.' " /><!--9906191800--> <input type="button" value="53" onclick=" QGboxb11.value=' 5 3 3 2 1 1'; QGboxb12.value=' 6 2.8 2.8 1.8 1.5 0.1'; QGspanAlert8.innerHTML='No common value element. Longest iteration=5 .' " /><!--9906192133--> <br> <font color=red> <span id=QGspanAlert8> </span><!--9906280944--> </font> <script type="text/javascript"><!-- document.write(link0); //9906291259 //--></script> <br> <span id=spanhowtouse> </span> <script type="text/javascript"> <!-- //9906241246 function how2use() { var alfa; var beta; var mat0,mat1; if(typeof QGboxb11=='object') alfa=readdata(QGboxb11.value) if(typeof QGboxb12=='object') beta=readdata(QGboxb12.value) if(typeof QGboxb17=='object') if(QGboxb17.value.length>0) { mat0=QGboxb17.value; mat1=s2a(mat0) } else return 'Please click [12] then [stoc] button first<br>' +'Create stochastic matrix. Next click<br>' +'[how to use]<br>' //9906241405 ; var alen=alfa.length; var blen=beta.length; var n=mat1.length; var ans0=''; //9906241250 var i,j,k; var i0,i1,i2,i3,i4,i5; var neg01=''; //9906301247 for(i=0;i<mat1.length;i++) for(j=0;j<mat1[i].length;j++) if(mat1[i][j]<0) { neg01='<font color=red size=+3><b>negative !</b></font>' i=mat1.length; break; } //9906301251 ans0+='' //9906241350 +'<a name=how002>&lt;a name=how002&gt;</a><br>' +'The following is "how to use" document<br>' +'User <a href="#atQGboxb11">input</a> ±_seq. and ²_seq.<br>' +'Both sequences must be in decending order.<br>' +'You can paste random order and click "s1"<br>' +'or click "s2", program will sort for you.<br>' +'<a name=how003>&lt;a name=how003&gt;</a><br>' +'Box 1 ±_seq.='+alfa+'<br>' +'Box 2 ²_seq.='+beta+'<br>' +'Output is "<a href="#atQGboxb17">Bx7 stochastic mat</a>"<br>' +'<a name=how004>&lt;a name=how004&gt;</a><br>' +'Box 7 doubly stochastic matrix= ' +neg01 //9906301252 +'<br>' +QGboxb17.value.replace(/\n/g,'<br>') +'<a name=how005>&lt;a name=how005&gt;</a><br>' +'If ±_seq. and ²_seq. have majorization relation,<br>' +'please see <a href="#majorBgn">four conditions</a>, program calculate<br>' +'stochastic matrix and save to box 7.<br>' +'<a name=how006>&lt;a name=how006&gt;</a><br>' +'Consider ±_seq. and ²_seq. as column vector<br>' +'each of size '+n+' rows and one column.<br>' +'Consider box 7 doubly stochastic matrix as<br>' +n+' by '+n+' square matrix. Their relation is<br>' +'±_vector = square matrix * ²_vector<br>' +'<a name=how007>&lt;a name=how007&gt;</a><br>' +'In expanded form it has '+n+' equations:<br><br>' for(i=0;i<alen;i++) { ans0+='' //9906241435 +alfa[i]+'=' for(j=0;j<alen;j++) { ans0+='' //9906241436 +'+'+mat1[i][j]+'*('+beta[j] +') ' } ans0+='<br><br>' } //9906241439 ans0+='' +'<a name=how008>&lt;a name=how008&gt;</a><br>' +'Key point to observe is that the COEFFICIENT <br>' +'number between "+" and "*" are <br>' +'(1) non-negative <font color=red><b>If you see "+-" then matrix <br>' +' &nbsp; &nbsp; has negative coefficient. That is not <br>' +' &nbsp; &nbsp; user error, that is programmer error.</b></font><br>' +'(2) coefficients are in the range [0,1] <br>' +'(3) in one equation, coefficients sum to one.<br>' +'<a name=how009>&lt;a name=how009&gt;</a><br>' +'Such structured ±_seq. and ²_seq. have<br>' +'power mean inequality relation <a href=tute0030.htm#ch08a003>eqn.8.2</a>.<br>' +'or Muirhead\'s inequality relation <a href=tute0044.htm#ch12c068>eqn.12.18</a>.<br>' +'Please read <a href="#textbook">textbook</a> for detail.<br>' +'2010-06-24-14-57<br><br>' +'<a name=how010>&lt;a name=how010&gt;</a><br>' +'Doubly stochastic matrix is a matrix<br>' +'(1) all elements are non-negative.<br>' +'(2) each column sum to one.<br>' +'(3) each row sum to one.<br>' +'<a name=how011>&lt;a name=how011&gt;</a><br>' +'If you click "Verify" or "V" <a href="#MMArray">button</a><br>' +'"<a href="#atQHboxb13">Box M4</a>" will show you the sums.<br>' +'Usually column sum to one and row sum<br>' +'to one are true. If you see negative <br>' +'matrix element in box 7, that indicate<br>' +'<a name=how012>&lt;a name=how012&gt;</a><br>' +'program error. If you see error and if<br>' +'you know programming and majorization<br>' +'theory, you can modify program code.<br>' +'Otherwise, please visit <a href=http://freeman2.com/freeman2.htm#whatsnew>what is new</a><br>' +'Hope LiuHH be able make correction.<br>' +'2010-06-24-15-11<br><br>' return ans0; return '<br>How to use ?<br>working 9906241243<br>'; } //--> </SCRIPT> <hr> <hr> <br> <a name="sortnumb">&lt;a name="sortnumb"&gt;</a> <br> <font color=red size=+2>Sort number</font> not text string <script type="text/javascript"><!-- document.write(link0); //9906291259 //--></script> <font color=red size=+2> Output may contain error, Please verify first. <br> Program environment is MSIE 6.0, please use MSIE </font> <br> <a name="sortClick"></a> <INPUT onclick='javascript: sortNumbMan(0); spanSort1.innerHTML="Output descending index. <font color=red><b>show permutation matrix</b></font>"' type=button value="Output New Index"><!--9807151815--> &nbsp; <INPUT onclick='javascript:bubbleMan(0),spanSort1.innerHTML="Output new eigenvalue sequence. Ignore eigenvector. Run by bubble_1()"' type=button value="Sorted Seq. 321"><!--9811240942 use [Seq. 321]--> <!--9807151816 use button [Output New Seq.]--> &nbsp; <INPUT onclick='javascript:bubbleMan(1),spanSort1.innerHTML="Output 1,2,3 increase value. [bubble_1() output 3,2,1 default]"' type=button value="Sorted Seq. 123"><!--9811240943 new add--> <br> <span id=spanSort1><font color=red> Box11 accept only numbers, not name list, not non-number string. </font></span> <br> <a name="ch01tb04"></a> <table border=0> <tr> <td> Box 71, input number sequence <br> <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("boxc21").value.toString())' type=button value="copy71"> <INPUT onclick='document.getElementById("boxc21").value=""' type=button value="del71"> <INPUT onclick='boxc22.value=upsidedown2(boxc21.value),spanSort1.innerHTML="line unit reverse box 71 text, output to box 72. Number, text both OK."' type=button value="up side down"> <!--9809291951 upsidedown(), onclick='upsidedown()' 9811220924 upsidedown2() onclick='boxc22.value=upsidedown2(boxc21.value)' --> <br> <TEXTAREA id=boxc21 name=boxc21 rows=6 cols=30 > 0.556 -0.145 0.302e3 -0.305e2 -0.088 </TEXTAREA> </td> <td> Box 72, output answer <br> <INPUT onclick='javascript:window.clipboardData.setData("Text",document.getElementById("boxc22").value.toString())' type=button value="copy72"> <INPUT onclick='document.getElementById("boxc22").value=""' type=button value="del72"> <br> <TEXTAREA id=boxc22 name=boxc22 rows=6 cols=30 > Click [Output New Index] to get permutation matrix Click [Sorted Seq. 321] to get sorted data number </TEXTAREA> </td> </tr> </table> This file sort numbers, not for sort text; upsidedown text ok. <br> 2009-07-15-11-18 start sortNumb() <br> 2009-07-15-18-40 done sortNumb() <br> 2009-09-29-20-50 done upsidedown() <br> <SCRIPT language=JavaScript> <!-- //sortNumbMan() all ...Man() are MANager //function. Handle input/output. Other //programmer may not need this manager //function. //This manager function call main engine // ouArray=sortNumb(inArray) //Other programmer may want to use sortNumb() // 9807161228 function sortNumbMan(smArg1) //9807151818 { var inArray=readdata(boxc21.value); //9807151819 var sortedArr= //9906301554 sortNumb(inArray); //alert('typeof sortedArr='+(typeof sortedArr)+'\n9906301610'); var sortedStr= //9906301555 (''+sortedArr).replace(/,/g,'\n'); var i,j,k; //9906301556 create permute matrix var permutMat=new Array(inArray.length); //9906301557 for(i=0;i<inArray.length;i++) { permutMat[i]=new Array(inArray.length); for(j=0;j<inArray.length;j++) { permutMat[i][j]=0; if(j==sortedArr[i])permutMat[i][j]=1; } } var permut321=''; //9906301602 for(i=0;i<inArray.length;i++) { for(j=0;j<inArray.length;j++) { permut321+=permutMat[i][j]+' '; } permut321+='\n' } var permut123=upsidedown2(permut321); //9906301624 boxc22.value='' //9807151824 +sortedStr +'\n\n' +'Above is INDEX of descending\n' +'Below is permutation matrix.\n\n' +'Permut321 matrix=\n' +permut321 //9906301605 +'Permut321 * Box71 input seq.\n' +'get 321 descending output.\n' +'\n' +'Permut123 matrix=\n' +permut123 //9906301625 +'Permut123 * Box71 input seq.\n' +'get 123 ascending output.\n\n' +'Please copy/paste permutation\n' +'matrix to Box M1 (Not box 1)\n' +'Copy/paste Box 71 sequence\n' +'to Box M2. click [M*M Array]\n' +'Result show up at Box M3.\n' +'Suggest open second browser\n' +'for 2nd copy of jsmajor2.htm\n' ; } //9807151826 function sortNumbMan(smArg1) /** 2009-07-22-11-05 found old record 8902WK01.DLY [[ A tutorial on pointers and arrays in C 2000-02-16-01-57 http://www.netcom.com/~tjensen/ptr/pointers.htm save as pointer_01 to pointer_14 2000-02-16-02-05 http://home.netcom.com/~tjensen/ ]] 02/16/2000 02:04 AM 19,015 ch10x.htm /**/ //bubbleMan() all ...Man() are MANager //function. Handle input/output. Other //programmer may not need this manager //function. //This manager function call main engine // bubble_1(inArray, // inArray.length // ) //Other programmer may want to use bubble_1() // 9807161229 function bubbleMan(bmArg1) //9807151827 { var inArray=readdata(boxc21.value); //9807151819 bubble_1(inArray, inArray.length ) //output 3,2,1 if(bmArg1==1) { //output 1,2,3 inArray=upsidedown2((''+inArray).replace(/,/g,'\n')) //9811240949 } boxc22.value='' //9807151830 +(''+inArray).replace(/,/g,'\n'); } //9807151832 function bubbleMan(bmArg1) /** 9807161850 Assume input sequence is inNumb[]=[ 0.556 -0.145 0.302e3 -0.305e2 -0.088 ] then inNumb[0] store 0.556 inNumb[1] store -0.145 inNumb[2] store 302.0 inNumb[3] store -30.5 inNumb[4] store -0.088 then call newSeq=sortNumb(inNumb) get newSeq=[2,0,4,1,3] it means inNumb[2] store 302.0 greater than inNumb[0] store 0.556 greater than inNumb[4] store -0.088 greater than inNumb[1] store -0.145 greater than inNumb[3] store -30.5 newSeq=[2,0,4,1,3] this new sequence can be used to re-order eigenvalue and to re-order eigenvector 9807161856 stop 9807271355 start above inNumb[0] ... inNumb[4] old index is bb = [0,1,2,3,4] function sortNumb() main job is to change index to [2,0,4,1,3] If sort number directly, use if (aa[j] > aa[i]) sortNumb() sort number IN-directly sort number index bb[i] directly, use if (aa[bb[j]] > aa[bb[i]]) 9807271401 stop /**/ //function bubble(aa, N //9807151528 function sortNumb( //9807151528 aa //aa is input number sequence. ) { // 9807151612 and 9807151616 are two key points var N=aa.length; //9807151625 var bb=new Array(N); for(var m0=0;m0<N;m0++) bb[m0]=m0; //9807250842 //above line, create bb=[0,1,2,3,4] var i, j, k; for (i = 0; i < N-1; i++) { for (j = i+1; j <= N-1; j++) { // if (aa[j] > aa[i]) //from greatest to smallest if (aa[bb[j]] > aa[bb[i]]) //9807151616 { k=bb[i]; //9807151612 bb[i]=bb[j]; //9807151611 bb[j]= k ; //9807151613 } //if (aa[bb[j]] > aa[bb[i]]) } //for (j = i+1; j <= N-1; j++) } //for (i = 0; i < N-1; i++) return bb; // 9807161630 } //9807151617 function sortNumb() /** 9807271416 start loop command for (i = 0; i < N-1; i++) for (j = i+1; j <= N-1; j++) work as following assume input data is aa[0] = 0.556 aa[1] = -0.145 aa[2] = 302.0 aa[3] = -30.5 aa[4] = -0.088 original index is bb=[0,1,2,3,4] Goal is to find new order as below aa[2] = 302.0 aa[0] = 0.556 aa[4] = -0.088 aa[1] = -0.145 aa[3] = -30.5 right hand side of '=' is in great to small order new index is bb=[2,0,4,1,3] First enter outer loop for (i = 0; i < N-1; i++) Index [0,1,2,3,4] has five number, so N=5 i start from 0, 1, 2 to 3 . i not touch 4 ÿi < N-1 ÿ If i=4 then last number aa[4] no one to compete with aa[4] . Need at least reserve one number to compete with aa[i] Therefore, Five number sequence, i not reach 4 N number sequence, i not reach N-1 If it is just begin, i=0 . aa[0] is main role. Next see inner loop for (j = i+1; j <= N-1; j++) index j start from i+1=1 j goto 4 and stop. ÿj <= N-1 ÿ In inner loop, outer loop index i is constant, never change. Take aa[1],aa[2],aa[3],aa[4] One after one, compare with aa[0]. If it is just begin, old order is bb=[0,1,2,3,4] command if (aa[bb[j]] > aa[bb[i]]) is if (aa[bb[1]] > aa[bb[0]]) is if (aa[1] > aa[0]) is if (-0.145 > 0.556) Not true, not run if(true) commands Inner loop index j increase get j=2 At this time, outer loop index i still =0 command if (aa[bb[j]] > aa[bb[i]]) is if (aa[2] > aa[0]) is if (302.0 > 0.556) It is TRUE !! run if(true) commands k=bb[i]; bb[i] value store at k bb[i]=bb[j]; bb[i] receive bb[j] value bb[j]= k; bb[j] receive bb[i] value that is bb[i] and bb[j] exchange value. Before first exchange bb=[0,1,2,3,4] After first exchange bb=[2,1,0,3,4] bb[j] compare with bb[i] continue as above fashion. Until all done, get new index sequence bb=[2,0,4,1,3] command return bb; // 9807161630 send new index back to calling function. 9807271441 stop /**/ /* Program bubble_1.c from PTRTUT10.HTM 6/13/97 Ted Jensen Redwood City, California tjensen@ix.netcom.com July 1998 //9807151221 open C:\lc0\c_online\pointer\ch10x.htm void bubble(int a[], int N) */ function bubble_1(a, N // , bb //9807151249 bb store new sequence, but ) //failed, then have to write sortNumb() { // int i, j, t; var i, j, t; for (i = N-1; i >= 0; i--) { for (j = 1; j <= i; j++) { // if (a[j-1] > a[j]) //9807151238 change to next if (a[j-1] < a[j]) //from greatest to smallest { t = a[j-1]; a[j-1] = a[j]; a[j] = t; } } } } /*---------------------- end bubble_1.c -----------------------*/ /** 2009-07-22-11-10 open pointers.htm 2000-02-16-01-57 http://www.netcom.com/~tjensen/ptr/pointers.htm [[ A TUTORIAL ON POINTERS AND ARRAYS IN C by Ted Jensen Version 1.1 (HTML version) July 1998 This material is hereby placed in the public domain Available in various formats via http://www.netcom.com/~tjensen/ptr/cpoint.htm TABLE OF CONTENTS Preface Introduction Chapter 1: What is a Pointer? Chapter 2: Pointer Types and Arrays. Chapter 3: Pointers and Strings Chapter 4: More on Strings Chapter 5: Pointers and Structures Chapter 6: More on Strings and Arrays of Strings Chapter 7: More on Multi-Dimensional Arrays Chapter 8: Pointers to Arrays Chapter 9: Pointers and Dynamic Allocation of Memory Chapter 10: Pointers to Functions Epilog ]] /**/ /** //9809291954 if box11 has [[ 0.556 -0.145 0.302e3 -0.305e2 -0.088]] last line no newline byte, next line inStr0=boxc21.value+'\n'; do not help .split() know box11 last line no newline byte, even if I attach '\n', .split() still drop '\n' from last line. then after upside down first line (was list line and no newline byte) and second line string to one line. 98,09,29,20,36 record //9809292038 version two in output string add '\n', success ouStr0+='\n'; //9809292040 9809291951 upsidedown(), onclick='upsidedown()' 9811220924 upsidedown2() onclick='boxc22.value=upsidedown2(boxc21.value)' function upsidedown() /**/ function upsidedown2(udArg1) { //var inStr0=boxc21.value var inStr0=udArg1; //9811220928 var ouStr0=''; var p0,p1,p2; inStr0=inStr0.split('\n'); ouStr0=inStr0[inStr0.length-1]; if(ouStr0.charCodeAt(ouStr0.length-1)>=32) ouStr0+='\n'; //9809292040 for(p0=inStr0.length-2;p0>=0;p0--) { ouStr0+=inStr0[p0] //9809292048 +(inStr0[p0].charCodeAt(inStr0[p0].length-1)<32?'':'\n') //9811241012 solve problem ; } return ouStr0; //9811220929 }//9809292050 // function upsidedown2(udArg1) //--> </script> <script type="text/javascript"> <!-- //2009-09-23-15-01 from tute0009.htm //find function readdata(inStr0) // //var w2=[0,0]; to var w2=[0]; 9906181215 // function readdata(inStr0) //9806101151 { //9807270955 read other's no-comment code //very headache, now back to my code, add //more comments. // //if input inStr0 is Not a Number (isNaN=true) //then inStr0 is an array or string of char. if(isNaN(inStr0) //9807161944 //and if inStr0[0] is a Number //(isNaN=false) (!isNaN=true) //inStr0 is a number array, return oroginal form &&!isNaN(inStr0[0])) return inStr0; //if input inStr0 is empty, return oroginal form if(inStr0.length==0)return inStr0; //9806101153 //if input inStr0 is a number, return oroginal form if(!isNaN(inStr0[0]))return inStr0; //9807271000 //up to here input inStr0 is one string of character //below change string of char '1.23 4.56 7.89 ...' //to number array aa[0]=1.23, aa[1]=4.56, aa[2]=7.89 ... //9807271010 here //assume input string is '1.23 4.56 7.89 ...' //oArray[0] store '1.23' this is a string //oArray[1] store '4.56' not a number yet //oArray[2] store '7.89' convert to number later //9807271043 var oArray=''; //9806101158 var w0,w1,numb0; for(w0=0;w0<inStr0.length;w0++) { numb0=''; //skip those char are NOT [0123456789-+.] // [!(...)] means [is not ...] // [!=] means [not equal] 9807271051 // w1=w0; //9802172117 while(!(inStr0.charAt(w1)>='0' &&inStr0.charAt(w1)<='9') &&inStr0.charAt(w1)!='-' &&inStr0.charAt(w1)!='+' &&inStr0.charAt(w1)!='.' &&w1<inStr0.length ) { //index w1 increase only, //no record what read 9807271058 w1++; } //above while() loop stop at reading //either one in the list [0123456789-+.] //then start next while loop. 9807271055 // while((inStr0.charAt(w1)>='0' &&inStr0.charAt(w1)<='9') ||inStr0.charAt(w1)=='-' ||inStr0.charAt(w1)=='+' ||inStr0.charAt(w1)=='.' ||inStr0.charAt(w1)=='e' ||inStr0.charAt(w1)=='E' &&w1<inStr0.length ) //9802171608 { //If it is either one of [0123456789-+.eE] //continue store data into numb0 . 9807271102 numb0+=inStr0.charAt(w1); w1++; } //if all I get is just '.' for example // 'aa.txt' it is not a number, //no need to build output oArray //9807271106 if(numb0.length==1&&numb0.charAt(0)=='.') { w0=w1; //9802172218 //update index w0=w1 continue; //continue to next iteration. } //skip the following building code. //up to this point, numb0 is useful number //build output string oArray //9807271111 w0=w1; //9806101210 update index //string numb0 to output string oArray //add a newline byte '\n' to separate data // 9807271113 oArray+=numb0+'\n'; //9806101159 } // for(w0=0;w0<inStr0.length;w0++) //done for() loop, done output string oArray //use newline byte '\n' to separate numbers. // oArray.split('\n') in oArray cut data at //newline byte '\n' and change string // '1.23\n4.56\n7.89\n...' //to array oArray[0]='1.23', oArray[1]='4.56', //oArray[2]='7.89' ... //no matter input data use ',' or tab or //newline to separate data, here all done //9807271125 oArray=oArray.split('\n'); //9806101643 //string '1' + string '2' = '12' //number 1 + number 2 = 3 //above is string array oArray[0]='1.23', //below is number array w2[0]=1.23 //9807271130 //var w2=[0,0]; 9906181215 change to next var w2=[0]; for(w0=0;w0<oArray.length;w0++) { //9806101704 //parseFloat() convert string number // to number number. w1=parseFloat(oArray[w0]); //no more number? done if(isNaN(w1))break; //9806101714 w2[w0]=w1; //create number array. } return w2; } // function readdata() 9806101214 //from function update0() //9802171511 function whyf(arg1) { //9906171609 if(arg1==1) { QGspanAlert1.innerHTML='Majorization. ' +'Box 2 majorize box 1 [why] see <a href=#atQGboxb17>box 8</a> below.'; QGboxb18.value='' +'Why box 2 majorize box 1 ?\n' +'Box1 Seq=[5 3 3 2 2]\n' +'Box2 Seq=[5 4 3 2 1]\n' +'Gradual compare as following\n' +'1st 5f"5\n' +'2nd 5+3f"5+4\n' +'3rd 5+3+3f"5+4+3\n' +'4th 5+3+3+2f"5+4+3+2\n' +'5th 5+3+3+2+2=5+4+3+2+1\n' +'Box1 Seq always f"\n' +'Box2 Seq . This relation\n' +'is called Box 2 sequence\n' +' majorize box 1 sequence\n' +'Last line total sum equality\n' +'is required Please read The\n' +'Cauchy-Schwarz Master Class\n' +'ISBN 978-0-521-54677-5 page\n' +'191/192. 2010-06-17-16-21' } } function stndrdth(i0) //9802141617 begin js code { var sth = [" ", "st", "nd", "rd", "th"]; if(i0<0) return sth[0]; if(i0%100>3&&i0%100<21) return sth[4]; if(i0%10==1) return sth[1]; if(i0%10==2) return sth[2]; if(i0%10==3) return sth[3]; return sth[4]; } //function stndrdth(i0) //9802141626 done js code //2010-06-20-22-58 include bye09() //to jsmajor2.htm 9906202258 // /** 2009-06-12-15-31 start Javascript use float number for calculation. Answer may contain long string of '000000' or '999999', for example 2.00000000000001 -25.999999999999996 On 2009-06-12-11-58 write a function function bye09(in09) //9806121158 To use this function, call as following coef2=bye09(coef2); where coef2 is a number. After call 2.00000000000001 change to 2 -25.999999999999996 change to -26 It is handy, you can use it too. If number is 1.23111111111 there is no change. function bye09(in09) handle output number for better looking. Do not call bye09() during calculation, that is just slow down process. If expect answer to be irrational Do not call bye09(). If expect answer to be integer or short decimal number like 1.2. In these case you can call bye09(). 2009-06-12-15-40 stop 2009-06-17-19-05 bye09() process only one number at a time. If you have a string of several numbers. Do not put number string as input argument bye09() will send string back immediately. This file has sample code at time stamp '9806171858' 2009-06-17-19-09 stop /**/ function bye09(in09) //9806121158 { // in09 is input number, // for example 2.0000000001 // If input is not a number, return here if(isNaN(in09)) return in09; // where is decimal point '.' ? var dotLoc=(in09+'').indexOf('.',0); // no decimal point? integer? return. if(dotLoc==-1) return in09; // change input string to number in09=parseFloat(in09); // if number has '000000' ? var zeroLoc=(in09+'').indexOf('000000',dotLoc); // find '000000' ,cut start '000000' if(zeroLoc>0) //9806121216 return parseFloat((in09+'').substring(0,zeroLoc)); //9806121209 // if number has '999999' ? var nineLoc=(in09+'').indexOf('999999',dotLoc); // find '999999' , cut start '999999' if(nineLoc>0) //9806121223 here { // copy number left to '999999' //but immediately neighbor digit not copy // nineLoc-1 because '999999' need increase // digit by one. //If input is 1.23999999999 copy only 1.2 //not copy 1.23 , because 3 will change to 4. var nine0=(in09+'').substring(0,nineLoc-1); //between '.' and '999999' has other digit. if(nineLoc-dotLoc>1) //9806121220 { //neighbor digit add one, paste to end nine0+=(parseInt((in09+'').charAt(nineLoc-1))+1); } else //between '.' and '999999' no other digit. {// modified number is an integer nine0=parseInt(nine0); //9806121313 if(nine0<0)nine0--; // negative integer minus one else nine0++; //9806121314 positive integer add one } return parseFloat(nine0); //9806121213 return new number } // no '000000' no '999999' return original form return parseFloat(in09); //9806121224 } //function bye09(in09) //--> </SCRIPT> <hr><br> Javascript index <br> <a href=http://freeman2.com/jsindex2.htm> http://freeman2.com/jsindex2.htm </a> &nbsp; <a href=jsindex2.htm> local </a> <!--9810201450 add local link--> <br> Save graph code to same folder as htm files. <br> <a href=http://freeman2.com/jsgraph2.js> http://freeman2.com/jsgraph2.js </a> &nbsp; <a href=jsgraph2.js> local </a> <br> <script src="jslist1e.js" language="javascript"></script> <br> <br> <!--9906191358--> File name jsmajor2.htm means <br> JavaScript MAJORization English (2) <br> Chinese version file name will be <br> jsmajor1.htm GWSéwc– (*Q§cSéwc–) <br> <br> This page, majorization matrix <br> <a href=http://freeman2.com/jsmajor2.htm> http://freeman2.com/jsmajor2.htm </a> <br> First Upload 2010-06-29 <br> (related material see <a href=http://freeman2.com/tute0047.htm>tute0047.htm</a>)<!--9906191402--> <br> <br> Thank you for visiting Freeman's page.0 <br> Freeman 02010-06-29-13-10 <br> </BODY> </HTML> <!-- 99,06,16,19,07 create matMVecf.htm 99,06,17,21,43 save matMVecf.htm as jsmajor2.htm -->