我从问题here找到了一个javascript决定因素算法。我的应用似乎工作正常,只要矩阵是2x2。除此之外的任何事情,算法都不能正确计算行列式。
我尝试逐行调试,看来,某些“if”条件未得到满足。
这是我的整个脚本。您可以向下滚动到底部,找到calcRec函数并查找“//以下两个条件都未满足”的注释。这是我无法理解问题的部分。您可以找到插件here
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.matrix = [[{"val":0},{"val":1}],[{"val":3},{"val":4}]]
var rowLength = $scope.matrix[0].length;
var colLength = $scope.matrix.length;
var addRow = function()
{
this.rowLength = $scope.matrix[0].length;
this.colLength = $scope.matrix.length;
var array = [];
for(var i=0;i<this.rowLength;i++)
{
array.push({"val":0});
}
$scope.matrix.push(array);
};
var addCol = function()
{
this.rowLength = $scope.matrix[0].length;
this.colLength = $scope.matrix.length;
for(var i=0;i<this.colLength;i++)
{
$scope.matrix[i].push({"val":0});
}
};
var removeRow = function()
{
this.rowLength = $scope.matrix[0].length;
this.colLength = $scope.matrix.length;
$scope.matrix.splice(this.colLength-1,1);
};
var removeCol = function()
{
this.rowLength = $scope.matrix[0].length;
this.colLength = $scope.matrix.length;
for(var i=0;i<this.colLength;i++)
{
$scope.matrix[i].splice(this.rowLength-1,1);
}
};
$scope.increaseSize = function()
{
addCol();
addRow();
$scope.updateDeterminant();
};
$scope.decreaseSize = function()
{
removeCol();
removeRow();
$scope.updateDeterminant();
};
$scope.updateDeterminant = function()
{
var tempMatrix = $scope.matrix;
var k = this.rowLength;
$scope.determinantValue = calcRec(tempMatrix);
//To be continued.
};
var calcRec = function(A)
{
var s;
var k = this.rowLength;
var det = 0;
if (A.length == 1)
{ //bottom case of the recursive function
return A[0][0].val;
}
if (A.length == 2)
{
det = A[0][0].val * A[1][1].val - A[1][0].val * A [0][1].val;
return det;
}
for (var i = 0; i < this.k; i++)
{
//creates smaller matrix- values not in same row, column
var smaller = new Array(A.length - 1);
for (var h = 0; h < smaller.length; h++)
{
smaller[h] = new Array(A.length - 1);
}
for (var a = 1; a < A.length; a++)
{
for (var b = 0; b < A.length; b++)
{
//Neither of the below 2 conditions are fulfilled.
if (b<i)
{
smaller[a-1][b].val = A[a][b].val;
}
else if(b>i)
{
smaller[a - 1][b - 1].val = A[a][b].val;
}
}
}
if (i % 2 === 0) {
s = 1;
} else {
s = -1;
}
det += s * A[0][i].val * (calcRec(smaller));
}
return (calcRec(A));
}
});
我的HTML代码在这里,而不是你需要它
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js" data-semver="1.2.16"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<span ng-repeat="rows in matrix track by $index"><br>
<span ng-repeat="element in rows track by $index">
<textarea ng-change="updateDeterminant()" ng-model="matrix[$parent.$index] [$index].val" rows="1" cols="1"></textarea>
</span>
</span><br>
<button ng-click="increaseSize()">Increase Size</button><br>
<button ng-click="decreaseSize()">Decrease Size</button>
Determinant is {{determinantValue}}
</body>
</html>
答案 0 :(得分:0)
在第98行,您初始化一个未定义的数组
smaller[h] = new Array(A.length - 1);
原因很明显
smaller[a-1][b].val
抛出错误,该错误表示&#34; TypeError:无法设置属性&#39; val&#39;未定义&#34;
答案 1 :(得分:0)
解决了它。我创建较小矩阵的方式存在问题。
初始代码
var smaller = new Array(A.length - 1);
for (var h = 0; h < smaller.length; h++)
{
smaller[h] = new Array(A.length - 1);
}
更正后的代码
var smaller = [];
for(var newvar=0;newvar<A.length-1;newvar++)
{
var array = []
for(var m=0;m<A.length-1;m++)
{
array.push({"val":0});
}
smaller.push(array);
}
事实证明,当你在javascript中处理数组时需要小心。这是我的plunk。它现在适用于所有nxn矩阵。