diff --git a/src/reimMatrix/__tests__/reimMatrixPhaseCorrection.test.ts b/src/reimMatrix/__tests__/reimMatrixPhaseCorrection.test.ts index b482f74c6..f1da85b32 100644 --- a/src/reimMatrix/__tests__/reimMatrixPhaseCorrection.test.ts +++ b/src/reimMatrix/__tests__/reimMatrixPhaseCorrection.test.ts @@ -16,6 +16,34 @@ test('reimMatrixPhaseCorrection: no correction when phi0 and phi1 are 0', () => expect(Array.from(result.im[1])).toStrictEqual(Array.from(data.im[1])); }); +test('reimMatrixPhaseCorrection: returns min and max values for the corrected data', () => { + const data = { + re: [Float64Array.from([1, 2, 3])], + im: [Float64Array.from([0, 0, 0])], + }; + + const result = reimMatrixPhaseCorrection(data, 0, 0); + + expect(result.minRe).toBe(1); + expect(result.maxRe).toBe(3); + expect(result.minIm).toBe(0); + expect(result.maxIm).toBe(0); +}); + +test('reimMatrixPhaseCorrection: tracks min and max for both components with phase rotation', () => { + const data = { + re: [Float64Array.from([1, 0, -1])], + im: [Float64Array.from([0, 1, 0])], + }; + + const result = reimMatrixPhaseCorrection(data, Math.PI / 2, 0); + + expect(result.minRe).toBeLessThanOrEqual(result.maxRe); + expect(result.minIm).toBeLessThanOrEqual(result.maxIm); + expect(result.re[0]).toHaveLength(3); + expect(result.im[0]).toHaveLength(3); +}); + test('reimMatrixPhaseCorrection: direction rows by default', () => { const data = { re: [Float64Array.from([1, 0, 0, 0])], diff --git a/src/reimMatrix/reimMatrixPhaseCorrection.ts b/src/reimMatrix/reimMatrixPhaseCorrection.ts index 6340b7e55..6e4a454cb 100644 --- a/src/reimMatrix/reimMatrixPhaseCorrection.ts +++ b/src/reimMatrix/reimMatrixPhaseCorrection.ts @@ -10,6 +10,13 @@ export interface ReimMatrixPhaseCorrectionOptions { direction?: 'rows' | 'columns'; } +export interface ReimMatrixPhaseCorrectionResult extends DataReImMatrix { + minRe: number; + maxRe: number; + minIm: number; + maxIm: number; +} + /** * Apply phase correction to a complex matrix along rows or columns. * All rows must have the same length. @@ -24,7 +31,7 @@ export function reimMatrixPhaseCorrection( phi0 = 0, phi1 = 0, options: ReimMatrixPhaseCorrectionOptions = {}, -): DataReImMatrix { +): ReimMatrixPhaseCorrectionResult { const { reverse = false, inPlace = false, direction = 'rows' } = options; phi0 = Number.isFinite(phi0) ? phi0 : 0; @@ -33,7 +40,16 @@ export function reimMatrixPhaseCorrection( const { re, im } = data; const numRows = re.length; - if (numRows === 0) return { re: [], im: [] }; + if (numRows === 0) { + return { + re: [], + im: [], + minRe: Number.NaN, + maxRe: Number.NaN, + minIm: Number.NaN, + maxIm: Number.NaN, + }; + } const numColumns = re[0].length; @@ -65,6 +81,11 @@ export function reimMatrixPhaseCorrection( const alpha = 2 * Math.sin(delta / 2) ** 2; const beta = Math.sin(delta); + let minRe = Number.POSITIVE_INFINITY; + let maxRe = Number.NEGATIVE_INFINITY; + let minIm = Number.POSITIVE_INFINITY; + let maxIm = Number.NEGATIVE_INFINITY; + if (direction === 'rows') { for (let j = 0; j < numRows; j++) { let firstAngle = phi0; @@ -82,6 +103,11 @@ export function reimMatrixPhaseCorrection( resultRe[j][i] = r * cosTheta - ii * sinTheta; resultIm[j][i] = ii * cosTheta + r * sinTheta; + if (resultRe[j][i] < minRe) minRe = resultRe[j][i]; + if (resultRe[j][i] > maxRe) maxRe = resultRe[j][i]; + if (resultIm[j][i] < minIm) minIm = resultIm[j][i]; + if (resultIm[j][i] > maxIm) maxIm = resultIm[j][i]; + const newCosTheta = cosTheta - (alpha * cosTheta + beta * sinTheta); const newSinTheta = sinTheta - (alpha * sinTheta - beta * cosTheta); @@ -106,6 +132,11 @@ export function reimMatrixPhaseCorrection( resultRe[row][col] = r * cosTheta - ii * sinTheta; resultIm[row][col] = ii * cosTheta + r * sinTheta; + if (resultRe[row][col] < minRe) minRe = resultRe[row][col]; + if (resultRe[row][col] > maxRe) maxRe = resultRe[row][col]; + if (resultIm[row][col] < minIm) minIm = resultIm[row][col]; + if (resultIm[row][col] > maxIm) maxIm = resultIm[row][col]; + const newCosTheta = cosTheta - (alpha * cosTheta + beta * sinTheta); const newSinTheta = sinTheta - (alpha * sinTheta - beta * cosTheta); @@ -115,5 +146,5 @@ export function reimMatrixPhaseCorrection( } } - return { re: resultRe, im: resultIm }; + return { re: resultRe, im: resultIm, minRe, maxRe, minIm, maxIm }; }