正剧开始:
星历2016年05月09日 10:58:13, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[风险与决策]。
<span style="font-size:18px;">0.30 , 0.50 , 0.20 , * 12.00 , 8.00 , 3.00 , 5.00 , 6.00 , 3.00 , -4.00 , -1.00 , 2.00 , = 5.30 , 5.20 , 2.80 , if (1) { var analyze = new DataAnalyze(); var matrix = new Matrix(); var m1 = [[12,5,-4],[8,6,-1],[3,3,2]]; var m2 = [[0.3, 0.5, 0.2]]; m1 = matrix.transposition(m1); var m3 = matrix.multiple(m2, m1); matrix.print(m2); hint('*'); matrix.print(m1); hint('='); matrix.print(m3); }</span>
<span style="font-size:18px;">0.01 , 0.25 , 0.74 , * 3800.00 , 62000.00 , 60000.00 , 3800.00 , 2000.00 , 10000.00 , 3800.00 , 2000.00 , 0.00 , = 3800.00 , 2600.00 , 3100.00 , if (1) { var analyze = new DataAnalyze(); var matrix = new Matrix(); var m1 = [[3800, 62000, 60000], [3800, 2000, 10000],[3800, 2000, 0]]; var m2 = [[0.01, 0.25, 0.74]]; var m3 = matrix.multiple(m2, m1); matrix.print(m2); hint('*'); matrix.print(m1); hint('='); matrix.print(m3); }</span>
<span style="font-size:18px;">0.60 , 0.40 , * -8.00 , -2.00 , 0.00 , 0.00 , -3.00 , -4.00 , = -4.80 , -2.40 , -1.60 , if (1) { var analyze = new DataAnalyze(); var matrix = new Matrix(); //损失函数,单位:万元 var m1 = [[-8, -2, 0], [0, -3, -4]]; var m2 = [[0.6, 0.4]]; var m3 = matrix.multiple(m2, m1); matrix.print(m2); hint('*'); matrix.print(m1); hint('='); matrix.print(m3); }</span>
<span style="font-size:18px;">//进货17台收益最大 0.05 , 0.10 , 0.20 , 0.40 , 0.20 , 0.05 , * 3000.00 , 2200.00 , 1400.00 , 600.00 , -200.00 , -1000.00 , 3000.00 , 3200.00 , 2400.00 , 1600.00 , 800.00 , 0.00 , 3000.00 , 3200.00 , 3400.00 , 2600.00 , 1800.00 , 1000.00 , 3000.00 , 3200.00 , 3400.00 , 3600.00 , 2800.00 , 2000.00 , 3000.00 , 3200.00 , 3400.00 , 3600.00 , 3800.00 , 3000.00 , 3000.00 , 3200.00 , 3400.00 , 3600.00 , 3800.00 , 4000.00 , = 3000.00 , 3150.00 , 3200.00 , 3050.00 , 2500.00 , 1750.00 , if (1) { var analyze = new DataAnalyze(); var matrix = new Matrix(); //收益函数,单位:元 var m1 = [], vector1 = []; //6种销售量 for (var i = 15; i <= 20; i++) { //销售量 vector1 = []; for (var j = 15; j <= 20; j++) { //进货量 vector1.push(1000*Math.min(i, j)-800*j); } m1.push(vector1); } var m2 = [[0.05, 0.10, 0.20, 0.40, 0.20, 0.05]]; var m3 = matrix.multiple(m2, m1); matrix.print(m2); hint('*'); matrix.print(m1); hint('='); matrix.print(m3); } </span>
<span style="font-size:18px;">//进货100本书收益最大 0.20 , 0.40 , 0.30 , 0.10 , * 100.00 , 0.00 , -100.00 , -200.00 , 100.00 , 200.00 , 100.00 , 0.00 , 100.00 , 200.00 , 300.00 , 200.00 , 100.00 , 200.00 , 300.00 , 400.00 , = 100.00 , 160.00 , 140.00 , 60.00 , if (1) { var analyze = new DataAnalyze(); var matrix = new Matrix(); //收益函数,单位:元 var m1 = [], vector1 = []; //4种销售量 for (var i = 50; i <= 200; i+=50) { //一个月内销售量 vector1 = []; for (var j = 50; j <= 200; j+=50) { //进货量 vector1.push(6*Math.min(i, j)-4*j + 2*(j-Math.min(i,j))); } m1.push(vector1); } var m2 = [[0.2, 0.4, 0.3, 0.1]]; var m3 = matrix.multiple(m2, m1); matrix.print(m2); hint('*'); matrix.print(m1); hint('='); matrix.print(m3); }</span>
<span style="font-size:18px;">0.90 , 0.10 , * 1300.00 , 1500.00 , 1300.00 , 100.00 , = 1300.00 , 1360.00 , if (1) { var analyze = new DataAnalyze(); var matrix = new Matrix(); //收益函数,单位:元 var m1 = [[1300, 1300],[1500, 100]]; var m2 = [[0.9,0.1]]; m1 = matrix.transposition(m1); var m3 = matrix.multiple(m2, m1); matrix.print(m2); hint('*'); matrix.print(m1); hint('='); matrix.print(m3); }</span>
<span style="font-size:18px;">9.050,9.002,9.000,9.000,9.000,9.000,9.000,9.000,9.000,9.000,9.000 9.200,8.920,8.822,8.788,8.776,8.771,8.770,8.770,8.769,8.769,8.769 if (1) { var analyze = new DataAnalyze(); var matrix = new Matrix(); //马尔可夫转移概率矩阵 //第一种选择 var m1 = [[0.95, 0.05],[0.9, 0.1]]; var m2 = [[1, 0]]; var m3 = matrix.multiple(m2, m1); var profitA = []; var tmp = 0; tmp = matrix.multiple(m3, [[10], [-9]]); profitA.push(tmp[0][0].toFixed(3)); for (var i = 0; i < 10; i++) { m3 = matrix.multiple(m3, m1); tmp = matrix.multiple(m3, [[10], [-9]]); profitA.push(tmp[0][0].toFixed(3)); } hint(profitA); //第二种选择 m1 = [[0.95, 0.05], [0.6, 0.4]]; m2 = [[1, 0]]; m3 = matrix.multiple(m2, m1); var profitB = []; tmp = 0; tmp = matrix.multiple(m3, [[10], [-6]]); profitB.push(tmp[0][0].toFixed(3)); for (var i = 0; i < 10; i++) { m3 = matrix.multiple(m3, m1); tmp = matrix.multiple(m3, [[10], [-6]]); profitB.push(tmp[0][0].toFixed(3)); } hint(profitB); }</span>
<span style="font-size:18px;">1.000 , 0.000 , * 0.950 , 0.050 , 0.600 , 0.400 , = 0.950 , 0.050 , * 0.950 , 0.050 , 0.600 , 0.400 , = 0.932 , 0.068 , * 0.950 , 0.050 , 0.600 , 0.400 , = 0.926 , 0.074 , * 0.950 , 0.050 , 0.600 , 0.400 , = 0.924 , 0.076 , * 0.950 , 0.050 , 0.600 , 0.400 , = 0.923 , 0.077 , if (1) { var analyze = new DataAnalyze(); var matrix = new Matrix(); //马尔可夫转移概率矩阵 var m1 = [[0.95, 0.05],[0.6, 0.4]]; var m2 = [[1, 0]]; var m3 = matrix.multiple(m2, m1); matrix.print(m2); hint('*'); matrix.print(m1); hint('='); matrix.print(m3); for (var i = 0; i < 4; i++) { m2 = m3; hint('*'); matrix.print(m1); hint('='); m3 = matrix.multiple(m2, m1); matrix.print(m3); } }</span>
<span style="font-size:18px;">/** * @usage 矩阵类 * @author mw * @date 2016年03月24日 星期四 14:51:45 * @param * @return * */ function Matrix() { this.delta = function(matrix) { var size = matrix.length; if (!matrix[0].length) { return 0; } else { if (matrix[0].length != size) { return 0; } if (size < 1) { return 0; } else if (size == 1) { return matrix[0][0]; } else { var d = 0; var subMatrix = []; for (var i = 0; i < size; i++) { subMatrix = this.subSet(matrix, i, 0); d += Math.pow(-1, i)*matrix[i][0]*this.delta(subMatrix); } return d; } } } this.subSet = function(matrix, row, col) { var size = matrix.length; if (!matrix[0].length) { return []; } else { if (matrix[0].length != size) { return []; } var newMatrix = new Array(); var rowArray= new Array(); for (var i = 0; i < size; i++) { if (i == row) { continue; } else { rowArray = []; for (var j = 0; j < size; j++) { if (j == col) { continue; } else { rowArray.push(matrix[i][j]); } } newMatrix.push(rowArray) } } return newMatrix; } } this.identityMatrix = function(rank) { var newMatrix = new Array(); var rowArray= new Array(); for (var i = 0; i < rank; i++) { rowArray = []; for (var j = 0; j < rank; j++) { if (j == i) { rowArray.push(1); } else { rowArray.push(0); } } newMatrix.push(rowArray); } return newMatrix; } //建立row*col空矩阵 this.zeroMatrix = function(row, col) { var newMatrix = new Array(); var rowArray= new Array(); for (var i = 0; i < row; i++) { rowArray = []; for (var j = 0; j < col; j++) { rowArray.push(0); } newMatrix.push(rowArray); } return newMatrix; } //把从1-n的n个数按顺序放到矩阵中 this.sequenceMatrix = function(rank) { var newMatrix = new Array(); var rowArray= new Array(); var count = 0; for (var i = 0; i < rank; i++) { rowArray = []; for (var j = 0; j < rank; j++) { count++; rowArray.push(count); } newMatrix.push(rowArray); } return newMatrix; } this.print = function(matrix) { var size = matrix.length; if (!matrix[0].length) { return []; } else { var rSize = matrix[0].length; var s = ''; for (var i = 0; i < size; i++) { s = ''; for (var j = 0; j < rSize; j++) { s += matrix[i][j].toFixed(3) + ' , '; } document.write(s+'<br/>'); } } } this.deepCopy = function(matrix) { var size = matrix.length; if (!matrix[0].length) { return this.zeroMatrix(1, 1); } else { var rSize = matrix[0].length; var newMatrix = new Array(); var rowArray= new Array(); for (var i = 0; i < size; i++) { rowArray = []; for (var j = 0; j < rSize; j++) { rowArray.push(matrix[i][j]); } newMatrix.push(rowArray); } return newMatrix; } } /** * @usage 矩阵的转置 * @author mw * @date 2016年05月09日 星期一 08:21:34 * @param * @return * */ this.transposition = function(matrix) { var row = matrix.length; var col = matrix[0].length; if (row > 0 && col > 0) { var transMatrix = this.zeroMatrix(col, row); for (var i = 0; i < row; i++) { for (var j = 0; j < col; j++) { transMatrix[j][i] = matrix[i][j]; } } return transMatrix; } return this.zeroMatrix(1, 1); } /** * @usage 矩阵的乘法 * @author mw * @date 2016年05月02日 星期一 10:44:46 * @param * @return * */ this.multiple = function(matrix1, matrix2) { var row1 = matrix1.length; var col1 = matrix1[0].length; var row2 = matrix2.length; var col2 = matrix2[0].length; var A = [row1, col1, row2, col2]; if (col1 != row2) { return this.zeroMatrix(1, 1); } var mulResult = this.zeroMatrix(row1, col2); for (var i = 0; i < row1; i++) { for (var j = 0; j < col2; j++) { for (var k = 0; k < row2; k++) { mulResult[i][j] += matrix1[i][k]*matrix2[k][j]; } } } return mulResult; } this.add = function(matrix1, matrix2) { var row1 = matrix1.length; var col1 = matrix1[0].length; var row2 = matrix2.length; var col2 = matrix2[0].length; if (row1 != row2 || col1 != col2) { return this.zeroMatrix(1, 1); } var addResult = this.zeroMatrix(row1, col1); for (var i = 0; i < row1; i++) { for (var j = 0; j < col1; j++) { addResult[i][j] = matrix1[i][j]+matrix2[i][j]; } } return addResult; } //二阶矩阵的逆 this.invOfRank2 = function(matrix) { var d = this.delta(matrix); if (d != 0) { return [[matrix[1][1]/d, -matrix[0][1]/d], [-matrix[1][0]/d, matrix[0][0]/d]]; } else { return this.zeroMatrix(1, 1); } } //二阶矩阵的特征值 this.eigenvalue = function(matrix) { var a = matrix[0][0]; var b = matrix[0][1]; var c = matrix[1][0]; var d = matrix[1][1]; var judge = (a+d)*(a+d)-4*(a*d-b*c); if ( judge >= 0) { var lambda_1 = 0.5*((a+d)-Math.sqrt(judge)); var lambda_2 = 0.5*((a+d)+Math.sqrt(judge)); } //特征向量有很多组,这里随便取一组 //特征值放在第一行,然后是每个特征值对应的一个特征向量 if (b != 0) { return [[lambda_1, lambda_2], [1, (lambda_1-a)/b], [b/(lambda_2-a), 1]]; } else if (c != 0) { return [[lambda_1, lambda_2], [1, c/(lambda_1-d)], [(lambda_2-d)/c, 1]]; } return this.zeroMatrix(1, 1); } } </span>