Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename Matrix#columnAt -> column #435

Merged
merged 1 commit into from
Jun 6, 2024
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
8 changes: 4 additions & 4 deletions apps/tutorial/serviceworker.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions apps/tutorial/serviceworker.js.map

Large diffs are not rendered by default.

43 changes: 22 additions & 21 deletions packages/simulator/src/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ export class Matrix {
return ok(this)
}

/**
* Retrieves a column from the matrix.
*/
column(colIndex: number): Result<Complex[], Error> {
if (colIndex < 0) {
return err(Error('colIndex < 0'))
}
if (colIndex > this.width) {
return err(Error('colIndex > this.width'))
}

const col = []
for (let row = 0; row < this.height; row++) {
const element = this.element(row, colIndex)
if (element.isErr()) {
return err(element.error)
}
col.push(element.value)
}
return ok(col)
}

/**
* Adds another matrix to the current matrix.
*/
Expand Down Expand Up @@ -199,27 +221,6 @@ export class Matrix {
return Matrix.create(h, w, buf)._unsafeUnwrap()
}

/**
* Returns the `colIndex` column of the matrix.
*
* @param colIndex - The column index
* @returns A result object with the column or an error
*/
columnAt(colIndex: number): Result<Complex[], Error> {
if (colIndex < 0) {
return err(Error('colIndex < 0'))
}
if (colIndex > this.width) {
return err(Error('colIndex > this.width'))
}

const col = []
for (let row = 0; row < this.height; row++) {
col.push(this.element(row, colIndex)._unsafeUnwrap())
}
return ok(col)
}

/**
* Returns the matrix as an array of rows.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/simulator/src/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2462,7 +2462,7 @@ export class Simulator {
}

amplitudes(): Complex[] {
return this.state.matrix.columnAt(0)._unsafeUnwrap()
return this.state.matrix.column(0)._unsafeUnwrap()
}

private u(u: Matrix, ...targets: number[]): void {
Expand Down
35 changes: 20 additions & 15 deletions packages/simulator/test/matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ describe('Matrix', () => {
expect(m._unsafeUnwrap().toString()).toBe('{{0, 10, 20}, {1, 11, 21}}')
})

describe('column', () => {
const m = M([
[2, 3],
[5, 7],
])

test('returns a Complex array', () => {
expect(equate(m.column(0)._unsafeUnwrap(), [2, 5])).toBeTruthy()
expect(equate(m.column(1)._unsafeUnwrap(), [3, 7])).toBeTruthy()
})

test('negative index error', () => {
expect(m.column(-1)._unsafeUnwrapErr().message).toBe('colIndex < 0')
})

test('index too large error', () => {
expect(m.column(100)._unsafeUnwrapErr().message).toBe('colIndex > this.width')
})
})

describe('add', () => {
test('add Matrix', () => {
expect(
Expand Down Expand Up @@ -216,21 +236,6 @@ describe('Matrix', () => {
})
})

test('columnAt', () => {
const m = squareMatrix(2, 3, 5, 7)
expect(equate(m.columnAt(0)._unsafeUnwrap(), [2, 5])).toBeTruthy()
expect(equate(m.columnAt(1)._unsafeUnwrap(), [3, 7])).toBeTruthy()
expect(equate(Matrix.column_vector(1, 2, 3)._unsafeUnwrap().columnAt(0)._unsafeUnwrap(), [1, 2, 3])).toBeTruthy()

const resErrNegativeIndex = m.columnAt(-1)
expect(resErrNegativeIndex.isErr()).toBeTruthy()
expect(resErrNegativeIndex._unsafeUnwrapErr().message).toBe('colIndex < 0')

const resErrIndexTooLarge = m.columnAt(100)
expect(resErrIndexTooLarge.isErr()).toBeTruthy()
expect(resErrIndexTooLarge._unsafeUnwrapErr().message).toBe('colIndex > this.width')
})

test('isHermitian', () => {
const i = Complex.I

Expand Down
Loading