Skip to content

Commit

Permalink
feat: upgrade to [email protected] and implement support for `log(x…
Browse files Browse the repository at this point in the history
…: Fraction, base: Fraction)`
  • Loading branch information
josdejong committed Nov 20, 2024
1 parent e93780e commit 52bcd8f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"complex.js": "^2.2.5",
"decimal.js": "^10.4.3",
"escape-latex": "^1.2.0",
"fraction.js": "^5.0.4",
"fraction.js": "^5.2.1",
"javascript-natural-sort": "^0.7.1",
"seedrandom": "^3.0.5",
"tiny-emitter": "^2.1.0",
Expand Down
19 changes: 14 additions & 5 deletions src/function/arithmetic/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { factory } from '../../utils/factory.js'
import { logNumber } from '../../plain/number/index.js'

const name = 'log'
const dependencies = ['config', 'typed', 'divideScalar', 'Complex']
const dependencies = ['config', 'typed', 'typeOf', 'divideScalar', 'Complex']

export const createLog = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, divideScalar, Complex }) => {
export const createLog = /* #__PURE__ */ factory(name, dependencies, ({ typed, typeOf, config, divideScalar, Complex }) => {
/**
* Calculate the logarithm of a value.
*
Expand Down Expand Up @@ -32,12 +32,12 @@ export const createLog = /* #__PURE__ */ factory(name, dependencies, ({ typed, c
*
* exp, log2, log10, log1p
*
* @param {number | BigNumber | Complex} x
* @param {number | BigNumber | Fraction | Complex} x
* Value for which to calculate the logarithm.
* @param {number | BigNumber | Complex} [base=e]
* @param {number | BigNumber | Fraction | Complex} [base=e]
* Optional base for the logarithm. If not provided, the natural
* logarithm of `x` is calculated.
* @return {number | BigNumber | Complex}
* @return {number | BigNumber | Fraction | Complex}
* Returns the logarithm of `x`
*/
return typed(name, {
Expand Down Expand Up @@ -65,6 +65,15 @@ export const createLog = /* #__PURE__ */ factory(name, dependencies, ({ typed, c

'any, any': typed.referToSelf(self => (x, base) => {
// calculate logarithm for a specified base, log(x, base)

if (typeOf(x) === 'Fraction' && typeOf(base) === 'Fraction') {
const result = x.log(base)

if (result !== null) {
return result
}
}

return divideScalar(self(x), self(base))
})
})
Expand Down
8 changes: 8 additions & 0 deletions test/unit-tests/function/arithmetic/log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const mathPredictable = math.create({ predictable: true })
const complex = math.complex
const matrix = math.matrix
const unit = math.unit
const fraction = math.fraction
const log = math.log

describe('log', function () {
Expand Down Expand Up @@ -93,6 +94,13 @@ describe('log', function () {
approxDeepEqual(log(complex(1, 0)), complex(0, 0))
})

it('should return the log of a Fraction', function () {
approxDeepEqual(log(fraction(27, 8), fraction(9, 4)), fraction(3, 2))
assert.throws(() => log(fraction(27, 8), fraction(-2, 5)),
/Cannot implicitly convert a Fraction to BigNumber or vice versa/
)
})

it('should handle complex number with large imaginary part', function () {
const tau4 = math.tau / 4
const real = [0, -1, 1]
Expand Down

0 comments on commit 52bcd8f

Please sign in to comment.