-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fraction numerator denominator helpers #3605
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
Open
AnslemHack
wants to merge
20
commits into
josdejong:develop
Choose a base branch
from
AnslemHack:fraction-numerator-denominator-helpers
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+453
−0
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9a0a1d1
added operators for denominator and numerator evaluation
df47a9e
added embeded docs for den and num
071dbb2
added embeded docs for den and num
0ab6886
updated the respective types for den and num operators
83eaf63
added comprehensive tests to cover complex scenarios
801394f
fix: correct JSDoc examples for num() and den() functions to fix fail…
ebb4c86
Merge branch 'develop' into fraction-numerator-denominator-helpers
AnslemHack af362d7
deleted den files from arithmetic folder
bd2ceb3
added docs
4bb73ed
moved den and num to fraction folder
3168e3f
added unite tests for den and num
58ffac5
added type tests for num and den
58c4828
updated authors
844f91c
Merge branch 'fraction-numerator-denominator-helpers' of github.com:A…
ecf6476
addressed all open issues
6400d48
updated history.md to include new feature
320b719
updated history.md to include new feature
89072d8
updated history.md to include new feature
e5287e1
adressed open issues with paretnthesis
bdd563e
resolved conflicts
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export const denDocs = { | ||
| name: 'den', | ||
| category: 'Fraction', | ||
| syntax: ['den(x)'], | ||
| description: 'Get the denominator of a fraction.', | ||
| examples: ['den(fraction(2, 3))', 'den(fraction(5, 8))'], | ||
| seealso: ['num', 'fraction'] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export const numDocs = { | ||
| name: 'num', | ||
| category: 'Fraction', | ||
| syntax: ['num(x)'], | ||
| description: 'Get the numerator of a fraction.', | ||
| examples: ['num(fraction(2, 3))', 'num(fraction(5, 8))'], | ||
| seealso: ['den', 'fraction'] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { factory } from '../../utils/factory.js' | ||
| import { deepMap } from '../../utils/collection.js' | ||
|
|
||
| const name = 'den' | ||
| const dependencies = ['typed', 'fraction'] | ||
|
|
||
| export const createDen = /* #__PURE__ */ factory( | ||
| name, | ||
| dependencies, | ||
| ({ typed, fraction }) => { | ||
| /** | ||
| * Get the denominator of a fraction. | ||
| * For a fraction `a/b`, the function returns `b`. | ||
| * | ||
| * The result is always in lowest terms. For example, `den(fraction(8, 6))` | ||
| * returns `3n` because 8/6 simplifies to 4/3. | ||
| * | ||
| * For negative fractions like `-a/b` or `a/-b`, the denominator is | ||
| * always returned as a positive bigint. The sign is stored in the | ||
| * numerator. So `den(fraction(-2, 3))` and `den(fraction(2, -3))` | ||
| * both return `3n`. | ||
| * | ||
| * For matrices, the function is evaluated element wise. | ||
| * | ||
| * Syntax: | ||
| * | ||
| * math.den(x) | ||
| * | ||
| * Examples: | ||
| * | ||
| * math.den(math.fraction(2, 3)) // returns 3n | ||
| * math.den(math.fraction(8, 6)) // returns 3n | ||
| * math.den(math.fraction('5/8')) // returns 8n | ||
| * math.den(math.fraction(-2, 3)) // returns 3n | ||
| * math.den(math.fraction(2, -3)) // returns 3n | ||
| * math.den(math.bignumber('0.5')) // returns 2n | ||
| * | ||
| * See also: | ||
| * | ||
| * num, fraction | ||
| * | ||
| * History: | ||
| * | ||
| * v15.2.0 Created | ||
| * | ||
| * @param {Fraction | BigNumber | Array | Matrix} x | ||
| * A fraction, BigNumber, or array with fractions | ||
| * @return {bigint | Array | Matrix} The denominator of x (in lowest terms) | ||
| */ | ||
| return typed(name, { | ||
| Fraction: x => x.d, | ||
| BigNumber: x => fraction(x).d, | ||
| 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) | ||
| }) | ||
| } | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { factory } from '../../utils/factory.js' | ||
| import { deepMap } from '../../utils/collection.js' | ||
|
|
||
| const name = 'num' | ||
| const dependencies = ['typed', 'fraction'] | ||
|
|
||
| export const createNum = /* #__PURE__ */ factory( | ||
| name, | ||
| dependencies, | ||
| ({ typed, fraction }) => { | ||
| /** | ||
| * Get the numerator of a fraction. | ||
| * For a fraction `a/b`, the function returns `a`. | ||
| * | ||
| * The result is always in lowest terms. For example, `num(fraction(8, 6))` | ||
| * returns `4n` because 8/6 simplifies to 4/3. | ||
| * | ||
| * For negative fractions like `-a/b` or `a/-b`, the sign is always | ||
| * included in the numerator. Both forms are normalized internally, so | ||
| * `num(fraction(-2, 3))` and `num(fraction(2, -3))` both return `-2`. | ||
| * | ||
| * For matrices, the function is evaluated element wise. | ||
| * | ||
| * Syntax: | ||
| * | ||
| * math.num(x) | ||
| * | ||
| * Examples: | ||
| * | ||
| * math.num(math.fraction(2, 3)) // returns 2n | ||
| * math.num(math.fraction(8, 6)) // returns 4n | ||
| * math.num(math.fraction('5/8')) // returns 5n | ||
| * math.num(math.fraction(-2, 3)) // returns -2n | ||
| * math.num(math.fraction(2, -3)) // returns -2n | ||
| * math.num(math.bignumber('0.5')) // returns 1n | ||
| * | ||
| * See also: | ||
| * | ||
| * den, fraction | ||
| * | ||
| * History: | ||
| * | ||
| * v15.2.0 Created | ||
| * | ||
| * @param {Fraction | BigNumber | Array | Matrix} x | ||
| * A fraction, BigNumber, or array with fractions | ||
| * @return {bigint | Array | Matrix} The numerator of x (in lowest terms) | ||
| */ | ||
| return typed(name, { | ||
| Fraction: x => x.s * x.n, | ||
| BigNumber: x => { | ||
| const f = fraction(x) | ||
| return f.s * f.n | ||
| }, | ||
| 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) | ||
| }) | ||
| } | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import assert from 'assert' | ||
| import math from '../../../../src/defaultInstance.js' | ||
|
|
||
| describe('den', function () { | ||
| it('should return the denominator of a fraction', function () { | ||
| assert.strictEqual(math.den(math.fraction(2, 3)), 3n) | ||
| assert.strictEqual(math.den(math.fraction(4, 8)), 2n) // simplified to 1/2n | ||
| assert.strictEqual(math.den(math.fraction(5, 1)), 1n) | ||
| }) | ||
|
|
||
| it('should return the denominator in lowest terms', function () { | ||
| assert.strictEqual(math.den(math.fraction(8, 6)), 3n) // 8/6 = 4/3 in lowest terms | ||
| assert.strictEqual(math.den(math.fraction(12, 9)), 3n) // 12/9 = 4/3 in lowest terms | ||
| assert.strictEqual(math.den(math.fraction(15, 10)), 2n) // 15/10 = 3/2 in lowest terms | ||
| assert.strictEqual(math.den(math.fraction(20, 15)), 3n) // 20/15 = 4/3 in lowest terms | ||
| }) | ||
|
|
||
| it('should return the denominator of a negative fraction', function () { | ||
| assert.strictEqual(math.den(math.fraction(-2, 3)), 3n) | ||
| assert.strictEqual(math.den(math.fraction(2, -3)), 3n) | ||
| assert.strictEqual(math.den(math.fraction(-2, -3)), 3n) | ||
| }) | ||
|
|
||
| it('should return the denominator of a fraction string', function () { | ||
| assert.strictEqual(math.den(math.fraction('2/3')), 3n) | ||
| assert.strictEqual(math.den(math.fraction('5/8')), 8n) | ||
| assert.strictEqual(math.den(math.fraction('-5/8')), 8n) | ||
| }) | ||
|
|
||
| it('should return the denominator for each element in a matrix', function () { | ||
| assert.deepStrictEqual( | ||
| math.den([math.fraction('2/3'), math.fraction('5/8')]), | ||
| [3n, 8n] | ||
| ) | ||
| assert.deepStrictEqual( | ||
| math | ||
| .den(math.matrix([math.fraction('2/3'), math.fraction('5/8')])) | ||
| .valueOf(), | ||
| [3n, 8n] | ||
| ) | ||
| }) | ||
|
|
||
| it('should return the denominator of a BigNumber by converting to fraction', function () { | ||
| assert.strictEqual(math.den(math.bignumber('1')), 1n) | ||
| assert.strictEqual(math.den(math.bignumber('0.5')), 2n) // 0.5 = 1/2 | ||
| assert.strictEqual(math.den(math.bignumber('0.25')), 4n) // 0.25 = 1/4 | ||
| assert.strictEqual(math.den(math.bignumber('0.125')), 8n) // 0.125 = 1/8 | ||
| assert.strictEqual(math.den(math.bignumber('-0.5')), 2n) // -0.5 = -1/2 | ||
| assert.strictEqual(math.den(math.bignumber('0.75')), 4n) // 0.75 = 3/4 | ||
| assert.strictEqual(math.den(math.bignumber('0.2')), 5n) // 0.2 = 1/5 | ||
| assert.strictEqual(math.den(math.bignumber('-0.2')), 5n) // -0.2 = -1/5 | ||
| }) | ||
|
|
||
| it('should return the denominator of a number by converting to fraction', function () { | ||
| assert.strictEqual(math.den(1), 1n) // 1 = 1/1 | ||
| assert.strictEqual(math.den(0.5), 2n) // 0.5 = 1/2 | ||
| assert.strictEqual(math.den(0.25), 4n) // 0.25 = 1/4 | ||
| assert.strictEqual(math.den(0.125), 8n) // 0.125 = 1/8 | ||
| assert.strictEqual(math.den(-0.5), 2n) // -0.5 = -1/2 | ||
| assert.strictEqual(math.den(0.75), 4n) // 0.75 = 3/4 | ||
| assert.strictEqual(math.den(0.2), 5n) // 0.2 = 1/5 | ||
| assert.strictEqual(math.den(-0.2), 5n) // -0.2 = -1/5 | ||
| assert.strictEqual(math.den(1.5), 2n) // 1.5 = 3/2 | ||
| }) | ||
|
|
||
| it('should return the denominator of a bigint by converting to fraction', function () { | ||
| assert.strictEqual(math.den(BigInt(1)), 1n) // 1 = 1/1 | ||
| assert.strictEqual(math.den(BigInt(3)), 1n) // 3 = 3/1 | ||
| assert.strictEqual(math.den(BigInt(-5)), 1n) // -5 = -5/1 | ||
| assert.strictEqual(math.den(BigInt(0)), 1n) // 0 = 0/1 | ||
| }) | ||
|
|
||
| it('should throw an error when called with an unsupported type of argument', function () { | ||
| assert.throws(function () { | ||
| math.den(new Date()) | ||
| }, /TypeError: Unexpected type of argument/) | ||
| assert.throws(function () { | ||
| math.den(math.complex(2, 3)) | ||
| }, /TypeError: Unexpected type of argument/) | ||
| }) | ||
|
|
||
| it('should throw an error in case of invalid number of arguments', function () { | ||
| assert.throws(function () { | ||
| math.den() | ||
| }, /TypeError: Too few arguments/) | ||
| assert.throws(function () { | ||
| math.den(math.fraction(1, 2), 2) | ||
| }, /TypeError: Too many arguments/) | ||
| }) | ||
|
|
||
| it('should LaTeX denominator', function () { | ||
| const expression = math.parse('den(fraction(1,2))') | ||
| assert.strictEqual( | ||
| expression.toTex(), | ||
| '\\mathrm{den}\\left(\\frac{1}{2}\\right)' | ||
| ) | ||
| }) | ||
gwhitney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And you can put the more specific
Matrix<bigint>type here, and add an example where you call num/den on an ordinary number or bigint. Thanks!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks! Just need to also put the
<bigint>into.toMatchTypeOf<MathArray>()in a couple of places above as well.