Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:josdejong/mathjs into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-canestraro committed Feb 7, 2023
2 parents 739387e + 68b4b50 commit edd789f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
7 changes: 7 additions & 0 deletions HISTORY.md
@@ -1,5 +1,12 @@
# History

# unpublished changes since 11.5.1

- Fix #2888: update type definitions of function `unit` to allow creating a
unit from a fraction or complex number.
- Fix #2892: an error in the examples of the embedded help of function `sort`.
- Fix #2891: functions `column` and `row` sometimes returning a scalar number.

# 2023-01-31, 11.5.1

- Add type definitions for function `rotationMatrix` (#2860).
Expand Down
2 changes: 1 addition & 1 deletion src/expression/embeddedDocs/function/matrix/sort.js
Expand Up @@ -8,7 +8,7 @@ export const sortDocs = {
description: 'Sort the items in a matrix. Compare can be a string "asc", "desc", "natural", or a custom sort function.',
examples: [
'sort([5, 10, 1])',
'sort(["C", "B", "A", "D"])',
'sort(["C", "B", "A", "D"], "natural")',
'sortByLength(a, b) = size(a)[1] - size(b)[1]',
'sort(["Langdon", "Tom", "Sara"], sortByLength)',
'sort(["10", "1", "2"], "natural")'
Expand Down
6 changes: 5 additions & 1 deletion src/function/matrix/column.js
@@ -1,4 +1,5 @@
import { factory } from '../../utils/factory.js'
import { isMatrix } from '../../utils/is.js'
import { clone } from '../../utils/object.js'
import { validateIndex } from '../../utils/array.js'

Expand Down Expand Up @@ -51,6 +52,9 @@ export const createColumn = /* #__PURE__ */ factory(name, dependencies, ({ typed

const rowRange = range(0, value.size()[0])
const index = new Index(rowRange, column)
return value.subset(index)
const result = value.subset(index)
return isMatrix(result)
? result
: matrix([[result]])
}
})
6 changes: 5 additions & 1 deletion src/function/matrix/row.js
@@ -1,4 +1,5 @@
import { factory } from '../../utils/factory.js'
import { isMatrix } from '../../utils/is.js'
import { clone } from '../../utils/object.js'
import { validateIndex } from '../../utils/array.js'

Expand Down Expand Up @@ -51,6 +52,9 @@ export const createRow = /* #__PURE__ */ factory(name, dependencies, ({ typed, I

const columnRange = range(0, value.size()[1])
const index = new Index(row, columnRange)
return value.subset(index)
const result = value.subset(index)
return isMatrix(result)
? result
: matrix([[result]])
}
})
2 changes: 1 addition & 1 deletion test/typescript-tests/testTypes.ts
Expand Up @@ -1362,7 +1362,7 @@ Math types examples: Type results after multiplying 'MathTypes' with matrices

// Unit
const a = math.unit(45, 'cm') // 450 mm
const b = math.unit(45, 'cm') // 450 mm
const b = math.unit(math.fraction(90, 2), 'cm') // 450 mm
const _r2 = math.multiply(a, b)

// 1D JS Array
Expand Down
10 changes: 10 additions & 0 deletions test/unit-tests/function/matrix/column.test.js
Expand Up @@ -84,6 +84,16 @@ describe('column', function () {
)
})

it('should return the column of an 1x1 array', function () {
assert.deepStrictEqual(column([[5]], 0), [[5]])
assert.deepStrictEqual(column([[5, 6, 7]], 0), [[5]])
})

it('should return the column of an 1x1 matrix', function () {
assert.deepStrictEqual(column(matrix([[5]]), 0), matrix([[5]]))
assert.deepStrictEqual(column(matrix([[5, 6, 7]]), 0), matrix([[5]]))
})

it('should return an empty matrix column', function () {
const c = column(m, 2)
assert.deepStrictEqual(
Expand Down
10 changes: 10 additions & 0 deletions test/unit-tests/function/matrix/row.test.js
Expand Up @@ -84,6 +84,16 @@ describe('row', function () {
)
})

it('should return the row of an 1x1 array', function () {
assert.deepStrictEqual(row([[5]], 0), [[5]])
assert.deepStrictEqual(row([[5], [6], [7]], 0), [[5]])
})

it('should return the row of an 1x1 matrix', function () {
assert.deepStrictEqual(row(matrix([[5]]), 0), matrix([[5]]))
assert.deepStrictEqual(row(matrix([[5], [6], [7]]), 0), matrix([[5]]))
})

it('should return an empty matrix row', function () {
const r = row(m, 2)
assert.deepStrictEqual(
Expand Down
4 changes: 2 additions & 2 deletions types/index.d.ts
Expand Up @@ -781,7 +781,7 @@ declare namespace math {
* @param unit The unit to be created
* @returns The created unit
*/
unit(value: number | BigNumber, unit: string): Unit
unit(value: number | BigNumber | Fraction | Complex, unit: string): Unit
unit(value: MathCollection, unit: string): Unit[]

/*************************************************************************
Expand Down Expand Up @@ -4402,7 +4402,7 @@ declare namespace math {
unit(this: MathJsChain<string>, unit?: string): MathJsChain<Unit>
unit(this: MathJsChain<Unit>, unit?: string): MathJsChain<Unit>
unit(
this: MathJsChain<number | BigNumber>,
this: MathJsChain<number | BigNumber | Fraction | Complex>,
unit?: string
): MathJsChain<Unit>
unit(this: MathJsChain<MathCollection>, unit?: string): MathJsChain<Unit[]>
Expand Down

0 comments on commit edd789f

Please sign in to comment.