Skip to content
Merged
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
28 changes: 28 additions & 0 deletions src/reimMatrix/__tests__/reimMatrixPhaseCorrection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])],
Expand Down
37 changes: 34 additions & 3 deletions src/reimMatrix/reimMatrixPhaseCorrection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -115,5 +146,5 @@ export function reimMatrixPhaseCorrection(
}
}

return { re: resultRe, im: resultIm };
return { re: resultRe, im: resultIm, minRe, maxRe, minIm, maxIm };
}
Loading