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

Determinant with small numbers fix #3139

Merged
merged 30 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9232a98
broadcasting
dvd101x Feb 7, 2023
c589ad5
Simplified broadcasting
Feb 8, 2023
fbfd332
Updated for broadcasting
Feb 8, 2023
8537fc9
Changed to camel case
Feb 8, 2023
099a7d8
Camel case and auto formating
Feb 8, 2023
3a0aa2d
Added comments
Feb 8, 2023
394c737
Skip if matrices have the same size
dvd101x Feb 9, 2023
772e1db
Fixed issue with undefined variable
dvd101x Feb 9, 2023
2cff0be
Implemented broadcasting in all functions
Feb 9, 2023
6508164
Merge branch 'develop' of https://github.com/dvd101x/mathjs into develop
Feb 9, 2023
3ab4ec9
Added helper functions
dvd101x Feb 12, 2023
899b785
Merge branch 'develop' into develop
dvd101x Feb 12, 2023
0d5fb32
Added function to check for broadcasting rules
dvd101x Feb 12, 2023
3b649ef
Tests for broadcasted arithmetic
Feb 15, 2023
7356dd3
Fixed issue with matrix the size of a vector
Feb 15, 2023
1e2138c
Documented and updated broadcasting
dvd101x Feb 18, 2023
ba8eda2
Included broadcast.test
dvd101x Feb 19, 2023
9a65882
Merge branch 'develop' into develop
josdejong Feb 23, 2023
a04770f
Merge branch 'josdejong:develop' into develop
dvd101x Feb 24, 2023
104310f
Included math to syntax when missing
dvd101x Apr 24, 2023
75e6c70
Merge branch 'develop' of https://github.com/dvd101x/mathjs into develop
dvd101x May 27, 2023
e2bdd4a
Merge branch 'develop' of https://github.com/dvd101x/mathjs into develop
dvd101x Aug 5, 2023
a131845
change isZero to take epsilon into account
dvd101x Jan 25, 2024
c0162ac
Removed unnecesarry cases
dvd101x Jan 25, 2024
a89285b
Change to arrow function
dvd101x Jan 25, 2024
90d2d81
Merge branch 'develop' into determinant
josdejong Jan 31, 2024
fb4fadb
updated isPositive and isNegative to test for nearly zero
dvd101x Feb 5, 2024
e827580
Merge branch 'develop' into determinant
dvd101x Feb 5, 2024
0fa448e
Include test for determinant of matrix with tiny numbers #3135
dvd101x Feb 9, 2024
0336e7a
Merge branch 'develop' into determinant
josdejong Feb 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/function/utils/isNegative.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { deepMap } from '../../utils/collection.js'
import { factory } from '../../utils/factory.js'
import { isNegativeNumber } from '../../plain/number/index.js'
import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual.js'
import { nearlyEqual } from '../../utils/number.js'

const name = 'isNegative'
const dependencies = ['typed']
const dependencies = ['typed', 'config']

