Skip to content

Commit

Permalink
Merge branch 'develop' into index-with-booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Jul 14, 2023
2 parents c7f5135 + 14614f2 commit b89f023
Show file tree
Hide file tree
Showing 20 changed files with 1,068 additions and 139 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,6 @@ kunalagrwl <kunalaggarwal82@gmail.com>
Michael Greminger <michael.greminger@gmail.com>
Kiku <kiku-cn@foxmail.com>
MaybePixem <47889538+MaybePixem@users.noreply.github.com>
Aly Khaled <alykhaled2001@live.com>

# Generated by tools/update-authors.js
10 changes: 9 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# History

# not yet published, 11.8.3
# not yet published, 11.9.0

- Implement function `solveODE` (#2958). Thanks @dvd101x.
- Implement functions `zpk2tf` and `freqz` (#2988, #2969). Thanks @alykhaled.
- Fix #2974: `simplify` puts plus and minus signs next to each other (#2981).
Thanks @MaybePixem.
- Fix #2973: fixes and improvements in the embedded docs (#2976).
Thanks @dvd101x.
- Fix #2996: two errors in the examples in the documentation about Expression
trees.
- Fix round-off errors near zero when converting temperatures (#2962).
Thanks @costerwi.
- Refactored function `range`, reducing the amount of code (#2995).
Thanks @dvd101x.


# 2023-06-20, 11.8.2
Expand Down
5 changes: 3 additions & 2 deletions docs/expressions/expression_trees.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ Examples:
const node1 = math.parse('a[3]')

const object = new math.SymbolNode('a')
const index = new math.IndexNode([3])
const constant3 = new math.ConstantNode(3)
const index = new math.IndexNode([constant3])
const node2 = new math.AccessorNode(object, index)
```

Expand Down Expand Up @@ -527,7 +528,7 @@ const three = new math.ConstantNode(3)

const range = new math.RangeNode(one, three)
const index = new math.IndexNode([range, two])
const node2 = new math.AccessNode(A, index)
const node2 = new math.AccessorNode(A, index)
```

### ObjectNode
Expand Down
10 changes: 10 additions & 0 deletions src/expression/embeddedDocs/embeddedDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ import { setPowersetDocs } from './function/set/setPowerset.js'
import { setSizeDocs } from './function/set/setSize.js'
import { setSymDifferenceDocs } from './function/set/setSymDifference.js'
import { setUnionDocs } from './function/set/setUnion.js'
import { zpk2tfDocs } from './function/signal/zpk2tf.js'
import { freqzDocs } from './function/signal/freqz.js'
import { erfDocs } from './function/special/erf.js'
import { madDocs } from './function/statistics/mad.js'
import { maxDocs } from './function/statistics/max.js'
Expand Down Expand Up @@ -236,6 +238,7 @@ import { numericDocs } from './function/utils/numeric.js'
import { octDocs } from './function/utils/oct.js'
import { printDocs } from './function/utils/print.js'
import { typeOfDocs } from './function/utils/typeOf.js'
import { solveODEDocs } from './function/numeric/solveODE.js'

export const embeddedDocs = {

Expand Down Expand Up @@ -476,6 +479,9 @@ export const embeddedDocs = {
schur: schurDocs,
lyap: lyapDocs,

// functions - numeric
solveODE: solveODEDocs,

// functions - probability
combinations: combinationsDocs,
combinationsWithRep: combinationsWithRepDocs,
Expand Down Expand Up @@ -515,6 +521,10 @@ export const embeddedDocs = {
setSymDifference: setSymDifferenceDocs,
setUnion: setUnionDocs,

// functions - signal
zpk2tf: zpk2tfDocs,
freqz: freqzDocs,

// functions - special
erf: erfDocs,

Expand Down
17 changes: 17 additions & 0 deletions src/expression/embeddedDocs/function/numeric/solveODE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const solveODEDocs = {
name: 'solveODE',
category: 'Numeric',
syntax: [
'solveODE(func, tspan, y0)',
'solveODE(func, tspan, y0, options)'
],
description: 'Numerical Integration of Ordinary Differential Equations.',
examples: [
'f(t,y) = y',
'tspan = [0, 4]',
'solveODE(f, tspan, 1)',
'solveODE(f, tspan, [1, 2])',
'solveODE(f, tspan, 1, { method:"RK23", maxStep:0.1 })'
],
seealso: ['derivative', 'simplifyCore']
}
15 changes: 15 additions & 0 deletions src/expression/embeddedDocs/function/signal/freqz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const freqzDocs = {
name: 'freqz',
category: 'Signal',
syntax: [
'freqz(b, a)',
'freqz(b, a, w)'
],
description: 'Calculates the frequency response of a filter given its numerator and denominator coefficients.',
examples: [
'freqz([1, 2], [1, 2, 3])',
'freqz([1, 2], [1, 2, 3], [0, 1])',
'freqz([1, 2], [1, 2, 3], 512)'
],
seealso: []
}
14 changes: 14 additions & 0 deletions src/expression/embeddedDocs/function/signal/zpk2tf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const zpk2tfDocs = {
name: 'zpk2tf',
category: 'Signal',
syntax: [
'zpk2tf(z, p, k)'
],
description: 'Compute the transfer function of a zero-pole-gain model.',
examples: [
'zpk2tf([1, 2], [-1, -2], 1)',
'zpk2tf([1, 2], [-1, -2])',
'zpk2tf([1 - 3i, 2 + 2i], [-1, -2])'
],
seealso: []
}
6 changes: 3 additions & 3 deletions src/expression/transform/range.transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { factory } from '../../utils/factory.js'
import { createRange } from '../../function/matrix/range.js'

const name = 'range'
const dependencies = ['typed', 'config', '?matrix', '?bignumber', 'smaller', 'smallerEq', 'larger', 'largerEq']
const dependencies = ['typed', 'config', '?matrix', '?bignumber', 'smaller', 'smallerEq', 'larger', 'largerEq', 'add', 'isPositive']

export const createRangeTransform = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, matrix, bignumber, smaller, smallerEq, larger, largerEq }) => {
const range = createRange({ typed, config, matrix, bignumber, smaller, smallerEq, larger, largerEq })
export const createRangeTransform = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, matrix, bignumber, smaller, smallerEq, larger, largerEq, add, isPositive }) => {
const range = createRange({ typed, config, matrix, bignumber, smaller, smallerEq, larger, largerEq, add, isPositive })

/**
* Attach a transform function to math.range
Expand Down
3 changes: 3 additions & 0 deletions src/factoriesAny.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export { createCtranspose } from './function/matrix/ctranspose.js'
export { createZeros } from './function/matrix/zeros.js'
export { createFft } from './function/matrix/fft.js'
export { createIfft } from './function/matrix/ifft.js'
export { createSolveODE } from './function/numeric/solveODE.js'
export { createErf } from './function/special/erf.js'
export { createMode } from './function/statistics/mode.js'
export { createProd } from './function/statistics/prod.js'
Expand Down Expand Up @@ -258,6 +259,8 @@ export { createResolve } from './function/algebra/resolve.js'
export { createSymbolicEqual } from './function/algebra/symbolicEqual.js'
export { createDerivative } from './function/algebra/derivative.js'
export { createRationalize } from './function/algebra/rationalize.js'
export { createZpk2tf } from './function/signal/zpk2tf.js'
export { createFreqz } from './function/signal/freqz.js'
export { createReviver } from './json/reviver.js'
export { createReplacer } from './json/replacer.js'
export {
Expand Down
146 changes: 26 additions & 120 deletions src/function/matrix/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { factory } from '../../utils/factory.js'
import { noBignumber, noMatrix } from '../../utils/noop.js'

const name = 'range'
const dependencies = ['typed', 'config', '?matrix', '?bignumber', 'smaller', 'smallerEq', 'larger', 'largerEq']
const dependencies = ['typed', 'config', '?matrix', '?bignumber', 'smaller', 'smallerEq', 'larger', 'largerEq', 'add', 'isPositive']

export const createRange = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, matrix, bignumber, smaller, smallerEq, larger, largerEq }) => {
export const createRange = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, matrix, bignumber, smaller, smallerEq, larger, largerEq, add, isPositive }) => {
/**
* Create an array from a range.
* By default, the range end is excluded. This can be customized by providing
Expand Down Expand Up @@ -56,41 +56,33 @@ export const createRange = /* #__PURE__ */ factory(name, dependencies, ({ typed,
'string, boolean': _strRange,

'number, number': function (start, end) {
return _out(_rangeEx(start, end, 1))
return _out(_range(start, end, 1, false))
},
'number, number, number': function (start, end, step) {
return _out(_rangeEx(start, end, step))
return _out(_range(start, end, step, false))
},
'number, number, boolean': function (start, end, includeEnd) {
return includeEnd
? _out(_rangeInc(start, end, 1))
: _out(_rangeEx(start, end, 1))
return _out(_range(start, end, 1, includeEnd))
},
'number, number, number, boolean': function (start, end, step, includeEnd) {
return includeEnd
? _out(_rangeInc(start, end, step))
: _out(_rangeEx(start, end, step))
return _out(_range(start, end, step, includeEnd))
},

'BigNumber, BigNumber': function (start, end) {
const BigNumber = start.constructor

return _out(_bigRangeEx(start, end, new BigNumber(1)))
return _out(_range(start, end, new BigNumber(1), false))
},
'BigNumber, BigNumber, BigNumber': function (start, end, step) {
return _out(_bigRangeEx(start, end, step))
return _out(_range(start, end, step, false))
},
'BigNumber, BigNumber, boolean': function (start, end, includeEnd) {
const BigNumber = start.constructor

return includeEnd
? _out(_bigRangeInc(start, end, new BigNumber(1)))
: _out(_bigRangeEx(start, end, new BigNumber(1)))
return _out(_range(start, end, new BigNumber(1), includeEnd))
},
'BigNumber, BigNumber, BigNumber, boolean': function (start, end, step, includeEnd) {
return includeEnd
? _out(_bigRangeInc(start, end, step))
: _out(_bigRangeEx(start, end, step))
return _out(_range(start, end, step, includeEnd))
}

})
Expand All @@ -109,126 +101,40 @@ export const createRange = /* #__PURE__ */ factory(name, dependencies, ({ typed,
throw new SyntaxError('String "' + str + '" is no valid range')
}

let fn
if (config.number === 'BigNumber') {
if (bignumber === undefined) {
noBignumber()
}

fn = includeEnd ? _bigRangeInc : _bigRangeEx
return _out(fn(
return _out(_range(
bignumber(r.start),
bignumber(r.end),
bignumber(r.step)))
bignumber(r.step)),
includeEnd)
} else {
fn = includeEnd ? _rangeInc : _rangeEx
return _out(fn(r.start, r.end, r.step))
return _out(_range(r.start, r.end, r.step, includeEnd))
}
}

/**
* Create a range with numbers. End is excluded
* @param {number} start
* @param {number} end
* @param {number} step
* Create a range with numbers or BigNumbers
* @param {number | BigNumber} start
* @param {number | BigNumber} end
* @param {number | BigNumber} step
* @param {boolean} includeEnd
* @returns {Array} range
* @private
*/
function _rangeEx (start, end, step) {
function _range (start, end, step, includeEnd) {
const array = []
const ongoing = isPositive(step)
? includeEnd ? smallerEq : smaller
: includeEnd ? largerEq : larger
let x = start
if (step > 0) {
while (smaller(x, end)) {
array.push(x)
x += step
}
} else if (step < 0) {
while (larger(x, end)) {
array.push(x)
x += step
}
}

return array
}

/**
* Create a range with numbers. End is included
* @param {number} start
* @param {number} end
* @param {number} step
* @returns {Array} range
* @private
*/
function _rangeInc (start, end, step) {
const array = []
let x = start
if (step > 0) {
while (smallerEq(x, end)) {
array.push(x)
x += step
}
} else if (step < 0) {
while (largerEq(x, end)) {
array.push(x)
x += step
}
}

return array
}

/**
* Create a range with big numbers. End is excluded
* @param {BigNumber} start
* @param {BigNumber} end
* @param {BigNumber} step
* @returns {Array} range
* @private
*/
function _bigRangeEx (start, end, step) {
const zero = bignumber(0)
const array = []
let x = start
if (step.gt(zero)) {
while (smaller(x, end)) {
array.push(x)
x = x.plus(step)
}
} else if (step.lt(zero)) {
while (larger(x, end)) {
array.push(x)
x = x.plus(step)
}
while (ongoing(x, end)) {
array.push(x)
x = add(x, step)
}

return array
}

/**
* Create a range with big numbers. End is included
* @param {BigNumber} start
* @param {BigNumber} end
* @param {BigNumber} step
* @returns {Array} range
* @private
*/
function _bigRangeInc (start, end, step) {
const zero = bignumber(0)
const array = []
let x = start
if (step.gt(zero)) {
while (smallerEq(x, end)) {
array.push(x)
x = x.plus(step)
}
} else if (step.lt(zero)) {
while (largerEq(x, end)) {
array.push(x)
x = x.plus(step)
}
}

return array
}

Expand Down
Loading

0 comments on commit b89f023

Please sign in to comment.