Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
53b9596
fix format
shivasankarka Mar 8, 2026
dacc868
clean up optimzie
shivasankarka Mar 13, 2026
6b94d37
fix format
shivasankarka Mar 13, 2026
0c8def6
fix t string error
shivasankarka Mar 13, 2026
1363a30
add root_scalar tests
shivasankarka Mar 13, 2026
75d2fd2
clean up optimzie
shivasankarka Mar 13, 2026
be7457e
fix format
shivasankarka Mar 13, 2026
aabe7e1
remove mismatched docstring
shivasankarka Mar 13, 2026
5edc90e
add min_scalar.mojo
shivasankarka Mar 13, 2026
061d7f5
added prelude
shivasankarka Mar 13, 2026
66aa088
fix format, clean up interpolate
shivasankarka Mar 13, 2026
f2725ef
Update utility.mojo
shivasankarka Mar 13, 2026
2b5f706
Update root_scalar.mojo
shivasankarka Mar 13, 2026
bc51a2f
fix docstrings, clean up the integration module
shivasankarka Mar 13, 2026
9682d58
clean up fft
shivasankarka Mar 13, 2026
2d3414b
fix docstring in min_scalar
shivasankarka Mar 13, 2026
6012101
Update fastfourier.mojo
shivasankarka Mar 13, 2026
0708dcd
clean up optimzie
shivasankarka Mar 13, 2026
c8ab200
add min_scalar.mojo
shivasankarka Mar 13, 2026
ddd8d26
fix format, clean up interpolate
shivasankarka Mar 13, 2026
802d3b2
update the differentiate module
shivasankarka Mar 13, 2026
06fcbd2
clean up integrate module
shivasankarka Mar 13, 2026
60e5ca6
rename to deriv, jacob to resolve mojo compiler conflict
shivasankarka Mar 13, 2026
e95796c
fix tests
shivasankarka Mar 13, 2026
515ba58
Update min_scalar.mojo
shivasankarka Mar 13, 2026
82ab0be
Merge branch 'v0.2' into clean_up
shivasankarka Mar 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions scijo/differentiate/__init__.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"""Differentiate Module (scijo.differentiate)

The `differentiate` module provides tools for numerical differentiation and gradient computation.
It includes functions for calculating derivatives, Jacobians, and other related operations essential for scientific computing tasks such as optimization, sensitivity analysis, and solving differential equations.
It includes functions for calculating derivatives, Jacobians, with much more to come in the future.
"""
from .derivative import derivative
from .jacobian import jacobian

from .deriv import derivative
from .jacob import jacobian
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ References:
https://en.wikipedia.org/wiki/Finite_difference_coefficient
"""

from numojo.prelude import *

from .utility import (
DiffResult,
Expand All @@ -27,6 +26,11 @@ from .utility import (
)


# ===----------------------------------------------------------------------=== #
# Derivative
# ===----------------------------------------------------------------------=== #


fn derivative[
dtype: DType,
func: fn[dtype: DType](
Expand Down Expand Up @@ -72,6 +76,17 @@ fn derivative[
Raises:
Error: If step_direction is not in {-1, 0, 1}.
Error: If the specified order is not supported for the chosen method.

Examples:
```mojo
from scijo.differentiate import derivative
from scijo.prelude import *

fn f[dtype: DType](x: Scalar[dtype], args: Optional[List[Scalar[dtype]]]) -> Scalar[dtype]:
return x * x

var res = derivative[f64, f](1.0)
```
"""

@parameter
Expand Down Expand Up @@ -128,6 +143,11 @@ fn derivative[
)


# ===----------------------------------------------------------------------=== #
# Internal methods
# ===----------------------------------------------------------------------=== #