export const createIsNegative = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
export const createIsNegative = /* #__PURE__ */ factory(name, dependencies, ({ typed, config }) => {
/**
* Test whether a value is negative: smaller than zero.
* The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`.
Expand Down Expand Up @@ -36,15 +38,13 @@ export const createIsNegative = /* #__PURE__ */ factory(name, dependencies, ({ t
* Throws an error in case of an unknown data type.
*/
return typed(name, {
number: isNegativeNumber,
number: x => nearlyEqual(x, 0, config.epsilon) ? false : isNegativeNumber(x),

BigNumber: function (x) {
return x.isNeg() && !x.isZero() && !x.isNaN()
},
BigNumber: x => bigNearlyEqual(x, new x.constructor(0), config.epsilon)
? false
: x.isNeg() && !x.isZero() && !x.isNaN(),

Fraction: function (x) {
return x.s < 0 // It's enough to decide on the sign
},
Fraction: x => x.s < 0, // It's enough to decide on the sign

Unit: typed.referToSelf(self =>
x => typed.find(self, x.valueType())(x.value)),
Expand Down
19 changes: 10 additions & 9 deletions src/function/utils/isPositive.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { deepMap } from '../../utils/collection.js'
import { factory } from '../../utils/factory.js'
import { isPositiveNumber } from '../../plain/number/index.js'
import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual.js'
import { nearlyEqual } from '../../utils/number.js'

const name = 'isPositive'
const dependencies = ['typed']
const dependencies = ['typed', 'config']

export const createIsPositive = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
export const createIsPositive = /* #__PURE__ */ factory(name, dependencies, ({ typed, config }) => {
/**
* Test whether a value is positive: larger than zero.
* The function supports types `number`, `BigNumber`, `Fraction`, and `Unit`.
Expand Down Expand Up @@ -38,15 +40,14 @@ export const createIsPositive = /* #__PURE__ */ factory(name, dependencies, ({ t
* Throws an error in case of an unknown data type.
*/
return typed(name, {
number: isPositiveNumber,
number: x => nearlyEqual(x, 0, config.epsilon) ? false : isPositiveNumber(x),

BigNumber: function (x) {
return !x.isNeg() && !x.isZero() && !x.isNaN()
},
BigNumber: x =>
bigNearlyEqual(x, new x.constructor(0), config.epsilon)
? false
: !x.isNeg() && !x.isZero() && !x.isNaN(),

Fraction: function (x) {
return x.s > 0 && x.n > 0
},
Fraction: x => x.s > 0 && x.n > 0,

Unit: typed.referToSelf(self =>
x => typed.find(self, x.valueType())(x.value)),
Expand Down
19 changes: 3 additions & 16 deletions src/function/utils/isZero.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { deepMap } from '../../utils/collection.js'
import { factory } from '../../utils/factory.js'
import { isZeroNumber } from '../../plain/number/index.js'

const name = 'isZero'
const dependencies = ['typed']
const dependencies = ['typed', 'equalScalar']

export const createIsZero = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
export const createIsZero = /* #__PURE__ */ factory(name, dependencies, ({ typed, equalScalar }) => {
/**
* Test whether a value is zero.
* The function can check for zero for types `number`, `BigNumber`, `Fraction`,
Expand Down Expand Up @@ -40,19 +39,7 @@ export const createIsZero = /* #__PURE__ */ factory(name, dependencies, ({ typed
* Throws an error in case of an unknown data type.
*/
return typed(name, {
number: isZeroNumber,

BigNumber: function (x) {
return x.isZero()
},

Complex: function (x) {
return x.re === 0 && x.im === 0
},

Fraction: function (x) {
return x.d === 1 && x.n === 0
},
'number | BigNumber | Complex | Fraction': x => equalScalar(x, 0),
josdejong marked this conversation as resolved.
Show resolved Hide resolved

Unit: typed.referToSelf(self =>
x => typed.find(self, x.valueType())(x.value)),
Expand Down
6 changes: 6 additions & 0 deletions test/unit-tests/function/matrix/det.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ describe('det', function () {
[2, 7, 4, 3, 7]
]), 1176)
assert.strictEqual(det(diag([4, -5, 6])), -120)
assert.strictEqual(
det([
[6.123234262925839e-17, -1, 1],
[-0.8660253882408142, 0.5, 1],
[-0.6495190262794495, -0.3749999701976776, 1]
]), 0.4330126459590976)
})

it('should return the determinant of a sparse matrix', function () {
Expand Down
8 changes: 8 additions & 0 deletions test/unit-tests/function/utils/isNegative.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('isNegative', function () {
assert.strictEqual(isNegative(NaN), false)
})

it('should test whether a number is near negative', function () {
// when epsilon is 1e-12
assert.strictEqual(isNegative(1e-17), false)
assert.strictEqual(isNegative(-1e-17), false)
assert.strictEqual(isNegative(1e-14), false)
assert.strictEqual(isNegative(-1e-14), true)
})

it('should test whether a boolean is negative', function () {
assert.strictEqual(isNegative(true), false)
assert.strictEqual(isNegative(false), false)
Expand Down
8 changes: 8 additions & 0 deletions test/unit-tests/function/utils/isPositive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ describe('isPositive', function () {
assert.strictEqual(isPositive(NaN), false)
})

it('should test whether a number is near positive', function () {
// when epsilon is 1e-12
assert.strictEqual(isPositive(1e-17), false)
assert.strictEqual(isPositive(-1e-17), false)
assert.strictEqual(isPositive(1e-14), true)
assert.strictEqual(isPositive(-1e-14), false)
})

it('should test whether a boolean is positive', function () {
assert.strictEqual(isPositive(true), true)
assert.strictEqual(isPositive(false), false)
Expand Down
8 changes: 8 additions & 0 deletions test/unit-tests/function/utils/isZero.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ describe('isZero', function () {
assert.strictEqual(isZero(NaN), false)
})

it('should test whether a number is near zero', function () {
// when epsilon is 1e-12
assert.strictEqual(isZero(1e-17), true)
assert.strictEqual(isZero(1e-16), true)
assert.strictEqual(isZero(1e-15), false)
assert.strictEqual(isZero(1e-14), false)
})

it('should test whether a boolean is zero', function () {
assert.strictEqual(isZero(true), false)
assert.strictEqual(isZero(false), true)
Expand Down