Skip to content

Commit

Permalink
feat: support units in range (#2997)
Browse files Browse the repository at this point in the history
* reange refactoring

* update authors

* Made _range to work also for _bigNumbers

* Range with units

* Included some Unit type in _range

* Included TypeScript types for function range

* Formatting

---------

Co-authored-by: David Contreras <david.contreras@guentner.com>
  • Loading branch information
dvd101x and David Contreras committed Jul 19, 2023
1 parent 14614f2 commit 8fe5e51
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/expression/embeddedDocs/function/matrix/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const rangeDocs = {
'range(3, 7)',
'range(0, 12, 2)',
'range("4:10")',
'range(1m, 1m, 3m)',
'a = [1, 2, 3, 4; 5, 6, 7, 8]',
'a[1:2, 1:2]'
],
Expand Down
19 changes: 13 additions & 6 deletions src/function/matrix/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export const createRange = /* #__PURE__ */ factory(name, dependencies, ({ typed,
*
* - `str: string`
* A string 'start:end' or 'start:step:end'
* - `start: {number | BigNumber}`
* - `start: {number | BigNumber | Unit}`
* Start of the range
* - `end: number | BigNumber`
* - `end: number | BigNumber | Unit`
* End of the range, excluded by default, included when parameter includeEnd=true
* - `step: number | BigNumber`
* - `step: number | BigNumber | Unit`
* Step size. Default value is 1.
* - `includeEnd: boolean`
* Option to specify whether to include the end or not. False by default.
Expand All @@ -40,6 +40,7 @@ export const createRange = /* #__PURE__ */ factory(name, dependencies, ({ typed,
* math.range(2, -3, -1) // [2, 1, 0, -1, -2]
* math.range('2:1:6') // [2, 3, 4, 5]
* math.range(2, 6, true) // [2, 3, 4, 5, 6]
* math.range(math.unit(2, 'm'), math.unit(-3, 'm'), math.unit(-1, 'm')) // [2 m, 1 m, 0 m , -1 m, -2 m]
*
* See also:
*
Expand Down Expand Up @@ -83,6 +84,12 @@ export const createRange = /* #__PURE__ */ factory(name, dependencies, ({ typed,
},
'BigNumber, BigNumber, BigNumber, boolean': function (start, end, step, includeEnd) {
return _out(_range(start, end, step, includeEnd))
},
'Unit, Unit, Unit': function (start, end, step) {
return _out(_range(start, end, step, false))
},
'Unit, Unit, Unit, boolean': function (start, end, step, includeEnd) {
return _out(_range(start, end, step, includeEnd))
}

})
Expand Down Expand Up @@ -118,9 +125,9 @@ export const createRange = /* #__PURE__ */ factory(name, dependencies, ({ typed,

/**
* Create a range with numbers or BigNumbers
* @param {number | BigNumber} start
* @param {number | BigNumber} end
* @param {number | BigNumber} step
* @param {number | BigNumber | Unit} start
* @param {number | BigNumber | Unit} end
* @param {number | BigNumber | Unit} step
* @param {boolean} includeEnd
* @returns {Array} range
* @private
Expand Down
20 changes: 19 additions & 1 deletion test/unit-tests/function/matrix/range.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import math from '../../../../src/defaultInstance.js'
const range = math.range
const matrix = math.matrix
const bignumber = math.bignumber
const unit = math.unit
const evaluate = math.evaluate

describe('range', function () {
it('should parse a valid string correctly', function () {
Expand Down Expand Up @@ -84,6 +86,18 @@ describe('range', function () {
assert.throws(function () { bigmath.range('1:a') }, /is no valid range/)
})

it('should create a range with units', function () {
assert.deepStrictEqual(range(unit(1, 'm'), unit(3, 'm'), unit(1, 'm')), matrix([unit(1, 'm'), unit(2, 'm')]))
assert.deepStrictEqual(range(unit(3, 'm'), unit(1, 'm'), unit(-1, 'm')), matrix([unit(3, 'm'), unit(2, 'm')]))
})

it('should parse a range with units', function () {
assert.deepStrictEqual(evaluate('1m:1m:3m'), matrix([unit(1, 'm'), unit(2, 'm'), unit(3, 'm')]))
assert.deepStrictEqual(evaluate('3m:-1m:0m'), matrix([unit(3, 'm'), unit(2, 'm'), unit(1, 'm'), unit(0, 'm')]))
assert.deepStrictEqual(evaluate('range(1m,3m,1m)'), matrix([unit(1, 'm'), unit(2, 'm'), unit(3, 'm')]))
assert.deepStrictEqual(evaluate('range(3m,0m,-1m)'), matrix([unit(3, 'm'), unit(2, 'm'), unit(1, 'm'), unit(0, 'm')]))
})

it('should gracefully handle round-off errors', function () {
assert.deepStrictEqual(range(1, 2, 0.1, true)._size, [11])
assert.deepStrictEqual(range(0.1, 0.2, 0.01, true)._size, [11])
Expand Down Expand Up @@ -129,10 +143,14 @@ describe('range', function () {
assert.throws(function () { range('invalid range') }, SyntaxError)
})

it('should throw an error if called with a unit', function () {
it('should throw an error if called with a single unit value', function () {
assert.throws(function () { range(math.unit('5cm')) }, TypeError)
})

it('should throw an error if called with a single only two units value', function () {
assert.throws(function () { range(math.unit('0cm'), math.unit('5cm')) }, TypeError)
})

it('should throw an error if called with a complex number', function () {
assert.throws(function () { range(math.complex(2, 3)) }, TypeError)
})
Expand Down
12 changes: 6 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1989,9 +1989,9 @@ declare namespace math {
includeEnd?: boolean
): Matrix
range(
start: number | BigNumber,
end: number | BigNumber,
step: number | BigNumber,
start: number | BigNumber | Unit,
end: number | BigNumber | Unit,
step: number | BigNumber | Unit,
includeEnd?: boolean
): Matrix

Expand Down Expand Up @@ -5477,9 +5477,9 @@ declare namespace math {
includeEnd?: boolean
): MathJsChain<Matrix>
range(
this: MathJsChain<number | BigNumber>,
end: number | BigNumber,
step: number | BigNumber,
this: MathJsChain<number | BigNumber | Unit>,
end: number | BigNumber | Unit,
step: number | BigNumber | Unit,
includeEnd?: boolean
): MathJsChain<Matrix>

Expand Down

0 comments on commit 8fe5e51

Please sign in to comment.