fn _derivative_central_difference[
dtype: DType,
func: fn[dtype: DType](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,25 @@ fn jacobian[
tolerances: Tolerance dictionary with "abs" and "rel" keys (reserved for future use).
maxiter: Maximum iterations (reserved for future use).

Raises:
Error: If function evaluation fails for any perturbation.

Returns:
NDArray[dtype] of shape (m, n) representing the Jacobian matrix,
where m is the output dimension and n is the input dimension.

Raises:
Error: If function evaluation fails for any perturbation.
Examples:
```mojo
import numojo as nm
from scijo.differentiate import jacobian
from scijo.prelude import *

fn f[dtype: DType](x: NDArray[dtype], args: Optional[List[Scalar[dtype]]]) raises -> NDArray[dtype]:
return x * x

var x = nm.array[f64]([1.0, 2.0])
var J = jacobian[f64, f](x)
```
"""
var n: Int = len(x)
var f0: NDArray[dtype] = f(x, args)
Expand Down
9 changes: 9 additions & 0 deletions scijo/differentiate/utility.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ References:
Arbitrarily Spaced Grids. Mathematics of Computation, 51(184), 699-706.
"""

# ===----------------------------------------------------------------------=== #
# Result types
# ===----------------------------------------------------------------------=== #


struct DiffResult[dtype: DType](ImplicitlyCopyable, Writable):
"""Result structure for numerical differentiation operations.
Expand Down Expand Up @@ -88,6 +92,11 @@ struct DiffResult[dtype: DType](ImplicitlyCopyable, Writable):
writer.write("Error displaying Result: " + String(e) + "\n")


# ===----------------------------------------------------------------------=== #
# Finite difference tables
# ===----------------------------------------------------------------------=== #


@parameter
fn generate_central_finite_difference_table[
dtype: DType
Expand Down
1 change: 1 addition & 0 deletions scijo/integrate/fixed_sample.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import numojo as nm
# Trapezoid
# ===----------------------------------------------------------------------=== #


fn trapezoid[
dtype: DType
](
Expand Down
22 changes: 7 additions & 15 deletions scijo/optimize/min_scalar.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ fn _brent_minimize[
var tmpx = a
var tmpf = fa
a = b
fa = fb
b = tmpx
fb = tmpf

Expand All @@ -113,7 +112,6 @@ fn _brent_minimize[
var bracket_iter = 0
while fb > fc and bracket_iter < 50:
a = b
fa = fb
b = c
fb = fc
c = b + (b - a) * Scalar[dtype](_phi)
Expand Down Expand Up @@ -151,13 +149,10 @@ fn _brent_minimize[
nfev=nfev,
)

var p: Scalar[dtype] = 0
var q: Scalar[dtype] = 0
var r: Scalar[dtype] = 0
if abs(e) > tol1:
r = (x - w) * (fx - fv)
q = (x - v) * (fx - fw)
p = (x - v) * q - (x - w) * r
var r = (x - w) * (fx - fv)
var q = (x - v) * (fx - fw)
var p = (x - v) * q - (x - w) * r
q = (q - r) * 2
if q > 0:
p = -p
Expand All @@ -167,7 +162,7 @@ fn _brent_minimize[
and p > q * (a - x)
and p < q * (c - x)
):
d = p / q
var d = p / q
var u1 = x + d
if (u1 - a) < tol2 or (c - u1) < tol2:
d = tol1 if x < m else -tol1
Expand Down Expand Up @@ -336,13 +331,10 @@ fn _bounded_minimize[
nfev=nfev,
)

var p: Scalar[dtype] = 0
var q: Scalar[dtype] = 0
var r: Scalar[dtype] = 0
if abs(e) > tol1:
r = (x - w) * (fx - fv)
q = (x - v) * (fx - fw)
p = (x - v) * q - (x - w) * r
var r = (x - w) * (fx - fv)
var q = (x - v) * (fx - fw)
var p = (x - v) * q - (x - w) * r
q = (q - r) * 2
if q > 0:
p = -p
Expand Down
34 changes: 9 additions & 25 deletions scijo/optimize/root_scalar.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ methods (secant).
# Root scalar
# ===----------------------------------------------------------------------=== #


fn root_scalar[
dtype: DType,
f: fn[dtype: DType](
Expand Down Expand Up @@ -70,37 +69,29 @@ fn root_scalar[
if method == "newton":
if not fprime:
raise Error(
"Scijo [root_scalar]: Derivative fprime must be provided for"
" Newton's method."
"Scijo [root_scalar]: Derivative fprime must be provided for Newton's method."
)
return newton[dtype, f, fprime.value()](args, x0, xtol, rtol, maxiter)
elif method == "bisect":
if not bracket:
raise Error(
"Scijo [root_scalar]: Bracket must be provided for bisection"
" method."
)
raise Error("Scijo [root_scalar]: Bracket must be provided for bisection method.")
return bisect[dtype, f](args, bracket.value(), xtol, rtol, maxiter)
elif method == "secant":
if not (x0 and x1):
raise Error(
"Scijo [root_scalar]: Initial guesses x0 and x1 must be"
" provided for secant method."
"Scijo [root_scalar]: Initial guesses x0 and x1 must be provided for secant method."
)
return secant[dtype, f](
args, x0.value(), x1.value(), xtol, rtol, maxiter
)
else:
raise Error(
"Scijo [root_scalar]: Unsupported method: " + String(method)
)
raise Error("Scijo [root_scalar]: Unsupported method: " + String(method))


# ===----------------------------------------------------------------------=== #
# Root scalar methods
# ===----------------------------------------------------------------------=== #


fn newton[
dtype: DType,
f: fn[dtype: DType](
Expand Down Expand Up @@ -145,19 +136,15 @@ fn newton[
if x0:
xn = x0.value()
else:
raise Error(
"Scijo [newton]: Initial guess x0 must be provided for Newton's"
" method."
)
raise Error("Scijo [newton]: Initial guess x0 must be provided for Newton's method.")

for _ in range(maxiter):
var fx = f(xn, args)
var fpx = fprime(xn, args)

if fpx == 0:
raise Error(
"Scijo [newton]: Derivative is zero. Newton-Raphson step would"
" divide by zero."
"Scijo [newton]: Derivative is zero. Newton-Raphson step would divide by zero."
)

var delta = fx / fpx
Expand Down Expand Up @@ -224,8 +211,8 @@ fn bisect[

if fa * fb > 0:
raise Error(
"Scijo [newton]: f(a) and f(b) must have opposite signs (bracket"
" does not enclose a root)."
"Scijo [newton]: f(a) and f(b) must have opposite signs (bracket does not enclose a"
" root)."
)

for _ in range(maxiter):
Expand Down Expand Up @@ -294,10 +281,7 @@ fn secant[

var denom = f1 - f0
if denom == 0:
raise Error(
"Scijo [newton]: Secant method encountered zero slope (f1 - f0"
" == 0)."
)
raise Error("Scijo [newton]: Secant method encountered zero slope (f1 - f0 == 0).")

var xn = b - (f1 * (b - a)) / denom

Expand Down
1 change: 0 additions & 1 deletion scijo/optimize/utility.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Data structures for returning results from optimization and root-finding routine
# RootResults
# ===----------------------------------------------------------------------=== #


struct RootResults[dtype: DType = DType.float64]():
"""Result structure for scalar root-finding operations.

Expand Down
2 changes: 1 addition & 1 deletion tests/test_differentiate.mojo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from scijo.differentiate.derivative import derivative
from scijo.differentiate import derivative

from testing import assert_almost_equal, assert_equal, assert_true, assert_false
from testing import TestSuite
Expand Down
2 changes: 1 addition & 1 deletion tests/test_quad.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from testing import assert_almost_equal, assert_equal, assert_true, assert_false
from testing import TestSuite
from math import sin, cos, exp, log, pi, sqrt

from scijo.integrate.quad import quad
from scijo.integrate import quad
import scijo as sj


Expand Down
Loading