Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 55 additions & 11 deletions Maths/RowEchelon.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,68 @@
// Set a tolerance value for floating-point comparisons
const tolerance = 0.000001

// Check if all the rows have same length of elements
/**
* Validates whether the given input is a valid two-dimensional rectangular matrix.
*
* Trace & Decisive State Transition:
* 1. Initial State: Validates outer structure. Input must be a non-empty array whose first row is an array.
* 2. Dimension Target: Establishes expected column count `numCols = matrix[0].length`.
* 3. Row Inspection & Decisive Transition: Iterates across row indices `i` (0 <= i < numRows).
* - Decisive failure: If any row `matrix[i]` is not an array or `matrix[i].length !== numCols`,
* the validation transitions immediately to `false` (early exit).
* - Decisive success: If every row matches `numCols`, the validation transitions to `true`.
*
* @param {any} matrix - Two-dimensional array candidate to validate.
* @returns {boolean} True if matrix is a valid rectangular 2D array; false otherwise.
*
* @example
* // Example 1: Small rectangular matrix (2 rows x 3 columns)
* // Input: [[1, 2, 3], [4, 5, 6]]
* // numCols = 3; row 0 length = 3; row 1 length = 3.
* // Output: true
* isMatrixValid([[1, 2, 3], [4, 5, 6]]) // => true
*
* @example
* // Example 2: 1x1 matrix (1 row x 1 column)
* // Input: [[42]]
* // numCols = 1; row 0 length = 1.
* // Output: true
* isMatrixValid([[42]]) // => true
*
* @example
* // Example 3: Zero-containing matrix (2 rows x 2 columns)
* // Input: [[0, 5], [0, 0]]
* // numCols = 2; row 0 length = 2; row 1 length = 2.
* // Output: true
* isMatrixValid([[0, 5], [0, 0]]) // => true
*
* @example
* // Example 4: Non-uniform / ragged matrix (decisive failure transition)
* // Input: [[1, 2], [3]]
* // numCols = 2; row 0 length = 2; row 1 length = 1 !== 2 (decisive transition -> false).
* // Output: false
* isMatrixValid([[1, 2], [3]]) // => false
*/
const isMatrixValid = (matrix) => {
let numRows = matrix.length
let numCols = matrix[0].length
for (let i = 0; i < numRows; i++) {
if (numCols !== matrix[i].length) {
return false
}
}

// Check for input other than a 2D matrix
// Check for input other than a non-empty 2D matrix
if (
!Array.isArray(matrix) ||
matrix.length === 0 ||
!Array.isArray(matrix[0])
) {
return false
}

const numRows = matrix.length
const numCols = matrix[0].length

for (let i = 0; i < numRows; i++) {
// Decisive transition: any row length deviation marks the matrix as invalid
if (!Array.isArray(matrix[i]) || matrix[i].length !== numCols) {
return false
}
}

return true
}

Expand Down Expand Up @@ -147,4 +191,4 @@ const rowEchelon = (matrix) => {
return result
}

export { rowEchelon }
export { rowEchelon, isMatrixValid }
116 changes: 115 additions & 1 deletion Maths/test/RowEchelon.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rowEchelon } from '../RowEchelon'
import { rowEchelon, isMatrixValid } from '../RowEchelon'
describe('Determinant', () => {
const tolerance = 0.000001
test.each([
Expand Down Expand Up @@ -87,3 +87,117 @@ describe('Determinant', () => {
expect(() => rowEchelon(matrix)).toThrowError(expected)
})
})

describe('isMatrixValid', () => {
test('validates small rectangular matrix fixture', () => {
const rectangularMatrix = [
[1, 2, 3],
[4, 5, 6]
]
expect(isMatrixValid(rectangularMatrix)).toBe(true)

const tallRectangularMatrix = [
[1, 2],
[3, 4],
[5, 6]
]
expect(isMatrixValid(tallRectangularMatrix)).toBe(true)
})

test('validates 1x1 matrix fixture', () => {
const singleElementMatrix = [[42]]
expect(isMatrixValid(singleElementMatrix)).toBe(true)

const singleZeroMatrix = [[0]]
expect(isMatrixValid(singleZeroMatrix)).toBe(true)
})

test('validates zero-containing matrix fixture', () => {
const zeroContainingMatrix = [
[0, 5],
[0, 0]
]
expect(isMatrixValid(zeroContainingMatrix)).toBe(true)

const allZeroRectangular = [
[0, 0, 0],
[0, 0, 0]
]
expect(isMatrixValid(allZeroRectangular)).toBe(true)
})

test('executes and asserts JSDoc documentation examples', () => {
// Example 1: Small rectangular matrix (2x3)
expect(
isMatrixValid([
[1, 2, 3],
[4, 5, 6]
])
).toBe(true)

// Example 2: 1x1 matrix
expect(isMatrixValid([[42]])).toBe(true)

// Example 3: Zero-containing matrix (2x2)
expect(
isMatrixValid([
[0, 5],
[0, 0]
])
).toBe(true)

// Example 4: Non-uniform / ragged matrix (decisive failure transition)
expect(isMatrixValid([[1, 2], [3]])).toBe(false)
})

test('rejects ragged matrices (decisive row length transition)', () => {
expect(
isMatrixValid([
[1, 2, 3],
[4, 5]
])
).toBe(false)
expect(
isMatrixValid([
[1, 2],
[3, 4, 5]
])
).toBe(false)
})

test('rejects non-matrix inputs and empty structures', () => {
expect(isMatrixValid([])).toBe(false)
expect(isMatrixValid(null)).toBe(false)
expect(isMatrixValid(undefined)).toBe(false)
expect(isMatrixValid('not a matrix')).toBe(false)
expect(isMatrixValid(123)).toBe(false)
expect(isMatrixValid([1, 2, 3])).toBe(false)
expect(isMatrixValid([[1, 2], 'not an array'])).toBe(false)
})

test('works consistently with rowEchelon on valid fixtures', () => {
// 1x1 matrix in rowEchelon
expect(rowEchelon([[42]])).toEqual([[1]])

// Rectangular 2x3 matrix in rowEchelon
const rect = [
[1, 2, 3],
[4, 5, 6]
]
const rectResult = rowEchelon(rect)
expect(rectResult.length).toBe(2)
expect(rectResult[0].length).toBe(3)
expect(rectResult[0][0]).toBeCloseTo(1)
expect(rectResult[1][0]).toBeCloseTo(0)

// Zero-containing matrix in rowEchelon
const zeroMat = [
[0, 2],
[3, 0]
]
const zeroResult = rowEchelon(zeroMat)
expect(zeroResult[0][0]).toBeCloseTo(1)
expect(zeroResult[1][0]).toBeCloseTo(0)
expect(zeroResult[1][1]).toBeCloseTo(1)
})
})
Loading