Skip to content

Commit 71c6cb8

Browse files
committed
Add triagularity tests
1 parent 2bf99b3 commit 71c6cb8

File tree

4 files changed

+155
-5
lines changed

4 files changed

+155
-5
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ Test whether the pairwise inner product of all column pairs is less than `tol`.
4949
#### `orthogonal( a [, tol], [, onFalse] )`
5050
Checks for squareness, column normality, and pair-wise column orthogonality to check if the matrix is orthogonal. `tol` is passed to the `columnsNormal` and `columnsOrthogonal`
5151

52+
#### `upperTriangular( a, [, tol] [, onFalse] )`
53+
Check whether all entries below the diagonal are within `tol` of zero. Works on tall and wide two-dimensional ndarrays.
54+
55+
#### `lowerTriangular( a, [, tol] [, onFalse] )`
56+
Check whether all entries above the diagonal are within `tol` of zero. Works on tall and wide two-dimensional ndarrays.
5257

5358
### TO DO:
5459

55-
- `upperTriangular( a, b, [, tol] [, onFalse] )`
56-
- `lowerTriangular( a, b, [, tol] [, onFalse] )`
5760
- `diagonal( a, [, tol] [, onFalse] )`
5861
- separate column methods to allow testing of specific pair-wise columns (or maybe that's simple enough that it's unnecessary)
5962

index.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,57 @@ var orthogonal = function(a, tol, onFalse) {
164164
return true;
165165
};
166166

167+
var upperTriangular = function(a, tol, onFalse) {
168+
if( tol === undefined ) {
169+
tol = 0.0;
170+
}
171+
172+
if( a.dimension !== 2 ) {
173+
output(onFalse,'upperTriangular():: can only test for triangularity of two-dimensional arrays');
174+
return false;
175+
}
176+
177+
for(var i=1; i<a.shape[0]; i++) {
178+
for(var j=0; j<i; j++) {
179+
if( Math.abs(a.get(i,j)) > tol ) {
180+
output(onFalse,'upperTriangular():: A[' + i + ',' + j + '] (= ' + a.get(i,j) + ') > ' + tol + '.');
181+
return false;
182+
}
183+
}
184+
}
185+
186+
return true;
187+
};
188+
189+
var lowerTriangular = function(a, tol, onFalse) {
190+
if( tol === undefined ) {
191+
tol = 0.0;
192+
}
193+
194+
if( a.dimension !== 2 ) {
195+
output(onFalse,'lowerTriangular():: can only test for triangularity of two-dimensional arrays');
196+
return false;
197+
}
198+
199+
for(var i=0; i<a.shape[0]; i++) {
200+
for(var j=i+1; j<a.shape[1]; j++) {
201+
if( Math.abs(a.get(i,j)) > tol ) {
202+
output(onFalse,'lowerTriangular():: A[' + i + ',' + j + '] (= ' + a.get(i,j) + ') > ' + tol + '.');
203+
return false;
204+
}
205+
}
206+
}
207+
208+
return true;
209+
};
210+
167211

168212

