Skip to content

Conversation

@forfudan
Copy link
Owner

This pull request introduces the root function to the DeciMojo library, which calculates the n-th root of a Decimal value using the Newton-Raphson method. The most important changes include adding the root function, updating the Decimal struct to include the root method, and adding comprehensive tests for the new functionality.

New Functionality:

  • src/decimojo/exponential.mojo: Added the root function to calculate the n-th root of a Decimal value. This function includes special cases for handling different values of n and x, and uses the Newton-Raphson method for iteration.

Updates to Existing Code:

  • src/decimojo/decimal.mojo: Updated the Decimal struct to include the root method, which calls the new root function.
  • src/decimojo/__init__.mojo: Updated the import statements to include the new root function from the exponential module.

Testing:

  • tests/test_root.mojo: Added comprehensive tests for the root function, covering basic root calculations, fractional inputs, edge cases, error conditions, precision, and mathematical identities.

Configuration:

  • mojoproject.toml: Added commands for testing and benchmarking the new root function.

@forfudan forfudan merged commit 8ddf9df into main Mar 18, 2025
1 of 2 checks passed
@forfudan forfudan deleted the root branch March 18, 2025 19:40
@forfudan
Copy link
Owner Author

=== DeciMojo Root Function Benchmark ===
Time: 2025-03-18T20:42:17.914611
System: Darwin 24.3.0
Processor: arm
Python version: 3.12.9
Python decimal precision: 28
Mojo decimal precision: 28

Running root function benchmarks with 100 iterations each

Benchmark:       Square root of perfect square
Value:           9
Root:            2
Mojo result:     3
Python result:   3.000000000000000000000000000
Mojo root():     40.0 ns per iteration
Python root():   47910.0 ns per iteration
Speedup factor:  1197.75

Benchmark:       Square root of non-perfect square
Value:           2
Root:            2
Mojo result:     1.4142135623730950488016887242
Python result:   1.414213562373095048801688724
Mojo root():     1950.0 ns per iteration
Python root():   45900.0 ns per iteration
Speedup factor:  23.53846153846154

Benchmark:       Cube root of perfect cube
Value:           8
Root:            3
Mojo result:     2
Python result:   2.000000000000000000000000000
Mojo root():     550.0 ns per iteration
Python root():   47890.0 ns per iteration
Speedup factor:  87.07272727272728

Benchmark:       Cube root of non-perfect cube
Value:           10
Root:            3
Mojo result:     2.1544346900318837217592935665
Python result:   2.154434690031883721759293566
Mojo root():     2080.0 ns per iteration
Python root():   16960.0 ns per iteration
Speedup factor:  8.153846153846153

Benchmark:       Fourth root of perfect power
Value:           16
Root:            4
Mojo result:     2
Python result:   2.000000000000000000000000000
Mojo root():     760.0 ns per iteration
Python root():   47190.0 ns per iteration
Speedup factor:  62.0921052631579

Benchmark:       Fifth root of perfect power
Value:           32
Root:            5
Mojo result:     2
Python result:   2.000000000000000000000000000
Mojo root():     810.0 ns per iteration
Python root():   48090.0 ns per iteration
Speedup factor:  59.370370370370374

Benchmark:       Root of decimal < 1
Value:           0.25
Root:            2
Mojo result:     0.5
Python result:   0.5000000000000000000000000000
Mojo root():     40.0 ns per iteration
Python root():   44700.0 ns per iteration
Speedup factor:  1117.5

Benchmark:       Root of small decimal
Value:           0.0625
Root:            4
Mojo result:     0.5
Python result:   0.5000000000000000000000000000
Mojo root():     7000.0 ns per iteration
Python root():   45950.0 ns per iteration
Speedup factor:  6.564285714285714

Benchmark:       High precision decimal
Value:           2.7182818284590452353602874
Root:            2
Mojo result:     1.6487212707001281468486507662
Python result:   1.648721270700128146848650766
Mojo root():     1560.0 ns per iteration
Python root():   44670.0 ns per iteration
Speedup factor:  28.634615384615383

Benchmark:       Large integer
Value:           1000000
Root:            2
Mojo result:     1000
Python result:   1000.000000000000000000000000
Mojo root():     50.0 ns per iteration
Python root():   17260.0 ns per iteration
Speedup factor:  345.2

Benchmark:       Large root
Value:           10
Root:            100
Mojo result:     1.0232929922807541309662751747
Python result:   1.023292992280754130966275175
Mojo root():     3670.0 ns per iteration
Python root():   11330.0 ns per iteration
Speedup factor:  3.087193460490463

Benchmark:       Odd root of negative number
Value:           -27
Root:            3
Note: Python doesn't directly support odd roots of negative numbers.
      Using abs() and then negating the result for comparison.
Mojo result:     -3
Python result:   -3.000000000000000000000000000
Mojo root():     530.0 ns per iteration
Python root():   50050.0 ns per iteration
Speedup factor:  94.43396226415095

Benchmark:       Root of 1 (any root)
Value:           1
Root:            7
Mojo result:     1
Python result:   1.000000000000000000000000000
Mojo root():     10.0 ns per iteration
Python root():   3830.0 ns per iteration
Speedup factor:  383.0

Benchmark:       Root of 0
Value:           0
Root:            3
Mojo result:     0
Python result:   0
Mojo root():     10.0 ns per iteration
Python root():   3870.0 ns per iteration
Speedup factor:  387.0

Benchmark:       Custom decimal
Value:           123.456
Root:            2
Mojo result:     11.111075555498666484621494041
Python result:   11.11107555549866648462149404
Mojo root():     1450.0 ns per iteration
Python root():   45590.0 ns per iteration
Speedup factor:  31.44137931034483

=== Root Function Benchmark Summary ===
Benchmarked:      15 different root() cases
Each case ran:    100 iterations
Average speedup:  255.65592978216335×

Individual speedup factors:
Case 1: 1197.75×
Case 2: 23.54×
Case 3: 87.07×
Case 4: 8.15×
Case 5: 62.09×
Case 6: 59.37×
Case 7: 1117.5×
Case 8: 6.56×
Case 9: 28.63×
Case 10: 345.2×
Case 11: 3.09×
Case 12: 94.43×
Case 13: 383.0×
Case 14: 387.0×
Case 15: 31.44×

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants