正剧开始:
星历2016年03月24日 14:45:50, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[二次函数 小结与复习题]。
话说最近[人叫板老师]老爱让[机器小伟]解方程,本来吧这也没什么,毕竟用python的numpy库可以轻松搞定。
但是呢,[工程师阿伟]却嫌那个numpy要加载的东西太多,也不知道里面都有些什么,反正附带的文件相当的多。
而且最重要的是,它是用python写的,所以阿伟就想用JS自己写一个,当然,这只能慢慢来。
考虑到[人叫板老师]现在的方程也就是一些二元、三元的,好像用行列式也可以解决了。
于是,阿伟终于忍不住了,还是走上了从头创建矩阵类的道路。
<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; } //把从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(2) + ' , '; } document.write(s+'<br/>'); } } } }</span>
<span style="font-size:18px;"> var matrix = new Matrix(); var m = matrix.identityMatrix(5); var m2 = [[1,5,7],[2,6,-3],[99,23,5]]; matrix.print(m); document.write(matrix.delta(m)+'<br/>'); matrix.print(m2); document.write(matrix.delta(m2)+'<br/>'); var m3 = [[4,5,7,3],[4,-2,6,-1],[5,8,5,3],[14,67,4,2]]; matrix.print(m3); document.write(matrix.delta(m3)+'<br/>');</span>
还是来看看这次[人叫板老师]出的题吧。
也不知道能不能用上这个工具。
<span style="font-size:18px;"> var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0, 180, 1); var scaleX = 2*r, scaleY = 2*r; var spaceX = 2, spaceY = 10; var xS = -10, xE = 10; var yS = -100, yE = 100; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); var transform = new Transform(); var a = [], b = [], c = [], d = []; for (var x = xS; x <= xE; x+=spaceX/2) { a.push([x, x*x-2*x-3]); b.push([x, 1+6*x-x*x]); c.push([x, 0.5*x*x+2*x+1]); d.push([x, -1/4*x*x+x-4]); } a = transform.scale(transform.translate(a, 0, 0), scaleX/spaceX, scaleY/spaceY); var tmp = [].concat(a); shape.pointDraw(tmp, 'red'); tmp = [].concat(a); shape.multiLineDraw(tmp, 'pink'); plot.setFillStyle('red'); plot.fillText('y = x*x-2*x-3', 100, -20, 200); b = transform.scale(transform.translate(b, 0, 0), scaleX/spaceX, scaleY/spaceY); tmp = [].concat(b); shape.pointDraw(tmp, 'blue'); tmp = [].concat(b); shape.multiLineDraw(tmp, '#22CCFF'); plot.setFillStyle('blue'); plot.fillText('y=1+6*x-x*x', 100, -40, 200); c = transform.scale(transform.translate(c, 0, 0), scaleX/spaceX, scaleY/spaceY); tmp = [].concat(c); shape.pointDraw(tmp, 'green'); tmp = [].concat(c); shape.multiLineDraw(tmp, '#CCFF22'); plot.setFillStyle('green'); plot.fillText('y=0.5*x*x+2*x+1', 100, -60, 200); d = transform.scale(transform.translate(d, 0, 0), scaleX/spaceX, scaleY/spaceY); tmp = [].concat(d); shape.pointDraw(tmp, 'orange'); tmp = [].concat(d); shape.multiLineDraw(tmp, '#CC8800'); plot.setFillStyle('orange'); plot.fillText('y=-1/4*x*x+x-4', 100, -80, 200); </span>
先用行列式解出a, b,c
<span style="font-size:18px;"> var matrix = new Matrix(); var matrixArray = new Array(); var rowArray = new Array(); var ma, mb, mc; var a = [[-3,2],[-1,-1],[1,3]]; matrixArray = [[9,-3,1],[1,-1,1],[1,1,1]]; ma = matrix.deepCopy(matrixArray); for (var i = 0; i < 3; i++) { ma[i][0] = a[i][1]; } mb = matrix.deepCopy(matrixArray); for (var i = 0; i < 3; i++) { mb[i][1] = a[i][1]; } mc = matrix.deepCopy(matrixArray); for (var i = 0; i < 3; i++) { mc[i][2] = a[i][1]; } var d, da, db, dc; d = matrix.delta(matrixArray); da = matrix.delta(ma); db = matrix.delta(mb); dc = matrix.delta(mc); matrix.print(matrixArray); document.write(d+'<br/>'); matrix.print(ma); document.write(da+'<br/>'); matrix.print(mb); document.write(db+'<br/>'); matrix.print(mc); document.write(dc+'<br/>'); var s = 'a = '+ (da/d).toFixed(2)+', b= '+(db/d).toFixed(2)+', c = ' + (dc/d).toFixed(2); document.write(s+'<br/>');</span>
<span style="font-size:18px;"> var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0, 180, 1.5); var scaleX = 2*r, scaleY = 2*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 20; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); var transform = new Transform(); var a = [], b = [], c = [], d = []; for (var x = xS; x <= xE; x+=spaceX/5) { a.push([x, 0.88*x*x+2*x+0.13]); } a = transform.scale(transform.translate(a, 0, 0), scaleX/spaceX, scaleY/spaceY); var tmp = [].concat(a); shape.pointDraw(tmp, 'red'); tmp = [].concat(a); shape.multiLineDraw(tmp, 'pink'); plot.setFillStyle('red'); plot.fillText('y = 0.88*x*x+2*x+0.13', 100, -20, 200);</span>
<span style="font-size:18px;"> var matrix = new Matrix(); var matrixArray = new Array(); var rowArray = new Array(); var ma, mb, mc; var a = [[-0.5,0],[1.5,0],[0,-5]]; for (var i = 0; i < 3; i++) { matrixArray.push([a[i][0]*a[i][0], a[i][0], 1]); } ma = matrix.deepCopy(matrixArray); for (var i = 0; i < 3; i++) { ma[i][0] = a[i][1]; } mb = matrix.deepCopy(matrixArray); for (var i = 0; i < 3; i++) { mb[i][1] = a[i][1]; } mc = matrix.deepCopy(matrixArray); for (var i = 0; i < 3; i++) { mc[i][2] = a[i][1]; } var d, da, db, dc; d = matrix.delta(matrixArray); da = matrix.delta(ma); db = matrix.delta(mb); dc = matrix.delta(mc); matrix.print(matrixArray); document.write(d+'<br/>'); matrix.print(ma); document.write(da+'<br/>'); matrix.print(mb); document.write(db+'<br/>'); matrix.print(mc); document.write(dc+'<br/>'); var s = 'a = '+ (da/d).toFixed(2)+', b= '+(db/d).toFixed(2)+', c = ' + (dc/d).toFixed(2); document.write(s+'<br/>'); </span>
<span style="font-size:18px;">0.25 , -0.50 , 1.00 , 2.25 , 1.50 , 1.00 , 0.00 , 0.00 , 1.00 , 1.5 0.00 , -0.50 , 1.00 , 0.00 , 1.50 , 1.00 , -5.00 , 0.00 , 1.00 , 10 0.25 , 0.00 , 1.00 , 2.25 , 0.00 , 1.00 , 0.00 , -5.00 , 1.00 , -10 0.25 , -0.50 , 0.00 , 2.25 , 1.50 , 0.00 , 0.00 , 0.00 , -5.00 , -7.5 a = 6.67, b= -6.67, c = -5.00</span>
<span style="font-size:18px;"> var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0, 180, 1.5); var scaleX = 2*r, scaleY = 2*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 20; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); var transform = new Transform(); var a = [], b = [], c = [], d = []; for (var x = xS; x <= xE; x+=spaceX/5) { a.push([x, 6.67*x*x-6.67*x-5]); } a = transform.scale(transform.translate(a, 0, 0), scaleX/spaceX, scaleY/spaceY); var tmp = [].concat(a); shape.pointDraw(tmp, 'red'); tmp = [].concat(a); shape.multiLineDraw(tmp, 'pink'); plot.setFillStyle('red'); plot.fillText('y = 6.67*x*x-6.67*x-5', 100, -20, 200);</span>
<span style="font-size:18px;"> var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0, 180, 1.5); var scaleX = 2*r, scaleY = 2*r; var spaceX = 5, spaceY = 150; var xS = -10, xE = 20; var yS = -10, yE = 800; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); var transform = new Transform(); var a = [], b = [], c = [], d = []; for (var x = xS; x <= xE; x+=spaceX/5) { a.push([x, 6.28*x*(18-x)]); } a = transform.scale(transform.translate(a, 0, 0), scaleX/spaceX, scaleY/spaceY); var tmp = [].concat(a); shape.pointDraw(tmp, 'red'); tmp = [].concat(a); shape.multiLineDraw(tmp, 'pink'); plot.setFillStyle('red'); plot.fillText('y = 6.28*x*(18-x)', 100, -20, 200);</span>
本节到此结束,欲知后事如何,请看下回分解。