169213
exports.approximatelyEqual = approximatelyEqual;
170214
exports.symmetric = symmetric;
171215
exports.columnsOrthogonal = columnsOrthogonal;
172216
exports.columnsNormalized = columnsNormalized;
173217
exports.orthogonal = orthogonal;
174-
//exports.upperTriangular = upperTriangular;
175-
//exports.lowerTriangular = lowerTriangular;
218+
exports.upperTriangular = upperTriangular;
219+
exports.lowerTriangular = lowerTriangular;
176220
//exports.diagonal = diagonal;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ndarray-tests",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Test numerical properties of ndarrays",
55
"main": "index.js",
66
"scripts": {

test/test.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,106 @@ describe('orthogonal',function() {
176176
});
177177

178178

179+
180+
describe('upperTriangular',function() {
181+
182+
var upperTall, almostUpperTall, upperWide, almostUpperWide;
183+
184+
beforeEach(function() {
185+
upperTall = ndarray([1,2,3,0,4,5,0,0,6,0,0,0],[4,3]);
186+
almostUpperTall = ndarray([1,2,3,1e-4,4,5,0,0,6,0,0,0],[4,3]);
187+
upperWide = ndarray([1,2,3,4,0,5,6,7,0,0,8,9],[3,4]);
188+
almostUpperWide = ndarray([1,2,3,4,1e-4,5,6,7,0,0,8,9],[3,4]);
189+
});
190+
191+
describe('tall matrices',function() {
192+
it('true if exactly upper triangular',function() {
193+
assert.isTrue( ndtest.upperTriangular(upperTall) );
194+
});
195+
196+
it('true if within tolerance',function() {
197+
assert.isTrue( ndtest.upperTriangular(almostUpperTall,1e-2) );
198+
});
199+
200+
it('false if no tolerance provided and not exactly upper triangular',function() {
201+
assert.isFalse( ndtest.upperTriangular(almostUpperTall) );
202+
});
203+
204+
it('false if not within tolerance',function() {
205+
assert.isFalse( ndtest.upperTriangular(almostUpperTall,1e-8) );
206+
});
207+
});
208+
209+
describe('wide matrices',function() {
210+
it('true if exactly upper triangular',function() {
211+
assert.isTrue( ndtest.upperTriangular(upperWide) );
212+
});
213+
214+
it('true if within tolerance',function() {
215+
assert.isTrue( ndtest.upperTriangular(almostUpperWide,1e-2) );
216+
});
217+
218+
it('false if no tolerance provided and not exactly upper triangular',function() {
219+
assert.isFalse( ndtest.upperTriangular(almostUpperWide) );
220+
});
221+
222+
it('false if not within tolerance',function() {
223+
assert.isFalse( ndtest.upperTriangular(almostUpperWide,1e-8) );
224+
});
225+
});
226+
227+
});
228+
229+
230+
231+
describe('lowerTriangular',function() {
232+
233+
var lowerTall, almostLowerTall, lowerWide, almostLowerWide;
234+
235+
beforeEach(function() {
236+
lowerTall = ndarray([1,0,0,1,1,0,1,1,1,1,1,1],[4,3]);
237+
almostLowerTall = ndarray([1,1e-4,0,1,1,0,1,1,1,1,1,1],[4,3]);
238+
lowerWide = ndarray([1,0,0,0,2,3,0,0,4,5,6,0,7,8,9,10],[3,4]);
239+
almostLowerWide = ndarray([1,1e-4,0,0,2,3,0,0,4,5,6,0,7,8,9,10],[3,4]);
240+
});
241+
242+
describe('tall matrices',function() {
243+
it('true if exactly lower triangular',function() {
244+
assert.isTrue( ndtest.lowerTriangular(lowerTall) );
245+
});
246+
247+
it('true if within tolerance',function() {
248+
assert.isTrue( ndtest.lowerTriangular(almostLowerTall,1e-2) );
249+
});
250+
251+
it('false if no tolerance provided and not exactly lower triangular',function() {
252+
assert.isFalse( ndtest.lowerTriangular(almostLowerTall) );
253+
});
254+
255+
it('false if not within tolerance',function() {
256+
assert.isFalse( ndtest.lowerTriangular(almostLowerTall,1e-8) );
257+
});
258+
});
259+
260+
describe('wide matrices',function() {
261+
it('true if exactly lower triangular',function() {
262+
assert.isTrue( ndtest.lowerTriangular(lowerWide) );
263+
});
264+
265+
it('true if within tolerance',function() {
266+
assert.isTrue( ndtest.lowerTriangular(almostLowerWide,1e-2) );
267+
});
268+
269+
it('false if no tolerance provided and not exactly lower triangular',function() {
270+
assert.isFalse( ndtest.lowerTriangular(almostLowerWide) );
271+
});
272+
273+
it('false if not within tolerance',function() {
274+
assert.isFalse( ndtest.lowerTriangular(almostLowerWide,1e-8) );
275+
});
276+
});
277+
278+
279+
});
280+
281+

0 commit comments

Comments
 (0)